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
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 ( ( TickType_t ) 128 / portTICK_PERIOD_MS )
\r
111 #define priLOOPS ( 5 )
\r
112 #define priMAX_COUNT ( ( unsigned long ) 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 xContinousIncrementHandle, 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 unsigned long 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 unsigned short usCheckVariable = ( unsigned short ) 0;
\r
131 static volatile portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
\r
132 static volatile portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
\r
134 /* Queue used by the second test. */
\r
135 static QueueHandle_t xSuspendedTestQueue;
\r
137 /*-----------------------------------------------------------*/
\r
139 * Start the three tasks as described at the top of the file.
\r
140 * Note that the limited count task is given a higher priority.
\r
142 void vStartDynamicPriorityTasks( void )
\r
144 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
146 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
147 in use. The queue registry is provided as a means for kernel aware
\r
148 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
149 is not being used. The call to vQueueAddToRegistry() will be removed
\r
150 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
151 defined to be less than 1. */
\r
152 vQueueAddToRegistry( xSuspendedTestQueue, ( signed char * ) "Suspended_Test_Queue" );
\r
154 xTaskCreate( vContinuousIncrementTask, "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinousIncrementHandle );
\r
155 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
156 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
157 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
158 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
160 /*-----------------------------------------------------------*/
\r
163 * Just loops around incrementing the shared variable until the limit has been
\r
164 * reached. Once the limit has been reached it suspends itself.
\r
166 static portTASK_FUNCTION( vLimitedIncrementTask, pvParameters )
\r
168 unsigned long *pulCounter;
\r
170 /* Take a pointer to the shared variable from the parameters passed into
\r
172 pulCounter = ( unsigned long * ) pvParameters;
\r
174 /* This will run before the control task, so the first thing it does is
\r
175 suspend - the control task will resume it when ready. */
\r
176 vTaskSuspend( NULL );
\r
180 /* Just count up to a value then suspend. */
\r
183 if( *pulCounter >= priMAX_COUNT )
\r
185 vTaskSuspend( NULL );
\r
189 /*-----------------------------------------------------------*/
\r
192 * Just keep counting the shared variable up. The control task will suspend
\r
193 * this task when it wants.
\r
195 static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )
\r
197 unsigned long *pulCounter;
\r
198 unsigned portBASE_TYPE uxOurPriority;
\r
200 /* Take a pointer to the shared variable from the parameters passed into
\r
202 pulCounter = ( unsigned long * ) pvParameters;
\r
204 /* Query our priority so we can raise it when exclusive access to the
\r
205 shared variable is required. */
\r
206 uxOurPriority = uxTaskPriorityGet( NULL );
\r
210 /* Raise our priority above the controller task to ensure a context
\r
211 switch does not occur while we are accessing this variable. */
\r
212 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
214 vTaskPrioritySet( NULL, uxOurPriority );
\r
217 /*-----------------------------------------------------------*/
\r
220 * Controller task as described above.
\r
222 static portTASK_FUNCTION( vCounterControlTask, pvParameters )
\r
224 unsigned long ulLastCounter;
\r
226 short sError = pdFALSE;
\r
228 /* Just to stop warning messages. */
\r
229 ( void ) pvParameters;
\r
233 /* Start with the counter at zero. */
\r
234 ulCounter = ( unsigned long ) 0;
\r
236 /* First section : */
\r
238 /* Check the continuous count task is running. */
\r
239 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
241 /* Suspend the continuous count task so we can take a mirror of the
\r
242 shared variable without risk of corruption. */
\r
243 vTaskSuspend( xContinousIncrementHandle );
\r
244 ulLastCounter = ulCounter;
\r
245 vTaskResume( xContinousIncrementHandle );
\r
247 /* Now delay to ensure the other task has processor time. */
\r
248 vTaskDelay( priSLEEP_TIME );
\r
250 /* Check the shared variable again. This time to ensure mutual
\r
251 exclusion the whole scheduler will be locked. This is just for
\r
255 if( ulLastCounter == ulCounter )
\r
257 /* The shared variable has not changed. There is a problem
\r
258 with the continuous count task so flag an error. */
\r
266 /* Second section: */
\r
268 /* Suspend the continuous counter task so it stops accessing the shared variable. */
\r
269 vTaskSuspend( xContinousIncrementHandle );
\r
271 /* Reset the variable. */
\r
272 ulCounter = ( unsigned long ) 0;
\r
274 /* Resume the limited count task which has a higher priority than us.
\r
275 We should therefore not return from this call until the limited count
\r
276 task has suspended itself with a known value in the counter variable. */
\r
277 vTaskResume( xLimitedIncrementHandle );
\r
279 /* Does the counter variable have the expected value? */
\r
280 if( ulCounter != priMAX_COUNT )
\r
285 if( sError == pdFALSE )
\r
287 /* If no errors have occurred then increment the check variable. */
\r
288 portENTER_CRITICAL();
\r
290 portEXIT_CRITICAL();
\r
293 /* Resume the continuous count task and do it all again. */
\r
294 vTaskResume( xContinousIncrementHandle );
\r
297 /*-----------------------------------------------------------*/
\r
299 static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )
\r
301 static unsigned long ulValueToSend = ( unsigned long ) 0;
\r
303 /* Just to stop warning messages. */
\r
304 ( void ) pvParameters;
\r
310 /* We must not block while the scheduler is suspended! */
\r
311 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
313 xSuspendedQueueSendError = pdTRUE;
\r
318 vTaskDelay( priSLEEP_TIME );
\r
323 /*-----------------------------------------------------------*/
\r
325 static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )
\r
327 static unsigned long ulExpectedValue = ( unsigned long ) 0, ulReceivedValue;
\r
328 portBASE_TYPE xGotValue;
\r
330 /* Just to stop warning messages. */
\r
331 ( void ) pvParameters;
\r
337 /* Suspending the scheduler here is fairly pointless and
\r
338 undesirable for a normal application. It is done here purely
\r
339 to test the scheduler. The inner xTaskResumeAll() should
\r
340 never return pdTRUE as the scheduler is still locked by the
\r
346 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
348 if( xTaskResumeAll() )
\r
350 xSuspendedQueueReceiveError = pdTRUE;
\r
355 #if configUSE_PREEMPTION == 0
\r
361 } while( xGotValue == pdFALSE );
\r
363 if( ulReceivedValue != ulExpectedValue )
\r
365 xSuspendedQueueReceiveError = pdTRUE;
\r
371 /*-----------------------------------------------------------*/
\r
373 /* Called to check that all the created tasks are still running without error. */
\r
374 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
\r
376 /* Keep a history of the check variables so we know if it has been incremented
\r
377 since the last call. */
\r
378 static unsigned short usLastTaskCheck = ( unsigned short ) 0;
\r
379 portBASE_TYPE xReturn = pdTRUE;
\r
381 /* Check the tasks are still running by ensuring the check variable
\r
382 is still incrementing. */
\r
384 if( usCheckVariable == usLastTaskCheck )
\r
386 /* The check has not incremented so an error exists. */
\r
390 if( xSuspendedQueueSendError == pdTRUE )
\r
395 if( xSuspendedQueueReceiveError == pdTRUE )
\r
400 usLastTaskCheck = usCheckVariable;
\r