/************************************************************************/
/* Copyright 2009-2013 MBARI						*/
/************************************************************************/
/* Summary  : Definitions for serial/power/analog I/O to SensorNode	*/
/* Filename : snLib.h							*/
/* Author   : Robert Herlien (rah)					*/
/* Project  : xFOCE							*/
/* Revision: 1.0							*/
/* Created  : 09/19/2012 from Oasis4 remote.h				*/
/*									    */
/* MBARI provides this documentation and code "as is", with no warranty,    */
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium  */
/* Research Institute to assist in its use, correction, modification, or    */
/* enhancement. This information should not be published or distributed to  */
/* third parties without specific written permission from MBARI.            */
/*									    */
/************************************************************************/
/* Modification History:						*/
/* 19sep2012 rah - created from remote.h in Oasis4			*/
/************************************************************************/

#ifndef INCsnLibh
#define INCsnLibh
#define SNLIB

#include <modbus.h>

typedef struct remSerPort *RemSerHandle;
typedef struct sensorNodeStruct *SensorNodeHandle;

#include <sensorNode.h>


/****************************************/
/* Typedefs and definitions		*/
/****************************************/

#define DEVNAME_LEN	256		/* Length of device name field	*/
#define SERBUF_SIZE	240		/* Size of serial buffer	*/	
#define SERREG_SIZE	(SERBUF_SIZE/2)


typedef struct remSerPort	/****************************************/
{				/* Remote Serial Port			*/
    struct sensorNodeStruct *snp; /* SensorNodeStruct for this ser port	*/
    modbus_t	*ctx;		/* libmodbus context from snp for convenience*/
    Nat16	port;		/* Serial port on the slave		*/
    Nat16	useCount;	/* Number of times opened		*/
    Nat16	bufChars;	/* Number of chars valid in buf		*/
    Nat16	rxQueued;	/* RxQueued reg. MUST be immed b4 buf	*/
    Byte	*bufPtr;	/* Ring buffer output ptr		*/
    Byte	rbuf[SERBUF_SIZE];   /* Read Buffer for serial chars	*/
    Nat16	wbuf[SERREG_SIZE+1]; /* Write Register Buffer		*/
} RemSerPort;			/****************************************/

typedef struct sensorNodeStruct	/****************************************/
{				/* Modbus Connection to SensorNode	*/
    modbus_t	*ctx;		/* libmodbus context			*/
    Nat16	slaveID;	/* SlaveID on this connection		*/
    Nat16	useCount;	/* Number of times opened		*/
    char	devName[DEVNAME_LEN];   /* Modbus serial port		*/
    RemSerPort	serPorts[SN_SER_PORTS]; /* SensorNode remote serial ports*/
} SensorNodeStruct;		/****************************************/


/****************************************/
/* Remote Serial I/O Definitions	*/
/****************************************/

#define MIN_READ		10	/* Min number of regs to read	*/

/* Register and Coil Definitions	*/
#define SerStatusReg		0xff
#define TxBusy(val, port)	(val & (1 << (4*port)))
#define RxAvail(val, port)	(val & (1 << (4*port+1)))
#define TxErr(val, port)	(val & (1 << (4*port+2)))

#define BaudReg(port)		(0x100 * (port + 1))
#define ModeReg(port)		((0x100 * port) + 0x101)
#define RxCtlReg(port)		((0x100 * port) + 0x102)
#define RxQReg(port)		((0x100 * port) + 0x107)
#define TxQueuedReg(port)	((0x100 * port) + 0x183)
#define TxCtlReg(port)		((0x100 * port) + 0x184)
#define TxBreakReg(port)	((0x100 * port) + 0x185)
#define TxReqReg(port)		((0x100 * port) + 0x187)

#define PowerBit(port)		(port + 1)
#define TxEnable(port)		(port + 5)
#define BlinkyDebugCoil		(10)
#define BlinkyStateCoil		(11)
#define ADCSwitch		(12)

#define AnalogReg(chan)		((8 * chan) + 0x20)
#define AnalogAvgReg(chan)	((8 * chan) + 0x21)
#define AnalogAvgMsReg(chan)	((8 * chan) + 0x22)

#define REM_NO_PTY      0
#define REM_EVEN_PTY    2
#define REM_ODD_PTY     4
#define REM_STOP2       1
#define REM_TX_FLUSH    0x20
#define REM_RX_FLUSH    0x20
#define REM_BREAK       1

#endif	/* INCsnLibh */
