2 FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
10 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
15 You should have received a copy of the GNU General Public License along
\r
16 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
17 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
19 A special exception to the GPL is included to allow you to distribute a
\r
20 combined work that includes FreeRTOS.org without being obliged to provide
\r
21 the source code for any proprietary components. See the licensing section
\r
22 of http://www.FreeRTOS.org for full details.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
55 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
56 with the cooperative scheduler.
\r
60 * This does the same as flop. c, but uses variables of type long instead of
\r
63 * As with flop. c, the tasks created in this file are a good test of the
\r
64 * scheduler context switch mechanism. The processor has to access 32bit
\r
65 * variables in two or four chunks (depending on the processor). The low
\r
66 * priority of these tasks means there is a high probability that a context
\r
67 * switch will occur mid calculation. See the flop. c documentation for
\r
70 * \page IntegerC integer.c
\r
71 * \ingroup DemoFiles
\r
78 + The constants used in the calculations are larger to ensure the
\r
79 optimiser does not truncate them to 16 bits.
\r
84 /* Scheduler include files. */
\r
85 #include "FreeRTOS.h"
\r
89 /* Demo program include files. */
\r
90 #include "integer.h"
\r
92 #define intgSTACK_SIZE ( ( unsigned portSHORT ) 256 )
\r
93 #define intgNUMBER_OF_TASKS ( 8 )
\r
95 /* Four tasks, each of which performs a different calculation on four byte
\r
96 variables. Each of the four is created twice. */
\r
97 static void vCompeteingIntMathTask1( void *pvParameters );
\r
98 static void vCompeteingIntMathTask2( void *pvParameters );
\r
99 static void vCompeteingIntMathTask3( void *pvParameters );
\r
100 static void vCompeteingIntMathTask4( void *pvParameters );
\r
102 /* These variables are used to check that all the tasks are still running. If a
\r
103 task gets a calculation wrong it will stop incrementing its check variable. */
\r
104 static volatile unsigned portSHORT usTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
105 /*-----------------------------------------------------------*/
\r
107 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )
\r
109 xTaskCreate( vCompeteingIntMathTask1, "IntMath1", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
110 xTaskCreate( vCompeteingIntMathTask2, "IntMath2", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
111 xTaskCreate( vCompeteingIntMathTask3, "IntMath3", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
112 xTaskCreate( vCompeteingIntMathTask4, "IntMath4", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
113 xTaskCreate( vCompeteingIntMathTask1, "IntMath5", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
114 xTaskCreate( vCompeteingIntMathTask2, "IntMath6", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
115 xTaskCreate( vCompeteingIntMathTask3, "IntMath7", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
116 xTaskCreate( vCompeteingIntMathTask4, "IntMath8", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
118 /*-----------------------------------------------------------*/
\r
120 static void vCompeteingIntMathTask1( void *pvParameters )
\r
122 portLONG l1, l2, l3, l4;
\r
123 portSHORT sError = pdFALSE;
\r
124 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
125 const portLONG lAnswer = ( ( portLONG ) 74565L + ( portLONG ) 1234567L ) * ( portLONG ) -918L;
\r
126 const portCHAR * const pcTaskStartMsg = "Integer math task 1 started.\r\n";
\r
127 const portCHAR * const pcTaskFailMsg = "Integer math task 1 failed.\r\n";
\r
129 /* Queue a message for printing to say the task has started. */
\r
130 vPrintDisplayMessage( &pcTaskStartMsg );
\r
132 /* The variable this task increments to show it is still running is passed in
\r
133 as the parameter. */
\r
134 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
136 /* Keep performing a calculation and checking the result against a constant. */
\r
139 l1 = ( portLONG ) 74565L;
\r
140 l2 = ( portLONG ) 1234567L;
\r
141 l3 = ( portLONG ) -918L;
\r
143 l4 = ( l1 + l2 ) * l3;
\r
147 /* If the calculation does not match the expected constant, stop the
\r
148 increment of the check variable. */
\r
149 if( l4 != lAnswer )
\r
151 vPrintDisplayMessage( &pcTaskFailMsg );
\r
155 if( sError == pdFALSE )
\r
157 /* If the calculation has always been correct, increment the check
\r
158 variable so we know this task is still running okay. */
\r
159 ( *pusTaskCheckVariable )++;
\r
163 /*-----------------------------------------------------------*/
\r
165 static void vCompeteingIntMathTask2( void *pvParameters )
\r
167 portLONG l1, l2, l3, l4;
\r
168 portSHORT sError = pdFALSE;
\r
169 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
170 const portLONG lAnswer = ( ( portLONG ) -389000L / ( portLONG ) 329999L ) * ( portLONG ) -89L;
\r
171 const portCHAR * const pcTaskStartMsg = "Integer math task 2 started.\r\n";
\r
172 const portCHAR * const pcTaskFailMsg = "Integer math task 2 failed.\r\n";
\r
174 /* Queue a message for printing to say the task has started. */
\r
175 vPrintDisplayMessage( &pcTaskStartMsg );
\r
177 /* The variable this task increments to show it is still running is passed in
\r
178 as the parameter. */
\r
179 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
181 /* Keep performing a calculation and checking the result against a constant. */
\r
188 l4 = ( l1 / l2 ) * l3;
\r
192 /* If the calculation does not match the expected constant, stop the
\r
193 increment of the check variable. */
\r
194 if( l4 != lAnswer )
\r
196 vPrintDisplayMessage( &pcTaskFailMsg );
\r
200 if( sError == pdFALSE )
\r
202 /* If the calculation has always been correct, increment the check
\r
203 variable so we know this task is still running okay. */
\r
204 ( *pusTaskCheckVariable )++;
\r
208 /*-----------------------------------------------------------*/
\r
210 static void vCompeteingIntMathTask3( void *pvParameters )
\r
212 portLONG *plArray, lTotal1, lTotal2;
\r
213 portSHORT sError = pdFALSE;
\r
214 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
215 const unsigned portSHORT usArraySize = ( unsigned portSHORT ) 250;
\r
216 unsigned portSHORT usPosition;
\r
217 const portCHAR * const pcTaskStartMsg = "Integer math task 3 started.\r\n";
\r
218 const portCHAR * const pcTaskFailMsg = "Integer math task 3 failed.\r\n";
\r
220 /* Queue a message for printing to say the task has started. */
\r
221 vPrintDisplayMessage( &pcTaskStartMsg );
\r
223 /* The variable this task increments to show it is still running is passed in
\r
224 as the parameter. */
\r
225 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
227 /* Create the array we are going to use for our check calculation. */
\r
228 plArray = ( portLONG * ) pvPortMalloc( ( size_t ) 250 * sizeof( portLONG ) );
\r
230 /* Keep filling the array, keeping a running total of the values placed in the
\r
231 array. Then run through the array adding up all the values. If the two totals
\r
232 do not match, stop the check variable from incrementing. */
\r
235 lTotal1 = ( portLONG ) 0;
\r
236 lTotal2 = ( portLONG ) 0;
\r
238 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
240 plArray[ usPosition ] = ( portLONG ) usPosition + ( portLONG ) 5;
\r
241 lTotal1 += ( portLONG ) usPosition + ( portLONG ) 5;
\r
246 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
248 lTotal2 += plArray[ usPosition ];
\r
251 if( lTotal1 != lTotal2 )
\r
253 vPrintDisplayMessage( &pcTaskFailMsg );
\r
259 if( sError == pdFALSE )
\r
261 /* If the calculation has always been correct, increment the check
\r
262 variable so we know this task is still running okay. */
\r
263 ( *pusTaskCheckVariable )++;
\r
267 /*-----------------------------------------------------------*/
\r
269 static void vCompeteingIntMathTask4( void *pvParameters )
\r
271 portLONG *plArray, lTotal1, lTotal2;
\r
272 portSHORT sError = pdFALSE;
\r
273 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
274 const unsigned portSHORT usArraySize = 250;
\r
275 unsigned portSHORT usPosition;
\r
276 const portCHAR * const pcTaskStartMsg = "Integer math task 4 started.\r\n";
\r
277 const portCHAR * const pcTaskFailMsg = "Integer math task 4 failed.\r\n";
\r
279 /* Queue a message for printing to say the task has started. */
\r
280 vPrintDisplayMessage( &pcTaskStartMsg );
\r
282 /* The variable this task increments to show it is still running is passed in
\r
283 as the parameter. */
\r
284 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
286 /* Create the array we are going to use for our check calculation. */
\r
287 plArray = ( portLONG * ) pvPortMalloc( ( size_t ) 250 * sizeof( portLONG ) );
\r
289 /* Keep filling the array, keeping a running total of the values placed in the
\r
290 array. Then run through the array adding up all the values. If the two totals
\r
291 do not match, stop the check variable from incrementing. */
\r
294 lTotal1 = ( portLONG ) 0;
\r
295 lTotal2 = ( portLONG ) 0;
\r
297 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
299 plArray[ usPosition ] = ( portLONG ) usPosition * ( portLONG ) 12;
\r
300 lTotal1 += ( portLONG ) usPosition * ( portLONG ) 12;
\r
305 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
307 lTotal2 += plArray[ usPosition ];
\r
311 if( lTotal1 != lTotal2 )
\r
313 vPrintDisplayMessage( &pcTaskFailMsg );
\r
319 if( sError == pdFALSE )
\r
321 /* If the calculation has always been correct, increment the check
\r
322 variable so we know this task is still running okay. */
\r
323 ( *pusTaskCheckVariable )++;
\r
327 /*-----------------------------------------------------------*/
\r
329 /* This is called to check that all the created tasks are still running. */
\r
330 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )
\r
332 /* Keep a history of the check variables so we know if they have been incremented
\r
333 since the last call. */
\r
334 static unsigned portSHORT usLastTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
335 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
337 /* Check the maths tasks are still running by ensuring their check variables
\r
338 are still incrementing. */
\r
339 for( xTask = 0; xTask < intgNUMBER_OF_TASKS; xTask++ )
\r
341 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
343 /* The check has not incremented so an error exists. */
\r
347 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r