]> begriffs open source - cmsis-freertos/blob - Demo/PIC32MZ_MPLAB/flop_mz.c
osEventFlagsWait: Fix flag comparison
[cmsis-freertos] / Demo / PIC32MZ_MPLAB / flop_mz.c
1 /*
2  * FreeRTOS Kernel V10.2.1
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*
29  * Creates eight tasks, each of which loops continuously performing a
30  * floating point calculation.
31  *
32  * All the tasks run at the idle priority and never block or yield.  This causes
33  * all eight tasks to time slice with the idle task.  Running at the idle priority
34  * means that these tasks will get pre-empted any time another task is ready to run
35  * or a time slice occurs.  More often than not the pre-emption will occur mid
36  * calculation, creating a good test of the schedulers context switch mechanism - a
37  * calculation producing an unexpected result could be a symptom of a corruption in
38  * the context of a task.
39  */
40
41 #include <stdlib.h>
42 #include <math.h>
43
44 /* Scheduler include files. */
45 #include "FreeRTOS.h"
46 #include "task.h"
47
48 /* Demo program include files. */
49 #include "flop_mz.h"
50
51 #define mathSTACK_SIZE          (configMINIMAL_STACK_SIZE + 100)
52 #define mathNUMBER_OF_TASKS  ( 8 )
53
54 /* Four tasks, each of which performs a different floating point calculation.
55 Each of the four is created twice. */
56 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
57 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
58 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
59 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
60
61 /* These variables are used to check that all the tasks are still running.  If a
62 task gets a calculation wrong it will
63 stop incrementing its check variable. */
64 static volatile unsigned long ulTaskCheck[ mathNUMBER_OF_TASKS ] = { 0 };
65
66 /*-----------------------------------------------------------*/
67
68 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
69 {
70         xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 0 ] ), uxPriority, NULL );
71         xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 1 ] ), uxPriority, NULL );
72         xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 2 ] ), uxPriority, NULL );
73         xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 3 ] ), uxPriority, NULL );
74         xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 4 ] ), uxPriority, NULL );
75         xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 5 ] ), uxPriority, NULL );
76         xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 6 ] ), uxPriority, NULL );
77         xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 7 ] ), uxPriority, NULL );
78 }
79 /*-----------------------------------------------------------*/
80
81 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
82 {
83 volatile portDOUBLE d1, d2, d3, d4;
84 volatile unsigned long *pulTaskCheckVariable;
85 volatile portDOUBLE dAnswer;
86 short sError = pdFALSE;
87
88
89         /* Must be called before any hardware floating point operations are
90         performed to let the RTOS portable layer know that this task requires
91         a floating point context. */
92         portTASK_USES_FLOATING_POINT();
93
94         d1 = 123.4567;
95         d2 = 2345.6789;
96         d3 = -918.222;
97
98         dAnswer = ( d1 + d2 ) * d3;
99
100         /* The variable this task increments to show it is still running is passed in
101         as the parameter. */
102         pulTaskCheckVariable = ( unsigned long * ) pvParameters;
103     
104         /* Keep performing a calculation and checking the result against a constant. */
105         for(;;)
106         {
107                 d1 = 123.4567;
108                 d2 = 2345.6789;
109                 d3 = -918.222;
110
111                 d4 = ( d1 + d2 ) * d3;
112
113                 #if configUSE_PREEMPTION == 0
114                         taskYIELD();
115                 #endif
116
117                 /* If the calculation does not match the expected constant, stop the
118                 increment of the check variable. */
119                 if( fabs( d4 - dAnswer ) > 0.001 )
120                 {
121                         sError = pdTRUE;
122                 }
123
124                 if( sError == pdFALSE )
125                 {
126                         /* If the calculation has always been correct, increment the check
127                         variable so we know this task is still running okay. */
128                         ( *pulTaskCheckVariable )++;
129                 }
130
131                 #if configUSE_PREEMPTION == 0
132                         taskYIELD();
133                 #endif
134
135         }
136 }
137 /*-----------------------------------------------------------*/
138
139 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
140 {
141 volatile portDOUBLE d1, d2, d3, d4;
142 volatile unsigned long *pulTaskCheckVariable;
143 volatile portDOUBLE dAnswer;
144 short sError = pdFALSE;
145
146         /* Must be called before any hardware floating point operations are
147         performed to let the RTOS portable layer know that this task requires
148         a floating point context. */
149         portTASK_USES_FLOATING_POINT();
150
151         d1 = -389.38;
152         d2 = 32498.2;
153         d3 = -2.0001;
154
155         dAnswer = ( d1 / d2 ) * d3;
156
157
158         /* The variable this task increments to show it is still running is passed in
159         as the parameter. */
160         pulTaskCheckVariable = ( unsigned long * ) pvParameters;
161
162         /* Keep performing a calculation and checking the result against a constant. */
163         for( ;; )
164         {
165                 d1 = -389.38;
166                 d2 = 32498.2;
167                 d3 = -2.0001;
168
169                 d4 = ( d1 / d2 ) * d3;
170
171                 #if configUSE_PREEMPTION == 0
172                         taskYIELD();
173                 #endif
174
175                 /* If the calculation does not match the expected constant, stop the
176                 increment of the check variable. */
177                 if( fabs( d4 - dAnswer ) > 0.001 )
178                 {
179                         sError = pdTRUE;
180                 }
181
182                 if( sError == pdFALSE )
183                 {
184                         /* If the calculation has always been correct, increment the check
185                         variable so we know
186                         this task is still running okay. */
187                         ( *pulTaskCheckVariable )++;
188                 }
189
190                 #if configUSE_PREEMPTION == 0
191                         taskYIELD();
192                 #endif
193         }
194 }
195 /*-----------------------------------------------------------*/
196
197 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
198 {
199 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
200 volatile unsigned long *pulTaskCheckVariable;
201 const size_t xArraySize = 10;
202 size_t xPosition;
203 short sError = pdFALSE;
204
205         /* Must be called before any hardware floating point operations are
206         performed to let the RTOS portable layer know that this task requires
207         a floating point context. */
208         portTASK_USES_FLOATING_POINT();
209
210         /* The variable this task increments to show it is still running is passed in
211         as the parameter. */
212         pulTaskCheckVariable = ( unsigned long * ) pvParameters;
213
214         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
215
216         /* Keep filling an array, keeping a running total of the values placed in the
217         array.  Then run through the array adding up all the values.  If the two totals
218         do not match, stop the check variable from incrementing. */
219         for( ;; )
220         {
221                 dTotal1 = 0.0;
222                 dTotal2 = 0.0;
223
224                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
225                 {
226                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;
227                         dTotal1 += ( portDOUBLE ) xPosition + 5.5;
228                 }
229
230                 #if configUSE_PREEMPTION == 0
231                         taskYIELD();
232                 #endif
233
234                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
235                 {
236                         dTotal2 += pdArray[ xPosition ];
237                 }
238
239                 dDifference = dTotal1 - dTotal2;
240                 if( fabs( dDifference ) > 0.001 )
241                 {
242                         sError = pdTRUE;
243                 }
244
245                 #if configUSE_PREEMPTION == 0
246                         taskYIELD();
247                 #endif
248
249                 if( sError == pdFALSE )
250                 {
251                         /* If the calculation has always been correct, increment the check
252                         variable so we know     this task is still running okay. */
253                         ( *pulTaskCheckVariable )++;
254                 }
255         }
256 }
257 /*-----------------------------------------------------------*/
258
259 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
260 {
261 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
262 volatile unsigned long *pulTaskCheckVariable;
263 const size_t xArraySize = 10;
264 size_t xPosition;
265 short sError = pdFALSE;
266
267         /* Must be called before any hardware floating point operations are
268         performed to let the RTOS portable layer know that this task requires
269         a floating point context. */
270         portTASK_USES_FLOATING_POINT();
271
272         /* The variable this task increments to show it is still running is passed in
273         as the parameter. */
274         pulTaskCheckVariable = ( unsigned long * ) pvParameters;
275
276         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
277
278         /* Keep filling an array, keeping a running total of the values placed in the
279         array.  Then run through the array adding up all the values.  If the two totals
280         do not match, stop the check variable from incrementing. */
281         for( ;; )
282         {
283                 dTotal1 = 0.0;
284                 dTotal2 = 0.0;
285
286                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
287                 {
288                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;
289                         dTotal1 += ( portDOUBLE ) xPosition * 12.123;
290                 }
291
292                 #if configUSE_PREEMPTION == 0
293                         taskYIELD();
294                 #endif
295
296                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
297                 {
298                         dTotal2 += pdArray[ xPosition ];
299                 }
300
301                 dDifference = dTotal1 - dTotal2;
302                 if( fabs( dDifference ) > 0.001 )
303                 {
304                         sError = pdTRUE;
305                 }
306
307                 #if configUSE_PREEMPTION == 0
308                         taskYIELD();
309                 #endif
310
311                 if( sError == pdFALSE )
312                 {
313                         /* If the calculation has always been correct, increment the check
314                         variable so we know     this task is still running okay. */
315                         ( *pulTaskCheckVariable )++;
316                 }
317         }
318 }
319 /*-----------------------------------------------------------*/
320
321 /* This is called to check that all the created tasks are still running. */
322 portBASE_TYPE xAreMathsTaskStillRunning( void )
323 {
324 /* Keep a history of the check variables so we know if they have been incremented
325 since the last call. */
326 static unsigned long ulLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
327 portBASE_TYPE xReturn = pdTRUE, xTask;
328
329         /* Check the maths tasks are still running by ensuring their check variables
330         are still incrementing. */
331         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
332         {
333                 if( ulTaskCheck[ xTask ] == ulLastTaskCheck[ xTask ] )
334                 {
335                         /* The check has not incremented so an error exists. */
336                         xReturn = pdFALSE;
337                 }
338
339                 ulLastTaskCheck[ xTask ] = ulTaskCheck[ xTask ];
340         }
341
342         return xReturn;
343 }
344
345
346