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 and in so doing test the floating point context switching.
\r
32 * This file also demonstrates the use of the xPortUsesFloatingPoint() function
\r
33 * which informs the kernel that the task requires its floating point context
\r
34 * saved on each switch.
\r
36 * All the tasks run at the idle priority and never block or yield. This causes
\r
37 * all eight tasks to time slice with the idle task. Running at the idle
\r
38 * priority means that these tasks will get pre-empted any time another task is
\r
39 * ready to run or a time slice occurs. More often than not the pre-emption
\r
40 * will occur mid calculation, creating a good test of the schedulers context
\r
41 * switch mechanism - a calculation producing an unexpected result could be a
\r
42 * symptom of a corruption in the context of a task.
\r
48 /* Scheduler include files. */
\r
49 #include "FreeRTOS.h"
\r
52 /* Demo program include files. */
\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 void vCompetingMathTask1( void *pvParameters );
\r
61 static void vCompetingMathTask2( void *pvParameters );
\r
62 static void vCompetingMathTask3( void *pvParameters );
\r
63 static void vCompetingMathTask4( void *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 otherwise the check variable will get incremented on each iteration of the
\r
69 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
71 /*-----------------------------------------------------------*/
\r
73 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
75 TaskHandle_t xCreatedTask;
\r
77 /* Create one of the floating point tasks... */
\r
78 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xCreatedTask );
\r
80 /* ... then enable floating point support for the created task so its flop
\r
81 flop registers are maintained in a consistent state. */
\r
82 xPortUsesFloatingPoint( xCreatedTask );
\r
84 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xCreatedTask );
\r
85 xPortUsesFloatingPoint( xCreatedTask );
\r
87 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xCreatedTask );
\r
88 xPortUsesFloatingPoint( xCreatedTask );
\r
90 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xCreatedTask );
\r
91 xPortUsesFloatingPoint( xCreatedTask );
\r
93 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xCreatedTask );
\r
94 xPortUsesFloatingPoint( xCreatedTask );
\r
96 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xCreatedTask );
\r
97 xPortUsesFloatingPoint( xCreatedTask );
\r
99 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xCreatedTask );
\r
100 xPortUsesFloatingPoint( xCreatedTask );
\r
102 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xCreatedTask );
\r
103 xPortUsesFloatingPoint( xCreatedTask );
\r
105 /*-----------------------------------------------------------*/
\r
107 static void vCompetingMathTask1( void *pvParameters )
\r
109 volatile double d1, d2, d3, d4;
\r
110 volatile unsigned short *pusTaskCheckVariable;
\r
111 volatile double dAnswer;
\r
112 short sError = pdFALSE;
\r
118 /* Calculate the expected answer. */
\r
119 dAnswer = ( d1 + d2 ) * d3;
\r
121 /* The variable this task increments to show it is still running is passed in
\r
122 as the parameter. */
\r
123 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
125 /* Keep performing a calculation and checking the result against a constant. */
\r
128 /* Perform the calculation. */
\r
133 d4 = ( d1 + d2 ) * d3;
\r
135 /* If the calculation does not match the expected constant, stop the
\r
136 increment of the check variable. */
\r
137 if( fabs( d4 - dAnswer ) > 0.001 )
\r
142 if( sError == pdFALSE )
\r
144 /* If the calculation has always been correct, increment the check
\r
145 variable so we know this task is still running okay. */
\r
146 ( *pusTaskCheckVariable )++;
\r
150 /*-----------------------------------------------------------*/
\r
152 static void vCompetingMathTask2( void *pvParameters )
\r
154 volatile double d1, d2, d3, d4;
\r
155 volatile unsigned short *pusTaskCheckVariable;
\r
156 volatile double dAnswer;
\r
157 short sError = pdFALSE;
\r
163 /* Calculate the expected answer. */
\r
164 dAnswer = ( d1 / d2 ) * d3;
\r
167 /* The variable this task increments to show it is still running is passed in
\r
168 as the parameter. */
\r
169 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
171 /* Keep performing a calculation and checking the result against a constant. */
\r
174 /* Perform the calculation. */
\r
179 d4 = ( d1 / d2 ) * d3;
\r
181 /* If the calculation does not match the expected constant, stop the
\r
182 increment of the check variable. */
\r
183 if( fabs( d4 - dAnswer ) > 0.001 )
\r
188 if( sError == pdFALSE )
\r
190 /* If the calculation has always been correct, increment the check
\r
191 variable so we know
\r
192 this task is still running okay. */
\r
193 ( *pusTaskCheckVariable )++;
\r
197 /*-----------------------------------------------------------*/
\r
199 static void vCompetingMathTask3( void *pvParameters )
\r
201 volatile double *pdArray, dTotal1, dTotal2, dDifference;
\r
202 volatile unsigned short *pusTaskCheckVariable;
\r
203 const size_t xArraySize = 10;
\r
205 short sError = pdFALSE;
\r
207 /* The variable this task increments to show it is still running is passed
\r
208 in as the parameter. */
\r
209 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
211 /* Allocate memory for use as an array. */
\r
212 pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );
\r
214 /* Keep filling an array, keeping a running total of the values placed in
\r
215 the array. Then run through the array adding up all the values. If the two
\r
216 totals do not match, stop the check variable from incrementing. */
\r
222 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
224 pdArray[ xPosition ] = ( double ) xPosition + 5.5;
\r
225 dTotal1 += ( double ) xPosition + 5.5;
\r
228 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
230 dTotal2 += pdArray[ xPosition ];
\r
233 dDifference = dTotal1 - dTotal2;
\r
234 if( fabs( dDifference ) > 0.001 )
\r
239 if( sError == pdFALSE )
\r
241 /* If the calculation has always been correct, increment the check
\r
242 variable so we know this task is still running okay. */
\r
243 ( *pusTaskCheckVariable )++;
\r
247 /*-----------------------------------------------------------*/
\r
249 static void vCompetingMathTask4( void *pvParameters )
\r
251 volatile double *pdArray, dTotal1, dTotal2, dDifference;
\r
252 volatile unsigned short *pusTaskCheckVariable;
\r
253 const size_t xArraySize = 10;
\r
255 short sError = pdFALSE;
\r
257 /* The variable this task increments to show it is still running is passed in
\r
258 as the parameter. */
\r
259 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
261 /* Allocate RAM for use as an array. */
\r
262 pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );
\r
264 /* Keep filling an array, keeping a running total of the values placed in the
\r
265 array. Then run through the array adding up all the values. If the two totals
\r
266 do not match, stop the check variable from incrementing. */
\r
272 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
274 pdArray[ xPosition ] = ( double ) xPosition * 12.123;
\r
275 dTotal1 += ( double ) xPosition * 12.123;
\r
278 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
280 dTotal2 += pdArray[ xPosition ];
\r
283 dDifference = dTotal1 - dTotal2;
\r
284 if( fabs( dDifference ) > 0.001 )
\r
289 if( sError == pdFALSE )
\r
291 /* If the calculation has always been correct, increment the check
\r
292 variable so we know this task is still running okay. */
\r
293 ( *pusTaskCheckVariable )++;
\r
297 /*-----------------------------------------------------------*/
\r
299 /* This is called to check that all the created tasks are still running. */
\r
300 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
302 /* Keep a history of the check variables so we know if they have been
\r
303 incremented since the last call. */
\r
304 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
305 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
307 /* Check the maths tasks are still running by ensuring their check variables
\r
308 are still incrementing. */
\r
309 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
311 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
313 /* The check has not incremented so an error exists. */
\r
317 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r