2 FreeRTOS.org V4.7.1 - 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
28 Please ensure to read the configuration and relevant port sections of the
\r
29 online documentation.
\r
31 +++ http://www.FreeRTOS.org +++
\r
32 Documentation, latest information, license and contact details.
\r
34 +++ http://www.SafeRTOS.com +++
\r
35 A version that is certified for use in safety critical systems.
\r
37 +++ http://www.OpenRTOS.com +++
\r
38 Commercial support, development, porting, licensing and training services.
\r
40 ***************************************************************************
\r
46 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
47 with the cooperative scheduler.
\r
51 * This does the same as flop. c, but uses variables of type long instead of
\r
54 * As with flop. c, the tasks created in this file are a good test of the
\r
55 * scheduler context switch mechanism. The processor has to access 32bit
\r
56 * variables in two or four chunks (depending on the processor). The low
\r
57 * priority of these tasks means there is a high probability that a context
\r
58 * switch will occur mid calculation. See the flop. c documentation for
\r
61 * \page IntegerC integer.c
\r
62 * \ingroup DemoFiles
\r
69 + The constants used in the calculations are larger to ensure the
\r
70 optimiser does not truncate them to 16 bits.
\r
75 /* Scheduler include files. */
\r
76 #include "FreeRTOS.h"
\r
80 /* Demo program include files. */
\r
81 #include "integer.h"
\r
83 #define intgSTACK_SIZE ( ( unsigned portSHORT ) 256 )
\r
84 #define intgNUMBER_OF_TASKS ( 8 )
\r
86 /* Four tasks, each of which performs a different calculation on four byte
\r
87 variables. Each of the four is created twice. */
\r
88 static void vCompeteingIntMathTask1( void *pvParameters );
\r
89 static void vCompeteingIntMathTask2( void *pvParameters );
\r
90 static void vCompeteingIntMathTask3( void *pvParameters );
\r
91 static void vCompeteingIntMathTask4( void *pvParameters );
\r
93 /* These variables are used to check that all the tasks are still running. If a
\r
94 task gets a calculation wrong it will stop incrementing its check variable. */
\r
95 static volatile unsigned portSHORT usTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
96 /*-----------------------------------------------------------*/
\r
98 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )
\r
100 xTaskCreate( vCompeteingIntMathTask1, "IntMath1", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
101 xTaskCreate( vCompeteingIntMathTask2, "IntMath2", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
102 xTaskCreate( vCompeteingIntMathTask3, "IntMath3", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
103 xTaskCreate( vCompeteingIntMathTask4, "IntMath4", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
104 xTaskCreate( vCompeteingIntMathTask1, "IntMath5", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
105 xTaskCreate( vCompeteingIntMathTask2, "IntMath6", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
106 xTaskCreate( vCompeteingIntMathTask3, "IntMath7", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
107 xTaskCreate( vCompeteingIntMathTask4, "IntMath8", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
109 /*-----------------------------------------------------------*/
\r
111 static void vCompeteingIntMathTask1( void *pvParameters )
\r
113 portLONG l1, l2, l3, l4;
\r
114 portSHORT sError = pdFALSE;
\r
115 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
116 const portLONG lAnswer = ( ( portLONG ) 74565L + ( portLONG ) 1234567L ) * ( portLONG ) -918L;
\r
117 const portCHAR * const pcTaskStartMsg = "Integer math task 1 started.\r\n";
\r
118 const portCHAR * const pcTaskFailMsg = "Integer math task 1 failed.\r\n";
\r
120 /* Queue a message for printing to say the task has started. */
\r
121 vPrintDisplayMessage( &pcTaskStartMsg );
\r
123 /* The variable this task increments to show it is still running is passed in
\r
124 as the parameter. */
\r
125 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
127 /* Keep performing a calculation and checking the result against a constant. */
\r
130 l1 = ( portLONG ) 74565L;
\r
131 l2 = ( portLONG ) 1234567L;
\r
132 l3 = ( portLONG ) -918L;
\r
134 l4 = ( l1 + l2 ) * l3;
\r
138 /* If the calculation does not match the expected constant, stop the
\r
139 increment of the check variable. */
\r
140 if( l4 != lAnswer )
\r
142 vPrintDisplayMessage( &pcTaskFailMsg );
\r
146 if( sError == pdFALSE )
\r
148 /* If the calculation has always been correct, increment the check
\r
149 variable so we know this task is still running okay. */
\r
150 ( *pusTaskCheckVariable )++;
\r
154 /*-----------------------------------------------------------*/
\r
156 static void vCompeteingIntMathTask2( void *pvParameters )
\r
158 portLONG l1, l2, l3, l4;
\r
159 portSHORT sError = pdFALSE;
\r
160 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
161 const portLONG lAnswer = ( ( portLONG ) -389000L / ( portLONG ) 329999L ) * ( portLONG ) -89L;
\r
162 const portCHAR * const pcTaskStartMsg = "Integer math task 2 started.\r\n";
\r
163 const portCHAR * const pcTaskFailMsg = "Integer math task 2 failed.\r\n";
\r
165 /* Queue a message for printing to say the task has started. */
\r
166 vPrintDisplayMessage( &pcTaskStartMsg );
\r
168 /* The variable this task increments to show it is still running is passed in
\r
169 as the parameter. */
\r
170 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
172 /* Keep performing a calculation and checking the result against a constant. */
\r
179 l4 = ( l1 / l2 ) * l3;
\r
183 /* If the calculation does not match the expected constant, stop the
\r
184 increment of the check variable. */
\r
185 if( l4 != lAnswer )
\r
187 vPrintDisplayMessage( &pcTaskFailMsg );
\r
191 if( sError == pdFALSE )
\r
193 /* If the calculation has always been correct, increment the check
\r
194 variable so we know this task is still running okay. */
\r
195 ( *pusTaskCheckVariable )++;
\r
199 /*-----------------------------------------------------------*/
\r
201 static void vCompeteingIntMathTask3( void *pvParameters )
\r
203 portLONG *plArray, lTotal1, lTotal2;
\r
204 portSHORT sError = pdFALSE;
\r
205 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
206 const unsigned portSHORT usArraySize = ( unsigned portSHORT ) 250;
\r
207 unsigned portSHORT usPosition;
\r
208 const portCHAR * const pcTaskStartMsg = "Integer math task 3 started.\r\n";
\r
209 const portCHAR * const pcTaskFailMsg = "Integer math task 3 failed.\r\n";
\r
211 /* Queue a message for printing to say the task has started. */
\r
212 vPrintDisplayMessage( &pcTaskStartMsg );
\r
214 /* The variable this task increments to show it is still running is passed in
\r
215 as the parameter. */
\r
216 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
218 /* Create the array we are going to use for our check calculation. */
\r
219 plArray = ( portLONG * ) pvPortMalloc( ( size_t ) 250 * sizeof( portLONG ) );
\r
221 /* Keep filling the array, keeping a running total of the values placed in the
\r
222 array. Then run through the array adding up all the values. If the two totals
\r
223 do not match, stop the check variable from incrementing. */
\r
226 lTotal1 = ( portLONG ) 0;
\r
227 lTotal2 = ( portLONG ) 0;
\r
229 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
231 plArray[ usPosition ] = ( portLONG ) usPosition + ( portLONG ) 5;
\r
232 lTotal1 += ( portLONG ) usPosition + ( portLONG ) 5;
\r
237 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
239 lTotal2 += plArray[ usPosition ];
\r
242 if( lTotal1 != lTotal2 )
\r
244 vPrintDisplayMessage( &pcTaskFailMsg );
\r
250 if( sError == pdFALSE )
\r
252 /* If the calculation has always been correct, increment the check
\r
253 variable so we know this task is still running okay. */
\r
254 ( *pusTaskCheckVariable )++;
\r
258 /*-----------------------------------------------------------*/
\r
260 static void vCompeteingIntMathTask4( void *pvParameters )
\r
262 portLONG *plArray, lTotal1, lTotal2;
\r
263 portSHORT sError = pdFALSE;
\r
264 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
265 const unsigned portSHORT usArraySize = 250;
\r
266 unsigned portSHORT usPosition;
\r
267 const portCHAR * const pcTaskStartMsg = "Integer math task 4 started.\r\n";
\r
268 const portCHAR * const pcTaskFailMsg = "Integer math task 4 failed.\r\n";
\r
270 /* Queue a message for printing to say the task has started. */
\r
271 vPrintDisplayMessage( &pcTaskStartMsg );
\r
273 /* The variable this task increments to show it is still running is passed in
\r
274 as the parameter. */
\r
275 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
277 /* Create the array we are going to use for our check calculation. */
\r
278 plArray = ( portLONG * ) pvPortMalloc( ( size_t ) 250 * sizeof( portLONG ) );
\r
280 /* Keep filling the array, keeping a running total of the values placed in the
\r
281 array. Then run through the array adding up all the values. If the two totals
\r
282 do not match, stop the check variable from incrementing. */
\r
285 lTotal1 = ( portLONG ) 0;
\r
286 lTotal2 = ( portLONG ) 0;
\r
288 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
290 plArray[ usPosition ] = ( portLONG ) usPosition * ( portLONG ) 12;
\r
291 lTotal1 += ( portLONG ) usPosition * ( portLONG ) 12;
\r
296 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
298 lTotal2 += plArray[ usPosition ];
\r
302 if( lTotal1 != lTotal2 )
\r
304 vPrintDisplayMessage( &pcTaskFailMsg );
\r
310 if( sError == pdFALSE )
\r
312 /* If the calculation has always been correct, increment the check
\r
313 variable so we know this task is still running okay. */
\r
314 ( *pusTaskCheckVariable )++;
\r
318 /*-----------------------------------------------------------*/
\r
320 /* This is called to check that all the created tasks are still running. */
\r
321 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )
\r
323 /* Keep a history of the check variables so we know if they have been incremented
\r
324 since the last call. */
\r
325 static unsigned portSHORT usLastTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
326 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
328 /* Check the maths tasks are still running by ensuring their check variables
\r
329 are still incrementing. */
\r
330 for( xTask = 0; xTask < intgNUMBER_OF_TASKS; xTask++ )
\r
332 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
334 /* The check has not incremented so an error exists. */
\r
338 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r