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