2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
57 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
58 with the cooperative scheduler.
\r
62 * Creates eight tasks, each of which loops continuously performing an (emulated)
\r
63 * floating point calculation.
\r
65 * All the tasks run at the idle priority and never block or yield. This causes
\r
66 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
67 * means that these tasks will get pre-empted any time another task is ready to run
\r
68 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
69 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
70 * calculation producing an unexpected result could be a symptom of a corruption in
\r
71 * the context of a task.
\r
73 * \page FlopC flop.c
\r
74 * \ingroup DemoFiles
\r
81 /* Scheduler include files. */
\r
82 #include "FreeRTOS.h"
\r
86 /* Demo program include files. */
\r
89 #define mathSTACK_SIZE ( ( unsigned short ) 512 )
\r
90 #define mathNUMBER_OF_TASKS ( 8 )
\r
92 /* Four tasks, each of which performs a different floating point calculation.
\r
93 Each of the four is created twice. */
\r
94 static void vCompetingMathTask1( void *pvParameters );
\r
95 static void vCompetingMathTask2( void *pvParameters );
\r
96 static void vCompetingMathTask3( void *pvParameters );
\r
97 static void vCompetingMathTask4( void *pvParameters );
\r
99 /* These variables are used to check that all the tasks are still running. If a
\r
100 task gets a calculation wrong it will
\r
101 stop incrementing its check variable. */
\r
102 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
104 /*-----------------------------------------------------------*/
\r
106 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
108 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
109 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
110 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
111 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
112 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
113 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
114 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
115 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
117 /*-----------------------------------------------------------*/
\r
119 static void vCompetingMathTask1( void *pvParameters )
\r
121 portDOUBLE d1, d2, d3, d4;
\r
122 volatile unsigned short *pusTaskCheckVariable;
\r
123 const portDOUBLE dAnswer = ( 123.4567 + 2345.6789 ) * -918.222;
\r
124 const char * const pcTaskStartMsg = "Math task 1 started.\r\n";
\r
125 const char * const pcTaskFailMsg = "Math task 1 failed.\r\n";
\r
126 short sError = pdFALSE;
\r
128 /* Queue a message for printing to say the task has started. */
\r
129 vPrintDisplayMessage( &pcTaskStartMsg );
\r
131 /* The variable this task increments to show it is still running is passed in
\r
132 as the parameter. */
\r
133 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
135 /* Keep performing a calculation and checking the result against a constant. */
\r
142 d4 = ( d1 + d2 ) * d3;
\r
146 /* If the calculation does not match the expected constant, stop the
\r
147 increment of the check variable. */
\r
148 if( fabs( d4 - dAnswer ) > 0.001 )
\r
150 vPrintDisplayMessage( &pcTaskFailMsg );
\r
154 if( sError == pdFALSE )
\r
156 /* If the calculation has always been correct, increment the check
\r
157 variable so we know this task is still running okay. */
\r
158 ( *pusTaskCheckVariable )++;
\r
164 /*-----------------------------------------------------------*/
\r
166 static void vCompetingMathTask2( void *pvParameters )
\r
168 portDOUBLE d1, d2, d3, d4;
\r
169 volatile unsigned short *pusTaskCheckVariable;
\r
170 const portDOUBLE dAnswer = ( -389.38 / 32498.2 ) * -2.0001;
\r
171 const char * const pcTaskStartMsg = "Math task 2 started.\r\n";
\r
172 const char * const pcTaskFailMsg = "Math task 2 failed.\r\n";
\r
173 short sError = pdFALSE;
\r
175 /* Queue a message for printing to say the task has started. */
\r
176 vPrintDisplayMessage( &pcTaskStartMsg );
\r
178 /* The variable this task increments to show it is still running is passed in
\r
179 as the parameter. */
\r
180 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
182 /* Keep performing a calculation and checking the result against a constant. */
\r
189 d4 = ( d1 / d2 ) * d3;
\r
193 /* If the calculation does not match the expected constant, stop the
\r
194 increment of the check variable. */
\r
195 if( fabs( d4 - dAnswer ) > 0.001 )
\r
197 vPrintDisplayMessage( &pcTaskFailMsg );
\r
201 if( sError == pdFALSE )
\r
203 /* If the calculation has always been correct, increment the check
\r
204 variable so we know
\r
205 this task is still running okay. */
\r
206 ( *pusTaskCheckVariable )++;
\r
212 /*-----------------------------------------------------------*/
\r
214 static void vCompetingMathTask3( void *pvParameters )
\r
216 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
217 volatile unsigned short *pusTaskCheckVariable;
\r
218 const unsigned short usArraySize = 250;
\r
219 unsigned short usPosition;
\r
220 const char * const pcTaskStartMsg = "Math task 3 started.\r\n";
\r
221 const char * const pcTaskFailMsg = "Math task 3 failed.\r\n";
\r
222 short sError = pdFALSE;
\r
224 /* Queue a message for printing to say the task has started. */
\r
225 vPrintDisplayMessage( &pcTaskStartMsg );
\r
227 /* The variable this task increments to show it is still running is passed in
\r
228 as the parameter. */
\r
229 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
231 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
233 /* Keep filling an array, keeping a running total of the values placed in the
\r
234 array. Then run through the array adding up all the values. If the two totals
\r
235 do not match, stop the check variable from incrementing. */
\r
241 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
243 pdArray[ usPosition ] = ( portDOUBLE ) usPosition + 5.5;
\r
244 dTotal1 += ( portDOUBLE ) usPosition + 5.5;
\r
249 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
251 dTotal2 += pdArray[ usPosition ];
\r
254 dDifference = dTotal1 - dTotal2;
\r
255 if( fabs( dDifference ) > 0.001 )
\r
257 vPrintDisplayMessage( &pcTaskFailMsg );
\r
263 if( sError == pdFALSE )
\r
265 /* If the calculation has always been correct, increment the check
\r
266 variable so we know this task is still running okay. */
\r
267 ( *pusTaskCheckVariable )++;
\r
271 /*-----------------------------------------------------------*/
\r
273 static void vCompetingMathTask4( void *pvParameters )
\r
275 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
276 volatile unsigned short *pusTaskCheckVariable;
\r
277 const unsigned short usArraySize = 250;
\r
278 unsigned short usPosition;
\r
279 const char * const pcTaskStartMsg = "Math task 4 started.\r\n";
\r
280 const char * const pcTaskFailMsg = "Math task 4 failed.\r\n";
\r
281 short sError = pdFALSE;
\r
283 /* Queue a message for printing to say the task has started. */
\r
284 vPrintDisplayMessage( &pcTaskStartMsg );
\r
286 /* The variable this task increments to show it is still running is passed in
\r
287 as the parameter. */
\r
288 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
290 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
292 /* Keep filling an array, keeping a running total of the values placed in the
\r
293 array. Then run through the array adding up all the values. If the two totals
\r
294 do not match, stop the check variable from incrementing. */
\r
300 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
302 pdArray[ usPosition ] = ( portDOUBLE ) usPosition * 12.123;
\r
303 dTotal1 += ( portDOUBLE ) usPosition * 12.123;
\r
308 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
310 dTotal2 += pdArray[ usPosition ];
\r
313 dDifference = dTotal1 - dTotal2;
\r
314 if( fabs( dDifference ) > 0.001 )
\r
316 vPrintDisplayMessage( &pcTaskFailMsg );
\r
322 if( sError == pdFALSE )
\r
324 /* If the calculation has always been correct, increment the check
\r
325 variable so we know this task is still running okay. */
\r
326 ( *pusTaskCheckVariable )++;
\r
330 /*-----------------------------------------------------------*/
\r
332 /* This is called to check that all the created tasks are still running. */
\r
333 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
335 /* Keep a history of the check variables so we know if they have been incremented
\r
336 since the last call. */
\r
337 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
338 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
340 /* Check the maths tasks are still running by ensuring their check variables
\r
341 are still incrementing. */
\r
342 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
344 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
346 /* The check has not incremented so an error exists. */
\r
350 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r