2 FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * Creates eight tasks, each of which loops continuously performing a floating
\r
56 * point calculation - using single precision variables.
\r
58 * All the tasks run at the idle priority and never block or yield. This causes
\r
59 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
60 * means that these tasks will get pre-empted any time another task is ready to run
\r
61 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
62 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
63 * calculation producing an unexpected result could be a symptom of a corruption in
\r
64 * the context of a task.
\r
70 /* Scheduler include files. */
\r
71 #include "FreeRTOS.h"
\r
74 /* Demo program include files. */
\r
77 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
78 #define mathNUMBER_OF_TASKS ( 8 )
\r
80 /* Four tasks, each of which performs a different floating point calculation.
\r
81 Each of the four is created twice. */
\r
82 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
83 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
84 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
85 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
87 /* These variables are used to check that all the tasks are still running. If a
\r
88 task gets a calculation wrong it will
\r
89 stop incrementing its check variable. */
\r
90 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
92 /*-----------------------------------------------------------*/
\r
94 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
96 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
97 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
98 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
99 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
100 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
101 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
102 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
103 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
105 /*-----------------------------------------------------------*/
\r
107 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
109 volatile float f1, f2, f3, f4;
\r
110 volatile unsigned short *pusTaskCheckVariable;
\r
111 volatile float fAnswer;
\r
112 short sError = pdFALSE;
\r
118 fAnswer = ( f1 + f2 ) * f3;
\r
120 /* The variable this task increments to show it is still running is passed in
\r
121 as the parameter. */
\r
122 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
124 /* Keep performing a calculation and checking the result against a constant. */
\r
131 f4 = ( f1 + f2 ) * f3;
\r
133 #if configUSE_PREEMPTION == 0
\r
137 /* If the calculation does not match the expected constant, stop the
\r
138 increment of the check variable. */
\r
139 if( fabs( f4 - fAnswer ) > 0.001F )
\r
144 if( sError == pdFALSE )
\r
146 /* If the calculation has always been correct, increment the check
\r
147 variable so we know this task is still running okay. */
\r
148 ( *pusTaskCheckVariable )++;
\r
151 #if configUSE_PREEMPTION == 0
\r
157 /*-----------------------------------------------------------*/
\r
159 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
161 volatile float f1, f2, f3, f4;
\r
162 volatile unsigned short *pusTaskCheckVariable;
\r
163 volatile float fAnswer;
\r
164 short sError = pdFALSE;
\r
170 fAnswer = ( f1 / f2 ) * f3;
\r
173 /* The variable this task increments to show it is still running is passed in
\r
174 as the parameter. */
\r
175 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
177 /* Keep performing a calculation and checking the result against a constant. */
\r
184 f4 = ( f1 / f2 ) * f3;
\r
186 #if configUSE_PREEMPTION == 0
\r
190 /* If the calculation does not match the expected constant, stop the
\r
191 increment of the check variable. */
\r
192 if( fabs( f4 - fAnswer ) > 0.001F )
\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
205 #if configUSE_PREEMPTION == 0
\r
210 /*-----------------------------------------------------------*/
\r
212 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
214 volatile float *pfArray, fTotal1, fTotal2, fDifference, fPosition;
\r
215 volatile unsigned short *pusTaskCheckVariable;
\r
216 const size_t xArraySize = 10;
\r
218 short sError = pdFALSE;
\r
220 /* The variable this task increments to show it is still running is passed in
\r
221 as the parameter. */
\r
222 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
224 pfArray = ( float * ) pvPortMalloc( xArraySize * sizeof( float ) );
\r
226 /* Keep filling an array, keeping a running total of the values placed in the
\r
227 array. Then run through the array adding up all the values. If the two totals
\r
228 do not match, stop the check variable from incrementing. */
\r
235 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
237 pfArray[ xPosition ] = fPosition + 5.5F;
\r
238 fTotal1 += fPosition + 5.5F;
\r
241 #if configUSE_PREEMPTION == 0
\r
245 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
247 fTotal2 += pfArray[ xPosition ];
\r
250 fDifference = fTotal1 - fTotal2;
\r
251 if( fabs( fDifference ) > 0.001F )
\r
256 #if configUSE_PREEMPTION == 0
\r
260 if( sError == pdFALSE )
\r
262 /* If the calculation has always been correct, increment the check
\r
263 variable so we know this task is still running okay. */
\r
264 ( *pusTaskCheckVariable )++;
\r
268 /*-----------------------------------------------------------*/
\r
270 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
272 volatile float *pfArray, fTotal1, fTotal2, fDifference, fPosition;
\r
273 volatile unsigned short *pusTaskCheckVariable;
\r
274 const size_t xArraySize = 10;
\r
276 short sError = pdFALSE;
\r
278 /* The variable this task increments to show it is still running is passed in
\r
279 as the parameter. */
\r
280 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
282 pfArray = ( float * ) pvPortMalloc( xArraySize * sizeof( float ) );
\r
284 /* Keep filling an array, keeping a running total of the values placed in the
\r
285 array. Then run through the array adding up all the values. If the two totals
\r
286 do not match, stop the check variable from incrementing. */
\r
293 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
295 pfArray[ xPosition ] = fPosition * 12.123F;
\r
296 fTotal1 += fPosition * 12.123F;
\r
299 #if configUSE_PREEMPTION == 0
\r
303 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
305 fTotal2 += pfArray[ xPosition ];
\r
308 fDifference = fTotal1 - fTotal2;
\r
309 if( fabs( fDifference ) > 0.001F )
\r
314 #if configUSE_PREEMPTION == 0
\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 short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 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