2 FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
71 with the cooperative scheduler.
\r
75 * This does the same as flop. c, but uses variables of type long instead of
\r
78 * As with flop. c, the tasks created in this file are a good test of the
\r
79 * scheduler context switch mechanism. The processor has to access 32bit
\r
80 * variables in two or four chunks (depending on the processor). The low
\r
81 * priority of these tasks means there is a high probability that a context
\r
82 * switch will occur mid calculation. See the flop. c documentation for
\r
85 * \page IntegerC integer.c
\r
86 * \ingroup DemoFiles
\r
93 + The constants used in the calculations are larger to ensure the
\r
94 optimiser does not truncate them to 16 bits.
\r
99 /* Scheduler include files. */
\r
100 #include "FreeRTOS.h"
\r
104 /* Demo program include files. */
\r
105 #include "integer.h"
\r
107 #define intgSTACK_SIZE ( ( unsigned short ) 256 )
\r
108 #define intgNUMBER_OF_TASKS ( 8 )
\r
110 /* Four tasks, each of which performs a different calculation on four byte
\r
111 variables. Each of the four is created twice. */
\r
112 static void vCompeteingIntMathTask1( void *pvParameters );
\r
113 static void vCompeteingIntMathTask2( void *pvParameters );
\r
114 static void vCompeteingIntMathTask3( void *pvParameters );
\r
115 static void vCompeteingIntMathTask4( void *pvParameters );
\r
117 /* These variables are used to check that all the tasks are still running. If a
\r
118 task gets a calculation wrong it will stop incrementing its check variable. */
\r
119 static volatile unsigned short usTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
120 /*-----------------------------------------------------------*/
\r
122 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )
\r
124 xTaskCreate( vCompeteingIntMathTask1, "IntMath1", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
125 xTaskCreate( vCompeteingIntMathTask2, "IntMath2", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
126 xTaskCreate( vCompeteingIntMathTask3, "IntMath3", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
127 xTaskCreate( vCompeteingIntMathTask4, "IntMath4", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
128 xTaskCreate( vCompeteingIntMathTask1, "IntMath5", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
129 xTaskCreate( vCompeteingIntMathTask2, "IntMath6", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
130 xTaskCreate( vCompeteingIntMathTask3, "IntMath7", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
131 xTaskCreate( vCompeteingIntMathTask4, "IntMath8", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
133 /*-----------------------------------------------------------*/
\r
135 static void vCompeteingIntMathTask1( void *pvParameters )
\r
137 long l1, l2, l3, l4;
\r
138 short sError = pdFALSE;
\r
139 volatile unsigned short *pusTaskCheckVariable;
\r
140 const long lAnswer = ( ( long ) 74565L + ( long ) 1234567L ) * ( long ) -918L;
\r
141 const char * const pcTaskStartMsg = "Integer math task 1 started.\r\n";
\r
142 const char * const pcTaskFailMsg = "Integer math task 1 failed.\r\n";
\r
144 /* Queue a message for printing to say the task has started. */
\r
145 vPrintDisplayMessage( &pcTaskStartMsg );
\r
147 /* The variable this task increments to show it is still running is passed in
\r
148 as the parameter. */
\r
149 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
151 /* Keep performing a calculation and checking the result against a constant. */
\r
154 l1 = ( long ) 74565L;
\r
155 l2 = ( long ) 1234567L;
\r
156 l3 = ( long ) -918L;
\r
158 l4 = ( l1 + l2 ) * l3;
\r
162 /* If the calculation does not match the expected constant, stop the
\r
163 increment of the check variable. */
\r
164 if( l4 != lAnswer )
\r
166 vPrintDisplayMessage( &pcTaskFailMsg );
\r
170 if( sError == pdFALSE )
\r
172 /* If the calculation has always been correct, increment the check
\r
173 variable so we know this task is still running okay. */
\r
174 ( *pusTaskCheckVariable )++;
\r
178 /*-----------------------------------------------------------*/
\r
180 static void vCompeteingIntMathTask2( void *pvParameters )
\r
182 long l1, l2, l3, l4;
\r
183 short sError = pdFALSE;
\r
184 volatile unsigned short *pusTaskCheckVariable;
\r
185 const long lAnswer = ( ( long ) -389000L / ( long ) 329999L ) * ( long ) -89L;
\r
186 const char * const pcTaskStartMsg = "Integer math task 2 started.\r\n";
\r
187 const char * const pcTaskFailMsg = "Integer math task 2 failed.\r\n";
\r
189 /* Queue a message for printing to say the task has started. */
\r
190 vPrintDisplayMessage( &pcTaskStartMsg );
\r
192 /* The variable this task increments to show it is still running is passed in
\r
193 as the parameter. */
\r
194 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
196 /* Keep performing a calculation and checking the result against a constant. */
\r
203 l4 = ( l1 / l2 ) * l3;
\r
207 /* If the calculation does not match the expected constant, stop the
\r
208 increment of the check variable. */
\r
209 if( l4 != lAnswer )
\r
211 vPrintDisplayMessage( &pcTaskFailMsg );
\r
215 if( sError == pdFALSE )
\r
217 /* If the calculation has always been correct, increment the check
\r
218 variable so we know this task is still running okay. */
\r
219 ( *pusTaskCheckVariable )++;
\r
223 /*-----------------------------------------------------------*/
\r
225 static void vCompeteingIntMathTask3( void *pvParameters )
\r
227 long *plArray, lTotal1, lTotal2;
\r
228 short sError = pdFALSE;
\r
229 volatile unsigned short *pusTaskCheckVariable;
\r
230 const unsigned short usArraySize = ( unsigned short ) 250;
\r
231 unsigned short usPosition;
\r
232 const char * const pcTaskStartMsg = "Integer math task 3 started.\r\n";
\r
233 const char * const pcTaskFailMsg = "Integer math task 3 failed.\r\n";
\r
235 /* Queue a message for printing to say the task has started. */
\r
236 vPrintDisplayMessage( &pcTaskStartMsg );
\r
238 /* The variable this task increments to show it is still running is passed in
\r
239 as the parameter. */
\r
240 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
242 /* Create the array we are going to use for our check calculation. */
\r
243 plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );
\r
245 /* Keep filling the array, keeping a running total of the values placed in the
\r
246 array. Then run through the array adding up all the values. If the two totals
\r
247 do not match, stop the check variable from incrementing. */
\r
250 lTotal1 = ( long ) 0;
\r
251 lTotal2 = ( long ) 0;
\r
253 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
255 plArray[ usPosition ] = ( long ) usPosition + ( long ) 5;
\r
256 lTotal1 += ( long ) usPosition + ( long ) 5;
\r
261 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
263 lTotal2 += plArray[ usPosition ];
\r
266 if( lTotal1 != lTotal2 )
\r
268 vPrintDisplayMessage( &pcTaskFailMsg );
\r
274 if( sError == pdFALSE )
\r
276 /* If the calculation has always been correct, increment the check
\r
277 variable so we know this task is still running okay. */
\r
278 ( *pusTaskCheckVariable )++;
\r
282 /*-----------------------------------------------------------*/
\r
284 static void vCompeteingIntMathTask4( void *pvParameters )
\r
286 long *plArray, lTotal1, lTotal2;
\r
287 short sError = pdFALSE;
\r
288 volatile unsigned short *pusTaskCheckVariable;
\r
289 const unsigned short usArraySize = 250;
\r
290 unsigned short usPosition;
\r
291 const char * const pcTaskStartMsg = "Integer math task 4 started.\r\n";
\r
292 const char * const pcTaskFailMsg = "Integer math task 4 failed.\r\n";
\r
294 /* Queue a message for printing to say the task has started. */
\r
295 vPrintDisplayMessage( &pcTaskStartMsg );
\r
297 /* The variable this task increments to show it is still running is passed in
\r
298 as the parameter. */
\r
299 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
301 /* Create the array we are going to use for our check calculation. */
\r
302 plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );
\r
304 /* Keep filling the array, keeping a running total of the values placed in the
\r
305 array. Then run through the array adding up all the values. If the two totals
\r
306 do not match, stop the check variable from incrementing. */
\r
309 lTotal1 = ( long ) 0;
\r
310 lTotal2 = ( long ) 0;
\r
312 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
314 plArray[ usPosition ] = ( long ) usPosition * ( long ) 12;
\r
315 lTotal1 += ( long ) usPosition * ( long ) 12;
\r
320 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
322 lTotal2 += plArray[ usPosition ];
\r
326 if( lTotal1 != lTotal2 )
\r
328 vPrintDisplayMessage( &pcTaskFailMsg );
\r
334 if( sError == pdFALSE )
\r
336 /* If the calculation has always been correct, increment the check
\r
337 variable so we know this task is still running okay. */
\r
338 ( *pusTaskCheckVariable )++;
\r
342 /*-----------------------------------------------------------*/
\r
344 /* This is called to check that all the created tasks are still running. */
\r
345 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )
\r
347 /* Keep a history of the check variables so we know if they have been incremented
\r
348 since the last call. */
\r
349 static unsigned short usLastTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
350 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
352 /* Check the maths tasks are still running by ensuring their check variables
\r
353 are still incrementing. */
\r
354 for( xTask = 0; xTask < intgNUMBER_OF_TASKS; xTask++ )
\r
356 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
358 /* The check has not incremented so an error exists. */
\r
362 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r