2 * FreeRTOS Kernel V10.2.0
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 * Creates eight tasks, each of which loops continuously performing a
\r
30 * floating point calculation.
\r
32 * All the tasks run at the idle priority and never block or yield. This causes
\r
33 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
34 * means that these tasks will get pre-empted any time another task is ready to run
\r
35 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
36 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
37 * calculation producing an unexpected result could be a symptom of a corruption in
\r
38 * the context of a task.
\r
40 * This file demonstrates the use of the task tag and traceTASK_SWITCHED_IN and
\r
41 * traceTASK_SWITCHED_OUT macros to save and restore the floating point context.
\r
47 /* Scheduler include files. */
\r
48 #include "FreeRTOS.h"
\r
51 /* Demo program include files. */
\r
54 /* Misc. definitions. */
\r
55 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
56 #define mathNUMBER_OF_TASKS ( 8 )
\r
58 /* Four tasks, each of which performs a different floating point calculation.
\r
59 Each of the four is created twice. */
\r
60 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
61 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
62 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
63 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
65 /* These variables are used to check that all the tasks are still running. If a
\r
66 task gets a calculation wrong it will stop incrementing its check variable. */
\r
67 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
69 /* Buffers into which the flop registers will be saved. There is a buffer for
\r
70 each task created within this file. Zeroing out this array is the normal and
\r
71 safe option as this will cause the task to start with all zeros in its flop
\r
73 static portDOUBLE dFlopRegisters[ mathNUMBER_OF_TASKS ][ portNO_FLOP_REGISTERS_TO_SAVE ];
\r
75 /*-----------------------------------------------------------*/
\r
77 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
79 TaskHandle_t xTaskJustCreated;
\r
82 /* Place known values into the buffers into which the flop registers are
\r
83 to be saved. This is for debug purposes only, it is not normally
\r
84 required. The last position in each array is left at zero as the status
\r
85 register will be loaded from there.
\r
87 It is intended that these values can be viewed being loaded into the
\r
88 flop registers when a task is started - however the Insight debugger
\r
89 does not seem to want to show the flop register values. */
\r
90 for( x = 0; x < mathNUMBER_OF_TASKS; x++ )
\r
92 for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1 ); y++ )
\r
94 dFlopRegisters[ x ][ y ] = ( x + 1 );
\r
98 /* Create the first task - passing it the address of the check variable
\r
99 that it is going to increment. This check variable is used as an
\r
100 indication that the task is still running. */
\r
101 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xTaskJustCreated );
\r
103 /* The task tag value is a value that can be associated with a task, but
\r
104 is not used by the scheduler itself. Its use is down to the application so
\r
105 it makes a convenient place in this case to store the pointer to the buffer
\r
106 into which the flop context of the task will be stored. The first created
\r
107 task uses dFlopRegisters[ 0 ], the second dFlopRegisters[ 1 ], etc. */
\r
108 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 0 ][ 0 ] ) );
\r
110 /* Create another 7 tasks, allocating a buffer for each. */
\r
111 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xTaskJustCreated );
\r
112 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 1 ][ 0 ] ) );
\r
114 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xTaskJustCreated );
\r
115 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 2 ][ 0 ] ) );
\r
117 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xTaskJustCreated );
\r
118 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 3 ][ 0 ] ) );
\r
120 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xTaskJustCreated );
\r
121 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 4 ][ 0 ] ) );
\r
123 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xTaskJustCreated );
\r
124 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 5 ][ 0 ] ) );
\r
126 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xTaskJustCreated );
\r
127 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 6 ][ 0 ] ) );
\r
129 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xTaskJustCreated );
\r
130 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 7 ][ 0 ] ) );
\r
132 /*-----------------------------------------------------------*/
\r
134 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
136 volatile portDOUBLE df1, df2, df3, df4;
\r
137 volatile unsigned short *pusTaskCheckVariable;
\r
138 volatile portDOUBLE dAnswer;
\r
139 short sError = pdFALSE;
\r
145 dAnswer = ( df1 + df2 ) * df3;
\r
147 /* The variable this task increments to show it is still running is passed in
\r
148 as the parameter. */
\r
149 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
151 /* Keep performing a calculation and checking the result against a constant. */
\r
158 df4 = ( df1 + df2 ) * df3;
\r
160 #if configUSE_PREEMPTION == 0
\r
164 /* If the calculation does not match the expected constant, stop the
\r
165 increment of the check variable. */
\r
166 if( fabs( df4 - dAnswer ) > 0.001 )
\r
171 if( sError == pdFALSE )
\r
173 /* If the calculation has always been correct, increment the check
\r
174 variable so we know this task is still running okay. */
\r
175 ( *pusTaskCheckVariable )++;
\r
178 #if configUSE_PREEMPTION == 0
\r
184 /*-----------------------------------------------------------*/
\r
186 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
188 volatile portDOUBLE df1, df2, df3, df4;
\r
189 volatile unsigned short *pusTaskCheckVariable;
\r
190 volatile portDOUBLE dAnswer;
\r
191 short sError = pdFALSE;
\r
197 dAnswer = ( df1 / df2 ) * df3;
\r
200 /* The variable this task increments to show it is still running is passed in
\r
201 as the parameter. */
\r
202 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
204 /* Keep performing a calculation and checking the result against a constant. */
\r
211 df4 = ( df1 / df2 ) * df3;
\r
213 #if configUSE_PREEMPTION == 0
\r
217 /* If the calculation does not match the expected constant, stop the
\r
218 increment of the check variable. */
\r
219 if( fabs( df4 - dAnswer ) > 0.001 )
\r
224 if( sError == pdFALSE )
\r
226 /* If the calculation has always been correct, increment the check
\r
227 variable so we know
\r
228 this task is still running okay. */
\r
229 ( *pusTaskCheckVariable )++;
\r
232 #if configUSE_PREEMPTION == 0
\r
237 /*-----------------------------------------------------------*/
\r
239 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
241 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
242 volatile unsigned short *pusTaskCheckVariable;
\r
243 const size_t xArraySize = 10;
\r
245 short sError = pdFALSE;
\r
247 /* The variable this task increments to show it is still running is passed in
\r
248 as the parameter. */
\r
249 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
251 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
253 /* Keep filling an array, keeping a running total of the values placed in the
\r
254 array. Then run through the array adding up all the values. If the two totals
\r
255 do not match, stop the check variable from incrementing. */
\r
261 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
263 pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;
\r
264 dTotal1 += ( portDOUBLE ) xPosition + 5.5;
\r
267 #if configUSE_PREEMPTION == 0
\r
271 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
273 dTotal2 += pdArray[ xPosition ];
\r
276 dDifference = dTotal1 - dTotal2;
\r
277 if( fabs( dDifference ) > 0.001 )
\r
282 #if configUSE_PREEMPTION == 0
\r
286 if( sError == pdFALSE )
\r
288 /* If the calculation has always been correct, increment the check
\r
289 variable so we know this task is still running okay. */
\r
290 ( *pusTaskCheckVariable )++;
\r
294 /*-----------------------------------------------------------*/
\r
296 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
298 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
299 volatile unsigned short *pusTaskCheckVariable;
\r
300 const size_t xArraySize = 10;
\r
302 short sError = pdFALSE;
\r
304 /* The variable this task increments to show it is still running is passed in
\r
305 as the parameter. */
\r
306 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
308 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
310 /* Keep filling an array, keeping a running total of the values placed in the
\r
311 array. Then run through the array adding up all the values. If the two totals
\r
312 do not match, stop the check variable from incrementing. */
\r
318 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
320 pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;
\r
321 dTotal1 += ( portDOUBLE ) xPosition * 12.123;
\r
324 #if configUSE_PREEMPTION == 0
\r
328 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
330 dTotal2 += pdArray[ xPosition ];
\r
333 dDifference = dTotal1 - dTotal2;
\r
334 if( fabs( dDifference ) > 0.001 )
\r
339 #if configUSE_PREEMPTION == 0
\r
343 if( sError == pdFALSE )
\r
345 /* If the calculation has always been correct, increment the check
\r
346 variable so we know this task is still running okay. */
\r
347 ( *pusTaskCheckVariable )++;
\r
351 /*-----------------------------------------------------------*/
\r
353 /* This is called to check that all the created tasks are still running. */
\r
354 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
356 /* Keep a history of the check variables so we know if they have been incremented
\r
357 since the last call. */
\r
358 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
359 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
361 /* Check the maths tasks are still running by ensuring their check variables
\r
362 are still incrementing. */
\r
363 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
365 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
367 /* The check has not incremented so an error exists. */
\r
371 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r