2 FreeRTOS V4.0.1 - Copyright (C) 2003-2006 Richard Barry.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS 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 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; 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, 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
30 ***************************************************************************
\r
36 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
37 with the cooperative scheduler.
\r
41 * Creates eight tasks, each of which loops continuously performing an (emulated)
\r
42 * floating point calculation.
\r
44 * All the tasks run at the idle priority and never block or yield. This causes
\r
45 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
46 * means that these tasks will get pre-empted any time another task is ready to run
\r
47 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
48 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
49 * calculation producing an unexpected result could be a symptom of a corruption in
\r
50 * the context of a task.
\r
52 * \page FlopC flop.c
\r
53 * \ingroup DemoFiles
\r
60 /* Scheduler include files. */
\r
61 #include "FreeRTOS.h"
\r
65 /* Demo program include files. */
\r
68 #define mathSTACK_SIZE ( ( unsigned portSHORT ) 512 )
\r
69 #define mathNUMBER_OF_TASKS ( 8 )
\r
71 /* Four tasks, each of which performs a different floating point calculation.
\r
72 Each of the four is created twice. */
\r
73 static void vCompetingMathTask1( void *pvParameters );
\r
74 static void vCompetingMathTask2( void *pvParameters );
\r
75 static void vCompetingMathTask3( void *pvParameters );
\r
76 static void vCompetingMathTask4( void *pvParameters );
\r
78 /* These variables are used to check that all the tasks are still running. If a
\r
79 task gets a calculation wrong it will
\r
80 stop incrementing its check variable. */
\r
81 static volatile unsigned portSHORT usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
83 /*-----------------------------------------------------------*/
\r
85 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
87 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
88 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
89 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
90 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
91 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
92 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
93 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
94 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
96 /*-----------------------------------------------------------*/
\r
98 static void vCompetingMathTask1( void *pvParameters )
\r
100 portDOUBLE d1, d2, d3, d4;
\r
101 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
102 const portDOUBLE dAnswer = ( 123.4567 + 2345.6789 ) * -918.222;
\r
103 const portCHAR * const pcTaskStartMsg = "Math task 1 started.\r\n";
\r
104 const portCHAR * const pcTaskFailMsg = "Math task 1 failed.\r\n";
\r
105 portSHORT sError = pdFALSE;
\r
107 /* Queue a message for printing to say the task has started. */
\r
108 vPrintDisplayMessage( &pcTaskStartMsg );
\r
110 /* The variable this task increments to show it is still running is passed in
\r
111 as the parameter. */
\r
112 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
114 /* Keep performing a calculation and checking the result against a constant. */
\r
121 d4 = ( d1 + d2 ) * d3;
\r
125 /* If the calculation does not match the expected constant, stop the
\r
126 increment of the check variable. */
\r
127 if( fabs( d4 - dAnswer ) > 0.001 )
\r
129 vPrintDisplayMessage( &pcTaskFailMsg );
\r
133 if( sError == pdFALSE )
\r
135 /* If the calculation has always been correct, increment the check
\r
136 variable so we know this task is still running okay. */
\r
137 ( *pusTaskCheckVariable )++;
\r
143 /*-----------------------------------------------------------*/
\r
145 static void vCompetingMathTask2( void *pvParameters )
\r
147 portDOUBLE d1, d2, d3, d4;
\r
148 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
149 const portDOUBLE dAnswer = ( -389.38 / 32498.2 ) * -2.0001;
\r
150 const portCHAR * const pcTaskStartMsg = "Math task 2 started.\r\n";
\r
151 const portCHAR * const pcTaskFailMsg = "Math task 2 failed.\r\n";
\r
152 portSHORT sError = pdFALSE;
\r
154 /* Queue a message for printing to say the task has started. */
\r
155 vPrintDisplayMessage( &pcTaskStartMsg );
\r
157 /* The variable this task increments to show it is still running is passed in
\r
158 as the parameter. */
\r
159 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
161 /* Keep performing a calculation and checking the result against a constant. */
\r
168 d4 = ( d1 / d2 ) * d3;
\r
172 /* If the calculation does not match the expected constant, stop the
\r
173 increment of the check variable. */
\r
174 if( fabs( d4 - dAnswer ) > 0.001 )
\r
176 vPrintDisplayMessage( &pcTaskFailMsg );
\r
180 if( sError == pdFALSE )
\r
182 /* If the calculation has always been correct, increment the check
\r
183 variable so we know
\r
184 this task is still running okay. */
\r
185 ( *pusTaskCheckVariable )++;
\r
191 /*-----------------------------------------------------------*/
\r
193 static void vCompetingMathTask3( void *pvParameters )
\r
195 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
196 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
197 const unsigned portSHORT usArraySize = 250;
\r
198 unsigned portSHORT usPosition;
\r
199 const portCHAR * const pcTaskStartMsg = "Math task 3 started.\r\n";
\r
200 const portCHAR * const pcTaskFailMsg = "Math task 3 failed.\r\n";
\r
201 portSHORT sError = pdFALSE;
\r
203 /* Queue a message for printing to say the task has started. */
\r
204 vPrintDisplayMessage( &pcTaskStartMsg );
\r
206 /* The variable this task increments to show it is still running is passed in
\r
207 as the parameter. */
\r
208 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
210 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
212 /* Keep filling an array, keeping a running total of the values placed in the
\r
213 array. Then run through the array adding up all the values. If the two totals
\r
214 do not match, stop the check variable from incrementing. */
\r
220 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
222 pdArray[ usPosition ] = ( portDOUBLE ) usPosition + 5.5;
\r
223 dTotal1 += ( portDOUBLE ) usPosition + 5.5;
\r
228 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
230 dTotal2 += pdArray[ usPosition ];
\r
233 dDifference = dTotal1 - dTotal2;
\r
234 if( fabs( dDifference ) > 0.001 )
\r
236 vPrintDisplayMessage( &pcTaskFailMsg );
\r
242 if( sError == pdFALSE )
\r
244 /* If the calculation has always been correct, increment the check
\r
245 variable so we know this task is still running okay. */
\r
246 ( *pusTaskCheckVariable )++;
\r
250 /*-----------------------------------------------------------*/
\r
252 static void vCompetingMathTask4( void *pvParameters )
\r
254 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
255 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
256 const unsigned portSHORT usArraySize = 250;
\r
257 unsigned portSHORT usPosition;
\r
258 const portCHAR * const pcTaskStartMsg = "Math task 4 started.\r\n";
\r
259 const portCHAR * const pcTaskFailMsg = "Math task 4 failed.\r\n";
\r
260 portSHORT sError = pdFALSE;
\r
262 /* Queue a message for printing to say the task has started. */
\r
263 vPrintDisplayMessage( &pcTaskStartMsg );
\r
265 /* The variable this task increments to show it is still running is passed in
\r
266 as the parameter. */
\r
267 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
269 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
271 /* Keep filling an array, keeping a running total of the values placed in the
\r
272 array. Then run through the array adding up all the values. If the two totals
\r
273 do not match, stop the check variable from incrementing. */
\r
279 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
281 pdArray[ usPosition ] = ( portDOUBLE ) usPosition * 12.123;
\r
282 dTotal1 += ( portDOUBLE ) usPosition * 12.123;
\r
287 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
289 dTotal2 += pdArray[ usPosition ];
\r
292 dDifference = dTotal1 - dTotal2;
\r
293 if( fabs( dDifference ) > 0.001 )
\r
295 vPrintDisplayMessage( &pcTaskFailMsg );
\r
301 if( sError == pdFALSE )
\r
303 /* If the calculation has always been correct, increment the check
\r
304 variable so we know this task is still running okay. */
\r
305 ( *pusTaskCheckVariable )++;
\r
309 /*-----------------------------------------------------------*/
\r
311 /* This is called to check that all the created tasks are still running. */
\r
312 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
314 /* Keep a history of the check variables so we know if they have been incremented
\r
315 since the last call. */
\r
316 static unsigned portSHORT usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
317 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
319 /* Check the maths tasks are still running by ensuring their check variables
\r
320 are still incrementing. */
\r
321 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
323 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
325 /* The check has not incremented so an error exists. */
\r
329 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r