2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 * The first test creates three tasks - two counter tasks (one continuous count
\r
71 * and one limited count) and one controller. A "count" variable is shared
\r
72 * between all three tasks. The two counter tasks should never be in a "ready"
\r
73 * state at the same time. The controller task runs at the same priority as
\r
74 * the continuous count task, and at a lower priority than the limited count
\r
77 * One counter task loops indefinitely, incrementing the shared count variable
\r
78 * on each iteration. To ensure it has exclusive access to the variable it
\r
79 * raises it's priority above that of the controller task before each
\r
80 * increment, lowering it again to it's original priority before starting the
\r
83 * The other counter task increments the shared count variable on each
\r
84 * iteration of it's loop until the count has reached a limit of 0xff - at
\r
85 * which point it suspends itself. It will not start a new loop until the
\r
86 * controller task has made it "ready" again by calling vTaskResume ().
\r
87 * This second counter task operates at a higher priority than controller
\r
88 * task so does not need to worry about mutual exclusion of the counter
\r
91 * The controller task is in two sections. The first section controls and
\r
92 * monitors the continuous count task. When this section is operational the
\r
93 * limited count task is suspended. Likewise, the second section controls
\r
94 * and monitors the limited count task. When this section is operational the
\r
95 * continuous count task is suspended.
\r
97 * In the first section the controller task first takes a copy of the shared
\r
98 * count variable. To ensure mutual exclusion on the count variable it
\r
99 * suspends the continuous count task, resuming it again when the copy has been
\r
100 * taken. The controller task then sleeps for a fixed period - during which
\r
101 * the continuous count task will execute and increment the shared variable.
\r
102 * When the controller task wakes it checks that the continuous count task
\r
103 * has executed by comparing the copy of the shared variable with its current
\r
104 * value. This time, to ensure mutual exclusion, the scheduler itself is
\r
105 * suspended with a call to vTaskSuspendAll (). This is for demonstration
\r
106 * purposes only and is not a recommended technique due to its inefficiency.
\r
108 * After a fixed number of iterations the controller task suspends the
\r
109 * continuous count task, and moves on to its second section.
\r
111 * At the start of the second section the shared variable is cleared to zero.
\r
112 * The limited count task is then woken from it's suspension by a call to
\r
113 * vTaskResume (). As this counter task operates at a higher priority than
\r
114 * the controller task the controller task should not run again until the
\r
115 * shared variable has been counted up to the limited value causing the counter
\r
116 * task to suspend itself. The next line after vTaskResume () is therefore
\r
117 * a check on the shared variable to ensure everything is as expected.
\r
120 * The second test consists of a couple of very simple tasks that post onto a
\r
121 * queue while the scheduler is suspended. This test was added to test parts
\r
122 * of the scheduler not exercised by the first test.
\r
125 * The final set of two tasks implements a third test. This simply raises the
\r
126 * priority of a task while the scheduler is suspended. Again this test was
\r
127 * added to exercise parts of the code not covered by the first test.
\r
129 * \page Priorities dynamic.c
\r
130 * \ingroup DemoFiles
\r
135 Changes from V2.0.0
\r
137 + Delay periods are now specified using variables and constants of
\r
138 portTickType rather than unsigned long.
\r
139 + Added a second, simple test that uses the functions
\r
140 vQueueReceiveWhenSuspendedTask() and vQueueSendWhenSuspendedTask().
\r
142 Changes from V3.1.1
\r
144 + Added a third simple test that uses the vTaskPrioritySet() function
\r
145 while the scheduler is suspended.
\r
146 + Modified the controller task slightly to test the calling of
\r
147 vTaskResumeAll() while the scheduler is suspended.
\r
150 #include <stdlib.h>
\r
152 /* Scheduler include files. */
\r
153 #include "FreeRTOS.h"
\r
155 #include "semphr.h"
\r
157 /* Demo app include files. */
\r
158 #include "dynamic.h"
\r
161 /* Function that implements the "limited count" task as described above. */
\r
162 static void vLimitedIncrementTask( void * pvParameters );
\r
164 /* Function that implements the "continuous count" task as described above. */
\r
165 static void vContinuousIncrementTask( void * pvParameters );
\r
167 /* Function that implements the controller task as described above. */
\r
168 static void vCounterControlTask( void * pvParameters );
\r
170 /* The simple test functions that check sending and receiving while the
\r
171 scheduler is suspended. */
\r
172 static void vQueueReceiveWhenSuspendedTask( void *pvParameters );
\r
173 static void vQueueSendWhenSuspendedTask( void *pvParameters );
\r
175 /* The simple test functions that check raising and lowering of task priorities
\r
176 while the scheduler is suspended. */
\r
177 static void prvChangePriorityWhenSuspendedTask( void *pvParameters );
\r
178 static void prvChangePriorityHelperTask( void *pvParameters );
\r
181 /* Demo task specific constants. */
\r
182 #define priSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
\r
183 #define priSLEEP_TIME ( ( portTickType ) 50 )
\r
184 #define priLOOPS ( 5 )
\r
185 #define priMAX_COUNT ( ( unsigned long ) 0xff )
\r
186 #define priNO_BLOCK ( ( portTickType ) 0 )
\r
187 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
189 /*-----------------------------------------------------------*/
\r
191 /* Handles to the two counter tasks. These could be passed in as parameters
\r
192 to the controller task to prevent them having to be file scope. */
\r
193 static xTaskHandle xContinuousIncrementHandle, xLimitedIncrementHandle, xChangePriorityWhenSuspendedHandle;
\r
195 /* The shared counter variable. This is passed in as a parameter to the two
\r
196 counter variables for demonstration purposes. */
\r
197 static unsigned long ulCounter;
\r
199 /* Variable used in a similar way by the test that checks the raising and
\r
200 lowering of task priorities while the scheduler is suspended. */
\r
201 static unsigned long ulPrioritySetCounter;
\r
203 /* Variables used to check that the tasks are still operating without error.
\r
204 Each complete iteration of the controller task increments this variable
\r
205 provided no errors have been found. The variable maintaining the same value
\r
206 is therefore indication of an error. */
\r
207 static unsigned short usCheckVariable = ( unsigned short ) 0;
\r
208 static portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
\r
209 static portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
\r
210 static portBASE_TYPE xPriorityRaiseWhenSuspendedError = pdFALSE;
\r
212 /* Queue used by the second test. */
\r
213 xQueueHandle xSuspendedTestQueue;
\r
215 /*-----------------------------------------------------------*/
\r
217 * Start the seven tasks as described at the top of the file.
\r
218 * Note that the limited count task is given a higher priority.
\r
220 void vStartDynamicPriorityTasks( void )
\r
222 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
223 xTaskCreate( vContinuousIncrementTask, "CONT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
\r
224 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
225 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
226 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_SEND", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
227 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RECV", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
228 xTaskCreate( prvChangePriorityWhenSuspendedTask, "1st_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
229 xTaskCreate( prvChangePriorityHelperTask, "2nd_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xChangePriorityWhenSuspendedHandle );
\r
231 /*-----------------------------------------------------------*/
\r
234 * Just loops around incrementing the shared variable until the limit has been
\r
235 * reached. Once the limit has been reached it suspends itself.
\r
237 static void vLimitedIncrementTask( void * pvParameters )
\r
239 unsigned long *pulCounter;
\r
241 /* Take a pointer to the shared variable from the parameters passed into
\r
243 pulCounter = ( unsigned long * ) pvParameters;
\r
245 /* This will run before the control task, so the first thing it does is
\r
246 suspend - the control task will resume it when ready. */
\r
247 vTaskSuspend( NULL );
\r
251 /* Just count up to a value then suspend. */
\r
252 ( *pulCounter )++;
\r
254 if( *pulCounter >= priMAX_COUNT )
\r
256 vTaskSuspend( NULL );
\r
260 /*-----------------------------------------------------------*/
\r
263 * Just keep counting the shared variable up. The control task will suspend
\r
264 * this task when it wants.
\r
266 static void vContinuousIncrementTask( void * pvParameters )
\r
268 unsigned long *pulCounter;
\r
269 unsigned portBASE_TYPE uxOurPriority;
\r
271 /* Take a pointer to the shared variable from the parameters passed into
\r
273 pulCounter = ( unsigned long * ) pvParameters;
\r
275 /* Query our priority so we can raise it when exclusive access to the
\r
276 shared variable is required. */
\r
277 uxOurPriority = uxTaskPriorityGet( NULL );
\r
281 /* Raise our priority above the controller task to ensure a context
\r
282 switch does not occur while we are accessing this variable. */
\r
283 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
284 ( *pulCounter )++;
\r
285 vTaskPrioritySet( NULL, uxOurPriority );
\r
287 #if configUSE_PREEMPTION == 0
\r
292 /*-----------------------------------------------------------*/
\r
295 * Controller task as described above.
\r
297 static void vCounterControlTask( void * pvParameters )
\r
299 unsigned long ulLastCounter;
\r
301 short sError = pdFALSE;
\r
302 const char * const pcTaskStartMsg = "Priority manipulation tasks started.\r\n";
\r
303 const char * const pcTaskFailMsg = "Priority manipulation Task Failed\r\n";
\r
305 /* Just to stop warning messages. */
\r
306 ( void ) pvParameters;
\r
308 /* Queue a message for printing to say the task has started. */
\r
309 vPrintDisplayMessage( &pcTaskStartMsg );
\r
313 /* Start with the counter at zero. */
\r
314 ulCounter = ( unsigned long ) 0;
\r
316 /* First section : */
\r
318 /* Check the continuous count task is running. */
\r
319 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
321 /* Suspend the continuous count task so we can take a mirror of the
\r
322 shared variable without risk of corruption. */
\r
323 vTaskSuspend( xContinuousIncrementHandle );
\r
324 ulLastCounter = ulCounter;
\r
325 vTaskResume( xContinuousIncrementHandle );
\r
327 /* Now delay to ensure the other task has processor time. */
\r
328 vTaskDelay( priSLEEP_TIME );
\r
330 /* Check the shared variable again. This time to ensure mutual
\r
331 exclusion the whole scheduler will be locked. This is just for
\r
335 if( ulLastCounter == ulCounter )
\r
337 /* The shared variable has not changed. There is a problem
\r
338 with the continuous count task so flag an error. */
\r
341 vPrintDisplayMessage( &pcTaskFailMsg );
\r
349 /* Second section: */
\r
351 /* Suspend the continuous counter task so it stops accessing the shared variable. */
\r
352 vTaskSuspend( xContinuousIncrementHandle );
\r
354 /* Reset the variable. */
\r
355 ulCounter = ( unsigned long ) 0;
\r
357 /* Resume the limited count task which has a higher priority than us.
\r
358 We should therefore not return from this call until the limited count
\r
359 task has suspended itself with a known value in the counter variable.
\r
360 The scheduler suspension is not necessary but is included for test
\r
363 vTaskResume( xLimitedIncrementHandle );
\r
366 /* Does the counter variable have the expected value? */
\r
367 if( ulCounter != priMAX_COUNT )
\r
370 vPrintDisplayMessage( &pcTaskFailMsg );
\r
373 if( sError == pdFALSE )
\r
375 /* If no errors have occurred then increment the check variable. */
\r
376 portENTER_CRITICAL();
\r
378 portEXIT_CRITICAL();
\r
381 #if configUSE_PREEMPTION == 0
\r
385 /* Resume the continuous count task and do it all again. */
\r
386 vTaskResume( xContinuousIncrementHandle );
\r
389 /*-----------------------------------------------------------*/
\r
391 static void vQueueSendWhenSuspendedTask( void *pvParameters )
\r
393 static unsigned long ulValueToSend = ( unsigned long ) 0;
\r
394 const char * const pcTaskStartMsg = "Queue send while suspended task started.\r\n";
\r
395 const char * const pcTaskFailMsg = "Queue send while suspended failed.\r\n";
\r
397 /* Just to stop warning messages. */
\r
398 ( void ) pvParameters;
\r
400 /* Queue a message for printing to say the task has started. */
\r
401 vPrintDisplayMessage( &pcTaskStartMsg );
\r
407 /* We must not block while the scheduler is suspended! */
\r
408 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
410 if( xSuspendedQueueSendError == pdFALSE )
\r
413 vPrintDisplayMessage( &pcTaskFailMsg );
\r
417 xSuspendedQueueSendError = pdTRUE;
\r
422 vTaskDelay( priSLEEP_TIME );
\r
427 /*-----------------------------------------------------------*/
\r
429 static void vQueueReceiveWhenSuspendedTask( void *pvParameters )
\r
431 static unsigned long ulExpectedValue = ( unsigned long ) 0, ulReceivedValue;
\r
432 const char * const pcTaskStartMsg = "Queue receive while suspended task started.\r\n";
\r
433 const char * const pcTaskFailMsg = "Queue receive while suspended failed.\r\n";
\r
434 portBASE_TYPE xGotValue;
\r
436 /* Just to stop warning messages. */
\r
437 ( void ) pvParameters;
\r
439 /* Queue a message for printing to say the task has started. */
\r
440 vPrintDisplayMessage( &pcTaskStartMsg );
\r
446 /* Suspending the scheduler here is fairly pointless and
\r
447 undesirable for a normal application. It is done here purely
\r
448 to test the scheduler. The inner xTaskResumeAll() should
\r
449 never return pdTRUE as the scheduler is still locked by the
\r
455 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
457 if( xTaskResumeAll() )
\r
459 xSuspendedQueueReceiveError = pdTRUE;
\r
464 #if configUSE_PREEMPTION == 0
\r
468 } while( xGotValue == pdFALSE );
\r
470 if( ulReceivedValue != ulExpectedValue )
\r
472 if( xSuspendedQueueReceiveError == pdFALSE )
\r
474 vPrintDisplayMessage( &pcTaskFailMsg );
\r
476 xSuspendedQueueReceiveError = pdTRUE;
\r
482 /*-----------------------------------------------------------*/
\r
484 static void prvChangePriorityWhenSuspendedTask( void *pvParameters )
\r
486 const char * const pcTaskStartMsg = "Priority change when suspended task started.\r\n";
\r
487 const char * const pcTaskFailMsg = "Priority change when suspended task failed.\r\n";
\r
489 /* Just to stop warning messages. */
\r
490 ( void ) pvParameters;
\r
492 /* Queue a message for printing to say the task has started. */
\r
493 vPrintDisplayMessage( &pcTaskStartMsg );
\r
497 /* Start with the counter at 0 so we know what the counter should be
\r
498 when we check it next. */
\r
499 ulPrioritySetCounter = ( unsigned long ) 0;
\r
501 /* Resume the helper task. At this time it has a priority lower than
\r
502 ours so no context switch should occur. */
\r
503 vTaskResume( xChangePriorityWhenSuspendedHandle );
\r
505 /* Check to ensure the task just resumed has not executed. */
\r
506 portENTER_CRITICAL();
\r
508 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
510 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
511 vPrintDisplayMessage( &pcTaskFailMsg );
\r
514 portEXIT_CRITICAL();
\r
516 /* Now try raising the priority while the scheduler is suspended. */
\r
519 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, ( configMAX_PRIORITIES - 1 ) );
\r
521 /* Again, even though the helper task has a priority greater than
\r
522 ours, it should not have executed yet because the scheduler is
\r
524 portENTER_CRITICAL();
\r
526 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
528 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
529 vPrintDisplayMessage( &pcTaskFailMsg );
\r
532 portEXIT_CRITICAL();
\r
536 /* Now the scheduler has been resumed the helper task should
\r
537 immediately preempt us and execute. When it executes it will increment
\r
538 the ulPrioritySetCounter exactly once before suspending itself.
\r
540 We should now always find the counter set to 1. */
\r
541 portENTER_CRITICAL();
\r
543 if( ulPrioritySetCounter != ( unsigned long ) 1 )
\r
545 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
546 vPrintDisplayMessage( &pcTaskFailMsg );
\r
549 portEXIT_CRITICAL();
\r
551 /* Delay until we try this again. */
\r
552 vTaskDelay( priSLEEP_TIME * 2 );
\r
554 /* Set the priority of the helper task back ready for the next
\r
555 execution of this task. */
\r
557 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, tskIDLE_PRIORITY );
\r
561 /*-----------------------------------------------------------*/
\r
563 static void prvChangePriorityHelperTask( void *pvParameters )
\r
565 /* Just to stop warning messages. */
\r
566 ( void ) pvParameters;
\r
570 /* This is the helper task for prvChangePriorityWhenSuspendedTask().
\r
571 It has it's priority raised and lowered. When it runs it simply
\r
572 increments the counter then suspends itself again. This allows
\r
573 prvChangePriorityWhenSuspendedTask() to know how many times it has
\r
575 ulPrioritySetCounter++;
\r
576 vTaskSuspend( NULL );
\r
579 /*-----------------------------------------------------------*/
\r
581 /* Called to check that all the created tasks are still running without error. */
\r
582 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
\r
584 /* Keep a history of the check variables so we know if it has been incremented
\r
585 since the last call. */
\r
586 static unsigned short usLastTaskCheck = ( unsigned short ) 0;
\r
587 portBASE_TYPE xReturn = pdTRUE;
\r
589 /* Check the tasks are still running by ensuring the check variable
\r
590 is still incrementing. */
\r
592 if( usCheckVariable == usLastTaskCheck )
\r
594 /* The check has not incremented so an error exists. */
\r
598 if( xSuspendedQueueSendError == pdTRUE )
\r
603 if( xSuspendedQueueReceiveError == pdTRUE )
\r
608 if( xPriorityRaiseWhenSuspendedError == pdTRUE )
\r
613 usLastTaskCheck = usCheckVariable;
\r