| 1 |
606 |
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 |
|
|
Changes from V1.2.3
|
| 56 |
|
|
|
| 57 |
|
|
+ The created tasks now include calls to tskYIELD(), allowing them to be used
|
| 58 |
|
|
with the cooperative scheduler.
|
| 59 |
|
|
*/
|
| 60 |
|
|
|
| 61 |
|
|
/**
|
| 62 |
|
|
* This does the same as flop. c, but uses variables of type long instead of
|
| 63 |
|
|
* type double.
|
| 64 |
|
|
*
|
| 65 |
|
|
* As with flop. c, the tasks created in this file are a good test of the
|
| 66 |
|
|
* scheduler context switch mechanism. The processor has to access 32bit
|
| 67 |
|
|
* variables in two or four chunks (depending on the processor). The low
|
| 68 |
|
|
* priority of these tasks means there is a high probability that a context
|
| 69 |
|
|
* switch will occur mid calculation. See the flop. c documentation for
|
| 70 |
|
|
* more information.
|
| 71 |
|
|
*
|
| 72 |
|
|
* \page IntegerC integer.c
|
| 73 |
|
|
* \ingroup DemoFiles
|
| 74 |
|
|
* <HR>
|
| 75 |
|
|
*/
|
| 76 |
|
|
|
| 77 |
|
|
/*
|
| 78 |
|
|
Changes from V1.2.1
|
| 79 |
|
|
|
| 80 |
|
|
+ The constants used in the calculations are larger to ensure the
|
| 81 |
|
|
optimiser does not truncate them to 16 bits.
|
| 82 |
|
|
*/
|
| 83 |
|
|
|
| 84 |
|
|
#include <stdlib.h>
|
| 85 |
|
|
|
| 86 |
|
|
/* Scheduler include files. */
|
| 87 |
|
|
#include "FreeRTOS.h"
|
| 88 |
|
|
#include "task.h"
|
| 89 |
|
|
#include "print.h"
|
| 90 |
|
|
|
| 91 |
|
|
/* Demo program include files. */
|
| 92 |
|
|
#include "integer.h"
|
| 93 |
|
|
|
| 94 |
|
|
#define intgSTACK_SIZE ( ( unsigned short ) 256 )
|
| 95 |
|
|
#define intgNUMBER_OF_TASKS ( 8 )
|
| 96 |
|
|
|
| 97 |
|
|
/* Four tasks, each of which performs a different calculation on four byte
|
| 98 |
|
|
variables. Each of the four is created twice. */
|
| 99 |
|
|
static void vCompeteingIntMathTask1( void *pvParameters );
|
| 100 |
|
|
static void vCompeteingIntMathTask2( void *pvParameters );
|
| 101 |
|
|
static void vCompeteingIntMathTask3( void *pvParameters );
|
| 102 |
|
|
static void vCompeteingIntMathTask4( void *pvParameters );
|
| 103 |
|
|
|
| 104 |
|
|
/* These variables are used to check that all the tasks are still running. If a
|
| 105 |
|
|
task gets a calculation wrong it will stop incrementing its check variable. */
|
| 106 |
|
|
static volatile unsigned short usTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
|
| 107 |
|
|
/*-----------------------------------------------------------*/
|
| 108 |
|
|
|
| 109 |
|
|
void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )
|
| 110 |
|
|
{
|
| 111 |
|
|
xTaskCreate( vCompeteingIntMathTask1, "IntMath1", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
|
| 112 |
|
|
xTaskCreate( vCompeteingIntMathTask2, "IntMath2", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
|
| 113 |
|
|
xTaskCreate( vCompeteingIntMathTask3, "IntMath3", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
|
| 114 |
|
|
xTaskCreate( vCompeteingIntMathTask4, "IntMath4", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
|
| 115 |
|
|
xTaskCreate( vCompeteingIntMathTask1, "IntMath5", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );
|
| 116 |
|
|
xTaskCreate( vCompeteingIntMathTask2, "IntMath6", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );
|
| 117 |
|
|
xTaskCreate( vCompeteingIntMathTask3, "IntMath7", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );
|
| 118 |
|
|
xTaskCreate( vCompeteingIntMathTask4, "IntMath8", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );
|
| 119 |
|
|
}
|
| 120 |
|
|
/*-----------------------------------------------------------*/
|
| 121 |
|
|
|
| 122 |
|
|
static void vCompeteingIntMathTask1( void *pvParameters )
|
| 123 |
|
|
{
|
| 124 |
|
|
long l1, l2, l3, l4;
|
| 125 |
|
|
short sError = pdFALSE;
|
| 126 |
|
|
volatile unsigned short *pusTaskCheckVariable;
|
| 127 |
|
|
const long lAnswer = ( ( long ) 74565L + ( long ) 1234567L ) * ( long ) -918L;
|
| 128 |
|
|
const char * const pcTaskStartMsg = "Integer math task 1 started.\r\n";
|
| 129 |
|
|
const char * const pcTaskFailMsg = "Integer math task 1 failed.\r\n";
|
| 130 |
|
|
|
| 131 |
|
|
/* Queue a message for printing to say the task has started. */
|
| 132 |
|
|
vPrintDisplayMessage( &pcTaskStartMsg );
|
| 133 |
|
|
|
| 134 |
|
|
/* The variable this task increments to show it is still running is passed in
|
| 135 |
|
|
as the parameter. */
|
| 136 |
|
|
pusTaskCheckVariable = ( unsigned short * ) pvParameters;
|
| 137 |
|
|
|
| 138 |
|
|
/* Keep performing a calculation and checking the result against a constant. */
|
| 139 |
|
|
for(;;)
|
| 140 |
|
|
{
|
| 141 |
|
|
l1 = ( long ) 74565L;
|
| 142 |
|
|
l2 = ( long ) 1234567L;
|
| 143 |
|
|
l3 = ( long ) -918L;
|
| 144 |
|
|
|
| 145 |
|
|
l4 = ( l1 + l2 ) * l3;
|
| 146 |
|
|
|
| 147 |
|
|
taskYIELD();
|
| 148 |
|
|
|
| 149 |
|
|
/* If the calculation does not match the expected constant, stop the
|
| 150 |
|
|
increment of the check variable. */
|
| 151 |
|
|
if( l4 != lAnswer )
|
| 152 |
|
|
{
|
| 153 |
|
|
vPrintDisplayMessage( &pcTaskFailMsg );
|
| 154 |
|
|
sError = pdTRUE;
|
| 155 |
|
|
}
|
| 156 |
|
|
|
| 157 |
|
|
if( sError == pdFALSE )
|
| 158 |
|
|
{
|
| 159 |
|
|
/* If the calculation has always been correct, increment the check
|
| 160 |
|
|
variable so we know this task is still running okay. */
|
| 161 |
|
|
( *pusTaskCheckVariable )++;
|
| 162 |
|
|
}
|
| 163 |
|
|
}
|
| 164 |
|
|
}
|
| 165 |
|
|
/*-----------------------------------------------------------*/
|
| 166 |
|
|
|
| 167 |
|
|
static void vCompeteingIntMathTask2( void *pvParameters )
|
| 168 |
|
|
{
|
| 169 |
|
|
long l1, l2, l3, l4;
|
| 170 |
|
|
short sError = pdFALSE;
|
| 171 |
|
|
volatile unsigned short *pusTaskCheckVariable;
|
| 172 |
|
|
const long lAnswer = ( ( long ) -389000L / ( long ) 329999L ) * ( long ) -89L;
|
| 173 |
|
|
const char * const pcTaskStartMsg = "Integer math task 2 started.\r\n";
|
| 174 |
|
|
const char * const pcTaskFailMsg = "Integer math task 2 failed.\r\n";
|
| 175 |
|
|
|
| 176 |
|
|
/* Queue a message for printing to say the task has started. */
|
| 177 |
|
|
vPrintDisplayMessage( &pcTaskStartMsg );
|
| 178 |
|
|
|
| 179 |
|
|
/* The variable this task increments to show it is still running is passed in
|
| 180 |
|
|
as the parameter. */
|
| 181 |
|
|
pusTaskCheckVariable = ( unsigned short * ) pvParameters;
|
| 182 |
|
|
|
| 183 |
|
|
/* Keep performing a calculation and checking the result against a constant. */
|
| 184 |
|
|
for( ;; )
|
| 185 |
|
|
{
|
| 186 |
|
|
l1 = -389000L;
|
| 187 |
|
|
l2 = 329999L;
|
| 188 |
|
|
l3 = -89L;
|
| 189 |
|
|
|
| 190 |
|
|
l4 = ( l1 / l2 ) * l3;
|
| 191 |
|
|
|
| 192 |
|
|
taskYIELD();
|
| 193 |
|
|
|
| 194 |
|
|
/* If the calculation does not match the expected constant, stop the
|
| 195 |
|
|
increment of the check variable. */
|
| 196 |
|
|
if( l4 != lAnswer )
|
| 197 |
|
|
{
|
| 198 |
|
|
vPrintDisplayMessage( &pcTaskFailMsg );
|
| 199 |
|
|
sError = pdTRUE;
|
| 200 |
|
|
}
|
| 201 |
|
|
|
| 202 |
|
|
if( sError == pdFALSE )
|
| 203 |
|
|
{
|
| 204 |
|
|
/* If the calculation has always been correct, increment the check
|
| 205 |
|
|
variable so we know this task is still running okay. */
|
| 206 |
|
|
( *pusTaskCheckVariable )++;
|
| 207 |
|
|
}
|
| 208 |
|
|
}
|
| 209 |
|
|
}
|
| 210 |
|
|
/*-----------------------------------------------------------*/
|
| 211 |
|
|
|
| 212 |
|
|
static void vCompeteingIntMathTask3( void *pvParameters )
|
| 213 |
|
|
{
|
| 214 |
|
|
long *plArray, lTotal1, lTotal2;
|
| 215 |
|
|
short sError = pdFALSE;
|
| 216 |
|
|
volatile unsigned short *pusTaskCheckVariable;
|
| 217 |
|
|
const unsigned short usArraySize = ( unsigned short ) 250;
|
| 218 |
|
|
unsigned short usPosition;
|
| 219 |
|
|
const char * const pcTaskStartMsg = "Integer math task 3 started.\r\n";
|
| 220 |
|
|
const char * const pcTaskFailMsg = "Integer math task 3 failed.\r\n";
|
| 221 |
|
|
|
| 222 |
|
|
/* Queue a message for printing to say the task has started. */
|
| 223 |
|
|
vPrintDisplayMessage( &pcTaskStartMsg );
|
| 224 |
|
|
|
| 225 |
|
|
/* The variable this task increments to show it is still running is passed in
|
| 226 |
|
|
as the parameter. */
|
| 227 |
|
|
pusTaskCheckVariable = ( unsigned short * ) pvParameters;
|
| 228 |
|
|
|
| 229 |
|
|
/* Create the array we are going to use for our check calculation. */
|
| 230 |
|
|
plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );
|
| 231 |
|
|
|
| 232 |
|
|
/* Keep filling the array, keeping a running total of the values placed in the
|
| 233 |
|
|
array. Then run through the array adding up all the values. If the two totals
|
| 234 |
|
|
do not match, stop the check variable from incrementing. */
|
| 235 |
|
|
for( ;; )
|
| 236 |
|
|
{
|
| 237 |
|
|
lTotal1 = ( long ) 0;
|
| 238 |
|
|
lTotal2 = ( long ) 0;
|
| 239 |
|
|
|
| 240 |
|
|
for( usPosition = 0; usPosition < usArraySize; usPosition++ )
|
| 241 |
|
|
{
|
| 242 |
|
|
plArray[ usPosition ] = ( long ) usPosition + ( long ) 5;
|
| 243 |
|
|
lTotal1 += ( long ) usPosition + ( long ) 5;
|
| 244 |
|
|
}
|
| 245 |
|
|
|
| 246 |
|
|
taskYIELD();
|
| 247 |
|
|
|
| 248 |
|
|
for( usPosition = 0; usPosition < usArraySize; usPosition++ )
|
| 249 |
|
|
{
|
| 250 |
|
|
lTotal2 += plArray[ usPosition ];
|
| 251 |
|
|
}
|
| 252 |
|
|
|
| 253 |
|
|
if( lTotal1 != lTotal2 )
|
| 254 |
|
|
{
|
| 255 |
|
|
vPrintDisplayMessage( &pcTaskFailMsg );
|
| 256 |
|
|
sError = pdTRUE;
|
| 257 |
|
|
}
|
| 258 |
|
|
|
| 259 |
|
|
taskYIELD();
|
| 260 |
|
|
|
| 261 |
|
|
if( sError == pdFALSE )
|
| 262 |
|
|
{
|
| 263 |
|
|
/* If the calculation has always been correct, increment the check
|
| 264 |
|
|
variable so we know this task is still running okay. */
|
| 265 |
|
|
( *pusTaskCheckVariable )++;
|
| 266 |
|
|
}
|
| 267 |
|
|
}
|
| 268 |
|
|
}
|
| 269 |
|
|
/*-----------------------------------------------------------*/
|
| 270 |
|
|
|
| 271 |
|
|
static void vCompeteingIntMathTask4( void *pvParameters )
|
| 272 |
|
|
{
|
| 273 |
|
|
long *plArray, lTotal1, lTotal2;
|
| 274 |
|
|
short sError = pdFALSE;
|
| 275 |
|
|
volatile unsigned short *pusTaskCheckVariable;
|
| 276 |
|
|
const unsigned short usArraySize = 250;
|
| 277 |
|
|
unsigned short usPosition;
|
| 278 |
|
|
const char * const pcTaskStartMsg = "Integer math task 4 started.\r\n";
|
| 279 |
|
|
const char * const pcTaskFailMsg = "Integer math task 4 failed.\r\n";
|
| 280 |
|
|
|
| 281 |
|
|
/* Queue a message for printing to say the task has started. */
|
| 282 |
|
|
vPrintDisplayMessage( &pcTaskStartMsg );
|
| 283 |
|
|
|
| 284 |
|
|
/* The variable this task increments to show it is still running is passed in
|
| 285 |
|
|
as the parameter. */
|
| 286 |
|
|
pusTaskCheckVariable = ( unsigned short * ) pvParameters;
|
| 287 |
|
|
|
| 288 |
|
|
/* Create the array we are going to use for our check calculation. */
|
| 289 |
|
|
plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );
|
| 290 |
|
|
|
| 291 |
|
|
/* Keep filling the array, keeping a running total of the values placed in the
|
| 292 |
|
|
array. Then run through the array adding up all the values. If the two totals
|
| 293 |
|
|
do not match, stop the check variable from incrementing. */
|
| 294 |
|
|
for( ;; )
|
| 295 |
|
|
{
|
| 296 |
|
|
lTotal1 = ( long ) 0;
|
| 297 |
|
|
lTotal2 = ( long ) 0;
|
| 298 |
|
|
|
| 299 |
|
|
for( usPosition = 0; usPosition < usArraySize; usPosition++ )
|
| 300 |
|
|
{
|
| 301 |
|
|
plArray[ usPosition ] = ( long ) usPosition * ( long ) 12;
|
| 302 |
|
|
lTotal1 += ( long ) usPosition * ( long ) 12;
|
| 303 |
|
|
}
|
| 304 |
|
|
|
| 305 |
|
|
taskYIELD();
|
| 306 |
|
|
|
| 307 |
|
|
for( usPosition = 0; usPosition < usArraySize; usPosition++ )
|
| 308 |
|
|
{
|
| 309 |
|
|
lTotal2 += plArray[ usPosition ];
|
| 310 |
|
|
}
|
| 311 |
|
|
|
| 312 |
|
|
|
| 313 |
|
|
if( lTotal1 != lTotal2 )
|
| 314 |
|
|
{
|
| 315 |
|
|
vPrintDisplayMessage( &pcTaskFailMsg );
|
| 316 |
|
|
sError = pdTRUE;
|
| 317 |
|
|
}
|
| 318 |
|
|
|
| 319 |
|
|
taskYIELD();
|
| 320 |
|
|
|
| 321 |
|
|
if( sError == pdFALSE )
|
| 322 |
|
|
{
|
| 323 |
|
|
/* If the calculation has always been correct, increment the check
|
| 324 |
|
|
variable so we know this task is still running okay. */
|
| 325 |
|
|
( *pusTaskCheckVariable )++;
|
| 326 |
|
|
}
|
| 327 |
|
|
}
|
| 328 |
|
|
}
|
| 329 |
|
|
/*-----------------------------------------------------------*/
|
| 330 |
|
|
|
| 331 |
|
|
/* This is called to check that all the created tasks are still running. */
|
| 332 |
|
|
portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )
|
| 333 |
|
|
{
|
| 334 |
|
|
/* Keep a history of the check variables so we know if they have been incremented
|
| 335 |
|
|
since the last call. */
|
| 336 |
|
|
static unsigned short usLastTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
|
| 337 |
|
|
portBASE_TYPE xReturn = pdTRUE, xTask;
|
| 338 |
|
|
|
| 339 |
|
|
/* Check the maths tasks are still running by ensuring their check variables
|
| 340 |
|
|
are still incrementing. */
|
| 341 |
|
|
for( xTask = 0; xTask < intgNUMBER_OF_TASKS; xTask++ )
|
| 342 |
|
|
{
|
| 343 |
|
|
if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )
|
| 344 |
|
|
{
|
| 345 |
|
|
/* The check has not incremented so an error exists. */
|
| 346 |
|
|
xReturn = pdFALSE;
|
| 347 |
|
|
}
|
| 348 |
|
|
|
| 349 |
|
|
usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];
|
| 350 |
|
|
}
|
| 351 |
|
|
|
| 352 |
|
|
return xReturn;
|
| 353 |
|
|
}
|