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