2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
50 * Creates eight tasks, each of which loops continuously performing a
\r
51 * floating point calculation.
\r
53 * All the tasks run at the idle priority and never block or yield. This causes
\r
54 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
55 * means that these tasks will get pre-empted any time another task is ready to run
\r
56 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
57 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
58 * calculation producing an unexpected result could be a symptom of a corruption in
\r
59 * the context of a task.
\r
61 * This file demonstrates the use of the task tag and traceTASK_SWITCHED_IN and
\r
62 * traceTASK_SWITCHED_OUT macros to save and restore the floating point context.
\r
68 /* Scheduler include files. */
\r
69 #include "FreeRTOS.h"
\r
72 /* Demo program include files. */
\r
75 /* Misc. definitions. */
\r
76 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
77 #define mathNUMBER_OF_TASKS ( 8 )
\r
79 /* Four tasks, each of which performs a different floating point calculation.
\r
80 Each of the four is created twice. */
\r
81 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
82 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
83 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
84 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
86 /* These variables are used to check that all the tasks are still running. If a
\r
87 task gets a calculation wrong it will stop incrementing its check variable. */
\r
88 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
90 /* Buffers into which the flop registers will be saved. There is a buffer for
\r
91 each task created within this file. Zeroing out this array is the normal and
\r
92 safe option as this will cause the task to start with all zeros in its flop
\r
94 static portDOUBLE dFlopRegisters[ mathNUMBER_OF_TASKS ][ portNO_FLOP_REGISTERS_TO_SAVE ];
\r
96 /*-----------------------------------------------------------*/
\r
98 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
100 xTaskHandle xTaskJustCreated;
\r
101 portBASE_TYPE x, y;
\r
103 /* Place known values into the buffers into which the flop registers are
\r
104 to be saved. This is for debug purposes only, it is not normally
\r
105 required. The last position in each array is left at zero as the status
\r
106 register will be loaded from there.
\r
108 It is intended that these values can be viewed being loaded into the
\r
109 flop registers when a task is started - however the Insight debugger
\r
110 does not seem to want to show the flop register values. */
\r
111 for( x = 0; x < mathNUMBER_OF_TASKS; x++ )
\r
113 for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1 ); y++ )
\r
115 dFlopRegisters[ x ][ y ] = ( x + 1 );
\r
119 /* Create the first task - passing it the address of the check variable
\r
120 that it is going to increment. This check variable is used as an
\r
121 indication that the task is still running. */
\r
122 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xTaskJustCreated );
\r
124 /* The task tag value is a value that can be associated with a task, but
\r
125 is not used by the scheduler itself. Its use is down to the application so
\r
126 it makes a convenient place in this case to store the pointer to the buffer
\r
127 into which the flop context of the task will be stored. The first created
\r
128 task uses dFlopRegisters[ 0 ], the second dFlopRegisters[ 1 ], etc. */
\r
129 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 0 ][ 0 ] ) );
\r
131 /* Create another 7 tasks, allocating a buffer for each. */
\r
132 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xTaskJustCreated );
\r
133 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 1 ][ 0 ] ) );
\r
135 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xTaskJustCreated );
\r
136 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 2 ][ 0 ] ) );
\r
138 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xTaskJustCreated );
\r
139 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 3 ][ 0 ] ) );
\r
141 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xTaskJustCreated );
\r
142 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 4 ][ 0 ] ) );
\r
144 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xTaskJustCreated );
\r
145 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 5 ][ 0 ] ) );
\r
147 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xTaskJustCreated );
\r
148 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 6 ][ 0 ] ) );
\r
150 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xTaskJustCreated );
\r
151 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 7 ][ 0 ] ) );
\r
153 /*-----------------------------------------------------------*/
\r
155 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
157 volatile portDOUBLE df1, df2, df3, df4;
\r
158 volatile unsigned short *pusTaskCheckVariable;
\r
159 volatile portDOUBLE dAnswer;
\r
160 short sError = pdFALSE;
\r
166 dAnswer = ( df1 + df2 ) * df3;
\r
168 /* The variable this task increments to show it is still running is passed in
\r
169 as the parameter. */
\r
170 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
172 /* Keep performing a calculation and checking the result against a constant. */
\r
179 df4 = ( df1 + df2 ) * df3;
\r
181 #if configUSE_PREEMPTION == 0
\r
185 /* If the calculation does not match the expected constant, stop the
\r
186 increment of the check variable. */
\r
187 if( fabs( df4 - dAnswer ) > 0.001 )
\r
192 if( sError == pdFALSE )
\r
194 /* If the calculation has always been correct, increment the check
\r
195 variable so we know this task is still running okay. */
\r
196 ( *pusTaskCheckVariable )++;
\r
199 #if configUSE_PREEMPTION == 0
\r
205 /*-----------------------------------------------------------*/
\r
207 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
209 volatile portDOUBLE df1, df2, df3, df4;
\r
210 volatile unsigned short *pusTaskCheckVariable;
\r
211 volatile portDOUBLE dAnswer;
\r
212 short sError = pdFALSE;
\r
218 dAnswer = ( df1 / df2 ) * df3;
\r
221 /* The variable this task increments to show it is still running is passed in
\r
222 as the parameter. */
\r
223 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
225 /* Keep performing a calculation and checking the result against a constant. */
\r
232 df4 = ( df1 / df2 ) * df3;
\r
234 #if configUSE_PREEMPTION == 0
\r
238 /* If the calculation does not match the expected constant, stop the
\r
239 increment of the check variable. */
\r
240 if( fabs( df4 - dAnswer ) > 0.001 )
\r
245 if( sError == pdFALSE )
\r
247 /* If the calculation has always been correct, increment the check
\r
248 variable so we know
\r
249 this task is still running okay. */
\r
250 ( *pusTaskCheckVariable )++;
\r
253 #if configUSE_PREEMPTION == 0
\r
258 /*-----------------------------------------------------------*/
\r
260 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
262 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
263 volatile unsigned short *pusTaskCheckVariable;
\r
264 const size_t xArraySize = 10;
\r
266 short sError = pdFALSE;
\r
268 /* The variable this task increments to show it is still running is passed in
\r
269 as the parameter. */
\r
270 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
272 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
274 /* Keep filling an array, keeping a running total of the values placed in the
\r
275 array. Then run through the array adding up all the values. If the two totals
\r
276 do not match, stop the check variable from incrementing. */
\r
282 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
284 pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;
\r
285 dTotal1 += ( portDOUBLE ) xPosition + 5.5;
\r
288 #if configUSE_PREEMPTION == 0
\r
292 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
294 dTotal2 += pdArray[ xPosition ];
\r
297 dDifference = dTotal1 - dTotal2;
\r
298 if( fabs( dDifference ) > 0.001 )
\r
303 #if configUSE_PREEMPTION == 0
\r
307 if( sError == pdFALSE )
\r
309 /* If the calculation has always been correct, increment the check
\r
310 variable so we know this task is still running okay. */
\r
311 ( *pusTaskCheckVariable )++;
\r
315 /*-----------------------------------------------------------*/
\r
317 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
319 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
320 volatile unsigned short *pusTaskCheckVariable;
\r
321 const size_t xArraySize = 10;
\r
323 short sError = pdFALSE;
\r
325 /* The variable this task increments to show it is still running is passed in
\r
326 as the parameter. */
\r
327 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
329 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
331 /* Keep filling an array, keeping a running total of the values placed in the
\r
332 array. Then run through the array adding up all the values. If the two totals
\r
333 do not match, stop the check variable from incrementing. */
\r
339 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
341 pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;
\r
342 dTotal1 += ( portDOUBLE ) xPosition * 12.123;
\r
345 #if configUSE_PREEMPTION == 0
\r
349 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
351 dTotal2 += pdArray[ xPosition ];
\r
354 dDifference = dTotal1 - dTotal2;
\r
355 if( fabs( dDifference ) > 0.001 )
\r
360 #if configUSE_PREEMPTION == 0
\r
364 if( sError == pdFALSE )
\r
366 /* If the calculation has always been correct, increment the check
\r
367 variable so we know this task is still running okay. */
\r
368 ( *pusTaskCheckVariable )++;
\r
372 /*-----------------------------------------------------------*/
\r
374 /* This is called to check that all the created tasks are still running. */
\r
375 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
377 /* Keep a history of the check variables so we know if they have been incremented
\r
378 since the last call. */
\r
379 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
380 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
382 /* Check the maths tasks are still running by ensuring their check variables
\r
383 are still incrementing. */
\r
384 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
386 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
388 /* The check has not incremented so an error exists. */
\r
392 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r