2 FreeRTOS.org V5.1.2 - Copyright (C) 2003-2009 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 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
31 * This is a concise, step by step, 'hands on' guide that describes both *
\r
32 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
33 * explains numerous examples that are written using the FreeRTOS API. *
\r
34 * Full source code for all the examples is provided in an accompanying *
\r
37 ***************************************************************************
\r
38 ***************************************************************************
\r
40 Please ensure to read the configuration and relevant port sections of the
\r
41 online documentation.
\r
43 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
46 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
49 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
50 licensing and training services.
\r
56 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
57 with the cooperative scheduler.
\r
61 * Creates eight tasks, each of which loops continuously performing an (emulated)
\r
62 * floating point calculation.
\r
64 * All the tasks run at the idle priority and never block or yield. This causes
\r
65 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
66 * means that these tasks will get pre-empted any time another task is ready to run
\r
67 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
68 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
69 * calculation producing an unexpected result could be a symptom of a corruption in
\r
70 * the context of a task.
\r
72 * \page FlopC flop.c
\r
73 * \ingroup DemoFiles
\r
80 /* Scheduler include files. */
\r
81 #include "FreeRTOS.h"
\r
85 /* Demo program include files. */
\r
88 #define mathSTACK_SIZE ( ( unsigned portSHORT ) 512 )
\r
89 #define mathNUMBER_OF_TASKS ( 8 )
\r
91 /* Four tasks, each of which performs a different floating point calculation.
\r
92 Each of the four is created twice. */
\r
93 static void vCompetingMathTask1( void *pvParameters );
\r
94 static void vCompetingMathTask2( void *pvParameters );
\r
95 static void vCompetingMathTask3( void *pvParameters );
\r
96 static void vCompetingMathTask4( void *pvParameters );
\r
98 /* These variables are used to check that all the tasks are still running. If a
\r
99 task gets a calculation wrong it will
\r
100 stop incrementing its check variable. */
\r
101 static volatile unsigned portSHORT usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
103 /*-----------------------------------------------------------*/
\r
105 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
107 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
108 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
109 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
110 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
111 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
112 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
113 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
114 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
116 /*-----------------------------------------------------------*/
\r
118 static void vCompetingMathTask1( void *pvParameters )
\r
120 portDOUBLE d1, d2, d3, d4;
\r
121 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
122 const portDOUBLE dAnswer = ( 123.4567 + 2345.6789 ) * -918.222;
\r
123 const portCHAR * const pcTaskStartMsg = "Math task 1 started.\r\n";
\r
124 const portCHAR * const pcTaskFailMsg = "Math task 1 failed.\r\n";
\r
125 portSHORT sError = pdFALSE;
\r
127 /* Queue a message for printing to say the task has started. */
\r
128 vPrintDisplayMessage( &pcTaskStartMsg );
\r
130 /* The variable this task increments to show it is still running is passed in
\r
131 as the parameter. */
\r
132 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
134 /* Keep performing a calculation and checking the result against a constant. */
\r
141 d4 = ( d1 + d2 ) * d3;
\r
145 /* If the calculation does not match the expected constant, stop the
\r
146 increment of the check variable. */
\r
147 if( fabs( d4 - dAnswer ) > 0.001 )
\r
149 vPrintDisplayMessage( &pcTaskFailMsg );
\r
153 if( sError == pdFALSE )
\r
155 /* If the calculation has always been correct, increment the check
\r
156 variable so we know this task is still running okay. */
\r
157 ( *pusTaskCheckVariable )++;
\r
163 /*-----------------------------------------------------------*/
\r
165 static void vCompetingMathTask2( void *pvParameters )
\r
167 portDOUBLE d1, d2, d3, d4;
\r
168 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
169 const portDOUBLE dAnswer = ( -389.38 / 32498.2 ) * -2.0001;
\r
170 const portCHAR * const pcTaskStartMsg = "Math task 2 started.\r\n";
\r
171 const portCHAR * const pcTaskFailMsg = "Math task 2 failed.\r\n";
\r
172 portSHORT sError = pdFALSE;
\r
174 /* Queue a message for printing to say the task has started. */
\r
175 vPrintDisplayMessage( &pcTaskStartMsg );
\r
177 /* The variable this task increments to show it is still running is passed in
\r
178 as the parameter. */
\r
179 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
181 /* Keep performing a calculation and checking the result against a constant. */
\r
188 d4 = ( d1 / d2 ) * d3;
\r
192 /* If the calculation does not match the expected constant, stop the
\r
193 increment of the check variable. */
\r
194 if( fabs( d4 - dAnswer ) > 0.001 )
\r
196 vPrintDisplayMessage( &pcTaskFailMsg );
\r
200 if( sError == pdFALSE )
\r
202 /* If the calculation has always been correct, increment the check
\r
203 variable so we know
\r
204 this task is still running okay. */
\r
205 ( *pusTaskCheckVariable )++;
\r
211 /*-----------------------------------------------------------*/
\r
213 static void vCompetingMathTask3( void *pvParameters )
\r
215 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
216 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
217 const unsigned portSHORT usArraySize = 250;
\r
218 unsigned portSHORT usPosition;
\r
219 const portCHAR * const pcTaskStartMsg = "Math task 3 started.\r\n";
\r
220 const portCHAR * const pcTaskFailMsg = "Math task 3 failed.\r\n";
\r
221 portSHORT sError = pdFALSE;
\r
223 /* Queue a message for printing to say the task has started. */
\r
224 vPrintDisplayMessage( &pcTaskStartMsg );
\r
226 /* The variable this task increments to show it is still running is passed in
\r
227 as the parameter. */
\r
228 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
230 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
232 /* Keep filling an array, keeping a running total of the values placed in the
\r
233 array. Then run through the array adding up all the values. If the two totals
\r
234 do not match, stop the check variable from incrementing. */
\r
240 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
242 pdArray[ usPosition ] = ( portDOUBLE ) usPosition + 5.5;
\r
243 dTotal1 += ( portDOUBLE ) usPosition + 5.5;
\r
248 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
250 dTotal2 += pdArray[ usPosition ];
\r
253 dDifference = dTotal1 - dTotal2;
\r
254 if( fabs( dDifference ) > 0.001 )
\r
256 vPrintDisplayMessage( &pcTaskFailMsg );
\r
262 if( sError == pdFALSE )
\r
264 /* If the calculation has always been correct, increment the check
\r
265 variable so we know this task is still running okay. */
\r
266 ( *pusTaskCheckVariable )++;
\r
270 /*-----------------------------------------------------------*/
\r
272 static void vCompetingMathTask4( void *pvParameters )
\r
274 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
275 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
276 const unsigned portSHORT usArraySize = 250;
\r
277 unsigned portSHORT usPosition;
\r
278 const portCHAR * const pcTaskStartMsg = "Math task 4 started.\r\n";
\r
279 const portCHAR * const pcTaskFailMsg = "Math task 4 failed.\r\n";
\r
280 portSHORT sError = pdFALSE;
\r
282 /* Queue a message for printing to say the task has started. */
\r
283 vPrintDisplayMessage( &pcTaskStartMsg );
\r
285 /* The variable this task increments to show it is still running is passed in
\r
286 as the parameter. */
\r
287 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
289 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
291 /* Keep filling an array, keeping a running total of the values placed in the
\r
292 array. Then run through the array adding up all the values. If the two totals
\r
293 do not match, stop the check variable from incrementing. */
\r
299 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
301 pdArray[ usPosition ] = ( portDOUBLE ) usPosition * 12.123;
\r
302 dTotal1 += ( portDOUBLE ) usPosition * 12.123;
\r
307 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
309 dTotal2 += pdArray[ usPosition ];
\r
312 dDifference = dTotal1 - dTotal2;
\r
313 if( fabs( dDifference ) > 0.001 )
\r
315 vPrintDisplayMessage( &pcTaskFailMsg );
\r
321 if( sError == pdFALSE )
\r
323 /* If the calculation has always been correct, increment the check
\r
324 variable so we know this task is still running okay. */
\r
325 ( *pusTaskCheckVariable )++;
\r
329 /*-----------------------------------------------------------*/
\r
331 /* This is called to check that all the created tasks are still running. */
\r
332 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
334 /* Keep a history of the check variables so we know if they have been incremented
\r
335 since the last call. */
\r
336 static unsigned portSHORT usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
337 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
339 /* Check the maths tasks are still running by ensuring their check variables
\r
340 are still incrementing. */
\r
341 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
343 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
345 /* The check has not incremented so an error exists. */
\r
349 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r