2 * FreeRTOS Kernel V10.1.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.
\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 * This does the same as flop. c, but uses variables of type long instead of
\r
39 * As with flop. c, the tasks created in this file are a good test of the
\r
40 * scheduler context switch mechanism. The processor has to access 32bit
\r
41 * variables in two or four chunks (depending on the processor). The low
\r
42 * priority of these tasks means there is a high probability that a context
\r
43 * switch will occur mid calculation. See the flop. c documentation for
\r
46 * \page IntegerC integer.c
\r
47 * \ingroup DemoFiles
\r
54 + The constants used in the calculations are larger to ensure the
\r
55 optimiser does not truncate them to 16 bits.
\r
60 /* Scheduler include files. */
\r
61 #include "FreeRTOS.h"
\r
65 /* Demo program include files. */
\r
66 #include "integer.h"
\r
68 #define intgSTACK_SIZE ( ( unsigned short ) 256 )
\r
69 #define intgNUMBER_OF_TASKS ( 8 )
\r
71 /* Four tasks, each of which performs a different calculation on four byte
\r
72 variables. Each of the four is created twice. */
\r
73 static void vCompeteingIntMathTask1( void *pvParameters );
\r
74 static void vCompeteingIntMathTask2( void *pvParameters );
\r
75 static void vCompeteingIntMathTask3( void *pvParameters );
\r
76 static void vCompeteingIntMathTask4( void *pvParameters );
\r
78 /* These variables are used to check that all the tasks are still running. If a
\r
79 task gets a calculation wrong it will stop incrementing its check variable. */
\r
80 static volatile unsigned short usTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
81 /*-----------------------------------------------------------*/
\r
83 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )
\r
85 xTaskCreate( vCompeteingIntMathTask1, "IntMath1", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
86 xTaskCreate( vCompeteingIntMathTask2, "IntMath2", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
87 xTaskCreate( vCompeteingIntMathTask3, "IntMath3", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
88 xTaskCreate( vCompeteingIntMathTask4, "IntMath4", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
89 xTaskCreate( vCompeteingIntMathTask1, "IntMath5", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
90 xTaskCreate( vCompeteingIntMathTask2, "IntMath6", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
91 xTaskCreate( vCompeteingIntMathTask3, "IntMath7", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
92 xTaskCreate( vCompeteingIntMathTask4, "IntMath8", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
94 /*-----------------------------------------------------------*/
\r
96 static void vCompeteingIntMathTask1( void *pvParameters )
\r
98 long l1, l2, l3, l4;
\r
99 short sError = pdFALSE;
\r
100 volatile unsigned short *pusTaskCheckVariable;
\r
101 const long lAnswer = ( ( long ) 74565L + ( long ) 1234567L ) * ( long ) -918L;
\r
102 const char * const pcTaskStartMsg = "Integer math task 1 started.\r\n";
\r
103 const char * const pcTaskFailMsg = "Integer math task 1 failed.\r\n";
\r
105 /* Queue a message for printing to say the task has started. */
\r
106 vPrintDisplayMessage( &pcTaskStartMsg );
\r
108 /* The variable this task increments to show it is still running is passed in
\r
109 as the parameter. */
\r
110 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
112 /* Keep performing a calculation and checking the result against a constant. */
\r
115 l1 = ( long ) 74565L;
\r
116 l2 = ( long ) 1234567L;
\r
117 l3 = ( long ) -918L;
\r
119 l4 = ( l1 + l2 ) * l3;
\r
123 /* If the calculation does not match the expected constant, stop the
\r
124 increment of the check variable. */
\r
125 if( l4 != lAnswer )
\r
127 vPrintDisplayMessage( &pcTaskFailMsg );
\r
131 if( sError == pdFALSE )
\r
133 /* If the calculation has always been correct, increment the check
\r
134 variable so we know this task is still running okay. */
\r
135 ( *pusTaskCheckVariable )++;
\r
139 /*-----------------------------------------------------------*/
\r
141 static void vCompeteingIntMathTask2( void *pvParameters )
\r
143 long l1, l2, l3, l4;
\r
144 short sError = pdFALSE;
\r
145 volatile unsigned short *pusTaskCheckVariable;
\r
146 const long lAnswer = ( ( long ) -389000L / ( long ) 329999L ) * ( long ) -89L;
\r
147 const char * const pcTaskStartMsg = "Integer math task 2 started.\r\n";
\r
148 const char * const pcTaskFailMsg = "Integer math task 2 failed.\r\n";
\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 l4 = ( l1 / l2 ) * l3;
\r
168 /* If the calculation does not match the expected constant, stop the
\r
169 increment of the check variable. */
\r
170 if( l4 != lAnswer )
\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 this task is still running okay. */
\r
180 ( *pusTaskCheckVariable )++;
\r
184 /*-----------------------------------------------------------*/
\r
186 static void vCompeteingIntMathTask3( void *pvParameters )
\r
188 long *plArray, lTotal1, lTotal2;
\r
189 short sError = pdFALSE;
\r
190 volatile unsigned short *pusTaskCheckVariable;
\r
191 const unsigned short usArraySize = ( unsigned short ) 250;
\r
192 unsigned short usPosition;
\r
193 const char * const pcTaskStartMsg = "Integer math task 3 started.\r\n";
\r
194 const char * const pcTaskFailMsg = "Integer math task 3 failed.\r\n";
\r
196 /* Queue a message for printing to say the task has started. */
\r
197 vPrintDisplayMessage( &pcTaskStartMsg );
\r
199 /* The variable this task increments to show it is still running is passed in
\r
200 as the parameter. */
\r
201 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
203 /* Create the array we are going to use for our check calculation. */
\r
204 plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );
\r
206 /* Keep filling the array, keeping a running total of the values placed in the
\r
207 array. Then run through the array adding up all the values. If the two totals
\r
208 do not match, stop the check variable from incrementing. */
\r
211 lTotal1 = ( long ) 0;
\r
212 lTotal2 = ( long ) 0;
\r
214 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
216 plArray[ usPosition ] = ( long ) usPosition + ( long ) 5;
\r
217 lTotal1 += ( long ) usPosition + ( long ) 5;
\r
222 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
224 lTotal2 += plArray[ usPosition ];
\r
227 if( lTotal1 != lTotal2 )
\r
229 vPrintDisplayMessage( &pcTaskFailMsg );
\r
235 if( sError == pdFALSE )
\r
237 /* If the calculation has always been correct, increment the check
\r
238 variable so we know this task is still running okay. */
\r
239 ( *pusTaskCheckVariable )++;
\r
243 /*-----------------------------------------------------------*/
\r
245 static void vCompeteingIntMathTask4( void *pvParameters )
\r
247 long *plArray, lTotal1, lTotal2;
\r
248 short sError = pdFALSE;
\r
249 volatile unsigned short *pusTaskCheckVariable;
\r
250 const unsigned short usArraySize = 250;
\r
251 unsigned short usPosition;
\r
252 const char * const pcTaskStartMsg = "Integer math task 4 started.\r\n";
\r
253 const char * const pcTaskFailMsg = "Integer math task 4 failed.\r\n";
\r
255 /* Queue a message for printing to say the task has started. */
\r
256 vPrintDisplayMessage( &pcTaskStartMsg );
\r
258 /* The variable this task increments to show it is still running is passed in
\r
259 as the parameter. */
\r
260 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
262 /* Create the array we are going to use for our check calculation. */
\r
263 plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );
\r
265 /* Keep filling the array, keeping a running total of the values placed in the
\r
266 array. Then run through the array adding up all the values. If the two totals
\r
267 do not match, stop the check variable from incrementing. */
\r
270 lTotal1 = ( long ) 0;
\r
271 lTotal2 = ( long ) 0;
\r
273 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
275 plArray[ usPosition ] = ( long ) usPosition * ( long ) 12;
\r
276 lTotal1 += ( long ) usPosition * ( long ) 12;
\r
281 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
283 lTotal2 += plArray[ usPosition ];
\r
287 if( lTotal1 != lTotal2 )
\r
289 vPrintDisplayMessage( &pcTaskFailMsg );
\r
295 if( sError == pdFALSE )
\r
297 /* If the calculation has always been correct, increment the check
\r
298 variable so we know this task is still running okay. */
\r
299 ( *pusTaskCheckVariable )++;
\r
303 /*-----------------------------------------------------------*/
\r
305 /* This is called to check that all the created tasks are still running. */
\r
306 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )
\r
308 /* Keep a history of the check variables so we know if they have been incremented
\r
309 since the last call. */
\r
310 static unsigned short usLastTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
311 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
313 /* Check the maths tasks are still running by ensuring their check variables
\r
314 are still incrementing. */
\r
315 for( xTask = 0; xTask < intgNUMBER_OF_TASKS; xTask++ )
\r
317 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
319 /* The check has not incremented so an error exists. */
\r
323 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r