2 FreeRTOS V7.6.0 - Copyright (C) 2013 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
67 * Creates eight tasks, each of which loops continuously performing a floating
\r
68 * point calculation and in so doing test the floating point context switching.
\r
69 * This file also demonstrates the use of the xPortUsesFloatingPoint() function
\r
70 * which informs the kernel that the task requires its floating point context
\r
71 * saved on each switch.
\r
73 * All the tasks run at the idle priority and never block or yield. This causes
\r
74 * all eight tasks to time slice with the idle task. Running at the idle
\r
75 * priority means that these tasks will get pre-empted any time another task is
\r
76 * ready to run or a time slice occurs. More often than not the pre-emption
\r
77 * will occur mid calculation, creating a good test of the schedulers context
\r
78 * switch mechanism - a calculation producing an unexpected result could be a
\r
79 * symptom of a corruption in the context of a task.
\r
85 /* Scheduler include files. */
\r
86 #include "FreeRTOS.h"
\r
89 /* Demo program include files. */
\r
92 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
93 #define mathNUMBER_OF_TASKS ( 8 )
\r
95 /* Four tasks, each of which performs a different floating point calculation.
\r
96 Each of the four is created twice. */
\r
97 static void vCompetingMathTask1( void *pvParameters );
\r
98 static void vCompetingMathTask2( void *pvParameters );
\r
99 static void vCompetingMathTask3( void *pvParameters );
\r
100 static void vCompetingMathTask4( void *pvParameters );
\r
102 /* These variables are used to check that all the tasks are still running. If a
\r
103 task gets a calculation wrong it will stop incrementing its check variable,
\r
104 otherwise the check variable will get incremented on each iteration of the
\r
105 tasks execution. */
\r
106 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
108 /*-----------------------------------------------------------*/
\r
110 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
112 xTaskHandle xCreatedTask;
\r
114 /* Create one of the floating point tasks... */
\r
115 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xCreatedTask );
\r
117 /* ... then enable floating point support for the created task so its flop
\r
118 flop registers are maintained in a consistent state. */
\r
119 xPortUsesFloatingPoint( xCreatedTask );
\r
121 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xCreatedTask );
\r
122 xPortUsesFloatingPoint( xCreatedTask );
\r
124 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xCreatedTask );
\r
125 xPortUsesFloatingPoint( xCreatedTask );
\r
127 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xCreatedTask );
\r
128 xPortUsesFloatingPoint( xCreatedTask );
\r
130 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xCreatedTask );
\r
131 xPortUsesFloatingPoint( xCreatedTask );
\r
133 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xCreatedTask );
\r
134 xPortUsesFloatingPoint( xCreatedTask );
\r
136 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xCreatedTask );
\r
137 xPortUsesFloatingPoint( xCreatedTask );
\r
139 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xCreatedTask );
\r
140 xPortUsesFloatingPoint( xCreatedTask );
\r
142 /*-----------------------------------------------------------*/
\r
144 static void vCompetingMathTask1( void *pvParameters )
\r
146 volatile double d1, d2, d3, d4;
\r
147 volatile unsigned short *pusTaskCheckVariable;
\r
148 volatile double dAnswer;
\r
149 short sError = pdFALSE;
\r
155 /* Calculate the expected answer. */
\r
156 dAnswer = ( d1 + d2 ) * d3;
\r
158 /* The variable this task increments to show it is still running is passed in
\r
159 as the parameter. */
\r
160 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
162 /* Keep performing a calculation and checking the result against a constant. */
\r
165 /* Perform the calculation. */
\r
170 d4 = ( d1 + d2 ) * d3;
\r
172 /* If the calculation does not match the expected constant, stop the
\r
173 increment of the check variable. */
\r
174 if( fabs( d4 - dAnswer ) > 0.001 )
\r
179 if( sError == pdFALSE )
\r
181 /* If the calculation has always been correct, increment the check
\r
182 variable so we know this task is still running okay. */
\r
183 ( *pusTaskCheckVariable )++;
\r
187 /*-----------------------------------------------------------*/
\r
189 static void vCompetingMathTask2( void *pvParameters )
\r
191 volatile double d1, d2, d3, d4;
\r
192 volatile unsigned short *pusTaskCheckVariable;
\r
193 volatile double dAnswer;
\r
194 short sError = pdFALSE;
\r
200 /* Calculate the expected answer. */
\r
201 dAnswer = ( d1 / d2 ) * d3;
\r
204 /* The variable this task increments to show it is still running is passed in
\r
205 as the parameter. */
\r
206 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
208 /* Keep performing a calculation and checking the result against a constant. */
\r
211 /* Perform the calculation. */
\r
216 d4 = ( d1 / d2 ) * d3;
\r
218 /* If the calculation does not match the expected constant, stop the
\r
219 increment of the check variable. */
\r
220 if( fabs( d4 - dAnswer ) > 0.001 )
\r
225 if( sError == pdFALSE )
\r
227 /* If the calculation has always been correct, increment the check
\r
228 variable so we know
\r
229 this task is still running okay. */
\r
230 ( *pusTaskCheckVariable )++;
\r
234 /*-----------------------------------------------------------*/
\r
236 static void vCompetingMathTask3( void *pvParameters )
\r
238 volatile double *pdArray, dTotal1, dTotal2, dDifference;
\r
239 volatile unsigned short *pusTaskCheckVariable;
\r
240 const size_t xArraySize = 10;
\r
242 short sError = pdFALSE;
\r
244 /* The variable this task increments to show it is still running is passed
\r
245 in as the parameter. */
\r
246 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
248 /* Allocate memory for use as an array. */
\r
249 pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );
\r
251 /* Keep filling an array, keeping a running total of the values placed in
\r
252 the array. Then run through the array adding up all the values. If the two
\r
253 totals do not match, stop the check variable from incrementing. */
\r
259 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
261 pdArray[ xPosition ] = ( double ) xPosition + 5.5;
\r
262 dTotal1 += ( double ) xPosition + 5.5;
\r
265 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
267 dTotal2 += pdArray[ xPosition ];
\r
270 dDifference = dTotal1 - dTotal2;
\r
271 if( fabs( dDifference ) > 0.001 )
\r
276 if( sError == pdFALSE )
\r
278 /* If the calculation has always been correct, increment the check
\r
279 variable so we know this task is still running okay. */
\r
280 ( *pusTaskCheckVariable )++;
\r
284 /*-----------------------------------------------------------*/
\r
286 static void vCompetingMathTask4( void *pvParameters )
\r
288 volatile double *pdArray, dTotal1, dTotal2, dDifference;
\r
289 volatile unsigned short *pusTaskCheckVariable;
\r
290 const size_t xArraySize = 10;
\r
292 short sError = pdFALSE;
\r
294 /* The variable this task increments to show it is still running is passed in
\r
295 as the parameter. */
\r
296 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
298 /* Allocate RAM for use as an array. */
\r
299 pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );
\r
301 /* Keep filling an array, keeping a running total of the values placed in the
\r
302 array. Then run through the array adding up all the values. If the two totals
\r
303 do not match, stop the check variable from incrementing. */
\r
309 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
311 pdArray[ xPosition ] = ( double ) xPosition * 12.123;
\r
312 dTotal1 += ( double ) xPosition * 12.123;
\r
315 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
317 dTotal2 += pdArray[ xPosition ];
\r
320 dDifference = dTotal1 - dTotal2;
\r
321 if( fabs( dDifference ) > 0.001 )
\r
326 if( sError == pdFALSE )
\r
328 /* If the calculation has always been correct, increment the check
\r
329 variable so we know this task is still running okay. */
\r
330 ( *pusTaskCheckVariable )++;
\r
334 /*-----------------------------------------------------------*/
\r
336 /* This is called to check that all the created tasks are still running. */
\r
337 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
339 /* Keep a history of the check variables so we know if they have been
\r
340 incremented since the last call. */
\r
341 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
342 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
344 /* Check the maths tasks are still running by ensuring their check variables
\r
345 are still incrementing. */
\r
346 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
348 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
350 /* The check has not incremented so an error exists. */
\r
354 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r