2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * Creates eight tasks, each of which loops continuously performing a floating
\r
67 * point calculation - using single precision variables.
\r
69 * All the tasks run at the idle priority and never block or yield. This causes
\r
70 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
71 * means that these tasks will get pre-empted any time another task is ready to run
\r
72 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
73 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
74 * calculation producing an unexpected result could be a symptom of a corruption in
\r
75 * the context of a task.
\r
81 /* Scheduler include files. */
\r
82 #include "FreeRTOS.h"
\r
85 /* Demo program include files. */
\r
88 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
89 #define mathNUMBER_OF_TASKS ( 8 )
\r
91 /* Four tasks, each of which performs a different floating point calculation.
\r
92 Each of the four is created twice. */
\r
93 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
94 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
95 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
96 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
98 /* These variables are used to check that all the tasks are still running. If a
\r
99 task gets a calculation wrong it will
\r
100 stop incrementing its check variable. */
\r
101 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
103 /*-----------------------------------------------------------*/
\r
105 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
107 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
108 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
109 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
110 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
111 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
112 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
113 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
114 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
116 /*-----------------------------------------------------------*/
\r
118 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
120 volatile float f1, f2, f3, f4;
\r
121 volatile unsigned short *pusTaskCheckVariable;
\r
122 volatile float fAnswer;
\r
123 short sError = pdFALSE;
\r
129 fAnswer = ( f1 + f2 ) * f3;
\r
131 /* The variable this task increments to show it is still running is passed in
\r
132 as the parameter. */
\r
133 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
135 /* Keep performing a calculation and checking the result against a constant. */
\r
142 f4 = ( f1 + f2 ) * f3;
\r
144 #if configUSE_PREEMPTION == 0
\r
148 /* If the calculation does not match the expected constant, stop the
\r
149 increment of the check variable. */
\r
150 if( fabs( f4 - fAnswer ) > 0.001F )
\r
155 if( sError == pdFALSE )
\r
157 /* If the calculation has always been correct, increment the check
\r
158 variable so we know this task is still running okay. */
\r
159 ( *pusTaskCheckVariable )++;
\r
162 #if configUSE_PREEMPTION == 0
\r
168 /*-----------------------------------------------------------*/
\r
170 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
172 volatile float f1, f2, f3, f4;
\r
173 volatile unsigned short *pusTaskCheckVariable;
\r
174 volatile float fAnswer;
\r
175 short sError = pdFALSE;
\r
181 fAnswer = ( f1 / f2 ) * f3;
\r
184 /* The variable this task increments to show it is still running is passed in
\r
185 as the parameter. */
\r
186 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
188 /* Keep performing a calculation and checking the result against a constant. */
\r
195 f4 = ( f1 / f2 ) * f3;
\r
197 #if configUSE_PREEMPTION == 0
\r
201 /* If the calculation does not match the expected constant, stop the
\r
202 increment of the check variable. */
\r
203 if( fabs( f4 - fAnswer ) > 0.001F )
\r
208 if( sError == pdFALSE )
\r
210 /* If the calculation has always been correct, increment the check
\r
211 variable so we know
\r
212 this task is still running okay. */
\r
213 ( *pusTaskCheckVariable )++;
\r
216 #if configUSE_PREEMPTION == 0
\r
221 /*-----------------------------------------------------------*/
\r
223 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
225 volatile float *pfArray, fTotal1, fTotal2, fDifference, fPosition;
\r
226 volatile unsigned short *pusTaskCheckVariable;
\r
227 const size_t xArraySize = 10;
\r
229 short sError = pdFALSE;
\r
231 /* The variable this task increments to show it is still running is passed in
\r
232 as the parameter. */
\r
233 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
235 pfArray = ( float * ) pvPortMalloc( xArraySize * sizeof( float ) );
\r
237 /* Keep filling an array, keeping a running total of the values placed in the
\r
238 array. Then run through the array adding up all the values. If the two totals
\r
239 do not match, stop the check variable from incrementing. */
\r
246 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
248 pfArray[ xPosition ] = fPosition + 5.5F;
\r
249 fTotal1 += fPosition + 5.5F;
\r
252 #if configUSE_PREEMPTION == 0
\r
256 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
258 fTotal2 += pfArray[ xPosition ];
\r
261 fDifference = fTotal1 - fTotal2;
\r
262 if( fabs( fDifference ) > 0.001F )
\r
267 #if configUSE_PREEMPTION == 0
\r
271 if( sError == pdFALSE )
\r
273 /* If the calculation has always been correct, increment the check
\r
274 variable so we know this task is still running okay. */
\r
275 ( *pusTaskCheckVariable )++;
\r
279 /*-----------------------------------------------------------*/
\r
281 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
283 volatile float *pfArray, fTotal1, fTotal2, fDifference, fPosition;
\r
284 volatile unsigned short *pusTaskCheckVariable;
\r
285 const size_t xArraySize = 10;
\r
287 short sError = pdFALSE;
\r
289 /* The variable this task increments to show it is still running is passed in
\r
290 as the parameter. */
\r
291 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
293 pfArray = ( float * ) pvPortMalloc( xArraySize * sizeof( float ) );
\r
295 /* Keep filling an array, keeping a running total of the values placed in the
\r
296 array. Then run through the array adding up all the values. If the two totals
\r
297 do not match, stop the check variable from incrementing. */
\r
304 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
306 pfArray[ xPosition ] = fPosition * 12.123F;
\r
307 fTotal1 += fPosition * 12.123F;
\r
310 #if configUSE_PREEMPTION == 0
\r
314 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
316 fTotal2 += pfArray[ xPosition ];
\r
319 fDifference = fTotal1 - fTotal2;
\r
320 if( fabs( fDifference ) > 0.001F )
\r
325 #if configUSE_PREEMPTION == 0
\r
329 if( sError == pdFALSE )
\r
331 /* If the calculation has always been correct, increment the check
\r
332 variable so we know this task is still running okay. */
\r
333 ( *pusTaskCheckVariable )++;
\r
337 /*-----------------------------------------------------------*/
\r
339 /* This is called to check that all the created tasks are still running. */
\r
340 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
342 /* Keep a history of the check variables so we know if they have been incremented
\r
343 since the last call. */
\r
344 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
345 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
347 /* Check the maths tasks are still running by ensuring their check variables
\r
348 are still incrementing. */
\r
349 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
351 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
353 /* The check has not incremented so an error exists. */
\r
357 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r