2 FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 * Creates eight tasks, each of which loops continuously performing a floating
\r
77 * point calculation and in so doing test the floating point context switching.
\r
78 * This file also demonstrates the use of the xPortUsesFloatingPoint() function
\r
79 * which informs the kernel that the task requires its floating point context
\r
80 * saved on each switch.
\r
82 * All the tasks run at the idle priority and never block or yield. This causes
\r
83 * all eight tasks to time slice with the idle task. Running at the idle
\r
84 * priority means that these tasks will get pre-empted any time another task is
\r
85 * ready to run or a time slice occurs. More often than not the pre-emption
\r
86 * will occur mid calculation, creating a good test of the schedulers context
\r
87 * switch mechanism - a calculation producing an unexpected result could be a
\r
88 * symptom of a corruption in the context of a task.
\r
94 /* Scheduler include files. */
\r
95 #include "FreeRTOS.h"
\r
98 /* Demo program include files. */
\r
101 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
102 #define mathNUMBER_OF_TASKS ( 8 )
\r
104 /* Four tasks, each of which performs a different floating point calculation.
\r
105 Each of the four is created twice. */
\r
106 static void vCompetingMathTask1( void *pvParameters );
\r
107 static void vCompetingMathTask2( void *pvParameters );
\r
108 static void vCompetingMathTask3( void *pvParameters );
\r
109 static void vCompetingMathTask4( void *pvParameters );
\r
111 /* These variables are used to check that all the tasks are still running. If a
\r
112 task gets a calculation wrong it will stop incrementing its check variable,
\r
113 otherwise the check variable will get incremented on each iteration of the
\r
114 tasks execution. */
\r
115 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
117 /*-----------------------------------------------------------*/
\r
119 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
121 xTaskHandle xCreatedTask;
\r
123 /* Create one of the floating point tasks... */
\r
124 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xCreatedTask );
\r
126 /* ... then enable floating point support for the created task so its flop
\r
127 flop registers are maintained in a consistent state. */
\r
128 xPortUsesFloatingPoint( xCreatedTask );
\r
130 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xCreatedTask );
\r
131 xPortUsesFloatingPoint( xCreatedTask );
\r
133 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xCreatedTask );
\r
134 xPortUsesFloatingPoint( xCreatedTask );
\r
136 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xCreatedTask );
\r
137 xPortUsesFloatingPoint( xCreatedTask );
\r
139 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xCreatedTask );
\r
140 xPortUsesFloatingPoint( xCreatedTask );
\r
142 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xCreatedTask );
\r
143 xPortUsesFloatingPoint( xCreatedTask );
\r
145 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xCreatedTask );
\r
146 xPortUsesFloatingPoint( xCreatedTask );
\r
148 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xCreatedTask );
\r
149 xPortUsesFloatingPoint( xCreatedTask );
\r
151 /*-----------------------------------------------------------*/
\r
153 static void vCompetingMathTask1( void *pvParameters )
\r
155 volatile double d1, d2, d3, d4;
\r
156 volatile unsigned short *pusTaskCheckVariable;
\r
157 volatile double dAnswer;
\r
158 short sError = pdFALSE;
\r
164 /* Calculate the expected answer. */
\r
165 dAnswer = ( d1 + d2 ) * d3;
\r
167 /* The variable this task increments to show it is still running is passed in
\r
168 as the parameter. */
\r
169 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
171 /* Keep performing a calculation and checking the result against a constant. */
\r
174 /* Perform the calculation. */
\r
179 d4 = ( d1 + d2 ) * d3;
\r
181 /* If the calculation does not match the expected constant, stop the
\r
182 increment of the check variable. */
\r
183 if( fabs( d4 - dAnswer ) > 0.001 )
\r
188 if( sError == pdFALSE )
\r
190 /* If the calculation has always been correct, increment the check
\r
191 variable so we know this task is still running okay. */
\r
192 ( *pusTaskCheckVariable )++;
\r
196 /*-----------------------------------------------------------*/
\r
198 static void vCompetingMathTask2( void *pvParameters )
\r
200 volatile double d1, d2, d3, d4;
\r
201 volatile unsigned short *pusTaskCheckVariable;
\r
202 volatile double dAnswer;
\r
203 short sError = pdFALSE;
\r
209 /* Calculate the expected answer. */
\r
210 dAnswer = ( d1 / d2 ) * d3;
\r
213 /* The variable this task increments to show it is still running is passed in
\r
214 as the parameter. */
\r
215 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
217 /* Keep performing a calculation and checking the result against a constant. */
\r
220 /* Perform the calculation. */
\r
225 d4 = ( d1 / d2 ) * d3;
\r
227 /* If the calculation does not match the expected constant, stop the
\r
228 increment of the check variable. */
\r
229 if( fabs( d4 - dAnswer ) > 0.001 )
\r
234 if( sError == pdFALSE )
\r
236 /* If the calculation has always been correct, increment the check
\r
237 variable so we know
\r
238 this task is still running okay. */
\r
239 ( *pusTaskCheckVariable )++;
\r
243 /*-----------------------------------------------------------*/
\r
245 static void vCompetingMathTask3( void *pvParameters )
\r
247 volatile double *pdArray, dTotal1, dTotal2, dDifference;
\r
248 volatile unsigned short *pusTaskCheckVariable;
\r
249 const size_t xArraySize = 10;
\r
251 short sError = pdFALSE;
\r
253 /* The variable this task increments to show it is still running is passed
\r
254 in as the parameter. */
\r
255 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
257 /* Allocate memory for use as an array. */
\r
258 pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );
\r
260 /* Keep filling an array, keeping a running total of the values placed in
\r
261 the array. Then run through the array adding up all the values. If the two
\r
262 totals do not match, stop the check variable from incrementing. */
\r
268 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
270 pdArray[ xPosition ] = ( double ) xPosition + 5.5;
\r
271 dTotal1 += ( double ) xPosition + 5.5;
\r
274 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
276 dTotal2 += pdArray[ xPosition ];
\r
279 dDifference = dTotal1 - dTotal2;
\r
280 if( fabs( dDifference ) > 0.001 )
\r
285 if( sError == pdFALSE )
\r
287 /* If the calculation has always been correct, increment the check
\r
288 variable so we know this task is still running okay. */
\r
289 ( *pusTaskCheckVariable )++;
\r
293 /*-----------------------------------------------------------*/
\r
295 static void vCompetingMathTask4( void *pvParameters )
\r
297 volatile double *pdArray, dTotal1, dTotal2, dDifference;
\r
298 volatile unsigned short *pusTaskCheckVariable;
\r
299 const size_t xArraySize = 10;
\r
301 short sError = pdFALSE;
\r
303 /* The variable this task increments to show it is still running is passed in
\r
304 as the parameter. */
\r
305 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
307 /* Allocate RAM for use as an array. */
\r
308 pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );
\r
310 /* Keep filling an array, keeping a running total of the values placed in the
\r
311 array. Then run through the array adding up all the values. If the two totals
\r
312 do not match, stop the check variable from incrementing. */
\r
318 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
320 pdArray[ xPosition ] = ( double ) xPosition * 12.123;
\r
321 dTotal1 += ( double ) xPosition * 12.123;
\r
324 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
326 dTotal2 += pdArray[ xPosition ];
\r
329 dDifference = dTotal1 - dTotal2;
\r
330 if( fabs( dDifference ) > 0.001 )
\r
335 if( sError == pdFALSE )
\r
337 /* If the calculation has always been correct, increment the check
\r
338 variable so we know this task is still running okay. */
\r
339 ( *pusTaskCheckVariable )++;
\r
343 /*-----------------------------------------------------------*/
\r
345 /* This is called to check that all the created tasks are still running. */
\r
346 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
348 /* Keep a history of the check variables so we know if they have been
\r
349 incremented since the last call. */
\r
350 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
351 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
353 /* Check the maths tasks are still running by ensuring their check variables
\r
354 are still incrementing. */
\r
355 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
357 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
359 /* The check has not incremented so an error exists. */
\r
363 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r