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
32 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
33 with the cooperative scheduler.
\r
37 * Creates eight tasks, each of which loops continuously performing an (emulated)
\r
38 * floating point calculation.
\r
40 * All the tasks run at the idle priority and never block or yield. This causes
\r
41 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
42 * means that these tasks will get pre-empted any time another task is ready to run
\r
43 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
44 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
45 * calculation producing an unexpected result could be a symptom of a corruption in
\r
46 * the context of a task.
\r
48 * \page FlopC flop.c
\r
49 * \ingroup DemoFiles
\r
56 /* Scheduler include files. */
\r
57 #include "FreeRTOS.h"
\r
61 /* Demo program include files. */
\r
64 #define mathSTACK_SIZE ( ( unsigned short ) 512 )
\r
65 #define mathNUMBER_OF_TASKS ( 8 )
\r
67 /* Four tasks, each of which performs a different floating point calculation.
\r
68 Each of the four is created twice. */
\r
69 static void vCompetingMathTask1( void *pvParameters );
\r
70 static void vCompetingMathTask2( void *pvParameters );
\r
71 static void vCompetingMathTask3( void *pvParameters );
\r
72 static void vCompetingMathTask4( void *pvParameters );
\r
74 /* These variables are used to check that all the tasks are still running. If a
\r
75 task gets a calculation wrong it will
\r
76 stop incrementing its check variable. */
\r
77 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
79 /*-----------------------------------------------------------*/
\r
81 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
83 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
84 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
85 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
86 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
87 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
88 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
89 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
90 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
92 /*-----------------------------------------------------------*/
\r
94 static void vCompetingMathTask1( void *pvParameters )
\r
96 portDOUBLE d1, d2, d3, d4;
\r
97 volatile unsigned short *pusTaskCheckVariable;
\r
98 const portDOUBLE dAnswer = ( 123.4567 + 2345.6789 ) * -918.222;
\r
99 const char * const pcTaskStartMsg = "Math task 1 started.\r\n";
\r
100 const char * const pcTaskFailMsg = "Math task 1 failed.\r\n";
\r
101 short sError = pdFALSE;
\r
103 /* Queue a message for printing to say the task has started. */
\r
104 vPrintDisplayMessage( &pcTaskStartMsg );
\r
106 /* The variable this task increments to show it is still running is passed in
\r
107 as the parameter. */
\r
108 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
110 /* Keep performing a calculation and checking the result against a constant. */
\r
117 d4 = ( d1 + d2 ) * d3;
\r
121 /* If the calculation does not match the expected constant, stop the
\r
122 increment of the check variable. */
\r
123 if( fabs( d4 - dAnswer ) > 0.001 )
\r
125 vPrintDisplayMessage( &pcTaskFailMsg );
\r
129 if( sError == pdFALSE )
\r
131 /* If the calculation has always been correct, increment the check
\r
132 variable so we know this task is still running okay. */
\r
133 ( *pusTaskCheckVariable )++;
\r
139 /*-----------------------------------------------------------*/
\r
141 static void vCompetingMathTask2( void *pvParameters )
\r
143 portDOUBLE d1, d2, d3, d4;
\r
144 volatile unsigned short *pusTaskCheckVariable;
\r
145 const portDOUBLE dAnswer = ( -389.38 / 32498.2 ) * -2.0001;
\r
146 const char * const pcTaskStartMsg = "Math task 2 started.\r\n";
\r
147 const char * const pcTaskFailMsg = "Math task 2 failed.\r\n";
\r
148 short sError = pdFALSE;
\r
150 /* Queue a message for printing to say the task has started. */
\r
151 vPrintDisplayMessage( &pcTaskStartMsg );
\r
153 /* The variable this task increments to show it is still running is passed in
\r
154 as the parameter. */
\r
155 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
157 /* Keep performing a calculation and checking the result against a constant. */
\r
164 d4 = ( d1 / d2 ) * d3;
\r
168 /* If the calculation does not match the expected constant, stop the
\r
169 increment of the check variable. */
\r
170 if( fabs( d4 - dAnswer ) > 0.001 )
\r
172 vPrintDisplayMessage( &pcTaskFailMsg );
\r
176 if( sError == pdFALSE )
\r
178 /* If the calculation has always been correct, increment the check
\r
179 variable so we know
\r
180 this task is still running okay. */
\r
181 ( *pusTaskCheckVariable )++;
\r
187 /*-----------------------------------------------------------*/
\r
189 static void vCompetingMathTask3( void *pvParameters )
\r
191 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
192 volatile unsigned short *pusTaskCheckVariable;
\r
193 const unsigned short usArraySize = 250;
\r
194 unsigned short usPosition;
\r
195 const char * const pcTaskStartMsg = "Math task 3 started.\r\n";
\r
196 const char * const pcTaskFailMsg = "Math task 3 failed.\r\n";
\r
197 short sError = pdFALSE;
\r
199 /* Queue a message for printing to say the task has started. */
\r
200 vPrintDisplayMessage( &pcTaskStartMsg );
\r
202 /* The variable this task increments to show it is still running is passed in
\r
203 as the parameter. */
\r
204 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
206 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
208 /* Keep filling an array, keeping a running total of the values placed in the
\r
209 array. Then run through the array adding up all the values. If the two totals
\r
210 do not match, stop the check variable from incrementing. */
\r
216 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
218 pdArray[ usPosition ] = ( portDOUBLE ) usPosition + 5.5;
\r
219 dTotal1 += ( portDOUBLE ) usPosition + 5.5;
\r
224 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
226 dTotal2 += pdArray[ usPosition ];
\r
229 dDifference = dTotal1 - dTotal2;
\r
230 if( fabs( dDifference ) > 0.001 )
\r
232 vPrintDisplayMessage( &pcTaskFailMsg );
\r
238 if( sError == pdFALSE )
\r
240 /* If the calculation has always been correct, increment the check
\r
241 variable so we know this task is still running okay. */
\r
242 ( *pusTaskCheckVariable )++;
\r
246 /*-----------------------------------------------------------*/
\r
248 static void vCompetingMathTask4( void *pvParameters )
\r
250 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
251 volatile unsigned short *pusTaskCheckVariable;
\r
252 const unsigned short usArraySize = 250;
\r
253 unsigned short usPosition;
\r
254 const char * const pcTaskStartMsg = "Math task 4 started.\r\n";
\r
255 const char * const pcTaskFailMsg = "Math task 4 failed.\r\n";
\r
256 short sError = pdFALSE;
\r
258 /* Queue a message for printing to say the task has started. */
\r
259 vPrintDisplayMessage( &pcTaskStartMsg );
\r
261 /* The variable this task increments to show it is still running is passed in
\r
262 as the parameter. */
\r
263 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
265 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
267 /* Keep filling an array, keeping a running total of the values placed in the
\r
268 array. Then run through the array adding up all the values. If the two totals
\r
269 do not match, stop the check variable from incrementing. */
\r
275 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
277 pdArray[ usPosition ] = ( portDOUBLE ) usPosition * 12.123;
\r
278 dTotal1 += ( portDOUBLE ) usPosition * 12.123;
\r
283 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
285 dTotal2 += pdArray[ usPosition ];
\r
288 dDifference = dTotal1 - dTotal2;
\r
289 if( fabs( dDifference ) > 0.001 )
\r
291 vPrintDisplayMessage( &pcTaskFailMsg );
\r
297 if( sError == pdFALSE )
\r
299 /* If the calculation has always been correct, increment the check
\r
300 variable so we know this task is still running okay. */
\r
301 ( *pusTaskCheckVariable )++;
\r
305 /*-----------------------------------------------------------*/
\r
307 /* This is called to check that all the created tasks are still running. */
\r
308 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
310 /* Keep a history of the check variables so we know if they have been incremented
\r
311 since the last call. */
\r
312 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
313 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
315 /* Check the maths tasks are still running by ensuring their check variables
\r
316 are still incrementing. */
\r
317 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
319 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
321 /* The check has not incremented so an error exists. */
\r
325 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r