FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Common / Full / flop.c
blob0a4768614d703b0dbe58fb81171e1b53d0c0c74a
1 /*
2 FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.
4 ***************************************************************************
5 * *
6 * If you are: *
7 * *
8 * + New to FreeRTOS, *
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
10 * + Looking for basic training, *
11 * + Wanting to improve your FreeRTOS skills and productivity *
12 * *
13 * then take a look at the FreeRTOS eBook *
14 * *
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
16 * http://www.FreeRTOS.org/Documentation *
17 * *
18 * A pdf reference manual is also available. Both are usually delivered *
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
20 * and 8pm GMT (although please allow up to 24 hours in case of *
21 * exceptional circumstances). Thank you for your support! *
22 * *
23 ***************************************************************************
25 This file is part of the FreeRTOS distribution.
27 FreeRTOS is free software; you can redistribute it and/or modify it under
28 the terms of the GNU General Public License (version 2) as published by the
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
31 a combined work that includes FreeRTOS without being obliged to provide the
32 source code for proprietary components outside of the FreeRTOS kernel.
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
36 more details. You should have received a copy of the GNU General Public
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
39 by writing to Richard Barry, contact details for whom are available on the
40 FreeRTOS WEB site.
42 1 tab == 4 spaces!
44 http://www.FreeRTOS.org - Documentation, latest information, license and
45 contact details.
47 http://www.SafeRTOS.com - A version that is certified for use in safety
48 critical systems.
50 http://www.OpenRTOS.com - Commercial support, development, porting,
51 licensing and training services.
55 Changes from V1.2.3
57 + The created tasks now include calls to tskYIELD(), allowing them to be used
58 with the cooperative scheduler.
61 /**
62 * Creates eight tasks, each of which loops continuously performing an (emulated)
63 * floating point calculation.
65 * All the tasks run at the idle priority and never block or yield. This causes
66 * all eight tasks to time slice with the idle task. Running at the idle priority
67 * means that these tasks will get pre-empted any time another task is ready to run
68 * or a time slice occurs. More often than not the pre-emption will occur mid
69 * calculation, creating a good test of the schedulers context switch mechanism - a
70 * calculation producing an unexpected result could be a symptom of a corruption in
71 * the context of a task.
73 * \page FlopC flop.c
74 * \ingroup DemoFiles
75 * <HR>
78 #include <stdlib.h>
79 #include <math.h>
81 /* Scheduler include files. */
82 #include "FreeRTOS.h"
83 #include "task.h"
84 #include "print.h"
86 /* Demo program include files. */
87 #include "flop.h"
89 #define mathSTACK_SIZE ( ( unsigned short ) 512 )
90 #define mathNUMBER_OF_TASKS ( 8 )
92 /* Four tasks, each of which performs a different floating point calculation.
93 Each of the four is created twice. */
94 static void vCompetingMathTask1( void *pvParameters );
95 static void vCompetingMathTask2( void *pvParameters );
96 static void vCompetingMathTask3( void *pvParameters );
97 static void vCompetingMathTask4( void *pvParameters );
99 /* These variables are used to check that all the tasks are still running. If a
100 task gets a calculation wrong it will
101 stop incrementing its check variable. */
102 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
104 /*-----------------------------------------------------------*/
106 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
108 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
109 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
110 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
111 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
112 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
113 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
114 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
115 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
117 /*-----------------------------------------------------------*/
119 static void vCompetingMathTask1( void *pvParameters )
121 portDOUBLE d1, d2, d3, d4;
122 volatile unsigned short *pusTaskCheckVariable;
123 const portDOUBLE dAnswer = ( 123.4567 + 2345.6789 ) * -918.222;
124 const char * const pcTaskStartMsg = "Math task 1 started.\r\n";
125 const char * const pcTaskFailMsg = "Math task 1 failed.\r\n";
126 short sError = pdFALSE;
128 /* Queue a message for printing to say the task has started. */
129 vPrintDisplayMessage( &pcTaskStartMsg );
131 /* The variable this task increments to show it is still running is passed in
132 as the parameter. */
133 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
135 /* Keep performing a calculation and checking the result against a constant. */
136 for(;;)
138 d1 = 123.4567;
139 d2 = 2345.6789;
140 d3 = -918.222;
142 d4 = ( d1 + d2 ) * d3;
144 taskYIELD();
146 /* If the calculation does not match the expected constant, stop the
147 increment of the check variable. */
148 if( fabs( d4 - dAnswer ) > 0.001 )
150 vPrintDisplayMessage( &pcTaskFailMsg );
151 sError = pdTRUE;
154 if( sError == pdFALSE )
156 /* If the calculation has always been correct, increment the check
157 variable so we know this task is still running okay. */
158 ( *pusTaskCheckVariable )++;
161 taskYIELD();
164 /*-----------------------------------------------------------*/
166 static void vCompetingMathTask2( void *pvParameters )
168 portDOUBLE d1, d2, d3, d4;
169 volatile unsigned short *pusTaskCheckVariable;
170 const portDOUBLE dAnswer = ( -389.38 / 32498.2 ) * -2.0001;
171 const char * const pcTaskStartMsg = "Math task 2 started.\r\n";
172 const char * const pcTaskFailMsg = "Math task 2 failed.\r\n";
173 short sError = pdFALSE;
175 /* Queue a message for printing to say the task has started. */
176 vPrintDisplayMessage( &pcTaskStartMsg );
178 /* The variable this task increments to show it is still running is passed in
179 as the parameter. */
180 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
182 /* Keep performing a calculation and checking the result against a constant. */
183 for( ;; )
185 d1 = -389.38;
186 d2 = 32498.2;
187 d3 = -2.0001;
189 d4 = ( d1 / d2 ) * d3;
191 taskYIELD();
193 /* If the calculation does not match the expected constant, stop the
194 increment of the check variable. */
195 if( fabs( d4 - dAnswer ) > 0.001 )
197 vPrintDisplayMessage( &pcTaskFailMsg );
198 sError = pdTRUE;
201 if( sError == pdFALSE )
203 /* If the calculation has always been correct, increment the check
204 variable so we know
205 this task is still running okay. */
206 ( *pusTaskCheckVariable )++;
209 taskYIELD();
212 /*-----------------------------------------------------------*/
214 static void vCompetingMathTask3( void *pvParameters )
216 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
217 volatile unsigned short *pusTaskCheckVariable;
218 const unsigned short usArraySize = 250;
219 unsigned short usPosition;
220 const char * const pcTaskStartMsg = "Math task 3 started.\r\n";
221 const char * const pcTaskFailMsg = "Math task 3 failed.\r\n";
222 short sError = pdFALSE;
224 /* Queue a message for printing to say the task has started. */
225 vPrintDisplayMessage( &pcTaskStartMsg );
227 /* The variable this task increments to show it is still running is passed in
228 as the parameter. */
229 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
231 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
233 /* Keep filling an array, keeping a running total of the values placed in the
234 array. Then run through the array adding up all the values. If the two totals
235 do not match, stop the check variable from incrementing. */
236 for( ;; )
238 dTotal1 = 0.0;
239 dTotal2 = 0.0;
241 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
243 pdArray[ usPosition ] = ( portDOUBLE ) usPosition + 5.5;
244 dTotal1 += ( portDOUBLE ) usPosition + 5.5;
247 taskYIELD();
249 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
251 dTotal2 += pdArray[ usPosition ];
254 dDifference = dTotal1 - dTotal2;
255 if( fabs( dDifference ) > 0.001 )
257 vPrintDisplayMessage( &pcTaskFailMsg );
258 sError = pdTRUE;
261 taskYIELD();
263 if( sError == pdFALSE )
265 /* If the calculation has always been correct, increment the check
266 variable so we know this task is still running okay. */
267 ( *pusTaskCheckVariable )++;
271 /*-----------------------------------------------------------*/
273 static void vCompetingMathTask4( void *pvParameters )
275 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
276 volatile unsigned short *pusTaskCheckVariable;
277 const unsigned short usArraySize = 250;
278 unsigned short usPosition;
279 const char * const pcTaskStartMsg = "Math task 4 started.\r\n";
280 const char * const pcTaskFailMsg = "Math task 4 failed.\r\n";
281 short sError = pdFALSE;
283 /* Queue a message for printing to say the task has started. */
284 vPrintDisplayMessage( &pcTaskStartMsg );
286 /* The variable this task increments to show it is still running is passed in
287 as the parameter. */
288 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
290 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
292 /* Keep filling an array, keeping a running total of the values placed in the
293 array. Then run through the array adding up all the values. If the two totals
294 do not match, stop the check variable from incrementing. */
295 for( ;; )
297 dTotal1 = 0.0;
298 dTotal2 = 0.0;
300 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
302 pdArray[ usPosition ] = ( portDOUBLE ) usPosition * 12.123;
303 dTotal1 += ( portDOUBLE ) usPosition * 12.123;
306 taskYIELD();
308 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
310 dTotal2 += pdArray[ usPosition ];
313 dDifference = dTotal1 - dTotal2;
314 if( fabs( dDifference ) > 0.001 )
316 vPrintDisplayMessage( &pcTaskFailMsg );
317 sError = pdTRUE;
320 taskYIELD();
322 if( sError == pdFALSE )
324 /* If the calculation has always been correct, increment the check
325 variable so we know this task is still running okay. */
326 ( *pusTaskCheckVariable )++;
330 /*-----------------------------------------------------------*/
332 /* This is called to check that all the created tasks are still running. */
333 portBASE_TYPE xAreMathsTaskStillRunning( void )
335 /* Keep a history of the check variables so we know if they have been incremented
336 since the last call. */
337 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
338 portBASE_TYPE xReturn = pdTRUE, xTask;
340 /* Check the maths tasks are still running by ensuring their check variables
341 are still incrementing. */
342 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
344 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
346 /* The check has not incremented so an error exists. */
347 xReturn = pdFALSE;
350 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
353 return xReturn;