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