]> begriffs open source - cmsis-freertos/blob - Demo/PPC440_Xilinx_Virtex5_GCC/RTOSDemo/flop/flop.c
Updated pack to FreeRTOS 10.4.6
[cmsis-freertos] / Demo / PPC440_Xilinx_Virtex5_GCC / RTOSDemo / flop / flop.c
1 /*
2  * FreeRTOS V202111.00
3  * Copyright (C) 2020 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  * This file demonstrates the use of the task tag and traceTASK_SWITCHED_IN and
41  * traceTASK_SWITCHED_OUT macros to save and restore the floating point context.
42  */
43
44 #include <stdlib.h>
45 #include <math.h>
46
47 /* Scheduler include files. */
48 #include "FreeRTOS.h"
49 #include "task.h"
50
51 /* Demo program include files. */
52 #include "flop.h"
53
54 /* Misc. definitions. */
55 #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE
56 #define mathNUMBER_OF_TASKS  ( 8 )
57
58 /* Four tasks, each of which performs a different floating point calculation.  
59 Each of the four is created twice. */
60 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
61 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
62 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
63 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
64
65 /* These variables are used to check that all the tasks are still running.  If a 
66 task gets a calculation wrong it will stop incrementing its check variable. */
67 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
68
69 /* Buffers into which the flop registers will be saved.  There is a buffer for 
70 each task created within this file.  Zeroing out this array is the normal and
71 safe option as this will cause the task to start with all zeros in its flop
72 context. */
73 static unsigned long ulFlopRegisters[ mathNUMBER_OF_TASKS ][ portNO_FLOP_REGISTERS_TO_SAVE ];
74
75 /*-----------------------------------------------------------*/
76
77 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
78 {
79 TaskHandle_t xTaskJustCreated;
80 portBASE_TYPE x, y;
81
82         /* Place known values into the buffers into which the flop registers are 
83         to be saved.  This is for debug purposes only, it is not normally
84         required.  The last position in each array is left at zero as the status
85         register will be loaded from there. 
86         
87         It is intended that these values can be viewed being loaded into the
88         flop registers when a task is started - however the Insight debugger
89         does not seem to want to show the flop register values. */
90         for( x = 0; x < mathNUMBER_OF_TASKS; x++ )
91         {
92                 for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1 ); y++ )
93                 {
94                         ulFlopRegisters[ x ][ y ] = ( x + 1 );
95                 }
96         }
97
98         /* Create the first task - passing it the address of the check variable
99         that it is going to increment.  This check variable is used as an 
100         indication that the task is still running. */
101         xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xTaskJustCreated );
102
103         /* The task     tag value is a value that can be associated with a task, but 
104         is not used by the scheduler itself.  Its use is down to the application so
105         it makes a convenient place in this case to store the pointer to the buffer
106         into which the flop context of the task will be stored.  The first created
107         task uses ulFlopRegisters[ 0 ], the second ulFlopRegisters[ 1 ], etc. */
108         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 0 ][ 0 ] ) );
109
110         /* Create another 7 tasks, allocating a buffer for each. */
111         xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xTaskJustCreated  );
112         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 1 ][ 0 ] ) );
113
114         xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xTaskJustCreated  );
115         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 2 ][ 0 ] ) );
116
117         xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xTaskJustCreated  );
118         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 3 ][ 0 ] ) );
119
120         xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xTaskJustCreated  );
121         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 4 ][ 0 ] ) );
122
123         xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xTaskJustCreated  );
124         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 5 ][ 0 ] ) );
125
126         xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xTaskJustCreated  );
127         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 6 ][ 0 ] ) );
128
129         xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xTaskJustCreated  );
130         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 7 ][ 0 ] ) );
131 }
132 /*-----------------------------------------------------------*/
133
134 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
135 {
136 volatile portFLOAT ff1, ff2, ff3, ff4;
137 volatile unsigned short *pusTaskCheckVariable;
138 volatile portFLOAT fAnswer;
139 short sError = pdFALSE;
140
141         ff1 = 123.4567F;
142         ff2 = 2345.6789F;
143         ff3 = -918.222F;
144
145         fAnswer = ( ff1 + ff2 ) * ff3;
146
147         /* The variable this task increments to show it is still running is passed in 
148         as the parameter. */
149         pusTaskCheckVariable = ( unsigned short * ) pvParameters;
150
151         /* Keep performing a calculation and checking the result against a constant. */
152         for(;;)
153         {
154                 ff1 = 123.4567F;
155                 ff2 = 2345.6789F;
156                 ff3 = -918.222F;
157
158                 ff4 = ( ff1 + ff2 ) * ff3;
159
160                 #if configUSE_PREEMPTION == 0
161                         taskYIELD();
162                 #endif
163
164                 /* If the calculation does not match the expected constant, stop the 
165                 increment of the check variable. */
166                 if( fabs( ff4 - fAnswer ) > 0.001F )
167                 {
168                         sError = pdTRUE;
169                 }
170
171                 if( sError == pdFALSE )
172                 {
173                         /* If the calculation has always been correct, increment the check 
174                         variable so we know this task is still running okay. */
175                         ( *pusTaskCheckVariable )++;
176                 }
177
178                 #if configUSE_PREEMPTION == 0
179                         taskYIELD();
180                 #endif
181
182         }
183 }
184 /*-----------------------------------------------------------*/
185
186 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
187 {
188 volatile portFLOAT ff1, ff2, ff3, ff4;
189 volatile unsigned short *pusTaskCheckVariable;
190 volatile portFLOAT fAnswer;
191 short sError = pdFALSE;
192
193         ff1 = -389.38F;
194         ff2 = 32498.2F;
195         ff3 = -2.0001F;
196
197         fAnswer = ( ff1 / ff2 ) * ff3;
198
199
200         /* The variable this task increments to show it is still running is passed in 
201         as the parameter. */
202         pusTaskCheckVariable = ( unsigned short * ) pvParameters;
203
204         /* Keep performing a calculation and checking the result against a constant. */
205         for( ;; )
206         {
207                 ff1 = -389.38F;
208                 ff2 = 32498.2F;
209                 ff3 = -2.0001F;
210
211                 ff4 = ( ff1 / ff2 ) * ff3;
212
213                 #if configUSE_PREEMPTION == 0
214                         taskYIELD();
215                 #endif
216                 
217                 /* If the calculation does not match the expected constant, stop the 
218                 increment of the check variable. */
219                 if( fabs( ff4 - fAnswer ) > 0.001F )
220                 {
221                         sError = pdTRUE;
222                 }
223
224                 if( sError == pdFALSE )
225                 {
226                         /* If the calculation has always been correct, increment the check 
227                         variable so we know
228                         this task is still running okay. */
229                         ( *pusTaskCheckVariable )++;
230                 }
231
232                 #if configUSE_PREEMPTION == 0
233                         taskYIELD();
234                 #endif
235         }
236 }
237 /*-----------------------------------------------------------*/
238
239 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
240 {
241 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
242 volatile unsigned short *pusTaskCheckVariable;
243 const size_t xArraySize = 10;
244 size_t xPosition;
245 short sError = pdFALSE;
246
247         /* The variable this task increments to show it is still running is passed in 
248         as the parameter. */
249         pusTaskCheckVariable = ( unsigned short * ) pvParameters;
250
251         pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
252
253         /* Keep filling an array, keeping a running total of the values placed in the 
254         array.  Then run through the array adding up all the values.  If the two totals 
255         do not match, stop the check variable from incrementing. */
256         for( ;; )
257         {
258                 fTotal1 = 0.0F;
259                 fTotal2 = 0.0F;
260
261                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
262                 {
263                         pfArray[ xPosition ] = ( portFLOAT ) xPosition + 5.5F;
264                         fTotal1 += ( portFLOAT ) xPosition + 5.5F;      
265                 }
266
267                 #if configUSE_PREEMPTION == 0
268                         taskYIELD();
269                 #endif
270
271                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
272                 {
273                         fTotal2 += pfArray[ xPosition ];
274                 }
275
276                 fDifference = fTotal1 - fTotal2;
277                 if( fabs( fDifference ) > 0.001F )
278                 {
279                         sError = pdTRUE;
280                 }
281
282                 #if configUSE_PREEMPTION == 0
283                         taskYIELD();
284                 #endif
285
286                 if( sError == pdFALSE )
287                 {
288                         /* If the calculation has always been correct, increment the check 
289                         variable so we know     this task is still running okay. */
290                         ( *pusTaskCheckVariable )++;
291                 }
292         }
293 }
294 /*-----------------------------------------------------------*/
295
296 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
297 {
298 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
299 volatile unsigned short *pusTaskCheckVariable;
300 const size_t xArraySize = 10;
301 size_t xPosition;
302 short sError = pdFALSE;
303
304         /* The variable this task increments to show it is still running is passed in 
305         as the parameter. */
306         pusTaskCheckVariable = ( unsigned short * ) pvParameters;
307
308         pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
309
310         /* Keep filling an array, keeping a running total of the values placed in the 
311         array.  Then run through the array adding up all the values.  If the two totals 
312         do not match, stop the check variable from incrementing. */
313         for( ;; )
314         {
315                 fTotal1 = 0.0F;
316                 fTotal2 = 0.0F;
317
318                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
319                 {
320                         pfArray[ xPosition ] = ( portFLOAT ) xPosition * 12.123F;
321                         fTotal1 += ( portFLOAT ) xPosition * 12.123F;   
322                 }
323
324                 #if configUSE_PREEMPTION == 0
325                         taskYIELD();
326                 #endif
327
328                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
329                 {
330                         fTotal2 += pfArray[ xPosition ];
331                 }
332
333                 fDifference = fTotal1 - fTotal2;
334                 if( fabs( fDifference ) > 0.001F )
335                 {
336                         sError = pdTRUE;
337                 }
338
339                 #if configUSE_PREEMPTION == 0
340                         taskYIELD();
341                 #endif
342
343                 if( sError == pdFALSE )
344                 {
345                         /* If the calculation has always been correct, increment the check 
346                         variable so we know     this task is still running okay. */
347                         ( *pusTaskCheckVariable )++;
348                 }
349         }
350 }                                
351 /*-----------------------------------------------------------*/
352
353 /* This is called to check that all the created tasks are still running. */
354 portBASE_TYPE xAreMathsTaskStillRunning( void )
355 {
356 /* Keep a history of the check variables so we know if they have been incremented 
357 since the last call. */
358 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
359 portBASE_TYPE xReturn = pdTRUE, xTask;
360
361         /* Check the maths tasks are still running by ensuring their check variables 
362         are still incrementing. */
363         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
364         {
365                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
366                 {
367                         /* The check has not incremented so an error exists. */
368                         xReturn = pdFALSE;
369                 }
370
371                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
372         }
373
374         return xReturn;
375 }
376
377
378