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