2 FreeRTOS.org V4.2.1 - 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
37 * Creates eight tasks, each of which loops continuously performing an (emulated)
\r
38 * floating point calculation.
\r
40 * All the tasks run at the idle priority and never block or yield. This causes
\r
41 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
42 * means that these tasks will get pre-empted any time another task is ready to run
\r
43 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
44 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
45 * calculation producing an unexpected result could be a symptom of a corruption in
\r
46 * the context of a task.
\r
52 /* Scheduler include files. */
\r
53 #include "FreeRTOS.h"
\r
56 /* Demo program include files. */
\r
59 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
60 #define mathNUMBER_OF_TASKS ( 8 )
\r
62 /* Four tasks, each of which performs a different floating point calculation.
\r
63 Each of the four is created twice. */
\r
64 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
65 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
66 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
67 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
69 /* These variables are used to check that all the tasks are still running. If a
\r
70 task gets a calculation wrong it will
\r
71 stop incrementing its check variable. */
\r
72 static volatile unsigned portSHORT usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
74 /*-----------------------------------------------------------*/
\r
76 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
78 xTaskCreate( vCompetingMathTask1, ( signed portCHAR * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
79 xTaskCreate( vCompetingMathTask2, ( signed portCHAR * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
80 xTaskCreate( vCompetingMathTask3, ( signed portCHAR * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
81 xTaskCreate( vCompetingMathTask4, ( signed portCHAR * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
82 xTaskCreate( vCompetingMathTask1, ( signed portCHAR * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
83 xTaskCreate( vCompetingMathTask2, ( signed portCHAR * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
84 xTaskCreate( vCompetingMathTask3, ( signed portCHAR * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
85 xTaskCreate( vCompetingMathTask4, ( signed portCHAR * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
87 /*-----------------------------------------------------------*/
\r
89 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
91 volatile portDOUBLE d1, d2, d3, d4;
\r
92 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
93 volatile portDOUBLE dAnswer;
\r
94 portSHORT sError = pdFALSE;
\r
100 dAnswer = ( d1 + d2 ) * d3;
\r
102 /* The variable this task increments to show it is still running is passed in
\r
103 as the parameter. */
\r
104 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
106 /* Keep performing a calculation and checking the result against a constant. */
\r
113 d4 = ( d1 + d2 ) * d3;
\r
115 #if configUSE_PREEMPTION == 0
\r
119 /* If the calculation does not match the expected constant, stop the
\r
120 increment of the check variable. */
\r
121 if( fabs( d4 - dAnswer ) > 0.001 )
\r
126 if( sError == pdFALSE )
\r
128 /* If the calculation has always been correct, increment the check
\r
129 variable so we know this task is still running okay. */
\r
130 ( *pusTaskCheckVariable )++;
\r
133 #if configUSE_PREEMPTION == 0
\r
139 /*-----------------------------------------------------------*/
\r
141 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
143 volatile portDOUBLE d1, d2, d3, d4;
\r
144 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
145 volatile portDOUBLE dAnswer;
\r
146 portSHORT sError = pdFALSE;
\r
152 dAnswer = ( d1 / d2 ) * d3;
\r
155 /* The variable this task increments to show it is still running is passed in
\r
156 as the parameter. */
\r
157 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
159 /* Keep performing a calculation and checking the result against a constant. */
\r
166 d4 = ( d1 / d2 ) * d3;
\r
168 #if configUSE_PREEMPTION == 0
\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
179 if( sError == pdFALSE )
\r
181 /* If the calculation has always been correct, increment the check
\r
182 variable so we know
\r
183 this task is still running okay. */
\r
184 ( *pusTaskCheckVariable )++;
\r
187 #if configUSE_PREEMPTION == 0
\r
192 /*-----------------------------------------------------------*/
\r
194 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
196 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
197 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
198 const size_t xArraySize = 10;
\r
200 portSHORT sError = pdFALSE;
\r
202 /* The variable this task increments to show it is still running is passed in
\r
203 as the parameter. */
\r
204 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
206 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
208 /* Keep filling an array, keeping a running total of the values placed in the
\r
209 array. Then run through the array adding up all the values. If the two totals
\r
210 do not match, stop the check variable from incrementing. */
\r
216 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
218 pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;
\r
219 dTotal1 += ( portDOUBLE ) xPosition + 5.5;
\r
222 #if configUSE_PREEMPTION == 0
\r
226 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
228 dTotal2 += pdArray[ xPosition ];
\r
231 dDifference = dTotal1 - dTotal2;
\r
232 if( fabs( dDifference ) > 0.001 )
\r
237 #if configUSE_PREEMPTION == 0
\r
241 if( sError == pdFALSE )
\r
243 /* If the calculation has always been correct, increment the check
\r
244 variable so we know this task is still running okay. */
\r
245 ( *pusTaskCheckVariable )++;
\r
249 /*-----------------------------------------------------------*/
\r
251 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
253 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
254 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
255 const size_t xArraySize = 10;
\r
257 portSHORT sError = pdFALSE;
\r
259 /* The variable this task increments to show it is still running is passed in
\r
260 as the parameter. */
\r
261 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
263 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
265 /* Keep filling an array, keeping a running total of the values placed in the
\r
266 array. Then run through the array adding up all the values. If the two totals
\r
267 do not match, stop the check variable from incrementing. */
\r
273 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
275 pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;
\r
276 dTotal1 += ( portDOUBLE ) xPosition * 12.123;
\r
279 #if configUSE_PREEMPTION == 0
\r
283 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
285 dTotal2 += pdArray[ xPosition ];
\r
288 dDifference = dTotal1 - dTotal2;
\r
289 if( fabs( dDifference ) > 0.001 )
\r
294 #if configUSE_PREEMPTION == 0
\r
298 if( sError == pdFALSE )
\r
300 /* If the calculation has always been correct, increment the check
\r
301 variable so we know this task is still running okay. */
\r
302 ( *pusTaskCheckVariable )++;
\r
306 /*-----------------------------------------------------------*/
\r
308 /* This is called to check that all the created tasks are still running. */
\r
309 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
311 /* Keep a history of the check variables so we know if they have been incremented
\r
312 since the last call. */
\r
313 static unsigned portSHORT usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
314 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
316 /* Check the maths tasks are still running by ensuring their check variables
\r
317 are still incrementing. */
\r
318 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
320 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
322 /* The check has not incremented so an error exists. */
\r
326 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r