2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 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
\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
49 #include "flop_mz.h"
\r
51 #define mathSTACK_SIZE (configMINIMAL_STACK_SIZE + 100)
\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 /*-----------------------------------------------------------*/
\r
68 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
70 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 0 ] ), uxPriority, NULL );
\r
71 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 1 ] ), uxPriority, NULL );
\r
72 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 2 ] ), uxPriority, NULL );
\r
73 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 3 ] ), uxPriority, NULL );
\r
74 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 4 ] ), uxPriority, NULL );
\r
75 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 5 ] ), uxPriority, NULL );
\r
76 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 6 ] ), uxPriority, NULL );
\r
77 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 7 ] ), uxPriority, NULL );
\r
79 /*-----------------------------------------------------------*/
\r
81 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
83 volatile portDOUBLE d1, d2, d3, d4;
\r
84 volatile unsigned long *pulTaskCheckVariable;
\r
85 volatile portDOUBLE dAnswer;
\r
86 short sError = pdFALSE;
\r
89 /* Must be called before any hardware floating point operations are
\r
90 performed to let the RTOS portable layer know that this task requires
\r
91 a floating point context. */
\r
92 portTASK_USES_FLOATING_POINT();
\r
98 dAnswer = ( d1 + d2 ) * d3;
\r
100 /* The variable this task increments to show it is still running is passed in
\r
101 as the parameter. */
\r
102 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
\r
104 /* Keep performing a calculation and checking the result against a constant. */
\r
111 d4 = ( d1 + d2 ) * d3;
\r
113 #if configUSE_PREEMPTION == 0
\r
117 /* If the calculation does not match the expected constant, stop the
\r
118 increment of the check variable. */
\r
119 if( fabs( d4 - dAnswer ) > 0.001 )
\r
124 if( sError == pdFALSE )
\r
126 /* If the calculation has always been correct, increment the check
\r
127 variable so we know this task is still running okay. */
\r
128 ( *pulTaskCheckVariable )++;
\r
131 #if configUSE_PREEMPTION == 0
\r
137 /*-----------------------------------------------------------*/
\r
139 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
141 volatile portDOUBLE d1, d2, d3, d4;
\r
142 volatile unsigned long *pulTaskCheckVariable;
\r
143 volatile portDOUBLE dAnswer;
\r
144 short sError = pdFALSE;
\r
146 /* Must be called before any hardware floating point operations are
\r
147 performed to let the RTOS portable layer know that this task requires
\r
148 a floating point context. */
\r
149 portTASK_USES_FLOATING_POINT();
\r
155 dAnswer = ( d1 / d2 ) * d3;
\r
158 /* The variable this task increments to show it is still running is passed in
\r
159 as the parameter. */
\r
160 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
\r
162 /* Keep performing a calculation and checking the result against a constant. */
\r
169 d4 = ( d1 / d2 ) * d3;
\r
171 #if configUSE_PREEMPTION == 0
\r
175 /* If the calculation does not match the expected constant, stop the
\r
176 increment of the check variable. */
\r
177 if( fabs( d4 - dAnswer ) > 0.001 )
\r
182 if( sError == pdFALSE )
\r
184 /* If the calculation has always been correct, increment the check
\r
185 variable so we know
\r
186 this task is still running okay. */
\r
187 ( *pulTaskCheckVariable )++;
\r
190 #if configUSE_PREEMPTION == 0
\r
195 /*-----------------------------------------------------------*/
\r
197 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
199 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
200 volatile unsigned long *pulTaskCheckVariable;
\r
201 const size_t xArraySize = 10;
\r
203 short sError = pdFALSE;
\r
205 /* Must be called before any hardware floating point operations are
\r
206 performed to let the RTOS portable layer know that this task requires
\r
207 a floating point context. */
\r
208 portTASK_USES_FLOATING_POINT();
\r
210 /* The variable this task increments to show it is still running is passed in
\r
211 as the parameter. */
\r
212 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
\r
214 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
216 /* Keep filling an array, keeping a running total of the values placed in the
\r
217 array. Then run through the array adding up all the values. If the two totals
\r
218 do not match, stop the check variable from incrementing. */
\r
224 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
226 pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;
\r
227 dTotal1 += ( portDOUBLE ) xPosition + 5.5;
\r
230 #if configUSE_PREEMPTION == 0
\r
234 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
236 dTotal2 += pdArray[ xPosition ];
\r
239 dDifference = dTotal1 - dTotal2;
\r
240 if( fabs( dDifference ) > 0.001 )
\r
245 #if configUSE_PREEMPTION == 0
\r
249 if( sError == pdFALSE )
\r
251 /* If the calculation has always been correct, increment the check
\r
252 variable so we know this task is still running okay. */
\r
253 ( *pulTaskCheckVariable )++;
\r
257 /*-----------------------------------------------------------*/
\r
259 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
261 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
262 volatile unsigned long *pulTaskCheckVariable;
\r
263 const size_t xArraySize = 10;
\r
265 short sError = pdFALSE;
\r
267 /* Must be called before any hardware floating point operations are
\r
268 performed to let the RTOS portable layer know that this task requires
\r
269 a floating point context. */
\r
270 portTASK_USES_FLOATING_POINT();
\r
272 /* The variable this task increments to show it is still running is passed in
\r
273 as the parameter. */
\r
274 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
\r
276 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
278 /* Keep filling an array, keeping a running total of the values placed in the
\r
279 array. Then run through the array adding up all the values. If the two totals
\r
280 do not match, stop the check variable from incrementing. */
\r
286 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
288 pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;
\r
289 dTotal1 += ( portDOUBLE ) xPosition * 12.123;
\r
292 #if configUSE_PREEMPTION == 0
\r
296 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
298 dTotal2 += pdArray[ xPosition ];
\r
301 dDifference = dTotal1 - dTotal2;
\r
302 if( fabs( dDifference ) > 0.001 )
\r
307 #if configUSE_PREEMPTION == 0
\r
311 if( sError == pdFALSE )
\r
313 /* If the calculation has always been correct, increment the check
\r
314 variable so we know this task is still running okay. */
\r
315 ( *pulTaskCheckVariable )++;
\r
319 /*-----------------------------------------------------------*/
\r
321 /* This is called to check that all the created tasks are still running. */
\r
322 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
324 /* Keep a history of the check variables so we know if they have been incremented
\r
325 since the last call. */
\r
326 static unsigned long ulLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
327 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
329 /* Check the maths tasks are still running by ensuring their check variables
\r
330 are still incrementing. */
\r
331 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
333 if( ulTaskCheck[ xTask ] == ulLastTaskCheck[ xTask ] )
\r
335 /* The check has not incremented so an error exists. */
\r
339 ulLastTaskCheck[ xTask ] = ulTaskCheck[ xTask ];
\r