]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/flop.c
Prepare for V7.2.0 release.
[freertos] / FreeRTOS / Demo / Common / Minimal / flop.c
1 /*\r
2     FreeRTOS V7.2.0 - 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 an (emulated) \r
69  * floating point calculation.\r
70  *\r
71  * All the tasks run at the idle priority and never block or yield.  This causes \r
72  * all eight tasks to time slice with the idle task.  Running at the idle priority \r
73  * means that these tasks will get pre-empted any time another task is ready to run\r
74  * or a time slice occurs.  More often than not the pre-emption will occur mid \r
75  * calculation, creating a good test of the schedulers context switch mechanism - a \r
76  * calculation producing an unexpected result could be a symptom of a corruption in \r
77  * the context of a task.\r
78  */\r
79 \r
80 #include <stdlib.h>\r
81 #include <math.h>\r
82 \r
83 /* Scheduler include files. */\r
84 #include "FreeRTOS.h"\r
85 #include "task.h"\r
86 \r
87 /* Demo program include files. */\r
88 #include "flop.h"\r
89 \r
90 #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE\r
91 #define mathNUMBER_OF_TASKS  ( 8 )\r
92 \r
93 /* Four tasks, each of which performs a different floating point calculation.  \r
94 Each of the four is created twice. */\r
95 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );\r
96 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );\r
97 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );\r
98 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );\r
99 \r
100 /* These variables are used to check that all the tasks are still running.  If a \r
101 task gets a calculation wrong it will\r
102 stop incrementing its check variable. */\r
103 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
108 {\r
109         xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
110         xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
111         xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
112         xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
113         xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );\r
114         xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );\r
115         xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );\r
116         xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );\r
117 }\r
118 /*-----------------------------------------------------------*/\r
119 \r
120 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
121 {\r
122 volatile portDOUBLE d1, d2, d3, d4;\r
123 volatile unsigned short *pusTaskCheckVariable;\r
124 volatile portDOUBLE dAnswer;\r
125 short sError = pdFALSE;\r
126 \r
127         d1 = 123.4567;\r
128         d2 = 2345.6789;\r
129         d3 = -918.222;\r
130 \r
131         dAnswer = ( d1 + d2 ) * d3;\r
132 \r
133         /* The variable this task increments to show it is still running is passed in \r
134         as the parameter. */\r
135         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
136 \r
137         /* Keep performing a calculation and checking the result against a constant. */\r
138         for(;;)\r
139         {\r
140                 d1 = 123.4567;\r
141                 d2 = 2345.6789;\r
142                 d3 = -918.222;\r
143 \r
144                 d4 = ( d1 + d2 ) * d3;\r
145 \r
146                 #if configUSE_PREEMPTION == 0\r
147                         taskYIELD();\r
148                 #endif\r
149 \r
150                 /* If the calculation does not match the expected constant, stop the \r
151                 increment of the check variable. */\r
152                 if( fabs( d4 - dAnswer ) > 0.001 )\r
153                 {\r
154                         sError = pdTRUE;\r
155                 }\r
156 \r
157                 if( sError == pdFALSE )\r
158                 {\r
159                         /* If the calculation has always been correct, increment the check \r
160                         variable so we know this task is still running okay. */\r
161                         ( *pusTaskCheckVariable )++;\r
162                 }\r
163 \r
164                 #if configUSE_PREEMPTION == 0\r
165                         taskYIELD();\r
166                 #endif\r
167 \r
168         }\r
169 }\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
173 {\r
174 volatile portDOUBLE d1, d2, d3, d4;\r
175 volatile unsigned short *pusTaskCheckVariable;\r
176 volatile portDOUBLE dAnswer;\r
177 short sError = pdFALSE;\r
178 \r
179         d1 = -389.38;\r
180         d2 = 32498.2;\r
181         d3 = -2.0001;\r
182 \r
183         dAnswer = ( d1 / d2 ) * d3;\r
184 \r
185 \r
186         /* The variable this task increments to show it is still running is passed in \r
187         as the parameter. */\r
188         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
189 \r
190         /* Keep performing a calculation and checking the result against a constant. */\r
191         for( ;; )\r
192         {\r
193                 d1 = -389.38;\r
194                 d2 = 32498.2;\r
195                 d3 = -2.0001;\r
196 \r
197                 d4 = ( d1 / d2 ) * d3;\r
198 \r
199                 #if configUSE_PREEMPTION == 0\r
200                         taskYIELD();\r
201                 #endif\r
202                 \r
203                 /* If the calculation does not match the expected constant, stop the \r
204                 increment of the check variable. */\r
205                 if( fabs( d4 - dAnswer ) > 0.001 )\r
206                 {\r
207                         sError = pdTRUE;\r
208                 }\r
209 \r
210                 if( sError == pdFALSE )\r
211                 {\r
212                         /* If the calculation has always been correct, increment the check \r
213                         variable so we know\r
214                         this task is still running okay. */\r
215                         ( *pusTaskCheckVariable )++;\r
216                 }\r
217 \r
218                 #if configUSE_PREEMPTION == 0\r
219                         taskYIELD();\r
220                 #endif\r
221         }\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
226 {\r
227 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
228 volatile unsigned short *pusTaskCheckVariable;\r
229 const size_t xArraySize = 10;\r
230 size_t xPosition;\r
231 short sError = pdFALSE;\r
232 \r
233         /* The variable this task increments to show it is still running is passed in \r
234         as the parameter. */\r
235         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
236 \r
237         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
238 \r
239         /* Keep filling an array, keeping a running total of the values placed in the \r
240         array.  Then run through the array adding up all the values.  If the two totals \r
241         do not match, stop the check variable from incrementing. */\r
242         for( ;; )\r
243         {\r
244                 dTotal1 = 0.0;\r
245                 dTotal2 = 0.0;\r
246 \r
247                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
248                 {\r
249                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;\r
250                         dTotal1 += ( portDOUBLE ) xPosition + 5.5;      \r
251                 }\r
252 \r
253                 #if configUSE_PREEMPTION == 0\r
254                         taskYIELD();\r
255                 #endif\r
256 \r
257                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
258                 {\r
259                         dTotal2 += pdArray[ xPosition ];\r
260                 }\r
261 \r
262                 dDifference = dTotal1 - dTotal2;\r
263                 if( fabs( dDifference ) > 0.001 )\r
264                 {\r
265                         sError = pdTRUE;\r
266                 }\r
267 \r
268                 #if configUSE_PREEMPTION == 0\r
269                         taskYIELD();\r
270                 #endif\r
271 \r
272                 if( sError == pdFALSE )\r
273                 {\r
274                         /* If the calculation has always been correct, increment the check \r
275                         variable so we know     this task is still running okay. */\r
276                         ( *pusTaskCheckVariable )++;\r
277                 }\r
278         }\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
283 {\r
284 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
285 volatile unsigned short *pusTaskCheckVariable;\r
286 const size_t xArraySize = 10;\r
287 size_t xPosition;\r
288 short sError = pdFALSE;\r
289 \r
290         /* The variable this task increments to show it is still running is passed in \r
291         as the parameter. */\r
292         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
293 \r
294         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
295 \r
296         /* Keep filling an array, keeping a running total of the values placed in the \r
297         array.  Then run through the array adding up all the values.  If the two totals \r
298         do not match, stop the check variable from incrementing. */\r
299         for( ;; )\r
300         {\r
301                 dTotal1 = 0.0;\r
302                 dTotal2 = 0.0;\r
303 \r
304                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
305                 {\r
306                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;\r
307                         dTotal1 += ( portDOUBLE ) xPosition * 12.123;   \r
308                 }\r
309 \r
310                 #if configUSE_PREEMPTION == 0\r
311                         taskYIELD();\r
312                 #endif\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 configUSE_PREEMPTION == 0\r
326                         taskYIELD();\r
327                 #endif\r
328 \r
329                 if( sError == pdFALSE )\r
330                 {\r
331                         /* If the calculation has always been correct, increment the check \r
332                         variable so we know     this task is still running okay. */\r
333                         ( *pusTaskCheckVariable )++;\r
334                 }\r
335         }\r
336 }                                \r
337 /*-----------------------------------------------------------*/\r
338 \r
339 /* This is called to check that all the created tasks are still running. */\r
340 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
341 {\r
342 /* Keep a history of the check variables so we know if they have been incremented \r
343 since the last call. */\r
344 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
345 portBASE_TYPE xReturn = pdTRUE, xTask;\r
346 \r
347         /* Check the maths tasks are still running by ensuring their check variables \r
348         are still incrementing. */\r
349         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
350         {\r
351                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
352                 {\r
353                         /* The check has not incremented so an error exists. */\r
354                         xReturn = pdFALSE;\r
355                 }\r
356 \r
357                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
358         }\r
359 \r
360         return xReturn;\r
361 }\r
362 \r
363 \r
364 \r