]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/flop.c
Ready for V5.1.1 release.
[freertos] / Demo / Common / Minimal / flop.c
1 /*\r
2         FreeRTOS.org V5.1.1 - Copyright (C) 2003-2008 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26     ***************************************************************************\r
27     ***************************************************************************\r
28     *                                                                         *\r
29     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
30     * and even write all or part of your application on your behalf.          *\r
31     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
32     * expedite your project.                                                  *\r
33     *                                                                         *\r
34     ***************************************************************************\r
35     ***************************************************************************\r
36 \r
37         Please ensure to read the configuration and relevant port sections of the\r
38         online documentation.\r
39 \r
40         http://www.FreeRTOS.org - Documentation, latest information, license and \r
41         contact details.\r
42 \r
43         http://www.SafeRTOS.com - A version that is certified for use in safety \r
44         critical systems.\r
45 \r
46         http://www.OpenRTOS.com - Commercial support, development, porting, \r
47         licensing and training services.\r
48 */\r
49 \r
50 /*\r
51  * Creates eight tasks, each of which loops continuously performing an (emulated) \r
52  * floating point calculation.\r
53  *\r
54  * All the tasks run at the idle priority and never block or yield.  This causes \r
55  * all eight tasks to time slice with the idle task.  Running at the idle priority \r
56  * means that these tasks will get pre-empted any time another task is ready to run\r
57  * or a time slice occurs.  More often than not the pre-emption will occur mid \r
58  * calculation, creating a good test of the schedulers context switch mechanism - a \r
59  * calculation producing an unexpected result could be a symptom of a corruption in \r
60  * the context of a task.\r
61  */\r
62 \r
63 #include <stdlib.h>\r
64 #include <math.h>\r
65 \r
66 /* Scheduler include files. */\r
67 #include "FreeRTOS.h"\r
68 #include "task.h"\r
69 \r
70 /* Demo program include files. */\r
71 #include "flop.h"\r
72 \r
73 #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE\r
74 #define mathNUMBER_OF_TASKS  ( 8 )\r
75 \r
76 /* Four tasks, each of which performs a different floating point calculation.  \r
77 Each of the four is created twice. */\r
78 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );\r
79 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );\r
80 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );\r
81 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );\r
82 \r
83 /* These variables are used to check that all the tasks are still running.  If a \r
84 task gets a calculation wrong it will\r
85 stop incrementing its check variable. */\r
86 static volatile unsigned portSHORT usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };\r
87 \r
88 /*-----------------------------------------------------------*/\r
89 \r
90 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
91 {\r
92         xTaskCreate( vCompetingMathTask1, ( signed portCHAR * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
93         xTaskCreate( vCompetingMathTask2, ( signed portCHAR * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
94         xTaskCreate( vCompetingMathTask3, ( signed portCHAR * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
95         xTaskCreate( vCompetingMathTask4, ( signed portCHAR * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
96         xTaskCreate( vCompetingMathTask1, ( signed portCHAR * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );\r
97         xTaskCreate( vCompetingMathTask2, ( signed portCHAR * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );\r
98         xTaskCreate( vCompetingMathTask3, ( signed portCHAR * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );\r
99         xTaskCreate( vCompetingMathTask4, ( signed portCHAR * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );\r
100 }\r
101 /*-----------------------------------------------------------*/\r
102 \r
103 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
104 {\r
105 volatile portDOUBLE d1, d2, d3, d4;\r
106 volatile unsigned portSHORT *pusTaskCheckVariable;\r
107 volatile portDOUBLE dAnswer;\r
108 portSHORT sError = pdFALSE;\r
109 \r
110         d1 = 123.4567;\r
111         d2 = 2345.6789;\r
112         d3 = -918.222;\r
113 \r
114         dAnswer = ( d1 + d2 ) * d3;\r
115 \r
116         /* The variable this task increments to show it is still running is passed in \r
117         as the parameter. */\r
118         pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;\r
119 \r
120         /* Keep performing a calculation and checking the result against a constant. */\r
121         for(;;)\r
122         {\r
123                 d1 = 123.4567;\r
124                 d2 = 2345.6789;\r
125                 d3 = -918.222;\r
126 \r
127                 d4 = ( d1 + d2 ) * d3;\r
128 \r
129                 #if configUSE_PREEMPTION == 0\r
130                         taskYIELD();\r
131                 #endif\r
132 \r
133                 /* If the calculation does not match the expected constant, stop the \r
134                 increment of the check variable. */\r
135                 if( fabs( d4 - dAnswer ) > 0.001 )\r
136                 {\r
137                         sError = pdTRUE;\r
138                 }\r
139 \r
140                 if( sError == pdFALSE )\r
141                 {\r
142                         /* If the calculation has always been correct, increment the check \r
143                         variable so we know this task is still running okay. */\r
144                         ( *pusTaskCheckVariable )++;\r
145                 }\r
146 \r
147                 #if configUSE_PREEMPTION == 0\r
148                         taskYIELD();\r
149                 #endif\r
150 \r
151         }\r
152 }\r
153 /*-----------------------------------------------------------*/\r
154 \r
155 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
156 {\r
157 volatile portDOUBLE d1, d2, d3, d4;\r
158 volatile unsigned portSHORT *pusTaskCheckVariable;\r
159 volatile portDOUBLE dAnswer;\r
160 portSHORT sError = pdFALSE;\r
161 \r
162         d1 = -389.38;\r
163         d2 = 32498.2;\r
164         d3 = -2.0001;\r
165 \r
166         dAnswer = ( d1 / d2 ) * d3;\r
167 \r
168 \r
169         /* The variable this task increments to show it is still running is passed in \r
170         as the parameter. */\r
171         pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;\r
172 \r
173         /* Keep performing a calculation and checking the result against a constant. */\r
174         for( ;; )\r
175         {\r
176                 d1 = -389.38;\r
177                 d2 = 32498.2;\r
178                 d3 = -2.0001;\r
179 \r
180                 d4 = ( d1 / d2 ) * d3;\r
181 \r
182                 #if configUSE_PREEMPTION == 0\r
183                         taskYIELD();\r
184                 #endif\r
185                 \r
186                 /* If the calculation does not match the expected constant, stop the \r
187                 increment of the check variable. */\r
188                 if( fabs( d4 - dAnswer ) > 0.001 )\r
189                 {\r
190                         sError = pdTRUE;\r
191                 }\r
192 \r
193                 if( sError == pdFALSE )\r
194                 {\r
195                         /* If the calculation has always been correct, increment the check \r
196                         variable so we know\r
197                         this task is still running okay. */\r
198                         ( *pusTaskCheckVariable )++;\r
199                 }\r
200 \r
201                 #if configUSE_PREEMPTION == 0\r
202                         taskYIELD();\r
203                 #endif\r
204         }\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
209 {\r
210 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
211 volatile unsigned portSHORT *pusTaskCheckVariable;\r
212 const size_t xArraySize = 10;\r
213 size_t xPosition;\r
214 portSHORT sError = pdFALSE;\r
215 \r
216         /* The variable this task increments to show it is still running is passed in \r
217         as the parameter. */\r
218         pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;\r
219 \r
220         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
221 \r
222         /* Keep filling an array, keeping a running total of the values placed in the \r
223         array.  Then run through the array adding up all the values.  If the two totals \r
224         do not match, stop the check variable from incrementing. */\r
225         for( ;; )\r
226         {\r
227                 dTotal1 = 0.0;\r
228                 dTotal2 = 0.0;\r
229 \r
230                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
231                 {\r
232                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;\r
233                         dTotal1 += ( portDOUBLE ) xPosition + 5.5;      \r
234                 }\r
235 \r
236                 #if configUSE_PREEMPTION == 0\r
237                         taskYIELD();\r
238                 #endif\r
239 \r
240                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
241                 {\r
242                         dTotal2 += pdArray[ xPosition ];\r
243                 }\r
244 \r
245                 dDifference = dTotal1 - dTotal2;\r
246                 if( fabs( dDifference ) > 0.001 )\r
247                 {\r
248                         sError = pdTRUE;\r
249                 }\r
250 \r
251                 #if configUSE_PREEMPTION == 0\r
252                         taskYIELD();\r
253                 #endif\r
254 \r
255                 if( sError == pdFALSE )\r
256                 {\r
257                         /* If the calculation has always been correct, increment the check \r
258                         variable so we know     this task is still running okay. */\r
259                         ( *pusTaskCheckVariable )++;\r
260                 }\r
261         }\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
266 {\r
267 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
268 volatile unsigned portSHORT *pusTaskCheckVariable;\r
269 const size_t xArraySize = 10;\r
270 size_t xPosition;\r
271 portSHORT sError = pdFALSE;\r
272 \r
273         /* The variable this task increments to show it is still running is passed in \r
274         as the parameter. */\r
275         pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;\r
276 \r
277         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
278 \r
279         /* Keep filling an array, keeping a running total of the values placed in the \r
280         array.  Then run through the array adding up all the values.  If the two totals \r
281         do not match, stop the check variable from incrementing. */\r
282         for( ;; )\r
283         {\r
284                 dTotal1 = 0.0;\r
285                 dTotal2 = 0.0;\r
286 \r
287                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
288                 {\r
289                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;\r
290                         dTotal1 += ( portDOUBLE ) xPosition * 12.123;   \r
291                 }\r
292 \r
293                 #if configUSE_PREEMPTION == 0\r
294                         taskYIELD();\r
295                 #endif\r
296 \r
297                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
298                 {\r
299                         dTotal2 += pdArray[ xPosition ];\r
300                 }\r
301 \r
302                 dDifference = dTotal1 - dTotal2;\r
303                 if( fabs( dDifference ) > 0.001 )\r
304                 {\r
305                         sError = pdTRUE;\r
306                 }\r
307 \r
308                 #if configUSE_PREEMPTION == 0\r
309                         taskYIELD();\r
310                 #endif\r
311 \r
312                 if( sError == pdFALSE )\r
313                 {\r
314                         /* If the calculation has always been correct, increment the check \r
315                         variable so we know     this task is still running okay. */\r
316                         ( *pusTaskCheckVariable )++;\r
317                 }\r
318         }\r
319 }                                \r
320 /*-----------------------------------------------------------*/\r
321 \r
322 /* This is called to check that all the created tasks are still running. */\r
323 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
324 {\r
325 /* Keep a history of the check variables so we know if they have been incremented \r
326 since the last call. */\r
327 static unsigned portSHORT usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };\r
328 portBASE_TYPE xReturn = pdTRUE, xTask;\r
329 \r
330         /* Check the maths tasks are still running by ensuring their check variables \r
331         are still incrementing. */\r
332         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
333         {\r
334                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
335                 {\r
336                         /* The check has not incremented so an error exists. */\r
337                         xReturn = pdFALSE;\r
338                 }\r
339 \r
340                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
341         }\r
342 \r
343         return xReturn;\r
344 }\r
345 \r
346 \r
347 \r