2 FreeRTOS V7.0.1 - Copyright (C) 2011 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 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
57 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
58 with the cooperative scheduler.
\r
62 * This does the same as flop. c, but uses variables of type long instead of
\r
65 * As with flop. c, the tasks created in this file are a good test of the
\r
66 * scheduler context switch mechanism. The processor has to access 32bit
\r
67 * variables in two or four chunks (depending on the processor). The low
\r
68 * priority of these tasks means there is a high probability that a context
\r
69 * switch will occur mid calculation. See the flop. c documentation for
\r
72 * \page IntegerC integer.c
\r
73 * \ingroup DemoFiles
\r
80 + The constants used in the calculations are larger to ensure the
\r
81 optimiser does not truncate them to 16 bits.
\r
86 /* Scheduler include files. */
\r
87 #include "FreeRTOS.h"
\r
91 /* Demo program include files. */
\r
92 #include "integer.h"
\r
94 #define intgSTACK_SIZE ( ( unsigned short ) 256 )
\r
95 #define intgNUMBER_OF_TASKS ( 8 )
\r
97 /* Four tasks, each of which performs a different calculation on four byte
\r
98 variables. Each of the four is created twice. */
\r
99 static void vCompeteingIntMathTask1( void *pvParameters );
\r
100 static void vCompeteingIntMathTask2( void *pvParameters );
\r
101 static void vCompeteingIntMathTask3( void *pvParameters );
\r
102 static void vCompeteingIntMathTask4( void *pvParameters );
\r
104 /* These variables are used to check that all the tasks are still running. If a
\r
105 task gets a calculation wrong it will stop incrementing its check variable. */
\r
106 static volatile unsigned short usTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
107 /*-----------------------------------------------------------*/
\r
109 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )
\r
111 xTaskCreate( vCompeteingIntMathTask1, "IntMath1", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
112 xTaskCreate( vCompeteingIntMathTask2, "IntMath2", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
113 xTaskCreate( vCompeteingIntMathTask3, "IntMath3", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
114 xTaskCreate( vCompeteingIntMathTask4, "IntMath4", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
115 xTaskCreate( vCompeteingIntMathTask1, "IntMath5", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
116 xTaskCreate( vCompeteingIntMathTask2, "IntMath6", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
117 xTaskCreate( vCompeteingIntMathTask3, "IntMath7", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
118 xTaskCreate( vCompeteingIntMathTask4, "IntMath8", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
120 /*-----------------------------------------------------------*/
\r
122 static void vCompeteingIntMathTask1( void *pvParameters )
\r
124 long l1, l2, l3, l4;
\r
125 short sError = pdFALSE;
\r
126 volatile unsigned short *pusTaskCheckVariable;
\r
127 const long lAnswer = ( ( long ) 74565L + ( long ) 1234567L ) * ( long ) -918L;
\r
128 const char * const pcTaskStartMsg = "Integer math task 1 started.\r\n";
\r
129 const char * const pcTaskFailMsg = "Integer math task 1 failed.\r\n";
\r
131 /* Queue a message for printing to say the task has started. */
\r
132 vPrintDisplayMessage( &pcTaskStartMsg );
\r
134 /* The variable this task increments to show it is still running is passed in
\r
135 as the parameter. */
\r
136 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
138 /* Keep performing a calculation and checking the result against a constant. */
\r
141 l1 = ( long ) 74565L;
\r
142 l2 = ( long ) 1234567L;
\r
143 l3 = ( long ) -918L;
\r
145 l4 = ( l1 + l2 ) * l3;
\r
149 /* If the calculation does not match the expected constant, stop the
\r
150 increment of the check variable. */
\r
151 if( l4 != lAnswer )
\r
153 vPrintDisplayMessage( &pcTaskFailMsg );
\r
157 if( sError == pdFALSE )
\r
159 /* If the calculation has always been correct, increment the check
\r
160 variable so we know this task is still running okay. */
\r
161 ( *pusTaskCheckVariable )++;
\r
165 /*-----------------------------------------------------------*/
\r
167 static void vCompeteingIntMathTask2( void *pvParameters )
\r
169 long l1, l2, l3, l4;
\r
170 short sError = pdFALSE;
\r
171 volatile unsigned short *pusTaskCheckVariable;
\r
172 const long lAnswer = ( ( long ) -389000L / ( long ) 329999L ) * ( long ) -89L;
\r
173 const char * const pcTaskStartMsg = "Integer math task 2 started.\r\n";
\r
174 const char * const pcTaskFailMsg = "Integer math task 2 failed.\r\n";
\r
176 /* Queue a message for printing to say the task has started. */
\r
177 vPrintDisplayMessage( &pcTaskStartMsg );
\r
179 /* The variable this task increments to show it is still running is passed in
\r
180 as the parameter. */
\r
181 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
183 /* Keep performing a calculation and checking the result against a constant. */
\r
190 l4 = ( l1 / l2 ) * l3;
\r
194 /* If the calculation does not match the expected constant, stop the
\r
195 increment of the check variable. */
\r
196 if( l4 != lAnswer )
\r
198 vPrintDisplayMessage( &pcTaskFailMsg );
\r
202 if( sError == pdFALSE )
\r
204 /* If the calculation has always been correct, increment the check
\r
205 variable so we know this task is still running okay. */
\r
206 ( *pusTaskCheckVariable )++;
\r
210 /*-----------------------------------------------------------*/
\r
212 static void vCompeteingIntMathTask3( void *pvParameters )
\r
214 long *plArray, lTotal1, lTotal2;
\r
215 short sError = pdFALSE;
\r
216 volatile unsigned short *pusTaskCheckVariable;
\r
217 const unsigned short usArraySize = ( unsigned short ) 250;
\r
218 unsigned short usPosition;
\r
219 const char * const pcTaskStartMsg = "Integer math task 3 started.\r\n";
\r
220 const char * const pcTaskFailMsg = "Integer math task 3 failed.\r\n";
\r
222 /* Queue a message for printing to say the task has started. */
\r
223 vPrintDisplayMessage( &pcTaskStartMsg );
\r
225 /* The variable this task increments to show it is still running is passed in
\r
226 as the parameter. */
\r
227 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
229 /* Create the array we are going to use for our check calculation. */
\r
230 plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );
\r
232 /* Keep filling the array, keeping a running total of the values placed in the
\r
233 array. Then run through the array adding up all the values. If the two totals
\r
234 do not match, stop the check variable from incrementing. */
\r
237 lTotal1 = ( long ) 0;
\r
238 lTotal2 = ( long ) 0;
\r
240 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
242 plArray[ usPosition ] = ( long ) usPosition + ( long ) 5;
\r
243 lTotal1 += ( long ) usPosition + ( long ) 5;
\r
248 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
250 lTotal2 += plArray[ usPosition ];
\r
253 if( lTotal1 != lTotal2 )
\r
255 vPrintDisplayMessage( &pcTaskFailMsg );
\r
261 if( sError == pdFALSE )
\r
263 /* If the calculation has always been correct, increment the check
\r
264 variable so we know this task is still running okay. */
\r
265 ( *pusTaskCheckVariable )++;
\r
269 /*-----------------------------------------------------------*/
\r
271 static void vCompeteingIntMathTask4( void *pvParameters )
\r
273 long *plArray, lTotal1, lTotal2;
\r
274 short sError = pdFALSE;
\r
275 volatile unsigned short *pusTaskCheckVariable;
\r
276 const unsigned short usArraySize = 250;
\r
277 unsigned short usPosition;
\r
278 const char * const pcTaskStartMsg = "Integer math task 4 started.\r\n";
\r
279 const char * const pcTaskFailMsg = "Integer math task 4 failed.\r\n";
\r
281 /* Queue a message for printing to say the task has started. */
\r
282 vPrintDisplayMessage( &pcTaskStartMsg );
\r
284 /* The variable this task increments to show it is still running is passed in
\r
285 as the parameter. */
\r
286 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
288 /* Create the array we are going to use for our check calculation. */
\r
289 plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );
\r
291 /* Keep filling the array, keeping a running total of the values placed in the
\r
292 array. Then run through the array adding up all the values. If the two totals
\r
293 do not match, stop the check variable from incrementing. */
\r
296 lTotal1 = ( long ) 0;
\r
297 lTotal2 = ( long ) 0;
\r
299 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
301 plArray[ usPosition ] = ( long ) usPosition * ( long ) 12;
\r
302 lTotal1 += ( long ) usPosition * ( long ) 12;
\r
307 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
309 lTotal2 += plArray[ usPosition ];
\r
313 if( lTotal1 != lTotal2 )
\r
315 vPrintDisplayMessage( &pcTaskFailMsg );
\r
321 if( sError == pdFALSE )
\r
323 /* If the calculation has always been correct, increment the check
\r
324 variable so we know this task is still running okay. */
\r
325 ( *pusTaskCheckVariable )++;
\r
329 /*-----------------------------------------------------------*/
\r
331 /* This is called to check that all the created tasks are still running. */
\r
332 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )
\r
334 /* Keep a history of the check variables so we know if they have been incremented
\r
335 since the last call. */
\r
336 static unsigned short usLastTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
337 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
339 /* Check the maths tasks are still running by ensuring their check variables
\r
340 are still incrementing. */
\r
341 for( xTask = 0; xTask < intgNUMBER_OF_TASKS; xTask++ )
\r
343 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
345 /* The check has not incremented so an error exists. */
\r
349 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r