2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
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
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
25 ***************************************************************************
\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
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\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
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
46 ***************************************************************************
\r
48 ***************************************************************************
\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
62 ***************************************************************************
\r
64 ***************************************************************************
\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
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\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
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
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
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
99 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
100 with the cooperative scheduler.
\r
104 * Creates eight tasks, each of which loops continuously performing an (emulated)
\r
105 * floating point calculation.
\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
115 * \page FlopC flop.c
\r
116 * \ingroup DemoFiles
\r
120 #include <stdlib.h>
\r
123 /* Scheduler include files. */
\r
124 #include "FreeRTOS.h"
\r
128 /* Demo program include files. */
\r
131 #define mathSTACK_SIZE ( ( unsigned short ) 512 )
\r
132 #define mathNUMBER_OF_TASKS ( 8 )
\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
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
146 /*-----------------------------------------------------------*/
\r
148 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\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
159 /*-----------------------------------------------------------*/
\r
161 static void vCompetingMathTask1( void *pvParameters )
\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
170 /* Queue a message for printing to say the task has started. */
\r
171 vPrintDisplayMessage( &pcTaskStartMsg );
\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
177 /* Keep performing a calculation and checking the result against a constant. */
\r
184 d4 = ( d1 + d2 ) * d3;
\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
192 vPrintDisplayMessage( &pcTaskFailMsg );
\r
196 if( sError == pdFALSE )
\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
206 /*-----------------------------------------------------------*/
\r
208 static void vCompetingMathTask2( void *pvParameters )
\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
217 /* Queue a message for printing to say the task has started. */
\r
218 vPrintDisplayMessage( &pcTaskStartMsg );
\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
224 /* Keep performing a calculation and checking the result against a constant. */
\r
231 d4 = ( d1 / d2 ) * d3;
\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
239 vPrintDisplayMessage( &pcTaskFailMsg );
\r
243 if( sError == pdFALSE )
\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
254 /*-----------------------------------------------------------*/
\r
256 static void vCompetingMathTask3( void *pvParameters )
\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
266 /* Queue a message for printing to say the task has started. */
\r
267 vPrintDisplayMessage( &pcTaskStartMsg );
\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
273 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\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
283 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
285 pdArray[ usPosition ] = ( portDOUBLE ) usPosition + 5.5;
\r
286 dTotal1 += ( portDOUBLE ) usPosition + 5.5;
\r
291 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
293 dTotal2 += pdArray[ usPosition ];
\r
296 dDifference = dTotal1 - dTotal2;
\r
297 if( fabs( dDifference ) > 0.001 )
\r
299 vPrintDisplayMessage( &pcTaskFailMsg );
\r
305 if( sError == pdFALSE )
\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
313 /*-----------------------------------------------------------*/
\r
315 static void vCompetingMathTask4( void *pvParameters )
\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
325 /* Queue a message for printing to say the task has started. */
\r
326 vPrintDisplayMessage( &pcTaskStartMsg );
\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
332 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\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
342 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
344 pdArray[ usPosition ] = ( portDOUBLE ) usPosition * 12.123;
\r
345 dTotal1 += ( portDOUBLE ) usPosition * 12.123;
\r
350 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
352 dTotal2 += pdArray[ usPosition ];
\r
355 dDifference = dTotal1 - dTotal2;
\r
356 if( fabs( dDifference ) > 0.001 )
\r
358 vPrintDisplayMessage( &pcTaskFailMsg );
\r
364 if( sError == pdFALSE )
\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
372 /*-----------------------------------------------------------*/
\r
374 /* This is called to check that all the created tasks are still running. */
\r
375 portBASE_TYPE xAreMathsTaskStillRunning( void )
\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
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
386 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
388 /* The check has not incremented so an error exists. */
\r
392 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r