2 * FreeRTOS Kernel V10.2.0
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
31 + The created tasks now include calls to tskYIELD(), allowing them to be used
32 with the cooperative scheduler.
36 * This does the same as flop. c, but uses variables of type long instead of
39 * As with flop. c, the tasks created in this file are a good test of the
40 * scheduler context switch mechanism. The processor has to access 32bit
41 * variables in two or four chunks (depending on the processor). The low
42 * priority of these tasks means there is a high probability that a context
43 * switch will occur mid calculation. See the flop. c documentation for
46 * \page IntegerC integer.c
54 + The constants used in the calculations are larger to ensure the
55 optimiser does not truncate them to 16 bits.
60 /* Scheduler include files. */
65 /* Demo program include files. */
68 #define intgSTACK_SIZE ( ( unsigned short ) 256 )
69 #define intgNUMBER_OF_TASKS ( 8 )
71 /* Four tasks, each of which performs a different calculation on four byte
72 variables. Each of the four is created twice. */
73 static void vCompeteingIntMathTask1( void *pvParameters );
74 static void vCompeteingIntMathTask2( void *pvParameters );
75 static void vCompeteingIntMathTask3( void *pvParameters );
76 static void vCompeteingIntMathTask4( void *pvParameters );
78 /* These variables are used to check that all the tasks are still running. If a
79 task gets a calculation wrong it will stop incrementing its check variable. */
80 static volatile unsigned short usTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
81 /*-----------------------------------------------------------*/
83 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )
85 xTaskCreate( vCompeteingIntMathTask1, "IntMath1", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
86 xTaskCreate( vCompeteingIntMathTask2, "IntMath2", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
87 xTaskCreate( vCompeteingIntMathTask3, "IntMath3", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
88 xTaskCreate( vCompeteingIntMathTask4, "IntMath4", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
89 xTaskCreate( vCompeteingIntMathTask1, "IntMath5", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
90 xTaskCreate( vCompeteingIntMathTask2, "IntMath6", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
91 xTaskCreate( vCompeteingIntMathTask3, "IntMath7", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
92 xTaskCreate( vCompeteingIntMathTask4, "IntMath8", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
94 /*-----------------------------------------------------------*/
96 static void vCompeteingIntMathTask1( void *pvParameters )
99 short sError = pdFALSE;
100 volatile unsigned short *pusTaskCheckVariable;
101 const long lAnswer = ( ( long ) 74565L + ( long ) 1234567L ) * ( long ) -918L;
102 const char * const pcTaskStartMsg = "Integer math task 1 started.\r\n";
103 const char * const pcTaskFailMsg = "Integer math task 1 failed.\r\n";
105 /* Queue a message for printing to say the task has started. */
106 vPrintDisplayMessage( &pcTaskStartMsg );
108 /* The variable this task increments to show it is still running is passed in
110 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
112 /* Keep performing a calculation and checking the result against a constant. */
115 l1 = ( long ) 74565L;
116 l2 = ( long ) 1234567L;
119 l4 = ( l1 + l2 ) * l3;
123 /* If the calculation does not match the expected constant, stop the
124 increment of the check variable. */
127 vPrintDisplayMessage( &pcTaskFailMsg );
131 if( sError == pdFALSE )
133 /* If the calculation has always been correct, increment the check
134 variable so we know this task is still running okay. */
135 ( *pusTaskCheckVariable )++;
139 /*-----------------------------------------------------------*/
141 static void vCompeteingIntMathTask2( void *pvParameters )
144 short sError = pdFALSE;
145 volatile unsigned short *pusTaskCheckVariable;
146 const long lAnswer = ( ( long ) -389000L / ( long ) 329999L ) * ( long ) -89L;
147 const char * const pcTaskStartMsg = "Integer math task 2 started.\r\n";
148 const char * const pcTaskFailMsg = "Integer math task 2 failed.\r\n";
150 /* Queue a message for printing to say the task has started. */
151 vPrintDisplayMessage( &pcTaskStartMsg );
153 /* The variable this task increments to show it is still running is passed in
155 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
157 /* Keep performing a calculation and checking the result against a constant. */
164 l4 = ( l1 / l2 ) * l3;
168 /* If the calculation does not match the expected constant, stop the
169 increment of the check variable. */
172 vPrintDisplayMessage( &pcTaskFailMsg );
176 if( sError == pdFALSE )
178 /* If the calculation has always been correct, increment the check
179 variable so we know this task is still running okay. */
180 ( *pusTaskCheckVariable )++;
184 /*-----------------------------------------------------------*/
186 static void vCompeteingIntMathTask3( void *pvParameters )
188 long *plArray, lTotal1, lTotal2;
189 short sError = pdFALSE;
190 volatile unsigned short *pusTaskCheckVariable;
191 const unsigned short usArraySize = ( unsigned short ) 250;
192 unsigned short usPosition;
193 const char * const pcTaskStartMsg = "Integer math task 3 started.\r\n";
194 const char * const pcTaskFailMsg = "Integer math task 3 failed.\r\n";
196 /* Queue a message for printing to say the task has started. */
197 vPrintDisplayMessage( &pcTaskStartMsg );
199 /* The variable this task increments to show it is still running is passed in
201 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
203 /* Create the array we are going to use for our check calculation. */
204 plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );
206 /* Keep filling the array, keeping a running total of the values placed in the
207 array. Then run through the array adding up all the values. If the two totals
208 do not match, stop the check variable from incrementing. */
211 lTotal1 = ( long ) 0;
212 lTotal2 = ( long ) 0;
214 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
216 plArray[ usPosition ] = ( long ) usPosition + ( long ) 5;
217 lTotal1 += ( long ) usPosition + ( long ) 5;
222 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
224 lTotal2 += plArray[ usPosition ];
227 if( lTotal1 != lTotal2 )
229 vPrintDisplayMessage( &pcTaskFailMsg );
235 if( sError == pdFALSE )
237 /* If the calculation has always been correct, increment the check
238 variable so we know this task is still running okay. */
239 ( *pusTaskCheckVariable )++;
243 /*-----------------------------------------------------------*/
245 static void vCompeteingIntMathTask4( void *pvParameters )
247 long *plArray, lTotal1, lTotal2;
248 short sError = pdFALSE;
249 volatile unsigned short *pusTaskCheckVariable;
250 const unsigned short usArraySize = 250;
251 unsigned short usPosition;
252 const char * const pcTaskStartMsg = "Integer math task 4 started.\r\n";
253 const char * const pcTaskFailMsg = "Integer math task 4 failed.\r\n";
255 /* Queue a message for printing to say the task has started. */
256 vPrintDisplayMessage( &pcTaskStartMsg );
258 /* The variable this task increments to show it is still running is passed in
260 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
262 /* Create the array we are going to use for our check calculation. */
263 plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );
265 /* Keep filling the array, keeping a running total of the values placed in the
266 array. Then run through the array adding up all the values. If the two totals
267 do not match, stop the check variable from incrementing. */
270 lTotal1 = ( long ) 0;
271 lTotal2 = ( long ) 0;
273 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
275 plArray[ usPosition ] = ( long ) usPosition * ( long ) 12;
276 lTotal1 += ( long ) usPosition * ( long ) 12;
281 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
283 lTotal2 += plArray[ usPosition ];
287 if( lTotal1 != lTotal2 )
289 vPrintDisplayMessage( &pcTaskFailMsg );
295 if( sError == pdFALSE )
297 /* If the calculation has always been correct, increment the check
298 variable so we know this task is still running okay. */
299 ( *pusTaskCheckVariable )++;
303 /*-----------------------------------------------------------*/
305 /* This is called to check that all the created tasks are still running. */
306 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )
308 /* Keep a history of the check variables so we know if they have been incremented
309 since the last call. */
310 static unsigned short usLastTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
311 portBASE_TYPE xReturn = pdTRUE, xTask;
313 /* Check the maths tasks are still running by ensuring their check variables
314 are still incrementing. */
315 for( xTask = 0; xTask < intgNUMBER_OF_TASKS; xTask++ )
317 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
319 /* The check has not incremented so an error exists. */
323 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];