2 FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry.
4 This file is part of the FreeRTOS.org distribution.
6 FreeRTOS.org is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 FreeRTOS.org is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with FreeRTOS.org; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 A special exception to the GPL can be applied should you wish to distribute
21 a combined work that includes FreeRTOS.org, without being obliged to provide
22 the source code for any proprietary components. See the licensing section
23 of http://www.FreeRTOS.org for full details of how and when the exception
26 ***************************************************************************
27 ***************************************************************************
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
30 * and even write all or part of your application on your behalf. *
31 * See http://www.OpenRTOS.com for details of the services we provide to *
32 * expedite your project. *
34 ***************************************************************************
35 ***************************************************************************
37 Please ensure to read the configuration and relevant port sections of the
40 http://www.FreeRTOS.org - Documentation, latest information, license and
43 http://www.SafeRTOS.com - A version that is certified for use in safety
46 http://www.OpenRTOS.com - Commercial support, development, porting,
47 licensing and training services.
51 * The first test creates three tasks - two counter tasks (one continuous count
52 * and one limited count) and one controller. A "count" variable is shared
53 * between all three tasks. The two counter tasks should never be in a "ready"
54 * state at the same time. The controller task runs at the same priority as
55 * the continuous count task, and at a lower priority than the limited count
58 * One counter task loops indefinitely, incrementing the shared count variable
59 * on each iteration. To ensure it has exclusive access to the variable it
60 * raises it's priority above that of the controller task before each
61 * increment, lowering it again to it's original priority before starting the
64 * The other counter task increments the shared count variable on each
65 * iteration of it's loop until the count has reached a limit of 0xff - at
66 * which point it suspends itself. It will not start a new loop until the
67 * controller task has made it "ready" again by calling vTaskResume ().
68 * This second counter task operates at a higher priority than controller
69 * task so does not need to worry about mutual exclusion of the counter
72 * The controller task is in two sections. The first section controls and
73 * monitors the continuous count task. When this section is operational the
74 * limited count task is suspended. Likewise, the second section controls
75 * and monitors the limited count task. When this section is operational the
76 * continuous count task is suspended.
78 * In the first section the controller task first takes a copy of the shared
79 * count variable. To ensure mutual exclusion on the count variable it
80 * suspends the continuous count task, resuming it again when the copy has been
81 * taken. The controller task then sleeps for a fixed period - during which
82 * the continuous count task will execute and increment the shared variable.
83 * When the controller task wakes it checks that the continuous count task
84 * has executed by comparing the copy of the shared variable with its current
85 * value. This time, to ensure mutual exclusion, the scheduler itself is
86 * suspended with a call to vTaskSuspendAll (). This is for demonstration
87 * purposes only and is not a recommended technique due to its inefficiency.
89 * After a fixed number of iterations the controller task suspends the
90 * continuous count task, and moves on to its second section.
92 * At the start of the second section the shared variable is cleared to zero.
93 * The limited count task is then woken from it's suspension by a call to
94 * vTaskResume (). As this counter task operates at a higher priority than
95 * the controller task the controller task should not run again until the
96 * shared variable has been counted up to the limited value causing the counter
97 * task to suspend itself. The next line after vTaskResume () is therefore
98 * a check on the shared variable to ensure everything is as expected.
101 * The second test consists of a couple of very simple tasks that post onto a
102 * queue while the scheduler is suspended. This test was added to test parts
103 * of the scheduler not exercised by the first test.
109 /* Scheduler include files. */
110 #include "FreeRTOS.h"
114 /* Demo app include files. */
117 /* Function that implements the "limited count" task as described above. */
118 static portTASK_FUNCTION_PROTO( vLimitedIncrementTask, pvParameters );
120 /* Function that implements the "continuous count" task as described above. */
121 static portTASK_FUNCTION_PROTO( vContinuousIncrementTask, pvParameters );
123 /* Function that implements the controller task as described above. */
124 static portTASK_FUNCTION_PROTO( vCounterControlTask, pvParameters );
126 static portTASK_FUNCTION_PROTO( vQueueReceiveWhenSuspendedTask, pvParameters );
127 static portTASK_FUNCTION_PROTO( vQueueSendWhenSuspendedTask, pvParameters );
129 /* Demo task specific constants. */
130 #define priSTACK_SIZE ( configMINIMAL_STACK_SIZE )
131 #define priSLEEP_TIME ( ( portTickType ) 128 / portTICK_RATE_MS )
132 #define priLOOPS ( 5 )
133 #define priMAX_COUNT ( ( unsigned portLONG ) 0xff )
134 #define priNO_BLOCK ( ( portTickType ) 0 )
135 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
137 /*-----------------------------------------------------------*/
139 /* Handles to the two counter tasks. These could be passed in as parameters
140 to the controller task to prevent them having to be file scope. */
141 static xTaskHandle xContinousIncrementHandle, xLimitedIncrementHandle;
143 /* The shared counter variable. This is passed in as a parameter to the two
144 counter variables for demonstration purposes. */
145 static unsigned portLONG ulCounter;
147 /* Variables used to check that the tasks are still operating without error.
148 Each complete iteration of the controller task increments this variable
149 provided no errors have been found. The variable maintaining the same value
150 is therefore indication of an error. */
151 static volatile unsigned portSHORT usCheckVariable = ( unsigned portSHORT ) 0;
152 static volatile portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
153 static volatile portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
155 /* Queue used by the second test. */
156 xQueueHandle xSuspendedTestQueue;
158 /*-----------------------------------------------------------*/
160 * Start the three tasks as described at the top of the file.
161 * Note that the limited count task is given a higher priority.
163 void vStartDynamicPriorityTasks( void )
165 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned portLONG ) );
166 xTaskCreate( vContinuousIncrementTask, ( signed portCHAR * ) "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinousIncrementHandle );
167 xTaskCreate( vLimitedIncrementTask, ( signed portCHAR * ) "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
168 xTaskCreate( vCounterControlTask, ( signed portCHAR * ) "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
169 xTaskCreate( vQueueSendWhenSuspendedTask, ( signed portCHAR * ) "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
170 xTaskCreate( vQueueReceiveWhenSuspendedTask, ( signed portCHAR * ) "SUSP_RX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
172 /*-----------------------------------------------------------*/
175 * Just loops around incrementing the shared variable until the limit has been
176 * reached. Once the limit has been reached it suspends itself.
178 static portTASK_FUNCTION( vLimitedIncrementTask, pvParameters )
180 unsigned portLONG *pulCounter;
182 /* Take a pointer to the shared variable from the parameters passed into
184 pulCounter = ( unsigned portLONG * ) pvParameters;
186 /* This will run before the control task, so the first thing it does is
187 suspend - the control task will resume it when ready. */
188 vTaskSuspend( NULL );
192 /* Just count up to a value then suspend. */
195 if( *pulCounter >= priMAX_COUNT )
197 vTaskSuspend( NULL );
201 /*-----------------------------------------------------------*/
204 * Just keep counting the shared variable up. The control task will suspend
205 * this task when it wants.
207 static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )
209 unsigned portLONG *pulCounter;
210 unsigned portBASE_TYPE uxOurPriority;
212 /* Take a pointer to the shared variable from the parameters passed into
214 pulCounter = ( unsigned portLONG * ) pvParameters;
216 /* Query our priority so we can raise it when exclusive access to the
217 shared variable is required. */
218 uxOurPriority = uxTaskPriorityGet( NULL );
222 /* Raise our priority above the controller task to ensure a context
223 switch does not occur while we are accessing this variable. */
224 vTaskPrioritySet( NULL, uxOurPriority + 1 );
226 vTaskPrioritySet( NULL, uxOurPriority );
229 /*-----------------------------------------------------------*/
232 * Controller task as described above.
234 static portTASK_FUNCTION( vCounterControlTask, pvParameters )
236 unsigned portLONG ulLastCounter;
238 portSHORT sError = pdFALSE;
240 /* Just to stop warning messages. */
241 ( void ) pvParameters;
245 /* Start with the counter at zero. */
246 ulCounter = ( unsigned portLONG ) 0;
248 /* First section : */
250 /* Check the continuous count task is running. */
251 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
253 /* Suspend the continuous count task so we can take a mirror of the
254 shared variable without risk of corruption. */
255 vTaskSuspend( xContinousIncrementHandle );
256 ulLastCounter = ulCounter;
257 vTaskResume( xContinousIncrementHandle );
259 /* Now delay to ensure the other task has processor time. */
260 vTaskDelay( priSLEEP_TIME );
262 /* Check the shared variable again. This time to ensure mutual
263 exclusion the whole scheduler will be locked. This is just for
267 if( ulLastCounter == ulCounter )
269 /* The shared variable has not changed. There is a problem
270 with the continuous count task so flag an error. */
278 /* Second section: */
280 /* Suspend the continuous counter task so it stops accessing the shared variable. */
281 vTaskSuspend( xContinousIncrementHandle );
283 /* Reset the variable. */
284 ulCounter = ( unsigned portLONG ) 0;
286 /* Resume the limited count task which has a higher priority than us.
287 We should therefore not return from this call until the limited count
288 task has suspended itself with a known value in the counter variable. */
289 vTaskResume( xLimitedIncrementHandle );
291 /* Does the counter variable have the expected value? */
292 if( ulCounter != priMAX_COUNT )
297 if( sError == pdFALSE )
299 /* If no errors have occurred then increment the check variable. */
300 portENTER_CRITICAL();
305 /* Resume the continuous count task and do it all again. */
306 vTaskResume( xContinousIncrementHandle );
309 /*-----------------------------------------------------------*/
311 static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )
313 static unsigned portLONG ulValueToSend = ( unsigned portLONG ) 0;
315 /* Just to stop warning messages. */
316 ( void ) pvParameters;
322 /* We must not block while the scheduler is suspended! */
323 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
325 xSuspendedQueueSendError = pdTRUE;
330 vTaskDelay( priSLEEP_TIME );
335 /*-----------------------------------------------------------*/
337 static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )
339 static unsigned portLONG ulExpectedValue = ( unsigned portLONG ) 0, ulReceivedValue;
340 portBASE_TYPE xGotValue;
342 /* Just to stop warning messages. */
343 ( void ) pvParameters;
349 /* Suspending the scheduler here is fairly pointless and
350 undesirable for a normal application. It is done here purely
351 to test the scheduler. The inner xTaskResumeAll() should
352 never return pdTRUE as the scheduler is still locked by the
358 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
360 if( xTaskResumeAll() )
362 xSuspendedQueueReceiveError = pdTRUE;
367 #if configUSE_PREEMPTION == 0
373 } while( xGotValue == pdFALSE );
375 if( ulReceivedValue != ulExpectedValue )
377 xSuspendedQueueReceiveError = pdTRUE;
383 /*-----------------------------------------------------------*/
385 /* Called to check that all the created tasks are still running without error. */
386 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
388 /* Keep a history of the check variables so we know if it has been incremented
389 since the last call. */
390 static unsigned portSHORT usLastTaskCheck = ( unsigned portSHORT ) 0;
391 portBASE_TYPE xReturn = pdTRUE;
393 /* Check the tasks are still running by ensuring the check variable
394 is still incrementing. */
396 if( usCheckVariable == usLastTaskCheck )
398 /* The check has not incremented so an error exists. */
402 if( xSuspendedQueueSendError == pdTRUE )
407 if( xSuspendedQueueReceiveError == pdTRUE )
412 usLastTaskCheck = usCheckVariable;