2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 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 an (emulated)
\r
30 * floating point calculation.
\r
32 * All the tasks run at the idle priority and never block or yield. This causes
\r
33 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
34 * means that these tasks will get pre-empted any time another task is ready to run
\r
35 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
36 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
37 * calculation producing an unexpected result could be a symptom of a corruption in
\r
38 * the context of a task.
\r
44 /* Scheduler include files. */
\r
45 #include "FreeRTOS.h"
\r
48 /* Demo program include files. */
\r
51 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
52 #define mathNUMBER_OF_TASKS ( 8 )
\r
54 /* Four tasks, each of which performs a different floating point calculation.
\r
55 Each of the four is created twice. */
\r
56 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
57 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
58 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
59 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
61 /* These variables are used to check that all the tasks are still running. If a
\r
62 task gets a calculation wrong it will
\r
63 stop incrementing its check variable. */
\r
64 static volatile unsigned long ulTaskCheck[ mathNUMBER_OF_TASKS ] = { 0 };
\r
66 /* Must be called before any hardware floating point operations are
\r
67 performed to let the RTOS portable layer know that this task requires
\r
68 a floating point context. */
\r
69 #if __TI_VFP_SUPPORT__
\r
70 extern void vPortTaskUsesFPU( void );
\r
73 /*-----------------------------------------------------------*/
\r
75 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
77 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 0 ] ), uxPriority, NULL );
\r
78 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 1 ] ), uxPriority, NULL );
\r
79 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 2 ] ), uxPriority, NULL );
\r
80 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 3 ] ), uxPriority, NULL );
\r
81 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 4 ] ), uxPriority, NULL );
\r
82 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 5 ] ), uxPriority, NULL );
\r
83 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 6 ] ), uxPriority, NULL );
\r
84 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 7 ] ), uxPriority, NULL );
\r
86 /*-----------------------------------------------------------*/
\r
88 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
90 volatile portDOUBLE d1, d2, d3, d4;
\r
91 volatile unsigned long *pulTaskCheckVariable;
\r
92 volatile portDOUBLE dAnswer;
\r
93 short sError = pdFALSE;
\r
96 /* Must be called before any hardware floating point operations are
\r
97 performed to let the RTOS portable layer know that this task requires
\r
98 a floating point context. */
\r
99 #if __TI_VFP_SUPPORT__
\r
100 vPortTaskUsesFPU();
\r
107 dAnswer = ( d1 + d2 ) * d3;
\r
109 /* The variable this task increments to show it is still running is passed in
\r
110 as the parameter. */
\r
111 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
\r
113 /* Keep performing a calculation and checking the result against a constant. */
\r
120 d4 = ( d1 + d2 ) * d3;
\r
122 #if configUSE_PREEMPTION == 0
\r
126 /* If the calculation does not match the expected constant, stop the
\r
127 increment of the check variable. */
\r
128 if( fabs( d4 - dAnswer ) > 0.001 )
\r
133 if( sError == pdFALSE )
\r
135 /* If the calculation has always been correct, increment the check
\r
136 variable so we know this task is still running okay. */
\r
137 ( *pulTaskCheckVariable )++;
\r
140 #if configUSE_PREEMPTION == 0
\r
146 /*-----------------------------------------------------------*/
\r
148 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
150 volatile portDOUBLE d1, d2, d3, d4;
\r
151 volatile unsigned long *pulTaskCheckVariable;
\r
152 volatile portDOUBLE dAnswer;
\r
153 short sError = pdFALSE;
\r
155 /* Must be called before any hardware floating point operations are
\r
156 performed to let the RTOS portable layer know that this task requires
\r
157 a floating point context. */
\r
158 #if __TI_VFP_SUPPORT__
\r
159 vPortTaskUsesFPU();
\r
166 dAnswer = ( d1 / d2 ) * d3;
\r
169 /* The variable this task increments to show it is still running is passed in
\r
170 as the parameter. */
\r
171 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
\r
173 /* Keep performing a calculation and checking the result against a constant. */
\r
180 d4 = ( d1 / d2 ) * d3;
\r
182 #if configUSE_PREEMPTION == 0
\r
186 /* If the calculation does not match the expected constant, stop the
\r
187 increment of the check variable. */
\r
188 if( fabs( d4 - dAnswer ) > 0.001 )
\r
193 if( sError == pdFALSE )
\r
195 /* If the calculation has always been correct, increment the check
\r
196 variable so we know
\r
197 this task is still running okay. */
\r
198 ( *pulTaskCheckVariable )++;
\r
201 #if configUSE_PREEMPTION == 0
\r
206 /*-----------------------------------------------------------*/
\r
208 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
210 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
211 volatile unsigned long *pulTaskCheckVariable;
\r
212 const size_t xArraySize = 10;
\r
214 short sError = pdFALSE;
\r
216 /* Must be called before any hardware floating point operations are
\r
217 performed to let the RTOS portable layer know that this task requires
\r
218 a floating point context. */
\r
219 #if __TI_VFP_SUPPORT__
\r
220 vPortTaskUsesFPU();
\r
223 /* The variable this task increments to show it is still running is passed in
\r
224 as the parameter. */
\r
225 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
\r
227 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
229 /* Keep filling an array, keeping a running total of the values placed in the
\r
230 array. Then run through the array adding up all the values. If the two totals
\r
231 do not match, stop the check variable from incrementing. */
\r
237 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
239 pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;
\r
240 dTotal1 += ( portDOUBLE ) xPosition + 5.5;
\r
243 #if configUSE_PREEMPTION == 0
\r
247 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
249 dTotal2 += pdArray[ xPosition ];
\r
252 dDifference = dTotal1 - dTotal2;
\r
253 if( fabs( dDifference ) > 0.001 )
\r
258 #if configUSE_PREEMPTION == 0
\r
262 if( sError == pdFALSE )
\r
264 /* If the calculation has always been correct, increment the check
\r
265 variable so we know this task is still running okay. */
\r
266 ( *pulTaskCheckVariable )++;
\r
270 /*-----------------------------------------------------------*/
\r
272 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
274 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
275 volatile unsigned long *pulTaskCheckVariable;
\r
276 const size_t xArraySize = 10;
\r
278 short sError = pdFALSE;
\r
280 /* Must be called before any hardware floating point operations are
\r
281 performed to let the RTOS portable layer know that this task requires
\r
282 a floating point context. */
\r
283 #if __TI_VFP_SUPPORT__
\r
284 vPortTaskUsesFPU();
\r
287 /* The variable this task increments to show it is still running is passed in
\r
288 as the parameter. */
\r
289 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
\r
291 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
293 /* Keep filling an array, keeping a running total of the values placed in the
\r
294 array. Then run through the array adding up all the values. If the two totals
\r
295 do not match, stop the check variable from incrementing. */
\r
301 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
303 pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;
\r
304 dTotal1 += ( portDOUBLE ) xPosition * 12.123;
\r
307 #if configUSE_PREEMPTION == 0
\r
311 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
313 dTotal2 += pdArray[ xPosition ];
\r
316 dDifference = dTotal1 - dTotal2;
\r
317 if( fabs( dDifference ) > 0.001 )
\r
322 #if configUSE_PREEMPTION == 0
\r
326 if( sError == pdFALSE )
\r
328 /* If the calculation has always been correct, increment the check
\r
329 variable so we know this task is still running okay. */
\r
330 ( *pulTaskCheckVariable )++;
\r
334 /*-----------------------------------------------------------*/
\r
336 /* This is called to check that all the created tasks are still running. */
\r
337 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
339 /* Keep a history of the check variables so we know if they have been incremented
\r
340 since the last call. */
\r
341 static unsigned long ulLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
342 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
344 /* Check the maths tasks are still running by ensuring their check variables
\r
345 are still incrementing. */
\r
346 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
348 if( ulTaskCheck[ xTask ] == ulLastTaskCheck[ xTask ] )
\r
350 /* The check has not incremented so an error exists. */
\r
354 ulLastTaskCheck[ xTask ] = ulTaskCheck[ xTask ];
\r