2 FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 * The first test creates three tasks - two counter tasks (one continuous count
\r
69 * and one limited count) and one controller. A "count" variable is shared
\r
70 * between all three tasks. The two counter tasks should never be in a "ready"
\r
71 * state at the same time. The controller task runs at the same priority as
\r
72 * the continuous count task, and at a lower priority than the limited count
\r
75 * One counter task loops indefinitely, incrementing the shared count variable
\r
76 * on each iteration. To ensure it has exclusive access to the variable it
\r
77 * raises it's priority above that of the controller task before each
\r
78 * increment, lowering it again to it's original priority before starting the
\r
81 * The other counter task increments the shared count variable on each
\r
82 * iteration of it's loop until the count has reached a limit of 0xff - at
\r
83 * which point it suspends itself. It will not start a new loop until the
\r
84 * controller task has made it "ready" again by calling vTaskResume ().
\r
85 * This second counter task operates at a higher priority than controller
\r
86 * task so does not need to worry about mutual exclusion of the counter
\r
89 * The controller task is in two sections. The first section controls and
\r
90 * monitors the continuous count task. When this section is operational the
\r
91 * limited count task is suspended. Likewise, the second section controls
\r
92 * and monitors the limited count task. When this section is operational the
\r
93 * continuous count task is suspended.
\r
95 * In the first section the controller task first takes a copy of the shared
\r
96 * count variable. To ensure mutual exclusion on the count variable it
\r
97 * suspends the continuous count task, resuming it again when the copy has been
\r
98 * taken. The controller task then sleeps for a fixed period - during which
\r
99 * the continuous count task will execute and increment the shared variable.
\r
100 * When the controller task wakes it checks that the continuous count task
\r
101 * has executed by comparing the copy of the shared variable with its current
\r
102 * value. This time, to ensure mutual exclusion, the scheduler itself is
\r
103 * suspended with a call to vTaskSuspendAll (). This is for demonstration
\r
104 * purposes only and is not a recommended technique due to its inefficiency.
\r
106 * After a fixed number of iterations the controller task suspends the
\r
107 * continuous count task, and moves on to its second section.
\r
109 * At the start of the second section the shared variable is cleared to zero.
\r
110 * The limited count task is then woken from it's suspension by a call to
\r
111 * vTaskResume (). As this counter task operates at a higher priority than
\r
112 * the controller task the controller task should not run again until the
\r
113 * shared variable has been counted up to the limited value causing the counter
\r
114 * task to suspend itself. The next line after vTaskResume () is therefore
\r
115 * a check on the shared variable to ensure everything is as expected.
\r
118 * The second test consists of a couple of very simple tasks that post onto a
\r
119 * queue while the scheduler is suspended. This test was added to test parts
\r
120 * of the scheduler not exercised by the first test.
\r
124 #include <stdlib.h>
\r
126 /* Scheduler include files. */
\r
127 #include "FreeRTOS.h"
\r
129 #include "semphr.h"
\r
131 /* Demo app include files. */
\r
132 #include "dynamic.h"
\r
134 /* Function that implements the "limited count" task as described above. */
\r
135 static portTASK_FUNCTION_PROTO( vLimitedIncrementTask, pvParameters );
\r
137 /* Function that implements the "continuous count" task as described above. */
\r
138 static portTASK_FUNCTION_PROTO( vContinuousIncrementTask, pvParameters );
\r
140 /* Function that implements the controller task as described above. */
\r
141 static portTASK_FUNCTION_PROTO( vCounterControlTask, pvParameters );
\r
143 static portTASK_FUNCTION_PROTO( vQueueReceiveWhenSuspendedTask, pvParameters );
\r
144 static portTASK_FUNCTION_PROTO( vQueueSendWhenSuspendedTask, pvParameters );
\r
146 /* Demo task specific constants. */
\r
147 #define priSTACK_SIZE ( configMINIMAL_STACK_SIZE )
\r
148 #define priSLEEP_TIME ( ( portTickType ) 128 / portTICK_RATE_MS )
\r
149 #define priLOOPS ( 5 )
\r
150 #define priMAX_COUNT ( ( unsigned long ) 0xff )
\r
151 #define priNO_BLOCK ( ( portTickType ) 0 )
\r
152 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
154 /*-----------------------------------------------------------*/
\r
156 /* Handles to the two counter tasks. These could be passed in as parameters
\r
157 to the controller task to prevent them having to be file scope. */
\r
158 static xTaskHandle xContinousIncrementHandle, xLimitedIncrementHandle;
\r
160 /* The shared counter variable. This is passed in as a parameter to the two
\r
161 counter variables for demonstration purposes. */
\r
162 static unsigned long ulCounter;
\r
164 /* Variables used to check that the tasks are still operating without error.
\r
165 Each complete iteration of the controller task increments this variable
\r
166 provided no errors have been found. The variable maintaining the same value
\r
167 is therefore indication of an error. */
\r
168 static volatile unsigned short usCheckVariable = ( unsigned short ) 0;
\r
169 static volatile portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
\r
170 static volatile portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
\r
172 /* Queue used by the second test. */
\r
173 xQueueHandle xSuspendedTestQueue;
\r
175 /*-----------------------------------------------------------*/
\r
177 * Start the three 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
184 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
185 in use. The queue registry is provided as a means for kernel aware
\r
186 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
187 is not being used. The call to vQueueAddToRegistry() will be removed
\r
188 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
189 defined to be less than 1. */
\r
190 vQueueAddToRegistry( xSuspendedTestQueue, ( signed char * ) "Suspended_Test_Queue" );
\r
192 xTaskCreate( vContinuousIncrementTask, ( signed char * ) "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinousIncrementHandle );
\r
193 xTaskCreate( vLimitedIncrementTask, ( signed char * ) "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
194 xTaskCreate( vCounterControlTask, ( signed char * ) "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
195 xTaskCreate( vQueueSendWhenSuspendedTask, ( signed char * ) "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
196 xTaskCreate( vQueueReceiveWhenSuspendedTask, ( signed char * ) "SUSP_RX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
198 /*-----------------------------------------------------------*/
\r
201 * Just loops around incrementing the shared variable until the limit has been
\r
202 * reached. Once the limit has been reached it suspends itself.
\r
204 static portTASK_FUNCTION( vLimitedIncrementTask, pvParameters )
\r
206 unsigned long *pulCounter;
\r
208 /* Take a pointer to the shared variable from the parameters passed into
\r
210 pulCounter = ( unsigned long * ) pvParameters;
\r
212 /* This will run before the control task, so the first thing it does is
\r
213 suspend - the control task will resume it when ready. */
\r
214 vTaskSuspend( NULL );
\r
218 /* Just count up to a value then suspend. */
\r
219 ( *pulCounter )++;
\r
221 if( *pulCounter >= priMAX_COUNT )
\r
223 vTaskSuspend( NULL );
\r
227 /*-----------------------------------------------------------*/
\r
230 * Just keep counting the shared variable up. The control task will suspend
\r
231 * this task when it wants.
\r
233 static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )
\r
235 unsigned long *pulCounter;
\r
236 unsigned portBASE_TYPE uxOurPriority;
\r
238 /* Take a pointer to the shared variable from the parameters passed into
\r
240 pulCounter = ( unsigned long * ) pvParameters;
\r
242 /* Query our priority so we can raise it when exclusive access to the
\r
243 shared variable is required. */
\r
244 uxOurPriority = uxTaskPriorityGet( NULL );
\r
248 /* Raise our priority above the controller task to ensure a context
\r
249 switch does not occur while we are accessing this variable. */
\r
250 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
251 ( *pulCounter )++;
\r
252 vTaskPrioritySet( NULL, uxOurPriority );
\r
255 /*-----------------------------------------------------------*/
\r
258 * Controller task as described above.
\r
260 static portTASK_FUNCTION( vCounterControlTask, pvParameters )
\r
262 unsigned long ulLastCounter;
\r
264 short sError = pdFALSE;
\r
266 /* Just to stop warning messages. */
\r
267 ( void ) pvParameters;
\r
271 /* Start with the counter at zero. */
\r
272 ulCounter = ( unsigned long ) 0;
\r
274 /* First section : */
\r
276 /* Check the continuous count task is running. */
\r
277 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
279 /* Suspend the continuous count task so we can take a mirror of the
\r
280 shared variable without risk of corruption. */
\r
281 vTaskSuspend( xContinousIncrementHandle );
\r
282 ulLastCounter = ulCounter;
\r
283 vTaskResume( xContinousIncrementHandle );
\r
285 /* Now delay to ensure the other task has processor time. */
\r
286 vTaskDelay( priSLEEP_TIME );
\r
288 /* Check the shared variable again. This time to ensure mutual
\r
289 exclusion the whole scheduler will be locked. This is just for
\r
293 if( ulLastCounter == ulCounter )
\r
295 /* The shared variable has not changed. There is a problem
\r
296 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 variable. */
\r
307 vTaskSuspend( xContinousIncrementHandle );
\r
309 /* Reset the variable. */
\r
310 ulCounter = ( unsigned long ) 0;
\r
312 /* Resume the limited count task which has a higher priority than us.
\r
313 We should therefore not return from this call until the limited count
\r
314 task has suspended itself with a known value in the counter variable. */
\r
315 vTaskResume( xLimitedIncrementHandle );
\r
317 /* Does the counter variable have the expected value? */
\r
318 if( ulCounter != priMAX_COUNT )
\r
323 if( sError == pdFALSE )
\r
325 /* If no errors have occurred then increment the check variable. */
\r
326 portENTER_CRITICAL();
\r
328 portEXIT_CRITICAL();
\r
331 /* Resume the continuous count task and do it all again. */
\r
332 vTaskResume( xContinousIncrementHandle );
\r
335 /*-----------------------------------------------------------*/
\r
337 static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )
\r
339 static unsigned long ulValueToSend = ( unsigned long ) 0;
\r
341 /* Just to stop warning messages. */
\r
342 ( void ) pvParameters;
\r
348 /* We must not block while the scheduler is suspended! */
\r
349 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
351 xSuspendedQueueSendError = pdTRUE;
\r
356 vTaskDelay( priSLEEP_TIME );
\r
361 /*-----------------------------------------------------------*/
\r
363 static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )
\r
365 static unsigned long ulExpectedValue = ( unsigned long ) 0, ulReceivedValue;
\r
366 portBASE_TYPE xGotValue;
\r
368 /* Just to stop warning messages. */
\r
369 ( void ) pvParameters;
\r
375 /* Suspending the scheduler here is fairly pointless and
\r
376 undesirable for a normal application. It is done here purely
\r
377 to test the scheduler. The inner xTaskResumeAll() should
\r
378 never return pdTRUE as the scheduler is still locked by the
\r
384 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
386 if( xTaskResumeAll() )
\r
388 xSuspendedQueueReceiveError = pdTRUE;
\r
393 #if configUSE_PREEMPTION == 0
\r
399 } while( xGotValue == pdFALSE );
\r
401 if( ulReceivedValue != ulExpectedValue )
\r
403 xSuspendedQueueReceiveError = pdTRUE;
\r
409 /*-----------------------------------------------------------*/
\r
411 /* Called to check that all the created tasks are still running without error. */
\r
412 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
\r
414 /* Keep a history of the check variables so we know if it has been incremented
\r
415 since the last call. */
\r
416 static unsigned short usLastTaskCheck = ( unsigned short ) 0;
\r
417 portBASE_TYPE xReturn = pdTRUE;
\r
419 /* Check the tasks are still running by ensuring the check variable
\r
420 is still incrementing. */
\r
422 if( usCheckVariable == usLastTaskCheck )
\r
424 /* The check has not incremented so an error exists. */
\r
428 if( xSuspendedQueueSendError == pdTRUE )
\r
433 if( xSuspendedQueueReceiveError == pdTRUE )
\r
438 usLastTaskCheck = usCheckVariable;
\r