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