1 |
586 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
4 |
|
|
***************************************************************************
|
5 |
|
|
* *
|
6 |
|
|
* If you are: *
|
7 |
|
|
* *
|
8 |
|
|
* + New to FreeRTOS, *
|
9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
10 |
|
|
* + Looking for basic training, *
|
11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
12 |
|
|
* *
|
13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
14 |
|
|
* *
|
15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
17 |
|
|
* *
|
18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
22 |
|
|
* *
|
23 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
36 |
|
|
more details. You should have received a copy of the GNU General Public
|
37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
40 |
|
|
FreeRTOS WEB site.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
/*
|
55 |
|
|
* Creates eight tasks, each of which loops continuously performing a
|
56 |
|
|
* floating point calculation.
|
57 |
|
|
*
|
58 |
|
|
* All the tasks run at the idle priority and never block or yield. This causes
|
59 |
|
|
* all eight tasks to time slice with the idle task. Running at the idle priority
|
60 |
|
|
* means that these tasks will get pre-empted any time another task is ready to run
|
61 |
|
|
* or a time slice occurs. More often than not the pre-emption will occur mid
|
62 |
|
|
* calculation, creating a good test of the schedulers context switch mechanism - a
|
63 |
|
|
* calculation producing an unexpected result could be a symptom of a corruption in
|
64 |
|
|
* the context of a task.
|
65 |
|
|
*
|
66 |
|
|
* This file demonstrates the use of the task tag and traceTASK_SWITCHED_IN and
|
67 |
|
|
* traceTASK_SWITCHED_OUT macros to save and restore the floating point context.
|
68 |
|
|
*/
|
69 |
|
|
|
70 |
|
|
#include <stdlib.h>
|
71 |
|
|
#include <math.h>
|
72 |
|
|
|
73 |
|
|
/* Scheduler include files. */
|
74 |
|
|
#include "FreeRTOS.h"
|
75 |
|
|
#include "task.h"
|
76 |
|
|
|
77 |
|
|
/* Demo program include files. */
|
78 |
|
|
#include "flop.h"
|
79 |
|
|
|
80 |
|
|
/* Misc. definitions. */
|
81 |
|
|
#define mathSTACK_SIZE configMINIMAL_STACK_SIZE
|
82 |
|
|
#define mathNUMBER_OF_TASKS ( 8 )
|
83 |
|
|
|
84 |
|
|
/* Four tasks, each of which performs a different floating point calculation.
|
85 |
|
|
Each of the four is created twice. */
|
86 |
|
|
static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
|
87 |
|
|
static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
|
88 |
|
|
static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
|
89 |
|
|
static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
|
90 |
|
|
|
91 |
|
|
/* These variables are used to check that all the tasks are still running. If a
|
92 |
|
|
task gets a calculation wrong it will stop incrementing its check variable. */
|
93 |
|
|
static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
|
94 |
|
|
|
95 |
|
|
/* Buffers into which the flop registers will be saved. There is a buffer for
|
96 |
|
|
each task created within this file. Zeroing out this array is the normal and
|
97 |
|
|
safe option as this will cause the task to start with all zeros in its flop
|
98 |
|
|
context. */
|
99 |
|
|
static unsigned long ulFlopRegisters[ mathNUMBER_OF_TASKS ][ portNO_FLOP_REGISTERS_TO_SAVE ];
|
100 |
|
|
|
101 |
|
|
/*-----------------------------------------------------------*/
|
102 |
|
|
|
103 |
|
|
void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
|
104 |
|
|
{
|
105 |
|
|
xTaskHandle xTaskJustCreated;
|
106 |
|
|
portBASE_TYPE x, y;
|
107 |
|
|
|
108 |
|
|
/* Place known values into the buffers into which the flop registers are
|
109 |
|
|
to be saved. This is for debug purposes only, it is not normally
|
110 |
|
|
required. The last position in each array is left at zero as the status
|
111 |
|
|
register will be loaded from there.
|
112 |
|
|
|
113 |
|
|
It is intended that these values can be viewed being loaded into the
|
114 |
|
|
flop registers when a task is started - however the Insight debugger
|
115 |
|
|
does not seem to want to show the flop register values. */
|
116 |
|
|
for( x = 0; x < mathNUMBER_OF_TASKS; x++ )
|
117 |
|
|
{
|
118 |
|
|
for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1 ); y++ )
|
119 |
|
|
{
|
120 |
|
|
ulFlopRegisters[ x ][ y ] = ( x + 1 );
|
121 |
|
|
}
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
/* Create the first task - passing it the address of the check variable
|
125 |
|
|
that it is going to increment. This check variable is used as an
|
126 |
|
|
indication that the task is still running. */
|
127 |
|
|
xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xTaskJustCreated );
|
128 |
|
|
|
129 |
|
|
/* The task tag value is a value that can be associated with a task, but
|
130 |
|
|
is not used by the scheduler itself. Its use is down to the application so
|
131 |
|
|
it makes a convenient place in this case to store the pointer to the buffer
|
132 |
|
|
into which the flop context of the task will be stored. The first created
|
133 |
|
|
task uses ulFlopRegisters[ 0 ], the second ulFlopRegisters[ 1 ], etc. */
|
134 |
|
|
vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 0 ][ 0 ] ) );
|
135 |
|
|
|
136 |
|
|
/* Create another 7 tasks, allocating a buffer for each. */
|
137 |
|
|
xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xTaskJustCreated );
|
138 |
|
|
vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 1 ][ 0 ] ) );
|
139 |
|
|
|
140 |
|
|
xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xTaskJustCreated );
|
141 |
|
|
vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 2 ][ 0 ] ) );
|
142 |
|
|
|
143 |
|
|
xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xTaskJustCreated );
|
144 |
|
|
vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 3 ][ 0 ] ) );
|
145 |
|
|
|
146 |
|
|
xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xTaskJustCreated );
|
147 |
|
|
vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 4 ][ 0 ] ) );
|
148 |
|
|
|
149 |
|
|
xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xTaskJustCreated );
|
150 |
|
|
vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 5 ][ 0 ] ) );
|
151 |
|
|
|
152 |
|
|
xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xTaskJustCreated );
|
153 |
|
|
vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 6 ][ 0 ] ) );
|
154 |
|
|
|
155 |
|
|
xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xTaskJustCreated );
|
156 |
|
|
vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 7 ][ 0 ] ) );
|
157 |
|
|
}
|
158 |
|
|
/*-----------------------------------------------------------*/
|
159 |
|
|
|
160 |
|
|
static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
|
161 |
|
|
{
|
162 |
|
|
volatile portFLOAT ff1, ff2, ff3, ff4;
|
163 |
|
|
volatile unsigned short *pusTaskCheckVariable;
|
164 |
|
|
volatile portFLOAT fAnswer;
|
165 |
|
|
short sError = pdFALSE;
|
166 |
|
|
|
167 |
|
|
ff1 = 123.4567F;
|
168 |
|
|
ff2 = 2345.6789F;
|
169 |
|
|
ff3 = -918.222F;
|
170 |
|
|
|
171 |
|
|
fAnswer = ( ff1 + ff2 ) * ff3;
|
172 |
|
|
|
173 |
|
|
/* The variable this task increments to show it is still running is passed in
|
174 |
|
|
as the parameter. */
|
175 |
|
|
pusTaskCheckVariable = ( unsigned short * ) pvParameters;
|
176 |
|
|
|
177 |
|
|
/* Keep performing a calculation and checking the result against a constant. */
|
178 |
|
|
for(;;)
|
179 |
|
|
{
|
180 |
|
|
ff1 = 123.4567F;
|
181 |
|
|
ff2 = 2345.6789F;
|
182 |
|
|
ff3 = -918.222F;
|
183 |
|
|
|
184 |
|
|
ff4 = ( ff1 + ff2 ) * ff3;
|
185 |
|
|
|
186 |
|
|
#if configUSE_PREEMPTION == 0
|
187 |
|
|
taskYIELD();
|
188 |
|
|
#endif
|
189 |
|
|
|
190 |
|
|
/* If the calculation does not match the expected constant, stop the
|
191 |
|
|
increment of the check variable. */
|
192 |
|
|
if( fabs( ff4 - fAnswer ) > 0.001F )
|
193 |
|
|
{
|
194 |
|
|
sError = pdTRUE;
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
if( sError == pdFALSE )
|
198 |
|
|
{
|
199 |
|
|
/* If the calculation has always been correct, increment the check
|
200 |
|
|
variable so we know this task is still running okay. */
|
201 |
|
|
( *pusTaskCheckVariable )++;
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
#if configUSE_PREEMPTION == 0
|
205 |
|
|
taskYIELD();
|
206 |
|
|
#endif
|
207 |
|
|
|
208 |
|
|
}
|
209 |
|
|
}
|
210 |
|
|
/*-----------------------------------------------------------*/
|
211 |
|
|
|
212 |
|
|
static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
|
213 |
|
|
{
|
214 |
|
|
volatile portFLOAT ff1, ff2, ff3, ff4;
|
215 |
|
|
volatile unsigned short *pusTaskCheckVariable;
|
216 |
|
|
volatile portFLOAT fAnswer;
|
217 |
|
|
short sError = pdFALSE;
|
218 |
|
|
|
219 |
|
|
ff1 = -389.38F;
|
220 |
|
|
ff2 = 32498.2F;
|
221 |
|
|
ff3 = -2.0001F;
|
222 |
|
|
|
223 |
|
|
fAnswer = ( ff1 / ff2 ) * ff3;
|
224 |
|
|
|
225 |
|
|
|
226 |
|
|
/* The variable this task increments to show it is still running is passed in
|
227 |
|
|
as the parameter. */
|
228 |
|
|
pusTaskCheckVariable = ( unsigned short * ) pvParameters;
|
229 |
|
|
|
230 |
|
|
/* Keep performing a calculation and checking the result against a constant. */
|
231 |
|
|
for( ;; )
|
232 |
|
|
{
|
233 |
|
|
ff1 = -389.38F;
|
234 |
|
|
ff2 = 32498.2F;
|
235 |
|
|
ff3 = -2.0001F;
|
236 |
|
|
|
237 |
|
|
ff4 = ( ff1 / ff2 ) * ff3;
|
238 |
|
|
|
239 |
|
|
#if configUSE_PREEMPTION == 0
|
240 |
|
|
taskYIELD();
|
241 |
|
|
#endif
|
242 |
|
|
|
243 |
|
|
/* If the calculation does not match the expected constant, stop the
|
244 |
|
|
increment of the check variable. */
|
245 |
|
|
if( fabs( ff4 - fAnswer ) > 0.001F )
|
246 |
|
|
{
|
247 |
|
|
sError = pdTRUE;
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
if( sError == pdFALSE )
|
251 |
|
|
{
|
252 |
|
|
/* If the calculation has always been correct, increment the check
|
253 |
|
|
variable so we know
|
254 |
|
|
this task is still running okay. */
|
255 |
|
|
( *pusTaskCheckVariable )++;
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
#if configUSE_PREEMPTION == 0
|
259 |
|
|
taskYIELD();
|
260 |
|
|
#endif
|
261 |
|
|
}
|
262 |
|
|
}
|
263 |
|
|
/*-----------------------------------------------------------*/
|
264 |
|
|
|
265 |
|
|
static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
|
266 |
|
|
{
|
267 |
|
|
volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
|
268 |
|
|
volatile unsigned short *pusTaskCheckVariable;
|
269 |
|
|
const size_t xArraySize = 10;
|
270 |
|
|
size_t xPosition;
|
271 |
|
|
short sError = pdFALSE;
|
272 |
|
|
|
273 |
|
|
/* The variable this task increments to show it is still running is passed in
|
274 |
|
|
as the parameter. */
|
275 |
|
|
pusTaskCheckVariable = ( unsigned short * ) pvParameters;
|
276 |
|
|
|
277 |
|
|
pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
|
278 |
|
|
|
279 |
|
|
/* Keep filling an array, keeping a running total of the values placed in the
|
280 |
|
|
array. Then run through the array adding up all the values. If the two totals
|
281 |
|
|
do not match, stop the check variable from incrementing. */
|
282 |
|
|
for( ;; )
|
283 |
|
|
{
|
284 |
|
|
fTotal1 = 0.0F;
|
285 |
|
|
fTotal2 = 0.0F;
|
286 |
|
|
|
287 |
|
|
for( xPosition = 0; xPosition < xArraySize; xPosition++ )
|
288 |
|
|
{
|
289 |
|
|
pfArray[ xPosition ] = ( portFLOAT ) xPosition + 5.5F;
|
290 |
|
|
fTotal1 += ( portFLOAT ) xPosition + 5.5F;
|
291 |
|
|
}
|
292 |
|
|
|
293 |
|
|
#if configUSE_PREEMPTION == 0
|
294 |
|
|
taskYIELD();
|
295 |
|
|
#endif
|
296 |
|
|
|
297 |
|
|
for( xPosition = 0; xPosition < xArraySize; xPosition++ )
|
298 |
|
|
{
|
299 |
|
|
fTotal2 += pfArray[ xPosition ];
|
300 |
|
|
}
|
301 |
|
|
|
302 |
|
|
fDifference = fTotal1 - fTotal2;
|
303 |
|
|
if( fabs( fDifference ) > 0.001F )
|
304 |
|
|
{
|
305 |
|
|
sError = pdTRUE;
|
306 |
|
|
}
|
307 |
|
|
|
308 |
|
|
#if configUSE_PREEMPTION == 0
|
309 |
|
|
taskYIELD();
|
310 |
|
|
#endif
|
311 |
|
|
|
312 |
|
|
if( sError == pdFALSE )
|
313 |
|
|
{
|
314 |
|
|
/* If the calculation has always been correct, increment the check
|
315 |
|
|
variable so we know this task is still running okay. */
|
316 |
|
|
( *pusTaskCheckVariable )++;
|
317 |
|
|
}
|
318 |
|
|
}
|
319 |
|
|
}
|
320 |
|
|
/*-----------------------------------------------------------*/
|
321 |
|
|
|
322 |
|
|
static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
|
323 |
|
|
{
|
324 |
|
|
volatile portFLOAT *pfArray, fTotal1, fTotal2, fDifference;
|
325 |
|
|
volatile unsigned short *pusTaskCheckVariable;
|
326 |
|
|
const size_t xArraySize = 10;
|
327 |
|
|
size_t xPosition;
|
328 |
|
|
short sError = pdFALSE;
|
329 |
|
|
|
330 |
|
|
/* The variable this task increments to show it is still running is passed in
|
331 |
|
|
as the parameter. */
|
332 |
|
|
pusTaskCheckVariable = ( unsigned short * ) pvParameters;
|
333 |
|
|
|
334 |
|
|
pfArray = ( portFLOAT * ) pvPortMalloc( xArraySize * sizeof( portFLOAT ) );
|
335 |
|
|
|
336 |
|
|
/* Keep filling an array, keeping a running total of the values placed in the
|
337 |
|
|
array. Then run through the array adding up all the values. If the two totals
|
338 |
|
|
do not match, stop the check variable from incrementing. */
|
339 |
|
|
for( ;; )
|
340 |
|
|
{
|
341 |
|
|
fTotal1 = 0.0F;
|
342 |
|
|
fTotal2 = 0.0F;
|
343 |
|
|
|
344 |
|
|
for( xPosition = 0; xPosition < xArraySize; xPosition++ )
|
345 |
|
|
{
|
346 |
|
|
pfArray[ xPosition ] = ( portFLOAT ) xPosition * 12.123F;
|
347 |
|
|
fTotal1 += ( portFLOAT ) xPosition * 12.123F;
|
348 |
|
|
}
|
349 |
|
|
|
350 |
|
|
#if configUSE_PREEMPTION == 0
|
351 |
|
|
taskYIELD();
|
352 |
|
|
#endif
|
353 |
|
|
|
354 |
|
|
for( xPosition = 0; xPosition < xArraySize; xPosition++ )
|
355 |
|
|
{
|
356 |
|
|
fTotal2 += pfArray[ xPosition ];
|
357 |
|
|
}
|
358 |
|
|
|
359 |
|
|
fDifference = fTotal1 - fTotal2;
|
360 |
|
|
if( fabs( fDifference ) > 0.001F )
|
361 |
|
|
{
|
362 |
|
|
sError = pdTRUE;
|
363 |
|
|
}
|
364 |
|
|
|
365 |
|
|
#if configUSE_PREEMPTION == 0
|
366 |
|
|
taskYIELD();
|
367 |
|
|
#endif
|
368 |
|
|
|
369 |
|
|
if( sError == pdFALSE )
|
370 |
|
|
{
|
371 |
|
|
/* If the calculation has always been correct, increment the check
|
372 |
|
|
variable so we know this task is still running okay. */
|
373 |
|
|
( *pusTaskCheckVariable )++;
|
374 |
|
|
}
|
375 |
|
|
}
|
376 |
|
|
}
|
377 |
|
|
/*-----------------------------------------------------------*/
|
378 |
|
|
|
379 |
|
|
/* This is called to check that all the created tasks are still running. */
|
380 |
|
|
portBASE_TYPE xAreMathsTaskStillRunning( void )
|
381 |
|
|
{
|
382 |
|
|
/* Keep a history of the check variables so we know if they have been incremented
|
383 |
|
|
since the last call. */
|
384 |
|
|
static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
|
385 |
|
|
portBASE_TYPE xReturn = pdTRUE, xTask;
|
386 |
|
|
|
387 |
|
|
/* Check the maths tasks are still running by ensuring their check variables
|
388 |
|
|
are still incrementing. */
|
389 |
|
|
for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
|
390 |
|
|
{
|
391 |
|
|
if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
|
392 |
|
|
{
|
393 |
|
|
/* The check has not incremented so an error exists. */
|
394 |
|
|
xReturn = pdFALSE;
|
395 |
|
|
}
|
396 |
|
|
|
397 |
|
|
usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
|
398 |
|
|
}
|
399 |
|
|
|
400 |
|
|
return xReturn;
|
401 |
|
|
}
|
402 |
|
|
|
403 |
|
|
|
404 |
|
|
|