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