2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 * Creates eight tasks, each of which loops continuously performing an (emulated)
\r
71 * floating point calculation.
\r
73 * All the tasks run at the idle priority and never block or yield. This causes
\r
74 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
75 * means that these tasks will get pre-empted any time another task is ready to run
\r
76 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
77 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
78 * calculation producing an unexpected result could be a symptom of a corruption in
\r
79 * the context of a task.
\r
85 /* Scheduler include files. */
\r
86 #include "FreeRTOS.h"
\r
89 /* Demo program include files. */
\r
92 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
93 #define mathNUMBER_OF_TASKS ( 8 )
\r
95 /* Four tasks, each of which performs a different floating point calculation.
\r
96 Each of the four is created twice. */
\r
97 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
98 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
99 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
100 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
102 /* These variables are used to check that all the tasks are still running. If a
\r
103 task gets a calculation wrong it will
\r
104 stop incrementing its check variable. */
\r
105 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
107 /*-----------------------------------------------------------*/
\r
109 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
111 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
112 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
113 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
114 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
115 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
116 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
117 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
118 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
120 /*-----------------------------------------------------------*/
\r
122 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
124 volatile portDOUBLE d1, d2, d3, d4;
\r
125 volatile unsigned short *pusTaskCheckVariable;
\r
126 volatile portDOUBLE dAnswer;
\r
127 short sError = pdFALSE;
\r
133 dAnswer = ( d1 + d2 ) * d3;
\r
135 /* The variable this task increments to show it is still running is passed in
\r
136 as the parameter. */
\r
137 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
139 /* Keep performing a calculation and checking the result against a constant. */
\r
146 d4 = ( d1 + d2 ) * d3;
\r
148 #if configUSE_PREEMPTION == 0
\r
152 /* If the calculation does not match the expected constant, stop the
\r
153 increment of the check variable. */
\r
154 if( fabs( d4 - dAnswer ) > 0.001 )
\r
159 if( sError == pdFALSE )
\r
161 /* If the calculation has always been correct, increment the check
\r
162 variable so we know this task is still running okay. */
\r
163 ( *pusTaskCheckVariable )++;
\r
166 #if configUSE_PREEMPTION == 0
\r
172 /*-----------------------------------------------------------*/
\r
174 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
176 volatile portDOUBLE d1, d2, d3, d4;
\r
177 volatile unsigned short *pusTaskCheckVariable;
\r
178 volatile portDOUBLE dAnswer;
\r
179 short sError = pdFALSE;
\r
185 dAnswer = ( d1 / d2 ) * d3;
\r
188 /* The variable this task increments to show it is still running is passed in
\r
189 as the parameter. */
\r
190 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
192 /* Keep performing a calculation and checking the result against a constant. */
\r
199 d4 = ( d1 / d2 ) * d3;
\r
201 #if configUSE_PREEMPTION == 0
\r
205 /* If the calculation does not match the expected constant, stop the
\r
206 increment of the check variable. */
\r
207 if( fabs( d4 - dAnswer ) > 0.001 )
\r
212 if( sError == pdFALSE )
\r
214 /* If the calculation has always been correct, increment the check
\r
215 variable so we know
\r
216 this task is still running okay. */
\r
217 ( *pusTaskCheckVariable )++;
\r
220 #if configUSE_PREEMPTION == 0
\r
225 /*-----------------------------------------------------------*/
\r
227 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
229 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
230 volatile unsigned short *pusTaskCheckVariable;
\r
231 const size_t xArraySize = 10;
\r
233 short sError = pdFALSE;
\r
235 /* The variable this task increments to show it is still running is passed in
\r
236 as the parameter. */
\r
237 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
239 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
241 /* Keep filling an array, keeping a running total of the values placed in the
\r
242 array. Then run through the array adding up all the values. If the two totals
\r
243 do not match, stop the check variable from incrementing. */
\r
249 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
251 pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;
\r
252 dTotal1 += ( portDOUBLE ) xPosition + 5.5;
\r
255 #if configUSE_PREEMPTION == 0
\r
259 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
261 dTotal2 += pdArray[ xPosition ];
\r
264 dDifference = dTotal1 - dTotal2;
\r
265 if( fabs( dDifference ) > 0.001 )
\r
270 #if configUSE_PREEMPTION == 0
\r
274 if( sError == pdFALSE )
\r
276 /* If the calculation has always been correct, increment the check
\r
277 variable so we know this task is still running okay. */
\r
278 ( *pusTaskCheckVariable )++;
\r
282 /*-----------------------------------------------------------*/
\r
284 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
286 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
287 volatile unsigned short *pusTaskCheckVariable;
\r
288 const size_t xArraySize = 10;
\r
290 short sError = pdFALSE;
\r
292 /* The variable this task increments to show it is still running is passed in
\r
293 as the parameter. */
\r
294 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
296 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
298 /* Keep filling an array, keeping a running total of the values placed in the
\r
299 array. Then run through the array adding up all the values. If the two totals
\r
300 do not match, stop the check variable from incrementing. */
\r
306 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
308 pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;
\r
309 dTotal1 += ( portDOUBLE ) xPosition * 12.123;
\r
312 #if configUSE_PREEMPTION == 0
\r
316 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
318 dTotal2 += pdArray[ xPosition ];
\r
321 dDifference = dTotal1 - dTotal2;
\r
322 if( fabs( dDifference ) > 0.001 )
\r
327 #if configUSE_PREEMPTION == 0
\r
331 if( sError == pdFALSE )
\r
333 /* If the calculation has always been correct, increment the check
\r
334 variable so we know this task is still running okay. */
\r
335 ( *pusTaskCheckVariable )++;
\r
339 /*-----------------------------------------------------------*/
\r
341 /* This is called to check that all the created tasks are still running. */
\r
342 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
344 /* Keep a history of the check variables so we know if they have been incremented
\r
345 since the last call. */
\r
346 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
347 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
349 /* Check the maths tasks are still running by ensuring their check variables
\r
350 are still incrementing. */
\r
351 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
353 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
355 /* The check has not incremented so an error exists. */
\r
359 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r