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