2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 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 its priority above that of the controller task before each
\r
39 * increment, lowering it again to its original priority before starting the
\r
42 * The other counter task increments the shared count variable on each
\r
43 * iteration of its 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 its 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
87 /* Scheduler include files. */
\r
88 #include "FreeRTOS.h"
\r
92 /* Demo app include files. */
\r
93 #include "dynamic.h"
\r
95 /* Function that implements the "limited count" task as described above. */
\r
96 static portTASK_FUNCTION_PROTO( vLimitedIncrementTask, pvParameters );
\r
98 /* Function that implements the "continuous count" task as described above. */
\r
99 static portTASK_FUNCTION_PROTO( vContinuousIncrementTask, pvParameters );
\r
101 /* Function that implements the controller task as described above. */
\r
102 static portTASK_FUNCTION_PROTO( vCounterControlTask, pvParameters );
\r
104 static portTASK_FUNCTION_PROTO( vQueueReceiveWhenSuspendedTask, pvParameters );
\r
105 static portTASK_FUNCTION_PROTO( vQueueSendWhenSuspendedTask, pvParameters );
\r
107 /* Demo task specific constants. */
\r
108 #ifndef priSUSPENDED_RX_TASK_STACK_SIZE
\r
109 #define priSUSPENDED_RX_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE )
\r
111 #define priSTACK_SIZE ( configMINIMAL_STACK_SIZE )
\r
112 #define priSLEEP_TIME pdMS_TO_TICKS( 128 )
\r
113 #define priLOOPS ( 5 )
\r
114 #define priMAX_COUNT ( ( uint32_t ) 0xff )
\r
115 #define priNO_BLOCK ( ( TickType_t ) 0 )
\r
116 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
118 /*-----------------------------------------------------------*/
\r
120 /* Handles to the two counter tasks. These could be passed in as parameters
\r
121 to the controller task to prevent them having to be file scope. */
\r
122 static TaskHandle_t xContinuousIncrementHandle, xLimitedIncrementHandle;
\r
124 /* The shared counter variable. This is passed in as a parameter to the two
\r
125 counter variables for demonstration purposes. */
\r
126 static uint32_t ulCounter;
\r
128 /* Variables used to check that the tasks are still operating without error.
\r
129 Each complete iteration of the controller task increments this variable
\r
130 provided no errors have been found. The variable maintaining the same value
\r
131 is therefore indication of an error. */
\r
132 static volatile uint16_t usCheckVariable = ( uint16_t ) 0;
\r
133 static volatile BaseType_t xSuspendedQueueSendError = pdFALSE;
\r
134 static volatile BaseType_t xSuspendedQueueReceiveError = pdFALSE;
\r
136 /* Queue used by the second test. */
\r
137 QueueHandle_t xSuspendedTestQueue;
\r
139 /* The value the queue receive task expects to receive next. This is file
\r
140 scope so xAreDynamicPriorityTasksStillRunning() can ensure it is still
\r
142 static uint32_t ulExpectedValue = ( uint32_t ) 0;
\r
144 /*-----------------------------------------------------------*/
\r
146 * Start the three tasks as described at the top of the file.
\r
147 * Note that the limited count task is given a higher priority.
\r
149 void vStartDynamicPriorityTasks( void )
\r
151 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( uint32_t ) );
\r
153 if( xSuspendedTestQueue != NULL )
\r
155 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
156 in use. The queue registry is provided as a means for kernel aware
\r
157 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
158 is not being used. The call to vQueueAddToRegistry() will be removed
\r
159 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
160 defined to be less than 1. */
\r
161 vQueueAddToRegistry( xSuspendedTestQueue, "Suspended_Test_Queue" );
\r
163 xTaskCreate( vContinuousIncrementTask, "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
\r
164 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
165 xTaskCreate( vCounterControlTask, "C_CTRL", priSUSPENDED_RX_TASK_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
166 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
167 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RX", priSUSPENDED_RX_TASK_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
170 /*-----------------------------------------------------------*/
\r
173 * Just loops around incrementing the shared variable until the limit has been
\r
174 * reached. Once the limit has been reached it suspends itself.
\r
176 static portTASK_FUNCTION( vLimitedIncrementTask, pvParameters )
\r
178 volatile uint32_t *pulCounter;
\r
180 /* Take a pointer to the shared variable from the parameters passed into
\r
182 pulCounter = ( volatile uint32_t * ) pvParameters;
\r
184 /* This will run before the control task, so the first thing it does is
\r
185 suspend - the control task will resume it when ready. */
\r
186 vTaskSuspend( NULL );
\r
190 /* Just count up to a value then suspend. */
\r
193 if( *pulCounter >= priMAX_COUNT )
\r
195 vTaskSuspend( NULL );
\r
199 /*-----------------------------------------------------------*/
\r
202 * Just keep counting the shared variable up. The control task will suspend
\r
203 * this task when it wants.
\r
205 static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )
\r
207 volatile uint32_t *pulCounter;
\r
208 UBaseType_t uxOurPriority;
\r
210 /* Take a pointer to the shared variable from the parameters passed into
\r
212 pulCounter = ( volatile uint32_t * ) pvParameters;
\r
214 /* Query our priority so we can raise it when exclusive access to the
\r
215 shared variable is required. */
\r
216 uxOurPriority = uxTaskPriorityGet( NULL );
\r
220 /* Raise the priority above the controller task to ensure a context
\r
221 switch does not occur while the variable is being accessed. */
\r
222 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
224 configASSERT( ( uxTaskPriorityGet( NULL ) == ( uxOurPriority + 1 ) ) );
\r
227 vTaskPrioritySet( NULL, uxOurPriority );
\r
229 #if( configUSE_PREEMPTION == 0 )
\r
233 configASSERT( ( uxTaskPriorityGet( NULL ) == uxOurPriority ) );
\r
236 /*-----------------------------------------------------------*/
\r
239 * Controller task as described above.
\r
241 static portTASK_FUNCTION( vCounterControlTask, pvParameters )
\r
243 uint32_t ulLastCounter;
\r
245 short sError = pdFALSE;
\r
247 /* Just to stop warning messages. */
\r
248 ( void ) pvParameters;
\r
252 /* Start with the counter at zero. */
\r
253 ulCounter = ( uint32_t ) 0;
\r
255 /* First section : */
\r
257 /* Check the continuous count task is running. */
\r
258 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
260 /* Suspend the continuous count task so we can take a mirror of the
\r
261 shared variable without risk of corruption. This is not really
\r
262 needed as the other task raises its priority above this task's
\r
264 vTaskSuspend( xContinuousIncrementHandle );
\r
266 #if( INCLUDE_eTaskGetState == 1 )
\r
268 configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eSuspended );
\r
270 #endif /* INCLUDE_eTaskGetState */
\r
272 ulLastCounter = ulCounter;
\r
274 vTaskResume( xContinuousIncrementHandle );
\r
276 #if( configUSE_PREEMPTION == 0 )
\r
280 #if( INCLUDE_eTaskGetState == 1 )
\r
282 configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eReady );
\r
284 #endif /* INCLUDE_eTaskGetState */
\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
304 /* Second section: */
\r
306 /* Suspend the continuous counter task so it stops accessing the shared
\r
308 vTaskSuspend( xContinuousIncrementHandle );
\r
310 /* Reset the variable. */
\r
311 ulCounter = ( uint32_t ) 0;
\r
313 #if( INCLUDE_eTaskGetState == 1 )
\r
315 configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );
\r
317 #endif /* INCLUDE_eTaskGetState */
\r
319 /* Resume the limited count task which has a higher priority than us.
\r
320 We should therefore not return from this call until the limited count
\r
321 task has suspended itself with a known value in the counter variable. */
\r
322 vTaskResume( xLimitedIncrementHandle );
\r
324 #if( configUSE_PREEMPTION == 0 )
\r
328 /* This task should not run again until xLimitedIncrementHandle has
\r
329 suspended itself. */
\r
330 #if( INCLUDE_eTaskGetState == 1 )
\r
332 configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );
\r
334 #endif /* INCLUDE_eTaskGetState */
\r
336 /* Does the counter variable have the expected value? */
\r
337 if( ulCounter != priMAX_COUNT )
\r
342 if( sError == pdFALSE )
\r
344 /* If no errors have occurred then increment the check variable. */
\r
345 portENTER_CRITICAL();
\r
347 portEXIT_CRITICAL();
\r
350 /* Resume the continuous count task and do it all again. */
\r
351 vTaskResume( xContinuousIncrementHandle );
\r
353 #if( configUSE_PREEMPTION == 0 )
\r
358 /*-----------------------------------------------------------*/
\r
360 static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )
\r
362 static uint32_t ulValueToSend = ( uint32_t ) 0;
\r
364 /* Just to stop warning messages. */
\r
365 ( void ) pvParameters;
\r
371 /* We must not block while the scheduler is suspended! */
\r
372 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
374 xSuspendedQueueSendError = pdTRUE;
\r
379 vTaskDelay( priSLEEP_TIME );
\r
384 /*-----------------------------------------------------------*/
\r
386 static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )
\r
388 uint32_t ulReceivedValue;
\r
389 BaseType_t xGotValue;
\r
391 /* Just to stop warning messages. */
\r
392 ( void ) pvParameters;
\r
398 /* Suspending the scheduler here is fairly pointless and
\r
399 undesirable for a normal application. It is done here purely
\r
400 to test the scheduler. The inner xTaskResumeAll() should
\r
401 never return pdTRUE as the scheduler is still locked by the
\r
407 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
409 if( xTaskResumeAll() != pdFALSE )
\r
411 xSuspendedQueueReceiveError = pdTRUE;
\r
416 #if configUSE_PREEMPTION == 0
\r
422 } while( xGotValue == pdFALSE );
\r
424 if( ulReceivedValue != ulExpectedValue )
\r
426 xSuspendedQueueReceiveError = pdTRUE;
\r
429 if( xSuspendedQueueReceiveError != pdTRUE )
\r
431 /* Only increment the variable if an error has not occurred. This
\r
432 allows xAreDynamicPriorityTasksStillRunning() to check for stalled
\r
433 tasks as well as explicit errors. */
\r
438 /*-----------------------------------------------------------*/
\r
440 /* Called to check that all the created tasks are still running without error. */
\r
441 BaseType_t xAreDynamicPriorityTasksStillRunning( void )
\r
443 /* Keep a history of the check variables so we know if it has been incremented
\r
444 since the last call. */
\r
445 static uint16_t usLastTaskCheck = ( uint16_t ) 0;
\r
446 static uint32_t ulLastExpectedValue = ( uint32_t ) 0U;
\r
447 BaseType_t xReturn = pdTRUE;
\r
449 /* Check the tasks are still running by ensuring the check variable
\r
450 is still incrementing. */
\r
452 if( usCheckVariable == usLastTaskCheck )
\r
454 /* The check has not incremented so an error exists. */
\r
458 if( ulExpectedValue == ulLastExpectedValue )
\r
460 /* The value being received by the queue receive task has not
\r
461 incremented so an error exists. */
\r
465 if( xSuspendedQueueSendError == pdTRUE )
\r
470 if( xSuspendedQueueReceiveError == pdTRUE )
\r
475 usLastTaskCheck = usCheckVariable;
\r
476 ulLastExpectedValue = ulExpectedValue;
\r