]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/flop.c
Update flop.c and FreeRTOS.h to include a macro (and a default null implementation...
[freertos] / FreeRTOS / Demo / Common / Minimal / flop.c
1 /*\r
2     FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  * Creates eight tasks, each of which loops continuously performing an (emulated) \r
77  * floating point calculation.\r
78  *\r
79  * All the tasks run at the idle priority and never block or yield.  This causes \r
80  * all eight tasks to time slice with the idle task.  Running at the idle priority \r
81  * means that these tasks will get pre-empted any time another task is ready to run\r
82  * or a time slice occurs.  More often than not the pre-emption will occur mid \r
83  * calculation, creating a good test of the schedulers context switch mechanism - a \r
84  * calculation producing an unexpected result could be a symptom of a corruption in \r
85  * the context of a task.\r
86  */\r
87 \r
88 #include <stdlib.h>\r
89 #include <math.h>\r
90 \r
91 /* Scheduler include files. */\r
92 #include "FreeRTOS.h"\r
93 #include "task.h"\r
94 \r
95 /* Demo program include files. */\r
96 #include "flop.h"\r
97 \r
98 #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE\r
99 #define mathNUMBER_OF_TASKS  ( 8 )\r
100 \r
101 /* Four tasks, each of which performs a different floating point calculation.  \r
102 Each of the four is created twice. */\r
103 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );\r
104 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );\r
105 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );\r
106 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );\r
107 \r
108 /* These variables are used to check that all the tasks are still running.  If a \r
109 task gets a calculation wrong it will\r
110 stop incrementing its check variable. */\r
111 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
112 \r
113 /*-----------------------------------------------------------*/\r
114 \r
115 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
116 {\r
117         xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
118         xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
119         xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
120         xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
121         xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );\r
122         xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );\r
123         xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );\r
124         xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );\r
125 }\r
126 /*-----------------------------------------------------------*/\r
127 \r
128 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
129 {\r
130 volatile portDOUBLE d1, d2, d3, d4;\r
131 volatile unsigned short *pusTaskCheckVariable;\r
132 volatile portDOUBLE dAnswer;\r
133 short sError = pdFALSE;\r
134 \r
135         /* Some ports require that tasks that use a hardware floating point unit\r
136         tell the kernel that they require a floating point context before any\r
137         floating point instructions are executed. */\r
138         portTASK_USES_FLOATING_POINT();\r
139 \r
140         d1 = 123.4567;\r
141         d2 = 2345.6789;\r
142         d3 = -918.222;\r
143 \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                 d1 = 123.4567;\r
154                 d2 = 2345.6789;\r
155                 d3 = -918.222;\r
156 \r
157                 d4 = ( d1 + d2 ) * d3;\r
158 \r
159                 #if configUSE_PREEMPTION == 0\r
160                         taskYIELD();\r
161                 #endif\r
162 \r
163                 /* If the calculation does not match the expected constant, stop the \r
164                 increment of the check variable. */\r
165                 if( fabs( d4 - dAnswer ) > 0.001 )\r
166                 {\r
167                         sError = pdTRUE;\r
168                 }\r
169 \r
170                 if( sError == pdFALSE )\r
171                 {\r
172                         /* If the calculation has always been correct, increment the check \r
173                         variable so we know this task is still running okay. */\r
174                         ( *pusTaskCheckVariable )++;\r
175                 }\r
176 \r
177                 #if configUSE_PREEMPTION == 0\r
178                         taskYIELD();\r
179                 #endif\r
180 \r
181         }\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
186 {\r
187 volatile portDOUBLE d1, d2, d3, d4;\r
188 volatile unsigned short *pusTaskCheckVariable;\r
189 volatile portDOUBLE dAnswer;\r
190 short sError = pdFALSE;\r
191 \r
192         /* Some ports require that tasks that use a hardware floating point unit\r
193         tell the kernel that they require a floating point context before any\r
194         floating point instructions are executed. */\r
195         portTASK_USES_FLOATING_POINT();\r
196 \r
197         d1 = -389.38;\r
198         d2 = 32498.2;\r
199         d3 = -2.0001;\r
200 \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                 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 configUSE_PREEMPTION == 0\r
218                         taskYIELD();\r
219                 #endif\r
220                 \r
221                 /* If the calculation does not match the expected constant, stop the \r
222                 increment of the check variable. */\r
223                 if( fabs( d4 - dAnswer ) > 0.001 )\r
224                 {\r
225                         sError = pdTRUE;\r
226                 }\r
227 \r
228                 if( sError == pdFALSE )\r
229                 {\r
230                         /* If the calculation has always been correct, increment the check \r
231                         variable so we know\r
232                         this task is still running okay. */\r
233                         ( *pusTaskCheckVariable )++;\r
234                 }\r
235 \r
236                 #if configUSE_PREEMPTION == 0\r
237                         taskYIELD();\r
238                 #endif\r
239         }\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
244 {\r
245 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
246 volatile unsigned short *pusTaskCheckVariable;\r
247 const size_t xArraySize = 10;\r
248 size_t xPosition;\r
249 short sError = pdFALSE;\r
250 \r
251         /* Some ports require that tasks that use a hardware floating point unit\r
252         tell the kernel that they require a floating point context before any\r
253         floating point instructions are executed. */\r
254         portTASK_USES_FLOATING_POINT();\r
255 \r
256         /* The variable this task increments to show it is still running is passed in \r
257         as the parameter. */\r
258         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
259 \r
260         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
261 \r
262         /* Keep filling an array, keeping a running total of the values placed in the \r
263         array.  Then run through the array adding up all the values.  If the two totals \r
264         do not match, stop the check variable from incrementing. */\r
265         for( ;; )\r
266         {\r
267                 dTotal1 = 0.0;\r
268                 dTotal2 = 0.0;\r
269 \r
270                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
271                 {\r
272                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;\r
273                         dTotal1 += ( portDOUBLE ) xPosition + 5.5;      \r
274                 }\r
275 \r
276                 #if configUSE_PREEMPTION == 0\r
277                         taskYIELD();\r
278                 #endif\r
279 \r
280                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
281                 {\r
282                         dTotal2 += pdArray[ xPosition ];\r
283                 }\r
284 \r
285                 dDifference = dTotal1 - dTotal2;\r
286                 if( fabs( dDifference ) > 0.001 )\r
287                 {\r
288                         sError = pdTRUE;\r
289                 }\r
290 \r
291                 #if configUSE_PREEMPTION == 0\r
292                         taskYIELD();\r
293                 #endif\r
294 \r
295                 if( sError == pdFALSE )\r
296                 {\r
297                         /* If the calculation has always been correct, increment the check \r
298                         variable so we know     this task is still running okay. */\r
299                         ( *pusTaskCheckVariable )++;\r
300                 }\r
301         }\r
302 }\r
303 /*-----------------------------------------------------------*/\r
304 \r
305 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
306 {\r
307 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
308 volatile unsigned short *pusTaskCheckVariable;\r
309 const size_t xArraySize = 10;\r
310 size_t xPosition;\r
311 short sError = pdFALSE;\r
312 \r
313         /* Some ports require that tasks that use a hardware floating point unit\r
314         tell the kernel that they require a floating point context before any\r
315         floating point instructions are executed. */\r
316         portTASK_USES_FLOATING_POINT();\r
317 \r
318         /* The variable this task increments to show it is still running is passed in \r
319         as the parameter. */\r
320         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
321 \r
322         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
323 \r
324         /* Keep filling an array, keeping a running total of the values placed in the \r
325         array.  Then run through the array adding up all the values.  If the two totals \r
326         do not match, stop the check variable from incrementing. */\r
327         for( ;; )\r
328         {\r
329                 dTotal1 = 0.0;\r
330                 dTotal2 = 0.0;\r
331 \r
332                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
333                 {\r
334                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;\r
335                         dTotal1 += ( portDOUBLE ) xPosition * 12.123;   \r
336                 }\r
337 \r
338                 #if configUSE_PREEMPTION == 0\r
339                         taskYIELD();\r
340                 #endif\r
341 \r
342                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
343                 {\r
344                         dTotal2 += pdArray[ xPosition ];\r
345                 }\r
346 \r
347                 dDifference = dTotal1 - dTotal2;\r
348                 if( fabs( dDifference ) > 0.001 )\r
349                 {\r
350                         sError = pdTRUE;\r
351                 }\r
352 \r
353                 #if configUSE_PREEMPTION == 0\r
354                         taskYIELD();\r
355                 #endif\r
356 \r
357                 if( sError == pdFALSE )\r
358                 {\r
359                         /* If the calculation has always been correct, increment the check \r
360                         variable so we know     this task is still running okay. */\r
361                         ( *pusTaskCheckVariable )++;\r
362                 }\r
363         }\r
364 }                                \r
365 /*-----------------------------------------------------------*/\r
366 \r
367 /* This is called to check that all the created tasks are still running. */\r
368 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
369 {\r
370 /* Keep a history of the check variables so we know if they have been incremented \r
371 since the last call. */\r
372 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
373 portBASE_TYPE xReturn = pdTRUE, xTask;\r
374 \r
375         /* Check the maths tasks are still running by ensuring their check variables \r
376         are still incrementing. */\r
377         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
378         {\r
379                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
380                 {\r
381                         /* The check has not incremented so an error exists. */\r
382                         xReturn = pdFALSE;\r
383                 }\r
384 \r
385                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
386         }\r
387 \r
388         return xReturn;\r
389 }\r
390 \r
391 \r
392 \r