2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
50 * The first test creates three tasks - two counter tasks (one continuous count
\r
51 * and one limited count) and one controller. A "count" variable is shared
\r
52 * between all three tasks. The two counter tasks should never be in a "ready"
\r
53 * state at the same time. The controller task runs at the same priority as
\r
54 * the continuous count task, and at a lower priority than the limited count
\r
57 * One counter task loops indefinitely, incrementing the shared count variable
\r
58 * on each iteration. To ensure it has exclusive access to the variable it
\r
59 * raises it's priority above that of the controller task before each
\r
60 * increment, lowering it again to it's original priority before starting the
\r
63 * The other counter task increments the shared count variable on each
\r
64 * iteration of it's loop until the count has reached a limit of 0xff - at
\r
65 * which point it suspends itself. It will not start a new loop until the
\r
66 * controller task has made it "ready" again by calling vTaskResume ().
\r
67 * This second counter task operates at a higher priority than controller
\r
68 * task so does not need to worry about mutual exclusion of the counter
\r
71 * The controller task is in two sections. The first section controls and
\r
72 * monitors the continuous count task. When this section is operational the
\r
73 * limited count task is suspended. Likewise, the second section controls
\r
74 * and monitors the limited count task. When this section is operational the
\r
75 * continuous count task is suspended.
\r
77 * In the first section the controller task first takes a copy of the shared
\r
78 * count variable. To ensure mutual exclusion on the count variable it
\r
79 * suspends the continuous count task, resuming it again when the copy has been
\r
80 * taken. The controller task then sleeps for a fixed period - during which
\r
81 * the continuous count task will execute and increment the shared variable.
\r
82 * When the controller task wakes it checks that the continuous count task
\r
83 * has executed by comparing the copy of the shared variable with its current
\r
84 * value. This time, to ensure mutual exclusion, the scheduler itself is
\r
85 * suspended with a call to vTaskSuspendAll (). This is for demonstration
\r
86 * purposes only and is not a recommended technique due to its inefficiency.
\r
88 * After a fixed number of iterations the controller task suspends the
\r
89 * continuous count task, and moves on to its second section.
\r
91 * At the start of the second section the shared variable is cleared to zero.
\r
92 * The limited count task is then woken from it's suspension by a call to
\r
93 * vTaskResume (). As this counter task operates at a higher priority than
\r
94 * the controller task the controller task should not run again until the
\r
95 * shared variable has been counted up to the limited value causing the counter
\r
96 * task to suspend itself. The next line after vTaskResume () is therefore
\r
97 * a check on the shared variable to ensure everything is as expected.
\r
100 * The second test consists of a couple of very simple tasks that post onto a
\r
101 * queue while the scheduler is suspended. This test was added to test parts
\r
102 * of the scheduler not exercised by the first test.
\r
105 * The final set of two tasks implements a third test. This simply raises the
\r
106 * priority of a task while the scheduler is suspended. Again this test was
\r
107 * added to exercise parts of the code not covered by the first test.
\r
109 * \page Priorities dynamic.c
\r
110 * \ingroup DemoFiles
\r
115 Changes from V2.0.0
\r
117 + Delay periods are now specified using variables and constants of
\r
118 portTickType rather than unsigned long.
\r
119 + Added a second, simple test that uses the functions
\r
120 vQueueReceiveWhenSuspendedTask() and vQueueSendWhenSuspendedTask().
\r
122 Changes from V3.1.1
\r
124 + Added a third simple test that uses the vTaskPrioritySet() function
\r
125 while the scheduler is suspended.
\r
126 + Modified the controller task slightly to test the calling of
\r
127 vTaskResumeAll() while the scheduler is suspended.
\r
130 #include <stdlib.h>
\r
132 /* Scheduler include files. */
\r
133 #include "FreeRTOS.h"
\r
135 #include "semphr.h"
\r
137 /* Demo app include files. */
\r
138 #include "dynamic.h"
\r
141 /* Function that implements the "limited count" task as described above. */
\r
142 static void vLimitedIncrementTask( void * pvParameters );
\r
144 /* Function that implements the "continuous count" task as described above. */
\r
145 static void vContinuousIncrementTask( void * pvParameters );
\r
147 /* Function that implements the controller task as described above. */
\r
148 static void vCounterControlTask( void * pvParameters );
\r
150 /* The simple test functions that check sending and receiving while the
\r
151 scheduler is suspended. */
\r
152 static void vQueueReceiveWhenSuspendedTask( void *pvParameters );
\r
153 static void vQueueSendWhenSuspendedTask( void *pvParameters );
\r
155 /* The simple test functions that check raising and lowering of task priorities
\r
156 while the scheduler is suspended. */
\r
157 static void prvChangePriorityWhenSuspendedTask( void *pvParameters );
\r
158 static void prvChangePriorityHelperTask( void *pvParameters );
\r
161 /* Demo task specific constants. */
\r
162 #define priSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
\r
163 #define priSLEEP_TIME ( ( portTickType ) 50 )
\r
164 #define priLOOPS ( 5 )
\r
165 #define priMAX_COUNT ( ( unsigned long ) 0xff )
\r
166 #define priNO_BLOCK ( ( portTickType ) 0 )
\r
167 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
169 /*-----------------------------------------------------------*/
\r
171 /* Handles to the two counter tasks. These could be passed in as parameters
\r
172 to the controller task to prevent them having to be file scope. */
\r
173 static xTaskHandle xContinuousIncrementHandle, xLimitedIncrementHandle, xChangePriorityWhenSuspendedHandle;
\r
175 /* The shared counter variable. This is passed in as a parameter to the two
\r
176 counter variables for demonstration purposes. */
\r
177 static unsigned long ulCounter;
\r
179 /* Variable used in a similar way by the test that checks the raising and
\r
180 lowering of task priorities while the scheduler is suspended. */
\r
181 static unsigned long ulPrioritySetCounter;
\r
183 /* Variables used to check that the tasks are still operating without error.
\r
184 Each complete iteration of the controller task increments this variable
\r
185 provided no errors have been found. The variable maintaining the same value
\r
186 is therefore indication of an error. */
\r
187 static unsigned short usCheckVariable = ( unsigned short ) 0;
\r
188 static portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
\r
189 static portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
\r
190 static portBASE_TYPE xPriorityRaiseWhenSuspendedError = pdFALSE;
\r
192 /* Queue used by the second test. */
\r
193 xQueueHandle xSuspendedTestQueue;
\r
195 /*-----------------------------------------------------------*/
\r
197 * Start the seven tasks as described at the top of the file.
\r
198 * Note that the limited count task is given a higher priority.
\r
200 void vStartDynamicPriorityTasks( void )
\r
202 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
203 xTaskCreate( vContinuousIncrementTask, "CONT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
\r
204 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
205 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
206 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_SEND", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
207 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RECV", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
208 xTaskCreate( prvChangePriorityWhenSuspendedTask, "1st_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
209 xTaskCreate( prvChangePriorityHelperTask, "2nd_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xChangePriorityWhenSuspendedHandle );
\r
211 /*-----------------------------------------------------------*/
\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
217 static void vLimitedIncrementTask( void * pvParameters )
\r
219 unsigned long *pulCounter;
\r
221 /* Take a pointer to the shared variable from the parameters passed into
\r
223 pulCounter = ( unsigned long * ) pvParameters;
\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
231 /* Just count up to a value then suspend. */
\r
232 ( *pulCounter )++;
\r
234 if( *pulCounter >= priMAX_COUNT )
\r
236 vTaskSuspend( NULL );
\r
240 /*-----------------------------------------------------------*/
\r
243 * Just keep counting the shared variable up. The control task will suspend
\r
244 * this task when it wants.
\r
246 static void vContinuousIncrementTask( void * pvParameters )
\r
248 unsigned long *pulCounter;
\r
249 unsigned portBASE_TYPE uxOurPriority;
\r
251 /* Take a pointer to the shared variable from the parameters passed into
\r
253 pulCounter = ( unsigned long * ) pvParameters;
\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
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
267 #if configUSE_PREEMPTION == 0
\r
272 /*-----------------------------------------------------------*/
\r
275 * Controller task as described above.
\r
277 static void vCounterControlTask( void * pvParameters )
\r
279 unsigned long ulLastCounter;
\r
281 short sError = pdFALSE;
\r
282 const char * const pcTaskStartMsg = "Priority manipulation tasks started.\r\n";
\r
283 const char * const pcTaskFailMsg = "Priority manipulation Task Failed\r\n";
\r
285 /* Just to stop warning messages. */
\r
286 ( void ) pvParameters;
\r
288 /* Queue a message for printing to say the task has started. */
\r
289 vPrintDisplayMessage( &pcTaskStartMsg );
\r
293 /* Start with the counter at zero. */
\r
294 ulCounter = ( unsigned long ) 0;
\r
296 /* First section : */
\r
298 /* Check the continuous count task is running. */
\r
299 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
301 /* Suspend the continuous count task so we can take a mirror of the
\r
302 shared variable without risk of corruption. */
\r
303 vTaskSuspend( xContinuousIncrementHandle );
\r
304 ulLastCounter = ulCounter;
\r
305 vTaskResume( xContinuousIncrementHandle );
\r
307 /* Now delay to ensure the other task has processor time. */
\r
308 vTaskDelay( priSLEEP_TIME );
\r
310 /* Check the shared variable again. This time to ensure mutual
\r
311 exclusion the whole scheduler will be locked. This is just for
\r
315 if( ulLastCounter == ulCounter )
\r
317 /* The shared variable has not changed. There is a problem
\r
318 with the continuous count task so flag an error. */
\r
321 vPrintDisplayMessage( &pcTaskFailMsg );
\r
329 /* Second section: */
\r
331 /* Suspend the continuous counter task so it stops accessing the shared variable. */
\r
332 vTaskSuspend( xContinuousIncrementHandle );
\r
334 /* Reset the variable. */
\r
335 ulCounter = ( unsigned long ) 0;
\r
337 /* Resume the limited count task which has a higher priority than us.
\r
338 We should therefore not return from this call until the limited count
\r
339 task has suspended itself with a known value in the counter variable.
\r
340 The scheduler suspension is not necessary but is included for test
\r
343 vTaskResume( xLimitedIncrementHandle );
\r
346 /* Does the counter variable have the expected value? */
\r
347 if( ulCounter != priMAX_COUNT )
\r
350 vPrintDisplayMessage( &pcTaskFailMsg );
\r
353 if( sError == pdFALSE )
\r
355 /* If no errors have occurred then increment the check variable. */
\r
356 portENTER_CRITICAL();
\r
358 portEXIT_CRITICAL();
\r
361 #if configUSE_PREEMPTION == 0
\r
365 /* Resume the continuous count task and do it all again. */
\r
366 vTaskResume( xContinuousIncrementHandle );
\r
369 /*-----------------------------------------------------------*/
\r
371 static void vQueueSendWhenSuspendedTask( void *pvParameters )
\r
373 static unsigned long ulValueToSend = ( unsigned long ) 0;
\r
374 const char * const pcTaskStartMsg = "Queue send while suspended task started.\r\n";
\r
375 const char * const pcTaskFailMsg = "Queue send while suspended failed.\r\n";
\r
377 /* Just to stop warning messages. */
\r
378 ( void ) pvParameters;
\r
380 /* Queue a message for printing to say the task has started. */
\r
381 vPrintDisplayMessage( &pcTaskStartMsg );
\r
387 /* We must not block while the scheduler is suspended! */
\r
388 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
390 if( xSuspendedQueueSendError == pdFALSE )
\r
393 vPrintDisplayMessage( &pcTaskFailMsg );
\r
397 xSuspendedQueueSendError = pdTRUE;
\r
402 vTaskDelay( priSLEEP_TIME );
\r
407 /*-----------------------------------------------------------*/
\r
409 static void vQueueReceiveWhenSuspendedTask( void *pvParameters )
\r
411 static unsigned long ulExpectedValue = ( unsigned long ) 0, ulReceivedValue;
\r
412 const char * const pcTaskStartMsg = "Queue receive while suspended task started.\r\n";
\r
413 const char * const pcTaskFailMsg = "Queue receive while suspended failed.\r\n";
\r
414 portBASE_TYPE xGotValue;
\r
416 /* Just to stop warning messages. */
\r
417 ( void ) pvParameters;
\r
419 /* Queue a message for printing to say the task has started. */
\r
420 vPrintDisplayMessage( &pcTaskStartMsg );
\r
426 /* Suspending the scheduler here is fairly pointless and
\r
427 undesirable for a normal application. It is done here purely
\r
428 to test the scheduler. The inner xTaskResumeAll() should
\r
429 never return pdTRUE as the scheduler is still locked by the
\r
435 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
437 if( xTaskResumeAll() )
\r
439 xSuspendedQueueReceiveError = pdTRUE;
\r
444 #if configUSE_PREEMPTION == 0
\r
448 } while( xGotValue == pdFALSE );
\r
450 if( ulReceivedValue != ulExpectedValue )
\r
452 if( xSuspendedQueueReceiveError == pdFALSE )
\r
454 vPrintDisplayMessage( &pcTaskFailMsg );
\r
456 xSuspendedQueueReceiveError = pdTRUE;
\r
462 /*-----------------------------------------------------------*/
\r
464 static void prvChangePriorityWhenSuspendedTask( void *pvParameters )
\r
466 const char * const pcTaskStartMsg = "Priority change when suspended task started.\r\n";
\r
467 const char * const pcTaskFailMsg = "Priority change when suspended task failed.\r\n";
\r
469 /* Just to stop warning messages. */
\r
470 ( void ) pvParameters;
\r
472 /* Queue a message for printing to say the task has started. */
\r
473 vPrintDisplayMessage( &pcTaskStartMsg );
\r
477 /* Start with the counter at 0 so we know what the counter should be
\r
478 when we check it next. */
\r
479 ulPrioritySetCounter = ( unsigned long ) 0;
\r
481 /* Resume the helper task. At this time it has a priority lower than
\r
482 ours so no context switch should occur. */
\r
483 vTaskResume( xChangePriorityWhenSuspendedHandle );
\r
485 /* Check to ensure the task just resumed has not executed. */
\r
486 portENTER_CRITICAL();
\r
488 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
490 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
491 vPrintDisplayMessage( &pcTaskFailMsg );
\r
494 portEXIT_CRITICAL();
\r
496 /* Now try raising the priority while the scheduler is suspended. */
\r
499 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, ( configMAX_PRIORITIES - 1 ) );
\r
501 /* Again, even though the helper task has a priority greater than
\r
502 ours, it should not have executed yet because the scheduler is
\r
504 portENTER_CRITICAL();
\r
506 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
508 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
509 vPrintDisplayMessage( &pcTaskFailMsg );
\r
512 portEXIT_CRITICAL();
\r
516 /* Now the scheduler has been resumed the helper task should
\r
517 immediately preempt us and execute. When it executes it will increment
\r
518 the ulPrioritySetCounter exactly once before suspending itself.
\r
520 We should now always find the counter set to 1. */
\r
521 portENTER_CRITICAL();
\r
523 if( ulPrioritySetCounter != ( unsigned long ) 1 )
\r
525 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
526 vPrintDisplayMessage( &pcTaskFailMsg );
\r
529 portEXIT_CRITICAL();
\r
531 /* Delay until we try this again. */
\r
532 vTaskDelay( priSLEEP_TIME * 2 );
\r
534 /* Set the priority of the helper task back ready for the next
\r
535 execution of this task. */
\r
537 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, tskIDLE_PRIORITY );
\r
541 /*-----------------------------------------------------------*/
\r
543 static void prvChangePriorityHelperTask( void *pvParameters )
\r
545 /* Just to stop warning messages. */
\r
546 ( void ) pvParameters;
\r
550 /* This is the helper task for prvChangePriorityWhenSuspendedTask().
\r
551 It has it's priority raised and lowered. When it runs it simply
\r
552 increments the counter then suspends itself again. This allows
\r
553 prvChangePriorityWhenSuspendedTask() to know how many times it has
\r
555 ulPrioritySetCounter++;
\r
556 vTaskSuspend( NULL );
\r
559 /*-----------------------------------------------------------*/
\r
561 /* Called to check that all the created tasks are still running without error. */
\r
562 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
\r
564 /* Keep a history of the check variables so we know if it has been incremented
\r
565 since the last call. */
\r
566 static unsigned short usLastTaskCheck = ( unsigned short ) 0;
\r
567 portBASE_TYPE xReturn = pdTRUE;
\r
569 /* Check the tasks are still running by ensuring the check variable
\r
570 is still incrementing. */
\r
572 if( usCheckVariable == usLastTaskCheck )
\r
574 /* The check has not incremented so an error exists. */
\r
578 if( xSuspendedQueueSendError == pdTRUE )
\r
583 if( xSuspendedQueueReceiveError == pdTRUE )
\r
588 if( xPriorityRaiseWhenSuspendedError == pdTRUE )
\r
593 usLastTaskCheck = usCheckVariable;
\r