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