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
31 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
32 with the cooperative scheduler.
\r
36 * Creates eight tasks, each of which loops continuously performing an (emulated)
\r
37 * floating point calculation.
\r
39 * All the tasks run at the idle priority and never block or yield. This causes
\r
40 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
41 * means that these tasks will get pre-empted any time another task is ready to run
\r
42 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
43 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
44 * calculation producing an unexpected result could be a symptom of a corruption in
\r
45 * the context of a task.
\r
47 * \page FlopC flop.c
\r
48 * \ingroup DemoFiles
\r
55 /* Scheduler include files. */
\r
56 #include "FreeRTOS.h"
\r
60 /* Demo program include files. */
\r
63 #define mathSTACK_SIZE ( ( unsigned short ) 512 )
\r
64 #define mathNUMBER_OF_TASKS ( 8 )
\r
66 /* Four tasks, each of which performs a different floating point calculation.
\r
67 Each of the four is created twice. */
\r
68 static void vCompetingMathTask1( void *pvParameters );
\r
69 static void vCompetingMathTask2( void *pvParameters );
\r
70 static void vCompetingMathTask3( void *pvParameters );
\r
71 static void vCompetingMathTask4( void *pvParameters );
\r
73 /* These variables are used to check that all the tasks are still running. If a
\r
74 task gets a calculation wrong it will
\r
75 stop incrementing its check variable. */
\r
76 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
78 /*-----------------------------------------------------------*/
\r
80 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
82 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
83 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
84 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
85 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
86 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
87 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
88 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
89 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
91 /*-----------------------------------------------------------*/
\r
93 static void vCompetingMathTask1( void *pvParameters )
\r
95 portDOUBLE d1, d2, d3, d4;
\r
96 volatile unsigned short *pusTaskCheckVariable;
\r
97 const portDOUBLE dAnswer = ( 123.4567 + 2345.6789 ) * -918.222;
\r
98 const char * const pcTaskStartMsg = "Math task 1 started.\r\n";
\r
99 const char * const pcTaskFailMsg = "Math task 1 failed.\r\n";
\r
100 short sError = pdFALSE;
\r
102 /* Queue a message for printing to say the task has started. */
\r
103 vPrintDisplayMessage( &pcTaskStartMsg );
\r
105 /* The variable this task increments to show it is still running is passed in
\r
106 as the parameter. */
\r
107 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
109 /* Keep performing a calculation and checking the result against a constant. */
\r
116 d4 = ( d1 + d2 ) * d3;
\r
120 /* If the calculation does not match the expected constant, stop the
\r
121 increment of the check variable. */
\r
122 if( fabs( d4 - dAnswer ) > 0.001 )
\r
124 vPrintDisplayMessage( &pcTaskFailMsg );
\r
128 if( sError == pdFALSE )
\r
130 /* If the calculation has always been correct, increment the check
\r
131 variable so we know this task is still running okay. */
\r
132 ( *pusTaskCheckVariable )++;
\r
138 /*-----------------------------------------------------------*/
\r
140 static void vCompetingMathTask2( void *pvParameters )
\r
142 portDOUBLE d1, d2, d3, d4;
\r
143 volatile unsigned short *pusTaskCheckVariable;
\r
144 const portDOUBLE dAnswer = ( -389.38 / 32498.2 ) * -2.0001;
\r
145 const char * const pcTaskStartMsg = "Math task 2 started.\r\n";
\r
146 const char * const pcTaskFailMsg = "Math task 2 failed.\r\n";
\r
147 short sError = pdFALSE;
\r
149 /* Queue a message for printing to say the task has started. */
\r
150 vPrintDisplayMessage( &pcTaskStartMsg );
\r
152 /* The variable this task increments to show it is still running is passed in
\r
153 as the parameter. */
\r
154 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
156 /* Keep performing a calculation and checking the result against a constant. */
\r
163 d4 = ( d1 / d2 ) * d3;
\r
167 /* If the calculation does not match the expected constant, stop the
\r
168 increment of the check variable. */
\r
169 if( fabs( d4 - dAnswer ) > 0.001 )
\r
171 vPrintDisplayMessage( &pcTaskFailMsg );
\r
175 if( sError == pdFALSE )
\r
177 /* If the calculation has always been correct, increment the check
\r
178 variable so we know
\r
179 this task is still running okay. */
\r
180 ( *pusTaskCheckVariable )++;
\r
186 /*-----------------------------------------------------------*/
\r
188 static void vCompetingMathTask3( void *pvParameters )
\r
190 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
191 volatile unsigned short *pusTaskCheckVariable;
\r
192 const unsigned short usArraySize = 250;
\r
193 unsigned short usPosition;
\r
194 const char * const pcTaskStartMsg = "Math task 3 started.\r\n";
\r
195 const char * const pcTaskFailMsg = "Math task 3 failed.\r\n";
\r
196 short sError = pdFALSE;
\r
198 /* Queue a message for printing to say the task has started. */
\r
199 vPrintDisplayMessage( &pcTaskStartMsg );
\r
201 /* The variable this task increments to show it is still running is passed in
\r
202 as the parameter. */
\r
203 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
205 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
207 /* Keep filling an array, keeping a running total of the values placed in the
\r
208 array. Then run through the array adding up all the values. If the two totals
\r
209 do not match, stop the check variable from incrementing. */
\r
215 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
217 pdArray[ usPosition ] = ( portDOUBLE ) usPosition + 5.5;
\r
218 dTotal1 += ( portDOUBLE ) usPosition + 5.5;
\r
223 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
225 dTotal2 += pdArray[ usPosition ];
\r
228 dDifference = dTotal1 - dTotal2;
\r
229 if( fabs( dDifference ) > 0.001 )
\r
231 vPrintDisplayMessage( &pcTaskFailMsg );
\r
237 if( sError == pdFALSE )
\r
239 /* If the calculation has always been correct, increment the check
\r
240 variable so we know this task is still running okay. */
\r
241 ( *pusTaskCheckVariable )++;
\r
245 /*-----------------------------------------------------------*/
\r
247 static void vCompetingMathTask4( void *pvParameters )
\r
249 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
250 volatile unsigned short *pusTaskCheckVariable;
\r
251 const unsigned short usArraySize = 250;
\r
252 unsigned short usPosition;
\r
253 const char * const pcTaskStartMsg = "Math task 4 started.\r\n";
\r
254 const char * const pcTaskFailMsg = "Math task 4 failed.\r\n";
\r
255 short sError = pdFALSE;
\r
257 /* Queue a message for printing to say the task has started. */
\r
258 vPrintDisplayMessage( &pcTaskStartMsg );
\r
260 /* The variable this task increments to show it is still running is passed in
\r
261 as the parameter. */
\r
262 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
264 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
266 /* Keep filling an array, keeping a running total of the values placed in the
\r
267 array. Then run through the array adding up all the values. If the two totals
\r
268 do not match, stop the check variable from incrementing. */
\r
274 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
276 pdArray[ usPosition ] = ( portDOUBLE ) usPosition * 12.123;
\r
277 dTotal1 += ( portDOUBLE ) usPosition * 12.123;
\r
282 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
284 dTotal2 += pdArray[ usPosition ];
\r
287 dDifference = dTotal1 - dTotal2;
\r
288 if( fabs( dDifference ) > 0.001 )
\r
290 vPrintDisplayMessage( &pcTaskFailMsg );
\r
296 if( sError == pdFALSE )
\r
298 /* If the calculation has always been correct, increment the check
\r
299 variable so we know this task is still running okay. */
\r
300 ( *pusTaskCheckVariable )++;
\r
304 /*-----------------------------------------------------------*/
\r
306 /* This is called to check that all the created tasks are still running. */
\r
307 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
309 /* Keep a history of the check variables so we know if they have been incremented
\r
310 since the last call. */
\r
311 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
312 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
314 /* Check the maths tasks are still running by ensuring their check variables
\r
315 are still incrementing. */
\r
316 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
318 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
320 /* The check has not incremented so an error exists. */
\r
324 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r