2 FreeRTOS V8.2.2 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
71 * Creates eight tasks, each of which loops continuously performing a
\r
72 * floating point calculation.
\r
74 * All the tasks run at the idle priority and never block or yield. This causes
\r
75 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
76 * means that these tasks will get pre-empted any time another task is ready to run
\r
77 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
78 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
79 * calculation producing an unexpected result could be a symptom of a corruption in
\r
80 * the context of a task.
\r
82 * This file demonstrates the use of the task tag and traceTASK_SWITCHED_IN and
\r
83 * traceTASK_SWITCHED_OUT macros to save and restore the floating point context.
\r
89 /* Scheduler include files. */
\r
90 #include "FreeRTOS.h"
\r
93 /* Demo program include files. */
\r
96 /* Misc. definitions. */
\r
97 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
98 #define mathNUMBER_OF_TASKS ( 8 )
\r
100 /* Four tasks, each of which performs a different floating point calculation.
\r
101 Each of the four is created twice. */
\r
102 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
103 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
104 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
105 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
107 /* These variables are used to check that all the tasks are still running. If a
\r
108 task gets a calculation wrong it will stop incrementing its check variable. */
\r
109 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
111 /* Buffers into which the flop registers will be saved. There is a buffer for
\r
112 each task created within this file. Zeroing out this array is the normal and
\r
113 safe option as this will cause the task to start with all zeros in its flop
\r
115 static unsigned long ulFlopRegisters[ mathNUMBER_OF_TASKS ][ portNO_FLOP_REGISTERS_TO_SAVE ];
\r
117 /*-----------------------------------------------------------*/
\r
119 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
121 TaskHandle_t xTaskJustCreated;
\r
122 portBASE_TYPE x, y;
\r
124 /* Place known values into the buffers into which the flop registers are
\r
125 to be saved. This is for debug purposes only, it is not normally
\r
126 required. The last position in each array is left at zero as the status
\r
127 register will be loaded from there.
\r
129 It is intended that these values can be viewed being loaded into the
\r
130 flop registers when a task is started - however the Insight debugger
\r
131 does not seem to want to show the flop register values. */
\r
132 for( x = 0; x < mathNUMBER_OF_TASKS; x++ )
\r
134 for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1 ); y++ )
\r
136 ulFlopRegisters[ x ][ y ] = ( x + 1 );
\r
140 /* Create the first task - passing it the address of the check variable
\r
141 that it is going to increment. This check variable is used as an
\r
142 indication that the task is still running. */
\r
143 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xTaskJustCreated );
\r
145 /* The task tag value is a value that can be associated with a task, but
\r
146 is not used by the scheduler itself. Its use is down to the application so
\r
147 it makes a convenient place in this case to store the pointer to the buffer
\r
148 into which the flop context of the task will be stored. The first created
\r
149 task uses ulFlopRegisters[ 0 ], the second ulFlopRegisters[ 1 ], etc. */
\r
150 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 0 ][ 0 ] ) );
\r
152 /* Create another 7 tasks, allocating a buffer for each. */
\r
153 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xTaskJustCreated );
\r
154 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 1 ][ 0 ] ) );
\r
156 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xTaskJustCreated );
\r
157 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 2 ][ 0 ] ) );
\r
159 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xTaskJustCreated );
\r
160 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 3 ][ 0 ] ) );
\r
162 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xTaskJustCreated );
\r
163 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 4 ][ 0 ] ) );
\r
165 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xTaskJustCreated );
\r
166 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 5 ][ 0 ] ) );
\r
168 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xTaskJustCreated );
\r
169 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 6 ][ 0 ] ) );
\r
171 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xTaskJustCreated );
\r
172 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 7 ][ 0 ] ) );
\r
174 /*-----------------------------------------------------------*/
\r
176 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
178 volatile portFLOAT ff1, ff2, ff3, ff4;
\r
179 volatile unsigned short *pusTaskCheckVariable;
\r
180 volatile portFLOAT fAnswer;
\r
181 short sError = pdFALSE;
\r
187 fAnswer = ( ff1 + ff2 ) * ff3;
\r
189 /* The variable this task increments to show it is still running is passed in
\r
190 as the parameter. */
\r
191 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
193 /* Keep performing a calculation and checking the result against a constant. */
\r
200 ff4 = ( ff1 + ff2 ) * ff3;
\r
202 #if configUSE_PREEMPTION == 0
\r
206 /* If the calculation does not match the expected constant, stop the
\r
207 increment of the check variable. */
\r
208 if( fabs( ff4 - fAnswer ) > 0.001F )
\r
213 if( sError == pdFALSE )
\r
215 /* If the calculation has always been correct, increment the check
\r
216 variable so we know this task is still running okay. */
\r
217 ( *pusTaskCheckVariable )++;
\r
220 #if configUSE_PREEMPTION == 0
\r
226 /*-----------------------------------------------------------*/
\r
228 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
230 volatile portFLOAT ff1, ff2, ff3, ff4;
\r
231 volatile unsigned short *pusTaskCheckVariable;
\r
232 volatile portFLOAT fAnswer;
\r
233 short sError = pdFALSE;
\r
239 fAnswer = ( ff1 / ff2 ) * ff3;
\r
242 /* The variable this task increments to show it is still running is passed in
\r
243 as the parameter. */
\r
244 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
246 /* Keep performing a calculation and checking the result against a constant. */
\r
253 ff4 = ( ff1 / ff2 ) * ff3;
\r
255 #if configUSE_PREEMPTION == 0
\r
259 /* If the calculation does not match the expected constant, stop the
\r
260 increment of the check variable. */
\r
261 if( fabs( ff4 - fAnswer ) > 0.001F )
\r
266 if( sError == pdFALSE )
\r
268 /* If the calculation has always been correct, increment the check
\r
269 variable so we know
\r
270 this task is still running okay. */
\r
271 ( *pusTaskCheckVariable )++;
\r
274 #if configUSE_PREEMPTION == 0
\r
279 /*-----------------------------------------------------------*/
\r
281 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
283 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
\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 = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
\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
303 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
305 pfArray[ xPosition ] = ( portFLOAT ) xPosition + 5.5F;
\r
306 fTotal1 += ( portFLOAT ) xPosition + 5.5F;
\r
309 #if configUSE_PREEMPTION == 0
\r
313 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
315 fTotal2 += pfArray[ xPosition ];
\r
318 fDifference = fTotal1 - fTotal2;
\r
319 if( fabs( fDifference ) > 0.001F )
\r
324 #if configUSE_PREEMPTION == 0
\r
328 if( sError == pdFALSE )
\r
330 /* If the calculation has always been correct, increment the check
\r
331 variable so we know this task is still running okay. */
\r
332 ( *pusTaskCheckVariable )++;
\r
336 /*-----------------------------------------------------------*/
\r
338 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
340 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
\r
341 volatile unsigned short *pusTaskCheckVariable;
\r
342 const size_t xArraySize = 10;
\r
344 short sError = pdFALSE;
\r
346 /* The variable this task increments to show it is still running is passed in
\r
347 as the parameter. */
\r
348 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
350 pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
\r
352 /* Keep filling an array, keeping a running total of the values placed in the
\r
353 array. Then run through the array adding up all the values. If the two totals
\r
354 do not match, stop the check variable from incrementing. */
\r
360 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
362 pfArray[ xPosition ] = ( portFLOAT ) xPosition * 12.123F;
\r
363 fTotal1 += ( portFLOAT ) xPosition * 12.123F;
\r
366 #if configUSE_PREEMPTION == 0
\r
370 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
372 fTotal2 += pfArray[ xPosition ];
\r
375 fDifference = fTotal1 - fTotal2;
\r
376 if( fabs( fDifference ) > 0.001F )
\r
381 #if configUSE_PREEMPTION == 0
\r
385 if( sError == pdFALSE )
\r
387 /* If the calculation has always been correct, increment the check
\r
388 variable so we know this task is still running okay. */
\r
389 ( *pusTaskCheckVariable )++;
\r
393 /*-----------------------------------------------------------*/
\r
395 /* This is called to check that all the created tasks are still running. */
\r
396 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
398 /* Keep a history of the check variables so we know if they have been incremented
\r
399 since the last call. */
\r
400 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
401 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
403 /* Check the maths tasks are still running by ensuring their check variables
\r
404 are still incrementing. */
\r
405 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
407 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
409 /* The check has not incremented so an error exists. */
\r
413 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r