]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/sp_flop.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / Common / Minimal / sp_flop.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30  * Creates eight tasks, each of which loops continuously performing a floating\r
31  * point calculation - using single precision variables.\r
32  *\r
33  * All the tasks run at the idle priority and never block or yield.  This causes\r
34  * all eight tasks to time slice with the idle task.  Running at the idle priority\r
35  * means that these tasks will get pre-empted any time another task is ready to run\r
36  * or a time slice occurs.  More often than not the pre-emption will occur mid\r
37  * calculation, creating a good test of the schedulers context switch mechanism - a\r
38  * calculation producing an unexpected result could be a symptom of a corruption in\r
39  * the context of a task.\r
40  */\r
41 \r
42 #include <stdlib.h>\r
43 #include <math.h>\r
44 \r
45 /* Scheduler include files. */\r
46 #include "FreeRTOS.h"\r
47 #include "task.h"\r
48 \r
49 /* Demo program include files. */\r
50 #include "flop.h"\r
51 \r
52 #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE\r
53 #define mathNUMBER_OF_TASKS  ( 8 )\r
54 \r
55 /* Four tasks, each of which performs a different floating point calculation.\r
56 Each of the four is created twice. */\r
57 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );\r
58 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );\r
59 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );\r
60 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );\r
61 \r
62 /* These variables are used to check that all the tasks are still running.  If a\r
63 task gets a calculation wrong it will\r
64 stop incrementing its check variable. */\r
65 static volatile uint16_t usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( uint16_t ) 0 };\r
66 \r
67 /*-----------------------------------------------------------*/\r
68 \r
69 void vStartMathTasks( UBaseType_t uxPriority )\r
70 {\r
71         xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
72         xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
73         xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
74         xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
75         xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );\r
76         xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );\r
77         xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );\r
78         xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );\r
79 }\r
80 /*-----------------------------------------------------------*/\r
81 \r
82 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
83 {\r
84 volatile float f1, f2, f3, f4;\r
85 volatile uint16_t *pusTaskCheckVariable;\r
86 volatile float fAnswer;\r
87 short sError = pdFALSE;\r
88 \r
89         f1 = 123.4567F;\r
90         f2 = 2345.6789F;\r
91         f3 = -918.222F;\r
92 \r
93         fAnswer = ( f1 + f2 ) * f3;\r
94 \r
95         /* The variable this task increments to show it is still running is passed in\r
96         as the parameter. */\r
97         pusTaskCheckVariable = ( uint16_t * ) pvParameters;\r
98 \r
99         /* Keep performing a calculation and checking the result against a constant. */\r
100         for(;;)\r
101         {\r
102                 f1 = 123.4567F;\r
103                 f2 = 2345.6789F;\r
104                 f3 = -918.222F;\r
105 \r
106                 f4 = ( f1 + f2 ) * f3;\r
107 \r
108                 #if configUSE_PREEMPTION == 0\r
109                         taskYIELD();\r
110                 #endif\r
111 \r
112                 /* If the calculation does not match the expected constant, stop the\r
113                 increment of the check variable. */\r
114                 if( fabs( f4 - fAnswer ) > 0.001F )\r
115                 {\r
116                         sError = pdTRUE;\r
117                 }\r
118 \r
119                 if( sError == pdFALSE )\r
120                 {\r
121                         /* If the calculation has always been correct, increment the check\r
122                         variable so we know this task is still running okay. */\r
123                         ( *pusTaskCheckVariable )++;\r
124                 }\r
125 \r
126                 #if configUSE_PREEMPTION == 0\r
127                         taskYIELD();\r
128                 #endif\r
129 \r
130         }\r
131 }\r
132 /*-----------------------------------------------------------*/\r
133 \r
134 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
135 {\r
136 volatile float f1, f2, f3, f4;\r
137 volatile uint16_t *pusTaskCheckVariable;\r
138 volatile float fAnswer;\r
139 short sError = pdFALSE;\r
140 \r
141         f1 = -389.38F;\r
142         f2 = 32498.2F;\r
143         f3 = -2.0001F;\r
144 \r
145         fAnswer = ( f1 / f2 ) * f3;\r
146 \r
147 \r
148         /* The variable this task increments to show it is still running is passed in\r
149         as the parameter. */\r
150         pusTaskCheckVariable = ( uint16_t * ) pvParameters;\r
151 \r
152         /* Keep performing a calculation and checking the result against a constant. */\r
153         for( ;; )\r
154         {\r
155                 f1 = -389.38F;\r
156                 f2 = 32498.2F;\r
157                 f3 = -2.0001F;\r
158 \r
159                 f4 = ( f1 / f2 ) * f3;\r
160 \r
161                 #if configUSE_PREEMPTION == 0\r
162                         taskYIELD();\r
163                 #endif\r
164 \r
165                 /* If the calculation does not match the expected constant, stop the\r
166                 increment of the check variable. */\r
167                 if( fabs( f4 - fAnswer ) > 0.001F )\r
168                 {\r
169                         sError = pdTRUE;\r
170                 }\r
171 \r
172                 if( sError == pdFALSE )\r
173                 {\r
174                         /* If the calculation has always been correct, increment the check\r
175                         variable so we know\r
176                         this task is still running okay. */\r
177                         ( *pusTaskCheckVariable )++;\r
178                 }\r
179 \r
180                 #if configUSE_PREEMPTION == 0\r
181                         taskYIELD();\r
182                 #endif\r
183         }\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
188 {\r
189 volatile float *pfArray, fTotal1, fTotal2, fDifference, fPosition;\r
190 volatile uint16_t *pusTaskCheckVariable;\r
191 const size_t xArraySize = 10;\r
192 size_t xPosition;\r
193 short sError = pdFALSE;\r
194 \r
195         /* The variable this task increments to show it is still running is passed in\r
196         as the parameter. */\r
197         pusTaskCheckVariable = ( uint16_t * ) pvParameters;\r
198 \r
199         pfArray = ( float * ) pvPortMalloc( xArraySize * sizeof( float ) );\r
200 \r
201         /* Keep filling an array, keeping a running total of the values placed in the\r
202         array.  Then run through the array adding up all the values.  If the two totals\r
203         do not match, stop the check variable from incrementing. */\r
204         for( ;; )\r
205         {\r
206                 fTotal1 = 0.0F;\r
207                 fTotal2 = 0.0F;\r
208                 fPosition = 0.0F;\r
209 \r
210                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
211                 {\r
212                         pfArray[ xPosition ] = fPosition + 5.5F;\r
213                         fTotal1 += fPosition + 5.5F;\r
214                 }\r
215 \r
216                 #if configUSE_PREEMPTION == 0\r
217                         taskYIELD();\r
218                 #endif\r
219 \r
220                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
221                 {\r
222                         fTotal2 += pfArray[ xPosition ];\r
223                 }\r
224 \r
225                 fDifference = fTotal1 - fTotal2;\r
226                 if( fabs( fDifference ) > 0.001F )\r
227                 {\r
228                         sError = pdTRUE;\r
229                 }\r
230 \r
231                 #if configUSE_PREEMPTION == 0\r
232                         taskYIELD();\r
233                 #endif\r
234 \r
235                 if( sError == pdFALSE )\r
236                 {\r
237                         /* If the calculation has always been correct, increment the check\r
238                         variable so we know     this task is still running okay. */\r
239                         ( *pusTaskCheckVariable )++;\r
240                 }\r
241         }\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
246 {\r
247 volatile float *pfArray, fTotal1, fTotal2, fDifference, fPosition;\r
248 volatile uint16_t *pusTaskCheckVariable;\r
249 const size_t xArraySize = 10;\r
250 size_t xPosition;\r
251 short sError = pdFALSE;\r
252 \r
253         /* The variable this task increments to show it is still running is passed in\r
254         as the parameter. */\r
255         pusTaskCheckVariable = ( uint16_t * ) pvParameters;\r
256 \r
257         pfArray = ( float * ) pvPortMalloc( xArraySize * sizeof( float ) );\r
258 \r
259         /* Keep filling an array, keeping a running total of the values placed in the\r
260         array.  Then run through the array adding up all the values.  If the two totals\r
261         do not match, stop the check variable from incrementing. */\r
262         for( ;; )\r
263         {\r
264                 fTotal1 = 0.0F;\r
265                 fTotal2 = 0.0F;\r
266                 fPosition = 0.0F;\r
267 \r
268                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
269                 {\r
270                         pfArray[ xPosition ] = fPosition * 12.123F;\r
271                         fTotal1 += fPosition * 12.123F;\r
272                 }\r
273 \r
274                 #if configUSE_PREEMPTION == 0\r
275                         taskYIELD();\r
276                 #endif\r
277 \r
278                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
279                 {\r
280                         fTotal2 += pfArray[ xPosition ];\r
281                 }\r
282 \r
283                 fDifference = fTotal1 - fTotal2;\r
284                 if( fabs( fDifference ) > 0.001F )\r
285                 {\r
286                         sError = pdTRUE;\r
287                 }\r
288 \r
289                 #if configUSE_PREEMPTION == 0\r
290                         taskYIELD();\r
291                 #endif\r
292 \r
293                 if( sError == pdFALSE )\r
294                 {\r
295                         /* If the calculation has always been correct, increment the check\r
296                         variable so we know     this task is still running okay. */\r
297                         ( *pusTaskCheckVariable )++;\r
298                 }\r
299         }\r
300 }\r
301 /*-----------------------------------------------------------*/\r
302 \r
303 /* This is called to check that all the created tasks are still running. */\r
304 BaseType_t xAreMathsTaskStillRunning( void )\r
305 {\r
306 /* Keep a history of the check variables so we know if they have been incremented\r
307 since the last call. */\r
308 static uint16_t usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( uint16_t ) 0 };\r
309 BaseType_t xReturn = pdTRUE, xTask;\r
310 \r
311         /* Check the maths tasks are still running by ensuring their check variables\r
312         are still incrementing. */\r
313         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
314         {\r
315                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
316                 {\r
317                         /* The check has not incremented so an error exists. */\r
318                         xReturn = pdFALSE;\r
319                 }\r
320 \r
321                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
322         }\r
323 \r
324         return xReturn;\r
325 }\r
326 \r
327 \r
328 \r