2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * The first test creates three tasks - two counter tasks (one continuous count
\r
56 * and one limited count) and one controller. A "count" variable is shared
\r
57 * between all three tasks. The two counter tasks should never be in a "ready"
\r
58 * state at the same time. The controller task runs at the same priority as
\r
59 * the continuous count task, and at a lower priority than the limited count
\r
62 * One counter task loops indefinitely, incrementing the shared count variable
\r
63 * on each iteration. To ensure it has exclusive access to the variable it
\r
64 * raises it's priority above that of the controller task before each
\r
65 * increment, lowering it again to it's original priority before starting the
\r
68 * The other counter task increments the shared count variable on each
\r
69 * iteration of it's loop until the count has reached a limit of 0xff - at
\r
70 * which point it suspends itself. It will not start a new loop until the
\r
71 * controller task has made it "ready" again by calling vTaskResume ().
\r
72 * This second counter task operates at a higher priority than controller
\r
73 * task so does not need to worry about mutual exclusion of the counter
\r
76 * The controller task is in two sections. The first section controls and
\r
77 * monitors the continuous count task. When this section is operational the
\r
78 * limited count task is suspended. Likewise, the second section controls
\r
79 * and monitors the limited count task. When this section is operational the
\r
80 * continuous count task is suspended.
\r
82 * In the first section the controller task first takes a copy of the shared
\r
83 * count variable. To ensure mutual exclusion on the count variable it
\r
84 * suspends the continuous count task, resuming it again when the copy has been
\r
85 * taken. The controller task then sleeps for a fixed period - during which
\r
86 * the continuous count task will execute and increment the shared variable.
\r
87 * When the controller task wakes it checks that the continuous count task
\r
88 * has executed by comparing the copy of the shared variable with its current
\r
89 * value. This time, to ensure mutual exclusion, the scheduler itself is
\r
90 * suspended with a call to vTaskSuspendAll (). This is for demonstration
\r
91 * purposes only and is not a recommended technique due to its inefficiency.
\r
93 * After a fixed number of iterations the controller task suspends the
\r
94 * continuous count task, and moves on to its second section.
\r
96 * At the start of the second section the shared variable is cleared to zero.
\r
97 * The limited count task is then woken from it's suspension by a call to
\r
98 * vTaskResume (). As this counter task operates at a higher priority than
\r
99 * the controller task the controller task should not run again until the
\r
100 * shared variable has been counted up to the limited value causing the counter
\r
101 * task to suspend itself. The next line after vTaskResume () is therefore
\r
102 * a check on the shared variable to ensure everything is as expected.
\r
105 * The second test consists of a couple of very simple tasks that post onto a
\r
106 * queue while the scheduler is suspended. This test was added to test parts
\r
107 * of the scheduler not exercised by the first test.
\r
110 * The final set of two tasks implements a third test. This simply raises the
\r
111 * priority of a task while the scheduler is suspended. Again this test was
\r
112 * added to exercise parts of the code not covered by the first test.
\r
114 * \page Priorities dynamic.c
\r
115 * \ingroup DemoFiles
\r
120 Changes from V2.0.0
\r
122 + Delay periods are now specified using variables and constants of
\r
123 portTickType rather than unsigned long.
\r
124 + Added a second, simple test that uses the functions
\r
125 vQueueReceiveWhenSuspendedTask() and vQueueSendWhenSuspendedTask().
\r
127 Changes from V3.1.1
\r
129 + Added a third simple test that uses the vTaskPrioritySet() function
\r
130 while the scheduler is suspended.
\r
131 + Modified the controller task slightly to test the calling of
\r
132 vTaskResumeAll() while the scheduler is suspended.
\r
135 #include <stdlib.h>
\r
137 /* Scheduler include files. */
\r
138 #include "FreeRTOS.h"
\r
140 #include "semphr.h"
\r
142 /* Demo app include files. */
\r
143 #include "dynamic.h"
\r
146 /* Function that implements the "limited count" task as described above. */
\r
147 static void vLimitedIncrementTask( void * pvParameters );
\r
149 /* Function that implements the "continuous count" task as described above. */
\r
150 static void vContinuousIncrementTask( void * pvParameters );
\r
152 /* Function that implements the controller task as described above. */
\r
153 static void vCounterControlTask( void * pvParameters );
\r
155 /* The simple test functions that check sending and receiving while the
\r
156 scheduler is suspended. */
\r
157 static void vQueueReceiveWhenSuspendedTask( void *pvParameters );
\r
158 static void vQueueSendWhenSuspendedTask( void *pvParameters );
\r
160 /* The simple test functions that check raising and lowering of task priorities
\r
161 while the scheduler is suspended. */
\r
162 static void prvChangePriorityWhenSuspendedTask( void *pvParameters );
\r
163 static void prvChangePriorityHelperTask( void *pvParameters );
\r
166 /* Demo task specific constants. */
\r
167 #define priSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
\r
168 #define priSLEEP_TIME ( ( portTickType ) 50 )
\r
169 #define priLOOPS ( 5 )
\r
170 #define priMAX_COUNT ( ( unsigned long ) 0xff )
\r
171 #define priNO_BLOCK ( ( portTickType ) 0 )
\r
172 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
174 /*-----------------------------------------------------------*/
\r
176 /* Handles to the two counter tasks. These could be passed in as parameters
\r
177 to the controller task to prevent them having to be file scope. */
\r
178 static xTaskHandle xContinuousIncrementHandle, xLimitedIncrementHandle, xChangePriorityWhenSuspendedHandle;
\r
180 /* The shared counter variable. This is passed in as a parameter to the two
\r
181 counter variables for demonstration purposes. */
\r
182 static unsigned long ulCounter;
\r
184 /* Variable used in a similar way by the test that checks the raising and
\r
185 lowering of task priorities while the scheduler is suspended. */
\r
186 static unsigned long ulPrioritySetCounter;
\r
188 /* Variables used to check that the tasks are still operating without error.
\r
189 Each complete iteration of the controller task increments this variable
\r
190 provided no errors have been found. The variable maintaining the same value
\r
191 is therefore indication of an error. */
\r
192 static unsigned short usCheckVariable = ( unsigned short ) 0;
\r
193 static portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
\r
194 static portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
\r
195 static portBASE_TYPE xPriorityRaiseWhenSuspendedError = pdFALSE;
\r
197 /* Queue used by the second test. */
\r
198 xQueueHandle xSuspendedTestQueue;
\r
200 /*-----------------------------------------------------------*/
\r
202 * Start the seven tasks as described at the top of the file.
\r
203 * Note that the limited count task is given a higher priority.
\r
205 void vStartDynamicPriorityTasks( void )
\r
207 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
208 xTaskCreate( vContinuousIncrementTask, "CONT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
\r
209 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
210 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
211 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_SEND", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
212 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RECV", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
213 xTaskCreate( prvChangePriorityWhenSuspendedTask, "1st_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
214 xTaskCreate( prvChangePriorityHelperTask, "2nd_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xChangePriorityWhenSuspendedHandle );
\r
216 /*-----------------------------------------------------------*/
\r
219 * Just loops around incrementing the shared variable until the limit has been
\r
220 * reached. Once the limit has been reached it suspends itself.
\r
222 static void vLimitedIncrementTask( void * pvParameters )
\r
224 unsigned long *pulCounter;
\r
226 /* Take a pointer to the shared variable from the parameters passed into
\r
228 pulCounter = ( unsigned long * ) pvParameters;
\r
230 /* This will run before the control task, so the first thing it does is
\r
231 suspend - the control task will resume it when ready. */
\r
232 vTaskSuspend( NULL );
\r
236 /* Just count up to a value then suspend. */
\r
237 ( *pulCounter )++;
\r
239 if( *pulCounter >= priMAX_COUNT )
\r
241 vTaskSuspend( NULL );
\r
245 /*-----------------------------------------------------------*/
\r
248 * Just keep counting the shared variable up. The control task will suspend
\r
249 * this task when it wants.
\r
251 static void vContinuousIncrementTask( void * pvParameters )
\r
253 unsigned long *pulCounter;
\r
254 unsigned portBASE_TYPE uxOurPriority;
\r
256 /* Take a pointer to the shared variable from the parameters passed into
\r
258 pulCounter = ( unsigned long * ) pvParameters;
\r
260 /* Query our priority so we can raise it when exclusive access to the
\r
261 shared variable is required. */
\r
262 uxOurPriority = uxTaskPriorityGet( NULL );
\r
266 /* Raise our priority above the controller task to ensure a context
\r
267 switch does not occur while we are accessing this variable. */
\r
268 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
269 ( *pulCounter )++;
\r
270 vTaskPrioritySet( NULL, uxOurPriority );
\r
272 #if configUSE_PREEMPTION == 0
\r
277 /*-----------------------------------------------------------*/
\r
280 * Controller task as described above.
\r
282 static void vCounterControlTask( void * pvParameters )
\r
284 unsigned long ulLastCounter;
\r
286 short sError = pdFALSE;
\r
287 const char * const pcTaskStartMsg = "Priority manipulation tasks started.\r\n";
\r
288 const char * const pcTaskFailMsg = "Priority manipulation Task Failed\r\n";
\r
290 /* Just to stop warning messages. */
\r
291 ( void ) pvParameters;
\r
293 /* Queue a message for printing to say the task has started. */
\r
294 vPrintDisplayMessage( &pcTaskStartMsg );
\r
298 /* Start with the counter at zero. */
\r
299 ulCounter = ( unsigned long ) 0;
\r
301 /* First section : */
\r
303 /* Check the continuous count task is running. */
\r
304 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
306 /* Suspend the continuous count task so we can take a mirror of the
\r
307 shared variable without risk of corruption. */
\r
308 vTaskSuspend( xContinuousIncrementHandle );
\r
309 ulLastCounter = ulCounter;
\r
310 vTaskResume( xContinuousIncrementHandle );
\r
312 /* Now delay to ensure the other task has processor time. */
\r
313 vTaskDelay( priSLEEP_TIME );
\r
315 /* Check the shared variable again. This time to ensure mutual
\r
316 exclusion the whole scheduler will be locked. This is just for
\r
320 if( ulLastCounter == ulCounter )
\r
322 /* The shared variable has not changed. There is a problem
\r
323 with the continuous count task so flag an error. */
\r
326 vPrintDisplayMessage( &pcTaskFailMsg );
\r
334 /* Second section: */
\r
336 /* Suspend the continuous counter task so it stops accessing the shared variable. */
\r
337 vTaskSuspend( xContinuousIncrementHandle );
\r
339 /* Reset the variable. */
\r
340 ulCounter = ( unsigned long ) 0;
\r
342 /* Resume the limited count task which has a higher priority than us.
\r
343 We should therefore not return from this call until the limited count
\r
344 task has suspended itself with a known value in the counter variable.
\r
345 The scheduler suspension is not necessary but is included for test
\r
348 vTaskResume( xLimitedIncrementHandle );
\r
351 /* Does the counter variable have the expected value? */
\r
352 if( ulCounter != priMAX_COUNT )
\r
355 vPrintDisplayMessage( &pcTaskFailMsg );
\r
358 if( sError == pdFALSE )
\r
360 /* If no errors have occurred then increment the check variable. */
\r
361 portENTER_CRITICAL();
\r
363 portEXIT_CRITICAL();
\r
366 #if configUSE_PREEMPTION == 0
\r
370 /* Resume the continuous count task and do it all again. */
\r
371 vTaskResume( xContinuousIncrementHandle );
\r
374 /*-----------------------------------------------------------*/
\r
376 static void vQueueSendWhenSuspendedTask( void *pvParameters )
\r
378 static unsigned long ulValueToSend = ( unsigned long ) 0;
\r
379 const char * const pcTaskStartMsg = "Queue send while suspended task started.\r\n";
\r
380 const char * const pcTaskFailMsg = "Queue send while suspended failed.\r\n";
\r
382 /* Just to stop warning messages. */
\r
383 ( void ) pvParameters;
\r
385 /* Queue a message for printing to say the task has started. */
\r
386 vPrintDisplayMessage( &pcTaskStartMsg );
\r
392 /* We must not block while the scheduler is suspended! */
\r
393 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
395 if( xSuspendedQueueSendError == pdFALSE )
\r
398 vPrintDisplayMessage( &pcTaskFailMsg );
\r
402 xSuspendedQueueSendError = pdTRUE;
\r
407 vTaskDelay( priSLEEP_TIME );
\r
412 /*-----------------------------------------------------------*/
\r
414 static void vQueueReceiveWhenSuspendedTask( void *pvParameters )
\r
416 static unsigned long ulExpectedValue = ( unsigned long ) 0, ulReceivedValue;
\r
417 const char * const pcTaskStartMsg = "Queue receive while suspended task started.\r\n";
\r
418 const char * const pcTaskFailMsg = "Queue receive while suspended failed.\r\n";
\r
419 portBASE_TYPE xGotValue;
\r
421 /* Just to stop warning messages. */
\r
422 ( void ) pvParameters;
\r
424 /* Queue a message for printing to say the task has started. */
\r
425 vPrintDisplayMessage( &pcTaskStartMsg );
\r
431 /* Suspending the scheduler here is fairly pointless and
\r
432 undesirable for a normal application. It is done here purely
\r
433 to test the scheduler. The inner xTaskResumeAll() should
\r
434 never return pdTRUE as the scheduler is still locked by the
\r
440 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
442 if( xTaskResumeAll() )
\r
444 xSuspendedQueueReceiveError = pdTRUE;
\r
449 #if configUSE_PREEMPTION == 0
\r
453 } while( xGotValue == pdFALSE );
\r
455 if( ulReceivedValue != ulExpectedValue )
\r
457 if( xSuspendedQueueReceiveError == pdFALSE )
\r
459 vPrintDisplayMessage( &pcTaskFailMsg );
\r
461 xSuspendedQueueReceiveError = pdTRUE;
\r
467 /*-----------------------------------------------------------*/
\r
469 static void prvChangePriorityWhenSuspendedTask( void *pvParameters )
\r
471 const char * const pcTaskStartMsg = "Priority change when suspended task started.\r\n";
\r
472 const char * const pcTaskFailMsg = "Priority change when suspended task failed.\r\n";
\r
474 /* Just to stop warning messages. */
\r
475 ( void ) pvParameters;
\r
477 /* Queue a message for printing to say the task has started. */
\r
478 vPrintDisplayMessage( &pcTaskStartMsg );
\r
482 /* Start with the counter at 0 so we know what the counter should be
\r
483 when we check it next. */
\r
484 ulPrioritySetCounter = ( unsigned long ) 0;
\r
486 /* Resume the helper task. At this time it has a priority lower than
\r
487 ours so no context switch should occur. */
\r
488 vTaskResume( xChangePriorityWhenSuspendedHandle );
\r
490 /* Check to ensure the task just resumed has not executed. */
\r
491 portENTER_CRITICAL();
\r
493 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
495 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
496 vPrintDisplayMessage( &pcTaskFailMsg );
\r
499 portEXIT_CRITICAL();
\r
501 /* Now try raising the priority while the scheduler is suspended. */
\r
504 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, ( configMAX_PRIORITIES - 1 ) );
\r
506 /* Again, even though the helper task has a priority greater than
\r
507 ours, it should not have executed yet because the scheduler is
\r
509 portENTER_CRITICAL();
\r
511 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
513 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
514 vPrintDisplayMessage( &pcTaskFailMsg );
\r
517 portEXIT_CRITICAL();
\r
521 /* Now the scheduler has been resumed the helper task should
\r
522 immediately preempt us and execute. When it executes it will increment
\r
523 the ulPrioritySetCounter exactly once before suspending itself.
\r
525 We should now always find the counter set to 1. */
\r
526 portENTER_CRITICAL();
\r
528 if( ulPrioritySetCounter != ( unsigned long ) 1 )
\r
530 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
531 vPrintDisplayMessage( &pcTaskFailMsg );
\r
534 portEXIT_CRITICAL();
\r
536 /* Delay until we try this again. */
\r
537 vTaskDelay( priSLEEP_TIME * 2 );
\r
539 /* Set the priority of the helper task back ready for the next
\r
540 execution of this task. */
\r
542 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, tskIDLE_PRIORITY );
\r
546 /*-----------------------------------------------------------*/
\r
548 static void prvChangePriorityHelperTask( void *pvParameters )
\r
550 /* Just to stop warning messages. */
\r
551 ( void ) pvParameters;
\r
555 /* This is the helper task for prvChangePriorityWhenSuspendedTask().
\r
556 It has it's priority raised and lowered. When it runs it simply
\r
557 increments the counter then suspends itself again. This allows
\r
558 prvChangePriorityWhenSuspendedTask() to know how many times it has
\r
560 ulPrioritySetCounter++;
\r
561 vTaskSuspend( NULL );
\r
564 /*-----------------------------------------------------------*/
\r
566 /* Called to check that all the created tasks are still running without error. */
\r
567 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
\r
569 /* Keep a history of the check variables so we know if it has been incremented
\r
570 since the last call. */
\r
571 static unsigned short usLastTaskCheck = ( unsigned short ) 0;
\r
572 portBASE_TYPE xReturn = pdTRUE;
\r
574 /* Check the tasks are still running by ensuring the check variable
\r
575 is still incrementing. */
\r
577 if( usCheckVariable == usLastTaskCheck )
\r
579 /* The check has not incremented so an error exists. */
\r
583 if( xSuspendedQueueSendError == pdTRUE )
\r
588 if( xSuspendedQueueReceiveError == pdTRUE )
\r
593 if( xPriorityRaiseWhenSuspendedError == pdTRUE )
\r
598 usLastTaskCheck = usCheckVariable;
\r