3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
31 + The created tasks now include calls to tskYIELD(), allowing them to be used
32 + with the cooperative scheduler.
36 * Creates eight tasks, each of which loops continuously performing an (emulated)
37 * floating point calculation.
39 * All the tasks run at the idle priority and never block or yield. This causes
40 * all eight tasks to time slice with the idle task. Running at the idle priority
41 * means that these tasks will get pre-empted any time another task is ready to run
42 * or a time slice occurs. More often than not the pre-emption will occur mid
43 * calculation, creating a good test of the schedulers context switch mechanism - a
44 * calculation producing an unexpected result could be a symptom of a corruption in
45 * the context of a task.
55 /* Scheduler include files. */
60 /* Demo program include files. */
63 #define mathSTACK_SIZE ( ( unsigned short ) 512 )
64 #define mathNUMBER_OF_TASKS ( 8 )
66 /* Four tasks, each of which performs a different floating point calculation.
67 * Each of the four is created twice. */
68 static void vCompetingMathTask1( void * pvParameters );
69 static void vCompetingMathTask2( void * pvParameters );
70 static void vCompetingMathTask3( void * pvParameters );
71 static void vCompetingMathTask4( void * pvParameters );
73 /* These variables are used to check that all the tasks are still running. If a
74 * task gets a calculation wrong it will
75 * stop incrementing its check variable. */
76 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
78 /*-----------------------------------------------------------*/
80 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
82 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
83 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
84 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
85 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
86 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
87 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
88 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
89 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
91 /*-----------------------------------------------------------*/
93 static void vCompetingMathTask1( void * pvParameters )
95 portDOUBLE d1, d2, d3, d4;
96 volatile unsigned short * pusTaskCheckVariable;
97 const portDOUBLE dAnswer = ( 123.4567 + 2345.6789 ) * -918.222;
98 const char * const pcTaskStartMsg = "Math task 1 started.\r\n";
99 const char * const pcTaskFailMsg = "Math task 1 failed.\r\n";
100 short sError = pdFALSE;
102 /* Queue a message for printing to say the task has started. */
103 vPrintDisplayMessage( &pcTaskStartMsg );
105 /* The variable this task increments to show it is still running is passed in
106 * as the parameter. */
107 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
109 /* Keep performing a calculation and checking the result against a constant. */
116 d4 = ( d1 + d2 ) * d3;
120 /* If the calculation does not match the expected constant, stop the
121 * increment of the check variable. */
122 if( fabs( d4 - dAnswer ) > 0.001 )
124 vPrintDisplayMessage( &pcTaskFailMsg );
128 if( sError == pdFALSE )
130 /* If the calculation has always been correct, increment the check
131 * variable so we know this task is still running okay. */
132 ( *pusTaskCheckVariable )++;
138 /*-----------------------------------------------------------*/
140 static void vCompetingMathTask2( void * pvParameters )
142 portDOUBLE d1, d2, d3, d4;
143 volatile unsigned short * pusTaskCheckVariable;
144 const portDOUBLE dAnswer = ( -389.38 / 32498.2 ) * -2.0001;
145 const char * const pcTaskStartMsg = "Math task 2 started.\r\n";
146 const char * const pcTaskFailMsg = "Math task 2 failed.\r\n";
147 short sError = pdFALSE;
149 /* Queue a message for printing to say the task has started. */
150 vPrintDisplayMessage( &pcTaskStartMsg );
152 /* The variable this task increments to show it is still running is passed in
153 * as the parameter. */
154 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
156 /* Keep performing a calculation and checking the result against a constant. */
163 d4 = ( d1 / d2 ) * d3;
167 /* If the calculation does not match the expected constant, stop the
168 * increment of the check variable. */
169 if( fabs( d4 - dAnswer ) > 0.001 )
171 vPrintDisplayMessage( &pcTaskFailMsg );
175 if( sError == pdFALSE )
177 /* If the calculation has always been correct, increment the check
178 * variable so we know
179 * this task is still running okay. */
180 ( *pusTaskCheckVariable )++;
186 /*-----------------------------------------------------------*/
188 static void vCompetingMathTask3( void * pvParameters )
190 portDOUBLE * pdArray, dTotal1, dTotal2, dDifference;
191 volatile unsigned short * pusTaskCheckVariable;
192 const unsigned short usArraySize = 250;
193 unsigned short usPosition;
194 const char * const pcTaskStartMsg = "Math task 3 started.\r\n";
195 const char * const pcTaskFailMsg = "Math task 3 failed.\r\n";
196 short sError = pdFALSE;
198 /* Queue a message for printing to say the task has started. */
199 vPrintDisplayMessage( &pcTaskStartMsg );
201 /* The variable this task increments to show it is still running is passed in
202 * as the parameter. */
203 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
205 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
207 /* Keep filling an array, keeping a running total of the values placed in the
208 * array. Then run through the array adding up all the values. If the two totals
209 * do not match, stop the check variable from incrementing. */
215 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
217 pdArray[ usPosition ] = ( portDOUBLE ) usPosition + 5.5;
218 dTotal1 += ( portDOUBLE ) usPosition + 5.5;
223 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
225 dTotal2 += pdArray[ usPosition ];
228 dDifference = dTotal1 - dTotal2;
230 if( fabs( dDifference ) > 0.001 )
232 vPrintDisplayMessage( &pcTaskFailMsg );
238 if( sError == pdFALSE )
240 /* If the calculation has always been correct, increment the check
241 * variable so we know this task is still running okay. */
242 ( *pusTaskCheckVariable )++;
246 /*-----------------------------------------------------------*/
248 static void vCompetingMathTask4( void * pvParameters )
250 portDOUBLE * pdArray, dTotal1, dTotal2, dDifference;
251 volatile unsigned short * pusTaskCheckVariable;
252 const unsigned short usArraySize = 250;
253 unsigned short usPosition;
254 const char * const pcTaskStartMsg = "Math task 4 started.\r\n";
255 const char * const pcTaskFailMsg = "Math task 4 failed.\r\n";
256 short sError = pdFALSE;
258 /* Queue a message for printing to say the task has started. */
259 vPrintDisplayMessage( &pcTaskStartMsg );
261 /* The variable this task increments to show it is still running is passed in
262 * as the parameter. */
263 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
265 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
267 /* Keep filling an array, keeping a running total of the values placed in the
268 * array. Then run through the array adding up all the values. If the two totals
269 * do not match, stop the check variable from incrementing. */
275 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
277 pdArray[ usPosition ] = ( portDOUBLE ) usPosition * 12.123;
278 dTotal1 += ( portDOUBLE ) usPosition * 12.123;
283 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
285 dTotal2 += pdArray[ usPosition ];
288 dDifference = dTotal1 - dTotal2;
290 if( fabs( dDifference ) > 0.001 )
292 vPrintDisplayMessage( &pcTaskFailMsg );
298 if( sError == pdFALSE )
300 /* If the calculation has always been correct, increment the check
301 * variable so we know this task is still running okay. */
302 ( *pusTaskCheckVariable )++;
306 /*-----------------------------------------------------------*/
308 /* This is called to check that all the created tasks are still running. */
309 portBASE_TYPE xAreMathsTaskStillRunning( void )
311 /* Keep a history of the check variables so we know if they have been incremented
312 * since the last call. */
313 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
314 portBASE_TYPE xReturn = pdTRUE, xTask;
316 /* Check the maths tasks are still running by ensuring their check variables
317 * are still incrementing. */
318 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
320 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
322 /* The check has not incremented so an error exists. */
326 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];