2 * FreeRTOS Kernel V10.1.1
\r
3 * Copyright (C) 2018 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 floating
\r
30 * point calculation - using single precision variables.
\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
44 /* Scheduler include files. */
\r
45 #include "FreeRTOS.h"
\r
48 /* Demo program include files. */
\r
51 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
52 #define mathNUMBER_OF_TASKS ( 8 )
\r
54 /* Four tasks, each of which performs a different floating point calculation.
\r
55 Each of the four is created twice. */
\r
56 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
57 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
58 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
59 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
61 /* These variables are used to check that all the tasks are still running. If a
\r
62 task gets a calculation wrong it will
\r
63 stop incrementing its check variable. */
\r
64 static volatile uint16_t usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( uint16_t ) 0 };
\r
66 /*-----------------------------------------------------------*/
\r
68 void vStartMathTasks( UBaseType_t uxPriority )
\r
70 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
71 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
72 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
73 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
74 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
75 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
76 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
77 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
79 /*-----------------------------------------------------------*/
\r
81 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
83 volatile float f1, f2, f3, f4;
\r
84 volatile uint16_t *pusTaskCheckVariable;
\r
85 volatile float fAnswer;
\r
86 short sError = pdFALSE;
\r
92 fAnswer = ( f1 + f2 ) * f3;
\r
94 /* The variable this task increments to show it is still running is passed in
\r
95 as the parameter. */
\r
96 pusTaskCheckVariable = ( uint16_t * ) pvParameters;
\r
98 /* Keep performing a calculation and checking the result against a constant. */
\r
105 f4 = ( f1 + f2 ) * f3;
\r
107 #if configUSE_PREEMPTION == 0
\r
111 /* If the calculation does not match the expected constant, stop the
\r
112 increment of the check variable. */
\r
113 if( fabs( f4 - fAnswer ) > 0.001F )
\r
118 if( sError == pdFALSE )
\r
120 /* If the calculation has always been correct, increment the check
\r
121 variable so we know this task is still running okay. */
\r
122 ( *pusTaskCheckVariable )++;
\r
125 #if configUSE_PREEMPTION == 0
\r
131 /*-----------------------------------------------------------*/
\r
133 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
135 volatile float f1, f2, f3, f4;
\r
136 volatile uint16_t *pusTaskCheckVariable;
\r
137 volatile float fAnswer;
\r
138 short sError = pdFALSE;
\r
144 fAnswer = ( f1 / f2 ) * f3;
\r
147 /* The variable this task increments to show it is still running is passed in
\r
148 as the parameter. */
\r
149 pusTaskCheckVariable = ( uint16_t * ) pvParameters;
\r
151 /* Keep performing a calculation and checking the result against a constant. */
\r
158 f4 = ( f1 / f2 ) * f3;
\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( f4 - fAnswer ) > 0.001F )
\r
171 if( sError == pdFALSE )
\r
173 /* If the calculation has always been correct, increment the check
\r
174 variable so we know
\r
175 this task is still running okay. */
\r
176 ( *pusTaskCheckVariable )++;
\r
179 #if configUSE_PREEMPTION == 0
\r
184 /*-----------------------------------------------------------*/
\r
186 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
188 volatile float *pfArray, fTotal1, fTotal2, fDifference, fPosition;
\r
189 volatile uint16_t *pusTaskCheckVariable;
\r
190 const size_t xArraySize = 10;
\r
192 short sError = pdFALSE;
\r
194 /* The variable this task increments to show it is still running is passed in
\r
195 as the parameter. */
\r
196 pusTaskCheckVariable = ( uint16_t * ) pvParameters;
\r
198 pfArray = ( float * ) pvPortMalloc( xArraySize * sizeof( float ) );
\r
200 /* Keep filling an array, keeping a running total of the values placed in the
\r
201 array. Then run through the array adding up all the values. If the two totals
\r
202 do not match, stop the check variable from incrementing. */
\r
209 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
211 pfArray[ xPosition ] = fPosition + 5.5F;
\r
212 fTotal1 += fPosition + 5.5F;
\r
215 #if configUSE_PREEMPTION == 0
\r
219 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
221 fTotal2 += pfArray[ xPosition ];
\r
224 fDifference = fTotal1 - fTotal2;
\r
225 if( fabs( fDifference ) > 0.001F )
\r
230 #if configUSE_PREEMPTION == 0
\r
234 if( sError == pdFALSE )
\r
236 /* If the calculation has always been correct, increment the check
\r
237 variable so we know this task is still running okay. */
\r
238 ( *pusTaskCheckVariable )++;
\r
242 /*-----------------------------------------------------------*/
\r
244 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
246 volatile float *pfArray, fTotal1, fTotal2, fDifference, fPosition;
\r
247 volatile uint16_t *pusTaskCheckVariable;
\r
248 const size_t xArraySize = 10;
\r
250 short sError = pdFALSE;
\r
252 /* The variable this task increments to show it is still running is passed in
\r
253 as the parameter. */
\r
254 pusTaskCheckVariable = ( uint16_t * ) pvParameters;
\r
256 pfArray = ( float * ) pvPortMalloc( xArraySize * sizeof( float ) );
\r
258 /* Keep filling an array, keeping a running total of the values placed in the
\r
259 array. Then run through the array adding up all the values. If the two totals
\r
260 do not match, stop the check variable from incrementing. */
\r
267 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
269 pfArray[ xPosition ] = fPosition * 12.123F;
\r
270 fTotal1 += fPosition * 12.123F;
\r
273 #if configUSE_PREEMPTION == 0
\r
277 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
279 fTotal2 += pfArray[ xPosition ];
\r
282 fDifference = fTotal1 - fTotal2;
\r
283 if( fabs( fDifference ) > 0.001F )
\r
288 #if configUSE_PREEMPTION == 0
\r
292 if( sError == pdFALSE )
\r
294 /* If the calculation has always been correct, increment the check
\r
295 variable so we know this task is still running okay. */
\r
296 ( *pusTaskCheckVariable )++;
\r
300 /*-----------------------------------------------------------*/
\r
302 /* This is called to check that all the created tasks are still running. */
\r
303 BaseType_t xAreMathsTaskStillRunning( void )
\r
305 /* Keep a history of the check variables so we know if they have been incremented
\r
306 since the last call. */
\r
307 static uint16_t usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( uint16_t ) 0 };
\r
308 BaseType_t xReturn = pdTRUE, xTask;
\r
310 /* Check the maths tasks are still running by ensuring their check variables
\r
311 are still incrementing. */
\r
312 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
314 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
316 /* The check has not incremented so an error exists. */
\r
320 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r