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