/************************************************************************/
/* 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 <sys/types.h>

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

#include <sensorNode.h>
#include <modbus.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)

/* Message response timeout in microseconds (== 600ms) */
#define RESPONSE_TIMEOUT 600000

/* Inter-character timeout in microseconds (== 50ms)    */
#define BYTE_TIMEOUT 50000


typedef struct remSerPort	/****************************************/
{				/* Remote Serial Port			*/
    struct sensorNodeStruct *snp; /* SensorNodeStruct for this ser port	*/
    modbus_t	*ctx;		/* libmodbus context from snp for convenience*/
    uint16_t	port;		/* Serial port on the slave		*/
    uint16_t	useCount;	/* Number of times opened		*/
    uint16_t	bufChars;	/* Number of chars valid in read buffer	*/
    uint16_t	bufIndex;	/* Read buffer output index		*/
    uint16_t	rbuf[SERREG_SIZE+1]; /* Read Register Buffer		*/
    uint16_t	wbuf[SERREG_SIZE+1]; /* Write Register Buffer		*/
} RemSerPort;			/****************************************/

typedef struct sensorNodeStruct	/****************************************/
{				/* Modbus Connection to SensorNode	*/
    modbus_t	*ctx;		/* libmodbus context			*/
    uint16_t	slaveID;	/* SlaveID on this connection		*/
    uint16_t	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					*/
/* Note that the Modbus addresses below use the Modbus convention where */
/* address n is represented on Modbus by (n-1).  I would have expected	*/
/* libmodbus to make this transparent, but it doesn't.			*/
/************************************************************************/
#define SerialNum		(0x0f)
#define SoftwareRev		(0x10)
#define Config1Reg		(0x11)
#define Config2Reg		(0x12)
#define PwrStatusReg		(0x13)
#define HwConfigReg		(0x14)
#define MbTimeoutReg		(0x15)

#define SerStatusReg		(0xfe)
#define TxBusy(val, port)	(val & (1 << (4*port-1)))
#define RxAvail(val, port)	(val & (1 << (4*port)))
#define TxErr(val, port)	(val & (1 << (4*port+1)))

#define BaudReg(port)		((0x100 * port) + 0xff)
#define ModeReg(port)		((0x100 * port) + 0x100)
#define RxCtlReg(port)		((0x100 * port) + 0x101)
#define RxErrReg(port)		((0x100 * port) + 0x103)
#define RxQReg(port)		((0x100 * port) + 0x106)
#define TxQueuedReg(port)	((0x100 * port) + 0x182)
#define TxCtlReg(port)		((0x100 * port) + 0x183)
#define TxBreakReg(port)	((0x100 * port) + 0x184)
#define TxReqReg(port)		((0x100 * port) + 0x186)

#define PowerBit(port)		(port)
#define TxEnable(port)		(port + 4)
#define BlinkyDebugCoil		(9)
#define BlinkyStateCoil		(10)
#define DCOnCoil		(11)
#define ConfigModeCoil		(12)

#define SN_AD_CHANS		8
#define AnalogReg(chan)		(chan + 0x1f)
#define AnalogAvgReg(chan)	((2 * chan) + 0x2f)
#define AnalogAvgMsReg(chan)	((2 * chan) + 0x30)

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

#endif	/* INCsnLibh */
