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 its priority above that of the controller task before each
\r
40 * increment, lowering it again to its original priority before starting the
\r
43 * The other counter task increments the shared count variable on each
\r
44 * iteration of its 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 its 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
88 /* Scheduler include files. */
\r
89 #include "FreeRTOS.h"
\r
93 /* Demo app include files. */
\r
94 #include "dynamic.h"
\r
96 /* Function that implements the "limited count" task as described above. */
\r
97 static portTASK_FUNCTION_PROTO( vLimitedIncrementTask, pvParameters );
\r
99 /* Function that implements the "continuous count" task as described above. */
\r
100 static portTASK_FUNCTION_PROTO( vContinuousIncrementTask, pvParameters );
\r
102 /* Function that implements the controller task as described above. */
\r
103 static portTASK_FUNCTION_PROTO( vCounterControlTask, pvParameters );
\r
105 static portTASK_FUNCTION_PROTO( vQueueReceiveWhenSuspendedTask, pvParameters );
\r
106 static portTASK_FUNCTION_PROTO( vQueueSendWhenSuspendedTask, pvParameters );
\r
108 /* Demo task specific constants. */
\r
109 #define priSTACK_SIZE ( configMINIMAL_STACK_SIZE )
\r
110 #define priSLEEP_TIME pdMS_TO_TICKS( 128 )
\r
111 #define priLOOPS ( 5 )
\r
112 #define priMAX_COUNT ( ( uint32_t ) 0xff )
\r
113 #define priNO_BLOCK ( ( TickType_t ) 0 )
\r
114 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
116 /*-----------------------------------------------------------*/
\r
118 /* Handles to the two counter tasks. These could be passed in as parameters
\r
119 to the controller task to prevent them having to be file scope. */
\r
120 static TaskHandle_t xContinuousIncrementHandle, xLimitedIncrementHandle;
\r
122 /* The shared counter variable. This is passed in as a parameter to the two
\r
123 counter variables for demonstration purposes. */
\r
124 static uint32_t ulCounter;
\r
126 /* Variables used to check that the tasks are still operating without error.
\r
127 Each complete iteration of the controller task increments this variable
\r
128 provided no errors have been found. The variable maintaining the same value
\r
129 is therefore indication of an error. */
\r
130 static volatile uint16_t usCheckVariable = ( uint16_t ) 0;
\r
131 static volatile BaseType_t xSuspendedQueueSendError = pdFALSE;
\r
132 static volatile BaseType_t xSuspendedQueueReceiveError = pdFALSE;
\r
134 /* Queue used by the second test. */
\r
135 QueueHandle_t xSuspendedTestQueue;
\r
137 /* The value the queue receive task expects to receive next. This is file
\r
138 scope so xAreDynamicPriorityTasksStillRunning() can ensure it is still
\r
140 static uint32_t ulExpectedValue = ( uint32_t ) 0;
\r
142 /*-----------------------------------------------------------*/
\r
144 * Start the three tasks as described at the top of the file.
\r
145 * Note that the limited count task is given a higher priority.
\r
147 void vStartDynamicPriorityTasks( void )
\r
149 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( uint32_t ) );
\r
151 if( xSuspendedTestQueue != NULL )
\r
153 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
154 in use. The queue registry is provided as a means for kernel aware
\r
155 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
156 is not being used. The call to vQueueAddToRegistry() will be removed
\r
157 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
158 defined to be less than 1. */
\r
159 vQueueAddToRegistry( xSuspendedTestQueue, "Suspended_Test_Queue" );
\r
161 xTaskCreate( vContinuousIncrementTask, "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
\r
162 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
163 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
164 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
165 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
168 /*-----------------------------------------------------------*/
\r
171 * Just loops around incrementing the shared variable until the limit has been
\r
172 * reached. Once the limit has been reached it suspends itself.
\r
174 static portTASK_FUNCTION( vLimitedIncrementTask, pvParameters )
\r
176 volatile uint32_t *pulCounter;
\r
178 /* Take a pointer to the shared variable from the parameters passed into
\r
180 pulCounter = ( volatile uint32_t * ) pvParameters;
\r
182 /* This will run before the control task, so the first thing it does is
\r
183 suspend - the control task will resume it when ready. */
\r
184 vTaskSuspend( NULL );
\r
188 /* Just count up to a value then suspend. */
\r
191 if( *pulCounter >= priMAX_COUNT )
\r
193 vTaskSuspend( NULL );
\r
197 /*-----------------------------------------------------------*/
\r
200 * Just keep counting the shared variable up. The control task will suspend
\r
201 * this task when it wants.
\r
203 static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )
\r
205 volatile uint32_t *pulCounter;
\r
206 UBaseType_t uxOurPriority;
\r
208 /* Take a pointer to the shared variable from the parameters passed into
\r
210 pulCounter = ( volatile uint32_t * ) pvParameters;
\r
212 /* Query our priority so we can raise it when exclusive access to the
\r
213 shared variable is required. */
\r
214 uxOurPriority = uxTaskPriorityGet( NULL );
\r
218 /* Raise the priority above the controller task to ensure a context
\r
219 switch does not occur while the variable is being accessed. */
\r
220 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
222 configASSERT( ( uxTaskPriorityGet( NULL ) == ( uxOurPriority + 1 ) ) );
\r
225 vTaskPrioritySet( NULL, uxOurPriority );
\r
227 #if( configUSE_PREEMPTION == 0 )
\r
231 configASSERT( ( uxTaskPriorityGet( NULL ) == uxOurPriority ) );
\r
234 /*-----------------------------------------------------------*/
\r
237 * Controller task as described above.
\r
239 static portTASK_FUNCTION( vCounterControlTask, pvParameters )
\r
241 uint32_t ulLastCounter;
\r
243 short sError = pdFALSE;
\r
245 /* Just to stop warning messages. */
\r
246 ( void ) pvParameters;
\r
250 /* Start with the counter at zero. */
\r
251 ulCounter = ( uint32_t ) 0;
\r
253 /* First section : */
\r
255 /* Check the continuous count task is running. */
\r
256 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
258 /* Suspend the continuous count task so we can take a mirror of the
\r
259 shared variable without risk of corruption. This is not really
\r
260 needed as the other task raises its priority above this task's
\r
262 vTaskSuspend( xContinuousIncrementHandle );
\r
264 #if( INCLUDE_eTaskGetState == 1 )
\r
266 configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eSuspended );
\r
268 #endif /* INCLUDE_eTaskGetState */
\r
270 ulLastCounter = ulCounter;
\r
272 vTaskResume( xContinuousIncrementHandle );
\r
274 #if( configUSE_PREEMPTION == 0 )
\r
278 #if( INCLUDE_eTaskGetState == 1 )
\r
280 configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eReady );
\r
282 #endif /* INCLUDE_eTaskGetState */
\r
284 /* Now delay to ensure the other task has processor time. */
\r
285 vTaskDelay( priSLEEP_TIME );
\r
287 /* Check the shared variable again. This time to ensure mutual
\r
288 exclusion the whole scheduler will be locked. This is just for
\r
292 if( ulLastCounter == ulCounter )
\r
294 /* The shared variable has not changed. There is a problem
\r
295 with the continuous count task so flag an error. */
\r
302 /* Second section: */
\r
304 /* Suspend the continuous counter task so it stops accessing the shared
\r
306 vTaskSuspend( xContinuousIncrementHandle );
\r
308 /* Reset the variable. */
\r
309 ulCounter = ( uint32_t ) 0;
\r
311 #if( INCLUDE_eTaskGetState == 1 )
\r
313 configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );
\r
315 #endif /* INCLUDE_eTaskGetState */
\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 vTaskResume( xLimitedIncrementHandle );
\r
322 #if( configUSE_PREEMPTION == 0 )
\r
326 /* This task should not run again until xLimitedIncrementHandle has
\r
327 suspended itself. */
\r
328 #if( INCLUDE_eTaskGetState == 1 )
\r
330 configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );
\r
332 #endif /* INCLUDE_eTaskGetState */
\r
334 /* Does the counter variable have the expected value? */
\r
335 if( ulCounter != priMAX_COUNT )
\r
340 if( sError == pdFALSE )
\r
342 /* If no errors have occurred then increment the check variable. */
\r
343 portENTER_CRITICAL();
\r
345 portEXIT_CRITICAL();
\r
348 /* Resume the continuous count task and do it all again. */
\r
349 vTaskResume( xContinuousIncrementHandle );
\r
351 #if( configUSE_PREEMPTION == 0 )
\r
356 /*-----------------------------------------------------------*/
\r
358 static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )
\r
360 static uint32_t ulValueToSend = ( uint32_t ) 0;
\r
362 /* Just to stop warning messages. */
\r
363 ( void ) pvParameters;
\r
369 /* We must not block while the scheduler is suspended! */
\r
370 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
372 xSuspendedQueueSendError = pdTRUE;
\r
377 vTaskDelay( priSLEEP_TIME );
\r
382 /*-----------------------------------------------------------*/
\r
384 static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )
\r
386 uint32_t ulReceivedValue;
\r
387 BaseType_t xGotValue;
\r
389 /* Just to stop warning messages. */
\r
390 ( void ) pvParameters;
\r
396 /* Suspending the scheduler here is fairly pointless and
\r
397 undesirable for a normal application. It is done here purely
\r
398 to test the scheduler. The inner xTaskResumeAll() should
\r
399 never return pdTRUE as the scheduler is still locked by the
\r
405 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
407 if( xTaskResumeAll() != pdFALSE )
\r
409 xSuspendedQueueReceiveError = pdTRUE;
\r
414 #if configUSE_PREEMPTION == 0
\r
420 } while( xGotValue == pdFALSE );
\r
422 if( ulReceivedValue != ulExpectedValue )
\r
424 xSuspendedQueueReceiveError = pdTRUE;
\r
427 if( xSuspendedQueueReceiveError != pdTRUE )
\r
429 /* Only increment the variable if an error has not occurred. This
\r
430 allows xAreDynamicPriorityTasksStillRunning() to check for stalled
\r
431 tasks as well as explicit errors. */
\r
436 /*-----------------------------------------------------------*/
\r
438 /* Called to check that all the created tasks are still running without error. */
\r
439 BaseType_t xAreDynamicPriorityTasksStillRunning( void )
\r
441 /* Keep a history of the check variables so we know if it has been incremented
\r
442 since the last call. */
\r
443 static uint16_t usLastTaskCheck = ( uint16_t ) 0;
\r
444 static uint32_t ulLastExpectedValue = ( uint32_t ) 0U;
\r
445 BaseType_t xReturn = pdTRUE;
\r
447 /* Check the tasks are still running by ensuring the check variable
\r
448 is still incrementing. */
\r
450 if( usCheckVariable == usLastTaskCheck )
\r
452 /* The check has not incremented so an error exists. */
\r
456 if( ulExpectedValue == ulLastExpectedValue )
\r
458 /* The value being received by the queue receive task has not
\r
459 incremented so an error exists. */
\r
463 if( xSuspendedQueueSendError == pdTRUE )
\r
468 if( xSuspendedQueueReceiveError == pdTRUE )
\r
473 usLastTaskCheck = usCheckVariable;
\r
474 ulLastExpectedValue = ulExpectedValue;
\r