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 * This does the same as flop. c, but uses variables of type long instead of
\r
40 * As with flop. c, the tasks created in this file are a good test of the
\r
41 * scheduler context switch mechanism. The processor has to access 32bit
\r
42 * variables in two or four chunks (depending on the processor). The low
\r
43 * priority of these tasks means there is a high probability that a context
\r
44 * switch will occur mid calculation. See the flop. c documentation for
\r
47 * \page IntegerC integer.c
\r
48 * \ingroup DemoFiles
\r
55 + The constants used in the calculations are larger to ensure the
\r
56 optimiser does not truncate them to 16 bits.
\r
61 /* Scheduler include files. */
\r
62 #include "FreeRTOS.h"
\r
66 /* Demo program include files. */
\r
67 #include "integer.h"
\r
69 #define intgSTACK_SIZE ( ( unsigned short ) 256 )
\r
70 #define intgNUMBER_OF_TASKS ( 8 )
\r
72 /* Four tasks, each of which performs a different calculation on four byte
\r
73 variables. Each of the four is created twice. */
\r
74 static void vCompeteingIntMathTask1( void *pvParameters );
\r
75 static void vCompeteingIntMathTask2( void *pvParameters );
\r
76 static void vCompeteingIntMathTask3( void *pvParameters );
\r
77 static void vCompeteingIntMathTask4( void *pvParameters );
\r
79 /* These variables are used to check that all the tasks are still running. If a
\r
80 task gets a calculation wrong it will stop incrementing its check variable. */
\r
81 static volatile unsigned short usTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
82 /*-----------------------------------------------------------*/
\r
84 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )
\r
86 xTaskCreate( vCompeteingIntMathTask1, "IntMath1", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
87 xTaskCreate( vCompeteingIntMathTask2, "IntMath2", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
88 xTaskCreate( vCompeteingIntMathTask3, "IntMath3", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
89 xTaskCreate( vCompeteingIntMathTask4, "IntMath4", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
90 xTaskCreate( vCompeteingIntMathTask1, "IntMath5", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
91 xTaskCreate( vCompeteingIntMathTask2, "IntMath6", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
92 xTaskCreate( vCompeteingIntMathTask3, "IntMath7", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
93 xTaskCreate( vCompeteingIntMathTask4, "IntMath8", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
95 /*-----------------------------------------------------------*/
\r
97 static void vCompeteingIntMathTask1( void *pvParameters )
\r
99 long l1, l2, l3, l4;
\r
100 short sError = pdFALSE;
\r
101 volatile unsigned short *pusTaskCheckVariable;
\r
102 const long lAnswer = ( ( long ) 74565L + ( long ) 1234567L ) * ( long ) -918L;
\r
103 const char * const pcTaskStartMsg = "Integer math task 1 started.\r\n";
\r
104 const char * const pcTaskFailMsg = "Integer math task 1 failed.\r\n";
\r
106 /* Queue a message for printing to say the task has started. */
\r
107 vPrintDisplayMessage( &pcTaskStartMsg );
\r
109 /* The variable this task increments to show it is still running is passed in
\r
110 as the parameter. */
\r
111 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
113 /* Keep performing a calculation and checking the result against a constant. */
\r
116 l1 = ( long ) 74565L;
\r
117 l2 = ( long ) 1234567L;
\r
118 l3 = ( long ) -918L;
\r
120 l4 = ( l1 + l2 ) * l3;
\r
124 /* If the calculation does not match the expected constant, stop the
\r
125 increment of the check variable. */
\r
126 if( l4 != lAnswer )
\r
128 vPrintDisplayMessage( &pcTaskFailMsg );
\r
132 if( sError == pdFALSE )
\r
134 /* If the calculation has always been correct, increment the check
\r
135 variable so we know this task is still running okay. */
\r
136 ( *pusTaskCheckVariable )++;
\r
140 /*-----------------------------------------------------------*/
\r
142 static void vCompeteingIntMathTask2( void *pvParameters )
\r
144 long l1, l2, l3, l4;
\r
145 short sError = pdFALSE;
\r
146 volatile unsigned short *pusTaskCheckVariable;
\r
147 const long lAnswer = ( ( long ) -389000L / ( long ) 329999L ) * ( long ) -89L;
\r
148 const char * const pcTaskStartMsg = "Integer math task 2 started.\r\n";
\r
149 const char * const pcTaskFailMsg = "Integer math task 2 failed.\r\n";
\r
151 /* Queue a message for printing to say the task has started. */
\r
152 vPrintDisplayMessage( &pcTaskStartMsg );
\r
154 /* The variable this task increments to show it is still running is passed in
\r
155 as the parameter. */
\r
156 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
158 /* Keep performing a calculation and checking the result against a constant. */
\r
165 l4 = ( l1 / l2 ) * l3;
\r
169 /* If the calculation does not match the expected constant, stop the
\r
170 increment of the check variable. */
\r
171 if( l4 != lAnswer )
\r
173 vPrintDisplayMessage( &pcTaskFailMsg );
\r
177 if( sError == pdFALSE )
\r
179 /* If the calculation has always been correct, increment the check
\r
180 variable so we know this task is still running okay. */
\r
181 ( *pusTaskCheckVariable )++;
\r
185 /*-----------------------------------------------------------*/
\r
187 static void vCompeteingIntMathTask3( void *pvParameters )
\r
189 long *plArray, lTotal1, lTotal2;
\r
190 short sError = pdFALSE;
\r
191 volatile unsigned short *pusTaskCheckVariable;
\r
192 const unsigned short usArraySize = ( unsigned short ) 250;
\r
193 unsigned short usPosition;
\r
194 const char * const pcTaskStartMsg = "Integer math task 3 started.\r\n";
\r
195 const char * const pcTaskFailMsg = "Integer math task 3 failed.\r\n";
\r
197 /* Queue a message for printing to say the task has started. */
\r
198 vPrintDisplayMessage( &pcTaskStartMsg );
\r
200 /* The variable this task increments to show it is still running is passed in
\r
201 as the parameter. */
\r
202 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
204 /* Create the array we are going to use for our check calculation. */
\r
205 plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );
\r
207 /* Keep filling the 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
212 lTotal1 = ( long ) 0;
\r
213 lTotal2 = ( long ) 0;
\r
215 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
217 plArray[ usPosition ] = ( long ) usPosition + ( long ) 5;
\r
218 lTotal1 += ( long ) usPosition + ( long ) 5;
\r
223 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
225 lTotal2 += plArray[ usPosition ];
\r
228 if( lTotal1 != lTotal2 )
\r
230 vPrintDisplayMessage( &pcTaskFailMsg );
\r
236 if( sError == pdFALSE )
\r
238 /* If the calculation has always been correct, increment the check
\r
239 variable so we know this task is still running okay. */
\r
240 ( *pusTaskCheckVariable )++;
\r
244 /*-----------------------------------------------------------*/
\r
246 static void vCompeteingIntMathTask4( void *pvParameters )
\r
248 long *plArray, lTotal1, lTotal2;
\r
249 short sError = pdFALSE;
\r
250 volatile unsigned short *pusTaskCheckVariable;
\r
251 const unsigned short usArraySize = 250;
\r
252 unsigned short usPosition;
\r
253 const char * const pcTaskStartMsg = "Integer math task 4 started.\r\n";
\r
254 const char * const pcTaskFailMsg = "Integer math task 4 failed.\r\n";
\r
256 /* Queue a message for printing to say the task has started. */
\r
257 vPrintDisplayMessage( &pcTaskStartMsg );
\r
259 /* The variable this task increments to show it is still running is passed in
\r
260 as the parameter. */
\r
261 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
263 /* Create the array we are going to use for our check calculation. */
\r
264 plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );
\r
266 /* Keep filling the 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
271 lTotal1 = ( long ) 0;
\r
272 lTotal2 = ( long ) 0;
\r
274 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
276 plArray[ usPosition ] = ( long ) usPosition * ( long ) 12;
\r
277 lTotal1 += ( long ) usPosition * ( long ) 12;
\r
282 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
284 lTotal2 += plArray[ usPosition ];
\r
288 if( lTotal1 != lTotal2 )
\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 xAreIntegerMathsTaskStillRunning( 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[ intgNUMBER_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 < intgNUMBER_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