2 FreeRTOS.org V4.3.0 - Copyright (C) 2003-2007 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 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
39 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
40 with the cooperative scheduler.
\r
44 * Creates eight tasks, each of which loops continuously performing an (emulated)
\r
45 * floating point calculation.
\r
47 * All the tasks run at the idle priority and never block or yield. This causes
\r
48 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
49 * means that these tasks will get pre-empted any time another task is ready to run
\r
50 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
51 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
52 * calculation producing an unexpected result could be a symptom of a corruption in
\r
53 * the context of a task.
\r
55 * \page FlopC flop.c
\r
56 * \ingroup DemoFiles
\r
63 /* Scheduler include files. */
\r
64 #include "FreeRTOS.h"
\r
68 /* Demo program include files. */
\r
71 #define mathSTACK_SIZE ( ( unsigned portSHORT ) 512 )
\r
72 #define mathNUMBER_OF_TASKS ( 8 )
\r
74 /* Four tasks, each of which performs a different floating point calculation.
\r
75 Each of the four is created twice. */
\r
76 static void vCompetingMathTask1( void *pvParameters );
\r
77 static void vCompetingMathTask2( void *pvParameters );
\r
78 static void vCompetingMathTask3( void *pvParameters );
\r
79 static void vCompetingMathTask4( void *pvParameters );
\r
81 /* These variables are used to check that all the tasks are still running. If a
\r
82 task gets a calculation wrong it will
\r
83 stop incrementing its check variable. */
\r
84 static volatile unsigned portSHORT usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
86 /*-----------------------------------------------------------*/
\r
88 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
90 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
91 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
92 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
93 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
94 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
95 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
96 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
97 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
99 /*-----------------------------------------------------------*/
\r
101 static void vCompetingMathTask1( void *pvParameters )
\r
103 portDOUBLE d1, d2, d3, d4;
\r
104 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
105 const portDOUBLE dAnswer = ( 123.4567 + 2345.6789 ) * -918.222;
\r
106 const portCHAR * const pcTaskStartMsg = "Math task 1 started.\r\n";
\r
107 const portCHAR * const pcTaskFailMsg = "Math task 1 failed.\r\n";
\r
108 portSHORT sError = pdFALSE;
\r
110 /* Queue a message for printing to say the task has started. */
\r
111 vPrintDisplayMessage( &pcTaskStartMsg );
\r
113 /* The variable this task increments to show it is still running is passed in
\r
114 as the parameter. */
\r
115 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
117 /* Keep performing a calculation and checking the result against a constant. */
\r
124 d4 = ( d1 + d2 ) * d3;
\r
128 /* If the calculation does not match the expected constant, stop the
\r
129 increment of the check variable. */
\r
130 if( fabs( d4 - dAnswer ) > 0.001 )
\r
132 vPrintDisplayMessage( &pcTaskFailMsg );
\r
136 if( sError == pdFALSE )
\r
138 /* If the calculation has always been correct, increment the check
\r
139 variable so we know this task is still running okay. */
\r
140 ( *pusTaskCheckVariable )++;
\r
146 /*-----------------------------------------------------------*/
\r
148 static void vCompetingMathTask2( void *pvParameters )
\r
150 portDOUBLE d1, d2, d3, d4;
\r
151 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
152 const portDOUBLE dAnswer = ( -389.38 / 32498.2 ) * -2.0001;
\r
153 const portCHAR * const pcTaskStartMsg = "Math task 2 started.\r\n";
\r
154 const portCHAR * const pcTaskFailMsg = "Math task 2 failed.\r\n";
\r
155 portSHORT sError = pdFALSE;
\r
157 /* Queue a message for printing to say the task has started. */
\r
158 vPrintDisplayMessage( &pcTaskStartMsg );
\r
160 /* The variable this task increments to show it is still running is passed in
\r
161 as the parameter. */
\r
162 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
164 /* Keep performing a calculation and checking the result against a constant. */
\r
171 d4 = ( d1 / d2 ) * d3;
\r
175 /* If the calculation does not match the expected constant, stop the
\r
176 increment of the check variable. */
\r
177 if( fabs( d4 - dAnswer ) > 0.001 )
\r
179 vPrintDisplayMessage( &pcTaskFailMsg );
\r
183 if( sError == pdFALSE )
\r
185 /* If the calculation has always been correct, increment the check
\r
186 variable so we know
\r
187 this task is still running okay. */
\r
188 ( *pusTaskCheckVariable )++;
\r
194 /*-----------------------------------------------------------*/
\r
196 static void vCompetingMathTask3( void *pvParameters )
\r
198 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
199 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
200 const unsigned portSHORT usArraySize = 250;
\r
201 unsigned portSHORT usPosition;
\r
202 const portCHAR * const pcTaskStartMsg = "Math task 3 started.\r\n";
\r
203 const portCHAR * const pcTaskFailMsg = "Math task 3 failed.\r\n";
\r
204 portSHORT sError = pdFALSE;
\r
206 /* Queue a message for printing to say the task has started. */
\r
207 vPrintDisplayMessage( &pcTaskStartMsg );
\r
209 /* The variable this task increments to show it is still running is passed in
\r
210 as the parameter. */
\r
211 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
213 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
215 /* Keep filling an array, keeping a running total of the values placed in the
\r
216 array. Then run through the array adding up all the values. If the two totals
\r
217 do not match, stop the check variable from incrementing. */
\r
223 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
225 pdArray[ usPosition ] = ( portDOUBLE ) usPosition + 5.5;
\r
226 dTotal1 += ( portDOUBLE ) usPosition + 5.5;
\r
231 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
233 dTotal2 += pdArray[ usPosition ];
\r
236 dDifference = dTotal1 - dTotal2;
\r
237 if( fabs( dDifference ) > 0.001 )
\r
239 vPrintDisplayMessage( &pcTaskFailMsg );
\r
245 if( sError == pdFALSE )
\r
247 /* If the calculation has always been correct, increment the check
\r
248 variable so we know this task is still running okay. */
\r
249 ( *pusTaskCheckVariable )++;
\r
253 /*-----------------------------------------------------------*/
\r
255 static void vCompetingMathTask4( void *pvParameters )
\r
257 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
258 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
259 const unsigned portSHORT usArraySize = 250;
\r
260 unsigned portSHORT usPosition;
\r
261 const portCHAR * const pcTaskStartMsg = "Math task 4 started.\r\n";
\r
262 const portCHAR * const pcTaskFailMsg = "Math task 4 failed.\r\n";
\r
263 portSHORT sError = pdFALSE;
\r
265 /* Queue a message for printing to say the task has started. */
\r
266 vPrintDisplayMessage( &pcTaskStartMsg );
\r
268 /* The variable this task increments to show it is still running is passed in
\r
269 as the parameter. */
\r
270 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
272 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
274 /* Keep filling an array, keeping a running total of the values placed in the
\r
275 array. Then run through the array adding up all the values. If the two totals
\r
276 do not match, stop the check variable from incrementing. */
\r
282 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
284 pdArray[ usPosition ] = ( portDOUBLE ) usPosition * 12.123;
\r
285 dTotal1 += ( portDOUBLE ) usPosition * 12.123;
\r
290 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
292 dTotal2 += pdArray[ usPosition ];
\r
295 dDifference = dTotal1 - dTotal2;
\r
296 if( fabs( dDifference ) > 0.001 )
\r
298 vPrintDisplayMessage( &pcTaskFailMsg );
\r
304 if( sError == pdFALSE )
\r
306 /* If the calculation has always been correct, increment the check
\r
307 variable so we know this task is still running okay. */
\r
308 ( *pusTaskCheckVariable )++;
\r
312 /*-----------------------------------------------------------*/
\r
314 /* This is called to check that all the created tasks are still running. */
\r
315 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
317 /* Keep a history of the check variables so we know if they have been incremented
\r
318 since the last call. */
\r
319 static unsigned portSHORT usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
320 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
322 /* Check the maths tasks are still running by ensuring their check variables
\r
323 are still incrementing. */
\r
324 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
326 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
328 /* The check has not incremented so an error exists. */
\r
332 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r