]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/SuperH_SH7216_Renesas/RTOSDemo/flop.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / SuperH_SH7216_Renesas / RTOSDemo / flop.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\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 modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or 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     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /*\r
68  * Creates eight tasks, each of which loops continuously performing a floating \r
69  * point calculation and in so doing test the floating point context switching.\r
70  * This file also demonstrates the use of the xPortUsesFloatingPoint() function\r
71  * which informs the kernel that the task requires its floating point context\r
72  * saved on each switch.\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 \r
76  * priority means that these tasks will get pre-empted any time another task is \r
77  * ready to run or a time slice occurs.  More often than not the pre-emption \r
78  * will occur mid calculation, creating a good test of the schedulers context \r
79  * switch mechanism - a calculation producing an unexpected result could be a \r
80  * symptom of a corruption in the context of a task.\r
81  */\r
82 \r
83 #include <stdlib.h>\r
84 #include <math.h>\r
85 \r
86 /* Scheduler include files. */\r
87 #include "FreeRTOS.h"\r
88 #include "task.h"\r
89 \r
90 /* Demo program include files. */\r
91 #include "flop.h"\r
92 \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 void vCompetingMathTask1( void *pvParameters );\r
99 static void vCompetingMathTask2( void *pvParameters );\r
100 static void vCompetingMathTask3( void *pvParameters );\r
101 static void vCompetingMathTask4( void *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 otherwise the check variable will get incremented on each iteration of the \r
106 tasks execution. */\r
107 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
108 \r
109 /*-----------------------------------------------------------*/\r
110 \r
111 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
112 {\r
113 xTaskHandle xCreatedTask;\r
114 \r
115         /* Create one of the floating point tasks... */\r
116         xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xCreatedTask );\r
117         \r
118         /* ... then enable floating point support for the created task so its flop\r
119         flop registers are maintained in a consistent state. */\r
120         xPortUsesFloatingPoint( xCreatedTask );\r
121 \r
122         xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xCreatedTask );\r
123         xPortUsesFloatingPoint( xCreatedTask );\r
124         \r
125         xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xCreatedTask );\r
126         xPortUsesFloatingPoint( xCreatedTask );\r
127         \r
128         xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xCreatedTask );\r
129         xPortUsesFloatingPoint( xCreatedTask );\r
130         \r
131         xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xCreatedTask );\r
132         xPortUsesFloatingPoint( xCreatedTask );\r
133         \r
134         xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xCreatedTask );\r
135         xPortUsesFloatingPoint( xCreatedTask );\r
136         \r
137         xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xCreatedTask );\r
138         xPortUsesFloatingPoint( xCreatedTask );\r
139         \r
140         xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xCreatedTask );\r
141         xPortUsesFloatingPoint( xCreatedTask );\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 static void vCompetingMathTask1( void *pvParameters )\r
146 {\r
147 volatile double d1, d2, d3, d4;\r
148 volatile unsigned short *pusTaskCheckVariable;\r
149 volatile double dAnswer;\r
150 short sError = pdFALSE;\r
151 \r
152         d1 = 123.4567;\r
153         d2 = 2345.6789;\r
154         d3 = -918.222;\r
155 \r
156         /* Calculate the expected answer. */\r
157         dAnswer = ( d1 + d2 ) * d3;\r
158 \r
159         /* The variable this task increments to show it is still running is passed in \r
160         as the parameter. */\r
161         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
162 \r
163         /* Keep performing a calculation and checking the result against a constant. */\r
164         for(;;)\r
165         {\r
166                 /* Perform the calculation. */\r
167                 d1 = 123.4567;\r
168                 d2 = 2345.6789;\r
169                 d3 = -918.222;\r
170 \r
171                 d4 = ( d1 + d2 ) * d3;\r
172 \r
173                 /* If the calculation does not match the expected constant, stop the \r
174                 increment of the check variable. */\r
175                 if( fabs( d4 - dAnswer ) > 0.001 )\r
176                 {\r
177                         sError = pdTRUE;\r
178                 }\r
179 \r
180                 if( sError == pdFALSE )\r
181                 {\r
182                         /* If the calculation has always been correct, increment the check \r
183                         variable so we know this task is still running okay. */\r
184                         ( *pusTaskCheckVariable )++;\r
185                 }\r
186         }\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 static void vCompetingMathTask2( void *pvParameters )\r
191 {\r
192 volatile double d1, d2, d3, d4;\r
193 volatile unsigned short *pusTaskCheckVariable;\r
194 volatile double dAnswer;\r
195 short sError = pdFALSE;\r
196 \r
197         d1 = -389.38;\r
198         d2 = 32498.2;\r
199         d3 = -2.0001;\r
200 \r
201         /* Calculate the expected answer. */\r
202         dAnswer = ( d1 / d2 ) * d3;\r
203 \r
204 \r
205         /* The variable this task increments to show it is still running is passed in \r
206         as the parameter. */\r
207         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
208 \r
209         /* Keep performing a calculation and checking the result against a constant. */\r
210         for( ;; )\r
211         {\r
212                 /* Perform the calculation. */\r
213                 d1 = -389.38;\r
214                 d2 = 32498.2;\r
215                 d3 = -2.0001;\r
216 \r
217                 d4 = ( d1 / d2 ) * d3;\r
218 \r
219                 /* If the calculation does not match the expected constant, stop the \r
220                 increment of the check variable. */\r
221                 if( fabs( d4 - dAnswer ) > 0.001 )\r
222                 {\r
223                         sError = pdTRUE;\r
224                 }\r
225 \r
226                 if( sError == pdFALSE )\r
227                 {\r
228                         /* If the calculation has always been correct, increment the check \r
229                         variable so we know\r
230                         this task is still running okay. */\r
231                         ( *pusTaskCheckVariable )++;\r
232                 }\r
233         }\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 static void vCompetingMathTask3( void *pvParameters )\r
238 {\r
239 volatile double *pdArray, dTotal1, dTotal2, dDifference;\r
240 volatile unsigned short *pusTaskCheckVariable;\r
241 const size_t xArraySize = 10;\r
242 size_t xPosition;\r
243 short sError = pdFALSE;\r
244 \r
245         /* The variable this task increments to show it is still running is passed \r
246         in as the parameter. */\r
247         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
248 \r
249         /* Allocate memory for use as an array. */\r
250         pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );\r
251 \r
252         /* Keep filling an array, keeping a running total of the values placed in \r
253         the array.  Then run through the array adding up all the values.  If the two \r
254         totals do not match, stop the check variable from incrementing. */\r
255         for( ;; )\r
256         {\r
257                 dTotal1 = 0.0;\r
258                 dTotal2 = 0.0;\r
259 \r
260                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
261                 {\r
262                         pdArray[ xPosition ] = ( double ) xPosition + 5.5;\r
263                         dTotal1 += ( double ) xPosition + 5.5;  \r
264                 }\r
265 \r
266                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
267                 {\r
268                         dTotal2 += pdArray[ xPosition ];\r
269                 }\r
270 \r
271                 dDifference = dTotal1 - dTotal2;\r
272                 if( fabs( dDifference ) > 0.001 )\r
273                 {\r
274                         sError = pdTRUE;\r
275                 }\r
276 \r
277                 if( sError == pdFALSE )\r
278                 {\r
279                         /* If the calculation has always been correct, increment the check \r
280                         variable so we know     this task is still running okay. */\r
281                         ( *pusTaskCheckVariable )++;\r
282                 }\r
283         }\r
284 }\r
285 /*-----------------------------------------------------------*/\r
286 \r
287 static void vCompetingMathTask4( void *pvParameters )\r
288 {\r
289 volatile double *pdArray, dTotal1, dTotal2, dDifference;\r
290 volatile unsigned short *pusTaskCheckVariable;\r
291 const size_t xArraySize = 10;\r
292 size_t xPosition;\r
293 short sError = pdFALSE;\r
294 \r
295         /* The variable this task increments to show it is still running is passed in \r
296         as the parameter. */\r
297         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
298 \r
299         /* Allocate RAM for use as an array. */\r
300         pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );\r
301 \r
302         /* Keep filling an array, keeping a running total of the values placed in the \r
303         array.  Then run through the array adding up all the values.  If the two totals \r
304         do not match, stop the check variable from incrementing. */\r
305         for( ;; )\r
306         {\r
307                 dTotal1 = 0.0;\r
308                 dTotal2 = 0.0;\r
309 \r
310                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
311                 {\r
312                         pdArray[ xPosition ] = ( double ) xPosition * 12.123;\r
313                         dTotal1 += ( double ) xPosition * 12.123;       \r
314                 }\r
315 \r
316                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
317                 {\r
318                         dTotal2 += pdArray[ xPosition ];\r
319                 }\r
320 \r
321                 dDifference = dTotal1 - dTotal2;\r
322                 if( fabs( dDifference ) > 0.001 )\r
323                 {\r
324                         sError = pdTRUE;\r
325                 }\r
326 \r
327                 if( sError == pdFALSE )\r
328                 {\r
329                         /* If the calculation has always been correct, increment the check \r
330                         variable so we know     this task is still running okay. */\r
331                         ( *pusTaskCheckVariable )++;\r
332                 }\r
333         }\r
334 }                                \r
335 /*-----------------------------------------------------------*/\r
336 \r
337 /* This is called to check that all the created tasks are still running. */\r
338 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
339 {\r
340 /* Keep a history of the check variables so we know if they have been \r
341 incremented since the last call. */\r
342 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
343 portBASE_TYPE xReturn = pdTRUE, xTask;\r
344 \r
345         /* Check the maths tasks are still running by ensuring their check variables \r
346         are still incrementing. */\r
347         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
348         {\r
349                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
350                 {\r
351                         /* The check has not incremented so an error exists. */\r
352                         xReturn = pdFALSE;\r
353                 }\r
354 \r
355                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
356         }\r
357 \r
358         return xReturn;\r
359 }\r
360 \r
361 \r
362 \r