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