]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/SuperH_SH7216_Renesas/RTOSDemo/flop.c
63684a0ad512068eabc1c7391de1bb96f2d989f6
[freertos] / FreeRTOS / Demo / SuperH_SH7216_Renesas / RTOSDemo / flop.c
1 /*\r
2     FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /*\r
66  * Creates eight tasks, each of which loops continuously performing a floating \r
67  * point calculation and in so doing test the floating point context switching.\r
68  * This file also demonstrates the use of the xPortUsesFloatingPoint() function\r
69  * which informs the kernel that the task requires its floating point context\r
70  * saved on each switch.\r
71  *\r
72  * All the tasks run at the idle priority and never block or yield.  This causes \r
73  * all eight tasks to time slice with the idle task.  Running at the idle \r
74  * priority means that these tasks will get pre-empted any time another task is \r
75  * ready to run or a time slice occurs.  More often than not the pre-emption \r
76  * will occur mid calculation, creating a good test of the schedulers context \r
77  * switch mechanism - a calculation producing an unexpected result could be a \r
78  * symptom of a corruption in the context of a task.\r
79  */\r
80 \r
81 #include <stdlib.h>\r
82 #include <math.h>\r
83 \r
84 /* Scheduler include files. */\r
85 #include "FreeRTOS.h"\r
86 #include "task.h"\r
87 \r
88 /* Demo program include files. */\r
89 #include "flop.h"\r
90 \r
91 #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE\r
92 #define mathNUMBER_OF_TASKS  ( 8 )\r
93 \r
94 /* Four tasks, each of which performs a different floating point calculation.  \r
95 Each of the four is created twice. */\r
96 static void vCompetingMathTask1( void *pvParameters );\r
97 static void vCompetingMathTask2( void *pvParameters );\r
98 static void vCompetingMathTask3( void *pvParameters );\r
99 static void vCompetingMathTask4( void *pvParameters );\r
100 \r
101 /* These variables are used to check that all the tasks are still running.  If a \r
102 task gets a calculation wrong it will stop incrementing its check variable,\r
103 otherwise the check variable will get incremented on each iteration of the \r
104 tasks execution. */\r
105 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
106 \r
107 /*-----------------------------------------------------------*/\r
108 \r
109 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
110 {\r
111 xTaskHandle xCreatedTask;\r
112 \r
113         /* Create one of the floating point tasks... */\r
114         xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xCreatedTask );\r
115         \r
116         /* ... then enable floating point support for the created task so its flop\r
117         flop registers are maintained in a consistent state. */\r
118         xPortUsesFloatingPoint( xCreatedTask );\r
119 \r
120         xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xCreatedTask );\r
121         xPortUsesFloatingPoint( xCreatedTask );\r
122         \r
123         xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xCreatedTask );\r
124         xPortUsesFloatingPoint( xCreatedTask );\r
125         \r
126         xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xCreatedTask );\r
127         xPortUsesFloatingPoint( xCreatedTask );\r
128         \r
129         xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xCreatedTask );\r
130         xPortUsesFloatingPoint( xCreatedTask );\r
131         \r
132         xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xCreatedTask );\r
133         xPortUsesFloatingPoint( xCreatedTask );\r
134         \r
135         xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xCreatedTask );\r
136         xPortUsesFloatingPoint( xCreatedTask );\r
137         \r
138         xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xCreatedTask );\r
139         xPortUsesFloatingPoint( xCreatedTask );\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 static void vCompetingMathTask1( void *pvParameters )\r
144 {\r
145 volatile double d1, d2, d3, d4;\r
146 volatile unsigned short *pusTaskCheckVariable;\r
147 volatile double dAnswer;\r
148 short sError = pdFALSE;\r
149 \r
150         d1 = 123.4567;\r
151         d2 = 2345.6789;\r
152         d3 = -918.222;\r
153 \r
154         /* Calculate the expected answer. */\r
155         dAnswer = ( d1 + d2 ) * d3;\r
156 \r
157         /* The variable this task increments to show it is still running is passed in \r
158         as the parameter. */\r
159         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
160 \r
161         /* Keep performing a calculation and checking the result against a constant. */\r
162         for(;;)\r
163         {\r
164                 /* Perform the calculation. */\r
165                 d1 = 123.4567;\r
166                 d2 = 2345.6789;\r
167                 d3 = -918.222;\r
168 \r
169                 d4 = ( d1 + d2 ) * d3;\r
170 \r
171                 /* If the calculation does not match the expected constant, stop the \r
172                 increment of the check variable. */\r
173                 if( fabs( d4 - dAnswer ) > 0.001 )\r
174                 {\r
175                         sError = pdTRUE;\r
176                 }\r
177 \r
178                 if( sError == pdFALSE )\r
179                 {\r
180                         /* If the calculation has always been correct, increment the check \r
181                         variable so we know this task is still running okay. */\r
182                         ( *pusTaskCheckVariable )++;\r
183                 }\r
184         }\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 static void vCompetingMathTask2( void *pvParameters )\r
189 {\r
190 volatile double d1, d2, d3, d4;\r
191 volatile unsigned short *pusTaskCheckVariable;\r
192 volatile double dAnswer;\r
193 short sError = pdFALSE;\r
194 \r
195         d1 = -389.38;\r
196         d2 = 32498.2;\r
197         d3 = -2.0001;\r
198 \r
199         /* Calculate the expected answer. */\r
200         dAnswer = ( d1 / d2 ) * d3;\r
201 \r
202 \r
203         /* The variable this task increments to show it is still running is passed in \r
204         as the parameter. */\r
205         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
206 \r
207         /* Keep performing a calculation and checking the result against a constant. */\r
208         for( ;; )\r
209         {\r
210                 /* Perform the calculation. */\r
211                 d1 = -389.38;\r
212                 d2 = 32498.2;\r
213                 d3 = -2.0001;\r
214 \r
215                 d4 = ( d1 / d2 ) * d3;\r
216 \r
217                 /* If the calculation does not match the expected constant, stop the \r
218                 increment of the check variable. */\r
219                 if( fabs( d4 - dAnswer ) > 0.001 )\r
220                 {\r
221                         sError = pdTRUE;\r
222                 }\r
223 \r
224                 if( sError == pdFALSE )\r
225                 {\r
226                         /* If the calculation has always been correct, increment the check \r
227                         variable so we know\r
228                         this task is still running okay. */\r
229                         ( *pusTaskCheckVariable )++;\r
230                 }\r
231         }\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 static void vCompetingMathTask3( void *pvParameters )\r
236 {\r
237 volatile double *pdArray, dTotal1, dTotal2, dDifference;\r
238 volatile unsigned short *pusTaskCheckVariable;\r
239 const size_t xArraySize = 10;\r
240 size_t xPosition;\r
241 short sError = pdFALSE;\r
242 \r
243         /* The variable this task increments to show it is still running is passed \r
244         in as the parameter. */\r
245         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
246 \r
247         /* Allocate memory for use as an array. */\r
248         pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );\r
249 \r
250         /* Keep filling an array, keeping a running total of the values placed in \r
251         the array.  Then run through the array adding up all the values.  If the two \r
252         totals do not match, stop the check variable from incrementing. */\r
253         for( ;; )\r
254         {\r
255                 dTotal1 = 0.0;\r
256                 dTotal2 = 0.0;\r
257 \r
258                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
259                 {\r
260                         pdArray[ xPosition ] = ( double ) xPosition + 5.5;\r
261                         dTotal1 += ( double ) xPosition + 5.5;  \r
262                 }\r
263 \r
264                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
265                 {\r
266                         dTotal2 += pdArray[ xPosition ];\r
267                 }\r
268 \r
269                 dDifference = dTotal1 - dTotal2;\r
270                 if( fabs( dDifference ) > 0.001 )\r
271                 {\r
272                         sError = pdTRUE;\r
273                 }\r
274 \r
275                 if( sError == pdFALSE )\r
276                 {\r
277                         /* If the calculation has always been correct, increment the check \r
278                         variable so we know     this task is still running okay. */\r
279                         ( *pusTaskCheckVariable )++;\r
280                 }\r
281         }\r
282 }\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 static void vCompetingMathTask4( void *pvParameters )\r
286 {\r
287 volatile double *pdArray, dTotal1, dTotal2, dDifference;\r
288 volatile unsigned short *pusTaskCheckVariable;\r
289 const size_t xArraySize = 10;\r
290 size_t xPosition;\r
291 short sError = pdFALSE;\r
292 \r
293         /* The variable this task increments to show it is still running is passed in \r
294         as the parameter. */\r
295         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
296 \r
297         /* Allocate RAM for use as an array. */\r
298         pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );\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                 dTotal1 = 0.0;\r
306                 dTotal2 = 0.0;\r
307 \r
308                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
309                 {\r
310                         pdArray[ xPosition ] = ( double ) xPosition * 12.123;\r
311                         dTotal1 += ( double ) xPosition * 12.123;       \r
312                 }\r
313 \r
314                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
315                 {\r
316                         dTotal2 += pdArray[ xPosition ];\r
317                 }\r
318 \r
319                 dDifference = dTotal1 - dTotal2;\r
320                 if( fabs( dDifference ) > 0.001 )\r
321                 {\r
322                         sError = pdTRUE;\r
323                 }\r
324 \r
325                 if( sError == pdFALSE )\r
326                 {\r
327                         /* If the calculation has always been correct, increment the check \r
328                         variable so we know     this task is still running okay. */\r
329                         ( *pusTaskCheckVariable )++;\r
330                 }\r
331         }\r
332 }                                \r
333 /*-----------------------------------------------------------*/\r
334 \r
335 /* This is called to check that all the created tasks are still running. */\r
336 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
337 {\r
338 /* Keep a history of the check variables so we know if they have been \r
339 incremented since the last call. */\r
340 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
341 portBASE_TYPE xReturn = pdTRUE, xTask;\r
342 \r
343         /* Check the maths tasks are still running by ensuring their check variables \r
344         are still incrementing. */\r
345         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
346         {\r
347                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
348                 {\r
349                         /* The check has not incremented so an error exists. */\r
350                         xReturn = pdFALSE;\r
351                 }\r
352 \r
353                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
354         }\r
355 \r
356         return xReturn;\r
357 }\r
358 \r
359 \r
360 \r