2 FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
69 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
70 with the cooperative scheduler.
\r
74 * This does the same as flop. c, but uses variables of type long instead of
\r
77 * As with flop. c, the tasks created in this file are a good test of the
\r
78 * scheduler context switch mechanism. The processor has to access 32bit
\r
79 * variables in two or four chunks (depending on the processor). The low
\r
80 * priority of these tasks means there is a high probability that a context
\r
81 * switch will occur mid calculation. See the flop. c documentation for
\r
84 * \page IntegerC integer.c
\r
85 * \ingroup DemoFiles
\r
92 + The constants used in the calculations are larger to ensure the
\r
93 optimiser does not truncate them to 16 bits.
\r
98 /* Scheduler include files. */
\r
99 #include "FreeRTOS.h"
\r
103 /* Demo program include files. */
\r
104 #include "integer.h"
\r
106 #define intgSTACK_SIZE ( ( unsigned short ) 256 )
\r
107 #define intgNUMBER_OF_TASKS ( 8 )
\r
109 /* Four tasks, each of which performs a different calculation on four byte
\r
110 variables. Each of the four is created twice. */
\r
111 static void vCompeteingIntMathTask1( void *pvParameters );
\r
112 static void vCompeteingIntMathTask2( void *pvParameters );
\r
113 static void vCompeteingIntMathTask3( void *pvParameters );
\r
114 static void vCompeteingIntMathTask4( void *pvParameters );
\r
116 /* These variables are used to check that all the tasks are still running. If a
\r
117 task gets a calculation wrong it will stop incrementing its check variable. */
\r
118 static volatile unsigned short usTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
119 /*-----------------------------------------------------------*/
\r
121 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )
\r
123 xTaskCreate( vCompeteingIntMathTask1, "IntMath1", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
124 xTaskCreate( vCompeteingIntMathTask2, "IntMath2", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
125 xTaskCreate( vCompeteingIntMathTask3, "IntMath3", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
126 xTaskCreate( vCompeteingIntMathTask4, "IntMath4", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
127 xTaskCreate( vCompeteingIntMathTask1, "IntMath5", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
128 xTaskCreate( vCompeteingIntMathTask2, "IntMath6", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
129 xTaskCreate( vCompeteingIntMathTask3, "IntMath7", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
130 xTaskCreate( vCompeteingIntMathTask4, "IntMath8", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
132 /*-----------------------------------------------------------*/
\r
134 static void vCompeteingIntMathTask1( void *pvParameters )
\r
136 long l1, l2, l3, l4;
\r
137 short sError = pdFALSE;
\r
138 volatile unsigned short *pusTaskCheckVariable;
\r
139 const long lAnswer = ( ( long ) 74565L + ( long ) 1234567L ) * ( long ) -918L;
\r
140 const char * const pcTaskStartMsg = "Integer math task 1 started.\r\n";
\r
141 const char * const pcTaskFailMsg = "Integer math task 1 failed.\r\n";
\r
143 /* Queue a message for printing to say the task has started. */
\r
144 vPrintDisplayMessage( &pcTaskStartMsg );
\r
146 /* The variable this task increments to show it is still running is passed in
\r
147 as the parameter. */
\r
148 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
150 /* Keep performing a calculation and checking the result against a constant. */
\r
153 l1 = ( long ) 74565L;
\r
154 l2 = ( long ) 1234567L;
\r
155 l3 = ( long ) -918L;
\r
157 l4 = ( l1 + l2 ) * l3;
\r
161 /* If the calculation does not match the expected constant, stop the
\r
162 increment of the check variable. */
\r
163 if( l4 != lAnswer )
\r
165 vPrintDisplayMessage( &pcTaskFailMsg );
\r
169 if( sError == pdFALSE )
\r
171 /* If the calculation has always been correct, increment the check
\r
172 variable so we know this task is still running okay. */
\r
173 ( *pusTaskCheckVariable )++;
\r
177 /*-----------------------------------------------------------*/
\r
179 static void vCompeteingIntMathTask2( void *pvParameters )
\r
181 long l1, l2, l3, l4;
\r
182 short sError = pdFALSE;
\r
183 volatile unsigned short *pusTaskCheckVariable;
\r
184 const long lAnswer = ( ( long ) -389000L / ( long ) 329999L ) * ( long ) -89L;
\r
185 const char * const pcTaskStartMsg = "Integer math task 2 started.\r\n";
\r
186 const char * const pcTaskFailMsg = "Integer math task 2 failed.\r\n";
\r
188 /* Queue a message for printing to say the task has started. */
\r
189 vPrintDisplayMessage( &pcTaskStartMsg );
\r
191 /* The variable this task increments to show it is still running is passed in
\r
192 as the parameter. */
\r
193 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
195 /* Keep performing a calculation and checking the result against a constant. */
\r
202 l4 = ( l1 / l2 ) * l3;
\r
206 /* If the calculation does not match the expected constant, stop the
\r
207 increment of the check variable. */
\r
208 if( l4 != lAnswer )
\r
210 vPrintDisplayMessage( &pcTaskFailMsg );
\r
214 if( sError == pdFALSE )
\r
216 /* If the calculation has always been correct, increment the check
\r
217 variable so we know this task is still running okay. */
\r
218 ( *pusTaskCheckVariable )++;
\r
222 /*-----------------------------------------------------------*/
\r
224 static void vCompeteingIntMathTask3( void *pvParameters )
\r
226 long *plArray, lTotal1, lTotal2;
\r
227 short sError = pdFALSE;
\r
228 volatile unsigned short *pusTaskCheckVariable;
\r
229 const unsigned short usArraySize = ( unsigned short ) 250;
\r
230 unsigned short usPosition;
\r
231 const char * const pcTaskStartMsg = "Integer math task 3 started.\r\n";
\r
232 const char * const pcTaskFailMsg = "Integer math task 3 failed.\r\n";
\r
234 /* Queue a message for printing to say the task has started. */
\r
235 vPrintDisplayMessage( &pcTaskStartMsg );
\r
237 /* The variable this task increments to show it is still running is passed in
\r
238 as the parameter. */
\r
239 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
241 /* Create the array we are going to use for our check calculation. */
\r
242 plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );
\r
244 /* Keep filling the array, keeping a running total of the values placed in the
\r
245 array. Then run through the array adding up all the values. If the two totals
\r
246 do not match, stop the check variable from incrementing. */
\r
249 lTotal1 = ( long ) 0;
\r
250 lTotal2 = ( long ) 0;
\r
252 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
254 plArray[ usPosition ] = ( long ) usPosition + ( long ) 5;
\r
255 lTotal1 += ( long ) usPosition + ( long ) 5;
\r
260 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
262 lTotal2 += plArray[ usPosition ];
\r
265 if( lTotal1 != lTotal2 )
\r
267 vPrintDisplayMessage( &pcTaskFailMsg );
\r
273 if( sError == pdFALSE )
\r
275 /* If the calculation has always been correct, increment the check
\r
276 variable so we know this task is still running okay. */
\r
277 ( *pusTaskCheckVariable )++;
\r
281 /*-----------------------------------------------------------*/
\r
283 static void vCompeteingIntMathTask4( void *pvParameters )
\r
285 long *plArray, lTotal1, lTotal2;
\r
286 short sError = pdFALSE;
\r
287 volatile unsigned short *pusTaskCheckVariable;
\r
288 const unsigned short usArraySize = 250;
\r
289 unsigned short usPosition;
\r
290 const char * const pcTaskStartMsg = "Integer math task 4 started.\r\n";
\r
291 const char * const pcTaskFailMsg = "Integer math task 4 failed.\r\n";
\r
293 /* Queue a message for printing to say the task has started. */
\r
294 vPrintDisplayMessage( &pcTaskStartMsg );
\r
296 /* The variable this task increments to show it is still running is passed in
\r
297 as the parameter. */
\r
298 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
300 /* Create the array we are going to use for our check calculation. */
\r
301 plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );
\r
303 /* Keep filling the array, keeping a running total of the values placed in the
\r
304 array. Then run through the array adding up all the values. If the two totals
\r
305 do not match, stop the check variable from incrementing. */
\r
308 lTotal1 = ( long ) 0;
\r
309 lTotal2 = ( long ) 0;
\r
311 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
313 plArray[ usPosition ] = ( long ) usPosition * ( long ) 12;
\r
314 lTotal1 += ( long ) usPosition * ( long ) 12;
\r
319 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
321 lTotal2 += plArray[ usPosition ];
\r
325 if( lTotal1 != lTotal2 )
\r
327 vPrintDisplayMessage( &pcTaskFailMsg );
\r
333 if( sError == pdFALSE )
\r
335 /* If the calculation has always been correct, increment the check
\r
336 variable so we know this task is still running okay. */
\r
337 ( *pusTaskCheckVariable )++;
\r
341 /*-----------------------------------------------------------*/
\r
343 /* This is called to check that all the created tasks are still running. */
\r
344 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )
\r
346 /* Keep a history of the check variables so we know if they have been incremented
\r
347 since the last call. */
\r
348 static unsigned short usLastTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
349 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
351 /* Check the maths tasks are still running by ensuring their check variables
\r
352 are still incrementing. */
\r
353 for( xTask = 0; xTask < intgNUMBER_OF_TASKS; xTask++ )
\r
355 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
357 /* The check has not incremented so an error exists. */
\r
361 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r