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