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