2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
68 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
69 with the cooperative scheduler.
\r
73 * Creates eight tasks, each of which loops continuously performing an (emulated)
\r
74 * floating point calculation.
\r
76 * All the tasks run at the idle priority and never block or yield. This causes
\r
77 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
78 * means that these tasks will get pre-empted any time another task is ready to run
\r
79 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
80 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
81 * calculation producing an unexpected result could be a symptom of a corruption in
\r
82 * the context of a task.
\r
84 * \page FlopC flop.c
\r
85 * \ingroup DemoFiles
\r
92 /* Scheduler include files. */
\r
93 #include "FreeRTOS.h"
\r
97 /* Demo program include files. */
\r
100 #define mathSTACK_SIZE ( ( unsigned short ) 512 )
\r
101 #define mathNUMBER_OF_TASKS ( 8 )
\r
103 /* Four tasks, each of which performs a different floating point calculation.
\r
104 Each of the four is created twice. */
\r
105 static void vCompetingMathTask1( void *pvParameters );
\r
106 static void vCompetingMathTask2( void *pvParameters );
\r
107 static void vCompetingMathTask3( void *pvParameters );
\r
108 static void vCompetingMathTask4( void *pvParameters );
\r
110 /* These variables are used to check that all the tasks are still running. If a
\r
111 task gets a calculation wrong it will
\r
112 stop incrementing its check variable. */
\r
113 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
115 /*-----------------------------------------------------------*/
\r
117 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
119 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
120 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
121 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
122 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
123 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
124 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
125 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
126 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
128 /*-----------------------------------------------------------*/
\r
130 static void vCompetingMathTask1( void *pvParameters )
\r
132 portDOUBLE d1, d2, d3, d4;
\r
133 volatile unsigned short *pusTaskCheckVariable;
\r
134 const portDOUBLE dAnswer = ( 123.4567 + 2345.6789 ) * -918.222;
\r
135 const char * const pcTaskStartMsg = "Math task 1 started.\r\n";
\r
136 const char * const pcTaskFailMsg = "Math task 1 failed.\r\n";
\r
137 short sError = pdFALSE;
\r
139 /* Queue a message for printing to say the task has started. */
\r
140 vPrintDisplayMessage( &pcTaskStartMsg );
\r
142 /* The variable this task increments to show it is still running is passed in
\r
143 as the parameter. */
\r
144 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
146 /* Keep performing a calculation and checking the result against a constant. */
\r
153 d4 = ( d1 + d2 ) * d3;
\r
157 /* If the calculation does not match the expected constant, stop the
\r
158 increment of the check variable. */
\r
159 if( fabs( d4 - dAnswer ) > 0.001 )
\r
161 vPrintDisplayMessage( &pcTaskFailMsg );
\r
165 if( sError == pdFALSE )
\r
167 /* If the calculation has always been correct, increment the check
\r
168 variable so we know this task is still running okay. */
\r
169 ( *pusTaskCheckVariable )++;
\r
175 /*-----------------------------------------------------------*/
\r
177 static void vCompetingMathTask2( void *pvParameters )
\r
179 portDOUBLE d1, d2, d3, d4;
\r
180 volatile unsigned short *pusTaskCheckVariable;
\r
181 const portDOUBLE dAnswer = ( -389.38 / 32498.2 ) * -2.0001;
\r
182 const char * const pcTaskStartMsg = "Math task 2 started.\r\n";
\r
183 const char * const pcTaskFailMsg = "Math task 2 failed.\r\n";
\r
184 short sError = pdFALSE;
\r
186 /* Queue a message for printing to say the task has started. */
\r
187 vPrintDisplayMessage( &pcTaskStartMsg );
\r
189 /* The variable this task increments to show it is still running is passed in
\r
190 as the parameter. */
\r
191 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
193 /* Keep performing a calculation and checking the result against a constant. */
\r
200 d4 = ( d1 / d2 ) * d3;
\r
204 /* If the calculation does not match the expected constant, stop the
\r
205 increment of the check variable. */
\r
206 if( fabs( d4 - dAnswer ) > 0.001 )
\r
208 vPrintDisplayMessage( &pcTaskFailMsg );
\r
212 if( sError == pdFALSE )
\r
214 /* If the calculation has always been correct, increment the check
\r
215 variable so we know
\r
216 this task is still running okay. */
\r
217 ( *pusTaskCheckVariable )++;
\r
223 /*-----------------------------------------------------------*/
\r
225 static void vCompetingMathTask3( void *pvParameters )
\r
227 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
228 volatile unsigned short *pusTaskCheckVariable;
\r
229 const unsigned short usArraySize = 250;
\r
230 unsigned short usPosition;
\r
231 const char * const pcTaskStartMsg = "Math task 3 started.\r\n";
\r
232 const char * const pcTaskFailMsg = "Math task 3 failed.\r\n";
\r
233 short sError = pdFALSE;
\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 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
244 /* Keep filling an 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
252 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
254 pdArray[ usPosition ] = ( portDOUBLE ) usPosition + 5.5;
\r
255 dTotal1 += ( portDOUBLE ) usPosition + 5.5;
\r
260 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
262 dTotal2 += pdArray[ usPosition ];
\r
265 dDifference = dTotal1 - dTotal2;
\r
266 if( fabs( dDifference ) > 0.001 )
\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 vCompetingMathTask4( void *pvParameters )
\r
286 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
287 volatile unsigned short *pusTaskCheckVariable;
\r
288 const unsigned short usArraySize = 250;
\r
289 unsigned short usPosition;
\r
290 const char * const pcTaskStartMsg = "Math task 4 started.\r\n";
\r
291 const char * const pcTaskFailMsg = "Math task 4 failed.\r\n";
\r
292 short sError = pdFALSE;
\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 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
303 /* Keep filling an 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
311 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
313 pdArray[ usPosition ] = ( portDOUBLE ) usPosition * 12.123;
\r
314 dTotal1 += ( portDOUBLE ) usPosition * 12.123;
\r
319 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
321 dTotal2 += pdArray[ usPosition ];
\r
324 dDifference = dTotal1 - dTotal2;
\r
325 if( fabs( dDifference ) > 0.001 )
\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 xAreMathsTaskStillRunning( 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[ mathNUMBER_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 < mathNUMBER_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