2 FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 * The first test creates three tasks - two counter tasks (one continuous count
\r
77 * and one limited count) and one controller. A "count" variable is shared
\r
78 * between all three tasks. The two counter tasks should never be in a "ready"
\r
79 * state at the same time. The controller task runs at the same priority as
\r
80 * the continuous count task, and at a lower priority than the limited count
\r
83 * One counter task loops indefinitely, incrementing the shared count variable
\r
84 * on each iteration. To ensure it has exclusive access to the variable it
\r
85 * raises it's priority above that of the controller task before each
\r
86 * increment, lowering it again to it's original priority before starting the
\r
89 * The other counter task increments the shared count variable on each
\r
90 * iteration of it's loop until the count has reached a limit of 0xff - at
\r
91 * which point it suspends itself. It will not start a new loop until the
\r
92 * controller task has made it "ready" again by calling vTaskResume ().
\r
93 * This second counter task operates at a higher priority than controller
\r
94 * task so does not need to worry about mutual exclusion of the counter
\r
97 * The controller task is in two sections. The first section controls and
\r
98 * monitors the continuous count task. When this section is operational the
\r
99 * limited count task is suspended. Likewise, the second section controls
\r
100 * and monitors the limited count task. When this section is operational the
\r
101 * continuous count task is suspended.
\r
103 * In the first section the controller task first takes a copy of the shared
\r
104 * count variable. To ensure mutual exclusion on the count variable it
\r
105 * suspends the continuous count task, resuming it again when the copy has been
\r
106 * taken. The controller task then sleeps for a fixed period - during which
\r
107 * the continuous count task will execute and increment the shared variable.
\r
108 * When the controller task wakes it checks that the continuous count task
\r
109 * has executed by comparing the copy of the shared variable with its current
\r
110 * value. This time, to ensure mutual exclusion, the scheduler itself is
\r
111 * suspended with a call to vTaskSuspendAll (). This is for demonstration
\r
112 * purposes only and is not a recommended technique due to its inefficiency.
\r
114 * After a fixed number of iterations the controller task suspends the
\r
115 * continuous count task, and moves on to its second section.
\r
117 * At the start of the second section the shared variable is cleared to zero.
\r
118 * The limited count task is then woken from it's suspension by a call to
\r
119 * vTaskResume (). As this counter task operates at a higher priority than
\r
120 * the controller task the controller task should not run again until the
\r
121 * shared variable has been counted up to the limited value causing the counter
\r
122 * task to suspend itself. The next line after vTaskResume () is therefore
\r
123 * a check on the shared variable to ensure everything is as expected.
\r
126 * The second test consists of a couple of very simple tasks that post onto a
\r
127 * queue while the scheduler is suspended. This test was added to test parts
\r
128 * of the scheduler not exercised by the first test.
\r
132 #include <stdlib.h>
\r
134 /* Scheduler include files. */
\r
135 #include "FreeRTOS.h"
\r
137 #include "semphr.h"
\r
139 /* Demo app include files. */
\r
140 #include "dynamic.h"
\r
142 /* Function that implements the "limited count" task as described above. */
\r
143 static portTASK_FUNCTION_PROTO( vLimitedIncrementTask, pvParameters );
\r
145 /* Function that implements the "continuous count" task as described above. */
\r
146 static portTASK_FUNCTION_PROTO( vContinuousIncrementTask, pvParameters );
\r
148 /* Function that implements the controller task as described above. */
\r
149 static portTASK_FUNCTION_PROTO( vCounterControlTask, pvParameters );
\r
151 static portTASK_FUNCTION_PROTO( vQueueReceiveWhenSuspendedTask, pvParameters );
\r
152 static portTASK_FUNCTION_PROTO( vQueueSendWhenSuspendedTask, pvParameters );
\r
154 /* Demo task specific constants. */
\r
155 #define priSTACK_SIZE ( configMINIMAL_STACK_SIZE )
\r
156 #define priSLEEP_TIME ( ( portTickType ) 128 / portTICK_RATE_MS )
\r
157 #define priLOOPS ( 5 )
\r
158 #define priMAX_COUNT ( ( unsigned long ) 0xff )
\r
159 #define priNO_BLOCK ( ( portTickType ) 0 )
\r
160 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
162 /*-----------------------------------------------------------*/
\r
164 /* Handles to the two counter tasks. These could be passed in as parameters
\r
165 to the controller task to prevent them having to be file scope. */
\r
166 static xTaskHandle xContinousIncrementHandle, xLimitedIncrementHandle;
\r
168 /* The shared counter variable. This is passed in as a parameter to the two
\r
169 counter variables for demonstration purposes. */
\r
170 static unsigned long ulCounter;
\r
172 /* Variables used to check that the tasks are still operating without error.
\r
173 Each complete iteration of the controller task increments this variable
\r
174 provided no errors have been found. The variable maintaining the same value
\r
175 is therefore indication of an error. */
\r
176 static volatile unsigned short usCheckVariable = ( unsigned short ) 0;
\r
177 static volatile portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
\r
178 static volatile portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
\r
180 /* Queue used by the second test. */
\r
181 xQueueHandle xSuspendedTestQueue;
\r
183 /* The value the queue receive task expects to receive next. This is file
\r
184 scope so xAreDynamicPriorityTasksStillRunning() can ensure it is still
\r
186 static unsigned long ulExpectedValue = ( unsigned long ) 0;
\r
188 /*-----------------------------------------------------------*/
\r
190 * Start the three tasks as described at the top of the file.
\r
191 * Note that the limited count task is given a higher priority.
\r
193 void vStartDynamicPriorityTasks( void )
\r
195 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
197 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
198 in use. The queue registry is provided as a means for kernel aware
\r
199 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
200 is not being used. The call to vQueueAddToRegistry() will be removed
\r
201 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
202 defined to be less than 1. */
\r
203 vQueueAddToRegistry( xSuspendedTestQueue, ( signed char * ) "Suspended_Test_Queue" );
\r
205 xTaskCreate( vContinuousIncrementTask, ( signed char * ) "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinousIncrementHandle );
\r
206 xTaskCreate( vLimitedIncrementTask, ( signed char * ) "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
207 xTaskCreate( vCounterControlTask, ( signed char * ) "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
208 xTaskCreate( vQueueSendWhenSuspendedTask, ( signed char * ) "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
209 xTaskCreate( vQueueReceiveWhenSuspendedTask, ( signed char * ) "SUSP_RX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
211 /*-----------------------------------------------------------*/
\r
214 * Just loops around incrementing the shared variable until the limit has been
\r
215 * reached. Once the limit has been reached it suspends itself.
\r
217 static portTASK_FUNCTION( vLimitedIncrementTask, pvParameters )
\r
219 unsigned long *pulCounter;
\r
221 /* Take a pointer to the shared variable from the parameters passed into
\r
223 pulCounter = ( unsigned long * ) pvParameters;
\r
225 /* This will run before the control task, so the first thing it does is
\r
226 suspend - the control task will resume it when ready. */
\r
227 vTaskSuspend( NULL );
\r
231 /* Just count up to a value then suspend. */
\r
234 if( *pulCounter >= priMAX_COUNT )
\r
236 vTaskSuspend( NULL );
\r
240 /*-----------------------------------------------------------*/
\r
243 * Just keep counting the shared variable up. The control task will suspend
\r
244 * this task when it wants.
\r
246 static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )
\r
248 unsigned long *pulCounter;
\r
249 unsigned portBASE_TYPE uxOurPriority;
\r
251 /* Take a pointer to the shared variable from the parameters passed into
\r
253 pulCounter = ( unsigned long * ) pvParameters;
\r
255 /* Query our priority so we can raise it when exclusive access to the
\r
256 shared variable is required. */
\r
257 uxOurPriority = uxTaskPriorityGet( NULL );
\r
261 /* Raise our priority above the controller task to ensure a context
\r
262 switch does not occur while we are accessing this variable. */
\r
263 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
265 vTaskPrioritySet( NULL, uxOurPriority );
\r
268 /*-----------------------------------------------------------*/
\r
271 * Controller task as described above.
\r
273 static portTASK_FUNCTION( vCounterControlTask, pvParameters )
\r
275 unsigned long ulLastCounter;
\r
277 short sError = pdFALSE;
\r
279 /* Just to stop warning messages. */
\r
280 ( void ) pvParameters;
\r
284 /* Start with the counter at zero. */
\r
285 ulCounter = ( unsigned long ) 0;
\r
287 /* First section : */
\r
289 /* Check the continuous count task is running. */
\r
290 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
292 /* Suspend the continuous count task so we can take a mirror of the
\r
293 shared variable without risk of corruption. */
\r
294 vTaskSuspend( xContinousIncrementHandle );
\r
295 ulLastCounter = ulCounter;
\r
296 vTaskResume( xContinousIncrementHandle );
\r
298 /* Now delay to ensure the other task has processor time. */
\r
299 vTaskDelay( priSLEEP_TIME );
\r
301 /* Check the shared variable again. This time to ensure mutual
\r
302 exclusion the whole scheduler will be locked. This is just for
\r
306 if( ulLastCounter == ulCounter )
\r
308 /* The shared variable has not changed. There is a problem
\r
309 with the continuous count task so flag an error. */
\r
317 /* Second section: */
\r
319 /* Suspend the continuous counter task so it stops accessing the shared variable. */
\r
320 vTaskSuspend( xContinousIncrementHandle );
\r
322 /* Reset the variable. */
\r
323 ulCounter = ( unsigned long ) 0;
\r
325 /* Resume the limited count task which has a higher priority than us.
\r
326 We should therefore not return from this call until the limited count
\r
327 task has suspended itself with a known value in the counter variable. */
\r
328 vTaskResume( xLimitedIncrementHandle );
\r
330 /* Does the counter variable have the expected value? */
\r
331 if( ulCounter != priMAX_COUNT )
\r
336 if( sError == pdFALSE )
\r
338 /* If no errors have occurred then increment the check variable. */
\r
339 portENTER_CRITICAL();
\r
341 portEXIT_CRITICAL();
\r
344 /* Resume the continuous count task and do it all again. */
\r
345 vTaskResume( xContinousIncrementHandle );
\r
348 /*-----------------------------------------------------------*/
\r
350 static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )
\r
352 static unsigned long ulValueToSend = ( unsigned long ) 0;
\r
354 /* Just to stop warning messages. */
\r
355 ( void ) pvParameters;
\r
361 /* We must not block while the scheduler is suspended! */
\r
362 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
364 xSuspendedQueueSendError = pdTRUE;
\r
369 vTaskDelay( priSLEEP_TIME );
\r
374 /*-----------------------------------------------------------*/
\r
376 static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )
\r
378 unsigned long ulReceivedValue;
\r
379 portBASE_TYPE xGotValue;
\r
381 /* Just to stop warning messages. */
\r
382 ( void ) pvParameters;
\r
388 /* Suspending the scheduler here is fairly pointless and
\r
389 undesirable for a normal application. It is done here purely
\r
390 to test the scheduler. The inner xTaskResumeAll() should
\r
391 never return pdTRUE as the scheduler is still locked by the
\r
397 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
399 if( xTaskResumeAll() != pdFALSE )
\r
401 xSuspendedQueueReceiveError = pdTRUE;
\r
406 #if configUSE_PREEMPTION == 0
\r
412 } while( xGotValue == pdFALSE );
\r
414 if( ulReceivedValue != ulExpectedValue )
\r
416 xSuspendedQueueReceiveError = pdTRUE;
\r
419 if( xSuspendedQueueReceiveError != pdTRUE )
\r
421 /* Only increment the variable if an error has not occurred. This
\r
422 allows xAreDynamicPriorityTasksStillRunning() to check for stalled
\r
423 tasks as well as explicit errors. */
\r
428 /*-----------------------------------------------------------*/
\r
430 /* Called to check that all the created tasks are still running without error. */
\r
431 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
\r
433 /* Keep a history of the check variables so we know if it has been incremented
\r
434 since the last call. */
\r
435 static unsigned short usLastTaskCheck = ( unsigned short ) 0;
\r
436 static unsigned long ulLastExpectedValue = ( unsigned long ) 0U;
\r
437 portBASE_TYPE xReturn = pdTRUE;
\r
439 /* Check the tasks are still running by ensuring the check variable
\r
440 is still incrementing. */
\r
442 if( usCheckVariable == usLastTaskCheck )
\r
444 /* The check has not incremented so an error exists. */
\r
448 if( ulExpectedValue == ulLastExpectedValue )
\r
450 /* The value being received by the queue receive task has not
\r
451 incremented so an error exists. */
\r
455 if( xSuspendedQueueSendError == pdTRUE )
\r
460 if( xSuspendedQueueReceiveError == pdTRUE )
\r
465 usLastTaskCheck = usCheckVariable;
\r
466 ulLastExpectedValue = ulExpectedValue;
\r