


#ifndef _BAT_MONITOR_H_
#define _BAT_MONITOR_H_

#include <stdio.h>
#include <string.h>
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
#include "fsl_debug_console.h"
#include "fsl_lpi2c.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/
#define EXAMPLE_I2C_MASTER_BASE (LPI2C3_BASE)

/* Select USB1 PLL (480 MHz) as master lpi2c clock source */
#define LPI2C_CLOCK_SOURCE_SELECT (0U)
/* Clock divider for master lpi2c clock source */
#define LPI2C_CLOCK_SOURCE_DIVIDER (5U)
/* Get frequency of lpi2c clock */
#define LPI2C_CLOCK_FREQUENCY ((CLOCK_GetFreq(kCLOCK_Usb1PllClk) / 8) / (LPI2C_CLOCK_SOURCE_DIVIDER + 1U))

#define LPI2C_MASTER_CLOCK_FREQUENCY LPI2C_CLOCK_FREQUENCY
#define WAIT_TIME                    10U

#define WAIT_TIME_CONFIG             20000U


#define EXAMPLE_I2C_MASTER ((LPI2C_Type *)EXAMPLE_I2C_MASTER_BASE)

#define LPI2C_MASTER_SLAVE_ADDR_7BIT 0x36U //0x6CU
#define LPI2C_BAUDRATE               400000U

#define LPI2C_REG_ADDRESS_LENGTH     1U
#define LPI2C_DATA_LENGTH            2U





//Registr MAP




enum max1726x_register{
    MAX1726X_STATUS_REG                 = 0x00,
    MAX1726X_VALRTTH_REG                = 0x01,
    MAX1726X_TALRTTH_REG                = 0x02,
    MAX1726X_SALRTTH_REG                = 0x03,
    MAX1726X_REPCAP_REG                 = 0x05,
    MAX1726X_REPSOC_REG                 = 0x06,
    MAX1726X_TEMP_REG                   = 0x08,
    MAX1726X_VCELL_REG                  = 0x09,
    MAX1726X_CURRENT_REG                = 0x0A,
    MAX1726X_AVGCURRENT_REG             = 0x0B,
    MAX1726X_REMCAP_REG                 = 0x0F,

    MAX1726X_FULLCAPREP_REG             = 0x10,
    MAX1726X_TTE_REG                    = 0X11,
    MAX1726X_QRTABLE00_REG              = 0x12,
    MAX1726X_FULLSOCTHR_REG             = 0x13,
    MAX1726X_CYCLES_REG                 = 0x17,
    MAX1726X_DESIGNCAP_REG              = 0x18,
    MAX1726X_AVGVCELL_REG               = 0x19,
    MAX1726X_MAXMINVOLT_REG             = 0x1B,
    MAX1726X_CONFIG_REG                 = 0x1D,
    MAX1726X_ICHGTERM_REG               = 0x1E,

    MAX1726X_TTF_REG                    = 0X20,
    MAX1726X_VERSION_REG                = 0x21,
    MAX1726X_QRTABLE10_REG              = 0x22,
    MAX1726X_FULLCAPNOM_REG             = 0x23,
    MAX1726X_LEARNCFG_REG               = 0x28,
    MAX1726X_RELAXCFG_REG               = 0x2A,
    MAX1726X_TGAIN_REG                  = 0x2C,
    MAX1726X_TOFF_REG                   = 0x2D,

    MAX1726X_QRTABLE20_REG              = 0x32,
    MAX1726X_RCOMP0_REG                 = 0x38,
    MAX1726X_TEMPCO_REG                 = 0x39,
    MAX1726X_VEMPTY_REG                 = 0x3A,
    MAX1726X_FSTAT_REG                  = 0x3D,

    MAX1726X_QRTABLE30_REG              = 0x42,
    MAX1726X_DQACC_REG                  = 0x45,
    MAX1726X_DPACC_REG                  = 0x46,
    MAX1726X_VFSOC0_REG                 = 0x48,
    MAX1726X_QH0_REG                    = 0x4C,
    MAX1726X_QH_REG                     = 0x4D,

    MAX1726X_VFSOC0_QH0_LOCK_REG        = 0x60,
    MAX1726X_LOCK1_REG                  = 0x62,
    MAX1726X_LOCK2_REG                  = 0x63,

    MAX1726X_MODELDATA_START_REG        = 0x80,
    MAX1726X_MODELDATA_RCOMPSEG_REG     = 0xAF,

    MAX1726X_IALRTTH_REG                = 0xB4,
    MAX1726X_CVMIXCAP_REG               = 0xB6,
    MAX1726X_CVHALFIME_REG              = 0xB7,
    MAX1726X_CURVE_REG                  = 0xB9,
    MAX1726X_HIBCFG_REG                 = 0xBA,
    MAX1726X_CONFIG2_REG                = 0xBB,

    MAX1726X_MODELCFG_REG               = 0xDB,

    MAX1726X_OCV_REG                    = 0xFB,
    MAX1726X_VFSOC_REG                  = 0xFF,
};






typedef struct bat_mon_init_values{
	uint16_t DesignCap;
	uint16_t IchgTerm;
	uint16_t VEmpty;
	double ChargeVoltage;
} bat_mon_init;



typedef struct fuelGaugeresults{
	uint16_t RepCap; //charge mAHrs remaining
	uint16_t RepSOC; // Charge in percentage remaining
	uint16_t TTE; // Time to empty

} fuelGaugeresultsHandle;


/*

uint32_t DesignCap_data		=	0x1450	;
uint32_t ichgterm_data		=	0x333	;
uint32_t modelcfg_data		=	0x8000	;
uint32_t QRTable00_data		=	0x1050	;
uint32_t QRTable10_data		=	0x2012	;
uint32_t VEmpty_data			=	0xa561	;
uint32_t RCOMP0_data			=	0x004d	;
uint32_t TempCo_data			=	0x223e	;


uint8_t DesignCap_reg_address		=	0x18;
uint8_t ichgterm_reg_address		=	0x1E;
uint8_t modelcfg_reg_address		=	0XDB;
uint8_t QRTable00_reg_address		=	0X12;
uint8_t QRTable10_reg_address		=	0X22;
uint8_t VEmpty_reg_address			=	0X3A;
uint8_t RCOMP0_reg_address			=	0X38;
uint8_t TempCo_reg_address			=	0X39;

*/


void init_bat_mon_i2c();
uint16_t read_reg_bat_mon(uint8_t reg_address);
status_t write_reg_bat_mon(uint8_t reg_address, uint16_t reg_data);

uint16_t check_por(void);
void init_battery_monitor(bat_mon_init *bat_mon_init_param, bool first_power_on);
status_t read_FuelGauge_results(bat_mon_init *bat_mon_init_param, fuelGaugeresultsHandle *fuelGaugeresults);



#endif
