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