2 FreeRTOS V7.5.2 - 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
\r
67 * floating point calculation.
\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
77 * This file demonstrates the use of the task tag and traceTASK_SWITCHED_IN and
\r
78 * traceTASK_SWITCHED_OUT macros to save and restore the floating point context.
\r
84 /* Scheduler include files. */
\r
85 #include "FreeRTOS.h"
\r
88 /* Demo program include files. */
\r
91 /* Misc. definitions. */
\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 stop incrementing its check variable. */
\r
104 static volatile unsigned portSHORT usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
106 /* Buffers into which the flop registers will be saved. There is a buffer for
\r
107 each task created within this file. Zeroing out this array is the normal and
\r
108 safe option as this will cause the task to start with all zeros in its flop
\r
110 static unsigned portLONG ulFlopRegisters[ mathNUMBER_OF_TASKS ][ portNO_FLOP_REGISTERS_TO_SAVE ];
\r
112 /*-----------------------------------------------------------*/
\r
114 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
116 xTaskHandle xTaskJustCreated;
\r
117 portBASE_TYPE x, y;
\r
119 /* Place known values into the buffers into which the flop registers are
\r
120 to be saved. This is for debug purposes only, it is not normally
\r
121 required. The last position in each array is left at zero as the status
\r
122 register will be loaded from there.
\r
124 It is intended that these values can be viewed being loaded into the
\r
125 flop registers when a task is started - however the Insight debugger
\r
126 does not seem to want to show the flop register values. */
\r
127 for( x = 0; x < mathNUMBER_OF_TASKS; x++ )
\r
129 for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1 ); y++ )
\r
131 ulFlopRegisters[ x ][ y ] = ( x + 1 );
\r
135 /* Create the first task - passing it the address of the check variable
\r
136 that it is going to increment. This check variable is used as an
\r
137 indication that the task is still running. */
\r
138 xTaskCreate( vCompetingMathTask1, ( signed portCHAR * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xTaskJustCreated );
\r
140 /* The task tag value is a value that can be associated with a task, but
\r
141 is not used by the scheduler itself. Its use is down to the application so
\r
142 it makes a convenient place in this case to store the pointer to the buffer
\r
143 into which the flop context of the task will be stored. The first created
\r
144 task uses ulFlopRegisters[ 0 ], the second ulFlopRegisters[ 1 ], etc. */
\r
145 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 0 ][ 0 ] ) );
\r
147 /* Create another 7 tasks, allocating a buffer for each. */
\r
148 xTaskCreate( vCompetingMathTask2, ( signed portCHAR * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xTaskJustCreated );
\r
149 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 1 ][ 0 ] ) );
\r
151 xTaskCreate( vCompetingMathTask3, ( signed portCHAR * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xTaskJustCreated );
\r
152 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 2 ][ 0 ] ) );
\r
154 xTaskCreate( vCompetingMathTask4, ( signed portCHAR * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xTaskJustCreated );
\r
155 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 3 ][ 0 ] ) );
\r
157 xTaskCreate( vCompetingMathTask1, ( signed portCHAR * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xTaskJustCreated );
\r
158 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 4 ][ 0 ] ) );
\r
160 xTaskCreate( vCompetingMathTask2, ( signed portCHAR * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xTaskJustCreated );
\r
161 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 5 ][ 0 ] ) );
\r
163 xTaskCreate( vCompetingMathTask3, ( signed portCHAR * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xTaskJustCreated );
\r
164 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 6 ][ 0 ] ) );
\r
166 xTaskCreate( vCompetingMathTask4, ( signed portCHAR * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xTaskJustCreated );
\r
167 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 7 ][ 0 ] ) );
\r
169 /*-----------------------------------------------------------*/
\r
171 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
173 volatile portFLOAT ff1, ff2, ff3, ff4;
\r
174 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
175 volatile portFLOAT fAnswer;
\r
176 portSHORT sError = pdFALSE;
\r
182 fAnswer = ( ff1 + ff2 ) * ff3;
\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 portSHORT * ) pvParameters;
\r
188 /* Keep performing a calculation and checking the result against a constant. */
\r
195 ff4 = ( ff1 + ff2 ) * ff3;
\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( ff4 - 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 this task is still running okay. */
\r
212 ( *pusTaskCheckVariable )++;
\r
215 #if configUSE_PREEMPTION == 0
\r
221 /*-----------------------------------------------------------*/
\r
223 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
225 volatile portFLOAT ff1, ff2, ff3, ff4;
\r
226 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
227 volatile portFLOAT fAnswer;
\r
228 portSHORT sError = pdFALSE;
\r
234 fAnswer = ( ff1 / ff2 ) * ff3;
\r
237 /* The variable this task increments to show it is still running is passed in
\r
238 as the parameter. */
\r
239 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
241 /* Keep performing a calculation and checking the result against a constant. */
\r
248 ff4 = ( ff1 / ff2 ) * ff3;
\r
250 #if configUSE_PREEMPTION == 0
\r
254 /* If the calculation does not match the expected constant, stop the
\r
255 increment of the check variable. */
\r
256 if( fabs( ff4 - fAnswer ) > 0.001F )
\r
261 if( sError == pdFALSE )
\r
263 /* If the calculation has always been correct, increment the check
\r
264 variable so we know
\r
265 this task is still running okay. */
\r
266 ( *pusTaskCheckVariable )++;
\r
269 #if configUSE_PREEMPTION == 0
\r
274 /*-----------------------------------------------------------*/
\r
276 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
278 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
\r
279 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
280 const size_t xArraySize = 10;
\r
282 portSHORT sError = pdFALSE;
\r
284 /* The variable this task increments to show it is still running is passed in
\r
285 as the parameter. */
\r
286 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
288 pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
\r
290 /* Keep filling an array, keeping a running total of the values placed in the
\r
291 array. Then run through the array adding up all the values. If the two totals
\r
292 do not match, stop the check variable from incrementing. */
\r
298 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
300 pfArray[ xPosition ] = ( portFLOAT ) xPosition + 5.5F;
\r
301 fTotal1 += ( portFLOAT ) xPosition + 5.5F;
\r
304 #if configUSE_PREEMPTION == 0
\r
308 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
310 fTotal2 += pfArray[ xPosition ];
\r
313 fDifference = fTotal1 - fTotal2;
\r
314 if( fabs( fDifference ) > 0.001F )
\r
319 #if configUSE_PREEMPTION == 0
\r
323 if( sError == pdFALSE )
\r
325 /* If the calculation has always been correct, increment the check
\r
326 variable so we know this task is still running okay. */
\r
327 ( *pusTaskCheckVariable )++;
\r
331 /*-----------------------------------------------------------*/
\r
333 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
335 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
\r
336 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
337 const size_t xArraySize = 10;
\r
339 portSHORT sError = pdFALSE;
\r
341 /* The variable this task increments to show it is still running is passed in
\r
342 as the parameter. */
\r
343 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
345 pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
\r
347 /* Keep filling an array, keeping a running total of the values placed in the
\r
348 array. Then run through the array adding up all the values. If the two totals
\r
349 do not match, stop the check variable from incrementing. */
\r
355 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
357 pfArray[ xPosition ] = ( portFLOAT ) xPosition * 12.123F;
\r
358 fTotal1 += ( portFLOAT ) xPosition * 12.123F;
\r
361 #if configUSE_PREEMPTION == 0
\r
365 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
367 fTotal2 += pfArray[ xPosition ];
\r
370 fDifference = fTotal1 - fTotal2;
\r
371 if( fabs( fDifference ) > 0.001F )
\r
376 #if configUSE_PREEMPTION == 0
\r
380 if( sError == pdFALSE )
\r
382 /* If the calculation has always been correct, increment the check
\r
383 variable so we know this task is still running okay. */
\r
384 ( *pusTaskCheckVariable )++;
\r
388 /*-----------------------------------------------------------*/
\r
390 /* This is called to check that all the created tasks are still running. */
\r
391 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
393 /* Keep a history of the check variables so we know if they have been incremented
\r
394 since the last call. */
\r
395 static unsigned portSHORT usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
396 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
398 /* Check the maths tasks are still running by ensuring their check variables
\r
399 are still incrementing. */
\r
400 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
402 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
404 /* The check has not incremented so an error exists. */
\r
408 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r