| 1 |
585 |
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 floating
|
| 56 |
|
|
* point calculation and in so doing test the floating point context switching.
|
| 57 |
|
|
* This file also demonstrates the use of the xPortUsesFloatingPoint() function
|
| 58 |
|
|
* which informs the kernel that the task requires its floating point context
|
| 59 |
|
|
* saved on each switch.
|
| 60 |
|
|
*
|
| 61 |
|
|
* All the tasks run at the idle priority and never block or yield. This causes
|
| 62 |
|
|
* all eight tasks to time slice with the idle task. Running at the idle
|
| 63 |
|
|
* priority means that these tasks will get pre-empted any time another task is
|
| 64 |
|
|
* ready to run or a time slice occurs. More often than not the pre-emption
|
| 65 |
|
|
* will occur mid calculation, creating a good test of the schedulers context
|
| 66 |
|
|
* switch mechanism - a calculation producing an unexpected result could be a
|
| 67 |
|
|
* symptom of a corruption in the context of a task.
|
| 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 |
|
|
#define mathSTACK_SIZE configMINIMAL_STACK_SIZE
|
| 81 |
|
|
#define mathNUMBER_OF_TASKS ( 8 )
|
| 82 |
|
|
|
| 83 |
|
|
/* Four tasks, each of which performs a different floating point calculation.
|
| 84 |
|
|
Each of the four is created twice. */
|
| 85 |
|
|
static void vCompetingMathTask1( void *pvParameters );
|
| 86 |
|
|
static void vCompetingMathTask2( void *pvParameters );
|
| 87 |
|
|
static void vCompetingMathTask3( void *pvParameters );
|
| 88 |
|
|
static void vCompetingMathTask4( void *pvParameters );
|
| 89 |
|
|
|
| 90 |
|
|
/* These variables are used to check that all the tasks are still running. If a
|
| 91 |
|
|
task gets a calculation wrong it will stop incrementing its check variable,
|
| 92 |
|
|
otherwise the check variable will get incremented on each iteration of the
|
| 93 |
|
|
tasks execution. */
|
| 94 |
|
|
static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
|
| 95 |
|
|
|
| 96 |
|
|
/*-----------------------------------------------------------*/
|
| 97 |
|
|
|
| 98 |
|
|
void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
|
| 99 |
|
|
{
|
| 100 |
|
|
xTaskHandle xCreatedTask;
|
| 101 |
|
|
|
| 102 |
|
|
/* Create one of the floating point tasks... */
|
| 103 |
|
|
xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, &xCreatedTask );
|
| 104 |
|
|
|
| 105 |
|
|
/* ... then enable floating point support for the created task so its flop
|
| 106 |
|
|
flop registers are maintained in a consistent state. */
|
| 107 |
|
|
xPortUsesFloatingPoint( xCreatedTask );
|
| 108 |
|
|
|
| 109 |
|
|
xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, &xCreatedTask );
|
| 110 |
|
|
xPortUsesFloatingPoint( xCreatedTask );
|
| 111 |
|
|
|
| 112 |
|
|
xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, &xCreatedTask );
|
| 113 |
|
|
xPortUsesFloatingPoint( xCreatedTask );
|
| 114 |
|
|
|
| 115 |
|
|
xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, &xCreatedTask );
|
| 116 |
|
|
xPortUsesFloatingPoint( xCreatedTask );
|
| 117 |
|
|
|
| 118 |
|
|
xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, &xCreatedTask );
|
| 119 |
|
|
xPortUsesFloatingPoint( xCreatedTask );
|
| 120 |
|
|
|
| 121 |
|
|
xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, &xCreatedTask );
|
| 122 |
|
|
xPortUsesFloatingPoint( xCreatedTask );
|
| 123 |
|
|
|
| 124 |
|
|
xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, &xCreatedTask );
|
| 125 |
|
|
xPortUsesFloatingPoint( xCreatedTask );
|
| 126 |
|
|
|
| 127 |
|
|
xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, &xCreatedTask );
|
| 128 |
|
|
xPortUsesFloatingPoint( xCreatedTask );
|
| 129 |
|
|
}
|
| 130 |
|
|
/*-----------------------------------------------------------*/
|
| 131 |
|
|
|
| 132 |
|
|
static void vCompetingMathTask1( void *pvParameters )
|
| 133 |
|
|
{
|
| 134 |
|
|
volatile double d1, d2, d3, d4;
|
| 135 |
|
|
volatile unsigned short *pusTaskCheckVariable;
|
| 136 |
|
|
volatile double dAnswer;
|
| 137 |
|
|
short sError = pdFALSE;
|
| 138 |
|
|
|
| 139 |
|
|
d1 = 123.4567;
|
| 140 |
|
|
d2 = 2345.6789;
|
| 141 |
|
|
d3 = -918.222;
|
| 142 |
|
|
|
| 143 |
|
|
/* Calculate the expected answer. */
|
| 144 |
|
|
dAnswer = ( d1 + d2 ) * d3;
|
| 145 |
|
|
|
| 146 |
|
|
/* The variable this task increments to show it is still running is passed in
|
| 147 |
|
|
as the parameter. */
|
| 148 |
|
|
pusTaskCheckVariable = ( unsigned short * ) pvParameters;
|
| 149 |
|
|
|
| 150 |
|
|
/* Keep performing a calculation and checking the result against a constant. */
|
| 151 |
|
|
for(;;)
|
| 152 |
|
|
{
|
| 153 |
|
|
/* Perform the calculation. */
|
| 154 |
|
|
d1 = 123.4567;
|
| 155 |
|
|
d2 = 2345.6789;
|
| 156 |
|
|
d3 = -918.222;
|
| 157 |
|
|
|
| 158 |
|
|
d4 = ( d1 + d2 ) * d3;
|
| 159 |
|
|
|
| 160 |
|
|
/* If the calculation does not match the expected constant, stop the
|
| 161 |
|
|
increment of the check variable. */
|
| 162 |
|
|
if( fabs( d4 - dAnswer ) > 0.001 )
|
| 163 |
|
|
{
|
| 164 |
|
|
sError = pdTRUE;
|
| 165 |
|
|
}
|
| 166 |
|
|
|
| 167 |
|
|
if( sError == pdFALSE )
|
| 168 |
|
|
{
|
| 169 |
|
|
/* If the calculation has always been correct, increment the check
|
| 170 |
|
|
variable so we know this task is still running okay. */
|
| 171 |
|
|
( *pusTaskCheckVariable )++;
|
| 172 |
|
|
}
|
| 173 |
|
|
}
|
| 174 |
|
|
}
|
| 175 |
|
|
/*-----------------------------------------------------------*/
|
| 176 |
|
|
|
| 177 |
|
|
static void vCompetingMathTask2( void *pvParameters )
|
| 178 |
|
|
{
|
| 179 |
|
|
volatile double d1, d2, d3, d4;
|
| 180 |
|
|
volatile unsigned short *pusTaskCheckVariable;
|
| 181 |
|
|
volatile double dAnswer;
|
| 182 |
|
|
short sError = pdFALSE;
|
| 183 |
|
|
|
| 184 |
|
|
d1 = -389.38;
|
| 185 |
|
|
d2 = 32498.2;
|
| 186 |
|
|
d3 = -2.0001;
|
| 187 |
|
|
|
| 188 |
|
|
/* Calculate the expected answer. */
|
| 189 |
|
|
dAnswer = ( d1 / d2 ) * d3;
|
| 190 |
|
|
|
| 191 |
|
|
|
| 192 |
|
|
/* The variable this task increments to show it is still running is passed in
|
| 193 |
|
|
as the parameter. */
|
| 194 |
|
|
pusTaskCheckVariable = ( unsigned short * ) pvParameters;
|
| 195 |
|
|
|
| 196 |
|
|
/* Keep performing a calculation and checking the result against a constant. */
|
| 197 |
|
|
for( ;; )
|
| 198 |
|
|
{
|
| 199 |
|
|
/* Perform the calculation. */
|
| 200 |
|
|
d1 = -389.38;
|
| 201 |
|
|
d2 = 32498.2;
|
| 202 |
|
|
d3 = -2.0001;
|
| 203 |
|
|
|
| 204 |
|
|
d4 = ( d1 / d2 ) * d3;
|
| 205 |
|
|
|
| 206 |
|
|
/* If the calculation does not match the expected constant, stop the
|
| 207 |
|
|
increment of the check variable. */
|
| 208 |
|
|
if( fabs( d4 - dAnswer ) > 0.001 )
|
| 209 |
|
|
{
|
| 210 |
|
|
sError = pdTRUE;
|
| 211 |
|
|
}
|
| 212 |
|
|
|
| 213 |
|
|
if( sError == pdFALSE )
|
| 214 |
|
|
{
|
| 215 |
|
|
/* If the calculation has always been correct, increment the check
|
| 216 |
|
|
variable so we know
|
| 217 |
|
|
this task is still running okay. */
|
| 218 |
|
|
( *pusTaskCheckVariable )++;
|
| 219 |
|
|
}
|
| 220 |
|
|
}
|
| 221 |
|
|
}
|
| 222 |
|
|
/*-----------------------------------------------------------*/
|
| 223 |
|
|
|
| 224 |
|
|
static void vCompetingMathTask3( void *pvParameters )
|
| 225 |
|
|
{
|
| 226 |
|
|
volatile double *pdArray, dTotal1, dTotal2, dDifference;
|
| 227 |
|
|
volatile unsigned short *pusTaskCheckVariable;
|
| 228 |
|
|
const size_t xArraySize = 10;
|
| 229 |
|
|
size_t xPosition;
|
| 230 |
|
|
short sError = pdFALSE;
|
| 231 |
|
|
|
| 232 |
|
|
/* The variable this task increments to show it is still running is passed
|
| 233 |
|
|
in as the parameter. */
|
| 234 |
|
|
pusTaskCheckVariable = ( unsigned short * ) pvParameters;
|
| 235 |
|
|
|
| 236 |
|
|
/* Allocate memory for use as an array. */
|
| 237 |
|
|
pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );
|
| 238 |
|
|
|
| 239 |
|
|
/* Keep filling an array, keeping a running total of the values placed in
|
| 240 |
|
|
the array. Then run through the array adding up all the values. If the two
|
| 241 |
|
|
totals do not match, stop the check variable from incrementing. */
|
| 242 |
|
|
for( ;; )
|
| 243 |
|
|
{
|
| 244 |
|
|
dTotal1 = 0.0;
|
| 245 |
|
|
dTotal2 = 0.0;
|
| 246 |
|
|
|
| 247 |
|
|
for( xPosition = 0; xPosition < xArraySize; xPosition++ )
|
| 248 |
|
|
{
|
| 249 |
|
|
pdArray[ xPosition ] = ( double ) xPosition + 5.5;
|
| 250 |
|
|
dTotal1 += ( double ) xPosition + 5.5;
|
| 251 |
|
|
}
|
| 252 |
|
|
|
| 253 |
|
|
for( xPosition = 0; xPosition < xArraySize; xPosition++ )
|
| 254 |
|
|
{
|
| 255 |
|
|
dTotal2 += pdArray[ xPosition ];
|
| 256 |
|
|
}
|
| 257 |
|
|
|
| 258 |
|
|
dDifference = dTotal1 - dTotal2;
|
| 259 |
|
|
if( fabs( dDifference ) > 0.001 )
|
| 260 |
|
|
{
|
| 261 |
|
|
sError = pdTRUE;
|
| 262 |
|
|
}
|
| 263 |
|
|
|
| 264 |
|
|
if( sError == pdFALSE )
|
| 265 |
|
|
{
|
| 266 |
|
|
/* If the calculation has always been correct, increment the check
|
| 267 |
|
|
variable so we know this task is still running okay. */
|
| 268 |
|
|
( *pusTaskCheckVariable )++;
|
| 269 |
|
|
}
|
| 270 |
|
|
}
|
| 271 |
|
|
}
|
| 272 |
|
|
/*-----------------------------------------------------------*/
|
| 273 |
|
|
|
| 274 |
|
|
static void vCompetingMathTask4( void *pvParameters )
|
| 275 |
|
|
{
|
| 276 |
|
|
volatile double *pdArray, dTotal1, dTotal2, dDifference;
|
| 277 |
|
|
volatile unsigned short *pusTaskCheckVariable;
|
| 278 |
|
|
const size_t xArraySize = 10;
|
| 279 |
|
|
size_t xPosition;
|
| 280 |
|
|
short sError = pdFALSE;
|
| 281 |
|
|
|
| 282 |
|
|
/* The variable this task increments to show it is still running is passed in
|
| 283 |
|
|
as the parameter. */
|
| 284 |
|
|
pusTaskCheckVariable = ( unsigned short * ) pvParameters;
|
| 285 |
|
|
|
| 286 |
|
|
/* Allocate RAM for use as an array. */
|
| 287 |
|
|
pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );
|
| 288 |
|
|
|
| 289 |
|
|
/* Keep filling an array, keeping a running total of the values placed in the
|
| 290 |
|
|
array. Then run through the array adding up all the values. If the two totals
|
| 291 |
|
|
do not match, stop the check variable from incrementing. */
|
| 292 |
|
|
for( ;; )
|
| 293 |
|
|
{
|
| 294 |
|
|
dTotal1 = 0.0;
|
| 295 |
|
|
dTotal2 = 0.0;
|
| 296 |
|
|
|
| 297 |
|
|
for( xPosition = 0; xPosition < xArraySize; xPosition++ )
|
| 298 |
|
|
{
|
| 299 |
|
|
pdArray[ xPosition ] = ( double ) xPosition * 12.123;
|
| 300 |
|
|
dTotal1 += ( double ) xPosition * 12.123;
|
| 301 |
|
|
}
|
| 302 |
|
|
|
| 303 |
|
|
for( xPosition = 0; xPosition < xArraySize; xPosition++ )
|
| 304 |
|
|
{
|
| 305 |
|
|
dTotal2 += pdArray[ xPosition ];
|
| 306 |
|
|
}
|
| 307 |
|
|
|
| 308 |
|
|
dDifference = dTotal1 - dTotal2;
|
| 309 |
|
|
if( fabs( dDifference ) > 0.001 )
|
| 310 |
|
|
{
|
| 311 |
|
|
sError = pdTRUE;
|
| 312 |
|
|
}
|
| 313 |
|
|
|
| 314 |
|
|
if( sError == pdFALSE )
|
| 315 |
|
|
{
|
| 316 |
|
|
/* If the calculation has always been correct, increment the check
|
| 317 |
|
|
variable so we know this task is still running okay. */
|
| 318 |
|
|
( *pusTaskCheckVariable )++;
|
| 319 |
|
|
}
|
| 320 |
|
|
}
|
| 321 |
|
|
}
|
| 322 |
|
|
/*-----------------------------------------------------------*/
|
| 323 |
|
|
|
| 324 |
|
|
/* This is called to check that all the created tasks are still running. */
|
| 325 |
|
|
portBASE_TYPE xAreMathsTaskStillRunning( void )
|
| 326 |
|
|
{
|
| 327 |
|
|
/* Keep a history of the check variables so we know if they have been
|
| 328 |
|
|
incremented since the last call. */
|
| 329 |
|
|
static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
|
| 330 |
|
|
portBASE_TYPE xReturn = pdTRUE, xTask;
|
| 331 |
|
|
|
| 332 |
|
|
/* Check the maths tasks are still running by ensuring their check variables
|
| 333 |
|
|
are still incrementing. */
|
| 334 |
|
|
for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
|
| 335 |
|
|
{
|
| 336 |
|
|
if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
|
| 337 |
|
|
{
|
| 338 |
|
|
/* The check has not incremented so an error exists. */
|
| 339 |
|
|
xReturn = pdFALSE;
|
| 340 |
|
|
}
|
| 341 |
|
|
|
| 342 |
|
|
usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
|
| 343 |
|
|
}
|
| 344 |
|
|
|
| 345 |
|
|
return xReturn;
|
| 346 |
|
|
}
|
| 347 |
|
|
|
| 348 |
|
|
|
| 349 |
|
|
|