2 FreeRTOS.org V4.8.0 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! Why not get us to quote to get FreeRTOS.org *
\r
30 * running on your hardware - or even write all or part of your application*
\r
31 * for you? See http://www.OpenRTOS.com for details. *
\r
33 ***************************************************************************
\r
34 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
52 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
53 with the cooperative scheduler.
\r
57 * This does the same as flop. c, but uses variables of type long instead of
\r
60 * As with flop. c, the tasks created in this file are a good test of the
\r
61 * scheduler context switch mechanism. The processor has to access 32bit
\r
62 * variables in two or four chunks (depending on the processor). The low
\r
63 * priority of these tasks means there is a high probability that a context
\r
64 * switch will occur mid calculation. See the flop. c documentation for
\r
67 * \page IntegerC integer.c
\r
68 * \ingroup DemoFiles
\r
75 + The constants used in the calculations are larger to ensure the
\r
76 optimiser does not truncate them to 16 bits.
\r
81 /* Scheduler include files. */
\r
82 #include "FreeRTOS.h"
\r
86 /* Demo program include files. */
\r
87 #include "integer.h"
\r
89 #define intgSTACK_SIZE ( ( unsigned portSHORT ) 256 )
\r
90 #define intgNUMBER_OF_TASKS ( 8 )
\r
92 /* Four tasks, each of which performs a different calculation on four byte
\r
93 variables. Each of the four is created twice. */
\r
94 static void vCompeteingIntMathTask1( void *pvParameters );
\r
95 static void vCompeteingIntMathTask2( void *pvParameters );
\r
96 static void vCompeteingIntMathTask3( void *pvParameters );
\r
97 static void vCompeteingIntMathTask4( void *pvParameters );
\r
99 /* These variables are used to check that all the tasks are still running. If a
\r
100 task gets a calculation wrong it will stop incrementing its check variable. */
\r
101 static volatile unsigned portSHORT usTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
102 /*-----------------------------------------------------------*/
\r
104 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )
\r
106 xTaskCreate( vCompeteingIntMathTask1, "IntMath1", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
107 xTaskCreate( vCompeteingIntMathTask2, "IntMath2", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
108 xTaskCreate( vCompeteingIntMathTask3, "IntMath3", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
109 xTaskCreate( vCompeteingIntMathTask4, "IntMath4", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
110 xTaskCreate( vCompeteingIntMathTask1, "IntMath5", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
111 xTaskCreate( vCompeteingIntMathTask2, "IntMath6", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
112 xTaskCreate( vCompeteingIntMathTask3, "IntMath7", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
113 xTaskCreate( vCompeteingIntMathTask4, "IntMath8", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
115 /*-----------------------------------------------------------*/
\r
117 static void vCompeteingIntMathTask1( void *pvParameters )
\r
119 portLONG l1, l2, l3, l4;
\r
120 portSHORT sError = pdFALSE;
\r
121 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
122 const portLONG lAnswer = ( ( portLONG ) 74565L + ( portLONG ) 1234567L ) * ( portLONG ) -918L;
\r
123 const portCHAR * const pcTaskStartMsg = "Integer math task 1 started.\r\n";
\r
124 const portCHAR * const pcTaskFailMsg = "Integer math task 1 failed.\r\n";
\r
126 /* Queue a message for printing to say the task has started. */
\r
127 vPrintDisplayMessage( &pcTaskStartMsg );
\r
129 /* The variable this task increments to show it is still running is passed in
\r
130 as the parameter. */
\r
131 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
133 /* Keep performing a calculation and checking the result against a constant. */
\r
136 l1 = ( portLONG ) 74565L;
\r
137 l2 = ( portLONG ) 1234567L;
\r
138 l3 = ( portLONG ) -918L;
\r
140 l4 = ( l1 + l2 ) * l3;
\r
144 /* If the calculation does not match the expected constant, stop the
\r
145 increment of the check variable. */
\r
146 if( l4 != lAnswer )
\r
148 vPrintDisplayMessage( &pcTaskFailMsg );
\r
152 if( sError == pdFALSE )
\r
154 /* If the calculation has always been correct, increment the check
\r
155 variable so we know this task is still running okay. */
\r
156 ( *pusTaskCheckVariable )++;
\r
160 /*-----------------------------------------------------------*/
\r
162 static void vCompeteingIntMathTask2( void *pvParameters )
\r
164 portLONG l1, l2, l3, l4;
\r
165 portSHORT sError = pdFALSE;
\r
166 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
167 const portLONG lAnswer = ( ( portLONG ) -389000L / ( portLONG ) 329999L ) * ( portLONG ) -89L;
\r
168 const portCHAR * const pcTaskStartMsg = "Integer math task 2 started.\r\n";
\r
169 const portCHAR * const pcTaskFailMsg = "Integer math task 2 failed.\r\n";
\r
171 /* Queue a message for printing to say the task has started. */
\r
172 vPrintDisplayMessage( &pcTaskStartMsg );
\r
174 /* The variable this task increments to show it is still running is passed in
\r
175 as the parameter. */
\r
176 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
178 /* Keep performing a calculation and checking the result against a constant. */
\r
185 l4 = ( l1 / l2 ) * l3;
\r
189 /* If the calculation does not match the expected constant, stop the
\r
190 increment of the check variable. */
\r
191 if( l4 != lAnswer )
\r
193 vPrintDisplayMessage( &pcTaskFailMsg );
\r
197 if( sError == pdFALSE )
\r
199 /* If the calculation has always been correct, increment the check
\r
200 variable so we know this task is still running okay. */
\r
201 ( *pusTaskCheckVariable )++;
\r
205 /*-----------------------------------------------------------*/
\r
207 static void vCompeteingIntMathTask3( void *pvParameters )
\r
209 portLONG *plArray, lTotal1, lTotal2;
\r
210 portSHORT sError = pdFALSE;
\r
211 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
212 const unsigned portSHORT usArraySize = ( unsigned portSHORT ) 250;
\r
213 unsigned portSHORT usPosition;
\r
214 const portCHAR * const pcTaskStartMsg = "Integer math task 3 started.\r\n";
\r
215 const portCHAR * const pcTaskFailMsg = "Integer math task 3 failed.\r\n";
\r
217 /* Queue a message for printing to say the task has started. */
\r
218 vPrintDisplayMessage( &pcTaskStartMsg );
\r
220 /* The variable this task increments to show it is still running is passed in
\r
221 as the parameter. */
\r
222 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
224 /* Create the array we are going to use for our check calculation. */
\r
225 plArray = ( portLONG * ) pvPortMalloc( ( size_t ) 250 * sizeof( portLONG ) );
\r
227 /* Keep filling the array, keeping a running total of the values placed in the
\r
228 array. Then run through the array adding up all the values. If the two totals
\r
229 do not match, stop the check variable from incrementing. */
\r
232 lTotal1 = ( portLONG ) 0;
\r
233 lTotal2 = ( portLONG ) 0;
\r
235 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
237 plArray[ usPosition ] = ( portLONG ) usPosition + ( portLONG ) 5;
\r
238 lTotal1 += ( portLONG ) usPosition + ( portLONG ) 5;
\r
243 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
245 lTotal2 += plArray[ usPosition ];
\r
248 if( lTotal1 != lTotal2 )
\r
250 vPrintDisplayMessage( &pcTaskFailMsg );
\r
256 if( sError == pdFALSE )
\r
258 /* If the calculation has always been correct, increment the check
\r
259 variable so we know this task is still running okay. */
\r
260 ( *pusTaskCheckVariable )++;
\r
264 /*-----------------------------------------------------------*/
\r
266 static void vCompeteingIntMathTask4( void *pvParameters )
\r
268 portLONG *plArray, lTotal1, lTotal2;
\r
269 portSHORT sError = pdFALSE;
\r
270 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
271 const unsigned portSHORT usArraySize = 250;
\r
272 unsigned portSHORT usPosition;
\r
273 const portCHAR * const pcTaskStartMsg = "Integer math task 4 started.\r\n";
\r
274 const portCHAR * const pcTaskFailMsg = "Integer math task 4 failed.\r\n";
\r
276 /* Queue a message for printing to say the task has started. */
\r
277 vPrintDisplayMessage( &pcTaskStartMsg );
\r
279 /* The variable this task increments to show it is still running is passed in
\r
280 as the parameter. */
\r
281 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
283 /* Create the array we are going to use for our check calculation. */
\r
284 plArray = ( portLONG * ) pvPortMalloc( ( size_t ) 250 * sizeof( portLONG ) );
\r
286 /* Keep filling the array, keeping a running total of the values placed in the
\r
287 array. Then run through the array adding up all the values. If the two totals
\r
288 do not match, stop the check variable from incrementing. */
\r
291 lTotal1 = ( portLONG ) 0;
\r
292 lTotal2 = ( portLONG ) 0;
\r
294 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
296 plArray[ usPosition ] = ( portLONG ) usPosition * ( portLONG ) 12;
\r
297 lTotal1 += ( portLONG ) usPosition * ( portLONG ) 12;
\r
302 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
304 lTotal2 += plArray[ usPosition ];
\r
308 if( lTotal1 != lTotal2 )
\r
310 vPrintDisplayMessage( &pcTaskFailMsg );
\r
316 if( sError == pdFALSE )
\r
318 /* If the calculation has always been correct, increment the check
\r
319 variable so we know this task is still running okay. */
\r
320 ( *pusTaskCheckVariable )++;
\r
324 /*-----------------------------------------------------------*/
\r
326 /* This is called to check that all the created tasks are still running. */
\r
327 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )
\r
329 /* Keep a history of the check variables so we know if they have been incremented
\r
330 since the last call. */
\r
331 static unsigned portSHORT usLastTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
332 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
334 /* Check the maths tasks are still running by ensuring their check variables
\r
335 are still incrementing. */
\r
336 for( xTask = 0; xTask < intgNUMBER_OF_TASKS; xTask++ )
\r
338 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
340 /* The check has not incremented so an error exists. */
\r
344 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r