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