2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * Creates eight tasks, each of which loops continuously performing a
\r
31 * floating point calculation.
\r
33 * All the tasks run at the idle priority and never block or yield. This causes
\r
34 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
35 * means that these tasks will get pre-empted any time another task is ready to run
\r
36 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
37 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
38 * calculation producing an unexpected result could be a symptom of a corruption in
\r
39 * the context of a task.
\r
45 /* Scheduler include files. */
\r
46 #include "FreeRTOS.h"
\r
49 /* Demo program include files. */
\r
50 #include "flop_mz.h"
\r
52 #define mathSTACK_SIZE (configMINIMAL_STACK_SIZE + 100)
\r
53 #define mathNUMBER_OF_TASKS ( 8 )
\r
55 /* Four tasks, each of which performs a different floating point calculation.
\r
56 Each of the four is created twice. */
\r
57 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
58 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
59 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
60 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
62 /* These variables are used to check that all the tasks are still running. If a
\r
63 task gets a calculation wrong it will
\r
64 stop incrementing its check variable. */
\r
65 static volatile unsigned long ulTaskCheck[ mathNUMBER_OF_TASKS ] = { 0 };
\r
67 /*-----------------------------------------------------------*/
\r
69 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
71 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 0 ] ), uxPriority, NULL );
\r
72 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 1 ] ), uxPriority, NULL );
\r
73 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 2 ] ), uxPriority, NULL );
\r
74 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 3 ] ), uxPriority, NULL );
\r
75 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 4 ] ), uxPriority, NULL );
\r
76 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 5 ] ), uxPriority, NULL );
\r
77 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 6 ] ), uxPriority, NULL );
\r
78 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 7 ] ), uxPriority, NULL );
\r
80 /*-----------------------------------------------------------*/
\r
82 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
84 volatile portDOUBLE d1, d2, d3, d4;
\r
85 volatile unsigned long *pulTaskCheckVariable;
\r
86 volatile portDOUBLE dAnswer;
\r
87 short sError = pdFALSE;
\r
90 /* Must be called before any hardware floating point operations are
\r
91 performed to let the RTOS portable layer know that this task requires
\r
92 a floating point context. */
\r
93 portTASK_USES_FLOATING_POINT();
\r
99 dAnswer = ( d1 + d2 ) * d3;
\r
101 /* The variable this task increments to show it is still running is passed in
\r
102 as the parameter. */
\r
103 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
\r
105 /* Keep performing a calculation and checking the result against a constant. */
\r
112 d4 = ( d1 + d2 ) * d3;
\r
114 #if configUSE_PREEMPTION == 0
\r
118 /* If the calculation does not match the expected constant, stop the
\r
119 increment of the check variable. */
\r
120 if( fabs( d4 - dAnswer ) > 0.001 )
\r
125 if( sError == pdFALSE )
\r
127 /* If the calculation has always been correct, increment the check
\r
128 variable so we know this task is still running okay. */
\r
129 ( *pulTaskCheckVariable )++;
\r
132 #if configUSE_PREEMPTION == 0
\r
138 /*-----------------------------------------------------------*/
\r
140 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
142 volatile portDOUBLE d1, d2, d3, d4;
\r
143 volatile unsigned long *pulTaskCheckVariable;
\r
144 volatile portDOUBLE dAnswer;
\r
145 short sError = pdFALSE;
\r
147 /* Must be called before any hardware floating point operations are
\r
148 performed to let the RTOS portable layer know that this task requires
\r
149 a floating point context. */
\r
150 portTASK_USES_FLOATING_POINT();
\r
156 dAnswer = ( d1 / d2 ) * d3;
\r
159 /* The variable this task increments to show it is still running is passed in
\r
160 as the parameter. */
\r
161 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
\r
163 /* Keep performing a calculation and checking the result against a constant. */
\r
170 d4 = ( d1 / d2 ) * d3;
\r
172 #if configUSE_PREEMPTION == 0
\r
176 /* If the calculation does not match the expected constant, stop the
\r
177 increment of the check variable. */
\r
178 if( fabs( d4 - dAnswer ) > 0.001 )
\r
183 if( sError == pdFALSE )
\r
185 /* If the calculation has always been correct, increment the check
\r
186 variable so we know
\r
187 this task is still running okay. */
\r
188 ( *pulTaskCheckVariable )++;
\r
191 #if configUSE_PREEMPTION == 0
\r
196 /*-----------------------------------------------------------*/
\r
198 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
200 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
201 volatile unsigned long *pulTaskCheckVariable;
\r
202 const size_t xArraySize = 10;
\r
204 short sError = pdFALSE;
\r
206 /* Must be called before any hardware floating point operations are
\r
207 performed to let the RTOS portable layer know that this task requires
\r
208 a floating point context. */
\r
209 portTASK_USES_FLOATING_POINT();
\r
211 /* The variable this task increments to show it is still running is passed in
\r
212 as the parameter. */
\r
213 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
\r
215 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
217 /* Keep filling an array, keeping a running total of the values placed in the
\r
218 array. Then run through the array adding up all the values. If the two totals
\r
219 do not match, stop the check variable from incrementing. */
\r
225 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
227 pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;
\r
228 dTotal1 += ( portDOUBLE ) xPosition + 5.5;
\r
231 #if configUSE_PREEMPTION == 0
\r
235 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
237 dTotal2 += pdArray[ xPosition ];
\r
240 dDifference = dTotal1 - dTotal2;
\r
241 if( fabs( dDifference ) > 0.001 )
\r
246 #if configUSE_PREEMPTION == 0
\r
250 if( sError == pdFALSE )
\r
252 /* If the calculation has always been correct, increment the check
\r
253 variable so we know this task is still running okay. */
\r
254 ( *pulTaskCheckVariable )++;
\r
258 /*-----------------------------------------------------------*/
\r
260 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
262 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
263 volatile unsigned long *pulTaskCheckVariable;
\r
264 const size_t xArraySize = 10;
\r
266 short sError = pdFALSE;
\r
268 /* Must be called before any hardware floating point operations are
\r
269 performed to let the RTOS portable layer know that this task requires
\r
270 a floating point context. */
\r
271 portTASK_USES_FLOATING_POINT();
\r
273 /* The variable this task increments to show it is still running is passed in
\r
274 as the parameter. */
\r
275 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
\r
277 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
279 /* Keep filling an array, keeping a running total of the values placed in the
\r
280 array. Then run through the array adding up all the values. If the two totals
\r
281 do not match, stop the check variable from incrementing. */
\r
287 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
289 pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;
\r
290 dTotal1 += ( portDOUBLE ) xPosition * 12.123;
\r
293 #if configUSE_PREEMPTION == 0
\r
297 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
299 dTotal2 += pdArray[ xPosition ];
\r
302 dDifference = dTotal1 - dTotal2;
\r
303 if( fabs( dDifference ) > 0.001 )
\r
308 #if configUSE_PREEMPTION == 0
\r
312 if( sError == pdFALSE )
\r
314 /* If the calculation has always been correct, increment the check
\r
315 variable so we know this task is still running okay. */
\r
316 ( *pulTaskCheckVariable )++;
\r
320 /*-----------------------------------------------------------*/
\r
322 /* This is called to check that all the created tasks are still running. */
\r
323 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
325 /* Keep a history of the check variables so we know if they have been incremented
\r
326 since the last call. */
\r
327 static unsigned long ulLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
328 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
330 /* Check the maths tasks are still running by ensuring their check variables
\r
331 are still incrementing. */
\r
332 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
334 if( ulTaskCheck[ xTask ] == ulLastTaskCheck[ xTask ] )
\r
336 /* The check has not incremented so an error exists. */
\r
340 ulLastTaskCheck[ xTask ] = ulTaskCheck[ xTask ];
\r