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