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 floating
\r
31 * point calculation - using single precision variables.
\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
45 /* Scheduler include files. */
\r
46 #include "FreeRTOS.h"
\r
49 /* Demo program include files. */
\r
52 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
53 #define mathNUMBER_OF_TASKS ( 8 )
\r
55 /* Four tasks, each of which performs a different floating point calculation.
\r
56 Each of the four is created twice. */
\r
57 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
58 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
59 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
60 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
62 /* These variables are used to check that all the tasks are still running. If a
\r
63 task gets a calculation wrong it will
\r
64 stop incrementing its check variable. */
\r
65 static volatile uint16_t usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( uint16_t ) 0 };
\r
67 /*-----------------------------------------------------------*/
\r
69 void vStartMathTasks( UBaseType_t uxPriority )
\r
71 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
72 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
73 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
74 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
75 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
76 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
77 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
78 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
80 /*-----------------------------------------------------------*/
\r
82 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
84 volatile float f1, f2, f3, f4;
\r
85 volatile uint16_t *pusTaskCheckVariable;
\r
86 volatile float fAnswer;
\r
87 short sError = pdFALSE;
\r
93 fAnswer = ( f1 + f2 ) * f3;
\r
95 /* The variable this task increments to show it is still running is passed in
\r
96 as the parameter. */
\r
97 pusTaskCheckVariable = ( uint16_t * ) pvParameters;
\r
99 /* Keep performing a calculation and checking the result against a constant. */
\r
106 f4 = ( f1 + f2 ) * f3;
\r
108 #if configUSE_PREEMPTION == 0
\r
112 /* If the calculation does not match the expected constant, stop the
\r
113 increment of the check variable. */
\r
114 if( fabs( f4 - fAnswer ) > 0.001F )
\r
119 if( sError == pdFALSE )
\r
121 /* If the calculation has always been correct, increment the check
\r
122 variable so we know this task is still running okay. */
\r
123 ( *pusTaskCheckVariable )++;
\r
126 #if configUSE_PREEMPTION == 0
\r
132 /*-----------------------------------------------------------*/
\r
134 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
136 volatile float f1, f2, f3, f4;
\r
137 volatile uint16_t *pusTaskCheckVariable;
\r
138 volatile float fAnswer;
\r
139 short sError = pdFALSE;
\r
145 fAnswer = ( f1 / f2 ) * f3;
\r
148 /* The variable this task increments to show it is still running is passed in
\r
149 as the parameter. */
\r
150 pusTaskCheckVariable = ( uint16_t * ) pvParameters;
\r
152 /* Keep performing a calculation and checking the result against a constant. */
\r
159 f4 = ( f1 / f2 ) * f3;
\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( f4 - 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
\r
176 this task is still running okay. */
\r
177 ( *pusTaskCheckVariable )++;
\r
180 #if configUSE_PREEMPTION == 0
\r
185 /*-----------------------------------------------------------*/
\r
187 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
189 volatile float *pfArray, fTotal1, fTotal2, fDifference, fPosition;
\r
190 volatile uint16_t *pusTaskCheckVariable;
\r
191 const size_t xArraySize = 10;
\r
193 short sError = pdFALSE;
\r
195 /* The variable this task increments to show it is still running is passed in
\r
196 as the parameter. */
\r
197 pusTaskCheckVariable = ( uint16_t * ) pvParameters;
\r
199 pfArray = ( float * ) pvPortMalloc( xArraySize * sizeof( float ) );
\r
201 /* Keep filling an array, keeping a running total of the values placed in the
\r
202 array. Then run through the array adding up all the values. If the two totals
\r
203 do not match, stop the check variable from incrementing. */
\r
210 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
212 pfArray[ xPosition ] = fPosition + 5.5F;
\r
213 fTotal1 += fPosition + 5.5F;
\r
216 #if configUSE_PREEMPTION == 0
\r
220 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
222 fTotal2 += pfArray[ xPosition ];
\r
225 fDifference = fTotal1 - fTotal2;
\r
226 if( fabs( fDifference ) > 0.001F )
\r
231 #if configUSE_PREEMPTION == 0
\r
235 if( sError == pdFALSE )
\r
237 /* If the calculation has always been correct, increment the check
\r
238 variable so we know this task is still running okay. */
\r
239 ( *pusTaskCheckVariable )++;
\r
243 /*-----------------------------------------------------------*/
\r
245 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
247 volatile float *pfArray, fTotal1, fTotal2, fDifference, fPosition;
\r
248 volatile uint16_t *pusTaskCheckVariable;
\r
249 const size_t xArraySize = 10;
\r
251 short sError = pdFALSE;
\r
253 /* The variable this task increments to show it is still running is passed in
\r
254 as the parameter. */
\r
255 pusTaskCheckVariable = ( uint16_t * ) pvParameters;
\r
257 pfArray = ( float * ) pvPortMalloc( xArraySize * sizeof( float ) );
\r
259 /* Keep filling an array, keeping a running total of the values placed in the
\r
260 array. Then run through the array adding up all the values. If the two totals
\r
261 do not match, stop the check variable from incrementing. */
\r
268 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
270 pfArray[ xPosition ] = fPosition * 12.123F;
\r
271 fTotal1 += fPosition * 12.123F;
\r
274 #if configUSE_PREEMPTION == 0
\r
278 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
280 fTotal2 += pfArray[ xPosition ];
\r
283 fDifference = fTotal1 - fTotal2;
\r
284 if( fabs( fDifference ) > 0.001F )
\r
289 #if configUSE_PREEMPTION == 0
\r
293 if( sError == pdFALSE )
\r
295 /* If the calculation has always been correct, increment the check
\r
296 variable so we know this task is still running okay. */
\r
297 ( *pusTaskCheckVariable )++;
\r
301 /*-----------------------------------------------------------*/
\r
303 /* This is called to check that all the created tasks are still running. */
\r
304 BaseType_t xAreMathsTaskStillRunning( void )
\r
306 /* Keep a history of the check variables so we know if they have been incremented
\r
307 since the last call. */
\r
308 static uint16_t usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( uint16_t ) 0 };
\r
309 BaseType_t xReturn = pdTRUE, xTask;
\r
311 /* Check the maths tasks are still running by ensuring their check variables
\r
312 are still incrementing. */
\r
313 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
315 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
317 /* The check has not incremented so an error exists. */
\r
321 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r