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