2 FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\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 modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 * The first test creates three tasks - two counter tasks (one continuous count
\r
69 * and one limited count) and one controller. A "count" variable is shared
\r
70 * between all three tasks. The two counter tasks should never be in a "ready"
\r
71 * state at the same time. The controller task runs at the same priority as
\r
72 * the continuous count task, and at a lower priority than the limited count
\r
75 * One counter task loops indefinitely, incrementing the shared count variable
\r
76 * on each iteration. To ensure it has exclusive access to the variable it
\r
77 * raises it's priority above that of the controller task before each
\r
78 * increment, lowering it again to it's original priority before starting the
\r
81 * The other counter task increments the shared count variable on each
\r
82 * iteration of it's loop until the count has reached a limit of 0xff - at
\r
83 * which point it suspends itself. It will not start a new loop until the
\r
84 * controller task has made it "ready" again by calling vTaskResume ().
\r
85 * This second counter task operates at a higher priority than controller
\r
86 * task so does not need to worry about mutual exclusion of the counter
\r
89 * The controller task is in two sections. The first section controls and
\r
90 * monitors the continuous count task. When this section is operational the
\r
91 * limited count task is suspended. Likewise, the second section controls
\r
92 * and monitors the limited count task. When this section is operational the
\r
93 * continuous count task is suspended.
\r
95 * In the first section the controller task first takes a copy of the shared
\r
96 * count variable. To ensure mutual exclusion on the count variable it
\r
97 * suspends the continuous count task, resuming it again when the copy has been
\r
98 * taken. The controller task then sleeps for a fixed period - during which
\r
99 * the continuous count task will execute and increment the shared variable.
\r
100 * When the controller task wakes it checks that the continuous count task
\r
101 * has executed by comparing the copy of the shared variable with its current
\r
102 * value. This time, to ensure mutual exclusion, the scheduler itself is
\r
103 * suspended with a call to vTaskSuspendAll (). This is for demonstration
\r
104 * purposes only and is not a recommended technique due to its inefficiency.
\r
106 * After a fixed number of iterations the controller task suspends the
\r
107 * continuous count task, and moves on to its second section.
\r
109 * At the start of the second section the shared variable is cleared to zero.
\r
110 * The limited count task is then woken from it's suspension by a call to
\r
111 * vTaskResume (). As this counter task operates at a higher priority than
\r
112 * the controller task the controller task should not run again until the
\r
113 * shared variable has been counted up to the limited value causing the counter
\r
114 * task to suspend itself. The next line after vTaskResume () is therefore
\r
115 * a check on the shared variable to ensure everything is as expected.
\r
118 * The second test consists of a couple of very simple tasks that post onto a
\r
119 * queue while the scheduler is suspended. This test was added to test parts
\r
120 * of the scheduler not exercised by the first test.
\r
123 * The final set of two tasks implements a third test. This simply raises the
\r
124 * priority of a task while the scheduler is suspended. Again this test was
\r
125 * added to exercise parts of the code not covered by the first test.
\r
127 * \page Priorities dynamic.c
\r
128 * \ingroup DemoFiles
\r
133 Changes from V2.0.0
\r
135 + Delay periods are now specified using variables and constants of
\r
136 portTickType rather than unsigned long.
\r
137 + Added a second, simple test that uses the functions
\r
138 vQueueReceiveWhenSuspendedTask() and vQueueSendWhenSuspendedTask().
\r
140 Changes from V3.1.1
\r
142 + Added a third simple test that uses the vTaskPrioritySet() function
\r
143 while the scheduler is suspended.
\r
144 + Modified the controller task slightly to test the calling of
\r
145 vTaskResumeAll() while the scheduler is suspended.
\r
148 #include <stdlib.h>
\r
150 /* Scheduler include files. */
\r
151 #include "FreeRTOS.h"
\r
153 #include "semphr.h"
\r
155 /* Demo app include files. */
\r
156 #include "dynamic.h"
\r
159 /* Function that implements the "limited count" task as described above. */
\r
160 static void vLimitedIncrementTask( void * pvParameters );
\r
162 /* Function that implements the "continuous count" task as described above. */
\r
163 static void vContinuousIncrementTask( void * pvParameters );
\r
165 /* Function that implements the controller task as described above. */
\r
166 static void vCounterControlTask( void * pvParameters );
\r
168 /* The simple test functions that check sending and receiving while the
\r
169 scheduler is suspended. */
\r
170 static void vQueueReceiveWhenSuspendedTask( void *pvParameters );
\r
171 static void vQueueSendWhenSuspendedTask( void *pvParameters );
\r
173 /* The simple test functions that check raising and lowering of task priorities
\r
174 while the scheduler is suspended. */
\r
175 static void prvChangePriorityWhenSuspendedTask( void *pvParameters );
\r
176 static void prvChangePriorityHelperTask( void *pvParameters );
\r
179 /* Demo task specific constants. */
\r
180 #define priSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
\r
181 #define priSLEEP_TIME ( ( portTickType ) 50 )
\r
182 #define priLOOPS ( 5 )
\r
183 #define priMAX_COUNT ( ( unsigned long ) 0xff )
\r
184 #define priNO_BLOCK ( ( portTickType ) 0 )
\r
185 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
187 /*-----------------------------------------------------------*/
\r
189 /* Handles to the two counter tasks. These could be passed in as parameters
\r
190 to the controller task to prevent them having to be file scope. */
\r
191 static xTaskHandle xContinuousIncrementHandle, xLimitedIncrementHandle, xChangePriorityWhenSuspendedHandle;
\r
193 /* The shared counter variable. This is passed in as a parameter to the two
\r
194 counter variables for demonstration purposes. */
\r
195 static unsigned long ulCounter;
\r
197 /* Variable used in a similar way by the test that checks the raising and
\r
198 lowering of task priorities while the scheduler is suspended. */
\r
199 static unsigned long ulPrioritySetCounter;
\r
201 /* Variables used to check that the tasks are still operating without error.
\r
202 Each complete iteration of the controller task increments this variable
\r
203 provided no errors have been found. The variable maintaining the same value
\r
204 is therefore indication of an error. */
\r
205 static unsigned short usCheckVariable = ( unsigned short ) 0;
\r
206 static portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
\r
207 static portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
\r
208 static portBASE_TYPE xPriorityRaiseWhenSuspendedError = pdFALSE;
\r
210 /* Queue used by the second test. */
\r
211 xQueueHandle xSuspendedTestQueue;
\r
213 /*-----------------------------------------------------------*/
\r
215 * Start the seven tasks as described at the top of the file.
\r
216 * Note that the limited count task is given a higher priority.
\r
218 void vStartDynamicPriorityTasks( void )
\r
220 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
221 xTaskCreate( vContinuousIncrementTask, "CONT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
\r
222 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
223 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
224 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_SEND", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
225 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RECV", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
226 xTaskCreate( prvChangePriorityWhenSuspendedTask, "1st_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
227 xTaskCreate( prvChangePriorityHelperTask, "2nd_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xChangePriorityWhenSuspendedHandle );
\r
229 /*-----------------------------------------------------------*/
\r
232 * Just loops around incrementing the shared variable until the limit has been
\r
233 * reached. Once the limit has been reached it suspends itself.
\r
235 static void vLimitedIncrementTask( void * pvParameters )
\r
237 unsigned long *pulCounter;
\r
239 /* Take a pointer to the shared variable from the parameters passed into
\r
241 pulCounter = ( unsigned long * ) pvParameters;
\r
243 /* This will run before the control task, so the first thing it does is
\r
244 suspend - the control task will resume it when ready. */
\r
245 vTaskSuspend( NULL );
\r
249 /* Just count up to a value then suspend. */
\r
250 ( *pulCounter )++;
\r
252 if( *pulCounter >= priMAX_COUNT )
\r
254 vTaskSuspend( NULL );
\r
258 /*-----------------------------------------------------------*/
\r
261 * Just keep counting the shared variable up. The control task will suspend
\r
262 * this task when it wants.
\r
264 static void vContinuousIncrementTask( void * pvParameters )
\r
266 unsigned long *pulCounter;
\r
267 unsigned portBASE_TYPE uxOurPriority;
\r
269 /* Take a pointer to the shared variable from the parameters passed into
\r
271 pulCounter = ( unsigned long * ) pvParameters;
\r
273 /* Query our priority so we can raise it when exclusive access to the
\r
274 shared variable is required. */
\r
275 uxOurPriority = uxTaskPriorityGet( NULL );
\r
279 /* Raise our priority above the controller task to ensure a context
\r
280 switch does not occur while we are accessing this variable. */
\r
281 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
282 ( *pulCounter )++;
\r
283 vTaskPrioritySet( NULL, uxOurPriority );
\r
285 #if configUSE_PREEMPTION == 0
\r
290 /*-----------------------------------------------------------*/
\r
293 * Controller task as described above.
\r
295 static void vCounterControlTask( void * pvParameters )
\r
297 unsigned long ulLastCounter;
\r
299 short sError = pdFALSE;
\r
300 const char * const pcTaskStartMsg = "Priority manipulation tasks started.\r\n";
\r
301 const char * const pcTaskFailMsg = "Priority manipulation Task Failed\r\n";
\r
303 /* Just to stop warning messages. */
\r
304 ( void ) pvParameters;
\r
306 /* Queue a message for printing to say the task has started. */
\r
307 vPrintDisplayMessage( &pcTaskStartMsg );
\r
311 /* Start with the counter at zero. */
\r
312 ulCounter = ( unsigned long ) 0;
\r
314 /* First section : */
\r
316 /* Check the continuous count task is running. */
\r
317 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
319 /* Suspend the continuous count task so we can take a mirror of the
\r
320 shared variable without risk of corruption. */
\r
321 vTaskSuspend( xContinuousIncrementHandle );
\r
322 ulLastCounter = ulCounter;
\r
323 vTaskResume( xContinuousIncrementHandle );
\r
325 /* Now delay to ensure the other task has processor time. */
\r
326 vTaskDelay( priSLEEP_TIME );
\r
328 /* Check the shared variable again. This time to ensure mutual
\r
329 exclusion the whole scheduler will be locked. This is just for
\r
333 if( ulLastCounter == ulCounter )
\r
335 /* The shared variable has not changed. There is a problem
\r
336 with the continuous count task so flag an error. */
\r
339 vPrintDisplayMessage( &pcTaskFailMsg );
\r
347 /* Second section: */
\r
349 /* Suspend the continuous counter task so it stops accessing the shared variable. */
\r
350 vTaskSuspend( xContinuousIncrementHandle );
\r
352 /* Reset the variable. */
\r
353 ulCounter = ( unsigned long ) 0;
\r
355 /* Resume the limited count task which has a higher priority than us.
\r
356 We should therefore not return from this call until the limited count
\r
357 task has suspended itself with a known value in the counter variable.
\r
358 The scheduler suspension is not necessary but is included for test
\r
361 vTaskResume( xLimitedIncrementHandle );
\r
364 /* Does the counter variable have the expected value? */
\r
365 if( ulCounter != priMAX_COUNT )
\r
368 vPrintDisplayMessage( &pcTaskFailMsg );
\r
371 if( sError == pdFALSE )
\r
373 /* If no errors have occurred then increment the check variable. */
\r
374 portENTER_CRITICAL();
\r
376 portEXIT_CRITICAL();
\r
379 #if configUSE_PREEMPTION == 0
\r
383 /* Resume the continuous count task and do it all again. */
\r
384 vTaskResume( xContinuousIncrementHandle );
\r
387 /*-----------------------------------------------------------*/
\r
389 static void vQueueSendWhenSuspendedTask( void *pvParameters )
\r
391 static unsigned long ulValueToSend = ( unsigned long ) 0;
\r
392 const char * const pcTaskStartMsg = "Queue send while suspended task started.\r\n";
\r
393 const char * const pcTaskFailMsg = "Queue send while suspended failed.\r\n";
\r
395 /* Just to stop warning messages. */
\r
396 ( void ) pvParameters;
\r
398 /* Queue a message for printing to say the task has started. */
\r
399 vPrintDisplayMessage( &pcTaskStartMsg );
\r
405 /* We must not block while the scheduler is suspended! */
\r
406 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
408 if( xSuspendedQueueSendError == pdFALSE )
\r
411 vPrintDisplayMessage( &pcTaskFailMsg );
\r
415 xSuspendedQueueSendError = pdTRUE;
\r
420 vTaskDelay( priSLEEP_TIME );
\r
425 /*-----------------------------------------------------------*/
\r
427 static void vQueueReceiveWhenSuspendedTask( void *pvParameters )
\r
429 static unsigned long ulExpectedValue = ( unsigned long ) 0, ulReceivedValue;
\r
430 const char * const pcTaskStartMsg = "Queue receive while suspended task started.\r\n";
\r
431 const char * const pcTaskFailMsg = "Queue receive while suspended failed.\r\n";
\r
432 portBASE_TYPE xGotValue;
\r
434 /* Just to stop warning messages. */
\r
435 ( void ) pvParameters;
\r
437 /* Queue a message for printing to say the task has started. */
\r
438 vPrintDisplayMessage( &pcTaskStartMsg );
\r
444 /* Suspending the scheduler here is fairly pointless and
\r
445 undesirable for a normal application. It is done here purely
\r
446 to test the scheduler. The inner xTaskResumeAll() should
\r
447 never return pdTRUE as the scheduler is still locked by the
\r
453 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
455 if( xTaskResumeAll() )
\r
457 xSuspendedQueueReceiveError = pdTRUE;
\r
462 #if configUSE_PREEMPTION == 0
\r
466 } while( xGotValue == pdFALSE );
\r
468 if( ulReceivedValue != ulExpectedValue )
\r
470 if( xSuspendedQueueReceiveError == pdFALSE )
\r
472 vPrintDisplayMessage( &pcTaskFailMsg );
\r
474 xSuspendedQueueReceiveError = pdTRUE;
\r
480 /*-----------------------------------------------------------*/
\r
482 static void prvChangePriorityWhenSuspendedTask( void *pvParameters )
\r
484 const char * const pcTaskStartMsg = "Priority change when suspended task started.\r\n";
\r
485 const char * const pcTaskFailMsg = "Priority change when suspended task failed.\r\n";
\r
487 /* Just to stop warning messages. */
\r
488 ( void ) pvParameters;
\r
490 /* Queue a message for printing to say the task has started. */
\r
491 vPrintDisplayMessage( &pcTaskStartMsg );
\r
495 /* Start with the counter at 0 so we know what the counter should be
\r
496 when we check it next. */
\r
497 ulPrioritySetCounter = ( unsigned long ) 0;
\r
499 /* Resume the helper task. At this time it has a priority lower than
\r
500 ours so no context switch should occur. */
\r
501 vTaskResume( xChangePriorityWhenSuspendedHandle );
\r
503 /* Check to ensure the task just resumed has not executed. */
\r
504 portENTER_CRITICAL();
\r
506 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
508 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
509 vPrintDisplayMessage( &pcTaskFailMsg );
\r
512 portEXIT_CRITICAL();
\r
514 /* Now try raising the priority while the scheduler is suspended. */
\r
517 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, ( configMAX_PRIORITIES - 1 ) );
\r
519 /* Again, even though the helper task has a priority greater than
\r
520 ours, it should not have executed yet because the scheduler is
\r
522 portENTER_CRITICAL();
\r
524 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
526 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
527 vPrintDisplayMessage( &pcTaskFailMsg );
\r
530 portEXIT_CRITICAL();
\r
534 /* Now the scheduler has been resumed the helper task should
\r
535 immediately preempt us and execute. When it executes it will increment
\r
536 the ulPrioritySetCounter exactly once before suspending itself.
\r
538 We should now always find the counter set to 1. */
\r
539 portENTER_CRITICAL();
\r
541 if( ulPrioritySetCounter != ( unsigned long ) 1 )
\r
543 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
544 vPrintDisplayMessage( &pcTaskFailMsg );
\r
547 portEXIT_CRITICAL();
\r
549 /* Delay until we try this again. */
\r
550 vTaskDelay( priSLEEP_TIME * 2 );
\r
552 /* Set the priority of the helper task back ready for the next
\r
553 execution of this task. */
\r
555 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, tskIDLE_PRIORITY );
\r
559 /*-----------------------------------------------------------*/
\r
561 static void prvChangePriorityHelperTask( void *pvParameters )
\r
563 /* Just to stop warning messages. */
\r
564 ( void ) pvParameters;
\r
568 /* This is the helper task for prvChangePriorityWhenSuspendedTask().
\r
569 It has it's priority raised and lowered. When it runs it simply
\r
570 increments the counter then suspends itself again. This allows
\r
571 prvChangePriorityWhenSuspendedTask() to know how many times it has
\r
573 ulPrioritySetCounter++;
\r
574 vTaskSuspend( NULL );
\r
577 /*-----------------------------------------------------------*/
\r
579 /* Called to check that all the created tasks are still running without error. */
\r
580 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
\r
582 /* Keep a history of the check variables so we know if it has been incremented
\r
583 since the last call. */
\r
584 static unsigned short usLastTaskCheck = ( unsigned short ) 0;
\r
585 portBASE_TYPE xReturn = pdTRUE;
\r
587 /* Check the tasks are still running by ensuring the check variable
\r
588 is still incrementing. */
\r
590 if( usCheckVariable == usLastTaskCheck )
\r
592 /* The check has not incremented so an error exists. */
\r
596 if( xSuspendedQueueSendError == pdTRUE )
\r
601 if( xSuspendedQueueReceiveError == pdTRUE )
\r
606 if( xPriorityRaiseWhenSuspendedError == pdTRUE )
\r
611 usLastTaskCheck = usCheckVariable;
\r