2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
73 + The created tasks now include calls to tskYIELD(), allowing them to be used
\r
74 with the cooperative scheduler.
\r
78 * Creates eight tasks, each of which loops continuously performing an (emulated)
\r
79 * floating point calculation.
\r
81 * All the tasks run at the idle priority and never block or yield. This causes
\r
82 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
83 * means that these tasks will get pre-empted any time another task is ready to run
\r
84 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
85 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
86 * calculation producing an unexpected result could be a symptom of a corruption in
\r
87 * the context of a task.
\r
89 * \page FlopC flop.c
\r
90 * \ingroup DemoFiles
\r
97 /* Scheduler include files. */
\r
98 #include "FreeRTOS.h"
\r
102 /* Demo program include files. */
\r
105 #define mathSTACK_SIZE ( ( unsigned short ) 512 )
\r
106 #define mathNUMBER_OF_TASKS ( 8 )
\r
108 /* Four tasks, each of which performs a different floating point calculation.
\r
109 Each of the four is created twice. */
\r
110 static void vCompetingMathTask1( void *pvParameters );
\r
111 static void vCompetingMathTask2( void *pvParameters );
\r
112 static void vCompetingMathTask3( void *pvParameters );
\r
113 static void vCompetingMathTask4( void *pvParameters );
\r
115 /* These variables are used to check that all the tasks are still running. If a
\r
116 task gets a calculation wrong it will
\r
117 stop incrementing its check variable. */
\r
118 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
120 /*-----------------------------------------------------------*/
\r
122 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
124 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
125 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
126 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
127 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
128 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
\r
129 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
\r
130 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
\r
131 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
\r
133 /*-----------------------------------------------------------*/
\r
135 static void vCompetingMathTask1( void *pvParameters )
\r
137 portDOUBLE d1, d2, d3, d4;
\r
138 volatile unsigned short *pusTaskCheckVariable;
\r
139 const portDOUBLE dAnswer = ( 123.4567 + 2345.6789 ) * -918.222;
\r
140 const char * const pcTaskStartMsg = "Math task 1 started.\r\n";
\r
141 const char * const pcTaskFailMsg = "Math task 1 failed.\r\n";
\r
142 short sError = pdFALSE;
\r
144 /* Queue a message for printing to say the task has started. */
\r
145 vPrintDisplayMessage( &pcTaskStartMsg );
\r
147 /* The variable this task increments to show it is still running is passed in
\r
148 as the parameter. */
\r
149 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
151 /* Keep performing a calculation and checking the result against a constant. */
\r
158 d4 = ( d1 + d2 ) * d3;
\r
162 /* If the calculation does not match the expected constant, stop the
\r
163 increment of the check variable. */
\r
164 if( fabs( d4 - dAnswer ) > 0.001 )
\r
166 vPrintDisplayMessage( &pcTaskFailMsg );
\r
170 if( sError == pdFALSE )
\r
172 /* If the calculation has always been correct, increment the check
\r
173 variable so we know this task is still running okay. */
\r
174 ( *pusTaskCheckVariable )++;
\r
180 /*-----------------------------------------------------------*/
\r
182 static void vCompetingMathTask2( void *pvParameters )
\r
184 portDOUBLE d1, d2, d3, d4;
\r
185 volatile unsigned short *pusTaskCheckVariable;
\r
186 const portDOUBLE dAnswer = ( -389.38 / 32498.2 ) * -2.0001;
\r
187 const char * const pcTaskStartMsg = "Math task 2 started.\r\n";
\r
188 const char * const pcTaskFailMsg = "Math task 2 failed.\r\n";
\r
189 short sError = pdFALSE;
\r
191 /* Queue a message for printing to say the task has started. */
\r
192 vPrintDisplayMessage( &pcTaskStartMsg );
\r
194 /* The variable this task increments to show it is still running is passed in
\r
195 as the parameter. */
\r
196 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
198 /* Keep performing a calculation and checking the result against a constant. */
\r
205 d4 = ( d1 / d2 ) * d3;
\r
209 /* If the calculation does not match the expected constant, stop the
\r
210 increment of the check variable. */
\r
211 if( fabs( d4 - dAnswer ) > 0.001 )
\r
213 vPrintDisplayMessage( &pcTaskFailMsg );
\r
217 if( sError == pdFALSE )
\r
219 /* If the calculation has always been correct, increment the check
\r
220 variable so we know
\r
221 this task is still running okay. */
\r
222 ( *pusTaskCheckVariable )++;
\r
228 /*-----------------------------------------------------------*/
\r
230 static void vCompetingMathTask3( void *pvParameters )
\r
232 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
233 volatile unsigned short *pusTaskCheckVariable;
\r
234 const unsigned short usArraySize = 250;
\r
235 unsigned short usPosition;
\r
236 const char * const pcTaskStartMsg = "Math task 3 started.\r\n";
\r
237 const char * const pcTaskFailMsg = "Math task 3 failed.\r\n";
\r
238 short sError = pdFALSE;
\r
240 /* Queue a message for printing to say the task has started. */
\r
241 vPrintDisplayMessage( &pcTaskStartMsg );
\r
243 /* The variable this task increments to show it is still running is passed in
\r
244 as the parameter. */
\r
245 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
247 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
249 /* Keep filling an array, keeping a running total of the values placed in the
\r
250 array. Then run through the array adding up all the values. If the two totals
\r
251 do not match, stop the check variable from incrementing. */
\r
257 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
259 pdArray[ usPosition ] = ( portDOUBLE ) usPosition + 5.5;
\r
260 dTotal1 += ( portDOUBLE ) usPosition + 5.5;
\r
265 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
267 dTotal2 += pdArray[ usPosition ];
\r
270 dDifference = dTotal1 - dTotal2;
\r
271 if( fabs( dDifference ) > 0.001 )
\r
273 vPrintDisplayMessage( &pcTaskFailMsg );
\r
279 if( sError == pdFALSE )
\r
281 /* If the calculation has always been correct, increment the check
\r
282 variable so we know this task is still running okay. */
\r
283 ( *pusTaskCheckVariable )++;
\r
287 /*-----------------------------------------------------------*/
\r
289 static void vCompetingMathTask4( void *pvParameters )
\r
291 portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
292 volatile unsigned short *pusTaskCheckVariable;
\r
293 const unsigned short usArraySize = 250;
\r
294 unsigned short usPosition;
\r
295 const char * const pcTaskStartMsg = "Math task 4 started.\r\n";
\r
296 const char * const pcTaskFailMsg = "Math task 4 failed.\r\n";
\r
297 short sError = pdFALSE;
\r
299 /* Queue a message for printing to say the task has started. */
\r
300 vPrintDisplayMessage( &pcTaskStartMsg );
\r
302 /* The variable this task increments to show it is still running is passed in
\r
303 as the parameter. */
\r
304 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
306 pdArray = ( portDOUBLE * ) pvPortMalloc( ( size_t ) 250 * sizeof( portDOUBLE ) );
\r
308 /* Keep filling an array, keeping a running total of the values placed in the
\r
309 array. Then run through the array adding up all the values. If the two totals
\r
310 do not match, stop the check variable from incrementing. */
\r
316 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
318 pdArray[ usPosition ] = ( portDOUBLE ) usPosition * 12.123;
\r
319 dTotal1 += ( portDOUBLE ) usPosition * 12.123;
\r
324 for( usPosition = 0; usPosition < usArraySize; usPosition++ )
\r
326 dTotal2 += pdArray[ usPosition ];
\r
329 dDifference = dTotal1 - dTotal2;
\r
330 if( fabs( dDifference ) > 0.001 )
\r
332 vPrintDisplayMessage( &pcTaskFailMsg );
\r
338 if( sError == pdFALSE )
\r
340 /* If the calculation has always been correct, increment the check
\r
341 variable so we know this task is still running okay. */
\r
342 ( *pusTaskCheckVariable )++;
\r
346 /*-----------------------------------------------------------*/
\r
348 /* This is called to check that all the created tasks are still running. */
\r
349 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
351 /* Keep a history of the check variables so we know if they have been incremented
\r
352 since the last call. */
\r
353 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
354 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
356 /* Check the maths tasks are still running by ensuring their check variables
\r
357 are still incrementing. */
\r
358 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
360 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
362 /* The check has not incremented so an error exists. */
\r
366 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r