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