]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Full/dynamic.c
Update version numbers to V7.4.1.
[freertos] / FreeRTOS / Demo / Common / Full / dynamic.c
1 /*\r
2     FreeRTOS V7.4.1 - 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  * The final set of two tasks implements a third test.  This simply raises the\r
132  * priority of a task while the scheduler is suspended.  Again this test was\r
133  * added to exercise parts of the code not covered by the first test.\r
134  *\r
135  * \page Priorities dynamic.c\r
136  * \ingroup DemoFiles\r
137  * <HR>\r
138  */\r
139 \r
140 /*\r
141 Changes from V2.0.0\r
142 \r
143         + Delay periods are now specified using variables and constants of\r
144           portTickType rather than unsigned long.\r
145         + Added a second, simple test that uses the functions \r
146           vQueueReceiveWhenSuspendedTask() and vQueueSendWhenSuspendedTask().\r
147 \r
148 Changes from V3.1.1\r
149 \r
150         + Added a third simple test that uses the vTaskPrioritySet() function\r
151           while the scheduler is suspended.\r
152         + Modified the controller task slightly to test the calling of \r
153           vTaskResumeAll() while the scheduler is suspended.\r
154 */\r
155 \r
156 #include <stdlib.h>\r
157 \r
158 /* Scheduler include files. */\r
159 #include "FreeRTOS.h"\r
160 #include "task.h"\r
161 #include "semphr.h"\r
162 \r
163 /* Demo app include files. */\r
164 #include "dynamic.h"\r
165 #include "print.h"\r
166 \r
167 /* Function that implements the "limited count" task as described above. */\r
168 static void vLimitedIncrementTask( void * pvParameters );\r
169 \r
170 /* Function that implements the "continuous count" task as described above. */\r
171 static void vContinuousIncrementTask( void * pvParameters );\r
172 \r
173 /* Function that implements the controller task as described above. */\r
174 static void vCounterControlTask( void * pvParameters );\r
175 \r
176 /* The simple test functions that check sending and receiving while the\r
177 scheduler is suspended. */\r
178 static void vQueueReceiveWhenSuspendedTask( void *pvParameters );\r
179 static void vQueueSendWhenSuspendedTask( void *pvParameters );\r
180 \r
181 /* The simple test functions that check raising and lowering of task priorities\r
182 while the scheduler is suspended. */\r
183 static void prvChangePriorityWhenSuspendedTask( void *pvParameters );\r
184 static void prvChangePriorityHelperTask( void *pvParameters );\r
185 \r
186 \r
187 /* Demo task specific constants. */\r
188 #define priSTACK_SIZE                           ( ( unsigned short ) configMINIMAL_STACK_SIZE )\r
189 #define priSLEEP_TIME                           ( ( portTickType ) 50 )\r
190 #define priLOOPS                                        ( 5 )\r
191 #define priMAX_COUNT                            ( ( unsigned long ) 0xff )\r
192 #define priNO_BLOCK                                     ( ( portTickType ) 0 )\r
193 #define priSUSPENDED_QUEUE_LENGTH       ( 1 )\r
194 \r
195 /*-----------------------------------------------------------*/\r
196 \r
197 /* Handles to the two counter tasks.  These could be passed in as parameters\r
198 to the controller task to prevent them having to be file scope. */\r
199 static xTaskHandle xContinuousIncrementHandle, xLimitedIncrementHandle, xChangePriorityWhenSuspendedHandle;\r
200 \r
201 /* The shared counter variable.  This is passed in as a parameter to the two \r
202 counter variables for demonstration purposes. */\r
203 static unsigned long ulCounter;\r
204 \r
205 /* Variable used in a similar way by the test that checks the raising and\r
206 lowering of task priorities while the scheduler is suspended. */\r
207 static unsigned long ulPrioritySetCounter;\r
208 \r
209 /* Variables used to check that the tasks are still operating without error.\r
210 Each complete iteration of the controller task increments this variable\r
211 provided no errors have been found.  The variable maintaining the same value\r
212 is therefore indication of an error. */\r
213 static unsigned short usCheckVariable = ( unsigned short ) 0;\r
214 static portBASE_TYPE xSuspendedQueueSendError = pdFALSE;\r
215 static portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;\r
216 static portBASE_TYPE xPriorityRaiseWhenSuspendedError = pdFALSE;\r
217 \r
218 /* Queue used by the second test. */\r
219 xQueueHandle xSuspendedTestQueue;\r
220 \r
221 /*-----------------------------------------------------------*/\r
222 /*\r
223  * Start the seven tasks as described at the top of the file.\r
224  * Note that the limited count task is given a higher priority.\r
225  */\r
226 void vStartDynamicPriorityTasks( void )\r
227 {\r
228         xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );\r
229         xTaskCreate( vContinuousIncrementTask, "CONT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );\r
230         xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );\r
231         xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
232         xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_SEND", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
233         xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RECV", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
234         xTaskCreate( prvChangePriorityWhenSuspendedTask, "1st_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );\r
235         xTaskCreate( prvChangePriorityHelperTask, "2nd_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xChangePriorityWhenSuspendedHandle );\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 /*\r
240  * Just loops around incrementing the shared variable until the limit has been\r
241  * reached.  Once the limit has been reached it suspends itself. \r
242  */\r
243 static void vLimitedIncrementTask( void * pvParameters )\r
244 {\r
245 unsigned long *pulCounter;\r
246 \r
247         /* Take a pointer to the shared variable from the parameters passed into\r
248         the task. */\r
249         pulCounter = ( unsigned long * ) pvParameters;\r
250 \r
251         /* This will run before the control task, so the first thing it does is\r
252         suspend - the control task will resume it when ready. */\r
253         vTaskSuspend( NULL );\r
254 \r
255         for( ;; )\r
256         {\r
257                 /* Just count up to a value then suspend. */\r
258                 ( *pulCounter )++;      \r
259                 \r
260                 if( *pulCounter >= priMAX_COUNT )\r
261                 {\r
262                         vTaskSuspend( NULL );\r
263                 }       \r
264         }\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r
268 /*\r
269  * Just keep counting the shared variable up.  The control task will suspend\r
270  * this task when it wants.\r
271  */\r
272 static void vContinuousIncrementTask( void * pvParameters )\r
273 {\r
274 unsigned long *pulCounter;\r
275 unsigned portBASE_TYPE uxOurPriority;\r
276 \r
277         /* Take a pointer to the shared variable from the parameters passed into\r
278         the task. */\r
279         pulCounter = ( unsigned long * ) pvParameters;\r
280 \r
281         /* Query our priority so we can raise it when exclusive access to the \r
282         shared variable is required. */\r
283         uxOurPriority = uxTaskPriorityGet( NULL );\r
284 \r
285         for( ;; )\r
286         {\r
287                 /* Raise our priority above the controller task to ensure a context\r
288                 switch does not occur while we are accessing this variable. */\r
289                 vTaskPrioritySet( NULL, uxOurPriority + 1 );\r
290                         ( *pulCounter )++;              \r
291                 vTaskPrioritySet( NULL, uxOurPriority );\r
292 \r
293                 #if configUSE_PREEMPTION == 0\r
294                         taskYIELD();\r
295                 #endif\r
296         }\r
297 }\r
298 /*-----------------------------------------------------------*/\r
299 \r
300 /*\r
301  * Controller task as described above.\r
302  */\r
303 static void vCounterControlTask( void * pvParameters )\r
304 {\r
305 unsigned long ulLastCounter;\r
306 short sLoops;\r
307 short sError = pdFALSE;\r
308 const char * const pcTaskStartMsg = "Priority manipulation tasks started.\r\n";\r
309 const char * const pcTaskFailMsg = "Priority manipulation Task Failed\r\n";\r
310 \r
311         /* Just to stop warning messages. */\r
312         ( void ) pvParameters;\r
313 \r
314         /* Queue a message for printing to say the task has started. */\r
315         vPrintDisplayMessage( &pcTaskStartMsg );\r
316 \r
317         for( ;; )\r
318         {\r
319                 /* Start with the counter at zero. */\r
320                 ulCounter = ( unsigned long ) 0;\r
321 \r
322                 /* First section : */\r
323 \r
324                 /* Check the continuous count task is running. */\r
325                 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )\r
326                 {\r
327                         /* Suspend the continuous count task so we can take a mirror of the\r
328                         shared variable without risk of corruption. */\r
329                         vTaskSuspend( xContinuousIncrementHandle );\r
330                                 ulLastCounter = ulCounter;\r
331                         vTaskResume( xContinuousIncrementHandle );\r
332                         \r
333                         /* Now delay to ensure the other task has processor time. */\r
334                         vTaskDelay( priSLEEP_TIME );\r
335 \r
336                         /* Check the shared variable again.  This time to ensure mutual \r
337                         exclusion the whole scheduler will be locked.  This is just for\r
338                         demo purposes! */\r
339                         vTaskSuspendAll();\r
340                         {\r
341                                 if( ulLastCounter == ulCounter )\r
342                                 {\r
343                                         /* The shared variable has not changed.  There is a problem\r
344                                         with the continuous count task so flag an error. */\r
345                                         sError = pdTRUE;\r
346                                         xTaskResumeAll();\r
347                                                 vPrintDisplayMessage( &pcTaskFailMsg );\r
348                                         vTaskSuspendAll();\r
349                                 }\r
350                         }\r
351                         xTaskResumeAll();\r
352                 }\r
353 \r
354 \r
355                 /* Second section: */\r
356 \r
357                 /* Suspend the continuous counter task so it stops accessing the shared variable. */\r
358                 vTaskSuspend( xContinuousIncrementHandle );\r
359 \r
360                 /* Reset the variable. */\r
361                 ulCounter = ( unsigned long ) 0;\r
362 \r
363                 /* Resume the limited count task which has a higher priority than us.\r
364                 We should therefore not return from this call until the limited count\r
365                 task has suspended itself with a known value in the counter variable. \r
366                 The scheduler suspension is not necessary but is included for test\r
367                 purposes. */\r
368                 vTaskSuspendAll();\r
369                         vTaskResume( xLimitedIncrementHandle );\r
370                 xTaskResumeAll();\r
371 \r
372                 /* Does the counter variable have the expected value? */\r
373                 if( ulCounter != priMAX_COUNT )\r
374                 {\r
375                         sError = pdTRUE;\r
376                         vPrintDisplayMessage( &pcTaskFailMsg );\r
377                 }\r
378 \r
379                 if( sError == pdFALSE )\r
380                 {\r
381                         /* If no errors have occurred then increment the check variable. */\r
382                         portENTER_CRITICAL();\r
383                                 usCheckVariable++;\r
384                         portEXIT_CRITICAL();\r
385                 }\r
386 \r
387                 #if configUSE_PREEMPTION == 0\r
388                         taskYIELD();\r
389                 #endif\r
390 \r
391                 /* Resume the continuous count task and do it all again. */\r
392                 vTaskResume( xContinuousIncrementHandle );\r
393         }\r
394 }\r
395 /*-----------------------------------------------------------*/\r
396 \r
397 static void vQueueSendWhenSuspendedTask( void *pvParameters )\r
398 {\r
399 static unsigned long ulValueToSend = ( unsigned long ) 0;\r
400 const char * const pcTaskStartMsg = "Queue send while suspended task started.\r\n";\r
401 const char * const pcTaskFailMsg = "Queue send while suspended failed.\r\n";\r
402 \r
403         /* Just to stop warning messages. */\r
404         ( void ) pvParameters;\r
405 \r
406         /* Queue a message for printing to say the task has started. */\r
407         vPrintDisplayMessage( &pcTaskStartMsg );\r
408 \r
409         for( ;; )\r
410         {\r
411                 vTaskSuspendAll();\r
412                 {\r
413                         /* We must not block while the scheduler is suspended! */\r
414                         if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )\r
415                         {\r
416                                 if( xSuspendedQueueSendError == pdFALSE )\r
417                                 {\r
418                                         xTaskResumeAll();\r
419                                                 vPrintDisplayMessage( &pcTaskFailMsg );\r
420                                         vTaskSuspendAll();\r
421                                 }\r
422 \r
423                                 xSuspendedQueueSendError = pdTRUE;\r
424                         }\r
425                 }\r
426                 xTaskResumeAll();\r
427 \r
428                 vTaskDelay( priSLEEP_TIME );\r
429 \r
430                 ++ulValueToSend;\r
431         }\r
432 }\r
433 /*-----------------------------------------------------------*/\r
434 \r
435 static void vQueueReceiveWhenSuspendedTask( void *pvParameters )\r
436 {\r
437 static unsigned long ulExpectedValue = ( unsigned long ) 0, ulReceivedValue;\r
438 const char * const pcTaskStartMsg = "Queue receive while suspended task started.\r\n";\r
439 const char * const pcTaskFailMsg = "Queue receive while suspended failed.\r\n";\r
440 portBASE_TYPE xGotValue;\r
441 \r
442         /* Just to stop warning messages. */\r
443         ( void ) pvParameters;\r
444 \r
445         /* Queue a message for printing to say the task has started. */\r
446         vPrintDisplayMessage( &pcTaskStartMsg );\r
447 \r
448         for( ;; )\r
449         {\r
450                 do\r
451                 {\r
452                         /* Suspending the scheduler here is fairly pointless and \r
453                         undesirable for a normal application.  It is done here purely\r
454                         to test the scheduler.  The inner xTaskResumeAll() should\r
455                         never return pdTRUE as the scheduler is still locked by the\r
456                         outer call. */\r
457                         vTaskSuspendAll();\r
458                         {\r
459                                 vTaskSuspendAll();\r
460                                 {\r
461                                         xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );\r
462                                 }\r
463                                 if( xTaskResumeAll() )\r
464                                 {\r
465                                         xSuspendedQueueReceiveError = pdTRUE;\r
466                                 }\r
467                         }\r
468                         xTaskResumeAll();\r
469 \r
470                         #if configUSE_PREEMPTION == 0\r
471                                 taskYIELD();\r
472                         #endif\r
473 \r
474                 } while( xGotValue == pdFALSE );\r
475 \r
476                 if( ulReceivedValue != ulExpectedValue )\r
477                 {\r
478                         if( xSuspendedQueueReceiveError == pdFALSE )\r
479                         {\r
480                                 vPrintDisplayMessage( &pcTaskFailMsg );\r
481                         }\r
482                         xSuspendedQueueReceiveError = pdTRUE;\r
483                 }\r
484 \r
485                 ++ulExpectedValue;\r
486         }\r
487 }\r
488 /*-----------------------------------------------------------*/\r
489 \r
490 static void prvChangePriorityWhenSuspendedTask( void *pvParameters )\r
491 {\r
492 const char * const pcTaskStartMsg = "Priority change when suspended task started.\r\n";\r
493 const char * const pcTaskFailMsg = "Priority change when suspended task failed.\r\n";\r
494 \r
495         /* Just to stop warning messages. */\r
496         ( void ) pvParameters;\r
497 \r
498         /* Queue a message for printing to say the task has started. */\r
499         vPrintDisplayMessage( &pcTaskStartMsg );        \r
500         \r
501         for( ;; )\r
502         {\r
503                 /* Start with the counter at 0 so we know what the counter should be\r
504                 when we check it next. */\r
505                 ulPrioritySetCounter = ( unsigned long ) 0;\r
506 \r
507                 /* Resume the helper task.  At this time it has a priority lower than\r
508                 ours so no context switch should occur. */\r
509                 vTaskResume( xChangePriorityWhenSuspendedHandle );\r
510 \r
511                 /* Check to ensure the task just resumed has not executed. */\r
512                 portENTER_CRITICAL();\r
513                 {\r
514                         if( ulPrioritySetCounter != ( unsigned long ) 0 )\r
515                         {\r
516                                 xPriorityRaiseWhenSuspendedError = pdTRUE;\r
517                                 vPrintDisplayMessage( &pcTaskFailMsg );\r
518                         }\r
519                 }\r
520                 portEXIT_CRITICAL();\r
521 \r
522                 /* Now try raising the priority while the scheduler is suspended. */\r
523                 vTaskSuspendAll();\r
524                 {\r
525                         vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, ( configMAX_PRIORITIES - 1 ) );\r
526 \r
527                         /* Again, even though the helper task has a priority greater than \r
528                         ours, it should not have executed yet because the scheduler is\r
529                         suspended. */\r
530                         portENTER_CRITICAL();\r
531                         {\r
532                                 if( ulPrioritySetCounter != ( unsigned long ) 0 )\r
533                                 {\r
534                                         xPriorityRaiseWhenSuspendedError = pdTRUE;\r
535                                         vPrintDisplayMessage( &pcTaskFailMsg );\r
536                                 }\r
537                         }\r
538                         portEXIT_CRITICAL();\r
539                 }\r
540                 xTaskResumeAll();\r
541                 \r
542                 /* Now the scheduler has been resumed the helper task should \r
543                 immediately preempt us and execute.  When it executes it will increment\r
544                 the ulPrioritySetCounter exactly once before suspending itself.\r
545 \r
546                 We should now always find the counter set to 1. */\r
547                 portENTER_CRITICAL();\r
548                 {\r
549                         if( ulPrioritySetCounter != ( unsigned long ) 1 )\r
550                         {\r
551                                 xPriorityRaiseWhenSuspendedError = pdTRUE;\r
552                                 vPrintDisplayMessage( &pcTaskFailMsg );\r
553                         }\r
554                 }\r
555                 portEXIT_CRITICAL();\r
556 \r
557                 /* Delay until we try this again. */            \r
558                 vTaskDelay( priSLEEP_TIME * 2 );\r
559                 \r
560                 /* Set the priority of the helper task back ready for the next \r
561                 execution of this task. */\r
562                 vTaskSuspendAll();\r
563                         vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, tskIDLE_PRIORITY );                               \r
564                 xTaskResumeAll();                               \r
565         }\r
566 }\r
567 /*-----------------------------------------------------------*/\r
568 \r
569 static void prvChangePriorityHelperTask( void *pvParameters )\r
570 {\r
571         /* Just to stop warning messages. */\r
572         ( void ) pvParameters;\r
573 \r
574         for( ;; )\r
575         {\r
576                 /* This is the helper task for prvChangePriorityWhenSuspendedTask().\r
577                 It has it's priority raised and lowered.  When it runs it simply \r
578                 increments the counter then suspends itself again.  This allows\r
579                 prvChangePriorityWhenSuspendedTask() to know how many times it has\r
580                 executed. */\r
581                 ulPrioritySetCounter++;\r
582                 vTaskSuspend( NULL );\r
583         }\r
584 }\r
585 /*-----------------------------------------------------------*/\r
586 \r
587 /* Called to check that all the created tasks are still running without error. */\r
588 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )\r
589 {\r
590 /* Keep a history of the check variables so we know if it has been incremented \r
591 since the last call. */\r
592 static unsigned short usLastTaskCheck = ( unsigned short ) 0;\r
593 portBASE_TYPE xReturn = pdTRUE;\r
594 \r
595         /* Check the tasks are still running by ensuring the check variable\r
596         is still incrementing. */\r
597 \r
598         if( usCheckVariable == usLastTaskCheck )\r
599         {\r
600                 /* The check has not incremented so an error exists. */\r
601                 xReturn = pdFALSE;\r
602         }\r
603 \r
604         if( xSuspendedQueueSendError == pdTRUE )\r
605         {\r
606                 xReturn = pdFALSE;\r
607         }\r
608 \r
609         if( xSuspendedQueueReceiveError == pdTRUE )\r
610         {\r
611                 xReturn = pdFALSE;\r
612         }\r
613 \r
614         if( xPriorityRaiseWhenSuspendedError == pdTRUE )\r
615         {\r
616                 xReturn = pdFALSE;\r
617         }\r
618 \r
619         usLastTaskCheck = usCheckVariable;\r
620         return xReturn;\r
621 }\r
622 \r
623 \r
624 \r
625 \r