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