FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Common / Full / integer.c
blobb78ab4f163c9f6ca9d2afdf8b3b7af4b5d581337
1 /*
2 FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.
4 ***************************************************************************
5 * *
6 * If you are: *
7 * *
8 * + New to FreeRTOS, *
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
10 * + Looking for basic training, *
11 * + Wanting to improve your FreeRTOS skills and productivity *
12 * *
13 * then take a look at the FreeRTOS eBook *
14 * *
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
16 * http://www.FreeRTOS.org/Documentation *
17 * *
18 * A pdf reference manual is also available. Both are usually delivered *
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
20 * and 8pm GMT (although please allow up to 24 hours in case of *
21 * exceptional circumstances). Thank you for your support! *
22 * *
23 ***************************************************************************
25 This file is part of the FreeRTOS distribution.
27 FreeRTOS is free software; you can redistribute it and/or modify it under
28 the terms of the GNU General Public License (version 2) as published by the
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
31 a combined work that includes FreeRTOS without being obliged to provide the
32 source code for proprietary components outside of the FreeRTOS kernel.
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
36 more details. You should have received a copy of the GNU General Public
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
39 by writing to Richard Barry, contact details for whom are available on the
40 FreeRTOS WEB site.
42 1 tab == 4 spaces!
44 http://www.FreeRTOS.org - Documentation, latest information, license and
45 contact details.
47 http://www.SafeRTOS.com - A version that is certified for use in safety
48 critical systems.
50 http://www.OpenRTOS.com - Commercial support, development, porting,
51 licensing and training services.
55 Changes from V1.2.3
57 + The created tasks now include calls to tskYIELD(), allowing them to be used
58 with the cooperative scheduler.
61 /**
62 * This does the same as flop. c, but uses variables of type long instead of
63 * type double.
65 * As with flop. c, the tasks created in this file are a good test of the
66 * scheduler context switch mechanism. The processor has to access 32bit
67 * variables in two or four chunks (depending on the processor). The low
68 * priority of these tasks means there is a high probability that a context
69 * switch will occur mid calculation. See the flop. c documentation for
70 * more information.
72 * \page IntegerC integer.c
73 * \ingroup DemoFiles
74 * <HR>
78 Changes from V1.2.1
80 + The constants used in the calculations are larger to ensure the
81 optimiser does not truncate them to 16 bits.
84 #include <stdlib.h>
86 /* Scheduler include files. */
87 #include "FreeRTOS.h"
88 #include "task.h"
89 #include "print.h"
91 /* Demo program include files. */
92 #include "integer.h"
94 #define intgSTACK_SIZE ( ( unsigned short ) 256 )
95 #define intgNUMBER_OF_TASKS ( 8 )
97 /* Four tasks, each of which performs a different calculation on four byte
98 variables. Each of the four is created twice. */
99 static void vCompeteingIntMathTask1( void *pvParameters );
100 static void vCompeteingIntMathTask2( void *pvParameters );
101 static void vCompeteingIntMathTask3( void *pvParameters );
102 static void vCompeteingIntMathTask4( void *pvParameters );
104 /* These variables are used to check that all the tasks are still running. If a
105 task gets a calculation wrong it will stop incrementing its check variable. */
106 static volatile unsigned short usTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
107 /*-----------------------------------------------------------*/
109 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )
111 xTaskCreate( vCompeteingIntMathTask1, "IntMath1", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
112 xTaskCreate( vCompeteingIntMathTask2, "IntMath2", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
113 xTaskCreate( vCompeteingIntMathTask3, "IntMath3", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
114 xTaskCreate( vCompeteingIntMathTask4, "IntMath4", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
115 xTaskCreate( vCompeteingIntMathTask1, "IntMath5", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
116 xTaskCreate( vCompeteingIntMathTask2, "IntMath6", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
117 xTaskCreate( vCompeteingIntMathTask3, "IntMath7", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
118 xTaskCreate( vCompeteingIntMathTask4, "IntMath8", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
120 /*-----------------------------------------------------------*/
122 static void vCompeteingIntMathTask1( void *pvParameters )
124 long l1, l2, l3, l4;
125 short sError = pdFALSE;
126 volatile unsigned short *pusTaskCheckVariable;
127 const long lAnswer = ( ( long ) 74565L + ( long ) 1234567L ) * ( long ) -918L;
128 const char * const pcTaskStartMsg = "Integer math task 1 started.\r\n";
129 const char * const pcTaskFailMsg = "Integer math task 1 failed.\r\n";
131 /* Queue a message for printing to say the task has started. */
132 vPrintDisplayMessage( &pcTaskStartMsg );
134 /* The variable this task increments to show it is still running is passed in
135 as the parameter. */
136 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
138 /* Keep performing a calculation and checking the result against a constant. */
139 for(;;)
141 l1 = ( long ) 74565L;
142 l2 = ( long ) 1234567L;
143 l3 = ( long ) -918L;
145 l4 = ( l1 + l2 ) * l3;
147 taskYIELD();
149 /* If the calculation does not match the expected constant, stop the
150 increment of the check variable. */
151 if( l4 != lAnswer )
153 vPrintDisplayMessage( &pcTaskFailMsg );
154 sError = pdTRUE;
157 if( sError == pdFALSE )
159 /* If the calculation has always been correct, increment the check
160 variable so we know this task is still running okay. */
161 ( *pusTaskCheckVariable )++;
165 /*-----------------------------------------------------------*/
167 static void vCompeteingIntMathTask2( void *pvParameters )
169 long l1, l2, l3, l4;
170 short sError = pdFALSE;
171 volatile unsigned short *pusTaskCheckVariable;
172 const long lAnswer = ( ( long ) -389000L / ( long ) 329999L ) * ( long ) -89L;
173 const char * const pcTaskStartMsg = "Integer math task 2 started.\r\n";
174 const char * const pcTaskFailMsg = "Integer math task 2 failed.\r\n";
176 /* Queue a message for printing to say the task has started. */
177 vPrintDisplayMessage( &pcTaskStartMsg );
179 /* The variable this task increments to show it is still running is passed in
180 as the parameter. */
181 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
183 /* Keep performing a calculation and checking the result against a constant. */
184 for( ;; )
186 l1 = -389000L;
187 l2 = 329999L;
188 l3 = -89L;
190 l4 = ( l1 / l2 ) * l3;
192 taskYIELD();
194 /* If the calculation does not match the expected constant, stop the
195 increment of the check variable. */
196 if( l4 != lAnswer )
198 vPrintDisplayMessage( &pcTaskFailMsg );
199 sError = pdTRUE;
202 if( sError == pdFALSE )
204 /* If the calculation has always been correct, increment the check
205 variable so we know this task is still running okay. */
206 ( *pusTaskCheckVariable )++;
210 /*-----------------------------------------------------------*/
212 static void vCompeteingIntMathTask3( void *pvParameters )
214 long *plArray, lTotal1, lTotal2;
215 short sError = pdFALSE;
216 volatile unsigned short *pusTaskCheckVariable;
217 const unsigned short usArraySize = ( unsigned short ) 250;
218 unsigned short usPosition;
219 const char * const pcTaskStartMsg = "Integer math task 3 started.\r\n";
220 const char * const pcTaskFailMsg = "Integer math task 3 failed.\r\n";
222 /* Queue a message for printing to say the task has started. */
223 vPrintDisplayMessage( &pcTaskStartMsg );
225 /* The variable this task increments to show it is still running is passed in
226 as the parameter. */
227 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
229 /* Create the array we are going to use for our check calculation. */
230 plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );
232 /* Keep filling the array, keeping a running total of the values placed in the
233 array. Then run through the array adding up all the values. If the two totals
234 do not match, stop the check variable from incrementing. */
235 for( ;; )
237 lTotal1 = ( long ) 0;
238 lTotal2 = ( long ) 0;
240 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
242 plArray[ usPosition ] = ( long ) usPosition + ( long ) 5;
243 lTotal1 += ( long ) usPosition + ( long ) 5;
246 taskYIELD();
248 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
250 lTotal2 += plArray[ usPosition ];
253 if( lTotal1 != lTotal2 )
255 vPrintDisplayMessage( &pcTaskFailMsg );
256 sError = pdTRUE;
259 taskYIELD();
261 if( sError == pdFALSE )
263 /* If the calculation has always been correct, increment the check
264 variable so we know this task is still running okay. */
265 ( *pusTaskCheckVariable )++;
269 /*-----------------------------------------------------------*/
271 static void vCompeteingIntMathTask4( void *pvParameters )
273 long *plArray, lTotal1, lTotal2;
274 short sError = pdFALSE;
275 volatile unsigned short *pusTaskCheckVariable;
276 const unsigned short usArraySize = 250;
277 unsigned short usPosition;
278 const char * const pcTaskStartMsg = "Integer math task 4 started.\r\n";
279 const char * const pcTaskFailMsg = "Integer math task 4 failed.\r\n";
281 /* Queue a message for printing to say the task has started. */
282 vPrintDisplayMessage( &pcTaskStartMsg );
284 /* The variable this task increments to show it is still running is passed in
285 as the parameter. */
286 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
288 /* Create the array we are going to use for our check calculation. */
289 plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );
291 /* Keep filling the array, keeping a running total of the values placed in the
292 array. Then run through the array adding up all the values. If the two totals
293 do not match, stop the check variable from incrementing. */
294 for( ;; )
296 lTotal1 = ( long ) 0;
297 lTotal2 = ( long ) 0;
299 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
301 plArray[ usPosition ] = ( long ) usPosition * ( long ) 12;
302 lTotal1 += ( long ) usPosition * ( long ) 12;
305 taskYIELD();
307 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
309 lTotal2 += plArray[ usPosition ];
313 if( lTotal1 != lTotal2 )
315 vPrintDisplayMessage( &pcTaskFailMsg );
316 sError = pdTRUE;
319 taskYIELD();
321 if( sError == pdFALSE )
323 /* If the calculation has always been correct, increment the check
324 variable so we know this task is still running okay. */
325 ( *pusTaskCheckVariable )++;
329 /*-----------------------------------------------------------*/
331 /* This is called to check that all the created tasks are still running. */
332 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )
334 /* Keep a history of the check variables so we know if they have been incremented
335 since the last call. */
336 static unsigned short usLastTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
337 portBASE_TYPE xReturn = pdTRUE, xTask;
339 /* Check the maths tasks are still running by ensuring their check variables
340 are still incrementing. */
341 for( xTask = 0; xTask < intgNUMBER_OF_TASKS; xTask++ )
343 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
345 /* The check has not incremented so an error exists. */
346 xReturn = pdFALSE;
349 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
352 return xReturn;