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
66 * Creates eight tasks, each of which loops continuously performing a floating
\r
67 * point calculation.
\r
69 * All the tasks run at the idle priority and never block or yield. This causes
\r
70 * all eight tasks to time slice with the idle task. Running at the idle
\r
71 * priority means that these tasks will get pre-empted any time another task is
\r
72 * ready to run or a time slice occurs. More often than not the pre-emption
\r
73 * will occur mid calculation, creating a good test of the schedulers context
\r
74 * switch mechanism - a calculation producing an unexpected result could be a
\r
75 * symptom of a corruption in the context of a task.
\r
81 /* Scheduler include files. */
\r
82 #include "FreeRTOS.h"
\r
85 /* Demo program include files. */
\r
88 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
89 #define mathNUMBER_OF_TASKS ( 4 )
\r
91 /* Four tasks, each of which performs a different floating point calculation.
\r
92 Each of the four is created twice. */
\r
93 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
94 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
95 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
96 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
98 /* These variables are used to check that all the tasks are still running. If a
\r
99 task gets a calculation wrong it will stop setting its check variable. */
\r
100 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
102 /*-----------------------------------------------------------*/
\r
104 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
106 xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
107 xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
108 xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
109 xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
111 /*-----------------------------------------------------------*/
\r
113 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
115 volatile portDOUBLE d1, d2, d3, d4;
\r
116 volatile unsigned short *pusTaskCheckVariable;
\r
117 volatile portDOUBLE dAnswer;
\r
118 short sError = pdFALSE;
\r
120 /* Some ports require that tasks that use a hardware floating point unit
\r
121 tell the kernel that they require a floating point context before any
\r
122 floating point instructions are executed. */
\r
123 portTASK_USES_FLOATING_POINT();
\r
129 dAnswer = ( d1 + d2 ) * d3;
\r
131 /* The variable this task increments to show it is still running is passed in
\r
132 as the parameter. */
\r
133 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
135 /* Keep performing a calculation and checking the result against a constant. */
\r
142 d4 = ( d1 + d2 ) * d3;
\r
144 #if configUSE_PREEMPTION == 0
\r
148 /* If the calculation does not match the expected constant, stop the
\r
149 increment of the check variable. */
\r
150 if( fabs( d4 - dAnswer ) > 0.001 )
\r
155 if( sError == pdFALSE )
\r
157 /* If the calculation has always been correct then set set the check
\r
158 variable. The check variable will get set to pdFALSE each time
\r
159 xAreMathsTaskStillRunning() is executed. */
\r
160 ( *pusTaskCheckVariable ) = pdTRUE;
\r
163 #if configUSE_PREEMPTION == 0
\r
169 /*-----------------------------------------------------------*/
\r
171 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
173 volatile portDOUBLE d1, d2, d3, d4;
\r
174 volatile unsigned short *pusTaskCheckVariable;
\r
175 volatile portDOUBLE dAnswer;
\r
176 short sError = pdFALSE;
\r
178 /* Some ports require that tasks that use a hardware floating point unit
\r
179 tell the kernel that they require a floating point context before any
\r
180 floating point instructions are executed. */
\r
181 portTASK_USES_FLOATING_POINT();
\r
187 dAnswer = ( d1 / d2 ) * d3;
\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
203 #if configUSE_PREEMPTION == 0
\r
207 /* If the calculation does not match the expected constant, stop the
\r
208 increment of the check variable. */
\r
209 if( fabs( d4 - dAnswer ) > 0.001 )
\r
214 if( sError == pdFALSE )
\r
216 /* If the calculation has always been correct then set set the check
\r
217 variable. The check variable will get set to pdFALSE each time
\r
218 xAreMathsTaskStillRunning() is executed. */
\r
219 ( *pusTaskCheckVariable ) = pdTRUE;
\r
222 #if configUSE_PREEMPTION == 0
\r
227 /*-----------------------------------------------------------*/
\r
229 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
231 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
232 volatile unsigned short *pusTaskCheckVariable;
\r
233 const size_t xArraySize = 10;
\r
235 short sError = pdFALSE;
\r
237 /* Some ports require that tasks that use a hardware floating point unit
\r
238 tell the kernel that they require a floating point context before any
\r
239 floating point instructions are executed. */
\r
240 portTASK_USES_FLOATING_POINT();
\r
242 /* The variable this task increments to show it is still running is passed in
\r
243 as the parameter. */
\r
244 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
246 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
248 /* Keep filling an array, keeping a running total of the values placed in the
\r
249 array. Then run through the array adding up all the values. If the two totals
\r
250 do not match, stop the check variable from incrementing. */
\r
256 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
258 pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;
\r
259 dTotal1 += ( portDOUBLE ) xPosition + 5.5;
\r
262 #if configUSE_PREEMPTION == 0
\r
266 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
268 dTotal2 += pdArray[ xPosition ];
\r
271 dDifference = dTotal1 - dTotal2;
\r
272 if( fabs( dDifference ) > 0.001 )
\r
277 #if configUSE_PREEMPTION == 0
\r
281 if( sError == pdFALSE )
\r
283 /* If the calculation has always been correct then set set the check
\r
284 variable. The check variable will get set to pdFALSE each time
\r
285 xAreMathsTaskStillRunning() is executed. */
\r
286 ( *pusTaskCheckVariable ) = pdTRUE;
\r
290 /*-----------------------------------------------------------*/
\r
292 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
294 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
295 volatile unsigned short *pusTaskCheckVariable;
\r
296 const size_t xArraySize = 10;
\r
298 short sError = pdFALSE;
\r
300 /* Some ports require that tasks that use a hardware floating point unit
\r
301 tell the kernel that they require a floating point context before any
\r
302 floating point instructions are executed. */
\r
303 portTASK_USES_FLOATING_POINT();
\r
305 /* The variable this task increments to show it is still running is passed in
\r
306 as the parameter. */
\r
307 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
309 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
311 /* Keep filling an array, keeping a running total of the values placed in the
\r
312 array. Then run through the array adding up all the values. If the two totals
\r
313 do not match, stop the check variable from incrementing. */
\r
319 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
321 pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;
\r
322 dTotal1 += ( portDOUBLE ) xPosition * 12.123;
\r
325 #if configUSE_PREEMPTION == 0
\r
329 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
331 dTotal2 += pdArray[ xPosition ];
\r
334 dDifference = dTotal1 - dTotal2;
\r
335 if( fabs( dDifference ) > 0.001 )
\r
340 #if configUSE_PREEMPTION == 0
\r
344 if( sError == pdFALSE )
\r
346 /* If the calculation has always been correct then set set the check
\r
347 variable. The check variable will get set to pdFALSE each time
\r
348 xAreMathsTaskStillRunning() is executed. */
\r
349 ( *pusTaskCheckVariable ) = pdTRUE;
\r
353 /*-----------------------------------------------------------*/
\r
355 /* This is called to check that all the created tasks are still running. */
\r
356 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
358 portBASE_TYPE xReturn = pdPASS, xTask;
\r
360 /* Check the maths tasks are still running by ensuring their check variables
\r
361 have been set to pdPASS. */
\r
362 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
364 if( usTaskCheck[ xTask ] != pdTRUE )
\r
366 /* The check has not been set so the associated task has either
\r
367 stalled or detected an error. */
\r
372 /* Reset the variable so it can be checked again the next time this
\r
373 function is executed. */
\r
374 usTaskCheck[ xTask ] = pdFALSE;
\r