]> git.sur5r.net Git - freertos/blob - Demo/PPC405_FPU_Xilinx_Virtex4_GCC/RTOSDemo/flop/flop.c
Remove compiler warnings.
[freertos] / Demo / PPC405_FPU_Xilinx_Virtex4_GCC / RTOSDemo / flop / flop.c
1 /*\r
2         FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26     ***************************************************************************\r
27     ***************************************************************************\r
28     *                                                                         *\r
29     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
30     * and even write all or part of your application on your behalf.          *\r
31     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
32     * expedite your project.                                                  *\r
33     *                                                                         *\r
34     ***************************************************************************\r
35     ***************************************************************************\r
36 \r
37         Please ensure to read the configuration and relevant port sections of the\r
38         online documentation.\r
39 \r
40         http://www.FreeRTOS.org - Documentation, latest information, license and \r
41         contact details.\r
42 \r
43         http://www.SafeRTOS.com - A version that is certified for use in safety \r
44         critical systems.\r
45 \r
46         http://www.OpenRTOS.com - Commercial support, development, porting, \r
47         licensing and training services.\r
48 */\r
49 \r
50 /*\r
51  * Creates eight tasks, each of which loops continuously performing a\r
52  * floating point calculation.\r
53  *\r
54  * All the tasks run at the idle priority and never block or yield.  This causes \r
55  * all eight tasks to time slice with the idle task.  Running at the idle priority \r
56  * means that these tasks will get pre-empted any time another task is ready to run\r
57  * or a time slice occurs.  More often than not the pre-emption will occur mid \r
58  * calculation, creating a good test of the schedulers context switch mechanism - a \r
59  * calculation producing an unexpected result could be a symptom of a corruption in \r
60  * the context of a task.\r
61  *\r
62  * This file demonstrates the use of the task tag and traceTASK_SWITCHED_IN and\r
63  * traceTASK_SWITCHED_OUT macros to save and restore the floating point context.\r
64  */\r
65 \r
66 #include <stdlib.h>\r
67 #include <math.h>\r
68 \r
69 /* Scheduler include files. */\r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 \r
73 /* Demo program include files. */\r
74 #include "flop.h"\r
75 \r
76 /* Misc. definitions. */\r
77 #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE\r
78 #define mathNUMBER_OF_TASKS  ( 8 )\r
79 \r
80 /* Four tasks, each of which performs a different floating point calculation.  \r
81 Each of the four is created twice. */\r
82 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );\r
83 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );\r
84 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );\r
85 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );\r
86 \r
87 /* These variables are used to check that all the tasks are still running.  If a \r
88 task gets a calculation wrong it will stop incrementing its check variable. */\r
89 static volatile unsigned portSHORT usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };\r
90 \r
91 /* Buffers into which the flop registers will be saved.  There is a buffer for \r
92 each task created within this file.  Zeroing out this array is the normal and\r
93 safe option as this will cause the task to start with all zeros in its flop\r
94 context. */\r
95 static unsigned portLONG ulFlopRegisters[ mathNUMBER_OF_TASKS ][ portNO_FLOP_REGISTERS_TO_SAVE ];\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
100 {\r
101 xTaskHandle xTaskJustCreated;\r
102 portBASE_TYPE x, y;\r
103 \r
104         /* Place known values into the buffers into which the flop registers are \r
105         to be saved.  This is for debug purposes only, it is not normally\r
106         required.  The last position in each array is left at zero as the status\r
107         register will be loaded from there. \r
108         \r
109         It is intended that these values can be viewed being loaded into the\r
110         flop registers when a task is started - however the Insight debugger\r
111         does not seem to want to show the flop register values. */\r
112         for( x = 0; x < mathNUMBER_OF_TASKS; x++ )\r
113         {\r
114                 for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1 ); y++ )\r
115                 {\r
116                         ulFlopRegisters[ x ][ y ] = ( x + 1 );\r
117                 }\r
118         }\r
119 \r
120         /* Create the first task - passing it the address of the check variable\r
121         that it is going to increment.  This check variable is used as an \r
122         indication that the task is still running. */\r
123         xTaskCreate( vCompetingMathTask1, ( signed portCHAR * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xTaskJustCreated );\r
124 \r
125         /* The task     tag value is a value that can be associated with a task, but \r
126         is not used by the scheduler itself.  Its use is down to the application so\r
127         it makes a convenient place in this case to store the pointer to the buffer\r
128         into which the flop context of the task will be stored.  The first created\r
129         task uses ulFlopRegisters[ 0 ], the second ulFlopRegisters[ 1 ], etc. */\r
130         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 0 ][ 0 ] ) );\r
131 \r
132         /* Create another 7 tasks, allocating a buffer for each. */\r
133         xTaskCreate( vCompetingMathTask2, ( signed portCHAR * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xTaskJustCreated  );\r
134         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 1 ][ 0 ] ) );\r
135 \r
136         xTaskCreate( vCompetingMathTask3, ( signed portCHAR * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xTaskJustCreated  );\r
137         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 2 ][ 0 ] ) );\r
138 \r
139         xTaskCreate( vCompetingMathTask4, ( signed portCHAR * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xTaskJustCreated  );\r
140         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 3 ][ 0 ] ) );\r
141 \r
142         xTaskCreate( vCompetingMathTask1, ( signed portCHAR * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xTaskJustCreated  );\r
143         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 4 ][ 0 ] ) );\r
144 \r
145         xTaskCreate( vCompetingMathTask2, ( signed portCHAR * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xTaskJustCreated  );\r
146         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 5 ][ 0 ] ) );\r
147 \r
148         xTaskCreate( vCompetingMathTask3, ( signed portCHAR * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xTaskJustCreated  );\r
149         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 6 ][ 0 ] ) );\r
150 \r
151         xTaskCreate( vCompetingMathTask4, ( signed portCHAR * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xTaskJustCreated  );\r
152         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 7 ][ 0 ] ) );\r
153 }\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
157 {\r
158 volatile portFLOAT ff1, ff2, ff3, ff4;\r
159 volatile unsigned portSHORT *pusTaskCheckVariable;\r
160 volatile portFLOAT fAnswer;\r
161 portSHORT sError = pdFALSE;\r
162 \r
163         ff1 = 123.4567F;\r
164         ff2 = 2345.6789F;\r
165         ff3 = -918.222F;\r
166 \r
167         fAnswer = ( ff1 + ff2 ) * ff3;\r
168 \r
169         /* The variable this task increments to show it is still running is passed in \r
170         as the parameter. */\r
171         pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;\r
172 \r
173         /* Keep performing a calculation and checking the result against a constant. */\r
174         for(;;)\r
175         {\r
176                 ff1 = 123.4567F;\r
177                 ff2 = 2345.6789F;\r
178                 ff3 = -918.222F;\r
179 \r
180                 ff4 = ( ff1 + ff2 ) * ff3;\r
181 \r
182                 #if configUSE_PREEMPTION == 0\r
183                         taskYIELD();\r
184                 #endif\r
185 \r
186                 /* If the calculation does not match the expected constant, stop the \r
187                 increment of the check variable. */\r
188                 if( fabs( ff4 - fAnswer ) > 0.001F )\r
189                 {\r
190                         sError = pdTRUE;\r
191                 }\r
192 \r
193                 if( sError == pdFALSE )\r
194                 {\r
195                         /* If the calculation has always been correct, increment the check \r
196                         variable so we know this task is still running okay. */\r
197                         ( *pusTaskCheckVariable )++;\r
198                 }\r
199 \r
200                 #if configUSE_PREEMPTION == 0\r
201                         taskYIELD();\r
202                 #endif\r
203 \r
204         }\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
209 {\r
210 volatile portFLOAT ff1, ff2, ff3, ff4;\r
211 volatile unsigned portSHORT *pusTaskCheckVariable;\r
212 volatile portFLOAT fAnswer;\r
213 portSHORT sError = pdFALSE;\r
214 \r
215         ff1 = -389.38F;\r
216         ff2 = 32498.2F;\r
217         ff3 = -2.0001F;\r
218 \r
219         fAnswer = ( ff1 / ff2 ) * ff3;\r
220 \r
221 \r
222         /* The variable this task increments to show it is still running is passed in \r
223         as the parameter. */\r
224         pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;\r
225 \r
226         /* Keep performing a calculation and checking the result against a constant. */\r
227         for( ;; )\r
228         {\r
229                 ff1 = -389.38F;\r
230                 ff2 = 32498.2F;\r
231                 ff3 = -2.0001F;\r
232 \r
233                 ff4 = ( ff1 / ff2 ) * ff3;\r
234 \r
235                 #if configUSE_PREEMPTION == 0\r
236                         taskYIELD();\r
237                 #endif\r
238                 \r
239                 /* If the calculation does not match the expected constant, stop the \r
240                 increment of the check variable. */\r
241                 if( fabs( ff4 - fAnswer ) > 0.001F )\r
242                 {\r
243                         sError = pdTRUE;\r
244                 }\r
245 \r
246                 if( sError == pdFALSE )\r
247                 {\r
248                         /* If the calculation has always been correct, increment the check \r
249                         variable so we know\r
250                         this task is still running okay. */\r
251                         ( *pusTaskCheckVariable )++;\r
252                 }\r
253 \r
254                 #if configUSE_PREEMPTION == 0\r
255                         taskYIELD();\r
256                 #endif\r
257         }\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
262 {\r
263 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;\r
264 volatile unsigned portSHORT *pusTaskCheckVariable;\r
265 const size_t xArraySize = 10;\r
266 size_t xPosition;\r
267 portSHORT sError = pdFALSE;\r
268 \r
269         /* The variable this task increments to show it is still running is passed in \r
270         as the parameter. */\r
271         pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;\r
272 \r
273         pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );\r
274 \r
275         /* Keep filling an array, keeping a running total of the values placed in the \r
276         array.  Then run through the array adding up all the values.  If the two totals \r
277         do not match, stop the check variable from incrementing. */\r
278         for( ;; )\r
279         {\r
280                 fTotal1 = 0.0F;\r
281                 fTotal2 = 0.0F;\r
282 \r
283                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
284                 {\r
285                         pfArray[ xPosition ] = ( portFLOAT ) xPosition + 5.5F;\r
286                         fTotal1 += ( portFLOAT ) xPosition + 5.5F;      \r
287                 }\r
288 \r
289                 #if configUSE_PREEMPTION == 0\r
290                         taskYIELD();\r
291                 #endif\r
292 \r
293                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
294                 {\r
295                         fTotal2 += pfArray[ xPosition ];\r
296                 }\r
297 \r
298                 fDifference = fTotal1 - fTotal2;\r
299                 if( fabs( fDifference ) > 0.001F )\r
300                 {\r
301                         sError = pdTRUE;\r
302                 }\r
303 \r
304                 #if configUSE_PREEMPTION == 0\r
305                         taskYIELD();\r
306                 #endif\r
307 \r
308                 if( sError == pdFALSE )\r
309                 {\r
310                         /* If the calculation has always been correct, increment the check \r
311                         variable so we know     this task is still running okay. */\r
312                         ( *pusTaskCheckVariable )++;\r
313                 }\r
314         }\r
315 }\r
316 /*-----------------------------------------------------------*/\r
317 \r
318 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
319 {\r
320 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;\r
321 volatile unsigned portSHORT *pusTaskCheckVariable;\r
322 const size_t xArraySize = 10;\r
323 size_t xPosition;\r
324 portSHORT sError = pdFALSE;\r
325 \r
326         /* The variable this task increments to show it is still running is passed in \r
327         as the parameter. */\r
328         pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;\r
329 \r
330         pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );\r
331 \r
332         /* Keep filling an array, keeping a running total of the values placed in the \r
333         array.  Then run through the array adding up all the values.  If the two totals \r
334         do not match, stop the check variable from incrementing. */\r
335         for( ;; )\r
336         {\r
337                 fTotal1 = 0.0F;\r
338                 fTotal2 = 0.0F;\r
339 \r
340                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
341                 {\r
342                         pfArray[ xPosition ] = ( portFLOAT ) xPosition * 12.123F;\r
343                         fTotal1 += ( portFLOAT ) xPosition * 12.123F;   \r
344                 }\r
345 \r
346                 #if configUSE_PREEMPTION == 0\r
347                         taskYIELD();\r
348                 #endif\r
349 \r
350                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
351                 {\r
352                         fTotal2 += pfArray[ xPosition ];\r
353                 }\r
354 \r
355                 fDifference = fTotal1 - fTotal2;\r
356                 if( fabs( fDifference ) > 0.001F )\r
357                 {\r
358                         sError = pdTRUE;\r
359                 }\r
360 \r
361                 #if configUSE_PREEMPTION == 0\r
362                         taskYIELD();\r
363                 #endif\r
364 \r
365                 if( sError == pdFALSE )\r
366                 {\r
367                         /* If the calculation has always been correct, increment the check \r
368                         variable so we know     this task is still running okay. */\r
369                         ( *pusTaskCheckVariable )++;\r
370                 }\r
371         }\r
372 }                                \r
373 /*-----------------------------------------------------------*/\r
374 \r
375 /* This is called to check that all the created tasks are still running. */\r
376 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
377 {\r
378 /* Keep a history of the check variables so we know if they have been incremented \r
379 since the last call. */\r
380 static unsigned portSHORT usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };\r
381 portBASE_TYPE xReturn = pdTRUE, xTask;\r
382 \r
383         /* Check the maths tasks are still running by ensuring their check variables \r
384         are still incrementing. */\r
385         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
386         {\r
387                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
388                 {\r
389                         /* The check has not incremented so an error exists. */\r
390                         xReturn = pdFALSE;\r
391                 }\r
392 \r
393                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
394         }\r
395 \r
396         return xReturn;\r
397 }\r
398 \r
399 \r
400 \r