2 FreeRTOS V7.1.1 - 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 * Creates eight tasks, each of which loops continuously performing a
\r
69 * floating point calculation.
\r
71 * All the tasks run at the idle priority and never block or yield. This causes
\r
72 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
73 * means that these tasks will get pre-empted any time another task is ready to run
\r
74 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
75 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
76 * calculation producing an unexpected result could be a symptom of a corruption in
\r
77 * the context of a task.
\r
79 * This file demonstrates the use of the task tag and traceTASK_SWITCHED_IN and
\r
80 * traceTASK_SWITCHED_OUT macros to save and restore the floating point context.
\r
86 /* Scheduler include files. */
\r
87 #include "FreeRTOS.h"
\r
90 /* Demo program include files. */
\r
93 /* Misc. definitions. */
\r
94 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
95 #define mathNUMBER_OF_TASKS ( 8 )
\r
97 /* Four tasks, each of which performs a different floating point calculation.
\r
98 Each of the four is created twice. */
\r
99 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
100 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
101 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
102 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
104 /* These variables are used to check that all the tasks are still running. If a
\r
105 task gets a calculation wrong it will stop incrementing its check variable. */
\r
106 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
108 /* Buffers into which the flop registers will be saved. There is a buffer for
\r
109 each task created within this file. Zeroing out this array is the normal and
\r
110 safe option as this will cause the task to start with all zeros in its flop
\r
112 static unsigned long ulFlopRegisters[ mathNUMBER_OF_TASKS ][ portNO_FLOP_REGISTERS_TO_SAVE ];
\r
114 /*-----------------------------------------------------------*/
\r
116 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
118 xTaskHandle xTaskJustCreated;
\r
119 portBASE_TYPE x, y;
\r
121 /* Place known values into the buffers into which the flop registers are
\r
122 to be saved. This is for debug purposes only, it is not normally
\r
123 required. The last position in each array is left at zero as the status
\r
124 register will be loaded from there.
\r
126 It is intended that these values can be viewed being loaded into the
\r
127 flop registers when a task is started - however the Insight debugger
\r
128 does not seem to want to show the flop register values. */
\r
129 for( x = 0; x < mathNUMBER_OF_TASKS; x++ )
\r
131 for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1 ); y++ )
\r
133 ulFlopRegisters[ x ][ y ] = ( x + 1 );
\r
137 /* Create the first task - passing it the address of the check variable
\r
138 that it is going to increment. This check variable is used as an
\r
139 indication that the task is still running. */
\r
140 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xTaskJustCreated );
\r
142 /* The task tag value is a value that can be associated with a task, but
\r
143 is not used by the scheduler itself. Its use is down to the application so
\r
144 it makes a convenient place in this case to store the pointer to the buffer
\r
145 into which the flop context of the task will be stored. The first created
\r
146 task uses ulFlopRegisters[ 0 ], the second ulFlopRegisters[ 1 ], etc. */
\r
147 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 0 ][ 0 ] ) );
\r
149 /* Create another 7 tasks, allocating a buffer for each. */
\r
150 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xTaskJustCreated );
\r
151 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 1 ][ 0 ] ) );
\r
153 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xTaskJustCreated );
\r
154 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 2 ][ 0 ] ) );
\r
156 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xTaskJustCreated );
\r
157 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 3 ][ 0 ] ) );
\r
159 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xTaskJustCreated );
\r
160 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 4 ][ 0 ] ) );
\r
162 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xTaskJustCreated );
\r
163 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 5 ][ 0 ] ) );
\r
165 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xTaskJustCreated );
\r
166 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 6 ][ 0 ] ) );
\r
168 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xTaskJustCreated );
\r
169 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 7 ][ 0 ] ) );
\r
171 /*-----------------------------------------------------------*/
\r
173 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
175 volatile portFLOAT ff1, ff2, ff3, ff4;
\r
176 volatile unsigned short *pusTaskCheckVariable;
\r
177 volatile portFLOAT fAnswer;
\r
178 short sError = pdFALSE;
\r
184 fAnswer = ( ff1 + ff2 ) * ff3;
\r
186 /* The variable this task increments to show it is still running is passed in
\r
187 as the parameter. */
\r
188 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
190 /* Keep performing a calculation and checking the result against a constant. */
\r
197 ff4 = ( ff1 + ff2 ) * ff3;
\r
199 #if configUSE_PREEMPTION == 0
\r
203 /* If the calculation does not match the expected constant, stop the
\r
204 increment of the check variable. */
\r
205 if( fabs( ff4 - fAnswer ) > 0.001F )
\r
210 if( sError == pdFALSE )
\r
212 /* If the calculation has always been correct, increment the check
\r
213 variable so we know this task is still running okay. */
\r
214 ( *pusTaskCheckVariable )++;
\r
217 #if configUSE_PREEMPTION == 0
\r
223 /*-----------------------------------------------------------*/
\r
225 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
227 volatile portFLOAT ff1, ff2, ff3, ff4;
\r
228 volatile unsigned short *pusTaskCheckVariable;
\r
229 volatile portFLOAT fAnswer;
\r
230 short sError = pdFALSE;
\r
236 fAnswer = ( ff1 / ff2 ) * ff3;
\r
239 /* The variable this task increments to show it is still running is passed in
\r
240 as the parameter. */
\r
241 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
243 /* Keep performing a calculation and checking the result against a constant. */
\r
250 ff4 = ( ff1 / ff2 ) * ff3;
\r
252 #if configUSE_PREEMPTION == 0
\r
256 /* If the calculation does not match the expected constant, stop the
\r
257 increment of the check variable. */
\r
258 if( fabs( ff4 - fAnswer ) > 0.001F )
\r
263 if( sError == pdFALSE )
\r
265 /* If the calculation has always been correct, increment the check
\r
266 variable so we know
\r
267 this task is still running okay. */
\r
268 ( *pusTaskCheckVariable )++;
\r
271 #if configUSE_PREEMPTION == 0
\r
276 /*-----------------------------------------------------------*/
\r
278 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
280 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
\r
281 volatile unsigned short *pusTaskCheckVariable;
\r
282 const size_t xArraySize = 10;
\r
284 short sError = pdFALSE;
\r
286 /* The variable this task increments to show it is still running is passed in
\r
287 as the parameter. */
\r
288 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
290 pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
\r
292 /* Keep filling an array, keeping a running total of the values placed in the
\r
293 array. Then run through the array adding up all the values. If the two totals
\r
294 do not match, stop the check variable from incrementing. */
\r
300 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
302 pfArray[ xPosition ] = ( portFLOAT ) xPosition + 5.5F;
\r
303 fTotal1 += ( portFLOAT ) xPosition + 5.5F;
\r
306 #if configUSE_PREEMPTION == 0
\r
310 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
312 fTotal2 += pfArray[ xPosition ];
\r
315 fDifference = fTotal1 - fTotal2;
\r
316 if( fabs( fDifference ) > 0.001F )
\r
321 #if configUSE_PREEMPTION == 0
\r
325 if( sError == pdFALSE )
\r
327 /* If the calculation has always been correct, increment the check
\r
328 variable so we know this task is still running okay. */
\r
329 ( *pusTaskCheckVariable )++;
\r
333 /*-----------------------------------------------------------*/
\r
335 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
337 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
\r
338 volatile unsigned short *pusTaskCheckVariable;
\r
339 const size_t xArraySize = 10;
\r
341 short sError = pdFALSE;
\r
343 /* The variable this task increments to show it is still running is passed in
\r
344 as the parameter. */
\r
345 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
347 pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
\r
349 /* Keep filling an array, keeping a running total of the values placed in the
\r
350 array. Then run through the array adding up all the values. If the two totals
\r
351 do not match, stop the check variable from incrementing. */
\r
357 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
359 pfArray[ xPosition ] = ( portFLOAT ) xPosition * 12.123F;
\r
360 fTotal1 += ( portFLOAT ) xPosition * 12.123F;
\r
363 #if configUSE_PREEMPTION == 0
\r
367 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
369 fTotal2 += pfArray[ xPosition ];
\r
372 fDifference = fTotal1 - fTotal2;
\r
373 if( fabs( fDifference ) > 0.001F )
\r
378 #if configUSE_PREEMPTION == 0
\r
382 if( sError == pdFALSE )
\r
384 /* If the calculation has always been correct, increment the check
\r
385 variable so we know this task is still running okay. */
\r
386 ( *pusTaskCheckVariable )++;
\r
390 /*-----------------------------------------------------------*/
\r
392 /* This is called to check that all the created tasks are still running. */
\r
393 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
395 /* Keep a history of the check variables so we know if they have been incremented
\r
396 since the last call. */
\r
397 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
398 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
400 /* Check the maths tasks are still running by ensuring their check variables
\r
401 are still incrementing. */
\r
402 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
404 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
406 /* The check has not incremented so an error exists. */
\r
410 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r