| 1 |
583 |
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 all the demo application tasks, then starts the scheduler. The WEB
|
| 56 |
|
|
* documentation provides more details of the demo application tasks.
|
| 57 |
|
|
*
|
| 58 |
|
|
* This demo is configured to execute on the ES449 prototyping board from
|
| 59 |
|
|
* SoftBaugh. The ES449 has a built in LCD display and a single built in user
|
| 60 |
|
|
* LED. Therefore, in place of flashing an LED, the 'flash' and 'check' tasks
|
| 61 |
|
|
* toggle '*' characters on the LCD. The left most '*' represents LED 0, the
|
| 62 |
|
|
* next LED 1, etc.
|
| 63 |
|
|
*
|
| 64 |
|
|
* Main. c also creates a task called 'Check'. This only executes every three
|
| 65 |
|
|
* seconds but has the highest priority so is guaranteed to get processor time.
|
| 66 |
|
|
* Its main function is to check that all the other tasks are still operational.
|
| 67 |
|
|
* Each task that does not flash an LED maintains a unique count that is
|
| 68 |
|
|
* incremented each time the task successfully completes its function. Should
|
| 69 |
|
|
* any error occur within such a task the count is permanently halted. The
|
| 70 |
|
|
* 'check' task inspects the count of each task to ensure it has changed since
|
| 71 |
|
|
* the last time the check task executed. If all the count variables have
|
| 72 |
|
|
* changed all the tasks are still executing error free, and the check task
|
| 73 |
|
|
* toggles an LED with a three second period. Should any task contain an error
|
| 74 |
|
|
* at any time the LED toggle rate will increase to 500ms.
|
| 75 |
|
|
*
|
| 76 |
|
|
* Please read the documentation for the MSP430 port available on
|
| 77 |
|
|
* http://www.FreeRTOS.org.
|
| 78 |
|
|
*/
|
| 79 |
|
|
|
| 80 |
|
|
/* Standard includes. */
|
| 81 |
|
|
#include <stdlib.h>
|
| 82 |
|
|
|
| 83 |
|
|
/* Scheduler includes. */
|
| 84 |
|
|
#include "FreeRTOS.h"
|
| 85 |
|
|
#include "task.h"
|
| 86 |
|
|
|
| 87 |
|
|
/* Demo application includes. */
|
| 88 |
|
|
#include "partest.h"
|
| 89 |
|
|
#include "flash.h"
|
| 90 |
|
|
#include "integer.h"
|
| 91 |
|
|
#include "comtest2.h"
|
| 92 |
|
|
#include "PollQ.h"
|
| 93 |
|
|
|
| 94 |
|
|
/* Constants required for hardware setup. */
|
| 95 |
|
|
#define mainALL_BITS_OUTPUT ( ( unsigned char ) 0xff )
|
| 96 |
|
|
#define mainMAX_FREQUENCY ( ( unsigned char ) 121 )
|
| 97 |
|
|
|
| 98 |
|
|
/* Constants that define the LED's used by the various tasks. [in this case
|
| 99 |
|
|
the '*' characters on the LCD represent LED's] */
|
| 100 |
|
|
#define mainCHECK_LED ( 4 )
|
| 101 |
|
|
#define mainCOM_TEST_LED ( 10 )
|
| 102 |
|
|
|
| 103 |
|
|
/* Demo task priorities. */
|
| 104 |
|
|
#define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
|
| 105 |
|
|
#define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
|
| 106 |
|
|
#define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
|
| 107 |
|
|
#define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
|
| 108 |
|
|
|
| 109 |
|
|
/* Baud rate used by the COM test tasks. */
|
| 110 |
|
|
#define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 19200 )
|
| 111 |
|
|
|
| 112 |
|
|
/* The frequency at which the 'Check' tasks executes. See the comments at the
|
| 113 |
|
|
top of the page. When the system is operating error free the 'Check' task
|
| 114 |
|
|
toggles an LED every three seconds. If an error is discovered in any task the
|
| 115 |
|
|
rate is increased to 500 milliseconds. [in this case the '*' characters on the
|
| 116 |
|
|
LCD represent LED's]*/
|
| 117 |
|
|
#define mainNO_ERROR_CHECK_DELAY ( ( portTickType ) 3000 / portTICK_RATE_MS )
|
| 118 |
|
|
#define mainERROR_CHECK_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
|
| 119 |
|
|
|
| 120 |
|
|
/* The constants used in the calculation. */
|
| 121 |
|
|
#define intgCONST1 ( ( long ) 123 )
|
| 122 |
|
|
#define intgCONST2 ( ( long ) 234567 )
|
| 123 |
|
|
#define intgCONST3 ( ( long ) -3 )
|
| 124 |
|
|
#define intgCONST4 ( ( long ) 7 )
|
| 125 |
|
|
#define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
|
| 126 |
|
|
|
| 127 |
|
|
/*
|
| 128 |
|
|
* The function that implements the Check task. See the comments at the head
|
| 129 |
|
|
* of the page for implementation details.
|
| 130 |
|
|
*/
|
| 131 |
|
|
static void vErrorChecks( void *pvParameters );
|
| 132 |
|
|
|
| 133 |
|
|
/*
|
| 134 |
|
|
* Called by the Check task. Returns pdPASS if all the other tasks are found
|
| 135 |
|
|
* to be operating without error - otherwise returns pdFAIL.
|
| 136 |
|
|
*/
|
| 137 |
|
|
static short prvCheckOtherTasksAreStillRunning( void );
|
| 138 |
|
|
|
| 139 |
|
|
/*
|
| 140 |
|
|
* Perform the hardware setup required by the ES449 in order to run the demo
|
| 141 |
|
|
* application.
|
| 142 |
|
|
*/
|
| 143 |
|
|
static void prvSetupHardware( void );
|
| 144 |
|
|
|
| 145 |
|
|
|
| 146 |
|
|
portBASE_TYPE xLocalError = pdFALSE;
|
| 147 |
|
|
volatile unsigned long ulIdleLoops = 0UL;
|
| 148 |
|
|
|
| 149 |
|
|
/*-----------------------------------------------------------*/
|
| 150 |
|
|
|
| 151 |
|
|
/*
|
| 152 |
|
|
* Start the demo application tasks - then start the real time scheduler.
|
| 153 |
|
|
*/
|
| 154 |
|
|
int main( void )
|
| 155 |
|
|
{
|
| 156 |
|
|
/* Setup the hardware ready for the demo. */
|
| 157 |
|
|
prvSetupHardware();
|
| 158 |
|
|
vParTestInitialise();
|
| 159 |
|
|
|
| 160 |
|
|
/* Start the standard demo application tasks. */
|
| 161 |
|
|
vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
|
| 162 |
|
|
vStartIntegerMathTasks( tskIDLE_PRIORITY );
|
| 163 |
|
|
vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED - 1 );
|
| 164 |
|
|
vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
|
| 165 |
|
|
|
| 166 |
|
|
/* Start the 'Check' task which is defined in this file. */
|
| 167 |
|
|
xTaskCreate( vErrorChecks, ( const signed char * const ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
|
| 168 |
|
|
|
| 169 |
|
|
/* Start the scheduler. */
|
| 170 |
|
|
vTaskStartScheduler();
|
| 171 |
|
|
|
| 172 |
|
|
/* As the scheduler has been started the demo applications tasks will be
|
| 173 |
|
|
executing and we should never get here! */
|
| 174 |
|
|
return 0;
|
| 175 |
|
|
}
|
| 176 |
|
|
/*-----------------------------------------------------------*/
|
| 177 |
|
|
|
| 178 |
|
|
static portTASK_FUNCTION( vErrorChecks, pvParameters )
|
| 179 |
|
|
{
|
| 180 |
|
|
portTickType xDelayPeriod = mainNO_ERROR_CHECK_DELAY;
|
| 181 |
|
|
|
| 182 |
|
|
/* Cycle for ever, delaying then checking all the other tasks are still
|
| 183 |
|
|
operating without error. */
|
| 184 |
|
|
for( ;; )
|
| 185 |
|
|
{
|
| 186 |
|
|
/* Wait until it is time to check again. The time we wait here depends
|
| 187 |
|
|
on whether an error has been detected or not. When an error is
|
| 188 |
|
|
detected the time is shortened resulting in a faster LED flash rate. */
|
| 189 |
|
|
vTaskDelay( xDelayPeriod );
|
| 190 |
|
|
|
| 191 |
|
|
/* See if the other tasks are all ok. */
|
| 192 |
|
|
if( prvCheckOtherTasksAreStillRunning() != pdPASS )
|
| 193 |
|
|
{
|
| 194 |
|
|
/* An error occurred in one of the tasks so shorten the delay
|
| 195 |
|
|
period - which has the effect of increasing the frequency of the
|
| 196 |
|
|
LED toggle. */
|
| 197 |
|
|
xDelayPeriod = mainERROR_CHECK_DELAY;
|
| 198 |
|
|
}
|
| 199 |
|
|
|
| 200 |
|
|
/* Flash! */
|
| 201 |
|
|
vParTestToggleLED( mainCHECK_LED );
|
| 202 |
|
|
}
|
| 203 |
|
|
}
|
| 204 |
|
|
/*-----------------------------------------------------------*/
|
| 205 |
|
|
|
| 206 |
|
|
static short prvCheckOtherTasksAreStillRunning( void )
|
| 207 |
|
|
{
|
| 208 |
|
|
static short sNoErrorFound = pdTRUE;
|
| 209 |
|
|
static unsigned long ulLastIdleLoopCount = 0UL;
|
| 210 |
|
|
|
| 211 |
|
|
/* The demo tasks maintain a count that increments every cycle of the task
|
| 212 |
|
|
provided that the task has never encountered an error. This function
|
| 213 |
|
|
checks the counts maintained by the tasks to ensure they are still being
|
| 214 |
|
|
incremented. A count remaining at the same value between calls therefore
|
| 215 |
|
|
indicates that an error has been detected. Only tasks that do not flash
|
| 216 |
|
|
an LED are checked. */
|
| 217 |
|
|
|
| 218 |
|
|
if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
|
| 219 |
|
|
{
|
| 220 |
|
|
sNoErrorFound = pdFALSE;
|
| 221 |
|
|
}
|
| 222 |
|
|
|
| 223 |
|
|
if( xAreComTestTasksStillRunning() != pdTRUE )
|
| 224 |
|
|
{
|
| 225 |
|
|
sNoErrorFound = pdFALSE;
|
| 226 |
|
|
}
|
| 227 |
|
|
|
| 228 |
|
|
if( xArePollingQueuesStillRunning() != pdTRUE )
|
| 229 |
|
|
{
|
| 230 |
|
|
sNoErrorFound = pdFALSE;
|
| 231 |
|
|
}
|
| 232 |
|
|
|
| 233 |
|
|
if( xLocalError == pdTRUE )
|
| 234 |
|
|
{
|
| 235 |
|
|
sNoErrorFound = pdFALSE;
|
| 236 |
|
|
}
|
| 237 |
|
|
|
| 238 |
|
|
if( ulIdleLoops == ulLastIdleLoopCount )
|
| 239 |
|
|
{
|
| 240 |
|
|
sNoErrorFound = pdFALSE;
|
| 241 |
|
|
}
|
| 242 |
|
|
else
|
| 243 |
|
|
{
|
| 244 |
|
|
ulLastIdleLoopCount = ulIdleLoops;
|
| 245 |
|
|
}
|
| 246 |
|
|
|
| 247 |
|
|
return sNoErrorFound;
|
| 248 |
|
|
}
|
| 249 |
|
|
/*-----------------------------------------------------------*/
|
| 250 |
|
|
|
| 251 |
|
|
static void prvSetupHardware( void )
|
| 252 |
|
|
{
|
| 253 |
|
|
/* Stop the watchdog. */
|
| 254 |
|
|
WDTCTL = WDTPW + WDTHOLD;
|
| 255 |
|
|
|
| 256 |
|
|
/* Setup DCO+ for ( xtal * D * (N + 1) ) operation. */
|
| 257 |
|
|
FLL_CTL0 |= DCOPLUS + XCAP18PF;
|
| 258 |
|
|
|
| 259 |
|
|
/* X2 DCO frequency, 8MHz nominal DCO */
|
| 260 |
|
|
SCFI0 |= FN_4;
|
| 261 |
|
|
|
| 262 |
|
|
/* (121+1) x 32768 x 2 = 7.99 Mhz */
|
| 263 |
|
|
SCFQCTL = mainMAX_FREQUENCY;
|
| 264 |
|
|
|
| 265 |
|
|
/* Setup the IO. This is just copied from the demo supplied by SoftBaugh
|
| 266 |
|
|
for the ES449 demo board. */
|
| 267 |
|
|
P1SEL = 0x32;
|
| 268 |
|
|
P2SEL = 0x00;
|
| 269 |
|
|
P3SEL = 0x00;
|
| 270 |
|
|
P4SEL = 0xFC;
|
| 271 |
|
|
P5SEL = 0xFF;
|
| 272 |
|
|
}
|
| 273 |
|
|
/*-----------------------------------------------------------*/
|
| 274 |
|
|
|
| 275 |
|
|
/* The idle hook is just a copy of the standard integer maths tasks. See
|
| 276 |
|
|
Demo/Common/integer.c for rationale. */
|
| 277 |
|
|
|
| 278 |
|
|
void vApplicationIdleHook( void ) __toplevel
|
| 279 |
|
|
{
|
| 280 |
|
|
/* These variables are all effectively set to constants so they are volatile to
|
| 281 |
|
|
ensure the compiler does not just get rid of them. */
|
| 282 |
|
|
volatile long lValue;
|
| 283 |
|
|
volatile signed portBASE_TYPE *pxTaskHasExecuted;
|
| 284 |
|
|
|
| 285 |
|
|
/* Keep performing a calculation and checking the result against a constant. */
|
| 286 |
|
|
for( ;; )
|
| 287 |
|
|
{
|
| 288 |
|
|
/* Perform the calculation. This will store partial value in
|
| 289 |
|
|
registers, resulting in a good test of the context switch mechanism. */
|
| 290 |
|
|
lValue = intgCONST1;
|
| 291 |
|
|
lValue += intgCONST2;
|
| 292 |
|
|
|
| 293 |
|
|
/* Yield in case cooperative scheduling is being used. */
|
| 294 |
|
|
#if configUSE_PREEMPTION == 0
|
| 295 |
|
|
{
|
| 296 |
|
|
taskYIELD();
|
| 297 |
|
|
}
|
| 298 |
|
|
#endif
|
| 299 |
|
|
|
| 300 |
|
|
/* Finish off the calculation. */
|
| 301 |
|
|
lValue *= intgCONST3;
|
| 302 |
|
|
lValue /= intgCONST4;
|
| 303 |
|
|
|
| 304 |
|
|
/* If the calculation is found to be incorrect we stop setting the
|
| 305 |
|
|
TaskHasExecuted variable so the check task can see an error has
|
| 306 |
|
|
occurred. */
|
| 307 |
|
|
if( lValue != intgEXPECTED_ANSWER ) /*lint !e774 volatile used to prevent this being optimised out. */
|
| 308 |
|
|
{
|
| 309 |
|
|
/* Don't bother with mutual exclusion - it is only read from the
|
| 310 |
|
|
check task and never written. */
|
| 311 |
|
|
xLocalError = pdTRUE;
|
| 312 |
|
|
}
|
| 313 |
|
|
/* Yield in case cooperative scheduling is being used. */
|
| 314 |
|
|
#if configUSE_PREEMPTION == 0
|
| 315 |
|
|
{
|
| 316 |
|
|
taskYIELD();
|
| 317 |
|
|
}
|
| 318 |
|
|
#endif
|
| 319 |
|
|
|
| 320 |
|
|
ulIdleLoops++;
|
| 321 |
|
|
|
| 322 |
|
|
/* Place the processor into low power mode. */
|
| 323 |
|
|
LPM3;
|
| 324 |
|
|
}
|
| 325 |
|
|
}
|
| 326 |
|
|
|
| 327 |
|
|
|
| 328 |
|
|
|
| 329 |
|
|
|
| 330 |
|
|
|