]> begriffs open source - freertos/blob - Demo/Common/Minimal/blocktim.c
Update to V4.7.1
[freertos] / Demo / Common / Minimal / blocktim.c
1 /*\r
2         FreeRTOS.org V4.7.1 - Copyright (C) 2003-2008 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27 \r
28         Please ensure to read the configuration and relevant port sections of the \r
29         online documentation.\r
30 \r
31         +++ http://www.FreeRTOS.org +++\r
32         Documentation, latest information, license and contact details.  \r
33 \r
34         +++ http://www.SafeRTOS.com +++\r
35         A version that is certified for use in safety critical systems.\r
36 \r
37         +++ http://www.OpenRTOS.com +++\r
38         Commercial support, development, porting, licensing and training services.\r
39 \r
40         ***************************************************************************\r
41 */\r
42 \r
43 /*\r
44  * This file contains some test scenarios that ensure tasks do not exit queue\r
45  * send or receive functions prematurely.  A description of the tests is\r
46  * included within the code.\r
47  */\r
48 \r
49 /* Kernel includes. */\r
50 #include "FreeRTOS.h"\r
51 #include "task.h"\r
52 #include "queue.h"\r
53 \r
54 /* Demo includes. */\r
55 #include "blocktim.h"\r
56 \r
57 /* Task priorities. */\r
58 #define bktPRIMARY_PRIORITY                     ( 3 )\r
59 #define bktSECONDARY_PRIORITY           ( 2 )\r
60 \r
61 /* Task behaviour. */\r
62 #define bktQUEUE_LENGTH                         ( 5 )\r
63 #define bktSHORT_WAIT                           ( ( ( portTickType ) 20 ) / portTICK_RATE_MS )\r
64 #define bktPRIMARY_BLOCK_TIME           ( 10 )\r
65 #define bktALLOWABLE_MARGIN                     ( 12 )\r
66 #define bktTIME_TO_BLOCK                        ( 175 )\r
67 #define bktDONT_BLOCK                           ( ( portTickType ) 0 )\r
68 #define bktRUN_INDICATOR                        ( ( unsigned portBASE_TYPE ) 0x55 )\r
69 \r
70 /* The queue on which the tasks block. */\r
71 static xQueueHandle xTestQueue;\r
72 \r
73 /* Handle to the secondary task is required by the primary task for calls\r
74 to vTaskSuspend/Resume(). */\r
75 static xTaskHandle xSecondary;\r
76 \r
77 /* Used to ensure that tasks are still executing without error. */\r
78 static portBASE_TYPE xPrimaryCycles = 0, xSecondaryCycles = 0;\r
79 static portBASE_TYPE xErrorOccurred = pdFALSE;\r
80 \r
81 /* Provides a simple mechanism for the primary task to know when the\r
82 secondary task has executed. */\r
83 static volatile unsigned portBASE_TYPE xRunIndicator;\r
84 \r
85 /* The two test tasks.  Their behaviour is commented within the files. */\r
86 static void vPrimaryBlockTimeTestTask( void *pvParameters );\r
87 static void vSecondaryBlockTimeTestTask( void *pvParameters );\r
88 \r
89 /*-----------------------------------------------------------*/\r
90 \r
91 void vCreateBlockTimeTasks( void )\r
92 {\r
93         /* Create the queue on which the two tasks block. */\r
94     xTestQueue = xQueueCreate( bktQUEUE_LENGTH, sizeof( portBASE_TYPE ) );\r
95 \r
96         /* Create the two test tasks. */\r
97         xTaskCreate( vPrimaryBlockTimeTestTask, ( signed portCHAR * )"BTest1", configMINIMAL_STACK_SIZE, NULL, bktPRIMARY_PRIORITY, NULL );\r
98         xTaskCreate( vSecondaryBlockTimeTestTask, ( signed portCHAR * )"BTest2", configMINIMAL_STACK_SIZE, NULL, bktSECONDARY_PRIORITY, &xSecondary );\r
99 }\r
100 /*-----------------------------------------------------------*/\r
101 \r
102 static void vPrimaryBlockTimeTestTask( void *pvParameters )\r
103 {\r
104 portBASE_TYPE xItem, xData;\r
105 portTickType xTimeWhenBlocking;\r
106 portTickType xTimeToBlock, xBlockedTime;\r
107 \r
108         ( void ) pvParameters;\r
109 \r
110         for( ;; )\r
111         {\r
112                 /*********************************************************************\r
113         Test 1\r
114 \r
115         Simple block time wakeup test on queue receives. */\r
116                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
117                 {\r
118                         /* The queue is empty. Attempt to read from the queue using a block\r
119                         time.  When we wake, ensure the delta in time is as expected. */\r
120                         xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;\r
121 \r
122                         /* A critical section is used to minimise the jitter in the time\r
123                         measurements. */\r
124                         portENTER_CRITICAL();\r
125                         {\r
126                                 xTimeWhenBlocking = xTaskGetTickCount();\r
127                                 \r
128                                 /* We should unblock after xTimeToBlock having not received\r
129                                 anything on the queue. */\r
130                                 if( xQueueReceive( xTestQueue, &xData, xTimeToBlock ) != errQUEUE_EMPTY )\r
131                                 {\r
132                                         xErrorOccurred = pdTRUE;\r
133                                 }\r
134 \r
135                                 /* How long were we blocked for? */\r
136                                 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;\r
137                         }\r
138                         portEXIT_CRITICAL();\r
139 \r
140                         if( xBlockedTime < xTimeToBlock )\r
141                         {\r
142                                 /* Should not have blocked for less than we requested. */\r
143                                 xErrorOccurred = pdTRUE;\r
144                         }\r
145 \r
146                         if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )\r
147                         {\r
148                                 /* Should not have blocked for longer than we requested,\r
149                                 although we would not necessarily run as soon as we were\r
150                                 unblocked so a margin is allowed. */\r
151                                 xErrorOccurred = pdTRUE;\r
152                         }\r
153                 }\r
154 \r
155                 /*********************************************************************\r
156         Test 2\r
157 \r
158         Simple block time wakeup test on queue sends.\r
159 \r
160                 First fill the queue.  It should be empty so all sends should pass. */\r
161                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
162                 {\r
163                         if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )\r
164                         {\r
165                                 xErrorOccurred = pdTRUE;\r
166                         }\r
167 \r
168                         #if configUSE_PREEMPTION == 0\r
169                                 taskYIELD();\r
170                         #endif\r
171                 }\r
172 \r
173                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
174                 {\r
175                         /* The queue is full. Attempt to write to the queue using a block\r
176                         time.  When we wake, ensure the delta in time is as expected. */\r
177                         xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;\r
178 \r
179                         portENTER_CRITICAL();\r
180                         {\r
181                                 xTimeWhenBlocking = xTaskGetTickCount();\r
182                                 \r
183                                 /* We should unblock after xTimeToBlock having not received\r
184                                 anything on the queue. */\r
185                                 if( xQueueSend( xTestQueue, &xItem, xTimeToBlock ) != errQUEUE_FULL )\r
186                                 {\r
187                                         xErrorOccurred = pdTRUE;\r
188                                 }\r
189 \r
190                                 /* How long were we blocked for? */\r
191                                 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;\r
192                         }\r
193                         portEXIT_CRITICAL();\r
194 \r
195                         if( xBlockedTime < xTimeToBlock )\r
196                         {\r
197                                 /* Should not have blocked for less than we requested. */\r
198                                 xErrorOccurred = pdTRUE;\r
199                         }\r
200 \r
201                         if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )\r
202                         {\r
203                                 /* Should not have blocked for longer than we requested,\r
204                                 although we would not necessarily run as soon as we were\r
205                                 unblocked so a margin is allowed. */\r
206                                 xErrorOccurred = pdTRUE;\r
207                         }\r
208                 }\r
209 \r
210                 /*********************************************************************\r
211         Test 3\r
212 \r
213                 Wake the other task, it will block attempting to post to the queue.\r
214                 When we read from the queue the other task will wake, but before it\r
215                 can run we will post to the queue again.  When the other task runs it\r
216                 will find the queue still full, even though it was woken.  It should\r
217                 recognise that its block time has not expired and return to block for\r
218                 the remains of its block time.\r
219 \r
220                 Wake the other task so it blocks attempting to post to the already\r
221                 full queue. */\r
222                 xRunIndicator = 0;\r
223                 vTaskResume( xSecondary );\r
224 \r
225                 /* We need to wait a little to ensure the other task executes. */\r
226                 while( xRunIndicator != bktRUN_INDICATOR )\r
227                 {\r
228                         /* The other task has not yet executed. */\r
229                         vTaskDelay( bktSHORT_WAIT );\r
230                 }\r
231                 /* Make sure the other task is blocked on the queue. */\r
232                 vTaskDelay( bktSHORT_WAIT );\r
233                 xRunIndicator = 0;\r
234 \r
235                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
236                 {\r
237                         /* Now when we make space on the queue the other task should wake\r
238                         but not execute as this task has higher priority. */                            \r
239                         if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )\r
240                         {\r
241                                 xErrorOccurred = pdTRUE;\r
242                         }\r
243 \r
244                         /* Now fill the queue again before the other task gets a chance to\r
245                         execute.  If the other task had executed we would find the queue\r
246                         full ourselves, and the other task have set xRunIndicator. */\r
247                         if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )\r
248                         {\r
249                                 xErrorOccurred = pdTRUE;\r
250                         }\r
251 \r
252                         if( xRunIndicator == bktRUN_INDICATOR )\r
253                         {\r
254                                 /* The other task should not have executed. */\r
255                                 xErrorOccurred = pdTRUE;\r
256                         }\r
257 \r
258                         /* Raise the priority of the other task so it executes and blocks\r
259                         on the queue again. */\r
260                         vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );\r
261 \r
262                         /* The other task should now have re-blocked without exiting the\r
263                         queue function. */\r
264                         if( xRunIndicator == bktRUN_INDICATOR )\r
265                         {\r
266                                 /* The other task should not have executed outside of the\r
267                                 queue function. */\r
268                                 xErrorOccurred = pdTRUE;\r
269                         }\r
270 \r
271                         /* Set the priority back down. */\r
272                         vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );                  \r
273                 }\r
274 \r
275                 /* Let the other task timeout.  When it unblockes it will check that it\r
276                 unblocked at the correct time, then suspend itself. */\r
277                 while( xRunIndicator != bktRUN_INDICATOR )\r
278                 {\r
279                         vTaskDelay( bktSHORT_WAIT );\r
280                 }\r
281                 vTaskDelay( bktSHORT_WAIT );\r
282                 xRunIndicator = 0;\r
283 \r
284 \r
285                 /*********************************************************************\r
286         Test 4\r
287 \r
288                 As per test 3 - but with the send and receive the other way around.\r
289                 The other task blocks attempting to read from the queue.\r
290 \r
291                 Empty the queue.  We should find that it is full. */\r
292                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
293                 {\r
294                         if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )\r
295                         {\r
296                                 xErrorOccurred = pdTRUE;\r
297                         }\r
298                 }\r
299                 \r
300                 /* Wake the other task so it blocks attempting to read from  the\r
301                 already empty queue. */\r
302                 vTaskResume( xSecondary );\r
303 \r
304                 /* We need to wait a little to ensure the other task executes. */\r
305                 while( xRunIndicator != bktRUN_INDICATOR )\r
306                 {\r
307                         vTaskDelay( bktSHORT_WAIT );\r
308                 }\r
309                 vTaskDelay( bktSHORT_WAIT );\r
310                 xRunIndicator = 0;\r
311 \r
312                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
313                 {\r
314                         /* Now when we place an item on the queue the other task should\r
315                         wake but not execute as this task has higher priority. */                               \r
316                         if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )\r
317                         {\r
318                                 xErrorOccurred = pdTRUE;\r
319                         }\r
320 \r
321                         /* Now empty the queue again before the other task gets a chance to\r
322                         execute.  If the other task had executed we would find the queue\r
323                         empty ourselves, and the other task would be suspended. */\r
324                         if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )\r
325                         {\r
326                                 xErrorOccurred = pdTRUE;\r
327                         }\r
328 \r
329                         if( xRunIndicator == bktRUN_INDICATOR )\r
330                         {\r
331                                 /* The other task should not have executed. */\r
332                                 xErrorOccurred = pdTRUE;\r
333                         }\r
334 \r
335                         /* Raise the priority of the other task so it executes and blocks\r
336                         on the queue again. */\r
337                         vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );\r
338 \r
339                         /* The other task should now have re-blocked without exiting the\r
340                         queue function. */\r
341                         if( xRunIndicator == bktRUN_INDICATOR )\r
342                         {\r
343                                 /* The other task should not have executed outside of the\r
344                                 queue function. */\r
345                                 xErrorOccurred = pdTRUE;\r
346                         }\r
347                         vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );                  \r
348                 }\r
349 \r
350                 /* Let the other task timeout.  When it unblockes it will check that it\r
351                 unblocked at the correct time, then suspend itself. */\r
352                 while( xRunIndicator != bktRUN_INDICATOR )\r
353                 {\r
354                         vTaskDelay( bktSHORT_WAIT );\r
355                 }\r
356                 vTaskDelay( bktSHORT_WAIT );\r
357 \r
358                 xPrimaryCycles++;\r
359         }\r
360 }\r
361 /*-----------------------------------------------------------*/\r
362 \r
363 static void vSecondaryBlockTimeTestTask( void *pvParameters )\r
364 {\r
365 portTickType xTimeWhenBlocking, xBlockedTime;\r
366 portBASE_TYPE xData;\r
367 \r
368         ( void ) pvParameters;\r
369 \r
370         for( ;; )\r
371         {\r
372                 /*********************************************************************\r
373         Test 1 and 2\r
374 \r
375                 This task does does not participate in these tests. */\r
376                 vTaskSuspend( NULL );\r
377 \r
378                 /*********************************************************************\r
379         Test 3\r
380 \r
381                 The first thing we do is attempt to read from the queue.  It should be\r
382                 full so we block.  Note the time before we block so we can check the\r
383                 wake time is as per that expected. */\r
384                 portENTER_CRITICAL();\r
385                 {\r
386                         xTimeWhenBlocking = xTaskGetTickCount();\r
387                         \r
388                         /* We should unblock after bktTIME_TO_BLOCK having not received\r
389                         anything on the queue. */\r
390                         xData = 0;\r
391                         xRunIndicator = bktRUN_INDICATOR;\r
392                         if( xQueueSend( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_FULL )\r
393                         {\r
394                                 xErrorOccurred = pdTRUE;\r
395                         }\r
396 \r
397                         /* How long were we inside the send function? */\r
398                         xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;\r
399                 }\r
400                 portEXIT_CRITICAL();\r
401 \r
402                 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */\r
403                 if( xBlockedTime < bktTIME_TO_BLOCK )\r
404                 {\r
405                         xErrorOccurred = pdTRUE;\r
406                 }\r
407 \r
408                 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN\r
409                 either.  A margin is permitted as we would not necessarily run as\r
410                 soon as we unblocked. */\r
411                 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )\r
412                 {\r
413                         xErrorOccurred = pdTRUE;\r
414                 }\r
415 \r
416                 /* Suspend ready for test 3. */\r
417                 xRunIndicator = bktRUN_INDICATOR;\r
418                 vTaskSuspend( NULL );\r
419 \r
420                 /*********************************************************************\r
421         Test 4\r
422 \r
423                 As per test three, but with the send and receive reversed. */\r
424                 portENTER_CRITICAL();\r
425                 {\r
426                         xTimeWhenBlocking = xTaskGetTickCount();\r
427                         \r
428                         /* We should unblock after bktTIME_TO_BLOCK having not received\r
429                         anything on the queue. */\r
430                         xRunIndicator = bktRUN_INDICATOR;\r
431                         if( xQueueReceive( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_EMPTY )\r
432                         {\r
433                                 xErrorOccurred = pdTRUE;\r
434                         }\r
435 \r
436                         xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;\r
437                 }\r
438                 portEXIT_CRITICAL();\r
439 \r
440                 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */\r
441                 if( xBlockedTime < bktTIME_TO_BLOCK )\r
442                 {\r
443                         xErrorOccurred = pdTRUE;\r
444                 }\r
445 \r
446                 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN\r
447                 either.  A margin is permitted as we would not necessarily run as soon\r
448                 as we unblocked. */\r
449                 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )\r
450                 {\r
451                         xErrorOccurred = pdTRUE;\r
452                 }\r
453 \r
454                 xRunIndicator = bktRUN_INDICATOR;\r
455 \r
456                 xSecondaryCycles++;\r
457         }\r
458 }\r
459 /*-----------------------------------------------------------*/\r
460 \r
461 portBASE_TYPE xAreBlockTimeTestTasksStillRunning( void )\r
462 {\r
463 static portBASE_TYPE xLastPrimaryCycleCount = 0, xLastSecondaryCycleCount = 0;\r
464 portBASE_TYPE xReturn = pdPASS;\r
465 \r
466         /* Have both tasks performed at least one cycle since this function was\r
467         last called? */\r
468         if( xPrimaryCycles == xLastPrimaryCycleCount )\r
469         {\r
470                 xReturn = pdFAIL;\r
471         }\r
472 \r
473         if( xSecondaryCycles == xLastSecondaryCycleCount )\r
474         {\r
475                 xReturn = pdFAIL;\r
476         }\r
477 \r
478         if( xErrorOccurred == pdTRUE )\r
479         {\r
480                 xReturn = pdFAIL;\r
481         }\r
482 \r
483         xLastSecondaryCycleCount = xSecondaryCycles;\r
484         xLastPrimaryCycleCount = xPrimaryCycles;\r
485 \r
486         return xReturn;\r
487 }\r