/*
 * i2c_isl28022.h
 *
 *  Created on: 2023/09/14
 *      Author: BestTechnology
 */

#include "hal_data.h"

#include <stdint.h>

#ifndef POWER_MONITOR_H_
#define POWER_MONITOR_H_

#define ISL28022_I2C_SLAVEADDRESS   0x40

#define ISLREG_BRNG_16V         0
#define ISLREG_BRNG_32V         1
#define ISLREG_BRNG_60V         2
#define ISLREG_BRNG_60V_        3

#define ISLREG_PGA_40mV         0
#define ISLREG_PGA_80mV         1
#define ISLREG_PGA_160mV        2
#define ISLREG_PGA_320mV        3

#define ISLREG_ADC_12b_72us     0
#define ISLREG_ADC_13b_132us    1
#define ISLREG_ADC_14b_258us    2
#define ISLREG_ADC_15b_508us    3
#define ISLREG_ADC_15b_508us_   8
#define ISLREG_ADC_2S_1ms       9
#define ISLREG_ADC_4S_2ms       10
#define ISLREG_ADC_8S_4ms       11
#define ISLREG_ADC_16S_8ms      12
#define ISLREG_ADC_32S_16ms     13
#define ISLREG_ADC_64S_32ms     14
#define ISLREG_ADC_128S_64ms    15

#define ISLREG_MODE_POWDWN      0
#define ISLREG_MODE_SHT         1
#define ISLREG_MODE_BUS         2
#define ISLREG_MODE_SHTBUS      3
#define ISLREG_MODE_ADCOFF      4
#define ISLREG_MODE_SHTCONT     5
#define ISLREG_MODE_BUSCONT     6
#define ISLREG_MODE_SHTBUSCONT  7

#define ISLREG_ADDR_CONFIG          0
#define ISLREG_ADDR_SHUNTVOLTAGE    1
#define ISLREG_ADDR_BUSVOLTAGE      2
#define ISLREG_ADDR_POWER           3
#define ISLREG_ADDR_CURRENT         4
#define ISLREG_ADDR_CALIBRATIONREG  5
#define ISLREG_ADDR_SHUNTVOLTAGETH  6
#define ISLREG_ADDR_BUSVOLTAGETH    7
#define ISLREG_ADDR_DCSINTSTAT      8
#define ISLREG_ADDR_AUXCTRLREG      9


typedef enum { tisl_none, tisl_busy, tisl_term } Tisl_comstat;

bool isl28022_init (uint8_t brng, uint8_t pga, uint8_t badc, uint8_t sadc, uint8_t mode, uint16_t cal);
bool isl28022_write (uint8_t addr, uint16_t val);
bool isl28022_read (uint8_t addr, uint16_t *dest);
bool isl28022_read_ex (uint8_t addr, uint16_t *dest);
extern Tisl_comstat isl28022_comp_stat (void);

extern void isl28022_force_reset_stat (void);

#endif /* POWER_MONITOR_H_ */
