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