]> git.sur5r.net Git - freertos/blob - Demo/Common/Full/flop.c
b4f2b36e95597b104a047ee4b6d6a4eb82c0e2f0
[freertos] / Demo / Common / Full / flop.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55 Changes from V1.2.3\r
56         \r
57         + The created tasks now include calls to tskYIELD(), allowing them to be used\r
58           with the cooperative scheduler.\r
59 */\r
60 \r
61 /**\r
62  * Creates eight tasks, each of which loops continuously performing an (emulated) \r
63  * floating point calculation.\r
64  *\r
65  * All the tasks run at the idle priority and never block or yield.  This causes \r
66  * all eight tasks to time slice with the idle task.  Running at the idle priority \r
67  * means that these tasks will get pre-empted any time another task is ready to run\r
68  * or a time slice occurs.  More often than not the pre-emption will occur mid \r
69  * calculation, creating a good test of the schedulers context switch mechanism - a \r
70  * calculation producing an unexpected result could be a symptom of a corruption in \r
71  * the context of a task.\r
72  *\r
73  * \page FlopC flop.c\r
74  * \ingroup DemoFiles\r
75  * <HR>\r
76  */\r
77 \r
78 #include <stdlib.h>\r
79 #include <math.h>\r
80 \r
81 /* Scheduler include files. */\r
82 #include "FreeRTOS.h"\r
83 #include "task.h"\r
84 #include "print.h"\r
85 \r
86 /* Demo program include files. */\r
87 #include "flop.h"\r
88 \r
89 #define mathSTACK_SIZE          ( ( unsigned short ) 512 )\r
90 #define mathNUMBER_OF_TASKS  ( 8 )\r
91 \r
92 /* Four tasks, each of which performs a different floating point calculation.  \r
93 Each of the four is created twice. */\r
94 static void vCompetingMathTask1( void *pvParameters );\r
95 static void vCompetingMathTask2( void *pvParameters );\r
96 static void vCompetingMathTask3( void *pvParameters );\r
97 static void vCompetingMathTask4( void *pvParameters );\r
98 \r
99 /* These variables are used to check that all the tasks are still running.  If a \r
100 task gets a calculation wrong it will\r
101 stop incrementing its check variable. */\r
102 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
107 {\r
108         xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
109         xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
110         xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
111         xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
112         xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );\r
113         xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );\r
114         xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );\r
115         xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );\r
116 }\r
117 /*-----------------------------------------------------------*/\r
118 \r
119 static void vCompetingMathTask1( void *pvParameters )\r
120 {\r
121 portDOUBLE d1, d2, d3, d4;\r
122 volatile unsigned short *pusTaskCheckVariable;\r
123 const portDOUBLE dAnswer = ( 123.4567 + 2345.6789 ) * -918.222;\r
124 const char * const pcTaskStartMsg = "Math task 1 started.\r\n";\r
125 const char * const pcTaskFailMsg = "Math task 1 failed.\r\n";\r
126 short sError = pdFALSE;\r
127 \r
128         /* Queue a message for printing to say the task has started. */\r
129         vPrintDisplayMessage( &pcTaskStartMsg );\r
130 \r
131         /* The variable this task increments to show it is still running is passed in \r
132         as the parameter. */\r
133         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
134 \r
135         /* Keep performing a calculation and checking the result against a constant. */\r
136         for(;;)\r
137         {\r
138                 d1 = 123.4567;\r
139                 d2 = 2345.6789;\r
140                 d3 = -918.222;\r
141 \r
142                 d4 = ( d1 + d2 ) * d3;\r
143 \r
144                 taskYIELD();\r
145 \r
146                 /* If the calculation does not match the expected constant, stop the \r
147                 increment of the check variable. */\r
148                 if( fabs( d4 - dAnswer ) > 0.001 )\r
149                 {\r
150                         vPrintDisplayMessage( &pcTaskFailMsg );\r
151                         sError = pdTRUE;\r
152                 }\r
153 \r
154                 if( sError == pdFALSE )\r
155                 {\r
156                         /* If the calculation has always been correct, increment the check \r
157                         variable so we know this task is still running okay. */\r
158                         ( *pusTaskCheckVariable )++;\r
159                 }\r
160 \r
161                 taskYIELD();\r
162         }\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 static void vCompetingMathTask2( void *pvParameters )\r
167 {\r
168 portDOUBLE d1, d2, d3, d4;\r
169 volatile unsigned short *pusTaskCheckVariable;\r
170 const portDOUBLE dAnswer = ( -389.38 / 32498.2 ) * -2.0001;\r
171 const char * const pcTaskStartMsg = "Math task 2 started.\r\n";\r
172 const char * const pcTaskFailMsg = "Math task 2 failed.\r\n";\r
173 short sError = pdFALSE;\r
174 \r
175         /* Queue a message for printing to say the task has started. */\r
176         vPrintDisplayMessage( &pcTaskStartMsg );\r
177 \r
178         /* The variable this task increments to show it is still running is passed in \r
179         as the parameter. */\r
180         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
181 \r
182         /* Keep performing a calculation and checking the result against a constant. */\r
183         for( ;; )\r
184         {\r
185                 d1 = -389.38;\r
186                 d2 = 32498.2;\r
187                 d3 = -2.0001;\r
188 \r
189                 d4 = ( d1 / d2 ) * d3;\r
190 \r
191                 taskYIELD();\r
192                 \r
193                 /* If the calculation does not match the expected constant, stop the \r
194                 increment of the check variable. */\r
195                 if( fabs( d4 - dAnswer ) > 0.001 )\r
196                 {\r
197                         vPrintDisplayMessage( &pcTaskFailMsg );\r
198                         sError = pdTRUE;\r
199                 }\r
200 \r
201                 if( sError == pdFALSE )\r
202                 {\r
203                         /* If the calculation has always been correct, increment the check \r
204                         variable so we know\r
205                         this task is still running okay. */\r
206                         ( *pusTaskCheckVariable )++;\r
207                 }\r
208 \r
209                 taskYIELD();\r
210         }\r
211 }\r
212 /*-----------------------------------------------------------*/\r
213 \r
214 static void vCompetingMathTask3( void *pvParameters )\r
215 {\r
216 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
217 volatile unsigned short *pusTaskCheckVariable;\r
218 const unsigned short usArraySize = 250;\r
219 unsigned short usPosition;\r
220 const char * const pcTaskStartMsg = "Math task 3 started.\r\n";\r
221 const char * const pcTaskFailMsg = "Math task 3 failed.\r\n";\r
222 short sError = pdFALSE;\r
223 \r
224         /* Queue a message for printing to say the task has started. */\r
225         vPrintDisplayMessage( &pcTaskStartMsg );\r
226 \r
227         /* The variable this task increments to show it is still running is passed in \r
228         as the parameter. */\r
229         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
230 \r
231         pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );\r
232 \r
233         /* Keep filling an array, keeping a running total of the values placed in the \r
234         array.  Then run through the array adding up all the values.  If the two totals \r
235         do not match, stop the check variable from incrementing. */\r
236         for( ;; )\r
237         {\r
238                 dTotal1 = 0.0;\r
239                 dTotal2 = 0.0;\r
240 \r
241                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
242                 {\r
243                         pdArray[ usPosition ] = ( portDOUBLE ) usPosition + 5.5;\r
244                         dTotal1 += ( portDOUBLE ) usPosition + 5.5;     \r
245                 }\r
246 \r
247                 taskYIELD();\r
248 \r
249                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
250                 {\r
251                         dTotal2 += pdArray[ usPosition ];\r
252                 }\r
253 \r
254                 dDifference = dTotal1 - dTotal2;\r
255                 if( fabs( dDifference ) > 0.001 )\r
256                 {\r
257                         vPrintDisplayMessage( &pcTaskFailMsg );\r
258                         sError = pdTRUE;\r
259                 }\r
260 \r
261                 taskYIELD();\r
262 \r
263                 if( sError == pdFALSE )\r
264                 {\r
265                         /* If the calculation has always been correct, increment the check \r
266                         variable so we know     this task is still running okay. */\r
267                         ( *pusTaskCheckVariable )++;\r
268                 }\r
269         }\r
270 }\r
271 /*-----------------------------------------------------------*/\r
272 \r
273 static void vCompetingMathTask4( void *pvParameters )\r
274 {\r
275 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
276 volatile unsigned short *pusTaskCheckVariable;\r
277 const unsigned short usArraySize = 250;\r
278 unsigned short usPosition;\r
279 const char * const pcTaskStartMsg = "Math task 4 started.\r\n";\r
280 const char * const pcTaskFailMsg = "Math task 4 failed.\r\n";\r
281 short sError = pdFALSE;\r
282 \r
283         /* Queue a message for printing to say the task has started. */\r
284         vPrintDisplayMessage( &pcTaskStartMsg );\r
285 \r
286         /* The variable this task increments to show it is still running is passed in \r
287         as the parameter. */\r
288         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
289 \r
290         pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );\r
291 \r
292         /* Keep filling an array, keeping a running total of the values placed in the \r
293         array.  Then run through the array adding up all the values.  If the two totals \r
294         do not match, stop the check variable from incrementing. */\r
295         for( ;; )\r
296         {\r
297                 dTotal1 = 0.0;\r
298                 dTotal2 = 0.0;\r
299 \r
300                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
301                 {\r
302                         pdArray[ usPosition ] = ( portDOUBLE ) usPosition * 12.123;\r
303                         dTotal1 += ( portDOUBLE ) usPosition * 12.123;  \r
304                 }\r
305 \r
306                 taskYIELD();\r
307 \r
308                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
309                 {\r
310                         dTotal2 += pdArray[ usPosition ];\r
311                 }\r
312 \r
313                 dDifference = dTotal1 - dTotal2;\r
314                 if( fabs( dDifference ) > 0.001 )\r
315                 {\r
316                         vPrintDisplayMessage( &pcTaskFailMsg );\r
317                         sError = pdTRUE;\r
318                 }\r
319 \r
320                 taskYIELD();\r
321 \r
322                 if( sError == pdFALSE )\r
323                 {\r
324                         /* If the calculation has always been correct, increment the check \r
325                         variable so we know     this task is still running okay. */\r
326                         ( *pusTaskCheckVariable )++;\r
327                 }\r
328         }\r
329 }\r
330 /*-----------------------------------------------------------*/\r
331 \r
332 /* This is called to check that all the created tasks are still running. */\r
333 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
334 {\r
335 /* Keep a history of the check variables so we know if they have been incremented \r
336 since the last call. */\r
337 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
338 portBASE_TYPE xReturn = pdTRUE, xTask;\r
339 \r
340         /* Check the maths tasks are still running by ensuring their check variables \r
341         are still incrementing. */\r
342         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
343         {\r
344                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
345                 {\r
346                         /* The check has not incremented so an error exists. */\r
347                         xReturn = pdFALSE;\r
348                 }\r
349 \r
350                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
351         }\r
352 \r
353         return xReturn;\r
354 }\r
355 \r
356 \r
357 \r