]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MZ_MPLAB/flop_mz.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / PIC32MZ_MPLAB / flop_mz.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\r
30  * floating 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 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_mz.h"\r
50 \r
51 #define mathSTACK_SIZE          (configMINIMAL_STACK_SIZE + 100)\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 unsigned long ulTaskCheck[ mathNUMBER_OF_TASKS ] = { 0 };\r
65 \r
66 /*-----------------------------------------------------------*/\r
67 \r
68 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
69 {\r
70         xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 0 ] ), uxPriority, NULL );\r
71         xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 1 ] ), uxPriority, NULL );\r
72         xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 2 ] ), uxPriority, NULL );\r
73         xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 3 ] ), uxPriority, NULL );\r
74         xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 4 ] ), uxPriority, NULL );\r
75         xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 5 ] ), uxPriority, NULL );\r
76         xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 6 ] ), uxPriority, NULL );\r
77         xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 7 ] ), 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 unsigned long *pulTaskCheckVariable;\r
85 volatile portDOUBLE dAnswer;\r
86 short sError = pdFALSE;\r
87 \r
88 \r
89         /* Must be called before any hardware floating point operations are\r
90         performed to let the RTOS portable layer know that this task requires\r
91         a floating point context. */\r
92         portTASK_USES_FLOATING_POINT();\r
93 \r
94         d1 = 123.4567;\r
95         d2 = 2345.6789;\r
96         d3 = -918.222;\r
97 \r
98         dAnswer = ( d1 + d2 ) * d3;\r
99 \r
100         /* The variable this task increments to show it is still running is passed in\r
101         as the parameter. */\r
102         pulTaskCheckVariable = ( unsigned long * ) pvParameters;\r
103     \r
104         /* Keep performing a calculation and checking the result against a constant. */\r
105         for(;;)\r
106         {\r
107                 d1 = 123.4567;\r
108                 d2 = 2345.6789;\r
109                 d3 = -918.222;\r
110 \r
111                 d4 = ( d1 + d2 ) * d3;\r
112 \r
113                 #if configUSE_PREEMPTION == 0\r
114                         taskYIELD();\r
115                 #endif\r
116 \r
117                 /* If the calculation does not match the expected constant, stop the\r
118                 increment of the check variable. */\r
119                 if( fabs( d4 - dAnswer ) > 0.001 )\r
120                 {\r
121                         sError = pdTRUE;\r
122                 }\r
123 \r
124                 if( sError == pdFALSE )\r
125                 {\r
126                         /* If the calculation has always been correct, increment the check\r
127                         variable so we know this task is still running okay. */\r
128                         ( *pulTaskCheckVariable )++;\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 unsigned long *pulTaskCheckVariable;\r
143 volatile portDOUBLE dAnswer;\r
144 short sError = pdFALSE;\r
145 \r
146         /* Must be called before any hardware floating point operations are\r
147         performed to let the RTOS portable layer know that this task requires\r
148         a floating point context. */\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         pulTaskCheckVariable = ( unsigned long * ) 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, increment the check\r
185                         variable so we know\r
186                         this task is still running okay. */\r
187                         ( *pulTaskCheckVariable )++;\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 unsigned long *pulTaskCheckVariable;\r
201 const size_t xArraySize = 10;\r
202 size_t xPosition;\r
203 short sError = pdFALSE;\r
204 \r
205         /* Must be called before any hardware floating point operations are\r
206         performed to let the RTOS portable layer know that this task requires\r
207         a floating point context. */\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         pulTaskCheckVariable = ( unsigned long * ) 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, increment the check\r
252                         variable so we know     this task is still running okay. */\r
253                         ( *pulTaskCheckVariable )++;\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 unsigned long *pulTaskCheckVariable;\r
263 const size_t xArraySize = 10;\r
264 size_t xPosition;\r
265 short sError = pdFALSE;\r
266 \r
267         /* Must be called before any hardware floating point operations are\r
268         performed to let the RTOS portable layer know that this task requires\r
269         a floating point context. */\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         pulTaskCheckVariable = ( unsigned long * ) 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, increment the check\r
314                         variable so we know     this task is still running okay. */\r
315                         ( *pulTaskCheckVariable )++;\r
316                 }\r
317         }\r
318 }\r
319 /*-----------------------------------------------------------*/\r
320 \r
321 /* This is called to check that all the created tasks are still running. */\r
322 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
323 {\r
324 /* Keep a history of the check variables so we know if they have been incremented\r
325 since the last call. */\r
326 static unsigned long ulLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
327 portBASE_TYPE xReturn = pdTRUE, xTask;\r
328 \r
329         /* Check the maths tasks are still running by ensuring their check variables\r
330         are still incrementing. */\r
331         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
332         {\r
333                 if( ulTaskCheck[ xTask ] == ulLastTaskCheck[ xTask ] )\r
334                 {\r
335                         /* The check has not incremented so an error exists. */\r
336                         xReturn = pdFALSE;\r
337                 }\r
338 \r
339                 ulLastTaskCheck[ xTask ] = ulTaskCheck[ xTask ];\r
340         }\r
341 \r
342         return xReturn;\r
343 }\r
344 \r
345 \r
346 \r