2 FreeRTOS V8.0.0 - 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 distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\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 * Creates eight tasks, each of which loops continuously performing an (emulated)
\r
75 * floating point calculation.
\r
77 * All the tasks run at the idle priority and never block or yield. This causes
\r
78 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
79 * means that these tasks will get pre-empted any time another task is ready to run
\r
80 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
81 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
82 * calculation producing an unexpected result could be a symptom of a corruption in
\r
83 * the context of a task.
\r
85 * \page FlopC flop.c
\r
86 * \ingroup DemoFiles
\r
93 /* Scheduler include files. */
\r
94 #include "FreeRTOS.h"
\r
98 /* Demo program include files. */
\r
101 #define mathSTACK_SIZE ( ( unsigned short ) 512 )
\r
102 #define mathNUMBER_OF_TASKS ( 8 )
\r
104 /* Four tasks, each of which performs a different floating point calculation.
\r
105 Each of the four is created twice. */
\r
106 static void vCompetingMathTask1( void *pvParameters );
\r
107 static void vCompetingMathTask2( void *pvParameters );
\r
108 static void vCompetingMathTask3( void *pvParameters );
\r
109 static void vCompetingMathTask4( void *pvParameters );
\r
111 /* These variables are used to check that all the tasks are still running. If a
\r
112 task gets a calculation wrong it will
\r
113 stop incrementing its check variable. */
\r
114 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
116 /*-----------------------------------------------------------*/
\r
118 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
120 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
121 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
122 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
123 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
124 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
125 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
126 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
127 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
129 /*-----------------------------------------------------------*/
\r
131 static void vCompetingMathTask1( void *pvParameters )
\r
133 portDOUBLE d1, d2, d3, d4;
\r
134 volatile unsigned short *pusTaskCheckVariable;
\r
135 const portDOUBLE dAnswer = ( 123.4567 + 2345.6789 ) * -918.222;
\r
136 const char * const pcTaskStartMsg = "Math task 1 started.\r\n";
\r
137 const char * const pcTaskFailMsg = "Math task 1 failed.\r\n";
\r
138 short sError = pdFALSE;
\r
140 /* Queue a message for printing to say the task has started. */
\r
141 vPrintDisplayMessage( &pcTaskStartMsg );
\r
143 /* The variable this task increments to show it is still running is passed in
\r
144 as the parameter. */
\r
145 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
147 /* Keep performing a calculation and checking the result against a constant. */
\r
154 d4 = ( d1 + d2 ) * d3;
\r
158 /* If the calculation does not match the expected constant, stop the
\r
159 increment of the check variable. */
\r
160 if( fabs( d4 - dAnswer ) > 0.001 )
\r
162 vPrintDisplayMessage( &pcTaskFailMsg );
\r
166 if( sError == pdFALSE )
\r
168 /* If the calculation has always been correct, increment the check
\r
169 variable so we know this task is still running okay. */
\r
170 ( *pusTaskCheckVariable )++;
\r
176 /*-----------------------------------------------------------*/
\r
178 static void vCompetingMathTask2( void *pvParameters )
\r
180 portDOUBLE d1, d2, d3, d4;
\r
181 volatile unsigned short *pusTaskCheckVariable;
\r
182 const portDOUBLE dAnswer = ( -389.38 / 32498.2 ) * -2.0001;
\r
183 const char * const pcTaskStartMsg = "Math task 2 started.\r\n";
\r
184 const char * const pcTaskFailMsg = "Math task 2 failed.\r\n";
\r
185 short sError = pdFALSE;
\r
187 /* Queue a message for printing to say the task has started. */
\r
188 vPrintDisplayMessage( &pcTaskStartMsg );
\r
190 /* The variable this task increments to show it is still running is passed in
\r
191 as the parameter. */
\r
192 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
194 /* Keep performing a calculation and checking the result against a constant. */
\r
201 d4 = ( d1 / d2 ) * d3;
\r
205 /* If the calculation does not match the expected constant, stop the
\r
206 increment of the check variable. */
\r
207 if( fabs( d4 - dAnswer ) > 0.001 )
\r
209 vPrintDisplayMessage( &pcTaskFailMsg );
\r
213 if( sError == pdFALSE )
\r
215 /* If the calculation has always been correct, increment the check
\r
216 variable so we know
\r
217 this task is still running okay. */
\r
218 ( *pusTaskCheckVariable )++;
\r
224 /*-----------------------------------------------------------*/
\r
226 static void vCompetingMathTask3( void *pvParameters )
\r
228 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
229 volatile unsigned short *pusTaskCheckVariable;
\r
230 const unsigned short usArraySize = 250;
\r
231 unsigned short usPosition;
\r
232 const char * const pcTaskStartMsg = "Math task 3 started.\r\n";
\r
233 const char * const pcTaskFailMsg = "Math task 3 failed.\r\n";
\r
234 short sError = pdFALSE;
\r
236 /* Queue a message for printing to say the task has started. */
\r
237 vPrintDisplayMessage( &pcTaskStartMsg );
\r
239 /* The variable this task increments to show it is still running is passed in
\r
240 as the parameter. */
\r
241 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
243 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
245 /* Keep filling an 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
253 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
255 pdArray[ usPosition ] = ( portDOUBLE ) usPosition + 5.5;
\r
256 dTotal1 += ( portDOUBLE ) usPosition + 5.5;
\r
261 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
263 dTotal2 += pdArray[ usPosition ];
\r
266 dDifference = dTotal1 - dTotal2;
\r
267 if( fabs( dDifference ) > 0.001 )
\r
269 vPrintDisplayMessage( &pcTaskFailMsg );
\r
275 if( sError == pdFALSE )
\r
277 /* If the calculation has always been correct, increment the check
\r
278 variable so we know this task is still running okay. */
\r
279 ( *pusTaskCheckVariable )++;
\r
283 /*-----------------------------------------------------------*/
\r
285 static void vCompetingMathTask4( void *pvParameters )
\r
287 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
288 volatile unsigned short *pusTaskCheckVariable;
\r
289 const unsigned short usArraySize = 250;
\r
290 unsigned short usPosition;
\r
291 const char * const pcTaskStartMsg = "Math task 4 started.\r\n";
\r
292 const char * const pcTaskFailMsg = "Math task 4 failed.\r\n";
\r
293 short sError = pdFALSE;
\r
295 /* Queue a message for printing to say the task has started. */
\r
296 vPrintDisplayMessage( &pcTaskStartMsg );
\r
298 /* The variable this task increments to show it is still running is passed in
\r
299 as the parameter. */
\r
300 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
302 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
304 /* Keep filling an 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
312 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
314 pdArray[ usPosition ] = ( portDOUBLE ) usPosition * 12.123;
\r
315 dTotal1 += ( portDOUBLE ) usPosition * 12.123;
\r
320 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
322 dTotal2 += pdArray[ usPosition ];
\r
325 dDifference = dTotal1 - dTotal2;
\r
326 if( fabs( dDifference ) > 0.001 )
\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 xAreMathsTaskStillRunning( 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[ mathNUMBER_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 < mathNUMBER_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