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