2 FreeRTOS.org V5.1.0 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
53 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
54 with the cooperative scheduler.
\r
58 * Creates eight tasks, each of which loops continuously performing an (emulated)
\r
59 * floating point calculation.
\r
61 * All the tasks run at the idle priority and never block or yield. This causes
\r
62 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
63 * means that these tasks will get pre-empted any time another task is ready to run
\r
64 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
65 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
66 * calculation producing an unexpected result could be a symptom of a corruption in
\r
67 * the context of a task.
\r
69 * \page FlopC flop.c
\r
70 * \ingroup DemoFiles
\r
77 /* Scheduler include files. */
\r
78 #include "FreeRTOS.h"
\r
82 /* Demo program include files. */
\r
85 #define mathSTACK_SIZE ( ( unsigned portSHORT ) 512 )
\r
86 #define mathNUMBER_OF_TASKS ( 8 )
\r
88 /* Four tasks, each of which performs a different floating point calculation.
\r
89 Each of the four is created twice. */
\r
90 static void vCompetingMathTask1( void *pvParameters );
\r
91 static void vCompetingMathTask2( void *pvParameters );
\r
92 static void vCompetingMathTask3( void *pvParameters );
\r
93 static void vCompetingMathTask4( void *pvParameters );
\r
95 /* These variables are used to check that all the tasks are still running. If a
\r
96 task gets a calculation wrong it will
\r
97 stop incrementing its check variable. */
\r
98 static volatile unsigned portSHORT usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
100 /*-----------------------------------------------------------*/
\r
102 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
104 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
105 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
106 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
107 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
108 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
109 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
110 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
111 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
113 /*-----------------------------------------------------------*/
\r
115 static void vCompetingMathTask1( void *pvParameters )
\r
117 portDOUBLE d1, d2, d3, d4;
\r
118 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
119 const portDOUBLE dAnswer = ( 123.4567 + 2345.6789 ) * -918.222;
\r
120 const portCHAR * const pcTaskStartMsg = "Math task 1 started.\r\n";
\r
121 const portCHAR * const pcTaskFailMsg = "Math task 1 failed.\r\n";
\r
122 portSHORT sError = pdFALSE;
\r
124 /* Queue a message for printing to say the task has started. */
\r
125 vPrintDisplayMessage( &pcTaskStartMsg );
\r
127 /* The variable this task increments to show it is still running is passed in
\r
128 as the parameter. */
\r
129 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
131 /* Keep performing a calculation and checking the result against a constant. */
\r
138 d4 = ( d1 + d2 ) * d3;
\r
142 /* If the calculation does not match the expected constant, stop the
\r
143 increment of the check variable. */
\r
144 if( fabs( d4 - dAnswer ) > 0.001 )
\r
146 vPrintDisplayMessage( &pcTaskFailMsg );
\r
150 if( sError == pdFALSE )
\r
152 /* If the calculation has always been correct, increment the check
\r
153 variable so we know this task is still running okay. */
\r
154 ( *pusTaskCheckVariable )++;
\r
160 /*-----------------------------------------------------------*/
\r
162 static void vCompetingMathTask2( void *pvParameters )
\r
164 portDOUBLE d1, d2, d3, d4;
\r
165 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
166 const portDOUBLE dAnswer = ( -389.38 / 32498.2 ) * -2.0001;
\r
167 const portCHAR * const pcTaskStartMsg = "Math task 2 started.\r\n";
\r
168 const portCHAR * const pcTaskFailMsg = "Math task 2 failed.\r\n";
\r
169 portSHORT sError = pdFALSE;
\r
171 /* Queue a message for printing to say the task has started. */
\r
172 vPrintDisplayMessage( &pcTaskStartMsg );
\r
174 /* The variable this task increments to show it is still running is passed in
\r
175 as the parameter. */
\r
176 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
178 /* Keep performing a calculation and checking the result against a constant. */
\r
185 d4 = ( d1 / d2 ) * d3;
\r
189 /* If the calculation does not match the expected constant, stop the
\r
190 increment of the check variable. */
\r
191 if( fabs( d4 - dAnswer ) > 0.001 )
\r
193 vPrintDisplayMessage( &pcTaskFailMsg );
\r
197 if( sError == pdFALSE )
\r
199 /* If the calculation has always been correct, increment the check
\r
200 variable so we know
\r
201 this task is still running okay. */
\r
202 ( *pusTaskCheckVariable )++;
\r
208 /*-----------------------------------------------------------*/
\r
210 static void vCompetingMathTask3( void *pvParameters )
\r
212 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
213 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
214 const unsigned portSHORT usArraySize = 250;
\r
215 unsigned portSHORT usPosition;
\r
216 const portCHAR * const pcTaskStartMsg = "Math task 3 started.\r\n";
\r
217 const portCHAR * const pcTaskFailMsg = "Math task 3 failed.\r\n";
\r
218 portSHORT sError = pdFALSE;
\r
220 /* Queue a message for printing to say the task has started. */
\r
221 vPrintDisplayMessage( &pcTaskStartMsg );
\r
223 /* The variable this task increments to show it is still running is passed in
\r
224 as the parameter. */
\r
225 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
227 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
229 /* Keep filling an array, keeping a running total of the values placed in the
\r
230 array. Then run through the array adding up all the values. If the two totals
\r
231 do not match, stop the check variable from incrementing. */
\r
237 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
239 pdArray[ usPosition ] = ( portDOUBLE ) usPosition + 5.5;
\r
240 dTotal1 += ( portDOUBLE ) usPosition + 5.5;
\r
245 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
247 dTotal2 += pdArray[ usPosition ];
\r
250 dDifference = dTotal1 - dTotal2;
\r
251 if( fabs( dDifference ) > 0.001 )
\r
253 vPrintDisplayMessage( &pcTaskFailMsg );
\r
259 if( sError == pdFALSE )
\r
261 /* If the calculation has always been correct, increment the check
\r
262 variable so we know this task is still running okay. */
\r
263 ( *pusTaskCheckVariable )++;
\r
267 /*-----------------------------------------------------------*/
\r
269 static void vCompetingMathTask4( void *pvParameters )
\r
271 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
272 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
273 const unsigned portSHORT usArraySize = 250;
\r
274 unsigned portSHORT usPosition;
\r
275 const portCHAR * const pcTaskStartMsg = "Math task 4 started.\r\n";
\r
276 const portCHAR * const pcTaskFailMsg = "Math task 4 failed.\r\n";
\r
277 portSHORT sError = pdFALSE;
\r
279 /* Queue a message for printing to say the task has started. */
\r
280 vPrintDisplayMessage( &pcTaskStartMsg );
\r
282 /* The variable this task increments to show it is still running is passed in
\r
283 as the parameter. */
\r
284 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
286 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
288 /* Keep filling an array, keeping a running total of the values placed in the
\r
289 array. Then run through the array adding up all the values. If the two totals
\r
290 do not match, stop the check variable from incrementing. */
\r
296 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
298 pdArray[ usPosition ] = ( portDOUBLE ) usPosition * 12.123;
\r
299 dTotal1 += ( portDOUBLE ) usPosition * 12.123;
\r
304 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
306 dTotal2 += pdArray[ usPosition ];
\r
309 dDifference = dTotal1 - dTotal2;
\r
310 if( fabs( dDifference ) > 0.001 )
\r
312 vPrintDisplayMessage( &pcTaskFailMsg );
\r
318 if( sError == pdFALSE )
\r
320 /* If the calculation has always been correct, increment the check
\r
321 variable so we know this task is still running okay. */
\r
322 ( *pusTaskCheckVariable )++;
\r
326 /*-----------------------------------------------------------*/
\r
328 /* This is called to check that all the created tasks are still running. */
\r
329 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
331 /* Keep a history of the check variables so we know if they have been incremented
\r
332 since the last call. */
\r
333 static unsigned portSHORT usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
334 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
336 /* Check the maths tasks are still running by ensuring their check variables
\r
337 are still incrementing. */
\r
338 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
340 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
342 /* The check has not incremented so an error exists. */
\r
346 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r