]> git.sur5r.net Git - freertos/blob - Demo/SuperH_SH7216_Renesas/RTOSDemo/flop.c
Add Cortus port to produce V6.0.5.
[freertos] / Demo / SuperH_SH7216_Renesas / RTOSDemo / flop.c
1 /*\r
2     FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  * Creates eight tasks, each of which loops continuously performing a floating \r
56  * point calculation and in so doing test the floating point context switching.\r
57  * This file also demonstrates the use of the xPortUsesFloatingPoint() function\r
58  * which informs the kernel that the task requires its floating point context\r
59  * saved on each switch.\r
60  *\r
61  * All the tasks run at the idle priority and never block or yield.  This causes \r
62  * all eight tasks to time slice with the idle task.  Running at the idle \r
63  * priority means that these tasks will get pre-empted any time another task is \r
64  * ready to run or a time slice occurs.  More often than not the pre-emption \r
65  * will occur mid calculation, creating a good test of the schedulers context \r
66  * switch mechanism - a calculation producing an unexpected result could be a \r
67  * symptom of a corruption in the context of a task.\r
68  */\r
69 \r
70 #include <stdlib.h>\r
71 #include <math.h>\r
72 \r
73 /* Scheduler include files. */\r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 \r
77 /* Demo program include files. */\r
78 #include "flop.h"\r
79 \r
80 #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE\r
81 #define mathNUMBER_OF_TASKS  ( 8 )\r
82 \r
83 /* Four tasks, each of which performs a different floating point calculation.  \r
84 Each of the four is created twice. */\r
85 static void vCompetingMathTask1( void *pvParameters );\r
86 static void vCompetingMathTask2( void *pvParameters );\r
87 static void vCompetingMathTask3( void *pvParameters );\r
88 static void vCompetingMathTask4( void *pvParameters );\r
89 \r
90 /* These variables are used to check that all the tasks are still running.  If a \r
91 task gets a calculation wrong it will stop incrementing its check variable,\r
92 otherwise the check variable will get incremented on each iteration of the \r
93 tasks execution. */\r
94 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
99 {\r
100 xTaskHandle xCreatedTask;\r
101 \r
102         /* Create one of the floating point tasks... */\r
103         xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xCreatedTask );\r
104         \r
105         /* ... then enable floating point support for the created task so its flop\r
106         flop registers are maintained in a consistent state. */\r
107         xPortUsesFloatingPoint( xCreatedTask );\r
108 \r
109         xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xCreatedTask );\r
110         xPortUsesFloatingPoint( xCreatedTask );\r
111         \r
112         xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xCreatedTask );\r
113         xPortUsesFloatingPoint( xCreatedTask );\r
114         \r
115         xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xCreatedTask );\r
116         xPortUsesFloatingPoint( xCreatedTask );\r
117         \r
118         xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xCreatedTask );\r
119         xPortUsesFloatingPoint( xCreatedTask );\r
120         \r
121         xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xCreatedTask );\r
122         xPortUsesFloatingPoint( xCreatedTask );\r
123         \r
124         xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xCreatedTask );\r
125         xPortUsesFloatingPoint( xCreatedTask );\r
126         \r
127         xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xCreatedTask );\r
128         xPortUsesFloatingPoint( xCreatedTask );\r
129 }\r
130 /*-----------------------------------------------------------*/\r
131 \r
132 static void vCompetingMathTask1( void *pvParameters )\r
133 {\r
134 volatile double d1, d2, d3, d4;\r
135 volatile unsigned short *pusTaskCheckVariable;\r
136 volatile double dAnswer;\r
137 short sError = pdFALSE;\r
138 \r
139         d1 = 123.4567;\r
140         d2 = 2345.6789;\r
141         d3 = -918.222;\r
142 \r
143         /* Calculate the expected answer. */\r
144         dAnswer = ( d1 + d2 ) * d3;\r
145 \r
146         /* The variable this task increments to show it is still running is passed in \r
147         as the parameter. */\r
148         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
149 \r
150         /* Keep performing a calculation and checking the result against a constant. */\r
151         for(;;)\r
152         {\r
153                 /* Perform the calculation. */\r
154                 d1 = 123.4567;\r
155                 d2 = 2345.6789;\r
156                 d3 = -918.222;\r
157 \r
158                 d4 = ( d1 + d2 ) * d3;\r
159 \r
160                 /* If the calculation does not match the expected constant, stop the \r
161                 increment of the check variable. */\r
162                 if( fabs( d4 - dAnswer ) > 0.001 )\r
163                 {\r
164                         sError = pdTRUE;\r
165                 }\r
166 \r
167                 if( sError == pdFALSE )\r
168                 {\r
169                         /* If the calculation has always been correct, increment the check \r
170                         variable so we know this task is still running okay. */\r
171                         ( *pusTaskCheckVariable )++;\r
172                 }\r
173         }\r
174 }\r
175 /*-----------------------------------------------------------*/\r
176 \r
177 static void vCompetingMathTask2( void *pvParameters )\r
178 {\r
179 volatile double d1, d2, d3, d4;\r
180 volatile unsigned short *pusTaskCheckVariable;\r
181 volatile double dAnswer;\r
182 short sError = pdFALSE;\r
183 \r
184         d1 = -389.38;\r
185         d2 = 32498.2;\r
186         d3 = -2.0001;\r
187 \r
188         /* Calculate the expected answer. */\r
189         dAnswer = ( d1 / d2 ) * d3;\r
190 \r
191 \r
192         /* The variable this task increments to show it is still running is passed in \r
193         as the parameter. */\r
194         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
195 \r
196         /* Keep performing a calculation and checking the result against a constant. */\r
197         for( ;; )\r
198         {\r
199                 /* Perform the calculation. */\r
200                 d1 = -389.38;\r
201                 d2 = 32498.2;\r
202                 d3 = -2.0001;\r
203 \r
204                 d4 = ( d1 / d2 ) * d3;\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( d4 - dAnswer ) > 0.001 )\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\r
217                         this task is still running okay. */\r
218                         ( *pusTaskCheckVariable )++;\r
219                 }\r
220         }\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 static void vCompetingMathTask3( void *pvParameters )\r
225 {\r
226 volatile double *pdArray, dTotal1, dTotal2, dDifference;\r
227 volatile unsigned short *pusTaskCheckVariable;\r
228 const size_t xArraySize = 10;\r
229 size_t xPosition;\r
230 short sError = pdFALSE;\r
231 \r
232         /* The variable this task increments to show it is still running is passed \r
233         in as the parameter. */\r
234         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
235 \r
236         /* Allocate memory for use as an array. */\r
237         pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );\r
238 \r
239         /* Keep filling an array, keeping a running total of the values placed in \r
240         the array.  Then run through the array adding up all the values.  If the two \r
241         totals do not match, stop the check variable from incrementing. */\r
242         for( ;; )\r
243         {\r
244                 dTotal1 = 0.0;\r
245                 dTotal2 = 0.0;\r
246 \r
247                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
248                 {\r
249                         pdArray[ xPosition ] = ( double ) xPosition + 5.5;\r
250                         dTotal1 += ( double ) xPosition + 5.5;  \r
251                 }\r
252 \r
253                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
254                 {\r
255                         dTotal2 += pdArray[ xPosition ];\r
256                 }\r
257 \r
258                 dDifference = dTotal1 - dTotal2;\r
259                 if( fabs( dDifference ) > 0.001 )\r
260                 {\r
261                         sError = pdTRUE;\r
262                 }\r
263 \r
264                 if( sError == pdFALSE )\r
265                 {\r
266                         /* If the calculation has always been correct, increment the check \r
267                         variable so we know     this task is still running okay. */\r
268                         ( *pusTaskCheckVariable )++;\r
269                 }\r
270         }\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 static void vCompetingMathTask4( void *pvParameters )\r
275 {\r
276 volatile double *pdArray, dTotal1, dTotal2, dDifference;\r
277 volatile unsigned short *pusTaskCheckVariable;\r
278 const size_t xArraySize = 10;\r
279 size_t xPosition;\r
280 short sError = pdFALSE;\r
281 \r
282         /* The variable this task increments to show it is still running is passed in \r
283         as the parameter. */\r
284         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
285 \r
286         /* Allocate RAM for use as an array. */\r
287         pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );\r
288 \r
289         /* Keep filling an array, keeping a running total of the values placed in the \r
290         array.  Then run through the array adding up all the values.  If the two totals \r
291         do not match, stop the check variable from incrementing. */\r
292         for( ;; )\r
293         {\r
294                 dTotal1 = 0.0;\r
295                 dTotal2 = 0.0;\r
296 \r
297                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
298                 {\r
299                         pdArray[ xPosition ] = ( double ) xPosition * 12.123;\r
300                         dTotal1 += ( double ) xPosition * 12.123;       \r
301                 }\r
302 \r
303                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
304                 {\r
305                         dTotal2 += pdArray[ xPosition ];\r
306                 }\r
307 \r
308                 dDifference = dTotal1 - dTotal2;\r
309                 if( fabs( dDifference ) > 0.001 )\r
310                 {\r
311                         sError = pdTRUE;\r
312                 }\r
313 \r
314                 if( sError == pdFALSE )\r
315                 {\r
316                         /* If the calculation has always been correct, increment the check \r
317                         variable so we know     this task is still running okay. */\r
318                         ( *pusTaskCheckVariable )++;\r
319                 }\r
320         }\r
321 }                                \r
322 /*-----------------------------------------------------------*/\r
323 \r
324 /* This is called to check that all the created tasks are still running. */\r
325 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
326 {\r
327 /* Keep a history of the check variables so we know if they have been \r
328 incremented since the last call. */\r
329 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
330 portBASE_TYPE xReturn = pdTRUE, xTask;\r
331 \r
332         /* Check the maths tasks are still running by ensuring their check variables \r
333         are still incrementing. */\r
334         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
335         {\r
336                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
337                 {\r
338                         /* The check has not incremented so an error exists. */\r
339                         xReturn = pdFALSE;\r
340                 }\r
341 \r
342                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
343         }\r
344 \r
345         return xReturn;\r
346 }\r
347 \r
348 \r
349 \r