]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Full/flop.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / Common / Full / 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 Changes from V1.2.3\r
31         \r
32         + The created tasks now include calls to tskYIELD(), allowing them to be used\r
33           with the cooperative scheduler.\r
34 */\r
35 \r
36 /**\r
37  * Creates eight tasks, each of which loops continuously performing an (emulated) \r
38  * floating point calculation.\r
39  *\r
40  * All the tasks run at the idle priority and never block or yield.  This causes \r
41  * all eight tasks to time slice with the idle task.  Running at the idle priority \r
42  * means that these tasks will get pre-empted any time another task is ready to run\r
43  * or a time slice occurs.  More often than not the pre-emption will occur mid \r
44  * calculation, creating a good test of the schedulers context switch mechanism - a \r
45  * calculation producing an unexpected result could be a symptom of a corruption in \r
46  * the context of a task.\r
47  *\r
48  * \page FlopC flop.c\r
49  * \ingroup DemoFiles\r
50  * <HR>\r
51  */\r
52 \r
53 #include <stdlib.h>\r
54 #include <math.h>\r
55 \r
56 /* Scheduler include files. */\r
57 #include "FreeRTOS.h"\r
58 #include "task.h"\r
59 #include "print.h"\r
60 \r
61 /* Demo program include files. */\r
62 #include "flop.h"\r
63 \r
64 #define mathSTACK_SIZE          ( ( unsigned short ) 512 )\r
65 #define mathNUMBER_OF_TASKS  ( 8 )\r
66 \r
67 /* Four tasks, each of which performs a different floating point calculation.  \r
68 Each of the four is created twice. */\r
69 static void vCompetingMathTask1( void *pvParameters );\r
70 static void vCompetingMathTask2( void *pvParameters );\r
71 static void vCompetingMathTask3( void *pvParameters );\r
72 static void vCompetingMathTask4( void *pvParameters );\r
73 \r
74 /* These variables are used to check that all the tasks are still running.  If a \r
75 task gets a calculation wrong it will\r
76 stop incrementing its check variable. */\r
77 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
78 \r
79 /*-----------------------------------------------------------*/\r
80 \r
81 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
82 {\r
83         xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
84         xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
85         xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
86         xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
87         xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );\r
88         xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );\r
89         xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );\r
90         xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );\r
91 }\r
92 /*-----------------------------------------------------------*/\r
93 \r
94 static void vCompetingMathTask1( void *pvParameters )\r
95 {\r
96 portDOUBLE d1, d2, d3, d4;\r
97 volatile unsigned short *pusTaskCheckVariable;\r
98 const portDOUBLE dAnswer = ( 123.4567 + 2345.6789 ) * -918.222;\r
99 const char * const pcTaskStartMsg = "Math task 1 started.\r\n";\r
100 const char * const pcTaskFailMsg = "Math task 1 failed.\r\n";\r
101 short sError = pdFALSE;\r
102 \r
103         /* Queue a message for printing to say the task has started. */\r
104         vPrintDisplayMessage( &pcTaskStartMsg );\r
105 \r
106         /* The variable this task increments to show it is still running is passed in \r
107         as the parameter. */\r
108         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
109 \r
110         /* Keep performing a calculation and checking the result against a constant. */\r
111         for(;;)\r
112         {\r
113                 d1 = 123.4567;\r
114                 d2 = 2345.6789;\r
115                 d3 = -918.222;\r
116 \r
117                 d4 = ( d1 + d2 ) * d3;\r
118 \r
119                 taskYIELD();\r
120 \r
121                 /* If the calculation does not match the expected constant, stop the \r
122                 increment of the check variable. */\r
123                 if( fabs( d4 - dAnswer ) > 0.001 )\r
124                 {\r
125                         vPrintDisplayMessage( &pcTaskFailMsg );\r
126                         sError = pdTRUE;\r
127                 }\r
128 \r
129                 if( sError == pdFALSE )\r
130                 {\r
131                         /* If the calculation has always been correct, increment the check \r
132                         variable so we know this task is still running okay. */\r
133                         ( *pusTaskCheckVariable )++;\r
134                 }\r
135 \r
136                 taskYIELD();\r
137         }\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 static void vCompetingMathTask2( void *pvParameters )\r
142 {\r
143 portDOUBLE d1, d2, d3, d4;\r
144 volatile unsigned short *pusTaskCheckVariable;\r
145 const portDOUBLE dAnswer = ( -389.38 / 32498.2 ) * -2.0001;\r
146 const char * const pcTaskStartMsg = "Math task 2 started.\r\n";\r
147 const char * const pcTaskFailMsg = "Math task 2 failed.\r\n";\r
148 short sError = pdFALSE;\r
149 \r
150         /* Queue a message for printing to say the task has started. */\r
151         vPrintDisplayMessage( &pcTaskStartMsg );\r
152 \r
153         /* The variable this task increments to show it is still running is passed in \r
154         as the parameter. */\r
155         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
156 \r
157         /* Keep performing a calculation and checking the result against a constant. */\r
158         for( ;; )\r
159         {\r
160                 d1 = -389.38;\r
161                 d2 = 32498.2;\r
162                 d3 = -2.0001;\r
163 \r
164                 d4 = ( d1 / d2 ) * d3;\r
165 \r
166                 taskYIELD();\r
167                 \r
168                 /* If the calculation does not match the expected constant, stop the \r
169                 increment of the check variable. */\r
170                 if( fabs( d4 - dAnswer ) > 0.001 )\r
171                 {\r
172                         vPrintDisplayMessage( &pcTaskFailMsg );\r
173                         sError = pdTRUE;\r
174                 }\r
175 \r
176                 if( sError == pdFALSE )\r
177                 {\r
178                         /* If the calculation has always been correct, increment the check \r
179                         variable so we know\r
180                         this task is still running okay. */\r
181                         ( *pusTaskCheckVariable )++;\r
182                 }\r
183 \r
184                 taskYIELD();\r
185         }\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 static void vCompetingMathTask3( void *pvParameters )\r
190 {\r
191 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
192 volatile unsigned short *pusTaskCheckVariable;\r
193 const unsigned short usArraySize = 250;\r
194 unsigned short usPosition;\r
195 const char * const pcTaskStartMsg = "Math task 3 started.\r\n";\r
196 const char * const pcTaskFailMsg = "Math task 3 failed.\r\n";\r
197 short sError = pdFALSE;\r
198 \r
199         /* Queue a message for printing to say the task has started. */\r
200         vPrintDisplayMessage( &pcTaskStartMsg );\r
201 \r
202         /* The variable this task increments to show it is still running is passed in \r
203         as the parameter. */\r
204         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
205 \r
206         pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );\r
207 \r
208         /* Keep filling an array, keeping a running total of the values placed in the \r
209         array.  Then run through the array adding up all the values.  If the two totals \r
210         do not match, stop the check variable from incrementing. */\r
211         for( ;; )\r
212         {\r
213                 dTotal1 = 0.0;\r
214                 dTotal2 = 0.0;\r
215 \r
216                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
217                 {\r
218                         pdArray[ usPosition ] = ( portDOUBLE ) usPosition + 5.5;\r
219                         dTotal1 += ( portDOUBLE ) usPosition + 5.5;     \r
220                 }\r
221 \r
222                 taskYIELD();\r
223 \r
224                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
225                 {\r
226                         dTotal2 += pdArray[ usPosition ];\r
227                 }\r
228 \r
229                 dDifference = dTotal1 - dTotal2;\r
230                 if( fabs( dDifference ) > 0.001 )\r
231                 {\r
232                         vPrintDisplayMessage( &pcTaskFailMsg );\r
233                         sError = pdTRUE;\r
234                 }\r
235 \r
236                 taskYIELD();\r
237 \r
238                 if( sError == pdFALSE )\r
239                 {\r
240                         /* If the calculation has always been correct, increment the check \r
241                         variable so we know     this task is still running okay. */\r
242                         ( *pusTaskCheckVariable )++;\r
243                 }\r
244         }\r
245 }\r
246 /*-----------------------------------------------------------*/\r
247 \r
248 static void vCompetingMathTask4( void *pvParameters )\r
249 {\r
250 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
251 volatile unsigned short *pusTaskCheckVariable;\r
252 const unsigned short usArraySize = 250;\r
253 unsigned short usPosition;\r
254 const char * const pcTaskStartMsg = "Math task 4 started.\r\n";\r
255 const char * const pcTaskFailMsg = "Math task 4 failed.\r\n";\r
256 short sError = pdFALSE;\r
257 \r
258         /* Queue a message for printing to say the task has started. */\r
259         vPrintDisplayMessage( &pcTaskStartMsg );\r
260 \r
261         /* The variable this task increments to show it is still running is passed in \r
262         as the parameter. */\r
263         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
264 \r
265         pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );\r
266 \r
267         /* Keep filling an array, keeping a running total of the values placed in the \r
268         array.  Then run through the array adding up all the values.  If the two totals \r
269         do not match, stop the check variable from incrementing. */\r
270         for( ;; )\r
271         {\r
272                 dTotal1 = 0.0;\r
273                 dTotal2 = 0.0;\r
274 \r
275                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
276                 {\r
277                         pdArray[ usPosition ] = ( portDOUBLE ) usPosition * 12.123;\r
278                         dTotal1 += ( portDOUBLE ) usPosition * 12.123;  \r
279                 }\r
280 \r
281                 taskYIELD();\r
282 \r
283                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
284                 {\r
285                         dTotal2 += pdArray[ usPosition ];\r
286                 }\r
287 \r
288                 dDifference = dTotal1 - dTotal2;\r
289                 if( fabs( dDifference ) > 0.001 )\r
290                 {\r
291                         vPrintDisplayMessage( &pcTaskFailMsg );\r
292                         sError = pdTRUE;\r
293                 }\r
294 \r
295                 taskYIELD();\r
296 \r
297                 if( sError == pdFALSE )\r
298                 {\r
299                         /* If the calculation has always been correct, increment the check \r
300                         variable so we know     this task is still running okay. */\r
301                         ( *pusTaskCheckVariable )++;\r
302                 }\r
303         }\r
304 }\r
305 /*-----------------------------------------------------------*/\r
306 \r
307 /* This is called to check that all the created tasks are still running. */\r
308 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
309 {\r
310 /* Keep a history of the check variables so we know if they have been incremented \r
311 since the last call. */\r
312 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
313 portBASE_TYPE xReturn = pdTRUE, xTask;\r
314 \r
315         /* Check the maths tasks are still running by ensuring their check variables \r
316         are still incrementing. */\r
317         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
318         {\r
319                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
320                 {\r
321                         /* The check has not incremented so an error exists. */\r
322                         xReturn = pdFALSE;\r
323                 }\r
324 \r
325                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
326         }\r
327 \r
328         return xReturn;\r
329 }\r
330 \r
331 \r
332 \r