2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 * The first test creates three tasks - two counter tasks (one continuous count
\r
30 * and one limited count) and one controller. A "count" variable is shared
\r
31 * between all three tasks. The two counter tasks should never be in a "ready"
\r
32 * state at the same time. The controller task runs at the same priority as
\r
33 * the continuous count task, and at a lower priority than the limited count
\r
36 * One counter task loops indefinitely, incrementing the shared count variable
\r
37 * on each iteration. To ensure it has exclusive access to the variable it
\r
38 * raises it's priority above that of the controller task before each
\r
39 * increment, lowering it again to it's original priority before starting the
\r
42 * The other counter task increments the shared count variable on each
\r
43 * iteration of it's loop until the count has reached a limit of 0xff - at
\r
44 * which point it suspends itself. It will not start a new loop until the
\r
45 * controller task has made it "ready" again by calling vTaskResume ().
\r
46 * This second counter task operates at a higher priority than controller
\r
47 * task so does not need to worry about mutual exclusion of the counter
\r
50 * The controller task is in two sections. The first section controls and
\r
51 * monitors the continuous count task. When this section is operational the
\r
52 * limited count task is suspended. Likewise, the second section controls
\r
53 * and monitors the limited count task. When this section is operational the
\r
54 * continuous count task is suspended.
\r
56 * In the first section the controller task first takes a copy of the shared
\r
57 * count variable. To ensure mutual exclusion on the count variable it
\r
58 * suspends the continuous count task, resuming it again when the copy has been
\r
59 * taken. The controller task then sleeps for a fixed period - during which
\r
60 * the continuous count task will execute and increment the shared variable.
\r
61 * When the controller task wakes it checks that the continuous count task
\r
62 * has executed by comparing the copy of the shared variable with its current
\r
63 * value. This time, to ensure mutual exclusion, the scheduler itself is
\r
64 * suspended with a call to vTaskSuspendAll (). This is for demonstration
\r
65 * purposes only and is not a recommended technique due to its inefficiency.
\r
67 * After a fixed number of iterations the controller task suspends the
\r
68 * continuous count task, and moves on to its second section.
\r
70 * At the start of the second section the shared variable is cleared to zero.
\r
71 * The limited count task is then woken from it's suspension by a call to
\r
72 * vTaskResume (). As this counter task operates at a higher priority than
\r
73 * the controller task the controller task should not run again until the
\r
74 * shared variable has been counted up to the limited value causing the counter
\r
75 * task to suspend itself. The next line after vTaskResume () is therefore
\r
76 * a check on the shared variable to ensure everything is as expected.
\r
79 * The second test consists of a couple of very simple tasks that post onto a
\r
80 * queue while the scheduler is suspended. This test was added to test parts
\r
81 * of the scheduler not exercised by the first test.
\r
84 * The final set of two tasks implements a third test. This simply raises the
\r
85 * priority of a task while the scheduler is suspended. Again this test was
\r
86 * added to exercise parts of the code not covered by the first test.
\r
88 * \page Priorities dynamic.c
\r
89 * \ingroup DemoFiles
\r
96 + Delay periods are now specified using variables and constants of
\r
97 TickType_t rather than unsigned long.
\r
98 + Added a second, simple test that uses the functions
\r
99 vQueueReceiveWhenSuspendedTask() and vQueueSendWhenSuspendedTask().
\r
101 Changes from V3.1.1
\r
103 + Added a third simple test that uses the vTaskPrioritySet() function
\r
104 while the scheduler is suspended.
\r
105 + Modified the controller task slightly to test the calling of
\r
106 vTaskResumeAll() while the scheduler is suspended.
\r
109 #include <stdlib.h>
\r
111 /* Scheduler include files. */
\r
112 #include "FreeRTOS.h"
\r
114 #include "semphr.h"
\r
116 /* Demo app include files. */
\r
117 #include "dynamic.h"
\r
120 /* Function that implements the "limited count" task as described above. */
\r
121 static void vLimitedIncrementTask( void * pvParameters );
\r
123 /* Function that implements the "continuous count" task as described above. */
\r
124 static void vContinuousIncrementTask( void * pvParameters );
\r
126 /* Function that implements the controller task as described above. */
\r
127 static void vCounterControlTask( void * pvParameters );
\r
129 /* The simple test functions that check sending and receiving while the
\r
130 scheduler is suspended. */
\r
131 static void vQueueReceiveWhenSuspendedTask( void *pvParameters );
\r
132 static void vQueueSendWhenSuspendedTask( void *pvParameters );
\r
134 /* The simple test functions that check raising and lowering of task priorities
\r
135 while the scheduler is suspended. */
\r
136 static void prvChangePriorityWhenSuspendedTask( void *pvParameters );
\r
137 static void prvChangePriorityHelperTask( void *pvParameters );
\r
140 /* Demo task specific constants. */
\r
141 #define priSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
\r
142 #define priSLEEP_TIME ( ( TickType_t ) 50 )
\r
143 #define priLOOPS ( 5 )
\r
144 #define priMAX_COUNT ( ( unsigned long ) 0xff )
\r
145 #define priNO_BLOCK ( ( TickType_t ) 0 )
\r
146 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
148 /*-----------------------------------------------------------*/
\r
150 /* Handles to the two counter tasks. These could be passed in as parameters
\r
151 to the controller task to prevent them having to be file scope. */
\r
152 static TaskHandle_t xContinuousIncrementHandle, xLimitedIncrementHandle, xChangePriorityWhenSuspendedHandle;
\r
154 /* The shared counter variable. This is passed in as a parameter to the two
\r
155 counter variables for demonstration purposes. */
\r
156 static unsigned long ulCounter;
\r
158 /* Variable used in a similar way by the test that checks the raising and
\r
159 lowering of task priorities while the scheduler is suspended. */
\r
160 static unsigned long ulPrioritySetCounter;
\r
162 /* Variables used to check that the tasks are still operating without error.
\r
163 Each complete iteration of the controller task increments this variable
\r
164 provided no errors have been found. The variable maintaining the same value
\r
165 is therefore indication of an error. */
\r
166 static unsigned short usCheckVariable = ( unsigned short ) 0;
\r
167 static portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
\r
168 static portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
\r
169 static portBASE_TYPE xPriorityRaiseWhenSuspendedError = pdFALSE;
\r
171 /* Queue used by the second test. */
\r
172 QueueHandle_t xSuspendedTestQueue;
\r
174 /*-----------------------------------------------------------*/
\r
176 * Start the seven tasks as described at the top of the file.
\r
177 * Note that the limited count task is given a higher priority.
\r
179 void vStartDynamicPriorityTasks( void )
\r
181 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
182 xTaskCreate( vContinuousIncrementTask, "CONT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
\r
183 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
184 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
185 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_SEND", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
186 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RECV", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
187 xTaskCreate( prvChangePriorityWhenSuspendedTask, "1st_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
188 xTaskCreate( prvChangePriorityHelperTask, "2nd_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xChangePriorityWhenSuspendedHandle );
\r
190 /*-----------------------------------------------------------*/
\r
193 * Just loops around incrementing the shared variable until the limit has been
\r
194 * reached. Once the limit has been reached it suspends itself.
\r
196 static void vLimitedIncrementTask( void * pvParameters )
\r
198 unsigned long *pulCounter;
\r
200 /* Take a pointer to the shared variable from the parameters passed into
\r
202 pulCounter = ( unsigned long * ) pvParameters;
\r
204 /* This will run before the control task, so the first thing it does is
\r
205 suspend - the control task will resume it when ready. */
\r
206 vTaskSuspend( NULL );
\r
210 /* Just count up to a value then suspend. */
\r
211 ( *pulCounter )++;
\r
213 if( *pulCounter >= priMAX_COUNT )
\r
215 vTaskSuspend( NULL );
\r
219 /*-----------------------------------------------------------*/
\r
222 * Just keep counting the shared variable up. The control task will suspend
\r
223 * this task when it wants.
\r
225 static void vContinuousIncrementTask( void * pvParameters )
\r
227 unsigned long *pulCounter;
\r
228 unsigned portBASE_TYPE uxOurPriority;
\r
230 /* Take a pointer to the shared variable from the parameters passed into
\r
232 pulCounter = ( unsigned long * ) pvParameters;
\r
234 /* Query our priority so we can raise it when exclusive access to the
\r
235 shared variable is required. */
\r
236 uxOurPriority = uxTaskPriorityGet( NULL );
\r
240 /* Raise our priority above the controller task to ensure a context
\r
241 switch does not occur while we are accessing this variable. */
\r
242 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
243 ( *pulCounter )++;
\r
244 vTaskPrioritySet( NULL, uxOurPriority );
\r
246 #if configUSE_PREEMPTION == 0
\r
251 /*-----------------------------------------------------------*/
\r
254 * Controller task as described above.
\r
256 static void vCounterControlTask( void * pvParameters )
\r
258 unsigned long ulLastCounter;
\r
260 short sError = pdFALSE;
\r
261 const char * const pcTaskStartMsg = "Priority manipulation tasks started.\r\n";
\r
262 const char * const pcTaskFailMsg = "Priority manipulation Task Failed\r\n";
\r
264 /* Just to stop warning messages. */
\r
265 ( void ) pvParameters;
\r
267 /* Queue a message for printing to say the task has started. */
\r
268 vPrintDisplayMessage( &pcTaskStartMsg );
\r
272 /* Start with the counter at zero. */
\r
273 ulCounter = ( unsigned long ) 0;
\r
275 /* First section : */
\r
277 /* Check the continuous count task is running. */
\r
278 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
280 /* Suspend the continuous count task so we can take a mirror of the
\r
281 shared variable without risk of corruption. */
\r
282 vTaskSuspend( xContinuousIncrementHandle );
\r
283 ulLastCounter = ulCounter;
\r
284 vTaskResume( xContinuousIncrementHandle );
\r
286 /* Now delay to ensure the other task has processor time. */
\r
287 vTaskDelay( priSLEEP_TIME );
\r
289 /* Check the shared variable again. This time to ensure mutual
\r
290 exclusion the whole scheduler will be locked. This is just for
\r
294 if( ulLastCounter == ulCounter )
\r
296 /* The shared variable has not changed. There is a problem
\r
297 with the continuous count task so flag an error. */
\r
300 vPrintDisplayMessage( &pcTaskFailMsg );
\r
308 /* Second section: */
\r
310 /* Suspend the continuous counter task so it stops accessing the shared variable. */
\r
311 vTaskSuspend( xContinuousIncrementHandle );
\r
313 /* Reset the variable. */
\r
314 ulCounter = ( unsigned long ) 0;
\r
316 /* Resume the limited count task which has a higher priority than us.
\r
317 We should therefore not return from this call until the limited count
\r
318 task has suspended itself with a known value in the counter variable.
\r
319 The scheduler suspension is not necessary but is included for test
\r
322 vTaskResume( xLimitedIncrementHandle );
\r
325 /* Does the counter variable have the expected value? */
\r
326 if( ulCounter != priMAX_COUNT )
\r
329 vPrintDisplayMessage( &pcTaskFailMsg );
\r
332 if( sError == pdFALSE )
\r
334 /* If no errors have occurred then increment the check variable. */
\r
335 portENTER_CRITICAL();
\r
337 portEXIT_CRITICAL();
\r
340 #if configUSE_PREEMPTION == 0
\r
344 /* Resume the continuous count task and do it all again. */
\r
345 vTaskResume( xContinuousIncrementHandle );
\r
348 /*-----------------------------------------------------------*/
\r
350 static void vQueueSendWhenSuspendedTask( void *pvParameters )
\r
352 static unsigned long ulValueToSend = ( unsigned long ) 0;
\r
353 const char * const pcTaskStartMsg = "Queue send while suspended task started.\r\n";
\r
354 const char * const pcTaskFailMsg = "Queue send while suspended failed.\r\n";
\r
356 /* Just to stop warning messages. */
\r
357 ( void ) pvParameters;
\r
359 /* Queue a message for printing to say the task has started. */
\r
360 vPrintDisplayMessage( &pcTaskStartMsg );
\r
366 /* We must not block while the scheduler is suspended! */
\r
367 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
369 if( xSuspendedQueueSendError == pdFALSE )
\r
372 vPrintDisplayMessage( &pcTaskFailMsg );
\r
376 xSuspendedQueueSendError = pdTRUE;
\r
381 vTaskDelay( priSLEEP_TIME );
\r
386 /*-----------------------------------------------------------*/
\r
388 static void vQueueReceiveWhenSuspendedTask( void *pvParameters )
\r
390 static unsigned long ulExpectedValue = ( unsigned long ) 0, ulReceivedValue;
\r
391 const char * const pcTaskStartMsg = "Queue receive while suspended task started.\r\n";
\r
392 const char * const pcTaskFailMsg = "Queue receive while suspended failed.\r\n";
\r
393 portBASE_TYPE xGotValue;
\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 /* Suspending the scheduler here is fairly pointless and
\r
406 undesirable for a normal application. It is done here purely
\r
407 to test the scheduler. The inner xTaskResumeAll() should
\r
408 never return pdTRUE as the scheduler is still locked by the
\r
414 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
416 if( xTaskResumeAll() )
\r
418 xSuspendedQueueReceiveError = pdTRUE;
\r
423 #if configUSE_PREEMPTION == 0
\r
427 } while( xGotValue == pdFALSE );
\r
429 if( ulReceivedValue != ulExpectedValue )
\r
431 if( xSuspendedQueueReceiveError == pdFALSE )
\r
433 vPrintDisplayMessage( &pcTaskFailMsg );
\r
435 xSuspendedQueueReceiveError = pdTRUE;
\r
441 /*-----------------------------------------------------------*/
\r
443 static void prvChangePriorityWhenSuspendedTask( void *pvParameters )
\r
445 const char * const pcTaskStartMsg = "Priority change when suspended task started.\r\n";
\r
446 const char * const pcTaskFailMsg = "Priority change when suspended task failed.\r\n";
\r
448 /* Just to stop warning messages. */
\r
449 ( void ) pvParameters;
\r
451 /* Queue a message for printing to say the task has started. */
\r
452 vPrintDisplayMessage( &pcTaskStartMsg );
\r
456 /* Start with the counter at 0 so we know what the counter should be
\r
457 when we check it next. */
\r
458 ulPrioritySetCounter = ( unsigned long ) 0;
\r
460 /* Resume the helper task. At this time it has a priority lower than
\r
461 ours so no context switch should occur. */
\r
462 vTaskResume( xChangePriorityWhenSuspendedHandle );
\r
464 /* Check to ensure the task just resumed has not executed. */
\r
465 portENTER_CRITICAL();
\r
467 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
469 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
470 vPrintDisplayMessage( &pcTaskFailMsg );
\r
473 portEXIT_CRITICAL();
\r
475 /* Now try raising the priority while the scheduler is suspended. */
\r
478 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, ( configMAX_PRIORITIES - 1 ) );
\r
480 /* Again, even though the helper task has a priority greater than
\r
481 ours, it should not have executed yet because the scheduler is
\r
483 portENTER_CRITICAL();
\r
485 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
487 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
488 vPrintDisplayMessage( &pcTaskFailMsg );
\r
491 portEXIT_CRITICAL();
\r
495 /* Now the scheduler has been resumed the helper task should
\r
496 immediately preempt us and execute. When it executes it will increment
\r
497 the ulPrioritySetCounter exactly once before suspending itself.
\r
499 We should now always find the counter set to 1. */
\r
500 portENTER_CRITICAL();
\r
502 if( ulPrioritySetCounter != ( unsigned long ) 1 )
\r
504 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
505 vPrintDisplayMessage( &pcTaskFailMsg );
\r
508 portEXIT_CRITICAL();
\r
510 /* Delay until we try this again. */
\r
511 vTaskDelay( priSLEEP_TIME * 2 );
\r
513 /* Set the priority of the helper task back ready for the next
\r
514 execution of this task. */
\r
516 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, tskIDLE_PRIORITY );
\r
520 /*-----------------------------------------------------------*/
\r
522 static void prvChangePriorityHelperTask( void *pvParameters )
\r
524 /* Just to stop warning messages. */
\r
525 ( void ) pvParameters;
\r
529 /* This is the helper task for prvChangePriorityWhenSuspendedTask().
\r
530 It has it's priority raised and lowered. When it runs it simply
\r
531 increments the counter then suspends itself again. This allows
\r
532 prvChangePriorityWhenSuspendedTask() to know how many times it has
\r
534 ulPrioritySetCounter++;
\r
535 vTaskSuspend( NULL );
\r
538 /*-----------------------------------------------------------*/
\r
540 /* Called to check that all the created tasks are still running without error. */
\r
541 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
\r
543 /* Keep a history of the check variables so we know if it has been incremented
\r
544 since the last call. */
\r
545 static unsigned short usLastTaskCheck = ( unsigned short ) 0;
\r
546 portBASE_TYPE xReturn = pdTRUE;
\r
548 /* Check the tasks are still running by ensuring the check variable
\r
549 is still incrementing. */
\r
551 if( usCheckVariable == usLastTaskCheck )
\r
553 /* The check has not incremented so an error exists. */
\r
557 if( xSuspendedQueueSendError == pdTRUE )
\r
562 if( xSuspendedQueueReceiveError == pdTRUE )
\r
567 if( xPriorityRaiseWhenSuspendedError == pdTRUE )
\r
572 usLastTaskCheck = usCheckVariable;
\r