2 FreeRTOS V7.4.2 - Copyright (C) 2013 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
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 * The first test creates three tasks - two counter tasks (one continuous count
\r
77 * and one limited count) and one controller. A "count" variable is shared
\r
78 * between all three tasks. The two counter tasks should never be in a "ready"
\r
79 * state at the same time. The controller task runs at the same priority as
\r
80 * the continuous count task, and at a lower priority than the limited count
\r
83 * One counter task loops indefinitely, incrementing the shared count variable
\r
84 * on each iteration. To ensure it has exclusive access to the variable it
\r
85 * raises it's priority above that of the controller task before each
\r
86 * increment, lowering it again to it's original priority before starting the
\r
89 * The other counter task increments the shared count variable on each
\r
90 * iteration of it's loop until the count has reached a limit of 0xff - at
\r
91 * which point it suspends itself. It will not start a new loop until the
\r
92 * controller task has made it "ready" again by calling vTaskResume ().
\r
93 * This second counter task operates at a higher priority than controller
\r
94 * task so does not need to worry about mutual exclusion of the counter
\r
97 * The controller task is in two sections. The first section controls and
\r
98 * monitors the continuous count task. When this section is operational the
\r
99 * limited count task is suspended. Likewise, the second section controls
\r
100 * and monitors the limited count task. When this section is operational the
\r
101 * continuous count task is suspended.
\r
103 * In the first section the controller task first takes a copy of the shared
\r
104 * count variable. To ensure mutual exclusion on the count variable it
\r
105 * suspends the continuous count task, resuming it again when the copy has been
\r
106 * taken. The controller task then sleeps for a fixed period - during which
\r
107 * the continuous count task will execute and increment the shared variable.
\r
108 * When the controller task wakes it checks that the continuous count task
\r
109 * has executed by comparing the copy of the shared variable with its current
\r
110 * value. This time, to ensure mutual exclusion, the scheduler itself is
\r
111 * suspended with a call to vTaskSuspendAll (). This is for demonstration
\r
112 * purposes only and is not a recommended technique due to its inefficiency.
\r
114 * After a fixed number of iterations the controller task suspends the
\r
115 * continuous count task, and moves on to its second section.
\r
117 * At the start of the second section the shared variable is cleared to zero.
\r
118 * The limited count task is then woken from it's suspension by a call to
\r
119 * vTaskResume (). As this counter task operates at a higher priority than
\r
120 * the controller task the controller task should not run again until the
\r
121 * shared variable has been counted up to the limited value causing the counter
\r
122 * task to suspend itself. The next line after vTaskResume () is therefore
\r
123 * a check on the shared variable to ensure everything is as expected.
\r
126 * The second test consists of a couple of very simple tasks that post onto a
\r
127 * queue while the scheduler is suspended. This test was added to test parts
\r
128 * of the scheduler not exercised by the first test.
\r
131 * The final set of two tasks implements a third test. This simply raises the
\r
132 * priority of a task while the scheduler is suspended. Again this test was
\r
133 * added to exercise parts of the code not covered by the first test.
\r
135 * \page Priorities dynamic.c
\r
136 * \ingroup DemoFiles
\r
141 Changes from V2.0.0
\r
143 + Delay periods are now specified using variables and constants of
\r
144 portTickType rather than unsigned long.
\r
145 + Added a second, simple test that uses the functions
\r
146 vQueueReceiveWhenSuspendedTask() and vQueueSendWhenSuspendedTask().
\r
148 Changes from V3.1.1
\r
150 + Added a third simple test that uses the vTaskPrioritySet() function
\r
151 while the scheduler is suspended.
\r
152 + Modified the controller task slightly to test the calling of
\r
153 vTaskResumeAll() while the scheduler is suspended.
\r
156 #include <stdlib.h>
\r
158 /* Scheduler include files. */
\r
159 #include "FreeRTOS.h"
\r
161 #include "semphr.h"
\r
163 /* Demo app include files. */
\r
164 #include "dynamic.h"
\r
167 /* Function that implements the "limited count" task as described above. */
\r
168 static void vLimitedIncrementTask( void * pvParameters );
\r
170 /* Function that implements the "continuous count" task as described above. */
\r
171 static void vContinuousIncrementTask( void * pvParameters );
\r
173 /* Function that implements the controller task as described above. */
\r
174 static void vCounterControlTask( void * pvParameters );
\r
176 /* The simple test functions that check sending and receiving while the
\r
177 scheduler is suspended. */
\r
178 static void vQueueReceiveWhenSuspendedTask( void *pvParameters );
\r
179 static void vQueueSendWhenSuspendedTask( void *pvParameters );
\r
181 /* The simple test functions that check raising and lowering of task priorities
\r
182 while the scheduler is suspended. */
\r
183 static void prvChangePriorityWhenSuspendedTask( void *pvParameters );
\r
184 static void prvChangePriorityHelperTask( void *pvParameters );
\r
187 /* Demo task specific constants. */
\r
188 #define priSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
\r
189 #define priSLEEP_TIME ( ( portTickType ) 50 )
\r
190 #define priLOOPS ( 5 )
\r
191 #define priMAX_COUNT ( ( unsigned long ) 0xff )
\r
192 #define priNO_BLOCK ( ( portTickType ) 0 )
\r
193 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
195 /*-----------------------------------------------------------*/
\r
197 /* Handles to the two counter tasks. These could be passed in as parameters
\r
198 to the controller task to prevent them having to be file scope. */
\r
199 static xTaskHandle xContinuousIncrementHandle, xLimitedIncrementHandle, xChangePriorityWhenSuspendedHandle;
\r
201 /* The shared counter variable. This is passed in as a parameter to the two
\r
202 counter variables for demonstration purposes. */
\r
203 static unsigned long ulCounter;
\r
205 /* Variable used in a similar way by the test that checks the raising and
\r
206 lowering of task priorities while the scheduler is suspended. */
\r
207 static unsigned long ulPrioritySetCounter;
\r
209 /* Variables used to check that the tasks are still operating without error.
\r
210 Each complete iteration of the controller task increments this variable
\r
211 provided no errors have been found. The variable maintaining the same value
\r
212 is therefore indication of an error. */
\r
213 static unsigned short usCheckVariable = ( unsigned short ) 0;
\r
214 static portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
\r
215 static portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
\r
216 static portBASE_TYPE xPriorityRaiseWhenSuspendedError = pdFALSE;
\r
218 /* Queue used by the second test. */
\r
219 xQueueHandle xSuspendedTestQueue;
\r
221 /*-----------------------------------------------------------*/
\r
223 * Start the seven tasks as described at the top of the file.
\r
224 * Note that the limited count task is given a higher priority.
\r
226 void vStartDynamicPriorityTasks( void )
\r
228 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
229 xTaskCreate( vContinuousIncrementTask, "CONT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
\r
230 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
231 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
232 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_SEND", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
233 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RECV", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
234 xTaskCreate( prvChangePriorityWhenSuspendedTask, "1st_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
235 xTaskCreate( prvChangePriorityHelperTask, "2nd_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xChangePriorityWhenSuspendedHandle );
\r
237 /*-----------------------------------------------------------*/
\r
240 * Just loops around incrementing the shared variable until the limit has been
\r
241 * reached. Once the limit has been reached it suspends itself.
\r
243 static void vLimitedIncrementTask( void * pvParameters )
\r
245 unsigned long *pulCounter;
\r
247 /* Take a pointer to the shared variable from the parameters passed into
\r
249 pulCounter = ( unsigned long * ) pvParameters;
\r
251 /* This will run before the control task, so the first thing it does is
\r
252 suspend - the control task will resume it when ready. */
\r
253 vTaskSuspend( NULL );
\r
257 /* Just count up to a value then suspend. */
\r
258 ( *pulCounter )++;
\r
260 if( *pulCounter >= priMAX_COUNT )
\r
262 vTaskSuspend( NULL );
\r
266 /*-----------------------------------------------------------*/
\r
269 * Just keep counting the shared variable up. The control task will suspend
\r
270 * this task when it wants.
\r
272 static void vContinuousIncrementTask( void * pvParameters )
\r
274 unsigned long *pulCounter;
\r
275 unsigned portBASE_TYPE uxOurPriority;
\r
277 /* Take a pointer to the shared variable from the parameters passed into
\r
279 pulCounter = ( unsigned long * ) pvParameters;
\r
281 /* Query our priority so we can raise it when exclusive access to the
\r
282 shared variable is required. */
\r
283 uxOurPriority = uxTaskPriorityGet( NULL );
\r
287 /* Raise our priority above the controller task to ensure a context
\r
288 switch does not occur while we are accessing this variable. */
\r
289 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
290 ( *pulCounter )++;
\r
291 vTaskPrioritySet( NULL, uxOurPriority );
\r
293 #if configUSE_PREEMPTION == 0
\r
298 /*-----------------------------------------------------------*/
\r
301 * Controller task as described above.
\r
303 static void vCounterControlTask( void * pvParameters )
\r
305 unsigned long ulLastCounter;
\r
307 short sError = pdFALSE;
\r
308 const char * const pcTaskStartMsg = "Priority manipulation tasks started.\r\n";
\r
309 const char * const pcTaskFailMsg = "Priority manipulation Task Failed\r\n";
\r
311 /* Just to stop warning messages. */
\r
312 ( void ) pvParameters;
\r
314 /* Queue a message for printing to say the task has started. */
\r
315 vPrintDisplayMessage( &pcTaskStartMsg );
\r
319 /* Start with the counter at zero. */
\r
320 ulCounter = ( unsigned long ) 0;
\r
322 /* First section : */
\r
324 /* Check the continuous count task is running. */
\r
325 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
327 /* Suspend the continuous count task so we can take a mirror of the
\r
328 shared variable without risk of corruption. */
\r
329 vTaskSuspend( xContinuousIncrementHandle );
\r
330 ulLastCounter = ulCounter;
\r
331 vTaskResume( xContinuousIncrementHandle );
\r
333 /* Now delay to ensure the other task has processor time. */
\r
334 vTaskDelay( priSLEEP_TIME );
\r
336 /* Check the shared variable again. This time to ensure mutual
\r
337 exclusion the whole scheduler will be locked. This is just for
\r
341 if( ulLastCounter == ulCounter )
\r
343 /* The shared variable has not changed. There is a problem
\r
344 with the continuous count task so flag an error. */
\r
347 vPrintDisplayMessage( &pcTaskFailMsg );
\r
355 /* Second section: */
\r
357 /* Suspend the continuous counter task so it stops accessing the shared variable. */
\r
358 vTaskSuspend( xContinuousIncrementHandle );
\r
360 /* Reset the variable. */
\r
361 ulCounter = ( unsigned long ) 0;
\r
363 /* Resume the limited count task which has a higher priority than us.
\r
364 We should therefore not return from this call until the limited count
\r
365 task has suspended itself with a known value in the counter variable.
\r
366 The scheduler suspension is not necessary but is included for test
\r
369 vTaskResume( xLimitedIncrementHandle );
\r
372 /* Does the counter variable have the expected value? */
\r
373 if( ulCounter != priMAX_COUNT )
\r
376 vPrintDisplayMessage( &pcTaskFailMsg );
\r
379 if( sError == pdFALSE )
\r
381 /* If no errors have occurred then increment the check variable. */
\r
382 portENTER_CRITICAL();
\r
384 portEXIT_CRITICAL();
\r
387 #if configUSE_PREEMPTION == 0
\r
391 /* Resume the continuous count task and do it all again. */
\r
392 vTaskResume( xContinuousIncrementHandle );
\r
395 /*-----------------------------------------------------------*/
\r
397 static void vQueueSendWhenSuspendedTask( void *pvParameters )
\r
399 static unsigned long ulValueToSend = ( unsigned long ) 0;
\r
400 const char * const pcTaskStartMsg = "Queue send while suspended task started.\r\n";
\r
401 const char * const pcTaskFailMsg = "Queue send while suspended failed.\r\n";
\r
403 /* Just to stop warning messages. */
\r
404 ( void ) pvParameters;
\r
406 /* Queue a message for printing to say the task has started. */
\r
407 vPrintDisplayMessage( &pcTaskStartMsg );
\r
413 /* We must not block while the scheduler is suspended! */
\r
414 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
416 if( xSuspendedQueueSendError == pdFALSE )
\r
419 vPrintDisplayMessage( &pcTaskFailMsg );
\r
423 xSuspendedQueueSendError = pdTRUE;
\r
428 vTaskDelay( priSLEEP_TIME );
\r
433 /*-----------------------------------------------------------*/
\r
435 static void vQueueReceiveWhenSuspendedTask( void *pvParameters )
\r
437 static unsigned long ulExpectedValue = ( unsigned long ) 0, ulReceivedValue;
\r
438 const char * const pcTaskStartMsg = "Queue receive while suspended task started.\r\n";
\r
439 const char * const pcTaskFailMsg = "Queue receive while suspended failed.\r\n";
\r
440 portBASE_TYPE xGotValue;
\r
442 /* Just to stop warning messages. */
\r
443 ( void ) pvParameters;
\r
445 /* Queue a message for printing to say the task has started. */
\r
446 vPrintDisplayMessage( &pcTaskStartMsg );
\r
452 /* Suspending the scheduler here is fairly pointless and
\r
453 undesirable for a normal application. It is done here purely
\r
454 to test the scheduler. The inner xTaskResumeAll() should
\r
455 never return pdTRUE as the scheduler is still locked by the
\r
461 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
463 if( xTaskResumeAll() )
\r
465 xSuspendedQueueReceiveError = pdTRUE;
\r
470 #if configUSE_PREEMPTION == 0
\r
474 } while( xGotValue == pdFALSE );
\r
476 if( ulReceivedValue != ulExpectedValue )
\r
478 if( xSuspendedQueueReceiveError == pdFALSE )
\r
480 vPrintDisplayMessage( &pcTaskFailMsg );
\r
482 xSuspendedQueueReceiveError = pdTRUE;
\r
488 /*-----------------------------------------------------------*/
\r
490 static void prvChangePriorityWhenSuspendedTask( void *pvParameters )
\r
492 const char * const pcTaskStartMsg = "Priority change when suspended task started.\r\n";
\r
493 const char * const pcTaskFailMsg = "Priority change when suspended task failed.\r\n";
\r
495 /* Just to stop warning messages. */
\r
496 ( void ) pvParameters;
\r
498 /* Queue a message for printing to say the task has started. */
\r
499 vPrintDisplayMessage( &pcTaskStartMsg );
\r
503 /* Start with the counter at 0 so we know what the counter should be
\r
504 when we check it next. */
\r
505 ulPrioritySetCounter = ( unsigned long ) 0;
\r
507 /* Resume the helper task. At this time it has a priority lower than
\r
508 ours so no context switch should occur. */
\r
509 vTaskResume( xChangePriorityWhenSuspendedHandle );
\r
511 /* Check to ensure the task just resumed has not executed. */
\r
512 portENTER_CRITICAL();
\r
514 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
516 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
517 vPrintDisplayMessage( &pcTaskFailMsg );
\r
520 portEXIT_CRITICAL();
\r
522 /* Now try raising the priority while the scheduler is suspended. */
\r
525 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, ( configMAX_PRIORITIES - 1 ) );
\r
527 /* Again, even though the helper task has a priority greater than
\r
528 ours, it should not have executed yet because the scheduler is
\r
530 portENTER_CRITICAL();
\r
532 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
534 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
535 vPrintDisplayMessage( &pcTaskFailMsg );
\r
538 portEXIT_CRITICAL();
\r
542 /* Now the scheduler has been resumed the helper task should
\r
543 immediately preempt us and execute. When it executes it will increment
\r
544 the ulPrioritySetCounter exactly once before suspending itself.
\r
546 We should now always find the counter set to 1. */
\r
547 portENTER_CRITICAL();
\r
549 if( ulPrioritySetCounter != ( unsigned long ) 1 )
\r
551 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
552 vPrintDisplayMessage( &pcTaskFailMsg );
\r
555 portEXIT_CRITICAL();
\r
557 /* Delay until we try this again. */
\r
558 vTaskDelay( priSLEEP_TIME * 2 );
\r
560 /* Set the priority of the helper task back ready for the next
\r
561 execution of this task. */
\r
563 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, tskIDLE_PRIORITY );
\r
567 /*-----------------------------------------------------------*/
\r
569 static void prvChangePriorityHelperTask( void *pvParameters )
\r
571 /* Just to stop warning messages. */
\r
572 ( void ) pvParameters;
\r
576 /* This is the helper task for prvChangePriorityWhenSuspendedTask().
\r
577 It has it's priority raised and lowered. When it runs it simply
\r
578 increments the counter then suspends itself again. This allows
\r
579 prvChangePriorityWhenSuspendedTask() to know how many times it has
\r
581 ulPrioritySetCounter++;
\r
582 vTaskSuspend( NULL );
\r
585 /*-----------------------------------------------------------*/
\r
587 /* Called to check that all the created tasks are still running without error. */
\r
588 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
\r
590 /* Keep a history of the check variables so we know if it has been incremented
\r
591 since the last call. */
\r
592 static unsigned short usLastTaskCheck = ( unsigned short ) 0;
\r
593 portBASE_TYPE xReturn = pdTRUE;
\r
595 /* Check the tasks are still running by ensuring the check variable
\r
596 is still incrementing. */
\r
598 if( usCheckVariable == usLastTaskCheck )
\r
600 /* The check has not incremented so an error exists. */
\r
604 if( xSuspendedQueueSendError == pdTRUE )
\r
609 if( xSuspendedQueueReceiveError == pdTRUE )
\r
614 if( xPriorityRaiseWhenSuspendedError == pdTRUE )
\r
619 usLastTaskCheck = usCheckVariable;
\r