2 FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 * The first test creates three tasks - two counter tasks (one continuous count
\r
68 * and one limited count) and one controller. A "count" variable is shared
\r
69 * between all three tasks. The two counter tasks should never be in a "ready"
\r
70 * state at the same time. The controller task runs at the same priority as
\r
71 * the continuous count task, and at a lower priority than the limited count
\r
74 * One counter task loops indefinitely, incrementing the shared count variable
\r
75 * on each iteration. To ensure it has exclusive access to the variable it
\r
76 * raises it's priority above that of the controller task before each
\r
77 * increment, lowering it again to it's original priority before starting the
\r
80 * The other counter task increments the shared count variable on each
\r
81 * iteration of it's loop until the count has reached a limit of 0xff - at
\r
82 * which point it suspends itself. It will not start a new loop until the
\r
83 * controller task has made it "ready" again by calling vTaskResume ().
\r
84 * This second counter task operates at a higher priority than controller
\r
85 * task so does not need to worry about mutual exclusion of the counter
\r
88 * The controller task is in two sections. The first section controls and
\r
89 * monitors the continuous count task. When this section is operational the
\r
90 * limited count task is suspended. Likewise, the second section controls
\r
91 * and monitors the limited count task. When this section is operational the
\r
92 * continuous count task is suspended.
\r
94 * In the first section the controller task first takes a copy of the shared
\r
95 * count variable. To ensure mutual exclusion on the count variable it
\r
96 * suspends the continuous count task, resuming it again when the copy has been
\r
97 * taken. The controller task then sleeps for a fixed period - during which
\r
98 * the continuous count task will execute and increment the shared variable.
\r
99 * When the controller task wakes it checks that the continuous count task
\r
100 * has executed by comparing the copy of the shared variable with its current
\r
101 * value. This time, to ensure mutual exclusion, the scheduler itself is
\r
102 * suspended with a call to vTaskSuspendAll (). This is for demonstration
\r
103 * purposes only and is not a recommended technique due to its inefficiency.
\r
105 * After a fixed number of iterations the controller task suspends the
\r
106 * continuous count task, and moves on to its second section.
\r
108 * At the start of the second section the shared variable is cleared to zero.
\r
109 * The limited count task is then woken from it's suspension by a call to
\r
110 * vTaskResume (). As this counter task operates at a higher priority than
\r
111 * the controller task the controller task should not run again until the
\r
112 * shared variable has been counted up to the limited value causing the counter
\r
113 * task to suspend itself. The next line after vTaskResume () is therefore
\r
114 * a check on the shared variable to ensure everything is as expected.
\r
117 * The second test consists of a couple of very simple tasks that post onto a
\r
118 * queue while the scheduler is suspended. This test was added to test parts
\r
119 * of the scheduler not exercised by the first test.
\r
122 * The final set of two tasks implements a third test. This simply raises the
\r
123 * priority of a task while the scheduler is suspended. Again this test was
\r
124 * added to exercise parts of the code not covered by the first test.
\r
126 * \page Priorities dynamic.c
\r
127 * \ingroup DemoFiles
\r
132 Changes from V2.0.0
\r
134 + Delay periods are now specified using variables and constants of
\r
135 TickType_t rather than unsigned long.
\r
136 + Added a second, simple test that uses the functions
\r
137 vQueueReceiveWhenSuspendedTask() and vQueueSendWhenSuspendedTask().
\r
139 Changes from V3.1.1
\r
141 + Added a third simple test that uses the vTaskPrioritySet() function
\r
142 while the scheduler is suspended.
\r
143 + Modified the controller task slightly to test the calling of
\r
144 vTaskResumeAll() while the scheduler is suspended.
\r
147 #include <stdlib.h>
\r
149 /* Scheduler include files. */
\r
150 #include "FreeRTOS.h"
\r
152 #include "semphr.h"
\r
154 /* Demo app include files. */
\r
155 #include "dynamic.h"
\r
158 /* Function that implements the "limited count" task as described above. */
\r
159 static void vLimitedIncrementTask( void * pvParameters );
\r
161 /* Function that implements the "continuous count" task as described above. */
\r
162 static void vContinuousIncrementTask( void * pvParameters );
\r
164 /* Function that implements the controller task as described above. */
\r
165 static void vCounterControlTask( void * pvParameters );
\r
167 /* The simple test functions that check sending and receiving while the
\r
168 scheduler is suspended. */
\r
169 static void vQueueReceiveWhenSuspendedTask( void *pvParameters );
\r
170 static void vQueueSendWhenSuspendedTask( void *pvParameters );
\r
172 /* The simple test functions that check raising and lowering of task priorities
\r
173 while the scheduler is suspended. */
\r
174 static void prvChangePriorityWhenSuspendedTask( void *pvParameters );
\r
175 static void prvChangePriorityHelperTask( void *pvParameters );
\r
178 /* Demo task specific constants. */
\r
179 #define priSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
\r
180 #define priSLEEP_TIME ( ( TickType_t ) 50 )
\r
181 #define priLOOPS ( 5 )
\r
182 #define priMAX_COUNT ( ( unsigned long ) 0xff )
\r
183 #define priNO_BLOCK ( ( TickType_t ) 0 )
\r
184 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
186 /*-----------------------------------------------------------*/
\r
188 /* Handles to the two counter tasks. These could be passed in as parameters
\r
189 to the controller task to prevent them having to be file scope. */
\r
190 static TaskHandle_t xContinuousIncrementHandle, xLimitedIncrementHandle, xChangePriorityWhenSuspendedHandle;
\r
192 /* The shared counter variable. This is passed in as a parameter to the two
\r
193 counter variables for demonstration purposes. */
\r
194 static unsigned long ulCounter;
\r
196 /* Variable used in a similar way by the test that checks the raising and
\r
197 lowering of task priorities while the scheduler is suspended. */
\r
198 static unsigned long ulPrioritySetCounter;
\r
200 /* Variables used to check that the tasks are still operating without error.
\r
201 Each complete iteration of the controller task increments this variable
\r
202 provided no errors have been found. The variable maintaining the same value
\r
203 is therefore indication of an error. */
\r
204 static unsigned short usCheckVariable = ( unsigned short ) 0;
\r
205 static portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
\r
206 static portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
\r
207 static portBASE_TYPE xPriorityRaiseWhenSuspendedError = pdFALSE;
\r
209 /* Queue used by the second test. */
\r
210 QueueHandle_t xSuspendedTestQueue;
\r
212 /*-----------------------------------------------------------*/
\r
214 * Start the seven tasks as described at the top of the file.
\r
215 * Note that the limited count task is given a higher priority.
\r
217 void vStartDynamicPriorityTasks( void )
\r
219 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
220 xTaskCreate( vContinuousIncrementTask, "CONT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
\r
221 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
222 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
223 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_SEND", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
224 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RECV", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
225 xTaskCreate( prvChangePriorityWhenSuspendedTask, "1st_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
226 xTaskCreate( prvChangePriorityHelperTask, "2nd_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xChangePriorityWhenSuspendedHandle );
\r
228 /*-----------------------------------------------------------*/
\r
231 * Just loops around incrementing the shared variable until the limit has been
\r
232 * reached. Once the limit has been reached it suspends itself.
\r
234 static void vLimitedIncrementTask( void * pvParameters )
\r
236 unsigned long *pulCounter;
\r
238 /* Take a pointer to the shared variable from the parameters passed into
\r
240 pulCounter = ( unsigned long * ) pvParameters;
\r
242 /* This will run before the control task, so the first thing it does is
\r
243 suspend - the control task will resume it when ready. */
\r
244 vTaskSuspend( NULL );
\r
248 /* Just count up to a value then suspend. */
\r
249 ( *pulCounter )++;
\r
251 if( *pulCounter >= priMAX_COUNT )
\r
253 vTaskSuspend( NULL );
\r
257 /*-----------------------------------------------------------*/
\r
260 * Just keep counting the shared variable up. The control task will suspend
\r
261 * this task when it wants.
\r
263 static void vContinuousIncrementTask( void * pvParameters )
\r
265 unsigned long *pulCounter;
\r
266 unsigned portBASE_TYPE uxOurPriority;
\r
268 /* Take a pointer to the shared variable from the parameters passed into
\r
270 pulCounter = ( unsigned long * ) pvParameters;
\r
272 /* Query our priority so we can raise it when exclusive access to the
\r
273 shared variable is required. */
\r
274 uxOurPriority = uxTaskPriorityGet( NULL );
\r
278 /* Raise our priority above the controller task to ensure a context
\r
279 switch does not occur while we are accessing this variable. */
\r
280 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
281 ( *pulCounter )++;
\r
282 vTaskPrioritySet( NULL, uxOurPriority );
\r
284 #if configUSE_PREEMPTION == 0
\r
289 /*-----------------------------------------------------------*/
\r
292 * Controller task as described above.
\r
294 static void vCounterControlTask( void * pvParameters )
\r
296 unsigned long ulLastCounter;
\r
298 short sError = pdFALSE;
\r
299 const char * const pcTaskStartMsg = "Priority manipulation tasks started.\r\n";
\r
300 const char * const pcTaskFailMsg = "Priority manipulation Task Failed\r\n";
\r
302 /* Just to stop warning messages. */
\r
303 ( void ) pvParameters;
\r
305 /* Queue a message for printing to say the task has started. */
\r
306 vPrintDisplayMessage( &pcTaskStartMsg );
\r
310 /* Start with the counter at zero. */
\r
311 ulCounter = ( unsigned long ) 0;
\r
313 /* First section : */
\r
315 /* Check the continuous count task is running. */
\r
316 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
318 /* Suspend the continuous count task so we can take a mirror of the
\r
319 shared variable without risk of corruption. */
\r
320 vTaskSuspend( xContinuousIncrementHandle );
\r
321 ulLastCounter = ulCounter;
\r
322 vTaskResume( xContinuousIncrementHandle );
\r
324 /* Now delay to ensure the other task has processor time. */
\r
325 vTaskDelay( priSLEEP_TIME );
\r
327 /* Check the shared variable again. This time to ensure mutual
\r
328 exclusion the whole scheduler will be locked. This is just for
\r
332 if( ulLastCounter == ulCounter )
\r
334 /* The shared variable has not changed. There is a problem
\r
335 with the continuous count task so flag an error. */
\r
338 vPrintDisplayMessage( &pcTaskFailMsg );
\r
346 /* Second section: */
\r
348 /* Suspend the continuous counter task so it stops accessing the shared variable. */
\r
349 vTaskSuspend( xContinuousIncrementHandle );
\r
351 /* Reset the variable. */
\r
352 ulCounter = ( unsigned long ) 0;
\r
354 /* Resume the limited count task which has a higher priority than us.
\r
355 We should therefore not return from this call until the limited count
\r
356 task has suspended itself with a known value in the counter variable.
\r
357 The scheduler suspension is not necessary but is included for test
\r
360 vTaskResume( xLimitedIncrementHandle );
\r
363 /* Does the counter variable have the expected value? */
\r
364 if( ulCounter != priMAX_COUNT )
\r
367 vPrintDisplayMessage( &pcTaskFailMsg );
\r
370 if( sError == pdFALSE )
\r
372 /* If no errors have occurred then increment the check variable. */
\r
373 portENTER_CRITICAL();
\r
375 portEXIT_CRITICAL();
\r
378 #if configUSE_PREEMPTION == 0
\r
382 /* Resume the continuous count task and do it all again. */
\r
383 vTaskResume( xContinuousIncrementHandle );
\r
386 /*-----------------------------------------------------------*/
\r
388 static void vQueueSendWhenSuspendedTask( void *pvParameters )
\r
390 static unsigned long ulValueToSend = ( unsigned long ) 0;
\r
391 const char * const pcTaskStartMsg = "Queue send while suspended task started.\r\n";
\r
392 const char * const pcTaskFailMsg = "Queue send while suspended failed.\r\n";
\r
394 /* Just to stop warning messages. */
\r
395 ( void ) pvParameters;
\r
397 /* Queue a message for printing to say the task has started. */
\r
398 vPrintDisplayMessage( &pcTaskStartMsg );
\r
404 /* We must not block while the scheduler is suspended! */
\r
405 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
407 if( xSuspendedQueueSendError == pdFALSE )
\r
410 vPrintDisplayMessage( &pcTaskFailMsg );
\r
414 xSuspendedQueueSendError = pdTRUE;
\r
419 vTaskDelay( priSLEEP_TIME );
\r
424 /*-----------------------------------------------------------*/
\r
426 static void vQueueReceiveWhenSuspendedTask( void *pvParameters )
\r
428 static unsigned long ulExpectedValue = ( unsigned long ) 0, ulReceivedValue;
\r
429 const char * const pcTaskStartMsg = "Queue receive while suspended task started.\r\n";
\r
430 const char * const pcTaskFailMsg = "Queue receive while suspended failed.\r\n";
\r
431 portBASE_TYPE xGotValue;
\r
433 /* Just to stop warning messages. */
\r
434 ( void ) pvParameters;
\r
436 /* Queue a message for printing to say the task has started. */
\r
437 vPrintDisplayMessage( &pcTaskStartMsg );
\r
443 /* Suspending the scheduler here is fairly pointless and
\r
444 undesirable for a normal application. It is done here purely
\r
445 to test the scheduler. The inner xTaskResumeAll() should
\r
446 never return pdTRUE as the scheduler is still locked by the
\r
452 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
454 if( xTaskResumeAll() )
\r
456 xSuspendedQueueReceiveError = pdTRUE;
\r
461 #if configUSE_PREEMPTION == 0
\r
465 } while( xGotValue == pdFALSE );
\r
467 if( ulReceivedValue != ulExpectedValue )
\r
469 if( xSuspendedQueueReceiveError == pdFALSE )
\r
471 vPrintDisplayMessage( &pcTaskFailMsg );
\r
473 xSuspendedQueueReceiveError = pdTRUE;
\r
479 /*-----------------------------------------------------------*/
\r
481 static void prvChangePriorityWhenSuspendedTask( void *pvParameters )
\r
483 const char * const pcTaskStartMsg = "Priority change when suspended task started.\r\n";
\r
484 const char * const pcTaskFailMsg = "Priority change when suspended task failed.\r\n";
\r
486 /* Just to stop warning messages. */
\r
487 ( void ) pvParameters;
\r
489 /* Queue a message for printing to say the task has started. */
\r
490 vPrintDisplayMessage( &pcTaskStartMsg );
\r
494 /* Start with the counter at 0 so we know what the counter should be
\r
495 when we check it next. */
\r
496 ulPrioritySetCounter = ( unsigned long ) 0;
\r
498 /* Resume the helper task. At this time it has a priority lower than
\r
499 ours so no context switch should occur. */
\r
500 vTaskResume( xChangePriorityWhenSuspendedHandle );
\r
502 /* Check to ensure the task just resumed has not executed. */
\r
503 portENTER_CRITICAL();
\r
505 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
507 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
508 vPrintDisplayMessage( &pcTaskFailMsg );
\r
511 portEXIT_CRITICAL();
\r
513 /* Now try raising the priority while the scheduler is suspended. */
\r
516 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, ( configMAX_PRIORITIES - 1 ) );
\r
518 /* Again, even though the helper task has a priority greater than
\r
519 ours, it should not have executed yet because the scheduler is
\r
521 portENTER_CRITICAL();
\r
523 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
525 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
526 vPrintDisplayMessage( &pcTaskFailMsg );
\r
529 portEXIT_CRITICAL();
\r
533 /* Now the scheduler has been resumed the helper task should
\r
534 immediately preempt us and execute. When it executes it will increment
\r
535 the ulPrioritySetCounter exactly once before suspending itself.
\r
537 We should now always find the counter set to 1. */
\r
538 portENTER_CRITICAL();
\r
540 if( ulPrioritySetCounter != ( unsigned long ) 1 )
\r
542 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
543 vPrintDisplayMessage( &pcTaskFailMsg );
\r
546 portEXIT_CRITICAL();
\r
548 /* Delay until we try this again. */
\r
549 vTaskDelay( priSLEEP_TIME * 2 );
\r
551 /* Set the priority of the helper task back ready for the next
\r
552 execution of this task. */
\r
554 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, tskIDLE_PRIORITY );
\r
558 /*-----------------------------------------------------------*/
\r
560 static void prvChangePriorityHelperTask( void *pvParameters )
\r
562 /* Just to stop warning messages. */
\r
563 ( void ) pvParameters;
\r
567 /* This is the helper task for prvChangePriorityWhenSuspendedTask().
\r
568 It has it's priority raised and lowered. When it runs it simply
\r
569 increments the counter then suspends itself again. This allows
\r
570 prvChangePriorityWhenSuspendedTask() to know how many times it has
\r
572 ulPrioritySetCounter++;
\r
573 vTaskSuspend( NULL );
\r
576 /*-----------------------------------------------------------*/
\r
578 /* Called to check that all the created tasks are still running without error. */
\r
579 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
\r
581 /* Keep a history of the check variables so we know if it has been incremented
\r
582 since the last call. */
\r
583 static unsigned short usLastTaskCheck = ( unsigned short ) 0;
\r
584 portBASE_TYPE xReturn = pdTRUE;
\r
586 /* Check the tasks are still running by ensuring the check variable
\r
587 is still incrementing. */
\r
589 if( usCheckVariable == usLastTaskCheck )
\r
591 /* The check has not incremented so an error exists. */
\r
595 if( xSuspendedQueueSendError == pdTRUE )
\r
600 if( xSuspendedQueueReceiveError == pdTRUE )
\r
605 if( xPriorityRaiseWhenSuspendedError == pdTRUE )
\r
610 usLastTaskCheck = usCheckVariable;
\r