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