/*********************************  dwarftop.h  ******************************
 * $Source: /home/cvs/ESP/gen2/software/msp430/include/dwarftop.h,v $
 *  Copyright (C) 2004 MBARI
 *
 *  MBARI Proprietary Information. All rights reserved.
 * $Id: dwarftop.h,v 1.12 2005/04/07 22:31:31 brent Exp $
 *
 * Generic framework for ESP Dwarf applications
 *  Intended to be #included at the top of each drawf application
 *
 *****************************************************************************/

#ifndef DWARFTOP_H
#define DWARFTOP_H

#include "setjmp.h"       //standard 'C' setjmp & longjmp
#include "interrupts.h"   //support for nested interrupts
#include "dco.h"          //Digitally Controller Clock Oscillator
#include "parser.h"       //byte fifo & command parsing primitives

#include "timers.h"       //interval timers

#ifndef RoscSrc  //default to external Rosc
#define RoscSrc DWRFRosc  //DWRFRosc for Rosc on 2.5 (0x20), 0 for internal Rosc
#endif

//Derived timing constants
#define NEARESTDIV(fast,slow)  (((fast)+(slow)/2)/(slow))
#define MCLKRATE     NEARESTDIV(CPURATE,BAUDRATE)*BAUDRATE
#define MCLKSperACLK NEARESTDIV(MCLKRATE,ACLKRATE) // MCLKS/ACLK
#define MCLKSperBAUD NEARESTDIV(MCLKRATE,BAUDRATE) // MCLKS/BAUDRATE

#ifdef I2CRATE
#define MCLKSperI2C  NEARESTDIV(MCLKRATE,I2CRATE)  // MCLKS/I2CRATE
#define I2Clow       (MCLKSperI2C/2)               //low period of I2C clock
#define I2Chigh      (MCLKSperI2C-I2Clow)          //high period of I2C clock
#endif


enum breakState { //state of BREAK processing on built-in cmd serial port
  noBreak,        // normal running state
  breakPending,   // waiting to assert BREAK to host
  sendingBreak    // asserting break to host
}; 

//forward declaration
static void kickstartXmit (void);
static void pollIn (void);
static void startResync (byte USARTerrMask);


// DATA

static enum breakState breakState = noBreak;

static unsigned mclk0;  //previously read MCLK count for DCO adjustment
  
static jmp_buf restartEnv;  //for possible longjmp on error


// Assert and Deassert RS-232 BREAK signal

#define ASSERTBRK()    (clear8 (P3SEL, 0x40)) 
#define DEASSERTBRK()  (set8 (P3SEL, 0x40))

#endif
