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