2 FreeRTOS.org V5.1.0 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
51 * The first test creates three tasks - two counter tasks (one continuous count
\r
52 * and one limited count) and one controller. A "count" variable is shared
\r
53 * between all three tasks. The two counter tasks should never be in a "ready"
\r
54 * state at the same time. The controller task runs at the same priority as
\r
55 * the continuous count task, and at a lower priority than the limited count
\r
58 * One counter task loops indefinitely, incrementing the shared count variable
\r
59 * on each iteration. To ensure it has exclusive access to the variable it
\r
60 * raises it's priority above that of the controller task before each
\r
61 * increment, lowering it again to it's original priority before starting the
\r
64 * The other counter task increments the shared count variable on each
\r
65 * iteration of it's loop until the count has reached a limit of 0xff - at
\r
66 * which point it suspends itself. It will not start a new loop until the
\r
67 * controller task has made it "ready" again by calling vTaskResume ().
\r
68 * This second counter task operates at a higher priority than controller
\r
69 * task so does not need to worry about mutual exclusion of the counter
\r
72 * The controller task is in two sections. The first section controls and
\r
73 * monitors the continuous count task. When this section is operational the
\r
74 * limited count task is suspended. Likewise, the second section controls
\r
75 * and monitors the limited count task. When this section is operational the
\r
76 * continuous count task is suspended.
\r
78 * In the first section the controller task first takes a copy of the shared
\r
79 * count variable. To ensure mutual exclusion on the count variable it
\r
80 * suspends the continuous count task, resuming it again when the copy has been
\r
81 * taken. The controller task then sleeps for a fixed period - during which
\r
82 * the continuous count task will execute and increment the shared variable.
\r
83 * When the controller task wakes it checks that the continuous count task
\r
84 * has executed by comparing the copy of the shared variable with its current
\r
85 * value. This time, to ensure mutual exclusion, the scheduler itself is
\r
86 * suspended with a call to vTaskSuspendAll (). This is for demonstration
\r
87 * purposes only and is not a recommended technique due to its inefficiency.
\r
89 * After a fixed number of iterations the controller task suspends the
\r
90 * continuous count task, and moves on to its second section.
\r
92 * At the start of the second section the shared variable is cleared to zero.
\r
93 * The limited count task is then woken from it's suspension by a call to
\r
94 * vTaskResume (). As this counter task operates at a higher priority than
\r
95 * the controller task the controller task should not run again until the
\r
96 * shared variable has been counted up to the limited value causing the counter
\r
97 * task to suspend itself. The next line after vTaskResume () is therefore
\r
98 * a check on the shared variable to ensure everything is as expected.
\r
101 * The second test consists of a couple of very simple tasks that post onto a
\r
102 * queue while the scheduler is suspended. This test was added to test parts
\r
103 * of the scheduler not exercised by the first test.
\r
106 * The final set of two tasks implements a third test. This simply raises the
\r
107 * priority of a task while the scheduler is suspended. Again this test was
\r
108 * added to exercise parts of the code not covered by the first test.
\r
110 * \page Priorities dynamic.c
\r
111 * \ingroup DemoFiles
\r
116 Changes from V2.0.0
\r
118 + Delay periods are now specified using variables and constants of
\r
119 portTickType rather than unsigned portLONG.
\r
120 + Added a second, simple test that uses the functions
\r
121 vQueueReceiveWhenSuspendedTask() and vQueueSendWhenSuspendedTask().
\r
123 Changes from V3.1.1
\r
125 + Added a third simple test that uses the vTaskPrioritySet() function
\r
126 while the scheduler is suspended.
\r
127 + Modified the controller task slightly to test the calling of
\r
128 vTaskResumeAll() while the scheduler is suspended.
\r
131 #include <stdlib.h>
\r
133 /* Scheduler include files. */
\r
134 #include "FreeRTOS.h"
\r
136 #include "semphr.h"
\r
138 /* Demo app include files. */
\r
139 #include "dynamic.h"
\r
142 /* Function that implements the "limited count" task as described above. */
\r
143 static void vLimitedIncrementTask( void * pvParameters );
\r
145 /* Function that implements the "continuous count" task as described above. */
\r
146 static void vContinuousIncrementTask( void * pvParameters );
\r
148 /* Function that implements the controller task as described above. */
\r
149 static void vCounterControlTask( void * pvParameters );
\r
151 /* The simple test functions that check sending and receiving while the
\r
152 scheduler is suspended. */
\r
153 static void vQueueReceiveWhenSuspendedTask( void *pvParameters );
\r
154 static void vQueueSendWhenSuspendedTask( void *pvParameters );
\r
156 /* The simple test functions that check raising and lowering of task priorities
\r
157 while the scheduler is suspended. */
\r
158 static void prvChangePriorityWhenSuspendedTask( void *pvParameters );
\r
159 static void prvChangePriorityHelperTask( void *pvParameters );
\r
162 /* Demo task specific constants. */
\r
163 #define priSTACK_SIZE ( ( unsigned portSHORT ) configMINIMAL_STACK_SIZE )
\r
164 #define priSLEEP_TIME ( ( portTickType ) 50 )
\r
165 #define priLOOPS ( 5 )
\r
166 #define priMAX_COUNT ( ( unsigned portLONG ) 0xff )
\r
167 #define priNO_BLOCK ( ( portTickType ) 0 )
\r
168 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
170 /*-----------------------------------------------------------*/
\r
172 /* Handles to the two counter tasks. These could be passed in as parameters
\r
173 to the controller task to prevent them having to be file scope. */
\r
174 static xTaskHandle xContinuousIncrementHandle, xLimitedIncrementHandle, xChangePriorityWhenSuspendedHandle;
\r
176 /* The shared counter variable. This is passed in as a parameter to the two
\r
177 counter variables for demonstration purposes. */
\r
178 static unsigned portLONG ulCounter;
\r
180 /* Variable used in a similar way by the test that checks the raising and
\r
181 lowering of task priorities while the scheduler is suspended. */
\r
182 static unsigned portLONG ulPrioritySetCounter;
\r
184 /* Variables used to check that the tasks are still operating without error.
\r
185 Each complete iteration of the controller task increments this variable
\r
186 provided no errors have been found. The variable maintaining the same value
\r
187 is therefore indication of an error. */
\r
188 static unsigned portSHORT usCheckVariable = ( unsigned portSHORT ) 0;
\r
189 static portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
\r
190 static portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
\r
191 static portBASE_TYPE xPriorityRaiseWhenSuspendedError = pdFALSE;
\r
193 /* Queue used by the second test. */
\r
194 xQueueHandle xSuspendedTestQueue;
\r
196 /*-----------------------------------------------------------*/
\r
198 * Start the seven tasks as described at the top of the file.
\r
199 * Note that the limited count task is given a higher priority.
\r
201 void vStartDynamicPriorityTasks( void )
\r
203 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
204 xTaskCreate( vContinuousIncrementTask, "CONT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
\r
205 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
206 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
207 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_SEND", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
208 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RECV", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
209 xTaskCreate( prvChangePriorityWhenSuspendedTask, "1st_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
210 xTaskCreate( prvChangePriorityHelperTask, "2nd_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xChangePriorityWhenSuspendedHandle );
\r
212 /*-----------------------------------------------------------*/
\r
215 * Just loops around incrementing the shared variable until the limit has been
\r
216 * reached. Once the limit has been reached it suspends itself.
\r
218 static void vLimitedIncrementTask( void * pvParameters )
\r
220 unsigned portLONG *pulCounter;
\r
222 /* Take a pointer to the shared variable from the parameters passed into
\r
224 pulCounter = ( unsigned portLONG * ) pvParameters;
\r
226 /* This will run before the control task, so the first thing it does is
\r
227 suspend - the control task will resume it when ready. */
\r
228 vTaskSuspend( NULL );
\r
232 /* Just count up to a value then suspend. */
\r
233 ( *pulCounter )++;
\r
235 if( *pulCounter >= priMAX_COUNT )
\r
237 vTaskSuspend( NULL );
\r
241 /*-----------------------------------------------------------*/
\r
244 * Just keep counting the shared variable up. The control task will suspend
\r
245 * this task when it wants.
\r
247 static void vContinuousIncrementTask( void * pvParameters )
\r
249 unsigned portLONG *pulCounter;
\r
250 unsigned portBASE_TYPE uxOurPriority;
\r
252 /* Take a pointer to the shared variable from the parameters passed into
\r
254 pulCounter = ( unsigned portLONG * ) pvParameters;
\r
256 /* Query our priority so we can raise it when exclusive access to the
\r
257 shared variable is required. */
\r
258 uxOurPriority = uxTaskPriorityGet( NULL );
\r
262 /* Raise our priority above the controller task to ensure a context
\r
263 switch does not occur while we are accessing this variable. */
\r
264 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
265 ( *pulCounter )++;
\r
266 vTaskPrioritySet( NULL, uxOurPriority );
\r
268 #if configUSE_PREEMPTION == 0
\r
273 /*-----------------------------------------------------------*/
\r
276 * Controller task as described above.
\r
278 static void vCounterControlTask( void * pvParameters )
\r
280 unsigned portLONG ulLastCounter;
\r
282 portSHORT sError = pdFALSE;
\r
283 const portCHAR * const pcTaskStartMsg = "Priority manipulation tasks started.\r\n";
\r
284 const portCHAR * const pcTaskFailMsg = "Priority manipulation Task Failed\r\n";
\r
286 /* Just to stop warning messages. */
\r
287 ( void ) pvParameters;
\r
289 /* Queue a message for printing to say the task has started. */
\r
290 vPrintDisplayMessage( &pcTaskStartMsg );
\r
294 /* Start with the counter at zero. */
\r
295 ulCounter = ( unsigned portLONG ) 0;
\r
297 /* First section : */
\r
299 /* Check the continuous count task is running. */
\r
300 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
302 /* Suspend the continuous count task so we can take a mirror of the
\r
303 shared variable without risk of corruption. */
\r
304 vTaskSuspend( xContinuousIncrementHandle );
\r
305 ulLastCounter = ulCounter;
\r
306 vTaskResume( xContinuousIncrementHandle );
\r
308 /* Now delay to ensure the other task has processor time. */
\r
309 vTaskDelay( priSLEEP_TIME );
\r
311 /* Check the shared variable again. This time to ensure mutual
\r
312 exclusion the whole scheduler will be locked. This is just for
\r
316 if( ulLastCounter == ulCounter )
\r
318 /* The shared variable has not changed. There is a problem
\r
319 with the continuous count task so flag an error. */
\r
322 vPrintDisplayMessage( &pcTaskFailMsg );
\r
330 /* Second section: */
\r
332 /* Suspend the continuous counter task so it stops accessing the shared variable. */
\r
333 vTaskSuspend( xContinuousIncrementHandle );
\r
335 /* Reset the variable. */
\r
336 ulCounter = ( unsigned portLONG ) 0;
\r
338 /* Resume the limited count task which has a higher priority than us.
\r
339 We should therefore not return from this call until the limited count
\r
340 task has suspended itself with a known value in the counter variable.
\r
341 The scheduler suspension is not necessary but is included for test
\r
344 vTaskResume( xLimitedIncrementHandle );
\r
347 /* Does the counter variable have the expected value? */
\r
348 if( ulCounter != priMAX_COUNT )
\r
351 vPrintDisplayMessage( &pcTaskFailMsg );
\r
354 if( sError == pdFALSE )
\r
356 /* If no errors have occurred then increment the check variable. */
\r
357 portENTER_CRITICAL();
\r
359 portEXIT_CRITICAL();
\r
362 #if configUSE_PREEMPTION == 0
\r
366 /* Resume the continuous count task and do it all again. */
\r
367 vTaskResume( xContinuousIncrementHandle );
\r
370 /*-----------------------------------------------------------*/
\r
372 static void vQueueSendWhenSuspendedTask( void *pvParameters )
\r
374 static unsigned portLONG ulValueToSend = ( unsigned portLONG ) 0;
\r
375 const portCHAR * const pcTaskStartMsg = "Queue send while suspended task started.\r\n";
\r
376 const portCHAR * const pcTaskFailMsg = "Queue send while suspended failed.\r\n";
\r
378 /* Just to stop warning messages. */
\r
379 ( void ) pvParameters;
\r
381 /* Queue a message for printing to say the task has started. */
\r
382 vPrintDisplayMessage( &pcTaskStartMsg );
\r
388 /* We must not block while the scheduler is suspended! */
\r
389 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
391 if( xSuspendedQueueSendError == pdFALSE )
\r
394 vPrintDisplayMessage( &pcTaskFailMsg );
\r
398 xSuspendedQueueSendError = pdTRUE;
\r
403 vTaskDelay( priSLEEP_TIME );
\r
408 /*-----------------------------------------------------------*/
\r
410 static void vQueueReceiveWhenSuspendedTask( void *pvParameters )
\r
412 static unsigned portLONG ulExpectedValue = ( unsigned portLONG ) 0, ulReceivedValue;
\r
413 const portCHAR * const pcTaskStartMsg = "Queue receive while suspended task started.\r\n";
\r
414 const portCHAR * const pcTaskFailMsg = "Queue receive while suspended failed.\r\n";
\r
415 portBASE_TYPE xGotValue;
\r
417 /* Just to stop warning messages. */
\r
418 ( void ) pvParameters;
\r
420 /* Queue a message for printing to say the task has started. */
\r
421 vPrintDisplayMessage( &pcTaskStartMsg );
\r
427 /* Suspending the scheduler here is fairly pointless and
\r
428 undesirable for a normal application. It is done here purely
\r
429 to test the scheduler. The inner xTaskResumeAll() should
\r
430 never return pdTRUE as the scheduler is still locked by the
\r
436 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
438 if( xTaskResumeAll() )
\r
440 xSuspendedQueueReceiveError = pdTRUE;
\r
445 #if configUSE_PREEMPTION == 0
\r
449 } while( xGotValue == pdFALSE );
\r
451 if( ulReceivedValue != ulExpectedValue )
\r
453 if( xSuspendedQueueReceiveError == pdFALSE )
\r
455 vPrintDisplayMessage( &pcTaskFailMsg );
\r
457 xSuspendedQueueReceiveError = pdTRUE;
\r
463 /*-----------------------------------------------------------*/
\r
465 static void prvChangePriorityWhenSuspendedTask( void *pvParameters )
\r
467 const portCHAR * const pcTaskStartMsg = "Priority change when suspended task started.\r\n";
\r
468 const portCHAR * const pcTaskFailMsg = "Priority change when suspended task failed.\r\n";
\r
470 /* Just to stop warning messages. */
\r
471 ( void ) pvParameters;
\r
473 /* Queue a message for printing to say the task has started. */
\r
474 vPrintDisplayMessage( &pcTaskStartMsg );
\r
478 /* Start with the counter at 0 so we know what the counter should be
\r
479 when we check it next. */
\r
480 ulPrioritySetCounter = ( unsigned portLONG ) 0;
\r
482 /* Resume the helper task. At this time it has a priority lower than
\r
483 ours so no context switch should occur. */
\r
484 vTaskResume( xChangePriorityWhenSuspendedHandle );
\r
486 /* Check to ensure the task just resumed has not executed. */
\r
487 portENTER_CRITICAL();
\r
489 if( ulPrioritySetCounter != ( unsigned portLONG ) 0 )
\r
491 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
492 vPrintDisplayMessage( &pcTaskFailMsg );
\r
495 portEXIT_CRITICAL();
\r
497 /* Now try raising the priority while the scheduler is suspended. */
\r
500 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, ( configMAX_PRIORITIES - 1 ) );
\r
502 /* Again, even though the helper task has a priority greater than
\r
503 ours, it should not have executed yet because the scheduler is
\r
505 portENTER_CRITICAL();
\r
507 if( ulPrioritySetCounter != ( unsigned portLONG ) 0 )
\r
509 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
510 vPrintDisplayMessage( &pcTaskFailMsg );
\r
513 portEXIT_CRITICAL();
\r
517 /* Now the scheduler has been resumed the helper task should
\r
518 immediately preempt us and execute. When it executes it will increment
\r
519 the ulPrioritySetCounter exactly once before suspending itself.
\r
521 We should now always find the counter set to 1. */
\r
522 portENTER_CRITICAL();
\r
524 if( ulPrioritySetCounter != ( unsigned portLONG ) 1 )
\r
526 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
527 vPrintDisplayMessage( &pcTaskFailMsg );
\r
530 portEXIT_CRITICAL();
\r
532 /* Delay until we try this again. */
\r
533 vTaskDelay( priSLEEP_TIME * 2 );
\r
535 /* Set the priority of the helper task back ready for the next
\r
536 execution of this task. */
\r
538 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, tskIDLE_PRIORITY );
\r
542 /*-----------------------------------------------------------*/
\r
544 static void prvChangePriorityHelperTask( void *pvParameters )
\r
546 /* Just to stop warning messages. */
\r
547 ( void ) pvParameters;
\r
551 /* This is the helper task for prvChangePriorityWhenSuspendedTask().
\r
552 It has it's priority raised and lowered. When it runs it simply
\r
553 increments the counter then suspends itself again. This allows
\r
554 prvChangePriorityWhenSuspendedTask() to know how many times it has
\r
556 ulPrioritySetCounter++;
\r
557 vTaskSuspend( NULL );
\r
560 /*-----------------------------------------------------------*/
\r
562 /* Called to check that all the created tasks are still running without error. */
\r
563 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
\r
565 /* Keep a history of the check variables so we know if it has been incremented
\r
566 since the last call. */
\r
567 static unsigned portSHORT usLastTaskCheck = ( unsigned portSHORT ) 0;
\r
568 portBASE_TYPE xReturn = pdTRUE;
\r
570 /* Check the tasks are still running by ensuring the check variable
\r
571 is still incrementing. */
\r
573 if( usCheckVariable == usLastTaskCheck )
\r
575 /* The check has not incremented so an error exists. */
\r
579 if( xSuspendedQueueSendError == pdTRUE )
\r
584 if( xSuspendedQueueReceiveError == pdTRUE )
\r
589 if( xPriorityRaiseWhenSuspendedError == pdTRUE )
\r
594 usLastTaskCheck = usCheckVariable;
\r