#ifndef __HAL_SY6970_H__
#define __HAL_SY6970_H__

 
#include "hc32_ll_gpio.h"
#include "hc32_ll_fcg.h"
#include "hc32_ll_interrupts.h"
#include "hc32_ll_tmr0.h"
#include "ring_buf.h"
#include "hc32_ll_aos.h"
 
#if 0
#define SY6970_ADDR  0x6A
  
void sy6970_init(void);
uint8_t sy6970_charge_status(void);
#endif
#define uint8 uint8_t
#define uint16 uint16_t
#define ENABLE								(1)
#define DISABLE								(0)

#define	REG_INPUT_CURRENT_CTR      			(0X00)
#define	REG_ADC_START_CTR      				(0X02)
#define	REG_CHARGE_CTR      				(0X03)
#define	REG_CURRENT_CHARGE_CTR      		(0X04)
#define	REG_VOLTAGE_CHARGE_CTR      		(0X06)

#define REG_CHARGE_STATUS_READ				(0X0B)
#define REG_NTC_STATUS_READ					(0X0C)
#define REG_VOLTAGE_READ					(0X0E)
#define REG_SYS_VOLTAGE_READ				(0X0F)
#define REG_CHARGE_CURRENT_READ				(0X12)
#define REG_CURRENT_STATUS_READ				(0X13)

#define SY6970_ADDR  (0x6A << 1)
#pragma  pack(1)

typedef struct
{
	uint8 IINLIM:6;
	uint8 EN_ILIM:1;
	uint8 EN_HIZ:1;
	/*
	 * Input current limit:
		(Actual input current limit is the lower of I2C or ILIM pin)
		IINLIM=100mA+50mA*[IINLIM]
		Range:100mA(000000)-3.25A(111111)
		000000=100mA
		000001=150mA
		…
		001000=500mA(Default)
		…
		111111=3.25A
		IINLIM will be changed according to the adapter type after input
		DP/DM detection is done.
		USB Host SDP=500mA
		USB CDP=1.5A
		USB DCP=3.25A
*/
}REG00_CONFIG_CON;
typedef struct
{
	uint8 auto_dpdm_en:1;
	uint8 force_dpdm:1;
	uint8 hv_type:1;
	uint8 hvdcp_en:1;
	uint8 aicl_en:1;
	uint8 boost_freq:1;
	uint8 conv_rate:1;
	uint8 conv_start:1;
}REG02_CONFIG_CON;
typedef union
{
	uint8 data;
	REG02_CONFIG_CON config;
}REG02_CONFIG;


typedef struct
{
	uint8 reserved:1;
	uint8 sys_min:3;
	uint8 chg_config:1;
	uint8 otg_config:1;
	uint8 wd_rst:1;
	uint8 bat_load_en:1;
}REG03_CHARGE_CON;
typedef union
{
	uint8 data;
	REG03_CHARGE_CON config;
}REG03_CHARGE;

typedef struct
{
	uint8 ICHG:7; //Fast Charge Current Limit: ICHG=[ICHG]*64mA

	uint8 EN_PUMPX:1;
}REG04_CURRENT_CON;

typedef union
{
	uint8 data;
	REG04_CURRENT_CON config;
}REG04_CURRENT;
typedef struct
{
	uint8 ITERM:4;	// Termination Current Limit: ITERM=64mA+[ITERM]*64mA
	uint8 IPRECHG:4;// Precharge Current Limit:IPRECHG=64mA+[IPRECHG]*64mA
}REG05_CURRENT_CON;

typedef struct
{
	uint8 VRECHG:1;	// Battery Recharge Threshold Offset: 0-100mV 1-200mV
	uint8 BATLOWV:1;// Battery Precharge to Fast Charge Threshold: 0-2.8V 1-3.0V
	uint8 VREG:1;	// Charge Voltage Limit: VREG=3.840V+[VREG]*16mV
}REG06_VREG_CON;

typedef struct
{
	uint8 JEITA_ISET:1;	// JEITA Low Temperature Current Setting:
	//Percentage with respect to ICHG register REG04[6:0]
	//0-50% 1-20%
	uint8 CHG_TIMER:2; // Fast Charge Timer Setting: 00-5 hrs 01-8 hrs 10-12 hrs 11-20 hrs
	uint8 EN_TIMER:1;
	uint8 WATCHDOG:2;//
	uint8 STAT_DIS:1;	//
	uint8 EN_TERM:1;
}REG07_VREG_CON;

typedef struct
{
	uint8 TREG:2;
	uint8 VCLAMP:3;
	uint8 BAT_COMP:3;
}REG08_BAT_CON;

typedef struct
{
	uint8 PUMPX_DN:1; // Current pulse control to request lower voltage:
	uint8 PUMPX_UP:1; // Current pulse control to request higher voltage:
	uint8 BATFET_RST_EN:1; //BATFET Reset Enable:
	uint8 BATFET_DLY:1; // BATFET turn off delay control:
	uint8 JEITA_VSET:1; // JEITA High Temperature Voltage Setting:
	uint8 BATFET_DIS:1; //Force BATFET Off:
	uint8 TMR2X_EN:1; //Safety Timer Setting during Input DPM and Thermal
	uint8 FORCE_AICL:1;//Force Start Adaptive Input Current Limit:
}REG09_CURRENT_CON;

typedef struct
{
	uint8 BOOST_LIM:3; // Boost Mode Current Limit:
	uint8 Reserved:1; // Reserved
	uint8 BOOSTV:4; //Boost Mode Voltage Regulation:VBOOST=4.55V+[BOOSTV]*64mV:
}REG0A_BOOSTV_CON;

typedef struct
{
	uint8 VSYS_STAT:1; //VSYS Regulation Status:0-Not in SYSMIN regulation (VBAT>VSYSMIN) 1-In SYSMIN regulation (VBAT<VSYSMIN)
	uint8 SDP_STAT:1; // Charging status: 00-Not Charging 001-Pre-charge (VBAT <VBATLOWV)
	//10-Fast Charging 11-Charge Termination Done
	uint8 PG_STAT:1; //Power Good Status: 0-Not Power Good 1-Power Good
	uint8 CHRG_STAT:2; // BATFET turn off delay control:
	/*00-Not Charging
	01-Pre-charge (VBAT <VBATLOWV)
	10-Fast Charging
	11-Charge Termination Done
	uint8 BUS_STAT:2;*/
	uint8 BUS_STAT:2; //BUS Status register:
	/*000:No input
	001:USB Host SDP
	010:USB CDP
	011:USB DCP
	100:HVDCP
	101:Unknown Adapter
	110:Non-Standard Adapter
	111:OTG*/
}REG0B_CURRENT_CON;
typedef struct
{
	uint8 bsys_stat:1;
	uint8 sdp_stat:1;
	uint8 pg_stat:1;
	uint8 charg_stat:2;
	/*
	 * Charging status:
	 * 00-Not Charging
	 * 01-Pre-charge (VBAT <VBATLOWV)
	 * 10-Fast Charging
	 * 11-Charge Termination Done
	 */
	uint8 bus_stat:3;
	/*
	 * BUS Status register:
	 * 	000:No input
	 * 	001:USB Host SDP
	 * 	010:USB CDP
	 * 	011:USB DCP
	 * 	100:HVDCP
	 * 	101:Unknown Adapter
	 * 	110:Non-Standard Adapter
	 * 	111:OTG
	 */
}REG0B_STATUS;

typedef struct
{
	uint8 NTC_FAULT:2;
	uint8 BAT_FAULT:1;
	uint8 CHRG_FAULT:2;
	uint8 BOOST_FAULT:1;
	uint8 WATCHDOG_FAULT:1;
}REG0C_TEM_INFO;

typedef struct
{
	uint8 VINDPM:7;
	uint8 VINDPM_MODE:1;
}REG0D_VINDPM_CON;
typedef union
{
	uint8 data;
	REG0D_VINDPM_CON config;
}REG0D_VINDPM;
typedef struct
{
	uint8 BATV:7;
	uint8 THERM_STAT:1;
}REG0E_BATTV_INFO;
typedef struct
{
	uint8 SYSV:7;
	uint8 Reserved:1;
}REG0F_BATTV_INFO;
typedef struct
{
	uint8 busv:7;
	uint8 bus_gd:1;
}REG11_BUS;
typedef union
{
	uint8 data;
	REG11_BUS reg_read;
}REG11_BUS_STATUS;

typedef struct
{
	uint8 ICHGR:7;
	uint8 RE:1;
}REG12_CHARGE_CURRENT;

typedef struct
{
	uint8 CurrentInputLimit:6;
	uint8 IDPM_STAT:1;
	uint8 VDPM_STAT:1;
}REG13_CURRENT_STATUS;
typedef union
{
	uint8 data_uint8;
	REG00_CONFIG_CON re00_config;
	REG02_CONFIG_CON reg02_config;
	REG03_CHARGE_CON reg03_config;
	REG04_CURRENT_CON reg04_config;
	REG06_VREG_CON reg06_config;
	REG0B_STATUS reg0b_onre_status;
	REG0C_TEM_INFO reg0c_tem_info;
	REG0D_VINDPM_CON reg0D_config;
	REG0E_BATTV_INFO reg0e_batt_info;
	REG0F_BATTV_INFO reg0f_sysv_info;
	REG11_BUS reg11_bus_info;
	REG12_CHARGE_CURRENT reg12_charge_info;
	REG13_CURRENT_STATUS reg13_current_input_info;
}REG_SYS_CONFIG;

#pragma  pack()


REG_SYS_CONFIG sy6970_get_reg_config(uint8_t cmd);

void sy6970_init(void);
void sy6970_read_byte(uint8_t dev_addr, uint8_t RAddr, uint8_t *RData);

uint16_t sy6970_get_batv(void);
uint16_t sy6970_get_sysv(void);

uint16_t sy6970_set_reg(uint8 cmd, uint16 cmd_data);
void sy6970_set_reg03_charge(void);
void sy6970_reg0d_vindpm_config(void);

REG_SYS_CONFIG sy6970_get_reg02_config(void);
REG_SYS_CONFIG sy6970_get_reg03_config(void);
REG_SYS_CONFIG sy6970_get_reg0b_charge_status(void);
REG_SYS_CONFIG sy6970_get_reg0d_vindpm_config(void);
REG_SYS_CONFIG sy6970_get_reg11_bus_status(void);

void sy6970_task(void *argv);
#endif