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 floating
\r
69 * point calculation and in so doing test the floating point context switching.
\r
70 * This file also demonstrates the use of the xPortUsesFloatingPoint() function
\r
71 * which informs the kernel that the task requires its floating point context
\r
72 * saved on each switch.
\r
74 * All the tasks run at the idle priority and never block or yield. This causes
\r
75 * all eight tasks to time slice with the idle task. Running at the idle
\r
76 * priority means that these tasks will get pre-empted any time another task is
\r
77 * ready to run or a time slice occurs. More often than not the pre-emption
\r
78 * will occur mid calculation, creating a good test of the schedulers context
\r
79 * switch mechanism - a calculation producing an unexpected result could be a
\r
80 * symptom of a corruption in the context of a task.
\r
86 /* Scheduler include files. */
\r
87 #include "FreeRTOS.h"
\r
90 /* Demo program include files. */
\r
93 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
94 #define mathNUMBER_OF_TASKS ( 8 )
\r
96 /* Four tasks, each of which performs a different floating point calculation.
\r
97 Each of the four is created twice. */
\r
98 static void vCompetingMathTask1( void *pvParameters );
\r
99 static void vCompetingMathTask2( void *pvParameters );
\r
100 static void vCompetingMathTask3( void *pvParameters );
\r
101 static void vCompetingMathTask4( void *pvParameters );
\r
103 /* These variables are used to check that all the tasks are still running. If a
\r
104 task gets a calculation wrong it will stop incrementing its check variable,
\r
105 otherwise the check variable will get incremented on each iteration of the
\r
106 tasks execution. */
\r
107 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
109 /*-----------------------------------------------------------*/
\r
111 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
113 xTaskHandle xCreatedTask;
\r
115 /* Create one of the floating point tasks... */
\r
116 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xCreatedTask );
\r
118 /* ... then enable floating point support for the created task so its flop
\r
119 flop registers are maintained in a consistent state. */
\r
120 xPortUsesFloatingPoint( xCreatedTask );
\r
122 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xCreatedTask );
\r
123 xPortUsesFloatingPoint( xCreatedTask );
\r
125 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xCreatedTask );
\r
126 xPortUsesFloatingPoint( xCreatedTask );
\r
128 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xCreatedTask );
\r
129 xPortUsesFloatingPoint( xCreatedTask );
\r
131 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xCreatedTask );
\r
132 xPortUsesFloatingPoint( xCreatedTask );
\r
134 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xCreatedTask );
\r
135 xPortUsesFloatingPoint( xCreatedTask );
\r
137 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xCreatedTask );
\r
138 xPortUsesFloatingPoint( xCreatedTask );
\r
140 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xCreatedTask );
\r
141 xPortUsesFloatingPoint( xCreatedTask );
\r
143 /*-----------------------------------------------------------*/
\r
145 static void vCompetingMathTask1( void *pvParameters )
\r
147 volatile double d1, d2, d3, d4;
\r
148 volatile unsigned short *pusTaskCheckVariable;
\r
149 volatile double dAnswer;
\r
150 short sError = pdFALSE;
\r
156 /* Calculate the expected answer. */
\r
157 dAnswer = ( d1 + d2 ) * d3;
\r
159 /* The variable this task increments to show it is still running is passed in
\r
160 as the parameter. */
\r
161 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
163 /* Keep performing a calculation and checking the result against a constant. */
\r
166 /* Perform the calculation. */
\r
171 d4 = ( d1 + d2 ) * d3;
\r
173 /* If the calculation does not match the expected constant, stop the
\r
174 increment of the check variable. */
\r
175 if( fabs( d4 - dAnswer ) > 0.001 )
\r
180 if( sError == pdFALSE )
\r
182 /* If the calculation has always been correct, increment the check
\r
183 variable so we know this task is still running okay. */
\r
184 ( *pusTaskCheckVariable )++;
\r
188 /*-----------------------------------------------------------*/
\r
190 static void vCompetingMathTask2( void *pvParameters )
\r
192 volatile double d1, d2, d3, d4;
\r
193 volatile unsigned short *pusTaskCheckVariable;
\r
194 volatile double dAnswer;
\r
195 short sError = pdFALSE;
\r
201 /* Calculate the expected answer. */
\r
202 dAnswer = ( d1 / d2 ) * d3;
\r
205 /* The variable this task increments to show it is still running is passed in
\r
206 as the parameter. */
\r
207 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
209 /* Keep performing a calculation and checking the result against a constant. */
\r
212 /* Perform the calculation. */
\r
217 d4 = ( d1 / d2 ) * d3;
\r
219 /* If the calculation does not match the expected constant, stop the
\r
220 increment of the check variable. */
\r
221 if( fabs( d4 - dAnswer ) > 0.001 )
\r
226 if( sError == pdFALSE )
\r
228 /* If the calculation has always been correct, increment the check
\r
229 variable so we know
\r
230 this task is still running okay. */
\r
231 ( *pusTaskCheckVariable )++;
\r
235 /*-----------------------------------------------------------*/
\r
237 static void vCompetingMathTask3( void *pvParameters )
\r
239 volatile double *pdArray, dTotal1, dTotal2, dDifference;
\r
240 volatile unsigned short *pusTaskCheckVariable;
\r
241 const size_t xArraySize = 10;
\r
243 short sError = pdFALSE;
\r
245 /* The variable this task increments to show it is still running is passed
\r
246 in as the parameter. */
\r
247 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
249 /* Allocate memory for use as an array. */
\r
250 pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );
\r
252 /* Keep filling an array, keeping a running total of the values placed in
\r
253 the array. Then run through the array adding up all the values. If the two
\r
254 totals do not match, stop the check variable from incrementing. */
\r
260 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
262 pdArray[ xPosition ] = ( double ) xPosition + 5.5;
\r
263 dTotal1 += ( double ) xPosition + 5.5;
\r
266 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
268 dTotal2 += pdArray[ xPosition ];
\r
271 dDifference = dTotal1 - dTotal2;
\r
272 if( fabs( dDifference ) > 0.001 )
\r
277 if( sError == pdFALSE )
\r
279 /* If the calculation has always been correct, increment the check
\r
280 variable so we know this task is still running okay. */
\r
281 ( *pusTaskCheckVariable )++;
\r
285 /*-----------------------------------------------------------*/
\r
287 static void vCompetingMathTask4( void *pvParameters )
\r
289 volatile double *pdArray, dTotal1, dTotal2, dDifference;
\r
290 volatile unsigned short *pusTaskCheckVariable;
\r
291 const size_t xArraySize = 10;
\r
293 short sError = pdFALSE;
\r
295 /* The variable this task increments to show it is still running is passed in
\r
296 as the parameter. */
\r
297 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
299 /* Allocate RAM for use as an array. */
\r
300 pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );
\r
302 /* Keep filling an array, keeping a running total of the values placed in the
\r
303 array. Then run through the array adding up all the values. If the two totals
\r
304 do not match, stop the check variable from incrementing. */
\r
310 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
312 pdArray[ xPosition ] = ( double ) xPosition * 12.123;
\r
313 dTotal1 += ( double ) xPosition * 12.123;
\r
316 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
318 dTotal2 += pdArray[ xPosition ];
\r
321 dDifference = dTotal1 - dTotal2;
\r
322 if( fabs( dDifference ) > 0.001 )
\r
327 if( sError == pdFALSE )
\r
329 /* If the calculation has always been correct, increment the check
\r
330 variable so we know this task is still running okay. */
\r
331 ( *pusTaskCheckVariable )++;
\r
335 /*-----------------------------------------------------------*/
\r
337 /* This is called to check that all the created tasks are still running. */
\r
338 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
340 /* Keep a history of the check variables so we know if they have been
\r
341 incremented since the last call. */
\r
342 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
343 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
345 /* Check the maths tasks are still running by ensuring their check variables
\r
346 are still incrementing. */
\r
347 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
349 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
351 /* The check has not incremented so an error exists. */
\r
355 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r