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