]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/dynamic.c
Improve the error detection in some of the standard demo tasks.
[freertos] / FreeRTOS / Demo / Common / Minimal / dynamic.c
1 /*\r
2     FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
67     Integrity Systems, who sell the code with commercial support,\r
68     indemnification and middleware, under the OpenRTOS brand.\r
69 \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
71     engineered and independently SIL3 certified version for use in safety and\r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  * The first test creates three tasks - two counter tasks (one continuous count\r
77  * and one limited count) and one controller.  A "count" variable is shared\r
78  * between all three tasks.  The two counter tasks should never be in a "ready"\r
79  * state at the same time.  The controller task runs at the same priority as\r
80  * the continuous count task, and at a lower priority than the limited count\r
81  * task.\r
82  *\r
83  * One counter task loops indefinitely, incrementing the shared count variable\r
84  * on each iteration.  To ensure it has exclusive access to the variable it\r
85  * raises it's priority above that of the controller task before each\r
86  * increment, lowering it again to it's original priority before starting the\r
87  * next iteration.\r
88  *\r
89  * The other counter task increments the shared count variable on each\r
90  * iteration of it's loop until the count has reached a limit of 0xff - at\r
91  * which point it suspends itself.  It will not start a new loop until the\r
92  * controller task has made it "ready" again by calling vTaskResume ().\r
93  * This second counter task operates at a higher priority than controller\r
94  * task so does not need to worry about mutual exclusion of the counter\r
95  * variable.\r
96  *\r
97  * The controller task is in two sections.  The first section controls and\r
98  * monitors the continuous count task.  When this section is operational the\r
99  * limited count task is suspended.  Likewise, the second section controls\r
100  * and monitors the limited count task.  When this section is operational the\r
101  * continuous count task is suspended.\r
102  *\r
103  * In the first section the controller task first takes a copy of the shared\r
104  * count variable.  To ensure mutual exclusion on the count variable it\r
105  * suspends the continuous count task, resuming it again when the copy has been\r
106  * taken.  The controller task then sleeps for a fixed period - during which\r
107  * the continuous count task will execute and increment the shared variable.\r
108  * When the controller task wakes it checks that the continuous count task\r
109  * has executed by comparing the copy of the shared variable with its current\r
110  * value.  This time, to ensure mutual exclusion, the scheduler itself is\r
111  * suspended with a call to vTaskSuspendAll ().  This is for demonstration\r
112  * purposes only and is not a recommended technique due to its inefficiency.\r
113  *\r
114  * After a fixed number of iterations the controller task suspends the\r
115  * continuous count task, and moves on to its second section.\r
116  *\r
117  * At the start of the second section the shared variable is cleared to zero.\r
118  * The limited count task is then woken from it's suspension by a call to\r
119  * vTaskResume ().  As this counter task operates at a higher priority than\r
120  * the controller task the controller task should not run again until the\r
121  * shared variable has been counted up to the limited value causing the counter\r
122  * task to suspend itself.  The next line after vTaskResume () is therefore\r
123  * a check on the shared variable to ensure everything is as expected.\r
124  *\r
125  *\r
126  * The second test consists of a couple of very simple tasks that post onto a\r
127  * queue while the scheduler is suspended.  This test was added to test parts\r
128  * of the scheduler not exercised by the first test.\r
129  *\r
130  */\r
131 \r
132 #include <stdlib.h>\r
133 \r
134 /* Scheduler include files. */\r
135 #include "FreeRTOS.h"\r
136 #include "task.h"\r
137 #include "semphr.h"\r
138 \r
139 /* Demo app include files. */\r
140 #include "dynamic.h"\r
141 \r
142 /* Function that implements the "limited count" task as described above. */\r
143 static portTASK_FUNCTION_PROTO( vLimitedIncrementTask, pvParameters );\r
144 \r
145 /* Function that implements the "continuous count" task as described above. */\r
146 static portTASK_FUNCTION_PROTO( vContinuousIncrementTask, pvParameters );\r
147 \r
148 /* Function that implements the controller task as described above. */\r
149 static portTASK_FUNCTION_PROTO( vCounterControlTask, pvParameters );\r
150 \r
151 static portTASK_FUNCTION_PROTO( vQueueReceiveWhenSuspendedTask, pvParameters );\r
152 static portTASK_FUNCTION_PROTO( vQueueSendWhenSuspendedTask, pvParameters );\r
153 \r
154 /* Demo task specific constants. */\r
155 #define priSTACK_SIZE                           ( configMINIMAL_STACK_SIZE )\r
156 #define priSLEEP_TIME                           ( ( portTickType ) 128 / portTICK_RATE_MS )\r
157 #define priLOOPS                                        ( 5 )\r
158 #define priMAX_COUNT                            ( ( unsigned long ) 0xff )\r
159 #define priNO_BLOCK                                     ( ( portTickType ) 0 )\r
160 #define priSUSPENDED_QUEUE_LENGTH       ( 1 )\r
161 \r
162 /*-----------------------------------------------------------*/\r
163 \r
164 /* Handles to the two counter tasks.  These could be passed in as parameters\r
165 to the controller task to prevent them having to be file scope. */\r
166 static xTaskHandle xContinousIncrementHandle, xLimitedIncrementHandle;\r
167 \r
168 /* The shared counter variable.  This is passed in as a parameter to the two\r
169 counter variables for demonstration purposes. */\r
170 static unsigned long ulCounter;\r
171 \r
172 /* Variables used to check that the tasks are still operating without error.\r
173 Each complete iteration of the controller task increments this variable\r
174 provided no errors have been found.  The variable maintaining the same value\r
175 is therefore indication of an error. */\r
176 static volatile unsigned short usCheckVariable = ( unsigned short ) 0;\r
177 static volatile portBASE_TYPE xSuspendedQueueSendError = pdFALSE;\r
178 static volatile portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;\r
179 \r
180 /* Queue used by the second test. */\r
181 xQueueHandle xSuspendedTestQueue;\r
182 \r
183 /* The value the queue receive task expects to receive next.  This is file\r
184 scope so xAreDynamicPriorityTasksStillRunning() can ensure it is still\r
185 incrementing. */\r
186 static unsigned long ulExpectedValue = ( unsigned long ) 0;\r
187 \r
188 /*-----------------------------------------------------------*/\r
189 /*\r
190  * Start the three tasks as described at the top of the file.\r
191  * Note that the limited count task is given a higher priority.\r
192  */\r
193 void vStartDynamicPriorityTasks( void )\r
194 {\r
195         xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );\r
196 \r
197         /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
198         in use.  The queue registry is provided as a means for kernel aware\r
199         debuggers to locate queues and has no purpose if a kernel aware debugger\r
200         is not being used.  The call to vQueueAddToRegistry() will be removed\r
201         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
202         defined to be less than 1. */\r
203         vQueueAddToRegistry( xSuspendedTestQueue, ( signed char * ) "Suspended_Test_Queue" );\r
204 \r
205         xTaskCreate( vContinuousIncrementTask, ( signed char * ) "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinousIncrementHandle );\r
206         xTaskCreate( vLimitedIncrementTask, ( signed char * ) "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );\r
207         xTaskCreate( vCounterControlTask, ( signed char * ) "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
208         xTaskCreate( vQueueSendWhenSuspendedTask, ( signed char * ) "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
209         xTaskCreate( vQueueReceiveWhenSuspendedTask, ( signed char * ) "SUSP_RX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 /*\r
214  * Just loops around incrementing the shared variable until the limit has been\r
215  * reached.  Once the limit has been reached it suspends itself.\r
216  */\r
217 static portTASK_FUNCTION( vLimitedIncrementTask, pvParameters )\r
218 {\r
219 unsigned long *pulCounter;\r
220 \r
221         /* Take a pointer to the shared variable from the parameters passed into\r
222         the task. */\r
223         pulCounter = ( unsigned long * ) pvParameters;\r
224 \r
225         /* This will run before the control task, so the first thing it does is\r
226         suspend - the control task will resume it when ready. */\r
227         vTaskSuspend( NULL );\r
228 \r
229         for( ;; )\r
230         {\r
231                 /* Just count up to a value then suspend. */\r
232                 ( *pulCounter )++;\r
233 \r
234                 if( *pulCounter >= priMAX_COUNT )\r
235                 {\r
236                         vTaskSuspend( NULL );\r
237                 }\r
238         }\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 /*\r
243  * Just keep counting the shared variable up.  The control task will suspend\r
244  * this task when it wants.\r
245  */\r
246 static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )\r
247 {\r
248 unsigned long *pulCounter;\r
249 unsigned portBASE_TYPE uxOurPriority;\r
250 \r
251         /* Take a pointer to the shared variable from the parameters passed into\r
252         the task. */\r
253         pulCounter = ( unsigned long * ) pvParameters;\r
254 \r
255         /* Query our priority so we can raise it when exclusive access to the\r
256         shared variable is required. */\r
257         uxOurPriority = uxTaskPriorityGet( NULL );\r
258 \r
259         for( ;; )\r
260         {\r
261                 /* Raise our priority above the controller task to ensure a context\r
262                 switch does not occur while we are accessing this variable. */\r
263                 vTaskPrioritySet( NULL, uxOurPriority + 1 );\r
264                         ( *pulCounter )++;\r
265                 vTaskPrioritySet( NULL, uxOurPriority );\r
266         }\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r
270 /*\r
271  * Controller task as described above.\r
272  */\r
273 static portTASK_FUNCTION( vCounterControlTask, pvParameters )\r
274 {\r
275 unsigned long ulLastCounter;\r
276 short sLoops;\r
277 short sError = pdFALSE;\r
278 \r
279         /* Just to stop warning messages. */\r
280         ( void ) pvParameters;\r
281 \r
282         for( ;; )\r
283         {\r
284                 /* Start with the counter at zero. */\r
285                 ulCounter = ( unsigned long ) 0;\r
286 \r
287                 /* First section : */\r
288 \r
289                 /* Check the continuous count task is running. */\r
290                 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )\r
291                 {\r
292                         /* Suspend the continuous count task so we can take a mirror of the\r
293                         shared variable without risk of corruption. */\r
294                         vTaskSuspend( xContinousIncrementHandle );\r
295                                 ulLastCounter = ulCounter;\r
296                         vTaskResume( xContinousIncrementHandle );\r
297 \r
298                         /* Now delay to ensure the other task has processor time. */\r
299                         vTaskDelay( priSLEEP_TIME );\r
300 \r
301                         /* Check the shared variable again.  This time to ensure mutual\r
302                         exclusion the whole scheduler will be locked.  This is just for\r
303                         demo purposes! */\r
304                         vTaskSuspendAll();\r
305                         {\r
306                                 if( ulLastCounter == ulCounter )\r
307                                 {\r
308                                         /* The shared variable has not changed.  There is a problem\r
309                                         with the continuous count task so flag an error. */\r
310                                         sError = pdTRUE;\r
311                                 }\r
312                         }\r
313                         xTaskResumeAll();\r
314                 }\r
315 \r
316 \r
317                 /* Second section: */\r
318 \r
319                 /* Suspend the continuous counter task so it stops accessing the shared variable. */\r
320                 vTaskSuspend( xContinousIncrementHandle );\r
321 \r
322                 /* Reset the variable. */\r
323                 ulCounter = ( unsigned long ) 0;\r
324 \r
325                 /* Resume the limited count task which has a higher priority than us.\r
326                 We should therefore not return from this call until the limited count\r
327                 task has suspended itself with a known value in the counter variable. */\r
328                 vTaskResume( xLimitedIncrementHandle );\r
329 \r
330                 /* Does the counter variable have the expected value? */\r
331                 if( ulCounter != priMAX_COUNT )\r
332                 {\r
333                         sError = pdTRUE;\r
334                 }\r
335 \r
336                 if( sError == pdFALSE )\r
337                 {\r
338                         /* If no errors have occurred then increment the check variable. */\r
339                         portENTER_CRITICAL();\r
340                                 usCheckVariable++;\r
341                         portEXIT_CRITICAL();\r
342                 }\r
343 \r
344                 /* Resume the continuous count task and do it all again. */\r
345                 vTaskResume( xContinousIncrementHandle );\r
346         }\r
347 }\r
348 /*-----------------------------------------------------------*/\r
349 \r
350 static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )\r
351 {\r
352 static unsigned long ulValueToSend = ( unsigned long ) 0;\r
353 \r
354         /* Just to stop warning messages. */\r
355         ( void ) pvParameters;\r
356 \r
357         for( ;; )\r
358         {\r
359                 vTaskSuspendAll();\r
360                 {\r
361                         /* We must not block while the scheduler is suspended! */\r
362                         if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )\r
363                         {\r
364                                 xSuspendedQueueSendError = pdTRUE;\r
365                         }\r
366                 }\r
367                 xTaskResumeAll();\r
368 \r
369                 vTaskDelay( priSLEEP_TIME );\r
370 \r
371                 ++ulValueToSend;\r
372         }\r
373 }\r
374 /*-----------------------------------------------------------*/\r
375 \r
376 static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )\r
377 {\r
378 unsigned long ulReceivedValue;\r
379 portBASE_TYPE xGotValue;\r
380 \r
381         /* Just to stop warning messages. */\r
382         ( void ) pvParameters;\r
383 \r
384         for( ;; )\r
385         {\r
386                 do\r
387                 {\r
388                         /* Suspending the scheduler here is fairly pointless and\r
389                         undesirable for a normal application.  It is done here purely\r
390                         to test the scheduler.  The inner xTaskResumeAll() should\r
391                         never return pdTRUE as the scheduler is still locked by the\r
392                         outer call. */\r
393                         vTaskSuspendAll();\r
394                         {\r
395                                 vTaskSuspendAll();\r
396                                 {\r
397                                         xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );\r
398                                 }\r
399                                 if( xTaskResumeAll() != pdFALSE )\r
400                                 {\r
401                                         xSuspendedQueueReceiveError = pdTRUE;\r
402                                 }\r
403                         }\r
404                         xTaskResumeAll();\r
405 \r
406                         #if configUSE_PREEMPTION == 0\r
407                         {\r
408                                 taskYIELD();\r
409                         }\r
410                         #endif\r
411 \r
412                 } while( xGotValue == pdFALSE );\r
413 \r
414                 if( ulReceivedValue != ulExpectedValue )\r
415                 {\r
416                         xSuspendedQueueReceiveError = pdTRUE;\r
417                 }\r
418 \r
419                 if( xSuspendedQueueReceiveError != pdTRUE )\r
420                 {\r
421                         /* Only increment the variable if an error has not occurred.  This\r
422                         allows xAreDynamicPriorityTasksStillRunning() to check for stalled\r
423                         tasks as well as explicit errors. */\r
424                         ++ulExpectedValue;\r
425                 }\r
426         }\r
427 }\r
428 /*-----------------------------------------------------------*/\r
429 \r
430 /* Called to check that all the created tasks are still running without error. */\r
431 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )\r
432 {\r
433 /* Keep a history of the check variables so we know if it has been incremented\r
434 since the last call. */\r
435 static unsigned short usLastTaskCheck = ( unsigned short ) 0;\r
436 static unsigned long ulLastExpectedValue = ( unsigned long ) 0U;\r
437 portBASE_TYPE xReturn = pdTRUE;\r
438 \r
439         /* Check the tasks are still running by ensuring the check variable\r
440         is still incrementing. */\r
441 \r
442         if( usCheckVariable == usLastTaskCheck )\r
443         {\r
444                 /* The check has not incremented so an error exists. */\r
445                 xReturn = pdFALSE;\r
446         }\r
447 \r
448         if( ulExpectedValue == ulLastExpectedValue )\r
449         {\r
450                 /* The value being received by the queue receive task has not\r
451                 incremented so an error exists. */\r
452                 xReturn = pdFALSE;\r
453         }\r
454 \r
455         if( xSuspendedQueueSendError == pdTRUE )\r
456         {\r
457                 xReturn = pdFALSE;\r
458         }\r
459 \r
460         if( xSuspendedQueueReceiveError == pdTRUE )\r
461         {\r
462                 xReturn = pdFALSE;\r
463         }\r
464 \r
465         usLastTaskCheck = usCheckVariable;\r
466         ulLastExpectedValue = ulExpectedValue;\r
467 \r
468         return xReturn;\r
469 }\r