]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/flop.c
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@82 1d2547de-c912-0410-9cb9...
[freertos] / Demo / Common / Minimal / flop.c
1 /*\r
2         FreeRTOS.org V4.3.0 - Copyright (C) 2003-2007 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         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 /*\r
37  * Creates eight tasks, each of which loops continuously performing an (emulated) \r
38  * floating point calculation.\r
39  *\r
40  * All the tasks run at the idle priority and never block or yield.  This causes \r
41  * all eight tasks to time slice with the idle task.  Running at the idle priority \r
42  * means that these tasks will get pre-empted any time another task is ready to run\r
43  * or a time slice occurs.  More often than not the pre-emption will occur mid \r
44  * calculation, creating a good test of the schedulers context switch mechanism - a \r
45  * calculation producing an unexpected result could be a symptom of a corruption in \r
46  * the context of a task.\r
47  */\r
48 \r
49 #include <stdlib.h>\r
50 #include <math.h>\r
51 \r
52 /* Scheduler include files. */\r
53 #include "FreeRTOS.h"\r
54 #include "task.h"\r
55 \r
56 /* Demo program include files. */\r
57 #include "flop.h"\r
58 \r
59 #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE\r
60 #define mathNUMBER_OF_TASKS  ( 8 )\r
61 \r
62 /* Four tasks, each of which performs a different floating point calculation.  \r
63 Each of the four is created twice. */\r
64 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );\r
65 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );\r
66 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );\r
67 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );\r
68 \r
69 /* These variables are used to check that all the tasks are still running.  If a \r
70 task gets a calculation wrong it will\r
71 stop incrementing its check variable. */\r
72 static volatile unsigned portSHORT usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };\r
73 \r
74 /*-----------------------------------------------------------*/\r
75 \r
76 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
77 {\r
78         xTaskCreate( vCompetingMathTask1, ( signed portCHAR * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
79         xTaskCreate( vCompetingMathTask2, ( signed portCHAR * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
80         xTaskCreate( vCompetingMathTask3, ( signed portCHAR * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
81         xTaskCreate( vCompetingMathTask4, ( signed portCHAR * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
82         xTaskCreate( vCompetingMathTask1, ( signed portCHAR * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );\r
83         xTaskCreate( vCompetingMathTask2, ( signed portCHAR * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );\r
84         xTaskCreate( vCompetingMathTask3, ( signed portCHAR * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );\r
85         xTaskCreate( vCompetingMathTask4, ( signed portCHAR * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );\r
86 }\r
87 /*-----------------------------------------------------------*/\r
88 \r
89 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
90 {\r
91 volatile portDOUBLE d1, d2, d3, d4;\r
92 volatile unsigned portSHORT *pusTaskCheckVariable;\r
93 volatile portDOUBLE dAnswer;\r
94 portSHORT sError = pdFALSE;\r
95 \r
96         d1 = 123.4567;\r
97         d2 = 2345.6789;\r
98         d3 = -918.222;\r
99 \r
100         dAnswer = ( d1 + d2 ) * d3;\r
101 \r
102         /* The variable this task increments to show it is still running is passed in \r
103         as the parameter. */\r
104         pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;\r
105 \r
106         /* Keep performing a calculation and checking the result against a constant. */\r
107         for(;;)\r
108         {\r
109                 d1 = 123.4567;\r
110                 d2 = 2345.6789;\r
111                 d3 = -918.222;\r
112 \r
113                 d4 = ( d1 + d2 ) * d3;\r
114 \r
115                 #if configUSE_PREEMPTION == 0\r
116                         taskYIELD();\r
117                 #endif\r
118 \r
119                 /* If the calculation does not match the expected constant, stop the \r
120                 increment of the check variable. */\r
121                 if( fabs( d4 - dAnswer ) > 0.001 )\r
122                 {\r
123                         sError = pdTRUE;\r
124                 }\r
125 \r
126                 if( sError == pdFALSE )\r
127                 {\r
128                         /* If the calculation has always been correct, increment the check \r
129                         variable so we know this task is still running okay. */\r
130                         ( *pusTaskCheckVariable )++;\r
131                 }\r
132 \r
133                 #if configUSE_PREEMPTION == 0\r
134                         taskYIELD();\r
135                 #endif\r
136 \r
137         }\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
142 {\r
143 volatile portDOUBLE d1, d2, d3, d4;\r
144 volatile unsigned portSHORT *pusTaskCheckVariable;\r
145 volatile portDOUBLE dAnswer;\r
146 portSHORT sError = pdFALSE;\r
147 \r
148         d1 = -389.38;\r
149         d2 = 32498.2;\r
150         d3 = -2.0001;\r
151 \r
152         dAnswer = ( d1 / d2 ) * d3;\r
153 \r
154 \r
155         /* The variable this task increments to show it is still running is passed in \r
156         as the parameter. */\r
157         pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;\r
158 \r
159         /* Keep performing a calculation and checking the result against a constant. */\r
160         for( ;; )\r
161         {\r
162                 d1 = -389.38;\r
163                 d2 = 32498.2;\r
164                 d3 = -2.0001;\r
165 \r
166                 d4 = ( d1 / d2 ) * d3;\r
167 \r
168                 #if configUSE_PREEMPTION == 0\r
169                         taskYIELD();\r
170                 #endif\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\r
183                         this task is still running okay. */\r
184                         ( *pusTaskCheckVariable )++;\r
185                 }\r
186 \r
187                 #if configUSE_PREEMPTION == 0\r
188                         taskYIELD();\r
189                 #endif\r
190         }\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
195 {\r
196 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
197 volatile unsigned portSHORT *pusTaskCheckVariable;\r
198 const size_t xArraySize = 10;\r
199 size_t xPosition;\r
200 portSHORT sError = pdFALSE;\r
201 \r
202         /* The variable this task increments to show it is still running is passed in \r
203         as the parameter. */\r
204         pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;\r
205 \r
206         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
207 \r
208         /* Keep filling an array, keeping a running total of the values placed in the \r
209         array.  Then run through the array adding up all the values.  If the two totals \r
210         do not match, stop the check variable from incrementing. */\r
211         for( ;; )\r
212         {\r
213                 dTotal1 = 0.0;\r
214                 dTotal2 = 0.0;\r
215 \r
216                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
217                 {\r
218                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;\r
219                         dTotal1 += ( portDOUBLE ) xPosition + 5.5;      \r
220                 }\r
221 \r
222                 #if configUSE_PREEMPTION == 0\r
223                         taskYIELD();\r
224                 #endif\r
225 \r
226                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
227                 {\r
228                         dTotal2 += pdArray[ xPosition ];\r
229                 }\r
230 \r
231                 dDifference = dTotal1 - dTotal2;\r
232                 if( fabs( dDifference ) > 0.001 )\r
233                 {\r
234                         sError = pdTRUE;\r
235                 }\r
236 \r
237                 #if configUSE_PREEMPTION == 0\r
238                         taskYIELD();\r
239                 #endif\r
240 \r
241                 if( sError == pdFALSE )\r
242                 {\r
243                         /* If the calculation has always been correct, increment the check \r
244                         variable so we know     this task is still running okay. */\r
245                         ( *pusTaskCheckVariable )++;\r
246                 }\r
247         }\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
252 {\r
253 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
254 volatile unsigned portSHORT *pusTaskCheckVariable;\r
255 const size_t xArraySize = 10;\r
256 size_t xPosition;\r
257 portSHORT sError = pdFALSE;\r
258 \r
259         /* The variable this task increments to show it is still running is passed in \r
260         as the parameter. */\r
261         pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;\r
262 \r
263         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
264 \r
265         /* Keep filling an array, keeping a running total of the values placed in the \r
266         array.  Then run through the array adding up all the values.  If the two totals \r
267         do not match, stop the check variable from incrementing. */\r
268         for( ;; )\r
269         {\r
270                 dTotal1 = 0.0;\r
271                 dTotal2 = 0.0;\r
272 \r
273                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
274                 {\r
275                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;\r
276                         dTotal1 += ( portDOUBLE ) xPosition * 12.123;   \r
277                 }\r
278 \r
279                 #if configUSE_PREEMPTION == 0\r
280                         taskYIELD();\r
281                 #endif\r
282 \r
283                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
284                 {\r
285                         dTotal2 += pdArray[ xPosition ];\r
286                 }\r
287 \r
288                 dDifference = dTotal1 - dTotal2;\r
289                 if( fabs( dDifference ) > 0.001 )\r
290                 {\r
291                         sError = pdTRUE;\r
292                 }\r
293 \r
294                 #if configUSE_PREEMPTION == 0\r
295                         taskYIELD();\r
296                 #endif\r
297 \r
298                 if( sError == pdFALSE )\r
299                 {\r
300                         /* If the calculation has always been correct, increment the check \r
301                         variable so we know     this task is still running okay. */\r
302                         ( *pusTaskCheckVariable )++;\r
303                 }\r
304         }\r
305 }                                \r
306 /*-----------------------------------------------------------*/\r
307 \r
308 /* This is called to check that all the created tasks are still running. */\r
309 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
310 {\r
311 /* Keep a history of the check variables so we know if they have been incremented \r
312 since the last call. */\r
313 static unsigned portSHORT usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };\r
314 portBASE_TYPE xReturn = pdTRUE, xTask;\r
315 \r
316         /* Check the maths tasks are still running by ensuring their check variables \r
317         are still incrementing. */\r
318         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
319         {\r
320                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
321                 {\r
322                         /* The check has not incremented so an error exists. */\r
323                         xReturn = pdFALSE;\r
324                 }\r
325 \r
326                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
327         }\r
328 \r
329         return xReturn;\r
330 }\r
331 \r
332 \r
333 \r