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