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