2 * FreeRTOS Kernel V10.2.1
3 * Copyright (C) 2019 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
29 * Creates eight tasks, each of which loops continuously performing a
30 * floating point calculation.
32 * All the tasks run at the idle priority and never block or yield. This causes
33 * all eight tasks to time slice with the idle task. Running at the idle priority
34 * means that these tasks will get pre-empted any time another task is ready to run
35 * or a time slice occurs. More often than not the pre-emption will occur mid
36 * calculation, creating a good test of the schedulers context switch mechanism - a
37 * calculation producing an unexpected result could be a symptom of a corruption in
38 * the context of a task.
44 /* Scheduler include files. */
48 /* Demo program include files. */
51 #define mathSTACK_SIZE (configMINIMAL_STACK_SIZE + 100)
52 #define mathNUMBER_OF_TASKS ( 8 )
54 /* Four tasks, each of which performs a different floating point calculation.
55 Each of the four is created twice. */
56 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
57 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
58 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
59 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
61 /* These variables are used to check that all the tasks are still running. If a
62 task gets a calculation wrong it will
63 stop incrementing its check variable. */
64 static volatile unsigned long ulTaskCheck[ mathNUMBER_OF_TASKS ] = { 0 };
66 /*-----------------------------------------------------------*/
68 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
70 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 0 ] ), uxPriority, NULL );
71 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 1 ] ), uxPriority, NULL );
72 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 2 ] ), uxPriority, NULL );
73 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 3 ] ), uxPriority, NULL );
74 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 4 ] ), uxPriority, NULL );
75 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 5 ] ), uxPriority, NULL );
76 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 6 ] ), uxPriority, NULL );
77 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 7 ] ), uxPriority, NULL );
79 /*-----------------------------------------------------------*/
81 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
83 volatile portDOUBLE d1, d2, d3, d4;
84 volatile unsigned long *pulTaskCheckVariable;
85 volatile portDOUBLE dAnswer;
86 short sError = pdFALSE;
89 /* Must be called before any hardware floating point operations are
90 performed to let the RTOS portable layer know that this task requires
91 a floating point context. */
92 portTASK_USES_FLOATING_POINT();
98 dAnswer = ( d1 + d2 ) * d3;
100 /* The variable this task increments to show it is still running is passed in
102 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
104 /* Keep performing a calculation and checking the result against a constant. */
111 d4 = ( d1 + d2 ) * d3;
113 #if configUSE_PREEMPTION == 0
117 /* If the calculation does not match the expected constant, stop the
118 increment of the check variable. */
119 if( fabs( d4 - dAnswer ) > 0.001 )
124 if( sError == pdFALSE )
126 /* If the calculation has always been correct, increment the check
127 variable so we know this task is still running okay. */
128 ( *pulTaskCheckVariable )++;
131 #if configUSE_PREEMPTION == 0
137 /*-----------------------------------------------------------*/
139 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
141 volatile portDOUBLE d1, d2, d3, d4;
142 volatile unsigned long *pulTaskCheckVariable;
143 volatile portDOUBLE dAnswer;
144 short sError = pdFALSE;
146 /* Must be called before any hardware floating point operations are
147 performed to let the RTOS portable layer know that this task requires
148 a floating point context. */
149 portTASK_USES_FLOATING_POINT();
155 dAnswer = ( d1 / d2 ) * d3;
158 /* The variable this task increments to show it is still running is passed in
160 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
162 /* Keep performing a calculation and checking the result against a constant. */
169 d4 = ( d1 / d2 ) * d3;
171 #if configUSE_PREEMPTION == 0
175 /* If the calculation does not match the expected constant, stop the
176 increment of the check variable. */
177 if( fabs( d4 - dAnswer ) > 0.001 )
182 if( sError == pdFALSE )
184 /* If the calculation has always been correct, increment the check
186 this task is still running okay. */
187 ( *pulTaskCheckVariable )++;
190 #if configUSE_PREEMPTION == 0
195 /*-----------------------------------------------------------*/
197 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
199 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
200 volatile unsigned long *pulTaskCheckVariable;
201 const size_t xArraySize = 10;
203 short sError = pdFALSE;
205 /* Must be called before any hardware floating point operations are
206 performed to let the RTOS portable layer know that this task requires
207 a floating point context. */
208 portTASK_USES_FLOATING_POINT();
210 /* The variable this task increments to show it is still running is passed in
212 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
214 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
216 /* Keep filling an array, keeping a running total of the values placed in the
217 array. Then run through the array adding up all the values. If the two totals
218 do not match, stop the check variable from incrementing. */
224 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
226 pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;
227 dTotal1 += ( portDOUBLE ) xPosition + 5.5;
230 #if configUSE_PREEMPTION == 0
234 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
236 dTotal2 += pdArray[ xPosition ];
239 dDifference = dTotal1 - dTotal2;
240 if( fabs( dDifference ) > 0.001 )
245 #if configUSE_PREEMPTION == 0
249 if( sError == pdFALSE )
251 /* If the calculation has always been correct, increment the check
252 variable so we know this task is still running okay. */
253 ( *pulTaskCheckVariable )++;
257 /*-----------------------------------------------------------*/
259 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
261 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
262 volatile unsigned long *pulTaskCheckVariable;
263 const size_t xArraySize = 10;
265 short sError = pdFALSE;
267 /* Must be called before any hardware floating point operations are
268 performed to let the RTOS portable layer know that this task requires
269 a floating point context. */
270 portTASK_USES_FLOATING_POINT();
272 /* The variable this task increments to show it is still running is passed in
274 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
276 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
278 /* Keep filling an array, keeping a running total of the values placed in the
279 array. Then run through the array adding up all the values. If the two totals
280 do not match, stop the check variable from incrementing. */
286 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
288 pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;
289 dTotal1 += ( portDOUBLE ) xPosition * 12.123;
292 #if configUSE_PREEMPTION == 0
296 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
298 dTotal2 += pdArray[ xPosition ];
301 dDifference = dTotal1 - dTotal2;
302 if( fabs( dDifference ) > 0.001 )
307 #if configUSE_PREEMPTION == 0
311 if( sError == pdFALSE )
313 /* If the calculation has always been correct, increment the check
314 variable so we know this task is still running okay. */
315 ( *pulTaskCheckVariable )++;
319 /*-----------------------------------------------------------*/
321 /* This is called to check that all the created tasks are still running. */
322 portBASE_TYPE xAreMathsTaskStillRunning( void )
324 /* Keep a history of the check variables so we know if they have been incremented
325 since the last call. */
326 static unsigned long ulLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
327 portBASE_TYPE xReturn = pdTRUE, xTask;
329 /* Check the maths tasks are still running by ensuring their check variables
330 are still incrementing. */
331 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
333 if( ulTaskCheck[ xTask ] == ulLastTaskCheck[ xTask ] )
335 /* The check has not incremented so an error exists. */
339 ulLastTaskCheck[ xTask ] = ulTaskCheck[ xTask ];