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