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