2 FreeRTOS V8.0.1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 * Creates eight tasks, each of which loops continuously performing a
\r
68 * floating point calculation.
\r
70 * All the tasks run at the idle priority and never block or yield. This causes
\r
71 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
72 * means that these tasks will get pre-empted any time another task is ready to run
\r
73 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
74 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
75 * calculation producing an unexpected result could be a symptom of a corruption in
\r
76 * the context of a task.
\r
78 * This file demonstrates the use of the task tag and traceTASK_SWITCHED_IN and
\r
79 * traceTASK_SWITCHED_OUT macros to save and restore the floating point context.
\r
85 /* Scheduler include files. */
\r
86 #include "FreeRTOS.h"
\r
89 /* Demo program include files. */
\r
92 /* Misc. definitions. */
\r
93 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
94 #define mathNUMBER_OF_TASKS ( 8 )
\r
96 /* Four tasks, each of which performs a different floating point calculation.
\r
97 Each of the four is created twice. */
\r
98 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
99 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
100 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
101 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
103 /* These variables are used to check that all the tasks are still running. If a
\r
104 task gets a calculation wrong it will stop incrementing its check variable. */
\r
105 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
107 /* Buffers into which the flop registers will be saved. There is a buffer for
\r
108 each task created within this file. Zeroing out this array is the normal and
\r
109 safe option as this will cause the task to start with all zeros in its flop
\r
111 static unsigned long ulFlopRegisters[ mathNUMBER_OF_TASKS ][ portNO_FLOP_REGISTERS_TO_SAVE ];
\r
113 /*-----------------------------------------------------------*/
\r
115 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
117 TaskHandle_t xTaskJustCreated;
\r
118 portBASE_TYPE x, y;
\r
120 /* Place known values into the buffers into which the flop registers are
\r
121 to be saved. This is for debug purposes only, it is not normally
\r
122 required. The last position in each array is left at zero as the status
\r
123 register will be loaded from there.
\r
125 It is intended that these values can be viewed being loaded into the
\r
126 flop registers when a task is started - however the Insight debugger
\r
127 does not seem to want to show the flop register values. */
\r
128 for( x = 0; x < mathNUMBER_OF_TASKS; x++ )
\r
130 for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1 ); y++ )
\r
132 ulFlopRegisters[ x ][ y ] = ( x + 1 );
\r
136 /* Create the first task - passing it the address of the check variable
\r
137 that it is going to increment. This check variable is used as an
\r
138 indication that the task is still running. */
\r
139 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xTaskJustCreated );
\r
141 /* The task tag value is a value that can be associated with a task, but
\r
142 is not used by the scheduler itself. Its use is down to the application so
\r
143 it makes a convenient place in this case to store the pointer to the buffer
\r
144 into which the flop context of the task will be stored. The first created
\r
145 task uses ulFlopRegisters[ 0 ], the second ulFlopRegisters[ 1 ], etc. */
\r
146 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 0 ][ 0 ] ) );
\r
148 /* Create another 7 tasks, allocating a buffer for each. */
\r
149 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xTaskJustCreated );
\r
150 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 1 ][ 0 ] ) );
\r
152 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xTaskJustCreated );
\r
153 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 2 ][ 0 ] ) );
\r
155 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xTaskJustCreated );
\r
156 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 3 ][ 0 ] ) );
\r
158 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xTaskJustCreated );
\r
159 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 4 ][ 0 ] ) );
\r
161 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xTaskJustCreated );
\r
162 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 5 ][ 0 ] ) );
\r
164 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xTaskJustCreated );
\r
165 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 6 ][ 0 ] ) );
\r
167 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xTaskJustCreated );
\r
168 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 7 ][ 0 ] ) );
\r
170 /*-----------------------------------------------------------*/
\r
172 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
174 volatile portFLOAT ff1, ff2, ff3, ff4;
\r
175 volatile unsigned short *pusTaskCheckVariable;
\r
176 volatile portFLOAT fAnswer;
\r
177 short sError = pdFALSE;
\r
183 fAnswer = ( ff1 + ff2 ) * ff3;
\r
185 /* The variable this task increments to show it is still running is passed in
\r
186 as the parameter. */
\r
187 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
189 /* Keep performing a calculation and checking the result against a constant. */
\r
196 ff4 = ( ff1 + ff2 ) * ff3;
\r
198 #if configUSE_PREEMPTION == 0
\r
202 /* If the calculation does not match the expected constant, stop the
\r
203 increment of the check variable. */
\r
204 if( fabs( ff4 - fAnswer ) > 0.001F )
\r
209 if( sError == pdFALSE )
\r
211 /* If the calculation has always been correct, increment the check
\r
212 variable so we know this task is still running okay. */
\r
213 ( *pusTaskCheckVariable )++;
\r
216 #if configUSE_PREEMPTION == 0
\r
222 /*-----------------------------------------------------------*/
\r
224 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
226 volatile portFLOAT ff1, ff2, ff3, ff4;
\r
227 volatile unsigned short *pusTaskCheckVariable;
\r
228 volatile portFLOAT fAnswer;
\r
229 short sError = pdFALSE;
\r
235 fAnswer = ( ff1 / ff2 ) * ff3;
\r
238 /* The variable this task increments to show it is still running is passed in
\r
239 as the parameter. */
\r
240 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
242 /* Keep performing a calculation and checking the result against a constant. */
\r
249 ff4 = ( ff1 / ff2 ) * ff3;
\r
251 #if configUSE_PREEMPTION == 0
\r
255 /* If the calculation does not match the expected constant, stop the
\r
256 increment of the check variable. */
\r
257 if( fabs( ff4 - fAnswer ) > 0.001F )
\r
262 if( sError == pdFALSE )
\r
264 /* If the calculation has always been correct, increment the check
\r
265 variable so we know
\r
266 this task is still running okay. */
\r
267 ( *pusTaskCheckVariable )++;
\r
270 #if configUSE_PREEMPTION == 0
\r
275 /*-----------------------------------------------------------*/
\r
277 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
279 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
\r
280 volatile unsigned short *pusTaskCheckVariable;
\r
281 const size_t xArraySize = 10;
\r
283 short sError = pdFALSE;
\r
285 /* The variable this task increments to show it is still running is passed in
\r
286 as the parameter. */
\r
287 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
289 pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
\r
291 /* Keep filling an array, keeping a running total of the values placed in the
\r
292 array. Then run through the array adding up all the values. If the two totals
\r
293 do not match, stop the check variable from incrementing. */
\r
299 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
301 pfArray[ xPosition ] = ( portFLOAT ) xPosition + 5.5F;
\r
302 fTotal1 += ( portFLOAT ) xPosition + 5.5F;
\r
305 #if configUSE_PREEMPTION == 0
\r
309 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
311 fTotal2 += pfArray[ xPosition ];
\r
314 fDifference = fTotal1 - fTotal2;
\r
315 if( fabs( fDifference ) > 0.001F )
\r
320 #if configUSE_PREEMPTION == 0
\r
324 if( sError == pdFALSE )
\r
326 /* If the calculation has always been correct, increment the check
\r
327 variable so we know this task is still running okay. */
\r
328 ( *pusTaskCheckVariable )++;
\r
332 /*-----------------------------------------------------------*/
\r
334 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
336 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
\r
337 volatile unsigned short *pusTaskCheckVariable;
\r
338 const size_t xArraySize = 10;
\r
340 short sError = pdFALSE;
\r
342 /* The variable this task increments to show it is still running is passed in
\r
343 as the parameter. */
\r
344 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
346 pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
\r
348 /* Keep filling an array, keeping a running total of the values placed in the
\r
349 array. Then run through the array adding up all the values. If the two totals
\r
350 do not match, stop the check variable from incrementing. */
\r
356 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
358 pfArray[ xPosition ] = ( portFLOAT ) xPosition * 12.123F;
\r
359 fTotal1 += ( portFLOAT ) xPosition * 12.123F;
\r
362 #if configUSE_PREEMPTION == 0
\r
366 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
368 fTotal2 += pfArray[ xPosition ];
\r
371 fDifference = fTotal1 - fTotal2;
\r
372 if( fabs( fDifference ) > 0.001F )
\r
377 #if configUSE_PREEMPTION == 0
\r
381 if( sError == pdFALSE )
\r
383 /* If the calculation has always been correct, increment the check
\r
384 variable so we know this task is still running okay. */
\r
385 ( *pusTaskCheckVariable )++;
\r
389 /*-----------------------------------------------------------*/
\r
391 /* This is called to check that all the created tasks are still running. */
\r
392 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
394 /* Keep a history of the check variables so we know if they have been incremented
\r
395 since the last call. */
\r
396 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
397 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
399 /* Check the maths tasks are still running by ensuring their check variables
\r
400 are still incrementing. */
\r
401 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
403 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
405 /* The check has not incremented so an error exists. */
\r
409 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r