2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * The first test creates three tasks - two counter tasks (one continuous count
\r
31 * and one limited count) and one controller. A "count" variable is shared
\r
32 * between all three tasks. The two counter tasks should never be in a "ready"
\r
33 * state at the same time. The controller task runs at the same priority as
\r
34 * the continuous count task, and at a lower priority than the limited count
\r
37 * One counter task loops indefinitely, incrementing the shared count variable
\r
38 * on each iteration. To ensure it has exclusive access to the variable it
\r
39 * raises it's priority above that of the controller task before each
\r
40 * increment, lowering it again to it's original priority before starting the
\r
43 * The other counter task increments the shared count variable on each
\r
44 * iteration of it's loop until the count has reached a limit of 0xff - at
\r
45 * which point it suspends itself. It will not start a new loop until the
\r
46 * controller task has made it "ready" again by calling vTaskResume ().
\r
47 * This second counter task operates at a higher priority than controller
\r
48 * task so does not need to worry about mutual exclusion of the counter
\r
51 * The controller task is in two sections. The first section controls and
\r
52 * monitors the continuous count task. When this section is operational the
\r
53 * limited count task is suspended. Likewise, the second section controls
\r
54 * and monitors the limited count task. When this section is operational the
\r
55 * continuous count task is suspended.
\r
57 * In the first section the controller task first takes a copy of the shared
\r
58 * count variable. To ensure mutual exclusion on the count variable it
\r
59 * suspends the continuous count task, resuming it again when the copy has been
\r
60 * taken. The controller task then sleeps for a fixed period - during which
\r
61 * the continuous count task will execute and increment the shared variable.
\r
62 * When the controller task wakes it checks that the continuous count task
\r
63 * has executed by comparing the copy of the shared variable with its current
\r
64 * value. This time, to ensure mutual exclusion, the scheduler itself is
\r
65 * suspended with a call to vTaskSuspendAll (). This is for demonstration
\r
66 * purposes only and is not a recommended technique due to its inefficiency.
\r
68 * After a fixed number of iterations the controller task suspends the
\r
69 * continuous count task, and moves on to its second section.
\r
71 * At the start of the second section the shared variable is cleared to zero.
\r
72 * The limited count task is then woken from it's suspension by a call to
\r
73 * vTaskResume (). As this counter task operates at a higher priority than
\r
74 * the controller task the controller task should not run again until the
\r
75 * shared variable has been counted up to the limited value causing the counter
\r
76 * task to suspend itself. The next line after vTaskResume () is therefore
\r
77 * a check on the shared variable to ensure everything is as expected.
\r
80 * The second test consists of a couple of very simple tasks that post onto a
\r
81 * queue while the scheduler is suspended. This test was added to test parts
\r
82 * of the scheduler not exercised by the first test.
\r
85 * The final set of two tasks implements a third test. This simply raises the
\r
86 * priority of a task while the scheduler is suspended. Again this test was
\r
87 * added to exercise parts of the code not covered by the first test.
\r
89 * \page Priorities dynamic.c
\r
90 * \ingroup DemoFiles
\r
97 + Delay periods are now specified using variables and constants of
\r
98 TickType_t rather than unsigned long.
\r
99 + Added a second, simple test that uses the functions
\r
100 vQueueReceiveWhenSuspendedTask() and vQueueSendWhenSuspendedTask().
\r
102 Changes from V3.1.1
\r
104 + Added a third simple test that uses the vTaskPrioritySet() function
\r
105 while the scheduler is suspended.
\r
106 + Modified the controller task slightly to test the calling of
\r
107 vTaskResumeAll() while the scheduler is suspended.
\r
110 #include <stdlib.h>
\r
112 /* Scheduler include files. */
\r
113 #include "FreeRTOS.h"
\r
115 #include "semphr.h"
\r
117 /* Demo app include files. */
\r
118 #include "dynamic.h"
\r
121 /* Function that implements the "limited count" task as described above. */
\r
122 static void vLimitedIncrementTask( void * pvParameters );
\r
124 /* Function that implements the "continuous count" task as described above. */
\r
125 static void vContinuousIncrementTask( void * pvParameters );
\r
127 /* Function that implements the controller task as described above. */
\r
128 static void vCounterControlTask( void * pvParameters );
\r
130 /* The simple test functions that check sending and receiving while the
\r
131 scheduler is suspended. */
\r
132 static void vQueueReceiveWhenSuspendedTask( void *pvParameters );
\r
133 static void vQueueSendWhenSuspendedTask( void *pvParameters );
\r
135 /* The simple test functions that check raising and lowering of task priorities
\r
136 while the scheduler is suspended. */
\r
137 static void prvChangePriorityWhenSuspendedTask( void *pvParameters );
\r
138 static void prvChangePriorityHelperTask( void *pvParameters );
\r
141 /* Demo task specific constants. */
\r
142 #define priSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
\r
143 #define priSLEEP_TIME ( ( TickType_t ) 50 )
\r
144 #define priLOOPS ( 5 )
\r
145 #define priMAX_COUNT ( ( unsigned long ) 0xff )
\r
146 #define priNO_BLOCK ( ( TickType_t ) 0 )
\r
147 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
149 /*-----------------------------------------------------------*/
\r
151 /* Handles to the two counter tasks. These could be passed in as parameters
\r
152 to the controller task to prevent them having to be file scope. */
\r
153 static TaskHandle_t xContinuousIncrementHandle, xLimitedIncrementHandle, xChangePriorityWhenSuspendedHandle;
\r
155 /* The shared counter variable. This is passed in as a parameter to the two
\r
156 counter variables for demonstration purposes. */
\r
157 static unsigned long ulCounter;
\r
159 /* Variable used in a similar way by the test that checks the raising and
\r
160 lowering of task priorities while the scheduler is suspended. */
\r
161 static unsigned long ulPrioritySetCounter;
\r
163 /* Variables used to check that the tasks are still operating without error.
\r
164 Each complete iteration of the controller task increments this variable
\r
165 provided no errors have been found. The variable maintaining the same value
\r
166 is therefore indication of an error. */
\r
167 static unsigned short usCheckVariable = ( unsigned short ) 0;
\r
168 static portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
\r
169 static portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
\r
170 static portBASE_TYPE xPriorityRaiseWhenSuspendedError = pdFALSE;
\r
172 /* Queue used by the second test. */
\r
173 QueueHandle_t xSuspendedTestQueue;
\r
175 /*-----------------------------------------------------------*/
\r
177 * Start the seven tasks as described at the top of the file.
\r
178 * Note that the limited count task is given a higher priority.
\r
180 void vStartDynamicPriorityTasks( void )
\r
182 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
183 xTaskCreate( vContinuousIncrementTask, "CONT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
\r
184 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
185 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
186 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_SEND", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
187 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RECV", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
188 xTaskCreate( prvChangePriorityWhenSuspendedTask, "1st_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
189 xTaskCreate( prvChangePriorityHelperTask, "2nd_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xChangePriorityWhenSuspendedHandle );
\r
191 /*-----------------------------------------------------------*/
\r
194 * Just loops around incrementing the shared variable until the limit has been
\r
195 * reached. Once the limit has been reached it suspends itself.
\r
197 static void vLimitedIncrementTask( void * pvParameters )
\r
199 unsigned long *pulCounter;
\r
201 /* Take a pointer to the shared variable from the parameters passed into
\r
203 pulCounter = ( unsigned long * ) pvParameters;
\r
205 /* This will run before the control task, so the first thing it does is
\r
206 suspend - the control task will resume it when ready. */
\r
207 vTaskSuspend( NULL );
\r
211 /* Just count up to a value then suspend. */
\r
212 ( *pulCounter )++;
\r
214 if( *pulCounter >= priMAX_COUNT )
\r
216 vTaskSuspend( NULL );
\r
220 /*-----------------------------------------------------------*/
\r
223 * Just keep counting the shared variable up. The control task will suspend
\r
224 * this task when it wants.
\r
226 static void vContinuousIncrementTask( void * pvParameters )
\r
228 unsigned long *pulCounter;
\r
229 unsigned portBASE_TYPE uxOurPriority;
\r
231 /* Take a pointer to the shared variable from the parameters passed into
\r
233 pulCounter = ( unsigned long * ) pvParameters;
\r
235 /* Query our priority so we can raise it when exclusive access to the
\r
236 shared variable is required. */
\r
237 uxOurPriority = uxTaskPriorityGet( NULL );
\r
241 /* Raise our priority above the controller task to ensure a context
\r
242 switch does not occur while we are accessing this variable. */
\r
243 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
244 ( *pulCounter )++;
\r
245 vTaskPrioritySet( NULL, uxOurPriority );
\r
247 #if configUSE_PREEMPTION == 0
\r
252 /*-----------------------------------------------------------*/
\r
255 * Controller task as described above.
\r
257 static void vCounterControlTask( void * pvParameters )
\r
259 unsigned long ulLastCounter;
\r
261 short sError = pdFALSE;
\r
262 const char * const pcTaskStartMsg = "Priority manipulation tasks started.\r\n";
\r
263 const char * const pcTaskFailMsg = "Priority manipulation Task Failed\r\n";
\r
265 /* Just to stop warning messages. */
\r
266 ( void ) pvParameters;
\r
268 /* Queue a message for printing to say the task has started. */
\r
269 vPrintDisplayMessage( &pcTaskStartMsg );
\r
273 /* Start with the counter at zero. */
\r
274 ulCounter = ( unsigned long ) 0;
\r
276 /* First section : */
\r
278 /* Check the continuous count task is running. */
\r
279 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
281 /* Suspend the continuous count task so we can take a mirror of the
\r
282 shared variable without risk of corruption. */
\r
283 vTaskSuspend( xContinuousIncrementHandle );
\r
284 ulLastCounter = ulCounter;
\r
285 vTaskResume( xContinuousIncrementHandle );
\r
287 /* Now delay to ensure the other task has processor time. */
\r
288 vTaskDelay( priSLEEP_TIME );
\r
290 /* Check the shared variable again. This time to ensure mutual
\r
291 exclusion the whole scheduler will be locked. This is just for
\r
295 if( ulLastCounter == ulCounter )
\r
297 /* The shared variable has not changed. There is a problem
\r
298 with the continuous count task so flag an error. */
\r
301 vPrintDisplayMessage( &pcTaskFailMsg );
\r
309 /* Second section: */
\r
311 /* Suspend the continuous counter task so it stops accessing the shared variable. */
\r
312 vTaskSuspend( xContinuousIncrementHandle );
\r
314 /* Reset the variable. */
\r
315 ulCounter = ( unsigned long ) 0;
\r
317 /* Resume the limited count task which has a higher priority than us.
\r
318 We should therefore not return from this call until the limited count
\r
319 task has suspended itself with a known value in the counter variable.
\r
320 The scheduler suspension is not necessary but is included for test
\r
323 vTaskResume( xLimitedIncrementHandle );
\r
326 /* Does the counter variable have the expected value? */
\r
327 if( ulCounter != priMAX_COUNT )
\r
330 vPrintDisplayMessage( &pcTaskFailMsg );
\r
333 if( sError == pdFALSE )
\r
335 /* If no errors have occurred then increment the check variable. */
\r
336 portENTER_CRITICAL();
\r
338 portEXIT_CRITICAL();
\r
341 #if configUSE_PREEMPTION == 0
\r
345 /* Resume the continuous count task and do it all again. */
\r
346 vTaskResume( xContinuousIncrementHandle );
\r
349 /*-----------------------------------------------------------*/
\r
351 static void vQueueSendWhenSuspendedTask( void *pvParameters )
\r
353 static unsigned long ulValueToSend = ( unsigned long ) 0;
\r
354 const char * const pcTaskStartMsg = "Queue send while suspended task started.\r\n";
\r
355 const char * const pcTaskFailMsg = "Queue send while suspended failed.\r\n";
\r
357 /* Just to stop warning messages. */
\r
358 ( void ) pvParameters;
\r
360 /* Queue a message for printing to say the task has started. */
\r
361 vPrintDisplayMessage( &pcTaskStartMsg );
\r
367 /* We must not block while the scheduler is suspended! */
\r
368 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
370 if( xSuspendedQueueSendError == pdFALSE )
\r
373 vPrintDisplayMessage( &pcTaskFailMsg );
\r
377 xSuspendedQueueSendError = pdTRUE;
\r
382 vTaskDelay( priSLEEP_TIME );
\r
387 /*-----------------------------------------------------------*/
\r
389 static void vQueueReceiveWhenSuspendedTask( void *pvParameters )
\r
391 static unsigned long ulExpectedValue = ( unsigned long ) 0, ulReceivedValue;
\r
392 const char * const pcTaskStartMsg = "Queue receive while suspended task started.\r\n";
\r
393 const char * const pcTaskFailMsg = "Queue receive while suspended failed.\r\n";
\r
394 portBASE_TYPE xGotValue;
\r
396 /* Just to stop warning messages. */
\r
397 ( void ) pvParameters;
\r
399 /* Queue a message for printing to say the task has started. */
\r
400 vPrintDisplayMessage( &pcTaskStartMsg );
\r
406 /* Suspending the scheduler here is fairly pointless and
\r
407 undesirable for a normal application. It is done here purely
\r
408 to test the scheduler. The inner xTaskResumeAll() should
\r
409 never return pdTRUE as the scheduler is still locked by the
\r
415 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
417 if( xTaskResumeAll() )
\r
419 xSuspendedQueueReceiveError = pdTRUE;
\r
424 #if configUSE_PREEMPTION == 0
\r
428 } while( xGotValue == pdFALSE );
\r
430 if( ulReceivedValue != ulExpectedValue )
\r
432 if( xSuspendedQueueReceiveError == pdFALSE )
\r
434 vPrintDisplayMessage( &pcTaskFailMsg );
\r
436 xSuspendedQueueReceiveError = pdTRUE;
\r
442 /*-----------------------------------------------------------*/
\r
444 static void prvChangePriorityWhenSuspendedTask( void *pvParameters )
\r
446 const char * const pcTaskStartMsg = "Priority change when suspended task started.\r\n";
\r
447 const char * const pcTaskFailMsg = "Priority change when suspended task failed.\r\n";
\r
449 /* Just to stop warning messages. */
\r
450 ( void ) pvParameters;
\r
452 /* Queue a message for printing to say the task has started. */
\r
453 vPrintDisplayMessage( &pcTaskStartMsg );
\r
457 /* Start with the counter at 0 so we know what the counter should be
\r
458 when we check it next. */
\r
459 ulPrioritySetCounter = ( unsigned long ) 0;
\r
461 /* Resume the helper task. At this time it has a priority lower than
\r
462 ours so no context switch should occur. */
\r
463 vTaskResume( xChangePriorityWhenSuspendedHandle );
\r
465 /* Check to ensure the task just resumed has not executed. */
\r
466 portENTER_CRITICAL();
\r
468 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
470 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
471 vPrintDisplayMessage( &pcTaskFailMsg );
\r
474 portEXIT_CRITICAL();
\r
476 /* Now try raising the priority while the scheduler is suspended. */
\r
479 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, ( configMAX_PRIORITIES - 1 ) );
\r
481 /* Again, even though the helper task has a priority greater than
\r
482 ours, it should not have executed yet because the scheduler is
\r
484 portENTER_CRITICAL();
\r
486 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
488 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
489 vPrintDisplayMessage( &pcTaskFailMsg );
\r
492 portEXIT_CRITICAL();
\r
496 /* Now the scheduler has been resumed the helper task should
\r
497 immediately preempt us and execute. When it executes it will increment
\r
498 the ulPrioritySetCounter exactly once before suspending itself.
\r
500 We should now always find the counter set to 1. */
\r
501 portENTER_CRITICAL();
\r
503 if( ulPrioritySetCounter != ( unsigned long ) 1 )
\r
505 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
506 vPrintDisplayMessage( &pcTaskFailMsg );
\r
509 portEXIT_CRITICAL();
\r
511 /* Delay until we try this again. */
\r
512 vTaskDelay( priSLEEP_TIME * 2 );
\r
514 /* Set the priority of the helper task back ready for the next
\r
515 execution of this task. */
\r
517 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, tskIDLE_PRIORITY );
\r
521 /*-----------------------------------------------------------*/
\r
523 static void prvChangePriorityHelperTask( void *pvParameters )
\r
525 /* Just to stop warning messages. */
\r
526 ( void ) pvParameters;
\r
530 /* This is the helper task for prvChangePriorityWhenSuspendedTask().
\r
531 It has it's priority raised and lowered. When it runs it simply
\r
532 increments the counter then suspends itself again. This allows
\r
533 prvChangePriorityWhenSuspendedTask() to know how many times it has
\r
535 ulPrioritySetCounter++;
\r
536 vTaskSuspend( NULL );
\r
539 /*-----------------------------------------------------------*/
\r
541 /* Called to check that all the created tasks are still running without error. */
\r
542 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
\r
544 /* Keep a history of the check variables so we know if it has been incremented
\r
545 since the last call. */
\r
546 static unsigned short usLastTaskCheck = ( unsigned short ) 0;
\r
547 portBASE_TYPE xReturn = pdTRUE;
\r
549 /* Check the tasks are still running by ensuring the check variable
\r
550 is still incrementing. */
\r
552 if( usCheckVariable == usLastTaskCheck )
\r
554 /* The check has not incremented so an error exists. */
\r
558 if( xSuspendedQueueSendError == pdTRUE )
\r
563 if( xSuspendedQueueReceiveError == pdTRUE )
\r
568 if( xPriorityRaiseWhenSuspendedError == pdTRUE )
\r
573 usLastTaskCheck = usCheckVariable;
\r