/*********************************  dwarfser.h  ******************************
 * $Source: /home/cvs/ESP/gen2/software/msp430/include/dwarfser.h,v $
 *  Copyright (C) 2004 MBARI
 *
 *  MBARI Proprietary Information. All rights reserved.
 * $Id: dwarfser.h,v 1.1 2005/11/24 00:25:17 brent Exp $
 *
 * Misc. declarations for low-level control of dwarf's serial channels
 *
 *****************************************************************************/

#ifndef DWARFSER_H
#define DWARFSER_H

//Derived timing constants
#define NEARESTDIV(fast,slow)  (((fast)+(slow)/2)/(slow))
#define MCLKRATE     NEARESTDIV(CPURATE,MAXBAUDRATE)*MAXBAUDRATE
#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 appState {
  running,        // normal running state
  breakPending,   // waiting to assert BREAK to host
  sendingBreak    // asserting break to host
}; 

extern enum appState appState;

// Assert and Deassert RS-232 BREAK signal

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


//assert RS-232 break after all data has clocked out
//call with interrupts disabled
static inline void startBRK(void) {
  if (appState == running) appState = breakPending;
}


//deassert RS-232 break
//call with interrupts disabled
static inline void endBRK(void) {
  DEASSERTBRK(); appState = running;
}


extern byte _mainFlags;

static inline void dwarfEnableRTS(void)
//allow the host to throttle our serial RS-232 output with RTS
{
  _mainFlags |= DWRFRTS;
}

static inline void dwarfDisableRTS(void)
//ignore host's RS-232 RTS flow control line
{
  _mainFlags &= ~DWRFRTS;
}

//non-zero if RTS handshake is enabled
#define dwarfUsingRTS()  (_mainFlags & DWRFRTS)

//non-zero if host is not asserting RTS
#define dwarfLostRTS() (DWRFIRQIN & DWRFRTS)


#endif

