]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/flop.c
6b3d0f2c7638851bcb56fbbf622208e3180a4cac
[freertos] / FreeRTOS / Demo / Common / Minimal / flop.c
1 /*\r
2     FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67  * Creates eight tasks, each of which loops continuously performing a floating \r
68  * point calculation.\r
69  *\r
70  * All the tasks run at the idle priority and never block or yield.  This causes\r
71  * all eight tasks to time slice with the idle task.  Running at the idle \r
72  * priority means that these tasks will get pre-empted any time another task is \r
73  * ready to run or a time slice occurs.  More often than not the pre-emption \r
74  * will occur mid calculation, creating a good test of the schedulers context \r
75  * switch mechanism - a calculation producing an unexpected result could be a \r
76  * symptom of a corruption in the context of a task.\r
77  */\r
78 \r
79 #include <stdlib.h>\r
80 #include <math.h>\r
81 \r
82 /* Scheduler include files. */\r
83 #include "FreeRTOS.h"\r
84 #include "task.h"\r
85 \r
86 /* Demo program include files. */\r
87 #include "flop.h"\r
88 \r
89 #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE\r
90 #define mathNUMBER_OF_TASKS  ( 4 )\r
91 \r
92 /* Four tasks, each of which performs a different floating point calculation.\r
93 Each of the four is created twice. */\r
94 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );\r
95 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );\r
96 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );\r
97 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );\r
98 \r
99 /* These variables are used to check that all the tasks are still running.  If a\r
100 task gets a calculation wrong it will stop setting its check variable. */\r
101 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
106 {\r
107         xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
108         xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
109         xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
110         xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
111 }\r
112 /*-----------------------------------------------------------*/\r
113 \r
114 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
115 {\r
116 volatile portDOUBLE d1, d2, d3, d4;\r
117 volatile unsigned short *pusTaskCheckVariable;\r
118 volatile portDOUBLE dAnswer;\r
119 short sError = pdFALSE;\r
120 \r
121         /* Some ports require that tasks that use a hardware floating point unit\r
122         tell the kernel that they require a floating point context before any\r
123         floating point instructions are executed. */\r
124         portTASK_USES_FLOATING_POINT();\r
125 \r
126         d1 = 123.4567;\r
127         d2 = 2345.6789;\r
128         d3 = -918.222;\r
129 \r
130         dAnswer = ( d1 + d2 ) * d3;\r
131 \r
132         /* The variable this task increments to show it is still running is passed in\r
133         as the parameter. */\r
134         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
135 \r
136         /* Keep performing a calculation and checking the result against a constant. */\r
137         for(;;)\r
138         {\r
139                 d1 = 123.4567;\r
140                 d2 = 2345.6789;\r
141                 d3 = -918.222;\r
142 \r
143                 d4 = ( d1 + d2 ) * d3;\r
144 \r
145                 #if configUSE_PREEMPTION == 0\r
146                         taskYIELD();\r
147                 #endif\r
148 \r
149                 /* If the calculation does not match the expected constant, stop the\r
150                 increment of the check variable. */\r
151                 if( fabs( d4 - dAnswer ) > 0.001 )\r
152                 {\r
153                         sError = pdTRUE;\r
154                 }\r
155 \r
156                 if( sError == pdFALSE )\r
157                 {\r
158                         /* If the calculation has always been correct then set set the check\r
159                         variable.  The check variable will get set to pdFALSE each time\r
160                         xAreMathsTaskStillRunning() is executed. */\r
161                         ( *pusTaskCheckVariable ) = pdTRUE;\r
162                 }\r
163 \r
164                 #if configUSE_PREEMPTION == 0\r
165                         taskYIELD();\r
166                 #endif\r
167 \r
168         }\r
169 }\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
173 {\r
174 volatile portDOUBLE d1, d2, d3, d4;\r
175 volatile unsigned short *pusTaskCheckVariable;\r
176 volatile portDOUBLE dAnswer;\r
177 short sError = pdFALSE;\r
178 \r
179         /* Some ports require that tasks that use a hardware floating point unit\r
180         tell the kernel that they require a floating point context before any\r
181         floating point instructions are executed. */\r
182         portTASK_USES_FLOATING_POINT();\r
183 \r
184         d1 = -389.38;\r
185         d2 = 32498.2;\r
186         d3 = -2.0001;\r
187 \r
188         dAnswer = ( d1 / d2 ) * d3;\r
189 \r
190 \r
191         /* The variable this task increments to show it is still running is passed in\r
192         as the parameter. */\r
193         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
194 \r
195         /* Keep performing a calculation and checking the result against a constant. */\r
196         for( ;; )\r
197         {\r
198                 d1 = -389.38;\r
199                 d2 = 32498.2;\r
200                 d3 = -2.0001;\r
201 \r
202                 d4 = ( d1 / d2 ) * d3;\r
203 \r
204                 #if configUSE_PREEMPTION == 0\r
205                         taskYIELD();\r
206                 #endif\r
207 \r
208                 /* If the calculation does not match the expected constant, stop the\r
209                 increment of the check variable. */\r
210                 if( fabs( d4 - dAnswer ) > 0.001 )\r
211                 {\r
212                         sError = pdTRUE;\r
213                 }\r
214 \r
215                 if( sError == pdFALSE )\r
216                 {\r
217                         /* If the calculation has always been correct then set set the check\r
218                         variable.  The check variable will get set to pdFALSE each time\r
219                         xAreMathsTaskStillRunning() is executed. */\r
220                         ( *pusTaskCheckVariable ) = pdTRUE;\r
221                 }\r
222 \r
223                 #if configUSE_PREEMPTION == 0\r
224                         taskYIELD();\r
225                 #endif\r
226         }\r
227 }\r
228 /*-----------------------------------------------------------*/\r
229 \r
230 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
231 {\r
232 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
233 volatile unsigned short *pusTaskCheckVariable;\r
234 const size_t xArraySize = 10;\r
235 size_t xPosition;\r
236 short sError = pdFALSE;\r
237 \r
238         /* Some ports require that tasks that use a hardware floating point unit\r
239         tell the kernel that they require a floating point context before any\r
240         floating point instructions are executed. */\r
241         portTASK_USES_FLOATING_POINT();\r
242 \r
243         /* The variable this task increments to show it is still running is passed in\r
244         as the parameter. */\r
245         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
246 \r
247         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
248 \r
249         /* Keep filling an array, keeping a running total of the values placed in the\r
250         array.  Then run through the array adding up all the values.  If the two totals\r
251         do not match, stop the check variable from incrementing. */\r
252         for( ;; )\r
253         {\r
254                 dTotal1 = 0.0;\r
255                 dTotal2 = 0.0;\r
256 \r
257                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
258                 {\r
259                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;\r
260                         dTotal1 += ( portDOUBLE ) xPosition + 5.5;\r
261                 }\r
262 \r
263                 #if configUSE_PREEMPTION == 0\r
264                         taskYIELD();\r
265                 #endif\r
266 \r
267                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
268                 {\r
269                         dTotal2 += pdArray[ xPosition ];\r
270                 }\r
271 \r
272                 dDifference = dTotal1 - dTotal2;\r
273                 if( fabs( dDifference ) > 0.001 )\r
274                 {\r
275                         sError = pdTRUE;\r
276                 }\r
277 \r
278                 #if configUSE_PREEMPTION == 0\r
279                         taskYIELD();\r
280                 #endif\r
281 \r
282                 if( sError == pdFALSE )\r
283                 {\r
284                         /* If the calculation has always been correct then set set the check\r
285                         variable.  The check variable will get set to pdFALSE each time\r
286                         xAreMathsTaskStillRunning() is executed. */\r
287                         ( *pusTaskCheckVariable ) = pdTRUE;\r
288                 }\r
289         }\r
290 }\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
294 {\r
295 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
296 volatile unsigned short *pusTaskCheckVariable;\r
297 const size_t xArraySize = 10;\r
298 size_t xPosition;\r
299 short sError = pdFALSE;\r
300 \r
301         /* Some ports require that tasks that use a hardware floating point unit\r
302         tell the kernel that they require a floating point context before any\r
303         floating point instructions are executed. */\r
304         portTASK_USES_FLOATING_POINT();\r
305 \r
306         /* The variable this task increments to show it is still running is passed in\r
307         as the parameter. */\r
308         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
309 \r
310         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
311 \r
312         /* Keep filling an array, keeping a running total of the values placed in the\r
313         array.  Then run through the array adding up all the values.  If the two totals\r
314         do not match, stop the check variable from incrementing. */\r
315         for( ;; )\r
316         {\r
317                 dTotal1 = 0.0;\r
318                 dTotal2 = 0.0;\r
319 \r
320                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
321                 {\r
322                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;\r
323                         dTotal1 += ( portDOUBLE ) xPosition * 12.123;\r
324                 }\r
325 \r
326                 #if configUSE_PREEMPTION == 0\r
327                         taskYIELD();\r
328                 #endif\r
329 \r
330                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
331                 {\r
332                         dTotal2 += pdArray[ xPosition ];\r
333                 }\r
334 \r
335                 dDifference = dTotal1 - dTotal2;\r
336                 if( fabs( dDifference ) > 0.001 )\r
337                 {\r
338                         sError = pdTRUE;\r
339                 }\r
340 \r
341                 #if configUSE_PREEMPTION == 0\r
342                         taskYIELD();\r
343                 #endif\r
344 \r
345                 if( sError == pdFALSE )\r
346                 {\r
347                         /* If the calculation has always been correct then set set the check\r
348                         variable.  The check variable will get set to pdFALSE each time\r
349                         xAreMathsTaskStillRunning() is executed. */\r
350                         ( *pusTaskCheckVariable ) = pdTRUE;\r
351                 }\r
352         }\r
353 }\r
354 /*-----------------------------------------------------------*/\r
355 \r
356 /* This is called to check that all the created tasks are still running. */\r
357 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
358 {\r
359 portBASE_TYPE xReturn = pdPASS, xTask;\r
360 \r
361         /* Check the maths tasks are still running by ensuring their check variables\r
362         have been set to pdPASS. */\r
363         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
364         {\r
365                 if( usTaskCheck[ xTask ] != pdTRUE )\r
366                 {\r
367                         /* The check has not been set so the associated task has either\r
368                         stalled or detected an error. */\r
369                         xReturn = pdFAIL;\r
370                 }\r
371                 else\r
372                 {\r
373                         /* Reset the variable so it can be checked again the next time this\r
374                         function is executed. */\r
375                         usTaskCheck[ xTask ] = pdFALSE;\r
376                 }\r
377         }\r
378                 \r
379         return xReturn;\r
380 }\r
381 \r
382 \r
383 \r