/****************************************************************************/
/* Copyright 1994 to 1996 MBARI                                             */
/****************************************************************************/
/* Summary  : Pan & Tilt Daughter Board Rev 2.0 Hardware Definitions        */
/* Filename : ptBoard.h                                                     */
/* Author   : Andrew Pearce                                                 */
/* Project  : Tiburon ROV Data Concentrator                                 */
/* Version  : 2.0                                                           */
/* Created  : 04/08/94                                                      */
/* Modified : 03/18/96                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header: /usr/tiburon/.cvsroot/micro/h/ptboard.h,v 1.3 1997/09/09 17:30:36 pean Exp $
 * $Log: ptboard.h,v $
 * Revision 1.3  1997/09/09 17:30:36  pean
 * *** empty log message ***
 *
 * Revision 1.2  1997/05/07 15:58:41  pean
 * Cleaned up Include files and makefile for new directory structure.
 *
 * Revision 1.2  95/11/10  14:42:25  14:42:25  pean (Andrew Pearce)
 * Added Software Position Limit Switches. Fixed Temeprature Sensor Range
 *
 * Revision 1.1  95/03/24  10:04:45  10:04:45  pean (Andrew Pearce)
 * Initial revision
 *
 */
/****************************************************************************/

#define SCC_CHAN_PER_UART      8       /* * serial channels per UART        */

#define PT_COUNT               4       /* Number of Pan/Tilt units          */
#define PT_TIMEOUT            10       /* 0.1 second timeout for cmds       */

#define PT_CTRL_STATUS_REG_ADDR     0xA000 /* Pan/Tilt Controller Status Reg*/
#define PT_UART_ADDRESS     (Byte*) 0xA200 /* Base address of Pan/Tilt UART */
#define PT_ADC_SELECT_ALL           0xA400 /* Select A/D convertors 0,1,2,3 */
#define PT_ADC_SELECT_0             0xA600 /* Select A/D convertor 0        */
#define PT_ADC_SELECT_1             0xA800 /* Select A/D convertor 1        */
#define PT_ADC_SELECT_2             0xAA00 /* Select A/D convertor 2        */
#define PT_ADC_SELECT_3             0xAC00 /* Select A/D convertor 3        */
#define PT_ADC_DESELECT_ALL         0xAE00 /* Deselect A/D convertors       */

#define PT_UART_CLOCK_FREQ  3686400L   /* UART clock frequency              */

#define PT_UART_BUF_SIZE         128   /* Transmit & Receive Buffer Sizes   */
#define PT_UART_CHAN_OFFSET     0x08   /* Each channel occupies 8 bytes     */

#define LINE_FEED               0x0a   /* Line feed character               */
#define CARRIAGE_RET            0x0d   /* Carriage Return character         */

                                       /* Return register absolute address  */
                                       /* given channel number and reg ID   */
#define UART_REG(chan, reg) \
    ((Byte *) (uartBaseAddress + (chan * PT_UART_CHAN_OFFSET + reg) ))

                                       /* * DAC Output Channel Assignments **/
#define PAN0_CW_LIMIT_CHAN         0   /* Pan 0 Clockwise Limit Chan        */
#define PAN0_CCW_LIMIT_CHAN        1   /* Pan 0 Counter Clockwise Limit Chan*/
#define TILT0_DOWN_LIMIT_CHAN      2   /* Tilt 0 Down Limit Chan            */
#define TILT0_UP_LIMIT_CHAN        3   /* Tilt 0 Up Limit Chan              */
#define PAN1_CW_LIMIT_CHAN         4   /* Pan 1 Clockwise Limit Chan        */
#define PAN1_CCW_LIMIT_CHAN        5   /* Pan 1 Counter Clockwise Limit Chan*/
#define TILT1_DOWN_LIMIT_CHAN      6   /* Tilt 1 Down Limit Chan            */
#define TILT1_UP_LIMIT_CHAN        7   /* Tilt 1 Up Limit Chan              */

#define PT_LED_CONTROL_CHAN        0   /* LED is connected to UART channel 0*/

#define PT_SERIAL_CHAN(chan)       chan       /* Serial channels map 1 to 1 */
#define PT_DAC_CW_CHAN(chan)  (2 * chan)      /* CW Limit D to A Channel    */
#define PT_DAC_CCW_CHAN(chan) (2 * chan + 1)  /* CCW Limit D to A Channel   */
#define PT_ADC_CHAN(chan)     (4 + chan)      /* A/D channels map 4 to 1    */

#define PT_ADC_ALL             0xff    /* Read all A/D Channels at once     */
#define PT_ADC_SELECT_TIME        6    /* Approx 24 usec ADC select pulse   */
#define PT_ADC_TIMEOUT        12500    /* 20 msec timeout for ADC reads     */

                                       /* ****** Micro A/D Channels ******  */
#define STEPPER_CTRL_TEMP_CHAN  ADC_CHAN_1  /* Stepper Controller Temp      */
#define TEMPERATURE_SENSOR_CHAN ADC_CHAN_4  /* Temperature sensor           */
#define HUMIDITY_SENSOR_CHAN    ADC_CHAN_5  /* Humidity sensor              */

                                       /* ***** Micro Port 1 Assigments     */
#define PT_ATOD_CAL_ENABLE	0x01   /* A to D Calibrate Line  P1.0       */
#define PT_DAC_SELECT           0x08   /* DAC Chip Select Output P1.3       */
#define PT_DAC_SCLOCK           0x40   /* DAC Data Clock Output  P1.6       */
#define PT_DAC_SDATA            0x80   /* DAC Data Input         P1.7       */

#define PT_DAC_MAX_CHAN            8   /* DAC Channel count                 */
#define PT_DAC_BITS                8   /* 8 Bit DAC                         */

#define PT_DAC_UNBUFFERED          0
#define PT_DAC_HALF_BUFFERED       1
#define PT_DAC_FULL_BUFFERED       2

typedef struct
{
    struct                             /* Status Flag bits                  */
    {
        unsigned chanOpen     : 1;     /* TRUE if channel is currently open */
        unsigned txBusy       : 1;     /* Transmitter busy for TxStartUp    */
        unsigned txStarted    : 1;     /* Transmitter has started running   */
    } flags;

    Byte rxTerm;                       /* Receive termination character     */
    Byte termRecvd;                    /* Termination Character Count       */
    Byte lastTxByte;

    Ring readBuf;                      /* Ring buffer for serial read       */
    Ring writeBuf;                     /* Ring buffer for serial write      */

    Byte readBuffer[PT_UART_BUF_SIZE]; /* Read buffer                       */
    Byte writeBuffer[PT_UART_BUF_SIZE];/* Write buffer                      */
} sccUartChan;

#ifdef __STDC__

Void initDaughterBoard( Void );        /* Initialize Daughter Board hardware*/
Void toggleDaughterBoardLed( Void );   /* Toggle Daughter Board LED         */

Int16 readTemperature( Int16 channel );/* Read Temperature Sensor           */
Int16 readHumiditySensor( Void );      /* Read housing relative humidity    */

Byte  panTiltStatusReg( Void );        /* Read Moving/Fault Bit Status Reg  */
Nat16 readPanTiltAtoD( Nat16 chan );   /* Read Single Pan/Tilt A/D          */
Int16 readAllPanTiltAtoD( Nat16 rawAtoD[] ); /* Read All Pan/Tilt A/Ds      */
Void calibratePanTiltAtoD( Void );     /* Calibrate Daughter Board A/D      */

Void  ptUartOpenSerialChan(Nat16 chan);
Void  ptUartCloseSerialChan(Nat16 chan);
Int16 ptUartSerialChanRead( Nat16 chan, Byte *buffer, Int16 nBytes);
Int16 ptUartSerialChanWrite(Nat16 chan, Byte *buffer, Int16 nBytes);

Int16 ptUartTermRecvd(Nat16 chan);     /* Return number of term chars recvd*/
Int16 ptUartBytesRecvd(Nat16 chan);    /* Return number of bytes received  */
Int16 ptUartWaitForData(Nat16 chan);   /* Wait for characters to arrive    */
Int16 ptUartWaitForTerm(Nat16 chan);   /* Wait for a term delimited string */

Int16 setPanTiltDtoABufMode( Int16 bufMode );
Int16 writePanTiltDtoA( Int16 chan, Byte value );

                                       /* Flush (Discard) Rx & Tx bytes    */
Void  ptUartSerialChanRxTxFlush(Nat16 chan);

Void  ptUartIsr( Void );               /* pt UART Interrupt Service Routine*/

#endif







