2 * FreeRTOS Kernel V10.1.0
\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 and in so doing test the floating point context switching.
\r
31 * This file also demonstrates the use of the xPortUsesFloatingPoint() function
\r
32 * which informs the kernel that the task requires its floating point context
\r
33 * saved on each switch.
\r
35 * All the tasks run at the idle priority and never block or yield. This causes
\r
36 * all eight tasks to time slice with the idle task. Running at the idle
\r
37 * priority means that these tasks will get pre-empted any time another task is
\r
38 * ready to run or a time slice occurs. More often than not the pre-emption
\r
39 * will occur mid calculation, creating a good test of the schedulers context
\r
40 * switch mechanism - a calculation producing an unexpected result could be a
\r
41 * symptom of a corruption in the context of a task.
\r
47 /* Scheduler include files. */
\r
48 #include "FreeRTOS.h"
\r
51 /* Demo program include files. */
\r
54 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
55 #define mathNUMBER_OF_TASKS ( 8 )
\r
57 /* Four tasks, each of which performs a different floating point calculation.
\r
58 Each of the four is created twice. */
\r
59 static void vCompetingMathTask1( void *pvParameters );
\r
60 static void vCompetingMathTask2( void *pvParameters );
\r
61 static void vCompetingMathTask3( void *pvParameters );
\r
62 static void vCompetingMathTask4( void *pvParameters );
\r
64 /* These variables are used to check that all the tasks are still running. If a
\r
65 task gets a calculation wrong it will stop incrementing its check variable,
\r
66 otherwise the check variable will get incremented on each iteration of the
\r
68 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
70 /*-----------------------------------------------------------*/
\r
72 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
74 TaskHandle_t xCreatedTask;
\r
76 /* Create one of the floating point tasks... */
\r
77 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xCreatedTask );
\r
79 /* ... then enable floating point support for the created task so its flop
\r
80 flop registers are maintained in a consistent state. */
\r
81 xPortUsesFloatingPoint( xCreatedTask );
\r
83 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xCreatedTask );
\r
84 xPortUsesFloatingPoint( xCreatedTask );
\r
86 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xCreatedTask );
\r
87 xPortUsesFloatingPoint( xCreatedTask );
\r
89 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xCreatedTask );
\r
90 xPortUsesFloatingPoint( xCreatedTask );
\r
92 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xCreatedTask );
\r
93 xPortUsesFloatingPoint( xCreatedTask );
\r
95 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xCreatedTask );
\r
96 xPortUsesFloatingPoint( xCreatedTask );
\r
98 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xCreatedTask );
\r
99 xPortUsesFloatingPoint( xCreatedTask );
\r
101 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xCreatedTask );
\r
102 xPortUsesFloatingPoint( xCreatedTask );
\r
104 /*-----------------------------------------------------------*/
\r
106 static void vCompetingMathTask1( void *pvParameters )
\r
108 volatile double d1, d2, d3, d4;
\r
109 volatile unsigned short *pusTaskCheckVariable;
\r
110 volatile double dAnswer;
\r
111 short sError = pdFALSE;
\r
117 /* Calculate the expected answer. */
\r
118 dAnswer = ( d1 + d2 ) * d3;
\r
120 /* The variable this task increments to show it is still running is passed in
\r
121 as the parameter. */
\r
122 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
124 /* Keep performing a calculation and checking the result against a constant. */
\r
127 /* Perform the calculation. */
\r
132 d4 = ( d1 + d2 ) * d3;
\r
134 /* If the calculation does not match the expected constant, stop the
\r
135 increment of the check variable. */
\r
136 if( fabs( d4 - dAnswer ) > 0.001 )
\r
141 if( sError == pdFALSE )
\r
143 /* If the calculation has always been correct, increment the check
\r
144 variable so we know this task is still running okay. */
\r
145 ( *pusTaskCheckVariable )++;
\r
149 /*-----------------------------------------------------------*/
\r
151 static void vCompetingMathTask2( void *pvParameters )
\r
153 volatile double d1, d2, d3, d4;
\r
154 volatile unsigned short *pusTaskCheckVariable;
\r
155 volatile double dAnswer;
\r
156 short sError = pdFALSE;
\r
162 /* Calculate the expected answer. */
\r
163 dAnswer = ( d1 / d2 ) * d3;
\r
166 /* The variable this task increments to show it is still running is passed in
\r
167 as the parameter. */
\r
168 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
170 /* Keep performing a calculation and checking the result against a constant. */
\r
173 /* Perform the calculation. */
\r
178 d4 = ( d1 / d2 ) * d3;
\r
180 /* If the calculation does not match the expected constant, stop the
\r
181 increment of the check variable. */
\r
182 if( fabs( d4 - dAnswer ) > 0.001 )
\r
187 if( sError == pdFALSE )
\r
189 /* If the calculation has always been correct, increment the check
\r
190 variable so we know
\r
191 this task is still running okay. */
\r
192 ( *pusTaskCheckVariable )++;
\r
196 /*-----------------------------------------------------------*/
\r
198 static void vCompetingMathTask3( void *pvParameters )
\r
200 volatile double *pdArray, dTotal1, dTotal2, dDifference;
\r
201 volatile unsigned short *pusTaskCheckVariable;
\r
202 const size_t xArraySize = 10;
\r
204 short sError = pdFALSE;
\r
206 /* The variable this task increments to show it is still running is passed
\r
207 in as the parameter. */
\r
208 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
210 /* Allocate memory for use as an array. */
\r
211 pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );
\r
213 /* Keep filling an array, keeping a running total of the values placed in
\r
214 the array. Then run through the array adding up all the values. If the two
\r
215 totals do not match, stop the check variable from incrementing. */
\r
221 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
223 pdArray[ xPosition ] = ( double ) xPosition + 5.5;
\r
224 dTotal1 += ( double ) xPosition + 5.5;
\r
227 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
229 dTotal2 += pdArray[ xPosition ];
\r
232 dDifference = dTotal1 - dTotal2;
\r
233 if( fabs( dDifference ) > 0.001 )
\r
238 if( sError == pdFALSE )
\r
240 /* If the calculation has always been correct, increment the check
\r
241 variable so we know this task is still running okay. */
\r
242 ( *pusTaskCheckVariable )++;
\r
246 /*-----------------------------------------------------------*/
\r
248 static void vCompetingMathTask4( void *pvParameters )
\r
250 volatile double *pdArray, dTotal1, dTotal2, dDifference;
\r
251 volatile unsigned short *pusTaskCheckVariable;
\r
252 const size_t xArraySize = 10;
\r
254 short sError = pdFALSE;
\r
256 /* The variable this task increments to show it is still running is passed in
\r
257 as the parameter. */
\r
258 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
260 /* Allocate RAM for use as an array. */
\r
261 pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );
\r
263 /* Keep filling an array, keeping a running total of the values placed in the
\r
264 array. Then run through the array adding up all the values. If the two totals
\r
265 do not match, stop the check variable from incrementing. */
\r
271 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
273 pdArray[ xPosition ] = ( double ) xPosition * 12.123;
\r
274 dTotal1 += ( double ) xPosition * 12.123;
\r
277 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
279 dTotal2 += pdArray[ xPosition ];
\r
282 dDifference = dTotal1 - dTotal2;
\r
283 if( fabs( dDifference ) > 0.001 )
\r
288 if( sError == pdFALSE )
\r
290 /* If the calculation has always been correct, increment the check
\r
291 variable so we know this task is still running okay. */
\r
292 ( *pusTaskCheckVariable )++;
\r
296 /*-----------------------------------------------------------*/
\r
298 /* This is called to check that all the created tasks are still running. */
\r
299 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
301 /* Keep a history of the check variables so we know if they have been
\r
302 incremented since the last call. */
\r
303 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
304 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
306 /* Check the maths tasks are still running by ensuring their check variables
\r
307 are still incrementing. */
\r
308 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
310 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
312 /* The check has not incremented so an error exists. */
\r
316 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r