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