]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/flop.c
63e61a6cde0777dbe373ccdac6fcb40b6b6bf370
[freertos] / FreeRTOS / Demo / Common / Minimal / flop.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 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.\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\r
34  * priority means that these tasks will get pre-empted any time another task is\r
35  * ready to run or a time slice occurs.  More often than not the pre-emption\r
36  * will occur mid calculation, creating a good test of the schedulers context\r
37  * switch mechanism - a calculation producing an unexpected result could be a\r
38  * symptom of a corruption in the context of a task.\r
39  */\r
40 \r
41 /* Standard includes. */\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 #ifndef mathSTACK_SIZE\r
53         #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE\r
54 #endif\r
55 \r
56 #define mathNUMBER_OF_TASKS  ( 4 )\r
57 \r
58 /* Four tasks, each of which performs a different floating point calculation.\r
59 Each of the four is created twice. */\r
60 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );\r
61 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );\r
62 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );\r
63 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );\r
64 \r
65 /* These variables are used to check that all the tasks are still running.  If a\r
66 task gets a calculation wrong it will stop setting its check variable. */\r
67 static uint16_t usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( uint16_t ) 0 };\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 void vStartMathTasks( UBaseType_t uxPriority )\r
72 {\r
73         xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
74         xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
75         xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
76         xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
77 }\r
78 /*-----------------------------------------------------------*/\r
79 \r
80 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
81 {\r
82 volatile portDOUBLE d1, d2, d3, d4;\r
83 volatile uint16_t *pusTaskCheckVariable;\r
84 volatile portDOUBLE dAnswer;\r
85 short sError = pdFALSE;\r
86 \r
87         /* Some ports require that tasks that use a hardware floating point unit\r
88         tell the kernel that they require a floating point context before any\r
89         floating point instructions are executed. */\r
90         portTASK_USES_FLOATING_POINT();\r
91 \r
92         d1 = 123.4567;\r
93         d2 = 2345.6789;\r
94         d3 = -918.222;\r
95 \r
96         dAnswer = ( d1 + d2 ) * d3;\r
97 \r
98         /* The variable this task increments to show it is still running is passed in\r
99         as the parameter. */\r
100         pusTaskCheckVariable = ( volatile uint16_t * ) pvParameters;\r
101 \r
102         /* Keep performing a calculation and checking the result against a constant. */\r
103         for(;;)\r
104         {\r
105                 d1 = 123.4567;\r
106                 d2 = 2345.6789;\r
107                 d3 = -918.222;\r
108 \r
109                 d4 = ( d1 + d2 ) * d3;\r
110 \r
111                 #if configUSE_PREEMPTION == 0\r
112                         taskYIELD();\r
113                 #endif\r
114 \r
115                 /* If the calculation does not match the expected constant, stop the\r
116                 increment of the check variable. */\r
117                 if( fabs( d4 - dAnswer ) > 0.001 )\r
118                 {\r
119                         sError = pdTRUE;\r
120                 }\r
121 \r
122                 if( sError == pdFALSE )\r
123                 {\r
124                         /* If the calculation has always been correct then set set the check\r
125                         variable.  The check variable will get set to pdFALSE each time\r
126                         xAreMathsTaskStillRunning() is executed. */\r
127                         ( *pusTaskCheckVariable ) = pdTRUE;\r
128                 }\r
129 \r
130                 #if configUSE_PREEMPTION == 0\r
131                         taskYIELD();\r
132                 #endif\r
133 \r
134         }\r
135 }\r
136 /*-----------------------------------------------------------*/\r
137 \r
138 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
139 {\r
140 volatile portDOUBLE d1, d2, d3, d4;\r
141 volatile uint16_t *pusTaskCheckVariable;\r
142 volatile portDOUBLE dAnswer;\r
143 short sError = pdFALSE;\r
144 \r
145         /* Some ports require that tasks that use a hardware floating point unit\r
146         tell the kernel that they require a floating point context before any\r
147         floating point instructions are executed. */\r
148         portTASK_USES_FLOATING_POINT();\r
149 \r
150         d1 = -389.38;\r
151         d2 = 32498.2;\r
152         d3 = -2.0001;\r
153 \r
154         dAnswer = ( d1 / d2 ) * d3;\r
155 \r
156 \r
157         /* The variable this task increments to show it is still running is passed in\r
158         as the parameter. */\r
159         pusTaskCheckVariable = ( volatile uint16_t * ) pvParameters;\r
160 \r
161         /* Keep performing a calculation and checking the result against a constant. */\r
162         for( ;; )\r
163         {\r
164                 d1 = -389.38;\r
165                 d2 = 32498.2;\r
166                 d3 = -2.0001;\r
167 \r
168                 d4 = ( d1 / d2 ) * d3;\r
169 \r
170                 #if configUSE_PREEMPTION == 0\r
171                         taskYIELD();\r
172                 #endif\r
173 \r
174                 /* If the calculation does not match the expected constant, stop the\r
175                 increment of the check variable. */\r
176                 if( fabs( d4 - dAnswer ) > 0.001 )\r
177                 {\r
178                         sError = pdTRUE;\r
179                 }\r
180 \r
181                 if( sError == pdFALSE )\r
182                 {\r
183                         /* If the calculation has always been correct then set set the check\r
184                         variable.  The check variable will get set to pdFALSE each time\r
185                         xAreMathsTaskStillRunning() is executed. */\r
186                         ( *pusTaskCheckVariable ) = pdTRUE;\r
187                 }\r
188 \r
189                 #if configUSE_PREEMPTION == 0\r
190                         taskYIELD();\r
191                 #endif\r
192         }\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
197 {\r
198 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
199 volatile uint16_t *pusTaskCheckVariable;\r
200 const size_t xArraySize = 10;\r
201 size_t xPosition;\r
202 short sError = pdFALSE;\r
203 \r
204         /* Some ports require that tasks that use a hardware floating point unit\r
205         tell the kernel that they require a floating point context before any\r
206         floating point instructions are executed. */\r
207         portTASK_USES_FLOATING_POINT();\r
208 \r
209         /* The variable this task increments to show it is still running is passed in\r
210         as the parameter. */\r
211         pusTaskCheckVariable = ( volatile uint16_t * ) pvParameters;\r
212 \r
213         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
214 \r
215         /* Keep filling an array, keeping a running total of the values placed in the\r
216         array.  Then run through the array adding up all the values.  If the two totals\r
217         do not match, stop the check variable from incrementing. */\r
218         for( ;; )\r
219         {\r
220                 dTotal1 = 0.0;\r
221                 dTotal2 = 0.0;\r
222 \r
223                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
224                 {\r
225                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;\r
226                         dTotal1 += ( portDOUBLE ) xPosition + 5.5;\r
227                 }\r
228 \r
229                 #if configUSE_PREEMPTION == 0\r
230                         taskYIELD();\r
231                 #endif\r
232 \r
233                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
234                 {\r
235                         dTotal2 += pdArray[ xPosition ];\r
236                 }\r
237 \r
238                 dDifference = dTotal1 - dTotal2;\r
239                 if( fabs( dDifference ) > 0.001 )\r
240                 {\r
241                         sError = pdTRUE;\r
242                 }\r
243 \r
244                 #if configUSE_PREEMPTION == 0\r
245                         taskYIELD();\r
246                 #endif\r
247 \r
248                 if( sError == pdFALSE )\r
249                 {\r
250                         /* If the calculation has always been correct then set set the check\r
251                         variable.  The check variable will get set to pdFALSE each time\r
252                         xAreMathsTaskStillRunning() is executed. */\r
253                         ( *pusTaskCheckVariable ) = pdTRUE;\r
254                 }\r
255         }\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
260 {\r
261 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
262 volatile uint16_t *pusTaskCheckVariable;\r
263 const size_t xArraySize = 10;\r
264 size_t xPosition;\r
265 short sError = pdFALSE;\r
266 \r
267         /* Some ports require that tasks that use a hardware floating point unit\r
268         tell the kernel that they require a floating point context before any\r
269         floating point instructions are executed. */\r
270         portTASK_USES_FLOATING_POINT();\r
271 \r
272         /* The variable this task increments to show it is still running is passed in\r
273         as the parameter. */\r
274         pusTaskCheckVariable = ( volatile uint16_t * ) pvParameters;\r
275 \r
276         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
277 \r
278         /* Keep filling an array, keeping a running total of the values placed in the\r
279         array.  Then run through the array adding up all the values.  If the two totals\r
280         do not match, stop the check variable from incrementing. */\r
281         for( ;; )\r
282         {\r
283                 dTotal1 = 0.0;\r
284                 dTotal2 = 0.0;\r
285 \r
286                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
287                 {\r
288                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;\r
289                         dTotal1 += ( portDOUBLE ) xPosition * 12.123;\r
290                 }\r
291 \r
292                 #if configUSE_PREEMPTION == 0\r
293                         taskYIELD();\r
294                 #endif\r
295 \r
296                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
297                 {\r
298                         dTotal2 += pdArray[ xPosition ];\r
299                 }\r
300 \r
301                 dDifference = dTotal1 - dTotal2;\r
302                 if( fabs( dDifference ) > 0.001 )\r
303                 {\r
304                         sError = pdTRUE;\r
305                 }\r
306 \r
307                 #if configUSE_PREEMPTION == 0\r
308                         taskYIELD();\r
309                 #endif\r
310 \r
311                 if( sError == pdFALSE )\r
312                 {\r
313                         /* If the calculation has always been correct then set set the check\r
314                         variable.  The check variable will get set to pdFALSE each time\r
315                         xAreMathsTaskStillRunning() is executed. */\r
316                         ( *pusTaskCheckVariable ) = pdTRUE;\r
317                 }\r
318         }\r
319 }\r
320 /*-----------------------------------------------------------*/\r
321 \r
322 /* This is called to check that all the created tasks are still running. */\r
323 BaseType_t xAreMathsTaskStillRunning( void )\r
324 {\r
325 BaseType_t xReturn = pdPASS, xTask;\r
326 \r
327         /* Check the maths tasks are still running by ensuring their check variables\r
328         have been set to pdPASS. */\r
329         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
330         {\r
331                 if( usTaskCheck[ xTask ] != pdTRUE )\r
332                 {\r
333                         /* The check has not been set so the associated task has either\r
334                         stalled or detected an error. */\r
335                         xReturn = pdFAIL;\r
336                 }\r
337                 else\r
338                 {\r
339                         /* Reset the variable so it can be checked again the next time this\r
340                         function is executed. */\r
341                         usTaskCheck[ xTask ] = pdFALSE;\r
342                 }\r
343         }\r
344 \r
345         return xReturn;\r
346 }\r
347 \r
348 \r
349 \r