2 FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
10 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
15 You should have received a copy of the GNU General Public License along
\r
16 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
17 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
19 A special exception to the GPL is included to allow you to distribute a
\r
20 combined work that includes FreeRTOS.org without being obliged to provide
\r
21 the source code for any proprietary components. See the licensing section
\r
22 of http://www.FreeRTOS.org for full details.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 * The first test creates three tasks - two counter tasks (one continuous count
\r
54 * and one limited count) and one controller. A "count" variable is shared
\r
55 * between all three tasks. The two counter tasks should never be in a "ready"
\r
56 * state at the same time. The controller task runs at the same priority as
\r
57 * the continuous count task, and at a lower priority than the limited count
\r
60 * One counter task loops indefinitely, incrementing the shared count variable
\r
61 * on each iteration. To ensure it has exclusive access to the variable it
\r
62 * raises it's priority above that of the controller task before each
\r
63 * increment, lowering it again to it's original priority before starting the
\r
66 * The other counter task increments the shared count variable on each
\r
67 * iteration of it's loop until the count has reached a limit of 0xff - at
\r
68 * which point it suspends itself. It will not start a new loop until the
\r
69 * controller task has made it "ready" again by calling vTaskResume ().
\r
70 * This second counter task operates at a higher priority than controller
\r
71 * task so does not need to worry about mutual exclusion of the counter
\r
74 * The controller task is in two sections. The first section controls and
\r
75 * monitors the continuous count task. When this section is operational the
\r
76 * limited count task is suspended. Likewise, the second section controls
\r
77 * and monitors the limited count task. When this section is operational the
\r
78 * continuous count task is suspended.
\r
80 * In the first section the controller task first takes a copy of the shared
\r
81 * count variable. To ensure mutual exclusion on the count variable it
\r
82 * suspends the continuous count task, resuming it again when the copy has been
\r
83 * taken. The controller task then sleeps for a fixed period - during which
\r
84 * the continuous count task will execute and increment the shared variable.
\r
85 * When the controller task wakes it checks that the continuous count task
\r
86 * has executed by comparing the copy of the shared variable with its current
\r
87 * value. This time, to ensure mutual exclusion, the scheduler itself is
\r
88 * suspended with a call to vTaskSuspendAll (). This is for demonstration
\r
89 * purposes only and is not a recommended technique due to its inefficiency.
\r
91 * After a fixed number of iterations the controller task suspends the
\r
92 * continuous count task, and moves on to its second section.
\r
94 * At the start of the second section the shared variable is cleared to zero.
\r
95 * The limited count task is then woken from it's suspension by a call to
\r
96 * vTaskResume (). As this counter task operates at a higher priority than
\r
97 * the controller task the controller task should not run again until the
\r
98 * shared variable has been counted up to the limited value causing the counter
\r
99 * task to suspend itself. The next line after vTaskResume () is therefore
\r
100 * a check on the shared variable to ensure everything is as expected.
\r
103 * The second test consists of a couple of very simple tasks that post onto a
\r
104 * queue while the scheduler is suspended. This test was added to test parts
\r
105 * of the scheduler not exercised by the first test.
\r
108 * The final set of two tasks implements a third test. This simply raises the
\r
109 * priority of a task while the scheduler is suspended. Again this test was
\r
110 * added to exercise parts of the code not covered by the first test.
\r
112 * \page Priorities dynamic.c
\r
113 * \ingroup DemoFiles
\r
118 Changes from V2.0.0
\r
120 + Delay periods are now specified using variables and constants of
\r
121 portTickType rather than unsigned portLONG.
\r
122 + Added a second, simple test that uses the functions
\r
123 vQueueReceiveWhenSuspendedTask() and vQueueSendWhenSuspendedTask().
\r
125 Changes from V3.1.1
\r
127 + Added a third simple test that uses the vTaskPrioritySet() function
\r
128 while the scheduler is suspended.
\r
129 + Modified the controller task slightly to test the calling of
\r
130 vTaskResumeAll() while the scheduler is suspended.
\r
133 #include <stdlib.h>
\r
135 /* Scheduler include files. */
\r
136 #include "FreeRTOS.h"
\r
138 #include "semphr.h"
\r
140 /* Demo app include files. */
\r
141 #include "dynamic.h"
\r
144 /* Function that implements the "limited count" task as described above. */
\r
145 static void vLimitedIncrementTask( void * pvParameters );
\r
147 /* Function that implements the "continuous count" task as described above. */
\r
148 static void vContinuousIncrementTask( void * pvParameters );
\r
150 /* Function that implements the controller task as described above. */
\r
151 static void vCounterControlTask( void * pvParameters );
\r
153 /* The simple test functions that check sending and receiving while the
\r
154 scheduler is suspended. */
\r
155 static void vQueueReceiveWhenSuspendedTask( void *pvParameters );
\r
156 static void vQueueSendWhenSuspendedTask( void *pvParameters );
\r
158 /* The simple test functions that check raising and lowering of task priorities
\r
159 while the scheduler is suspended. */
\r
160 static void prvChangePriorityWhenSuspendedTask( void *pvParameters );
\r
161 static void prvChangePriorityHelperTask( void *pvParameters );
\r
164 /* Demo task specific constants. */
\r
165 #define priSTACK_SIZE ( ( unsigned portSHORT ) configMINIMAL_STACK_SIZE )
\r
166 #define priSLEEP_TIME ( ( portTickType ) 50 )
\r
167 #define priLOOPS ( 5 )
\r
168 #define priMAX_COUNT ( ( unsigned portLONG ) 0xff )
\r
169 #define priNO_BLOCK ( ( portTickType ) 0 )
\r
170 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
172 /*-----------------------------------------------------------*/
\r
174 /* Handles to the two counter tasks. These could be passed in as parameters
\r
175 to the controller task to prevent them having to be file scope. */
\r
176 static xTaskHandle xContinuousIncrementHandle, xLimitedIncrementHandle, xChangePriorityWhenSuspendedHandle;
\r
178 /* The shared counter variable. This is passed in as a parameter to the two
\r
179 counter variables for demonstration purposes. */
\r
180 static unsigned portLONG ulCounter;
\r
182 /* Variable used in a similar way by the test that checks the raising and
\r
183 lowering of task priorities while the scheduler is suspended. */
\r
184 static unsigned portLONG ulPrioritySetCounter;
\r
186 /* Variables used to check that the tasks are still operating without error.
\r
187 Each complete iteration of the controller task increments this variable
\r
188 provided no errors have been found. The variable maintaining the same value
\r
189 is therefore indication of an error. */
\r
190 static unsigned portSHORT usCheckVariable = ( unsigned portSHORT ) 0;
\r
191 static portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
\r
192 static portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
\r
193 static portBASE_TYPE xPriorityRaiseWhenSuspendedError = pdFALSE;
\r
195 /* Queue used by the second test. */
\r
196 xQueueHandle xSuspendedTestQueue;
\r
198 /*-----------------------------------------------------------*/
\r
200 * Start the seven tasks as described at the top of the file.
\r
201 * Note that the limited count task is given a higher priority.
\r
203 void vStartDynamicPriorityTasks( void )
\r
205 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
206 xTaskCreate( vContinuousIncrementTask, "CONT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
\r
207 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
208 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
209 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_SEND", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
210 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RECV", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
211 xTaskCreate( prvChangePriorityWhenSuspendedTask, "1st_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
212 xTaskCreate( prvChangePriorityHelperTask, "2nd_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xChangePriorityWhenSuspendedHandle );
\r
214 /*-----------------------------------------------------------*/
\r
217 * Just loops around incrementing the shared variable until the limit has been
\r
218 * reached. Once the limit has been reached it suspends itself.
\r
220 static void vLimitedIncrementTask( void * pvParameters )
\r
222 unsigned portLONG *pulCounter;
\r
224 /* Take a pointer to the shared variable from the parameters passed into
\r
226 pulCounter = ( unsigned portLONG * ) pvParameters;
\r
228 /* This will run before the control task, so the first thing it does is
\r
229 suspend - the control task will resume it when ready. */
\r
230 vTaskSuspend( NULL );
\r
234 /* Just count up to a value then suspend. */
\r
235 ( *pulCounter )++;
\r
237 if( *pulCounter >= priMAX_COUNT )
\r
239 vTaskSuspend( NULL );
\r
243 /*-----------------------------------------------------------*/
\r
246 * Just keep counting the shared variable up. The control task will suspend
\r
247 * this task when it wants.
\r
249 static void vContinuousIncrementTask( void * pvParameters )
\r
251 unsigned portLONG *pulCounter;
\r
252 unsigned portBASE_TYPE uxOurPriority;
\r
254 /* Take a pointer to the shared variable from the parameters passed into
\r
256 pulCounter = ( unsigned portLONG * ) pvParameters;
\r
258 /* Query our priority so we can raise it when exclusive access to the
\r
259 shared variable is required. */
\r
260 uxOurPriority = uxTaskPriorityGet( NULL );
\r
264 /* Raise our priority above the controller task to ensure a context
\r
265 switch does not occur while we are accessing this variable. */
\r
266 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
267 ( *pulCounter )++;
\r
268 vTaskPrioritySet( NULL, uxOurPriority );
\r
270 #if configUSE_PREEMPTION == 0
\r
275 /*-----------------------------------------------------------*/
\r
278 * Controller task as described above.
\r
280 static void vCounterControlTask( void * pvParameters )
\r
282 unsigned portLONG ulLastCounter;
\r
284 portSHORT sError = pdFALSE;
\r
285 const portCHAR * const pcTaskStartMsg = "Priority manipulation tasks started.\r\n";
\r
286 const portCHAR * const pcTaskFailMsg = "Priority manipulation Task Failed\r\n";
\r
288 /* Just to stop warning messages. */
\r
289 ( void ) pvParameters;
\r
291 /* Queue a message for printing to say the task has started. */
\r
292 vPrintDisplayMessage( &pcTaskStartMsg );
\r
296 /* Start with the counter at zero. */
\r
297 ulCounter = ( unsigned portLONG ) 0;
\r
299 /* First section : */
\r
301 /* Check the continuous count task is running. */
\r
302 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
304 /* Suspend the continuous count task so we can take a mirror of the
\r
305 shared variable without risk of corruption. */
\r
306 vTaskSuspend( xContinuousIncrementHandle );
\r
307 ulLastCounter = ulCounter;
\r
308 vTaskResume( xContinuousIncrementHandle );
\r
310 /* Now delay to ensure the other task has processor time. */
\r
311 vTaskDelay( priSLEEP_TIME );
\r
313 /* Check the shared variable again. This time to ensure mutual
\r
314 exclusion the whole scheduler will be locked. This is just for
\r
318 if( ulLastCounter == ulCounter )
\r
320 /* The shared variable has not changed. There is a problem
\r
321 with the continuous count task so flag an error. */
\r
324 vPrintDisplayMessage( &pcTaskFailMsg );
\r
332 /* Second section: */
\r
334 /* Suspend the continuous counter task so it stops accessing the shared variable. */
\r
335 vTaskSuspend( xContinuousIncrementHandle );
\r
337 /* Reset the variable. */
\r
338 ulCounter = ( unsigned portLONG ) 0;
\r
340 /* Resume the limited count task which has a higher priority than us.
\r
341 We should therefore not return from this call until the limited count
\r
342 task has suspended itself with a known value in the counter variable.
\r
343 The scheduler suspension is not necessary but is included for test
\r
346 vTaskResume( xLimitedIncrementHandle );
\r
349 /* Does the counter variable have the expected value? */
\r
350 if( ulCounter != priMAX_COUNT )
\r
353 vPrintDisplayMessage( &pcTaskFailMsg );
\r
356 if( sError == pdFALSE )
\r
358 /* If no errors have occurred then increment the check variable. */
\r
359 portENTER_CRITICAL();
\r
361 portEXIT_CRITICAL();
\r
364 #if configUSE_PREEMPTION == 0
\r
368 /* Resume the continuous count task and do it all again. */
\r
369 vTaskResume( xContinuousIncrementHandle );
\r
372 /*-----------------------------------------------------------*/
\r
374 static void vQueueSendWhenSuspendedTask( void *pvParameters )
\r
376 static unsigned portLONG ulValueToSend = ( unsigned portLONG ) 0;
\r
377 const portCHAR * const pcTaskStartMsg = "Queue send while suspended task started.\r\n";
\r
378 const portCHAR * const pcTaskFailMsg = "Queue send while suspended failed.\r\n";
\r
380 /* Just to stop warning messages. */
\r
381 ( void ) pvParameters;
\r
383 /* Queue a message for printing to say the task has started. */
\r
384 vPrintDisplayMessage( &pcTaskStartMsg );
\r
390 /* We must not block while the scheduler is suspended! */
\r
391 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
393 if( xSuspendedQueueSendError == pdFALSE )
\r
396 vPrintDisplayMessage( &pcTaskFailMsg );
\r
400 xSuspendedQueueSendError = pdTRUE;
\r
405 vTaskDelay( priSLEEP_TIME );
\r
410 /*-----------------------------------------------------------*/
\r
412 static void vQueueReceiveWhenSuspendedTask( void *pvParameters )
\r
414 static unsigned portLONG ulExpectedValue = ( unsigned portLONG ) 0, ulReceivedValue;
\r
415 const portCHAR * const pcTaskStartMsg = "Queue receive while suspended task started.\r\n";
\r
416 const portCHAR * const pcTaskFailMsg = "Queue receive while suspended failed.\r\n";
\r
417 portBASE_TYPE xGotValue;
\r
419 /* Just to stop warning messages. */
\r
420 ( void ) pvParameters;
\r
422 /* Queue a message for printing to say the task has started. */
\r
423 vPrintDisplayMessage( &pcTaskStartMsg );
\r
429 /* Suspending the scheduler here is fairly pointless and
\r
430 undesirable for a normal application. It is done here purely
\r
431 to test the scheduler. The inner xTaskResumeAll() should
\r
432 never return pdTRUE as the scheduler is still locked by the
\r
438 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
440 if( xTaskResumeAll() )
\r
442 xSuspendedQueueReceiveError = pdTRUE;
\r
447 #if configUSE_PREEMPTION == 0
\r
451 } while( xGotValue == pdFALSE );
\r
453 if( ulReceivedValue != ulExpectedValue )
\r
455 if( xSuspendedQueueReceiveError == pdFALSE )
\r
457 vPrintDisplayMessage( &pcTaskFailMsg );
\r
459 xSuspendedQueueReceiveError = pdTRUE;
\r
465 /*-----------------------------------------------------------*/
\r
467 static void prvChangePriorityWhenSuspendedTask( void *pvParameters )
\r
469 const portCHAR * const pcTaskStartMsg = "Priority change when suspended task started.\r\n";
\r
470 const portCHAR * const pcTaskFailMsg = "Priority change when suspended task failed.\r\n";
\r
472 /* Just to stop warning messages. */
\r
473 ( void ) pvParameters;
\r
475 /* Queue a message for printing to say the task has started. */
\r
476 vPrintDisplayMessage( &pcTaskStartMsg );
\r
480 /* Start with the counter at 0 so we know what the counter should be
\r
481 when we check it next. */
\r
482 ulPrioritySetCounter = ( unsigned portLONG ) 0;
\r
484 /* Resume the helper task. At this time it has a priority lower than
\r
485 ours so no context switch should occur. */
\r
486 vTaskResume( xChangePriorityWhenSuspendedHandle );
\r
488 /* Check to ensure the task just resumed has not executed. */
\r
489 portENTER_CRITICAL();
\r
491 if( ulPrioritySetCounter != ( unsigned portLONG ) 0 )
\r
493 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
494 vPrintDisplayMessage( &pcTaskFailMsg );
\r
497 portEXIT_CRITICAL();
\r
499 /* Now try raising the priority while the scheduler is suspended. */
\r
502 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, ( configMAX_PRIORITIES - 1 ) );
\r
504 /* Again, even though the helper task has a priority greater than
\r
505 ours, it should not have executed yet because the scheduler is
\r
507 portENTER_CRITICAL();
\r
509 if( ulPrioritySetCounter != ( unsigned portLONG ) 0 )
\r
511 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
512 vPrintDisplayMessage( &pcTaskFailMsg );
\r
515 portEXIT_CRITICAL();
\r
519 /* Now the scheduler has been resumed the helper task should
\r
520 immediately preempt us and execute. When it executes it will increment
\r
521 the ulPrioritySetCounter exactly once before suspending itself.
\r
523 We should now always find the counter set to 1. */
\r
524 portENTER_CRITICAL();
\r
526 if( ulPrioritySetCounter != ( unsigned portLONG ) 1 )
\r
528 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
529 vPrintDisplayMessage( &pcTaskFailMsg );
\r
532 portEXIT_CRITICAL();
\r
534 /* Delay until we try this again. */
\r
535 vTaskDelay( priSLEEP_TIME * 2 );
\r
537 /* Set the priority of the helper task back ready for the next
\r
538 execution of this task. */
\r
540 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, tskIDLE_PRIORITY );
\r
544 /*-----------------------------------------------------------*/
\r
546 static void prvChangePriorityHelperTask( void *pvParameters )
\r
548 /* Just to stop warning messages. */
\r
549 ( void ) pvParameters;
\r
553 /* This is the helper task for prvChangePriorityWhenSuspendedTask().
\r
554 It has it's priority raised and lowered. When it runs it simply
\r
555 increments the counter then suspends itself again. This allows
\r
556 prvChangePriorityWhenSuspendedTask() to know how many times it has
\r
558 ulPrioritySetCounter++;
\r
559 vTaskSuspend( NULL );
\r
562 /*-----------------------------------------------------------*/
\r
564 /* Called to check that all the created tasks are still running without error. */
\r
565 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
\r
567 /* Keep a history of the check variables so we know if it has been incremented
\r
568 since the last call. */
\r
569 static unsigned portSHORT usLastTaskCheck = ( unsigned portSHORT ) 0;
\r
570 portBASE_TYPE xReturn = pdTRUE;
\r
572 /* Check the tasks are still running by ensuring the check variable
\r
573 is still incrementing. */
\r
575 if( usCheckVariable == usLastTaskCheck )
\r
577 /* The check has not incremented so an error exists. */
\r
581 if( xSuspendedQueueSendError == pdTRUE )
\r
586 if( xSuspendedQueueReceiveError == pdTRUE )
\r
591 if( xPriorityRaiseWhenSuspendedError == pdTRUE )
\r
596 usLastTaskCheck = usCheckVariable;
\r