]> git.sur5r.net Git - freertos/blob - Demo/PPC440_DP_FPU_Xilinx_Virtex5_GCC/RTOSDemo/flop/flop.c
51f923878474d4b3f11ea2ce96fe860ac42d01ba
[freertos] / Demo / PPC440_DP_FPU_Xilinx_Virtex5_GCC / RTOSDemo / flop / flop.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  * Creates eight tasks, each of which loops continuously performing a\r
56  * floating point calculation.\r
57  *\r
58  * All the tasks run at the idle priority and never block or yield.  This causes \r
59  * all eight tasks to time slice with the idle task.  Running at the idle priority \r
60  * means that these tasks will get pre-empted any time another task is ready to run\r
61  * or a time slice occurs.  More often than not the pre-emption will occur mid \r
62  * calculation, creating a good test of the schedulers context switch mechanism - a \r
63  * calculation producing an unexpected result could be a symptom of a corruption in \r
64  * the context of a task.\r
65  *\r
66  * This file demonstrates the use of the task tag and traceTASK_SWITCHED_IN and\r
67  * traceTASK_SWITCHED_OUT macros to save and restore the floating point context.\r
68  */\r
69 \r
70 #include <stdlib.h>\r
71 #include <math.h>\r
72 \r
73 /* Scheduler include files. */\r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 \r
77 /* Demo program include files. */\r
78 #include "flop.h"\r
79 \r
80 /* Misc. definitions. */\r
81 #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE\r
82 #define mathNUMBER_OF_TASKS  ( 8 )\r
83 \r
84 /* Four tasks, each of which performs a different floating point calculation.  \r
85 Each of the four is created twice. */\r
86 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );\r
87 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );\r
88 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );\r
89 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );\r
90 \r
91 /* These variables are used to check that all the tasks are still running.  If a \r
92 task gets a calculation wrong it will stop incrementing its check variable. */\r
93 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
94 \r
95 /* Buffers into which the flop registers will be saved.  There is a buffer for \r
96 each task created within this file.  Zeroing out this array is the normal and\r
97 safe option as this will cause the task to start with all zeros in its flop\r
98 context. */\r
99 static portDOUBLE dFlopRegisters[ mathNUMBER_OF_TASKS ][ portNO_FLOP_REGISTERS_TO_SAVE ];\r
100 \r
101 /*-----------------------------------------------------------*/\r
102 \r
103 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
104 {\r
105 xTaskHandle xTaskJustCreated;\r
106 portBASE_TYPE x, y;\r
107 \r
108         /* Place known values into the buffers into which the flop registers are \r
109         to be saved.  This is for debug purposes only, it is not normally\r
110         required.  The last position in each array is left at zero as the status\r
111         register will be loaded from there. \r
112         \r
113         It is intended that these values can be viewed being loaded into the\r
114         flop registers when a task is started - however the Insight debugger\r
115         does not seem to want to show the flop register values. */\r
116         for( x = 0; x < mathNUMBER_OF_TASKS; x++ )\r
117         {\r
118                 for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1 ); y++ )\r
119                 {\r
120                         dFlopRegisters[ x ][ y ] = ( x + 1 );\r
121                 }\r
122         }\r
123 \r
124         /* Create the first task - passing it the address of the check variable\r
125         that it is going to increment.  This check variable is used as an \r
126         indication that the task is still running. */\r
127         xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xTaskJustCreated );\r
128 \r
129         /* The task     tag value is a value that can be associated with a task, but \r
130         is not used by the scheduler itself.  Its use is down to the application so\r
131         it makes a convenient place in this case to store the pointer to the buffer\r
132         into which the flop context of the task will be stored.  The first created\r
133         task uses dFlopRegisters[ 0 ], the second dFlopRegisters[ 1 ], etc. */\r
134         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 0 ][ 0 ] ) );\r
135 \r
136         /* Create another 7 tasks, allocating a buffer for each. */\r
137         xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xTaskJustCreated  );\r
138         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 1 ][ 0 ] ) );\r
139 \r
140         xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xTaskJustCreated  );\r
141         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 2 ][ 0 ] ) );\r
142 \r
143         xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xTaskJustCreated  );\r
144         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 3 ][ 0 ] ) );\r
145 \r
146         xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xTaskJustCreated  );\r
147         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 4 ][ 0 ] ) );\r
148 \r
149         xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xTaskJustCreated  );\r
150         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 5 ][ 0 ] ) );\r
151 \r
152         xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xTaskJustCreated  );\r
153         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 6 ][ 0 ] ) );\r
154 \r
155         xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xTaskJustCreated  );\r
156         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 7 ][ 0 ] ) );\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
161 {\r
162 volatile portDOUBLE df1, df2, df3, df4;\r
163 volatile unsigned short *pusTaskCheckVariable;\r
164 volatile portDOUBLE dAnswer;\r
165 short sError = pdFALSE;\r
166 \r
167         df1 = 123.4567;\r
168         df2 = 2345.6789;\r
169         df3 = -918.222;\r
170 \r
171         dAnswer = ( df1 + df2 ) * df3;\r
172 \r
173         /* The variable this task increments to show it is still running is passed in \r
174         as the parameter. */\r
175         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
176 \r
177         /* Keep performing a calculation and checking the result against a constant. */\r
178         for(;;)\r
179         {\r
180                 df1 = 123.4567;\r
181                 df2 = 2345.6789;\r
182                 df3 = -918.222;\r
183 \r
184                 df4 = ( df1 + df2 ) * df3;\r
185 \r
186                 #if configUSE_PREEMPTION == 0\r
187                         taskYIELD();\r
188                 #endif\r
189 \r
190                 /* If the calculation does not match the expected constant, stop the \r
191                 increment of the check variable. */\r
192                 if( fabs( df4 - dAnswer ) > 0.001 )\r
193                 {\r
194                         sError = pdTRUE;\r
195                 }\r
196 \r
197                 if( sError == pdFALSE )\r
198                 {\r
199                         /* If the calculation has always been correct, increment the check \r
200                         variable so we know this task is still running okay. */\r
201                         ( *pusTaskCheckVariable )++;\r
202                 }\r
203 \r
204                 #if configUSE_PREEMPTION == 0\r
205                         taskYIELD();\r
206                 #endif\r
207 \r
208         }\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
213 {\r
214 volatile portDOUBLE df1, df2, df3, df4;\r
215 volatile unsigned short *pusTaskCheckVariable;\r
216 volatile portDOUBLE dAnswer;\r
217 short sError = pdFALSE;\r
218 \r
219         df1 = -389.38;\r
220         df2 = 32498.2;\r
221         df3 = -2.0001;\r
222 \r
223         dAnswer = ( df1 / df2 ) * df3;\r
224 \r
225 \r
226         /* The variable this task increments to show it is still running is passed in \r
227         as the parameter. */\r
228         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
229 \r
230         /* Keep performing a calculation and checking the result against a constant. */\r
231         for( ;; )\r
232         {\r
233                 df1 = -389.38;\r
234                 df2 = 32498.2;\r
235                 df3 = -2.0001;\r
236 \r
237                 df4 = ( df1 / df2 ) * df3;\r
238 \r
239                 #if configUSE_PREEMPTION == 0\r
240                         taskYIELD();\r
241                 #endif\r
242                 \r
243                 /* If the calculation does not match the expected constant, stop the \r
244                 increment of the check variable. */\r
245                 if( fabs( df4 - dAnswer ) > 0.001 )\r
246                 {\r
247                         sError = pdTRUE;\r
248                 }\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\r
254                         this task is still running okay. */\r
255                         ( *pusTaskCheckVariable )++;\r
256                 }\r
257 \r
258                 #if configUSE_PREEMPTION == 0\r
259                         taskYIELD();\r
260                 #endif\r
261         }\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
266 {\r
267 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
268 volatile unsigned short *pusTaskCheckVariable;\r
269 const size_t xArraySize = 10;\r
270 size_t xPosition;\r
271 short sError = pdFALSE;\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         pusTaskCheckVariable = ( unsigned short * ) 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 + 5.5;\r
290                         dTotal1 += ( portDOUBLE ) xPosition + 5.5;      \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                         ( *pusTaskCheckVariable )++;\r
317                 }\r
318         }\r
319 }\r
320 /*-----------------------------------------------------------*/\r
321 \r
322 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
323 {\r
324 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
325 volatile unsigned short *pusTaskCheckVariable;\r
326 const size_t xArraySize = 10;\r
327 size_t xPosition;\r
328 short sError = pdFALSE;\r
329 \r
330         /* The variable this task increments to show it is still running is passed in \r
331         as the parameter. */\r
332         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
333 \r
334         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
335 \r
336         /* Keep filling an array, keeping a running total of the values placed in the \r
337         array.  Then run through the array adding up all the values.  If the two totals \r
338         do not match, stop the check variable from incrementing. */\r
339         for( ;; )\r
340         {\r
341                 dTotal1 = 0.0;\r
342                 dTotal2 = 0.0;\r
343 \r
344                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
345                 {\r
346                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;\r
347                         dTotal1 += ( portDOUBLE ) xPosition * 12.123;   \r
348                 }\r
349 \r
350                 #if configUSE_PREEMPTION == 0\r
351                         taskYIELD();\r
352                 #endif\r
353 \r
354                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
355                 {\r
356                         dTotal2 += pdArray[ xPosition ];\r
357                 }\r
358 \r
359                 dDifference = dTotal1 - dTotal2;\r
360                 if( fabs( dDifference ) > 0.001 )\r
361                 {\r
362                         sError = pdTRUE;\r
363                 }\r
364 \r
365                 #if configUSE_PREEMPTION == 0\r
366                         taskYIELD();\r
367                 #endif\r
368 \r
369                 if( sError == pdFALSE )\r
370                 {\r
371                         /* If the calculation has always been correct, increment the check \r
372                         variable so we know     this task is still running okay. */\r
373                         ( *pusTaskCheckVariable )++;\r
374                 }\r
375         }\r
376 }                                \r
377 /*-----------------------------------------------------------*/\r
378 \r
379 /* This is called to check that all the created tasks are still running. */\r
380 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
381 {\r
382 /* Keep a history of the check variables so we know if they have been incremented \r
383 since the last call. */\r
384 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
385 portBASE_TYPE xReturn = pdTRUE, xTask;\r
386 \r
387         /* Check the maths tasks are still running by ensuring their check variables \r
388         are still incrementing. */\r
389         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
390         {\r
391                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
392                 {\r
393                         /* The check has not incremented so an error exists. */\r
394                         xReturn = pdFALSE;\r
395                 }\r
396 \r
397                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
398         }\r
399 \r
400         return xReturn;\r
401 }\r
402 \r
403 \r
404 \r