]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MZ_MPLAB/flop_mz.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / PIC32MZ_MPLAB / flop_mz.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\r
31  * floating 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 priority\r
35  * means that these tasks will get pre-empted any time another task is ready to run\r
36  * or a time slice occurs.  More often than not the pre-emption will occur mid\r
37  * calculation, creating a good test of the schedulers context switch mechanism - a\r
38  * calculation producing an unexpected result could be a symptom of a corruption in\r
39  * the context of a task.\r
40  */\r
41 \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_mz.h"\r
51 \r
52 #define mathSTACK_SIZE          (configMINIMAL_STACK_SIZE + 100)\r
53 #define mathNUMBER_OF_TASKS  ( 8 )\r
54 \r
55 /* Four tasks, each of which performs a different floating point calculation.\r
56 Each of the four is created twice. */\r
57 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );\r
58 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );\r
59 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );\r
60 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );\r
61 \r
62 /* These variables are used to check that all the tasks are still running.  If a\r
63 task gets a calculation wrong it will\r
64 stop incrementing its check variable. */\r
65 static volatile unsigned long ulTaskCheck[ mathNUMBER_OF_TASKS ] = { 0 };\r
66 \r
67 /*-----------------------------------------------------------*/\r
68 \r
69 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
70 {\r
71         xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 0 ] ), uxPriority, NULL );\r
72         xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 1 ] ), uxPriority, NULL );\r
73         xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 2 ] ), uxPriority, NULL );\r
74         xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 3 ] ), uxPriority, NULL );\r
75         xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 4 ] ), uxPriority, NULL );\r
76         xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 5 ] ), uxPriority, NULL );\r
77         xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 6 ] ), uxPriority, NULL );\r
78         xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 7 ] ), uxPriority, NULL );\r
79 }\r
80 /*-----------------------------------------------------------*/\r
81 \r
82 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
83 {\r
84 volatile portDOUBLE d1, d2, d3, d4;\r
85 volatile unsigned long *pulTaskCheckVariable;\r
86 volatile portDOUBLE dAnswer;\r
87 short sError = pdFALSE;\r
88 \r
89 \r
90         /* Must be called before any hardware floating point operations are\r
91         performed to let the RTOS portable layer know that this task requires\r
92         a floating point context. */\r
93         portTASK_USES_FLOATING_POINT();\r
94 \r
95         d1 = 123.4567;\r
96         d2 = 2345.6789;\r
97         d3 = -918.222;\r
98 \r
99         dAnswer = ( d1 + d2 ) * d3;\r
100 \r
101         /* The variable this task increments to show it is still running is passed in\r
102         as the parameter. */\r
103         pulTaskCheckVariable = ( unsigned long * ) pvParameters;\r
104     \r
105         /* Keep performing a calculation and checking the result against a constant. */\r
106         for(;;)\r
107         {\r
108                 d1 = 123.4567;\r
109                 d2 = 2345.6789;\r
110                 d3 = -918.222;\r
111 \r
112                 d4 = ( d1 + d2 ) * d3;\r
113 \r
114                 #if configUSE_PREEMPTION == 0\r
115                         taskYIELD();\r
116                 #endif\r
117 \r
118                 /* If the calculation does not match the expected constant, stop the\r
119                 increment of the check variable. */\r
120                 if( fabs( d4 - dAnswer ) > 0.001 )\r
121                 {\r
122                         sError = pdTRUE;\r
123                 }\r
124 \r
125                 if( sError == pdFALSE )\r
126                 {\r
127                         /* If the calculation has always been correct, increment the check\r
128                         variable so we know this task is still running okay. */\r
129                         ( *pulTaskCheckVariable )++;\r
130                 }\r
131 \r
132                 #if configUSE_PREEMPTION == 0\r
133                         taskYIELD();\r
134                 #endif\r
135 \r
136         }\r
137 }\r
138 /*-----------------------------------------------------------*/\r
139 \r
140 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
141 {\r
142 volatile portDOUBLE d1, d2, d3, d4;\r
143 volatile unsigned long *pulTaskCheckVariable;\r
144 volatile portDOUBLE dAnswer;\r
145 short sError = pdFALSE;\r
146 \r
147         /* Must be called before any hardware floating point operations are\r
148         performed to let the RTOS portable layer know that this task requires\r
149         a floating point context. */\r
150         portTASK_USES_FLOATING_POINT();\r
151 \r
152         d1 = -389.38;\r
153         d2 = 32498.2;\r
154         d3 = -2.0001;\r
155 \r
156         dAnswer = ( d1 / d2 ) * d3;\r
157 \r
158 \r
159         /* The variable this task increments to show it is still running is passed in\r
160         as the parameter. */\r
161         pulTaskCheckVariable = ( unsigned long * ) pvParameters;\r
162 \r
163         /* Keep performing a calculation and checking the result against a constant. */\r
164         for( ;; )\r
165         {\r
166                 d1 = -389.38;\r
167                 d2 = 32498.2;\r
168                 d3 = -2.0001;\r
169 \r
170                 d4 = ( d1 / d2 ) * d3;\r
171 \r
172                 #if configUSE_PREEMPTION == 0\r
173                         taskYIELD();\r
174                 #endif\r
175 \r
176                 /* If the calculation does not match the expected constant, stop the\r
177                 increment of the check variable. */\r
178                 if( fabs( d4 - dAnswer ) > 0.001 )\r
179                 {\r
180                         sError = pdTRUE;\r
181                 }\r
182 \r
183                 if( sError == pdFALSE )\r
184                 {\r
185                         /* If the calculation has always been correct, increment the check\r
186                         variable so we know\r
187                         this task is still running okay. */\r
188                         ( *pulTaskCheckVariable )++;\r
189                 }\r
190 \r
191                 #if configUSE_PREEMPTION == 0\r
192                         taskYIELD();\r
193                 #endif\r
194         }\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
199 {\r
200 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
201 volatile unsigned long *pulTaskCheckVariable;\r
202 const size_t xArraySize = 10;\r
203 size_t xPosition;\r
204 short sError = pdFALSE;\r
205 \r
206         /* Must be called before any hardware floating point operations are\r
207         performed to let the RTOS portable layer know that this task requires\r
208         a floating point context. */\r
209         portTASK_USES_FLOATING_POINT();\r
210 \r
211         /* The variable this task increments to show it is still running is passed in\r
212         as the parameter. */\r
213         pulTaskCheckVariable = ( unsigned long * ) pvParameters;\r
214 \r
215         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
216 \r
217         /* Keep filling an array, keeping a running total of the values placed in the\r
218         array.  Then run through the array adding up all the values.  If the two totals\r
219         do not match, stop the check variable from incrementing. */\r
220         for( ;; )\r
221         {\r
222                 dTotal1 = 0.0;\r
223                 dTotal2 = 0.0;\r
224 \r
225                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
226                 {\r
227                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;\r
228                         dTotal1 += ( portDOUBLE ) xPosition + 5.5;\r
229                 }\r
230 \r
231                 #if configUSE_PREEMPTION == 0\r
232                         taskYIELD();\r
233                 #endif\r
234 \r
235                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
236                 {\r
237                         dTotal2 += pdArray[ xPosition ];\r
238                 }\r
239 \r
240                 dDifference = dTotal1 - dTotal2;\r
241                 if( fabs( dDifference ) > 0.001 )\r
242                 {\r
243                         sError = pdTRUE;\r
244                 }\r
245 \r
246                 #if configUSE_PREEMPTION == 0\r
247                         taskYIELD();\r
248                 #endif\r
249 \r
250                 if( sError == pdFALSE )\r
251                 {\r
252                         /* If the calculation has always been correct, increment the check\r
253                         variable so we know     this task is still running okay. */\r
254                         ( *pulTaskCheckVariable )++;\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 unsigned long *pulTaskCheckVariable;\r
264 const size_t xArraySize = 10;\r
265 size_t xPosition;\r
266 short sError = pdFALSE;\r
267 \r
268         /* Must be called before any hardware floating point operations are\r
269         performed to let the RTOS portable layer know that this task requires\r
270         a floating point context. */\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         pulTaskCheckVariable = ( unsigned long * ) 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, increment the check\r
315                         variable so we know     this task is still running okay. */\r
316                         ( *pulTaskCheckVariable )++;\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 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
324 {\r
325 /* Keep a history of the check variables so we know if they have been incremented\r
326 since the last call. */\r
327 static unsigned long ulLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
328 portBASE_TYPE xReturn = pdTRUE, xTask;\r
329 \r
330         /* Check the maths tasks are still running by ensuring their check variables\r
331         are still incrementing. */\r
332         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
333         {\r
334                 if( ulTaskCheck[ xTask ] == ulLastTaskCheck[ xTask ] )\r
335                 {\r
336                         /* The check has not incremented so an error exists. */\r
337                         xReturn = pdFALSE;\r
338                 }\r
339 \r
340                 ulLastTaskCheck[ xTask ] = ulTaskCheck[ xTask ];\r
341         }\r
342 \r
343         return xReturn;\r
344 }\r
345 \r
346 \r
347 \r