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