2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
97 * Creates eight tasks, each of which loops continuously performing a
\r
98 * floating point calculation.
\r
100 * All the tasks run at the idle priority and never block or yield. This causes
\r
101 * all eight tasks to time slice with the idle task. Running at the idle priority
\r
102 * means that these tasks will get pre-empted any time another task is ready to run
\r
103 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
104 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
105 * calculation producing an unexpected result could be a symptom of a corruption in
\r
106 * the context of a task.
\r
108 * This file demonstrates the use of the task tag and traceTASK_SWITCHED_IN and
\r
109 * traceTASK_SWITCHED_OUT macros to save and restore the floating point context.
\r
112 #include <stdlib.h>
\r
115 /* Scheduler include files. */
\r
116 #include "FreeRTOS.h"
\r
119 /* Demo program include files. */
\r
122 /* Misc. definitions. */
\r
123 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
124 #define mathNUMBER_OF_TASKS ( 8 )
\r
126 /* Four tasks, each of which performs a different floating point calculation.
\r
127 Each of the four is created twice. */
\r
128 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
129 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
130 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
131 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
133 /* These variables are used to check that all the tasks are still running. If a
\r
134 task gets a calculation wrong it will stop incrementing its check variable. */
\r
135 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
137 /* Buffers into which the flop registers will be saved. There is a buffer for
\r
138 each task created within this file. Zeroing out this array is the normal and
\r
139 safe option as this will cause the task to start with all zeros in its flop
\r
141 static unsigned long ulFlopRegisters[ mathNUMBER_OF_TASKS ][ portNO_FLOP_REGISTERS_TO_SAVE ];
\r
143 /*-----------------------------------------------------------*/
\r
145 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
147 TaskHandle_t xTaskJustCreated;
\r
148 portBASE_TYPE x, y;
\r
150 /* Place known values into the buffers into which the flop registers are
\r
151 to be saved. This is for debug purposes only, it is not normally
\r
152 required. The last position in each array is left at zero as the status
\r
153 register will be loaded from there.
\r
155 It is intended that these values can be viewed being loaded into the
\r
156 flop registers when a task is started - however the Insight debugger
\r
157 does not seem to want to show the flop register values. */
\r
158 for( x = 0; x < mathNUMBER_OF_TASKS; x++ )
\r
160 for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1 ); y++ )
\r
162 ulFlopRegisters[ x ][ y ] = ( x + 1 );
\r
166 /* Create the first task - passing it the address of the check variable
\r
167 that it is going to increment. This check variable is used as an
\r
168 indication that the task is still running. */
\r
169 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xTaskJustCreated );
\r
171 /* The task tag value is a value that can be associated with a task, but
\r
172 is not used by the scheduler itself. Its use is down to the application so
\r
173 it makes a convenient place in this case to store the pointer to the buffer
\r
174 into which the flop context of the task will be stored. The first created
\r
175 task uses ulFlopRegisters[ 0 ], the second ulFlopRegisters[ 1 ], etc. */
\r
176 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 0 ][ 0 ] ) );
\r
178 /* Create another 7 tasks, allocating a buffer for each. */
\r
179 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xTaskJustCreated );
\r
180 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 1 ][ 0 ] ) );
\r
182 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xTaskJustCreated );
\r
183 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 2 ][ 0 ] ) );
\r
185 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xTaskJustCreated );
\r
186 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 3 ][ 0 ] ) );
\r
188 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xTaskJustCreated );
\r
189 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 4 ][ 0 ] ) );
\r
191 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xTaskJustCreated );
\r
192 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 5 ][ 0 ] ) );
\r
194 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xTaskJustCreated );
\r
195 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 6 ][ 0 ] ) );
\r
197 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xTaskJustCreated );
\r
198 vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 7 ][ 0 ] ) );
\r
200 /*-----------------------------------------------------------*/
\r
202 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
204 volatile portFLOAT ff1, ff2, ff3, ff4;
\r
205 volatile unsigned short *pusTaskCheckVariable;
\r
206 volatile portFLOAT fAnswer;
\r
207 short sError = pdFALSE;
\r
213 fAnswer = ( ff1 + ff2 ) * ff3;
\r
215 /* The variable this task increments to show it is still running is passed in
\r
216 as the parameter. */
\r
217 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
219 /* Keep performing a calculation and checking the result against a constant. */
\r
226 ff4 = ( ff1 + ff2 ) * ff3;
\r
228 #if configUSE_PREEMPTION == 0
\r
232 /* If the calculation does not match the expected constant, stop the
\r
233 increment of the check variable. */
\r
234 if( fabs( ff4 - fAnswer ) > 0.001F )
\r
239 if( sError == pdFALSE )
\r
241 /* If the calculation has always been correct, increment the check
\r
242 variable so we know this task is still running okay. */
\r
243 ( *pusTaskCheckVariable )++;
\r
246 #if configUSE_PREEMPTION == 0
\r
252 /*-----------------------------------------------------------*/
\r
254 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
256 volatile portFLOAT ff1, ff2, ff3, ff4;
\r
257 volatile unsigned short *pusTaskCheckVariable;
\r
258 volatile portFLOAT fAnswer;
\r
259 short sError = pdFALSE;
\r
265 fAnswer = ( ff1 / ff2 ) * ff3;
\r
268 /* The variable this task increments to show it is still running is passed in
\r
269 as the parameter. */
\r
270 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
272 /* Keep performing a calculation and checking the result against a constant. */
\r
279 ff4 = ( ff1 / ff2 ) * ff3;
\r
281 #if configUSE_PREEMPTION == 0
\r
285 /* If the calculation does not match the expected constant, stop the
\r
286 increment of the check variable. */
\r
287 if( fabs( ff4 - fAnswer ) > 0.001F )
\r
292 if( sError == pdFALSE )
\r
294 /* If the calculation has always been correct, increment the check
\r
295 variable so we know
\r
296 this task is still running okay. */
\r
297 ( *pusTaskCheckVariable )++;
\r
300 #if configUSE_PREEMPTION == 0
\r
305 /*-----------------------------------------------------------*/
\r
307 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
309 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
\r
310 volatile unsigned short *pusTaskCheckVariable;
\r
311 const size_t xArraySize = 10;
\r
313 short sError = pdFALSE;
\r
315 /* The variable this task increments to show it is still running is passed in
\r
316 as the parameter. */
\r
317 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
319 pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
\r
321 /* Keep filling an array, keeping a running total of the values placed in the
\r
322 array. Then run through the array adding up all the values. If the two totals
\r
323 do not match, stop the check variable from incrementing. */
\r
329 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
331 pfArray[ xPosition ] = ( portFLOAT ) xPosition + 5.5F;
\r
332 fTotal1 += ( portFLOAT ) xPosition + 5.5F;
\r
335 #if configUSE_PREEMPTION == 0
\r
339 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
341 fTotal2 += pfArray[ xPosition ];
\r
344 fDifference = fTotal1 - fTotal2;
\r
345 if( fabs( fDifference ) > 0.001F )
\r
350 #if configUSE_PREEMPTION == 0
\r
354 if( sError == pdFALSE )
\r
356 /* If the calculation has always been correct, increment the check
\r
357 variable so we know this task is still running okay. */
\r
358 ( *pusTaskCheckVariable )++;
\r
362 /*-----------------------------------------------------------*/
\r
364 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
366 volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
\r
367 volatile unsigned short *pusTaskCheckVariable;
\r
368 const size_t xArraySize = 10;
\r
370 short sError = pdFALSE;
\r
372 /* The variable this task increments to show it is still running is passed in
\r
373 as the parameter. */
\r
374 pusTaskCheckVariable = ( unsigned short * ) pvParameters;
\r
376 pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
\r
378 /* Keep filling an array, keeping a running total of the values placed in the
\r
379 array. Then run through the array adding up all the values. If the two totals
\r
380 do not match, stop the check variable from incrementing. */
\r
386 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
388 pfArray[ xPosition ] = ( portFLOAT ) xPosition * 12.123F;
\r
389 fTotal1 += ( portFLOAT ) xPosition * 12.123F;
\r
392 #if configUSE_PREEMPTION == 0
\r
396 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
398 fTotal2 += pfArray[ xPosition ];
\r
401 fDifference = fTotal1 - fTotal2;
\r
402 if( fabs( fDifference ) > 0.001F )
\r
407 #if configUSE_PREEMPTION == 0
\r
411 if( sError == pdFALSE )
\r
413 /* If the calculation has always been correct, increment the check
\r
414 variable so we know this task is still running okay. */
\r
415 ( *pusTaskCheckVariable )++;
\r
419 /*-----------------------------------------------------------*/
\r
421 /* This is called to check that all the created tasks are still running. */
\r
422 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
424 /* Keep a history of the check variables so we know if they have been incremented
\r
425 since the last call. */
\r
426 static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
427 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
429 /* Check the maths tasks are still running by ensuring their check variables
\r
430 are still incrementing. */
\r
431 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
433 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
\r
435 /* The check has not incremented so an error exists. */
\r
439 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
\r