]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/flop.c
d51bf4520d80ddea0b9d32856a7940b284b7583e
[freertos] / Demo / Common / Minimal / flop.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\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
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\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
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \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
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  * Creates eight tasks, each of which loops continuously performing an (emulated) \r
56  * floating point calculation.\r
57  *\r
58  * All the tasks run at the idle priority and never block or yield.  This causes \r
59  * all eight tasks to time slice with the idle task.  Running at the idle priority \r
60  * means that these tasks will get pre-empted any time another task is ready to run\r
61  * or a time slice occurs.  More often than not the pre-emption will occur mid \r
62  * calculation, creating a good test of the schedulers context switch mechanism - a \r
63  * calculation producing an unexpected result could be a symptom of a corruption in \r
64  * the context of a task.\r
65  */\r
66 \r
67 #include <stdlib.h>\r
68 #include <math.h>\r
69 \r
70 /* Scheduler include files. */\r
71 #include "FreeRTOS.h"\r
72 #include "task.h"\r
73 \r
74 /* Demo program include files. */\r
75 #include "flop.h"\r
76 \r
77 #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE\r
78 #define mathNUMBER_OF_TASKS  ( 8 )\r
79 \r
80 /* Four tasks, each of which performs a different floating point calculation.  \r
81 Each of the four is created twice. */\r
82 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );\r
83 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );\r
84 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );\r
85 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );\r
86 \r
87 /* These variables are used to check that all the tasks are still running.  If a \r
88 task gets a calculation wrong it will\r
89 stop incrementing its check variable. */\r
90 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
95 {\r
96         xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
97         xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
98         xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
99         xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
100         xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );\r
101         xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );\r
102         xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );\r
103         xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );\r
104 }\r
105 /*-----------------------------------------------------------*/\r
106 \r
107 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
108 {\r
109 volatile portDOUBLE d1, d2, d3, d4;\r
110 volatile unsigned short *pusTaskCheckVariable;\r
111 volatile portDOUBLE dAnswer;\r
112 short sError = pdFALSE;\r
113 \r
114         d1 = 123.4567;\r
115         d2 = 2345.6789;\r
116         d3 = -918.222;\r
117 \r
118         dAnswer = ( d1 + d2 ) * d3;\r
119 \r
120         /* The variable this task increments to show it is still running is passed in \r
121         as the parameter. */\r
122         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
123 \r
124         /* Keep performing a calculation and checking the result against a constant. */\r
125         for(;;)\r
126         {\r
127                 d1 = 123.4567;\r
128                 d2 = 2345.6789;\r
129                 d3 = -918.222;\r
130 \r
131                 d4 = ( d1 + d2 ) * d3;\r
132 \r
133                 #if configUSE_PREEMPTION == 0\r
134                         taskYIELD();\r
135                 #endif\r
136 \r
137                 /* If the calculation does not match the expected constant, stop the \r
138                 increment of the check variable. */\r
139                 if( fabs( d4 - dAnswer ) > 0.001 )\r
140                 {\r
141                         sError = pdTRUE;\r
142                 }\r
143 \r
144                 if( sError == pdFALSE )\r
145                 {\r
146                         /* If the calculation has always been correct, increment the check \r
147                         variable so we know this task is still running okay. */\r
148                         ( *pusTaskCheckVariable )++;\r
149                 }\r
150 \r
151                 #if configUSE_PREEMPTION == 0\r
152                         taskYIELD();\r
153                 #endif\r
154 \r
155         }\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
160 {\r
161 volatile portDOUBLE d1, d2, d3, d4;\r
162 volatile unsigned short *pusTaskCheckVariable;\r
163 volatile portDOUBLE dAnswer;\r
164 short sError = pdFALSE;\r
165 \r
166         d1 = -389.38;\r
167         d2 = 32498.2;\r
168         d3 = -2.0001;\r
169 \r
170         dAnswer = ( d1 / d2 ) * d3;\r
171 \r
172 \r
173         /* The variable this task increments to show it is still running is passed in \r
174         as the parameter. */\r
175         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
176 \r
177         /* Keep performing a calculation and checking the result against a constant. */\r
178         for( ;; )\r
179         {\r
180                 d1 = -389.38;\r
181                 d2 = 32498.2;\r
182                 d3 = -2.0001;\r
183 \r
184                 d4 = ( d1 / d2 ) * d3;\r
185 \r
186                 #if configUSE_PREEMPTION == 0\r
187                         taskYIELD();\r
188                 #endif\r
189                 \r
190                 /* If the calculation does not match the expected constant, stop the \r
191                 increment of the check variable. */\r
192                 if( fabs( d4 - dAnswer ) > 0.001 )\r
193                 {\r
194                         sError = pdTRUE;\r
195                 }\r
196 \r
197                 if( sError == pdFALSE )\r
198                 {\r
199                         /* If the calculation has always been correct, increment the check \r
200                         variable so we know\r
201                         this task is still running okay. */\r
202                         ( *pusTaskCheckVariable )++;\r
203                 }\r
204 \r
205                 #if configUSE_PREEMPTION == 0\r
206                         taskYIELD();\r
207                 #endif\r
208         }\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
213 {\r
214 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
215 volatile unsigned short *pusTaskCheckVariable;\r
216 const size_t xArraySize = 10;\r
217 size_t xPosition;\r
218 short sError = pdFALSE;\r
219 \r
220         /* The variable this task increments to show it is still running is passed in \r
221         as the parameter. */\r
222         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
223 \r
224         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
225 \r
226         /* Keep filling an array, keeping a running total of the values placed in the \r
227         array.  Then run through the array adding up all the values.  If the two totals \r
228         do not match, stop the check variable from incrementing. */\r
229         for( ;; )\r
230         {\r
231                 dTotal1 = 0.0;\r
232                 dTotal2 = 0.0;\r
233 \r
234                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
235                 {\r
236                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;\r
237                         dTotal1 += ( portDOUBLE ) xPosition + 5.5;      \r
238                 }\r
239 \r
240                 #if configUSE_PREEMPTION == 0\r
241                         taskYIELD();\r
242                 #endif\r
243 \r
244                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
245                 {\r
246                         dTotal2 += pdArray[ xPosition ];\r
247                 }\r
248 \r
249                 dDifference = dTotal1 - dTotal2;\r
250                 if( fabs( dDifference ) > 0.001 )\r
251                 {\r
252                         sError = pdTRUE;\r
253                 }\r
254 \r
255                 #if configUSE_PREEMPTION == 0\r
256                         taskYIELD();\r
257                 #endif\r
258 \r
259                 if( sError == pdFALSE )\r
260                 {\r
261                         /* If the calculation has always been correct, increment the check \r
262                         variable so we know     this task is still running okay. */\r
263                         ( *pusTaskCheckVariable )++;\r
264                 }\r
265         }\r
266 }\r
267 /*-----------------------------------------------------------*/\r
268 \r
269 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
270 {\r
271 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
272 volatile unsigned short *pusTaskCheckVariable;\r
273 const size_t xArraySize = 10;\r
274 size_t xPosition;\r
275 short sError = pdFALSE;\r
276 \r
277         /* The variable this task increments to show it is still running is passed in \r
278         as the parameter. */\r
279         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
280 \r
281         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
282 \r
283         /* Keep filling an array, keeping a running total of the values placed in the \r
284         array.  Then run through the array adding up all the values.  If the two totals \r
285         do not match, stop the check variable from incrementing. */\r
286         for( ;; )\r
287         {\r
288                 dTotal1 = 0.0;\r
289                 dTotal2 = 0.0;\r
290 \r
291                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
292                 {\r
293                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;\r
294                         dTotal1 += ( portDOUBLE ) xPosition * 12.123;   \r
295                 }\r
296 \r
297                 #if configUSE_PREEMPTION == 0\r
298                         taskYIELD();\r
299                 #endif\r
300 \r
301                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
302                 {\r
303                         dTotal2 += pdArray[ xPosition ];\r
304                 }\r
305 \r
306                 dDifference = dTotal1 - dTotal2;\r
307                 if( fabs( dDifference ) > 0.001 )\r
308                 {\r
309                         sError = pdTRUE;\r
310                 }\r
311 \r
312                 #if configUSE_PREEMPTION == 0\r
313                         taskYIELD();\r
314                 #endif\r
315 \r
316                 if( sError == pdFALSE )\r
317                 {\r
318                         /* If the calculation has always been correct, increment the check \r
319                         variable so we know     this task is still running okay. */\r
320                         ( *pusTaskCheckVariable )++;\r
321                 }\r
322         }\r
323 }                                \r
324 /*-----------------------------------------------------------*/\r
325 \r
326 /* This is called to check that all the created tasks are still running. */\r
327 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
328 {\r
329 /* Keep a history of the check variables so we know if they have been incremented \r
330 since the last call. */\r
331 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
332 portBASE_TYPE xReturn = pdTRUE, xTask;\r
333 \r
334         /* Check the maths tasks are still running by ensuring their check variables \r
335         are still incrementing. */\r
336         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
337         {\r
338                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
339                 {\r
340                         /* The check has not incremented so an error exists. */\r
341                         xReturn = pdFALSE;\r
342                 }\r
343 \r
344                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
345         }\r
346 \r
347         return xReturn;\r
348 }\r
349 \r
350 \r
351 \r