2 FreeRTOS.org V5.0.0 - 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 * Creates eight tasks, each of which loops continuously performing a
\r
52 * floating point calculation.
\r
54 * All the tasks run at the idle priority and never block or yield. This causes
\r
55 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
56 * means that these tasks will get pre-empted any time another task is ready to run
\r
57 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
58 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
59 * calculation producing an unexpected result could be a symptom of a corruption in
\r
60 * the context of a task.
\r
62 * This file demonstrates the use of the task tag and traceTASK_SWITCHED_IN and
\r
63 * traceTASK_SWITCHED_OUT macros to save and restore the floating point context.
\r
69 /* Scheduler include files. */
\r
70 #include "FreeRTOS.h"
\r
73 /* Demo program include files. */
\r
76 /* Misc. definitions. */
\r
77 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
78 #define mathNUMBER_OF_TASKS ( 8 )
\r
80 /* Four tasks, each of which performs a different floating point calculation.
\r
81 Each of the four is created twice. */
\r
82 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
83 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
84 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
85 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
87 /* These variables are used to check that all the tasks are still running. If a
\r
88 task gets a calculation wrong it will stop incrementing its check variable. */
\r
89 static volatile unsigned portSHORT usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
91 /* Buffers into which the flop registers will be saved. There is a buffer for
\r
92 each task created within this file. Zeroing out this array is the normal and
\r
93 safe option as this will cause the task to start with all zeros in its flop
\r
95 static unsigned portLONG ulFlopRegisters[ mathNUMBER_OF_TASKS ][ portNO_FLOP_REGISTERS_TO_SAVE ];
\r
97 /*-----------------------------------------------------------*/
\r
99 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
101 xTaskHandle xTaskJustCreated;
\r
102 portBASE_TYPE x, y;
\r
104 /* Place known values into the buffers into which the flop registers are
\r
105 to be saved. This is for debug purposes only, it is not normally
\r
106 required. The last position in each array is left at zero as the status
\r
107 register will be loaded from there.
\r
109 It is intended that these values can be viewed being loaded into the
\r
110 flop registers when a task is started - however the Insight debugger
\r
111 does not seem to want to show the flop register values. */
\r
112 for( x = 0; x < mathNUMBER_OF_TASKS; x++ )
\r
114 for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1 ); y++ )
\r
116 ulFlopRegisters[ x ][ y ] = ( x + 1 );
\r
120 /* Create the first task - passing it the address of the check variable
\r
121 that it is going to increment. This check variable is used as an
\r
122 indication that the task is still running. */
\r
123 xTaskCreate( vCompetingMathTask1, ( signed portCHAR * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xTaskJustCreated );
\r
125 /* The task tag value is a value that can be associated with a task, but
\r
126 is not used by the scheduler itself. Its use is down to the application so
\r
127 it makes a convenient place in this case to store the pointer to the buffer
\r
128 into which the flop context of the task will be stored. The first created
\r
129 task uses ulFlopRegisters[ 0 ], the second ulFlopRegisters[ 1 ], etc. */
\r
130 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 0 ][ 0 ] ) );
\r
132 /* Create another 7 tasks, allocating a buffer for each. */
\r
133 xTaskCreate( vCompetingMathTask2, ( signed portCHAR * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xTaskJustCreated );
\r
134 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 1 ][ 0 ] ) );
\r
136 xTaskCreate( vCompetingMathTask3, ( signed portCHAR * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xTaskJustCreated );
\r
137 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 2 ][ 0 ] ) );
\r
139 xTaskCreate( vCompetingMathTask4, ( signed portCHAR * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xTaskJustCreated );
\r
140 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 3 ][ 0 ] ) );
\r
142 xTaskCreate( vCompetingMathTask1, ( signed portCHAR * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xTaskJustCreated );
\r
143 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 4 ][ 0 ] ) );
\r
145 xTaskCreate( vCompetingMathTask2, ( signed portCHAR * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xTaskJustCreated );
\r
146 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 5 ][ 0 ] ) );
\r
148 xTaskCreate( vCompetingMathTask3, ( signed portCHAR * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xTaskJustCreated );
\r
149 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 6 ][ 0 ] ) );
\r
151 xTaskCreate( vCompetingMathTask4, ( signed portCHAR * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xTaskJustCreated );
\r
152 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 7 ][ 0 ] ) );
\r
154 /*-----------------------------------------------------------*/
\r
156 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
158 volatile portFLOAT ff1, ff2, ff3, ff4;
\r
159 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
160 volatile portFLOAT fAnswer;
\r
161 portSHORT sError = pdFALSE;
\r
167 fAnswer = ( ff1 + ff2 ) * ff3;
\r
169 /* The variable this task increments to show it is still running is passed in
\r
170 as the parameter. */
\r
171 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
173 /* Keep performing a calculation and checking the result against a constant. */
\r
180 ff4 = ( ff1 + ff2 ) * ff3;
\r
182 #if configUSE_PREEMPTION == 0
\r
186 /* If the calculation does not match the expected constant, stop the
\r
187 increment of the check variable. */
\r
188 if( fabs( ff4 - fAnswer ) > 0.001F )
\r
193 if( sError == pdFALSE )
\r
195 /* If the calculation has always been correct, increment the check
\r
196 variable so we know this task is still running okay. */
\r
197 ( *pusTaskCheckVariable )++;
\r
200 #if configUSE_PREEMPTION == 0
\r
206 /*-----------------------------------------------------------*/
\r
208 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
210 volatile portFLOAT ff1, ff2, ff3, ff4;
\r
211 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
212 volatile portFLOAT fAnswer;
\r
213 portSHORT sError = pdFALSE;
\r
219 fAnswer = ( ff1 / ff2 ) * ff3;
\r
222 /* The variable this task increments to show it is still running is passed in
\r
223 as the parameter. */
\r
224 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
226 /* Keep performing a calculation and checking the result against a constant. */
\r
233 ff4 = ( ff1 / ff2 ) * ff3;
\r
235 #if configUSE_PREEMPTION == 0
\r
239 /* If the calculation does not match the expected constant, stop the
\r
240 increment of the check variable. */
\r
241 if( fabs( ff4 - fAnswer ) > 0.001F )
\r
246 if( sError == pdFALSE )
\r
248 /* If the calculation has always been correct, increment the check
\r
249 variable so we know
\r
250 this task is still running okay. */
\r
251 ( *pusTaskCheckVariable )++;
\r
254 #if configUSE_PREEMPTION == 0
\r
259 /*-----------------------------------------------------------*/
\r
261 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
263 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
\r
264 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
265 const size_t xArraySize = 10;
\r
267 portSHORT sError = pdFALSE;
\r
269 /* The variable this task increments to show it is still running is passed in
\r
270 as the parameter. */
\r
271 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
273 pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
\r
275 /* Keep filling an array, keeping a running total of the values placed in the
\r
276 array. Then run through the array adding up all the values. If the two totals
\r
277 do not match, stop the check variable from incrementing. */
\r
283 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
285 pfArray[ xPosition ] = ( portFLOAT ) xPosition + 5.5F;
\r
286 fTotal1 += ( portFLOAT ) xPosition + 5.5F;
\r
289 #if configUSE_PREEMPTION == 0
\r
293 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
295 fTotal2 += pfArray[ xPosition ];
\r
298 fDifference = fTotal1 - fTotal2;
\r
299 if( fabs( fDifference ) > 0.001F )
\r
304 #if configUSE_PREEMPTION == 0
\r
308 if( sError == pdFALSE )
\r
310 /* If the calculation has always been correct, increment the check
\r
311 variable so we know this task is still running okay. */
\r
312 ( *pusTaskCheckVariable )++;
\r
316 /*-----------------------------------------------------------*/
\r
318 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
320 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
\r
321 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
322 const size_t xArraySize = 10;
\r
324 portSHORT sError = pdFALSE;
\r
326 /* The variable this task increments to show it is still running is passed in
\r
327 as the parameter. */
\r
328 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
330 pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
\r
332 /* Keep filling an array, keeping a running total of the values placed in the
\r
333 array. Then run through the array adding up all the values. If the two totals
\r
334 do not match, stop the check variable from incrementing. */
\r
340 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
342 pfArray[ xPosition ] = ( portFLOAT ) xPosition * 12.123F;
\r
343 fTotal1 += ( portFLOAT ) xPosition * 12.123F;
\r
346 #if configUSE_PREEMPTION == 0
\r
350 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
352 fTotal2 += pfArray[ xPosition ];
\r
355 fDifference = fTotal1 - fTotal2;
\r
356 if( fabs( fDifference ) > 0.001F )
\r
361 #if configUSE_PREEMPTION == 0
\r
365 if( sError == pdFALSE )
\r
367 /* If the calculation has always been correct, increment the check
\r
368 variable so we know this task is still running okay. */
\r
369 ( *pusTaskCheckVariable )++;
\r
373 /*-----------------------------------------------------------*/
\r
375 /* This is called to check that all the created tasks are still running. */
\r
376 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
378 /* Keep a history of the check variables so we know if they have been incremented
\r
379 since the last call. */
\r
380 static unsigned portSHORT usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
381 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
383 /* Check the maths tasks are still running by ensuring their check variables
\r
384 are still incrementing. */
\r
385 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
387 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
389 /* The check has not incremented so an error exists. */
\r
393 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r