#ifndef __MS5192T_H__
#define __MS5192T_H__


/* MS5192T GPIO */
#define MS5192T_RDY_STATE       GPIO1_STATE

/*MS5192T Registers*/
#define MS5192T_REG_COMM		0 /* Communications Register(WO, 8-bit) */
#define MS5192T_REG_STAT	    0 /* Status Register	    (RO, 8-bit) */
#define MS5192T_REG_MODE	    1 /* Mode Register	     	(RW, 16-bit */
#define MS5192T_REG_CONF	    2 /* Configuration Register (RW, 16-bit)*/
#define MS5192T_REG_DATA	    3 /* Data Register	     	(RO, 16-/24-bit) */
#define MS5192T_REG_ID	        4 /* ID Register	     	(RO, 8-bit) */
#define MS5192T_REG_IO	        5 /* IO Register	     	(RO, 8-bit) */
#define MS5192T_REG_OFFSET      6 /* Offset Register	    (RW, 24-bit */
#define MS5192T_REG_FULLSALE	7 /* Full-Scale Register	(RW, 24-bit */

/* Communications Register Bit Designations (AD7793_REG_COMM) */
#define MS5192T_COMM_WEN		(1 << 7) 			/* Write Enable */
#define MS5192T_COMM_WRITE	    (0 << 6) 			/* Write Operation */
#define MS5192T_COMM_READ       (1 << 6) 			/* Read Operation */
#define MS5192T_COMM_ADDR(x)	(((x) & 0x7) << 3)	/* Register Address */
#define MS5192T_COMM_CREAD	    (1 << 2) 			/* Continuous Read of Data Register */

/* Status Register Bit Designations (AD7793_REG_STAT) */
#define MS5192T_STAT_RDY		(1 << 7) /* Ready */
#define MS5192T_STAT_ERR		(1 << 6) /* Error (Overrange, Underrange) */
#define MS5192T_STAT_CH3		(1 << 2) /* Channel 3 */
#define MS5192T_STAT_CH2		(1 << 1) /* Channel 2 */
#define MS5192T_STAT_CH1		(1 << 0) /* Channel 1 */

/* Mode Register Bit Designations (MS5192T_REG_MODE) */
#define MS5192T_MODE_SEL(x)		(((x) & 0x7) << 13)	/* Operation Mode Select */
#define MS5192T_MODE_CLKSRC(x)	(((x) & 0x3) << 6) 	/* ADC Clock Source Select */
#define MS5192T_MODE_RATE(x)	((x) & 0xF) 		/* Filter Update Rate Select */

/* AD7793_MODE_SEL(x) options */
#define MS5192T_MODE_CONT       0 /* Continuous Conversion Mode */
#define MS5192T_MODE_SINGLE		1 /* Single Conversion Mode */
#define MS5192T_MODE_IDLE		2 /* Idle Mode */
#define MS5192T_MODE_PWRDN      3 /* Power-Down Mode */
#define MS5192T_MODE_CAL_INT_ZERO 4 /* Internal Zero-Scale Calibration */
#define MS5192T_MODE_CAL_INT_FULL 5 /* Internal Full-Scale Calibration */
#define MS5192T_MODE_CAL_SYS_ZERO 6 /* System Zero-Scale Calibration */
#define MS5192T_MODE_CAL_SYS_FULL 7 /* System Full-Scale Calibration */

/* MS5192T_MODE_CLKSRC(x) options */
#define MS5192T_CLK_INT		    0 /* Internal 64 kHz Clk not available at the CLK pin */
#define MS5192T_CLK_INT_CO	    1 /* Internal 64 kHz Clk available at the CLK pin */
#define MS5192T_CLK_EXT		    2 /* External 64 kHz Clock */
#define MS5192T_CLK_EXT_DIV2	3 /* External Clock divided by 2 */

/* MS5192T update rate */
#define MS5192T_UP_RATE_500     1 /* 500 Hz */
#define MS5192T_UP_RATE_250     2 /* 250 Hz */
#define MS5192T_UP_RATE_125     3 /* 125 Hz */
#define MS5192T_UP_RATE_62      4 /* 62.5 Hz */
#define MS5192T_UP_RATE_50      5 /* 50 Hz */
#define MS5192T_UP_RATE_39      6 /* 39.2 Hz */
#define MS5192T_UP_RATE_33      7 /* 33.3 Hz */
#define MS5192T_UP_RATE_19      8 /* 19.2 Hz */
#define MS5192T_UP_RATE_16      9 /* 16.7 Hz */
#define MS5192T_UP_RATE_14      10 /* 14 Hz */
#define MS5192T_UP_RATE_12      11 /* 12.5 Hz */
#define MS5192T_UP_RATE_10      12 /* 500 Hz */
#define MS5192T_UP_RATE_8       13 /* 8.33 Hz */
#define MS5192T_UP_RATE_6       14 /* 6.25 Hz */
#define MS5192T_UP_RATE_4       15 /* 4.17 Hz */

/* Configuration Register Bit Designations (MS5192T_REG_CONF) */
#define MS5192T_CONF_VBIAS(x)   (((x) & 0x3) << 14) 	/* Bias Voltage Generator Enable */
#define MS5192T_CONF_BO_EN	    (1 << 13) 			/* Burnout Current Enable */
#define MS5192T_CONF_UNIPOLAR   (1 << 12) 			/* Unipolar/Bipolar Enable */
#define MS5192T_CONF_BOOST	    (1 << 11) 			/* Boost Enable */
#define MS5192T_CONF_GAIN(x)	(((x) & 0x7) << 8) 	/* Gain Select */
#define MS5192T_CONF_REFSEL(x)  (((x) & 0x1) << 7) 	/* INT/EXT Reference Select */
#define MS5192T_CONF_BUF		(1 << 4) 				/* Buffered Mode Enable */
#define MS5192T_CONF_CHAN(x)	((x) & 0x7) 			/* Channel select */

/* MS5192T_CONF_GAIN(x) options */
#define MS5192T_GAIN_1          0
#define MS5192T_GAIN_2          1
#define MS5192T_GAIN_4          2
#define MS5192T_GAIN_8          3
#define MS5192T_GAIN_16         4
#define MS5192T_GAIN_32         5
#define MS5192T_GAIN_64         6
#define MS5192T_GAIN_128        7

/* MS5192T_CONF_REFSEL(x) options */
#define MS5192T_REFSEL_INT      1	/* Internal Reference Selected. */
#define MS5192T_REFSEL_EXT      0	/* External Reference Applied between REFIN(+) and REFIN(–). */

/* MS5192T_CONF_CHAN(x) options */
#define MS5192T_CH_AIN1P_AIN1M	0 /* AIN1(+) - AIN1(-) */
#define MS5192T_CH_AIN2P_AIN2M	1 /* AIN2(+) - AIN2(-) */
#define MS5192T_CH_AIN3P_AIN3M	2 /* AIN3(+) - AIN3(-) */
#define MS5192T_CH_AIN1M_AIN1M	3 /* AIN1(-) - AIN1(-) */
#define MS5192T_CH_TEMP			6 /* Temp Sensor */
#define MS5192T_CH_AVDD_MONITOR	7 /* AVDD Monitor */

/* ID Register Bit Designations (AD7793_REG_ID) */
#define MS5192T_ID			    0xA
#define MS5192T_ID_MASK		    0xF

/* IO (Excitation Current Sources) Register Bit Designations (MS5192T_REG_IO) */
#define MS5192T_IEXCDIR(x)	    (((x) & 0x3) << 2)
#define MS5192T_IEXCEN(x)	    (((x) & 0x3) << 0)

/* MS5192T_IEXCDIR(x) options*/
#define MS5192T_DIR_IEXC1_IOUT1_IEXC2_IOUT2	0  /* IEXC1 connect to IOUT1, IEXC2 connect to IOUT2 */
#define MS5192T_DIR_IEXC1_IOUT2_IEXC2_IOUT1	1  /* IEXC1 connect to IOUT2, IEXC2 connect to IOUT1 */
#define MS5192T_DIR_IEXC1_IEXC2_IOUT1		2  /* Both current sources IEXC1,2 connect to IOUT1  */
#define MS5192T_DIR_IEXC1_IEXC2_IOUT2		3  /* Both current sources IEXC1,2 connect to IOUT2 */

/* MS5192T_IEXCEN(x) options*/
#define MS5192T_EN_IXCEN_10uA				1  /* Excitation Current 10uA */
#define MS5192T_EN_IXCEN_210uA				2  /* Excitation Current 210uA */
#define MS5192T_EN_IXCEN_1mA			    3  /* Excitation Current 1mA */

/******************************************************************************/
/* Functions Prototypes                                                       */
/******************************************************************************/

/* Initialize MS5192T and check if the device is present*/
unsigned char MS5192T_Init(void);

/* Sends 32 consecutive 1's on SPI in order to reset the part. */
void MS5192T_Reset(void);

/* Reads the value of the selected register. */
unsigned long MS5192T_GetRegisterValue(unsigned char regAddress, 
									  unsigned char size,
                                      unsigned char modifyCS);

/* Writes a value to the register. */
void MS5192T_SetRegisterValue(unsigned char regAddress,
							 unsigned long regValue, 
							 unsigned char size,
                             unsigned char modifyCS);

/* Waits for RDY pin to go low. */
void MS5192T_WaitRdyGoLow(void);

/* Sets the operating mode of MS5192T. */
void MS5192T_SetMode(unsigned long mode);     

/* Sets update rate */
void MS5192T_SetUpdateRate(unsigned long rate);

/* Selects the channel of MS5192T. */
void MS5192T_SetChannel(unsigned long channel);

/* Sets the gain of the In-Amp. */
void MS5192T_SetGain(unsigned long gain);

/* Unipolar/Bipolar Enable */
void MS5192T_SetPolar(unsigned long polar);

/* Sets the reference source for the ADC. */
void MS5192T_SetIntReference(unsigned char type);

/* Performs the given calibration to the specified channel. */
void MS5192T_Calibrate(unsigned char mode, unsigned char channel);

/* Returns the result of a single conversion. */
unsigned long MS5192T_SingleConversion(void);

/* Returns the average of several conversion results. */
unsigned long MS5192T_ContinuousReadAvg(unsigned char sampleNumber);

#endif	// _AD7793_H_
