]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/sp_flop.c
***IMMINENT RELEASE NOTICE***
[freertos] / FreeRTOS / Demo / Common / Minimal / sp_flop.c
1 /*\r
2     FreeRTOS V8.1.0 - 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     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS 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 - using single precision variables.\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 priority \r
72  * means that these tasks will get pre-empted any time another task is ready to run\r
73  * or a time slice occurs.  More often than not the pre-emption will occur mid \r
74  * calculation, creating a good test of the schedulers context switch mechanism - a \r
75  * calculation producing an unexpected result could be a symptom of a corruption in \r
76  * 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  ( 8 )\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\r
101 stop incrementing its check variable. */\r
102 static volatile uint16_t usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( uint16_t ) 0 };\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 void vStartMathTasks( UBaseType_t uxPriority )\r
107 {\r
108         xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
109         xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
110         xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
111         xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
112         xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );\r
113         xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );\r
114         xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );\r
115         xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );\r
116 }\r
117 /*-----------------------------------------------------------*/\r
118 \r
119 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
120 {\r
121 volatile float f1, f2, f3, f4;\r
122 volatile uint16_t *pusTaskCheckVariable;\r
123 volatile float fAnswer;\r
124 short sError = pdFALSE;\r
125 \r
126         f1 = 123.4567F;\r
127         f2 = 2345.6789F;\r
128         f3 = -918.222F;\r
129 \r
130         fAnswer = ( f1 + f2 ) * f3;\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 = ( uint16_t * ) pvParameters;\r
135 \r
136         /* Keep performing a calculation and checking the result against a constant. */\r
137         for(;;)\r
138         {\r
139                 f1 = 123.4567F;\r
140                 f2 = 2345.6789F;\r
141                 f3 = -918.222F;\r
142 \r
143                 f4 = ( f1 + f2 ) * f3;\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( f4 - fAnswer ) > 0.001F )\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, increment the check \r
159                         variable so we know this task is still running okay. */\r
160                         ( *pusTaskCheckVariable )++;\r
161                 }\r
162 \r
163                 #if configUSE_PREEMPTION == 0\r
164                         taskYIELD();\r
165                 #endif\r
166 \r
167         }\r
168 }\r
169 /*-----------------------------------------------------------*/\r
170 \r
171 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
172 {\r
173 volatile float f1, f2, f3, f4;\r
174 volatile uint16_t *pusTaskCheckVariable;\r
175 volatile float fAnswer;\r
176 short sError = pdFALSE;\r
177 \r
178         f1 = -389.38F;\r
179         f2 = 32498.2F;\r
180         f3 = -2.0001F;\r
181 \r
182         fAnswer = ( f1 / f2 ) * f3;\r
183 \r
184 \r
185         /* The variable this task increments to show it is still running is passed in \r
186         as the parameter. */\r
187         pusTaskCheckVariable = ( uint16_t * ) pvParameters;\r
188 \r
189         /* Keep performing a calculation and checking the result against a constant. */\r
190         for( ;; )\r
191         {\r
192                 f1 = -389.38F;\r
193                 f2 = 32498.2F;\r
194                 f3 = -2.0001F;\r
195 \r
196                 f4 = ( f1 / f2 ) * f3;\r
197 \r
198                 #if configUSE_PREEMPTION == 0\r
199                         taskYIELD();\r
200                 #endif\r
201                 \r
202                 /* If the calculation does not match the expected constant, stop the \r
203                 increment of the check variable. */\r
204                 if( fabs( f4 - fAnswer ) > 0.001F )\r
205                 {\r
206                         sError = pdTRUE;\r
207                 }\r
208 \r
209                 if( sError == pdFALSE )\r
210                 {\r
211                         /* If the calculation has always been correct, increment the check \r
212                         variable so we know\r
213                         this task is still running okay. */\r
214                         ( *pusTaskCheckVariable )++;\r
215                 }\r
216 \r
217                 #if configUSE_PREEMPTION == 0\r
218                         taskYIELD();\r
219                 #endif\r
220         }\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
225 {\r
226 volatile float *pfArray, fTotal1, fTotal2, fDifference, fPosition;\r
227 volatile uint16_t *pusTaskCheckVariable;\r
228 const size_t xArraySize = 10;\r
229 size_t xPosition;\r
230 short sError = pdFALSE;\r
231 \r
232         /* The variable this task increments to show it is still running is passed in \r
233         as the parameter. */\r
234         pusTaskCheckVariable = ( uint16_t * ) pvParameters;\r
235 \r
236         pfArray = ( float * ) pvPortMalloc( xArraySize * sizeof( float ) );\r
237 \r
238         /* Keep filling an array, keeping a running total of the values placed in the \r
239         array.  Then run through the array adding up all the values.  If the two totals \r
240         do not match, stop the check variable from incrementing. */\r
241         for( ;; )\r
242         {\r
243                 fTotal1 = 0.0F;\r
244                 fTotal2 = 0.0F;\r
245                 fPosition = 0.0F;\r
246                 \r
247                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
248                 {\r
249                         pfArray[ xPosition ] = fPosition + 5.5F;\r
250                         fTotal1 += fPosition + 5.5F;    \r
251                 }\r
252 \r
253                 #if configUSE_PREEMPTION == 0\r
254                         taskYIELD();\r
255                 #endif\r
256 \r
257                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
258                 {\r
259                         fTotal2 += pfArray[ xPosition ];\r
260                 }\r
261 \r
262                 fDifference = fTotal1 - fTotal2;\r
263                 if( fabs( fDifference ) > 0.001F )\r
264                 {\r
265                         sError = pdTRUE;\r
266                 }\r
267 \r
268                 #if configUSE_PREEMPTION == 0\r
269                         taskYIELD();\r
270                 #endif\r
271 \r
272                 if( sError == pdFALSE )\r
273                 {\r
274                         /* If the calculation has always been correct, increment the check \r
275                         variable so we know     this task is still running okay. */\r
276                         ( *pusTaskCheckVariable )++;\r
277                 }\r
278         }\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
283 {\r
284 volatile float *pfArray, fTotal1, fTotal2, fDifference, fPosition;\r
285 volatile uint16_t *pusTaskCheckVariable;\r
286 const size_t xArraySize = 10;\r
287 size_t xPosition;\r
288 short sError = pdFALSE;\r
289 \r
290         /* The variable this task increments to show it is still running is passed in \r
291         as the parameter. */\r
292         pusTaskCheckVariable = ( uint16_t * ) pvParameters;\r
293 \r
294         pfArray = ( float * ) pvPortMalloc( xArraySize * sizeof( float ) );\r
295 \r
296         /* Keep filling an array, keeping a running total of the values placed in the \r
297         array.  Then run through the array adding up all the values.  If the two totals \r
298         do not match, stop the check variable from incrementing. */\r
299         for( ;; )\r
300         {\r
301                 fTotal1 = 0.0F;\r
302                 fTotal2 = 0.0F;\r
303                 fPosition = 0.0F;\r
304 \r
305                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
306                 {\r
307                         pfArray[ xPosition ] = fPosition * 12.123F;\r
308                         fTotal1 += fPosition * 12.123F; \r
309                 }\r
310 \r
311                 #if configUSE_PREEMPTION == 0\r
312                         taskYIELD();\r
313                 #endif\r
314 \r
315                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
316                 {\r
317                         fTotal2 += pfArray[ xPosition ];\r
318                 }\r
319 \r
320                 fDifference = fTotal1 - fTotal2;\r
321                 if( fabs( fDifference ) > 0.001F )\r
322                 {\r
323                         sError = pdTRUE;\r
324                 }\r
325 \r
326                 #if configUSE_PREEMPTION == 0\r
327                         taskYIELD();\r
328                 #endif\r
329 \r
330                 if( sError == pdFALSE )\r
331                 {\r
332                         /* If the calculation has always been correct, increment the check \r
333                         variable so we know     this task is still running okay. */\r
334                         ( *pusTaskCheckVariable )++;\r
335                 }\r
336         }\r
337 }                                \r
338 /*-----------------------------------------------------------*/\r
339 \r
340 /* This is called to check that all the created tasks are still running. */\r
341 BaseType_t xAreMathsTaskStillRunning( void )\r
342 {\r
343 /* Keep a history of the check variables so we know if they have been incremented \r
344 since the last call. */\r
345 static uint16_t usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( uint16_t ) 0 };\r
346 BaseType_t xReturn = pdTRUE, xTask;\r
347 \r
348         /* Check the maths tasks are still running by ensuring their check variables \r
349         are still incrementing. */\r
350         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
351         {\r
352                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
353                 {\r
354                         /* The check has not incremented so an error exists. */\r
355                         xReturn = pdFALSE;\r
356                 }\r
357 \r
358                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
359         }\r
360 \r
361         return xReturn;\r
362 }\r
363 \r
364 \r
365 \r