2 FreeRTOS.org V4.3.0 - Copyright (C) 2003-2007 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 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
39 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
40 with the cooperative scheduler.
\r
44 * This does the same as flop. c, but uses variables of type long instead of
\r
47 * As with flop. c, the tasks created in this file are a good test of the
\r
48 * scheduler context switch mechanism. The processor has to access 32bit
\r
49 * variables in two or four chunks (depending on the processor). The low
\r
50 * priority of these tasks means there is a high probability that a context
\r
51 * switch will occur mid calculation. See the flop. c documentation for
\r
54 * \page IntegerC integer.c
\r
55 * \ingroup DemoFiles
\r
62 + The constants used in the calculations are larger to ensure the
\r
63 optimiser does not truncate them to 16 bits.
\r
68 /* Scheduler include files. */
\r
69 #include "FreeRTOS.h"
\r
73 /* Demo program include files. */
\r
74 #include "integer.h"
\r
76 #define intgSTACK_SIZE ( ( unsigned portSHORT ) 256 )
\r
77 #define intgNUMBER_OF_TASKS ( 8 )
\r
79 /* Four tasks, each of which performs a different calculation on four byte
\r
80 variables. Each of the four is created twice. */
\r
81 static void vCompeteingIntMathTask1( void *pvParameters );
\r
82 static void vCompeteingIntMathTask2( void *pvParameters );
\r
83 static void vCompeteingIntMathTask3( void *pvParameters );
\r
84 static void vCompeteingIntMathTask4( void *pvParameters );
\r
86 /* These variables are used to check that all the tasks are still running. If a
\r
87 task gets a calculation wrong it will stop incrementing its check variable. */
\r
88 static volatile unsigned portSHORT usTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
89 /*-----------------------------------------------------------*/
\r
91 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )
\r
93 xTaskCreate( vCompeteingIntMathTask1, "IntMath1", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
94 xTaskCreate( vCompeteingIntMathTask2, "IntMath2", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
95 xTaskCreate( vCompeteingIntMathTask3, "IntMath3", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
96 xTaskCreate( vCompeteingIntMathTask4, "IntMath4", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
97 xTaskCreate( vCompeteingIntMathTask1, "IntMath5", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
98 xTaskCreate( vCompeteingIntMathTask2, "IntMath6", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
99 xTaskCreate( vCompeteingIntMathTask3, "IntMath7", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
100 xTaskCreate( vCompeteingIntMathTask4, "IntMath8", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
102 /*-----------------------------------------------------------*/
\r
104 static void vCompeteingIntMathTask1( void *pvParameters )
\r
106 portLONG l1, l2, l3, l4;
\r
107 portSHORT sError = pdFALSE;
\r
108 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
109 const portLONG lAnswer = ( ( portLONG ) 74565L + ( portLONG ) 1234567L ) * ( portLONG ) -918L;
\r
110 const portCHAR * const pcTaskStartMsg = "Integer math task 1 started.\r\n";
\r
111 const portCHAR * const pcTaskFailMsg = "Integer math task 1 failed.\r\n";
\r
113 /* Queue a message for printing to say the task has started. */
\r
114 vPrintDisplayMessage( &pcTaskStartMsg );
\r
116 /* The variable this task increments to show it is still running is passed in
\r
117 as the parameter. */
\r
118 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
120 /* Keep performing a calculation and checking the result against a constant. */
\r
123 l1 = ( portLONG ) 74565L;
\r
124 l2 = ( portLONG ) 1234567L;
\r
125 l3 = ( portLONG ) -918L;
\r
127 l4 = ( l1 + l2 ) * l3;
\r
131 /* If the calculation does not match the expected constant, stop the
\r
132 increment of the check variable. */
\r
133 if( l4 != lAnswer )
\r
135 vPrintDisplayMessage( &pcTaskFailMsg );
\r
139 if( sError == pdFALSE )
\r
141 /* If the calculation has always been correct, increment the check
\r
142 variable so we know this task is still running okay. */
\r
143 ( *pusTaskCheckVariable )++;
\r
147 /*-----------------------------------------------------------*/
\r
149 static void vCompeteingIntMathTask2( void *pvParameters )
\r
151 portLONG l1, l2, l3, l4;
\r
152 portSHORT sError = pdFALSE;
\r
153 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
154 const portLONG lAnswer = ( ( portLONG ) -389000L / ( portLONG ) 329999L ) * ( portLONG ) -89L;
\r
155 const portCHAR * const pcTaskStartMsg = "Integer math task 2 started.\r\n";
\r
156 const portCHAR * const pcTaskFailMsg = "Integer math task 2 failed.\r\n";
\r
158 /* Queue a message for printing to say the task has started. */
\r
159 vPrintDisplayMessage( &pcTaskStartMsg );
\r
161 /* The variable this task increments to show it is still running is passed in
\r
162 as the parameter. */
\r
163 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
165 /* Keep performing a calculation and checking the result against a constant. */
\r
172 l4 = ( l1 / l2 ) * l3;
\r
176 /* If the calculation does not match the expected constant, stop the
\r
177 increment of the check variable. */
\r
178 if( l4 != lAnswer )
\r
180 vPrintDisplayMessage( &pcTaskFailMsg );
\r
184 if( sError == pdFALSE )
\r
186 /* If the calculation has always been correct, increment the check
\r
187 variable so we know this task is still running okay. */
\r
188 ( *pusTaskCheckVariable )++;
\r
192 /*-----------------------------------------------------------*/
\r
194 static void vCompeteingIntMathTask3( void *pvParameters )
\r
196 portLONG *plArray, lTotal1, lTotal2;
\r
197 portSHORT sError = pdFALSE;
\r
198 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
199 const unsigned portSHORT usArraySize = ( unsigned portSHORT ) 250;
\r
200 unsigned portSHORT usPosition;
\r
201 const portCHAR * const pcTaskStartMsg = "Integer math task 3 started.\r\n";
\r
202 const portCHAR * const pcTaskFailMsg = "Integer math task 3 failed.\r\n";
\r
204 /* Queue a message for printing to say the task has started. */
\r
205 vPrintDisplayMessage( &pcTaskStartMsg );
\r
207 /* The variable this task increments to show it is still running is passed in
\r
208 as the parameter. */
\r
209 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
211 /* Create the array we are going to use for our check calculation. */
\r
212 plArray = ( portLONG * ) pvPortMalloc( ( size_t ) 250 * sizeof( portLONG ) );
\r
214 /* Keep filling the array, keeping a running total of the values placed in the
\r
215 array. Then run through the array adding up all the values. If the two totals
\r
216 do not match, stop the check variable from incrementing. */
\r
219 lTotal1 = ( portLONG ) 0;
\r
220 lTotal2 = ( portLONG ) 0;
\r
222 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
224 plArray[ usPosition ] = ( portLONG ) usPosition + ( portLONG ) 5;
\r
225 lTotal1 += ( portLONG ) usPosition + ( portLONG ) 5;
\r
230 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
232 lTotal2 += plArray[ usPosition ];
\r
235 if( lTotal1 != lTotal2 )
\r
237 vPrintDisplayMessage( &pcTaskFailMsg );
\r
243 if( sError == pdFALSE )
\r
245 /* If the calculation has always been correct, increment the check
\r
246 variable so we know this task is still running okay. */
\r
247 ( *pusTaskCheckVariable )++;
\r
251 /*-----------------------------------------------------------*/
\r
253 static void vCompeteingIntMathTask4( void *pvParameters )
\r
255 portLONG *plArray, lTotal1, lTotal2;
\r
256 portSHORT sError = pdFALSE;
\r
257 volatile unsigned portSHORT *pusTaskCheckVariable;
\r
258 const unsigned portSHORT usArraySize = 250;
\r
259 unsigned portSHORT usPosition;
\r
260 const portCHAR * const pcTaskStartMsg = "Integer math task 4 started.\r\n";
\r
261 const portCHAR * const pcTaskFailMsg = "Integer math task 4 failed.\r\n";
\r
263 /* Queue a message for printing to say the task has started. */
\r
264 vPrintDisplayMessage( &pcTaskStartMsg );
\r
266 /* The variable this task increments to show it is still running is passed in
\r
267 as the parameter. */
\r
268 pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;
\r
270 /* Create the array we are going to use for our check calculation. */
\r
271 plArray = ( portLONG * ) pvPortMalloc( ( size_t ) 250 * sizeof( portLONG ) );
\r
273 /* Keep filling the array, keeping a running total of the values placed in the
\r
274 array. Then run through the array adding up all the values. If the two totals
\r
275 do not match, stop the check variable from incrementing. */
\r
278 lTotal1 = ( portLONG ) 0;
\r
279 lTotal2 = ( portLONG ) 0;
\r
281 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
283 plArray[ usPosition ] = ( portLONG ) usPosition * ( portLONG ) 12;
\r
284 lTotal1 += ( portLONG ) usPosition * ( portLONG ) 12;
\r
289 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
291 lTotal2 += plArray[ usPosition ];
\r
295 if( lTotal1 != lTotal2 )
\r
297 vPrintDisplayMessage( &pcTaskFailMsg );
\r
303 if( sError == pdFALSE )
\r
305 /* If the calculation has always been correct, increment the check
\r
306 variable so we know this task is still running okay. */
\r
307 ( *pusTaskCheckVariable )++;
\r
311 /*-----------------------------------------------------------*/
\r
313 /* This is called to check that all the created tasks are still running. */
\r
314 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )
\r
316 /* Keep a history of the check variables so we know if they have been incremented
\r
317 since the last call. */
\r
318 static unsigned portSHORT usLastTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };
\r
319 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
321 /* Check the maths tasks are still running by ensuring their check variables
\r
322 are still incrementing. */
\r
323 for( xTask = 0; xTask < intgNUMBER_OF_TASKS; xTask++ )
\r
325 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
327 /* The check has not incremented so an error exists. */
\r
331 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r