2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * Creates eight tasks, each of which loops continuously performing a
\r
31 * floating point calculation.
\r
33 * All the tasks run at the idle priority and never block or yield. This causes
\r
34 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
35 * means that these tasks will get pre-empted any time another task is ready to run
\r
36 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
37 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
38 * calculation producing an unexpected result could be a symptom of a corruption in
\r
39 * the context of a task.
\r
41 * This file demonstrates the use of the task tag and traceTASK_SWITCHED_IN and
\r
42 * traceTASK_SWITCHED_OUT macros to save and restore the floating point context.
\r
48 /* Scheduler include files. */
\r
49 #include "FreeRTOS.h"
\r
52 /* Demo program include files. */
\r
55 /* Misc. definitions. */
\r
56 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
57 #define mathNUMBER_OF_TASKS ( 8 )
\r
59 /* Four tasks, each of which performs a different floating point calculation.
\r
60 Each of the four is created twice. */
\r
61 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
62 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
63 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
64 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
66 /* These variables are used to check that all the tasks are still running. If a
\r
67 task gets a calculation wrong it will stop incrementing its check variable. */
\r
68 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
70 /* Buffers into which the flop registers will be saved. There is a buffer for
\r
71 each task created within this file. Zeroing out this array is the normal and
\r
72 safe option as this will cause the task to start with all zeros in its flop
\r
74 static unsigned long ulFlopRegisters[ mathNUMBER_OF_TASKS ][ portNO_FLOP_REGISTERS_TO_SAVE ];
\r
76 /*-----------------------------------------------------------*/
\r
78 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
80 TaskHandle_t xTaskJustCreated;
\r
83 /* Place known values into the buffers into which the flop registers are
\r
84 to be saved. This is for debug purposes only, it is not normally
\r
85 required. The last position in each array is left at zero as the status
\r
86 register will be loaded from there.
\r
88 It is intended that these values can be viewed being loaded into the
\r
89 flop registers when a task is started - however the Insight debugger
\r
90 does not seem to want to show the flop register values. */
\r
91 for( x = 0; x < mathNUMBER_OF_TASKS; x++ )
\r
93 for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1 ); y++ )
\r
95 ulFlopRegisters[ x ][ y ] = ( x + 1 );
\r
99 /* Create the first task - passing it the address of the check variable
\r
100 that it is going to increment. This check variable is used as an
\r
101 indication that the task is still running. */
\r
102 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xTaskJustCreated );
\r
104 /* The task tag value is a value that can be associated with a task, but
\r
105 is not used by the scheduler itself. Its use is down to the application so
\r
106 it makes a convenient place in this case to store the pointer to the buffer
\r
107 into which the flop context of the task will be stored. The first created
\r
108 task uses ulFlopRegisters[ 0 ], the second ulFlopRegisters[ 1 ], etc. */
\r
109 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 0 ][ 0 ] ) );
\r
111 /* Create another 7 tasks, allocating a buffer for each. */
\r
112 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xTaskJustCreated );
\r
113 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 1 ][ 0 ] ) );
\r
115 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xTaskJustCreated );
\r
116 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 2 ][ 0 ] ) );
\r
118 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xTaskJustCreated );
\r
119 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 3 ][ 0 ] ) );
\r
121 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xTaskJustCreated );
\r
122 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 4 ][ 0 ] ) );
\r
124 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xTaskJustCreated );
\r
125 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 5 ][ 0 ] ) );
\r
127 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xTaskJustCreated );
\r
128 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 6 ][ 0 ] ) );
\r
130 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xTaskJustCreated );
\r
131 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 7 ][ 0 ] ) );
\r
133 /*-----------------------------------------------------------*/
\r
135 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
137 volatile portFLOAT ff1, ff2, ff3, ff4;
\r
138 volatile unsigned short *pusTaskCheckVariable;
\r
139 volatile portFLOAT fAnswer;
\r
140 short sError = pdFALSE;
\r
146 fAnswer = ( ff1 + ff2 ) * ff3;
\r
148 /* The variable this task increments to show it is still running is passed in
\r
149 as the parameter. */
\r
150 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
152 /* Keep performing a calculation and checking the result against a constant. */
\r
159 ff4 = ( ff1 + ff2 ) * ff3;
\r
161 #if configUSE_PREEMPTION == 0
\r
165 /* If the calculation does not match the expected constant, stop the
\r
166 increment of the check variable. */
\r
167 if( fabs( ff4 - fAnswer ) > 0.001F )
\r
172 if( sError == pdFALSE )
\r
174 /* If the calculation has always been correct, increment the check
\r
175 variable so we know this task is still running okay. */
\r
176 ( *pusTaskCheckVariable )++;
\r
179 #if configUSE_PREEMPTION == 0
\r
185 /*-----------------------------------------------------------*/
\r
187 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
189 volatile portFLOAT ff1, ff2, ff3, ff4;
\r
190 volatile unsigned short *pusTaskCheckVariable;
\r
191 volatile portFLOAT fAnswer;
\r
192 short sError = pdFALSE;
\r
198 fAnswer = ( ff1 / ff2 ) * ff3;
\r
201 /* The variable this task increments to show it is still running is passed in
\r
202 as the parameter. */
\r
203 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
205 /* Keep performing a calculation and checking the result against a constant. */
\r
212 ff4 = ( ff1 / ff2 ) * ff3;
\r
214 #if configUSE_PREEMPTION == 0
\r
218 /* If the calculation does not match the expected constant, stop the
\r
219 increment of the check variable. */
\r
220 if( fabs( ff4 - fAnswer ) > 0.001F )
\r
225 if( sError == pdFALSE )
\r
227 /* If the calculation has always been correct, increment the check
\r
228 variable so we know
\r
229 this task is still running okay. */
\r
230 ( *pusTaskCheckVariable )++;
\r
233 #if configUSE_PREEMPTION == 0
\r
238 /*-----------------------------------------------------------*/
\r
240 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
242 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
\r
243 volatile unsigned short *pusTaskCheckVariable;
\r
244 const size_t xArraySize = 10;
\r
246 short sError = pdFALSE;
\r
248 /* The variable this task increments to show it is still running is passed in
\r
249 as the parameter. */
\r
250 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
252 pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
\r
254 /* Keep filling an array, keeping a running total of the values placed in the
\r
255 array. Then run through the array adding up all the values. If the two totals
\r
256 do not match, stop the check variable from incrementing. */
\r
262 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
264 pfArray[ xPosition ] = ( portFLOAT ) xPosition + 5.5F;
\r
265 fTotal1 += ( portFLOAT ) xPosition + 5.5F;
\r
268 #if configUSE_PREEMPTION == 0
\r
272 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
274 fTotal2 += pfArray[ xPosition ];
\r
277 fDifference = fTotal1 - fTotal2;
\r
278 if( fabs( fDifference ) > 0.001F )
\r
283 #if configUSE_PREEMPTION == 0
\r
287 if( sError == pdFALSE )
\r
289 /* If the calculation has always been correct, increment the check
\r
290 variable so we know this task is still running okay. */
\r
291 ( *pusTaskCheckVariable )++;
\r
295 /*-----------------------------------------------------------*/
\r
297 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
299 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
\r
300 volatile unsigned short *pusTaskCheckVariable;
\r
301 const size_t xArraySize = 10;
\r
303 short sError = pdFALSE;
\r
305 /* The variable this task increments to show it is still running is passed in
\r
306 as the parameter. */
\r
307 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
309 pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
\r
311 /* Keep filling an array, keeping a running total of the values placed in the
\r
312 array. Then run through the array adding up all the values. If the two totals
\r
313 do not match, stop the check variable from incrementing. */
\r
319 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
321 pfArray[ xPosition ] = ( portFLOAT ) xPosition * 12.123F;
\r
322 fTotal1 += ( portFLOAT ) xPosition * 12.123F;
\r
325 #if configUSE_PREEMPTION == 0
\r
329 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
331 fTotal2 += pfArray[ xPosition ];
\r
334 fDifference = fTotal1 - fTotal2;
\r
335 if( fabs( fDifference ) > 0.001F )
\r
340 #if configUSE_PREEMPTION == 0
\r
344 if( sError == pdFALSE )
\r
346 /* If the calculation has always been correct, increment the check
\r
347 variable so we know this task is still running okay. */
\r
348 ( *pusTaskCheckVariable )++;
\r
352 /*-----------------------------------------------------------*/
\r
354 /* This is called to check that all the created tasks are still running. */
\r
355 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
357 /* Keep a history of the check variables so we know if they have been incremented
\r
358 since the last call. */
\r
359 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
360 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
362 /* Check the maths tasks are still running by ensuring their check variables
\r
363 are still incrementing. */
\r
364 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
366 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
368 /* The check has not incremented so an error exists. */
\r
372 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r