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