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