#ifndef INA219_INC
#define INA219_INC				1

#include	<io_regs.h>
#include	<io_i2c.h>
#include	<my_defs.h>


/* Routines to access INA219 Current/Power Monitor with I2C Interface */

// INA219 RETURN CODES
#define INA219_OVF_FALSE		0
#define INA219_OVF_TRUE			1

// INA219 I2C ADDRESS BYTES
#define INA219_ADDR_WR          0x80
#define INA219_ADDR_RD          0x81

// INA219 REGISTERS
#define INA219_CONFIG_REG       0x00    // configuration register
#define INA219_SHUNT_V_REG      0x01    // differential shunt voltage
#define INA219_BUS_V_REG        0x02    // bus voltage (wrt to system/chip GND)
#define INA219_POWER_REG        0x03    // system power draw (= V_BUS * I_SHUNT)
#define INA219_CURRENT_REG      0x04    // shunt current
#define INA219_CALIB_REG        0x05    // calibration register

// INA219 CONFIG REG DEFAULTS (p26-27 in datasheet)
#define D_RANGE                 0       // 0-16V bus voltage range
#define D_GAIN                  0       // gain=1, +/-40mV shunt range
#define D_BUS_ADC               13      // collect 32 samples to average
#define D_SHUNT_ADC             13      // collect 32 samples to average
#define D_MODE                  0       // power down mode

// INA219 CONFIG REG BIT LABELS
#define RST     				15
#define BRNG    				13
#define PG1     				12
#define PG0     				11
#define BADC4   				10
#define BADC3   				9
#define BADC2   				8
#define BADC1   				7
#define SADC4   				6
#define SADC3   				5
#define SADC2   				4
#define SADC1   				3
#define MODE3   				2
#define MODE2   				1
#define MODE1   				0

// INA219 CALIB REG DEFAULTS (p17-22 in datasheet)
#define D_SHUNT                 0.050	//value of shunt in Ohms
#define D_V_SHUNT_MAX           0.04	//maximum value of voltage across shunt
#define D_V_BUS_MAX             16		//maximum voltage of bus
#define D_I_MAX_EXPECTED        0.8		//maximum current draw of bus + shunt
#define D_CAL_REG				0x8000	//default values are for a 0.050 Ohm shunt on a variable VBATT bus with max current of 0.8A

// INA219 Mode Vals used by INA219_SetMode()
#define POWER_DOWN_MODE         0
#define SHUNT_V_TRIG_MODE       1
#define BUS_V_TRIG_MODE         2
#define SHUNT_BUS_V_TRIG_MODE   3
#define ADC_OFF_MODE            4
#define SHUNT_V_CONT_MODE       5
#define BUS_V_CONT_MODE         6
#define SHUNT_BUS_V_CONT_MODE   7

// INA219 RESET VALUE - INA219_Reset()
#define INA219_RESET_VAL        0xFF

// INA219 CONVERT TIMEOUT - INA219_ReadSensor()
#define INA219_CONV_TIMEOUT     250000     //250 ms Data Acquisition Timeout (CF2 elapsed timer func uses microsecs)

// INA219 BIT MASK VALUES
#define OVF_BIT_MASK            0x01
#define CNVR_BIT_MASK           0x02
#define MODE_BITS_MASK			0xF8


// INA219 PROTOTYPES
int   INA219_Init(ushort Range, ushort Gain, ushort Bus_ADC, ushort Shunt_ADC, uchar Mode, ushort Calib);
int   INA219_ReadVoltageCurrent(float *Volts, float *Amps);
int   INA219_ReadPowerMonitor(float *Volts, float *Svolts, float *Amps, float *Watts);
int   INA219_ReadSensor(ushort *E_cts, ushort *S_cts, ushort *I_cts, ushort *P_cts, uchar *OVF);
int   INA219_SetMode(uchar Mode);
int   INA219_Reset();

#endif