2 FreeRTOS V6.0.2 - Copyright (C) 2010 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\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 exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 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 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * Creates eight tasks, each of which loops continuously performing a floating
\r
56 * point calculation and in so doing test the floating point context switching.
\r
57 * This file also demonstrates the use of the xPortUsesFloatingPoint() function
\r
58 * which informs the kernel that the task requires its floating point context
\r
59 * saved on each switch.
\r
61 * All the tasks run at the idle priority and never block or yield. This causes
\r
62 * all eight tasks to time slice with the idle task. Running at the idle
\r
63 * priority means that these tasks will get pre-empted any time another task is
\r
64 * ready to run or a time slice occurs. More often than not the pre-emption
\r
65 * will occur mid calculation, creating a good test of the schedulers context
\r
66 * switch mechanism - a calculation producing an unexpected result could be a
\r
67 * symptom of a corruption in the context of a task.
\r
73 /* Scheduler include files. */
\r
74 #include "FreeRTOS.h"
\r
77 /* Demo program include files. */
\r
80 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
81 #define mathNUMBER_OF_TASKS ( 8 )
\r
83 /* Four tasks, each of which performs a different floating point calculation.
\r
84 Each of the four is created twice. */
\r
85 static void vCompetingMathTask1( void *pvParameters );
\r
86 static void vCompetingMathTask2( void *pvParameters );
\r
87 static void vCompetingMathTask3( void *pvParameters );
\r
88 static void vCompetingMathTask4( void *pvParameters );
\r
90 /* These variables are used to check that all the tasks are still running. If a
\r
91 task gets a calculation wrong it will stop incrementing its check variable,
\r
92 otherwise the check variable will get incremented on each iteration of the
\r
94 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
96 /*-----------------------------------------------------------*/
\r
98 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
100 xTaskHandle xCreatedTask;
\r
102 /* Create one of the floating point tasks... */
\r
103 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xCreatedTask );
\r
105 /* ... then enable floating point support for the created task so its flop
\r
106 flop registers are maintained in a consistent state. */
\r
107 xPortUsesFloatingPoint( xCreatedTask );
\r
109 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xCreatedTask );
\r
110 xPortUsesFloatingPoint( xCreatedTask );
\r
112 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xCreatedTask );
\r
113 xPortUsesFloatingPoint( xCreatedTask );
\r
115 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xCreatedTask );
\r
116 xPortUsesFloatingPoint( xCreatedTask );
\r
118 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xCreatedTask );
\r
119 xPortUsesFloatingPoint( xCreatedTask );
\r
121 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xCreatedTask );
\r
122 xPortUsesFloatingPoint( xCreatedTask );
\r
124 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xCreatedTask );
\r
125 xPortUsesFloatingPoint( xCreatedTask );
\r
127 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xCreatedTask );
\r
128 xPortUsesFloatingPoint( xCreatedTask );
\r
130 /*-----------------------------------------------------------*/
\r
132 static void vCompetingMathTask1( void *pvParameters )
\r
134 volatile double d1, d2, d3, d4;
\r
135 volatile unsigned short *pusTaskCheckVariable;
\r
136 volatile double dAnswer;
\r
137 short sError = pdFALSE;
\r
143 /* Calculate the expected answer. */
\r
144 dAnswer = ( d1 + d2 ) * d3;
\r
146 /* The variable this task increments to show it is still running is passed in
\r
147 as the parameter. */
\r
148 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
150 /* Keep performing a calculation and checking the result against a constant. */
\r
153 /* Perform the calculation. */
\r
158 d4 = ( d1 + d2 ) * d3;
\r
160 /* If the calculation does not match the expected constant, stop the
\r
161 increment of the check variable. */
\r
162 if( fabs( d4 - dAnswer ) > 0.001 )
\r
167 if( sError == pdFALSE )
\r
169 /* If the calculation has always been correct, increment the check
\r
170 variable so we know this task is still running okay. */
\r
171 ( *pusTaskCheckVariable )++;
\r
175 /*-----------------------------------------------------------*/
\r
177 static void vCompetingMathTask2( void *pvParameters )
\r
179 volatile double d1, d2, d3, d4;
\r
180 volatile unsigned short *pusTaskCheckVariable;
\r
181 volatile double dAnswer;
\r
182 short sError = pdFALSE;
\r
188 /* Calculate the expected answer. */
\r
189 dAnswer = ( d1 / d2 ) * d3;
\r
192 /* The variable this task increments to show it is still running is passed in
\r
193 as the parameter. */
\r
194 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
196 /* Keep performing a calculation and checking the result against a constant. */
\r
199 /* Perform the calculation. */
\r
204 d4 = ( d1 / d2 ) * d3;
\r
206 /* If the calculation does not match the expected constant, stop the
\r
207 increment of the check variable. */
\r
208 if( fabs( d4 - dAnswer ) > 0.001 )
\r
213 if( sError == pdFALSE )
\r
215 /* If the calculation has always been correct, increment the check
\r
216 variable so we know
\r
217 this task is still running okay. */
\r
218 ( *pusTaskCheckVariable )++;
\r
222 /*-----------------------------------------------------------*/
\r
224 static void vCompetingMathTask3( void *pvParameters )
\r
226 volatile double *pdArray, dTotal1, dTotal2, dDifference;
\r
227 volatile unsigned short *pusTaskCheckVariable;
\r
228 const size_t xArraySize = 10;
\r
230 short sError = pdFALSE;
\r
232 /* The variable this task increments to show it is still running is passed
\r
233 in as the parameter. */
\r
234 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
236 /* Allocate memory for use as an array. */
\r
237 pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );
\r
239 /* Keep filling an array, keeping a running total of the values placed in
\r
240 the array. Then run through the array adding up all the values. If the two
\r
241 totals do not match, stop the check variable from incrementing. */
\r
247 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
249 pdArray[ xPosition ] = ( double ) xPosition + 5.5;
\r
250 dTotal1 += ( double ) xPosition + 5.5;
\r
253 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
255 dTotal2 += pdArray[ xPosition ];
\r
258 dDifference = dTotal1 - dTotal2;
\r
259 if( fabs( dDifference ) > 0.001 )
\r
264 if( sError == pdFALSE )
\r
266 /* If the calculation has always been correct, increment the check
\r
267 variable so we know this task is still running okay. */
\r
268 ( *pusTaskCheckVariable )++;
\r
272 /*-----------------------------------------------------------*/
\r
274 static void vCompetingMathTask4( void *pvParameters )
\r
276 volatile double *pdArray, dTotal1, dTotal2, dDifference;
\r
277 volatile unsigned short *pusTaskCheckVariable;
\r
278 const size_t xArraySize = 10;
\r
280 short sError = pdFALSE;
\r
282 /* The variable this task increments to show it is still running is passed in
\r
283 as the parameter. */
\r
284 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
286 /* Allocate RAM for use as an array. */
\r
287 pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );
\r
289 /* Keep filling an array, keeping a running total of the values placed in the
\r
290 array. Then run through the array adding up all the values. If the two totals
\r
291 do not match, stop the check variable from incrementing. */
\r
297 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
299 pdArray[ xPosition ] = ( double ) xPosition * 12.123;
\r
300 dTotal1 += ( double ) xPosition * 12.123;
\r
303 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
305 dTotal2 += pdArray[ xPosition ];
\r
308 dDifference = dTotal1 - dTotal2;
\r
309 if( fabs( dDifference ) > 0.001 )
\r
314 if( sError == pdFALSE )
\r
316 /* If the calculation has always been correct, increment the check
\r
317 variable so we know this task is still running okay. */
\r
318 ( *pusTaskCheckVariable )++;
\r
322 /*-----------------------------------------------------------*/
\r
324 /* This is called to check that all the created tasks are still running. */
\r
325 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
327 /* Keep a history of the check variables so we know if they have been
\r
328 incremented since the last call. */
\r
329 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
330 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
332 /* Check the maths tasks are still running by ensuring their check variables
\r
333 are still incrementing. */
\r
334 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
336 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
338 /* The check has not incremented so an error exists. */
\r
342 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r