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