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