/**
  *****************************************************************************
  * @file    sys_ex_mcu_ctr.h
  * @author  Solution Team
  * @version V1.00
  * @date    28-February-2023
  * @brief   Battery interface
  *****************************************************************************
  * @attention 
  *  Copyright (c) 2017 - 2023 Unicmicro Co., Ltd. 
  *  All Rights Reserved. 
  *****************************************************************************
  */
 
#ifndef __DPM_EX_MCU_CTRL_H_
#define __DPM_EX_MCU_CTRL_H_


#include "sys_defs.h"
#include "libc.h"
#include "drv_i2c.h"
#include "drv_timer.h"


#define DPM_EX_MCU_BASE_ADDR                    (0xA8)
#define DPM_EX_MCU_BITRATE_50K                  (0xFB)
#define DPM_EX_MCU_BITRATE_DEF                  DPM_EX_MCU_BITRATE_50K
#define DPM_EX_MCU_TRY_CNT                      (2)
#define SYS_MCU_CTRL_I2C                        (DRV_I2C_ID_1)


#define DPM_EX_MCU_VER_REG                      (0x0)


#define DPM_EX_MCU_BAT_CAP_REG                  (0x1)


#define DPM_EX_MCU_VBAT_HI_REG                  (0x2)
#define DPM_EX_MCU_VBAT_LO_REG                  (0x3)


#define DPM_EX_MCU_IBAT_HI_REG                  (0x4)
#define DPM_EX_MCU_IBAT_LO_REG                  (0x5)


#define DPM_EX_MCU_VBUS_HI_REG                  (0x6)
#define DPM_EX_MCU_VBUS_LO_REG                  (0x7)


#define DPM_EX_MCU_IBUS_HI_REG                  (0x8)
#define DPM_EX_MCU_IBUS_LO_REG                  (0x9)


#define DPM_EX_MCU_CTRL_REG                     (0xA)

#define DPM_EX_MCU_CTRL_REG_ERR                 (0)
#define DPM_EX_MCU_CTRL_REG_ERR_MASK            (0xFE)
#define DPM_EX_MCU_CTRL_REG_ERR_SET             (0x01)

#define DPM_EX_MCU_CTRL_REG_LCP                 (1)
#define DPM_EX_MCU_CTRL_REG_LCP_MASK            (0xFD)
#define DPM_EX_MCU_CTRL_REG_LCP_SET             (0x02)

#define DPM_EX_MCU_CTRL_REG_DCHG_LI             (2)
#define DPM_EX_MCU_CTRL_REG_DCHG_LI_MASK        (0xFB)
#define DPM_EX_MCU_CTRL_REG_DCHG_LI_SET         (0x04)

#define DPM_EX_MCU_CTRL_REG_SYS_IDLE            (3)
#define DPM_EX_MCU_CTRL_REG_SYS_IDLE_MASK       (0xF7)
#define DPM_EX_MCU_CTRL_REG_SYS_IDLE_SET        (0x08)

#define DPM_EX_MCU_CTRL_REG_DCHG_OCP            (4)
#define DPM_EX_MCU_CTRL_REG_DCHG_OCP_MASK       (0xEF)
#define DPM_EX_MCU_CTRL_REG_DCHG_OCP_SET        (0x10)

#define DPM_EX_MCU_CTRL_REG_CHG                 (5)
#define DPM_EX_MCU_CTRL_REG_CHG_MASK            (0xDF)
#define DPM_EX_MCU_CTRL_REG_CHG_SET             (0x20)

#define DPM_EX_MCU_CTRL_REG_OTP                 (6)
#define DPM_EX_MCU_CTRL_REG_OTP_MASK            (0xBF)
#define DPM_EX_MCU_CTRL_REG_OTP_SET             (0x40)

#define DPM_EX_MCU_PWEN_REG                       (0xB)


#define DPM_EX_MCU_PWEN_REG_CHARGE              (0)
#define DPM_EX_MCU_PWEN_REG_CHARGE_MASK         (0xFE)
#define DPM_EX_MCU_PWEN_REG_CHARGE_SET          (0x01)

#define DPM_EX_MCU_PWEN_REG_DISCHAGE            (1)
#define DPM_EX_MCU_PWEN_REG_DISCHAGE_MASK       (0xFD)
#define DPM_EX_MCU_PWEN_REG_DISCHAGE_SET        (0x02)

#define DPM_EX_MCU_SET_STA_REG                  (0xC)

#define DPM_EX_MCU_SET_STA_REG_CHG              (1)//(0)
#define DPM_EX_MCU_SET_STA_REG_CHG_MASK         (0xFD)//(0xFE)
#define DPM_EX_MCU_SET_STA_REG_CHG_SET          (0x02)//(0x01)

#define DPM_EX_MCU_SET_STA_REG_DISCHG           (0)//(1)
#define DPM_EX_MCU_SET_STA_REG_DISCHG_MASK      (0xFE)//(0xFD)
#define DPM_EX_MCU_SET_STA_REG_DISCHG_SET       (0x01)//(0x02)

/**
 * Set the register value to external MCU
 *
 * \param reg           Register address
 * \param val           Register value
 */
bool_t dpm_ex_mcu_reg_set(u8_t reg, u8_t val);



/**
 * Get the register value of external MCU
 *
 * \param reg           Register address
 * \return Register value
 */
u8_t dpm_ex_mcu_reg_get(u8_t reg);

u8_t dpm_ex_mcu_reg_mget(u8_t reg, u8_t cnt, u8_t *p_val);
u8_t dpm_ex_mcu_reg_mset(u8_t reg, u8_t cnt, u8_t *p_val);

#if 0
#define dpm_ex_mcu_ver_get()            (dpm_ex_mcu_reg_get(DPM_EX_MCU_VER_REG))
#define dpm_ex_mcu_bat_cap_get()        (dpm_ex_mcu_reg_get(DPM_EX_MCU_BAT_CAP_REG))

#if 0  /* deleted by James Gan - 2020-05-18, these registers are writable only */
#define dpm_ex_mcu_bat_cap_set(soc)     (dpm_ex_mcu_reg_set(DPM_EX_MCU_BAT_CAP_REG,soc))

u16_t dpm_ex_mcu_vbat_get(void);
u16_t dpm_ex_mcu_ibat_get(void);
u16_t dpm_ex_mcu_vbus_get(void);
u16_t dpm_ex_mcu_ibus_get(void);
#endif /* #if 0 */

void dpm_ex_mcu_vbat_set(u16_t vbat);
void dpm_ex_mcu_ibat_set(u16_t ibat);
void dpm_ex_mcu_vbus_set(u16_t vbus);
void dpm_ex_mcu_ibus_set(u16_t ibus);
void dpm_ex_mcu_ctr16_set(u16_t val);


#define dpm_ex_mcu_ctrl_get()           (dpm_ex_mcu_reg_get(DPM_EX_MCU_CTRL_REG))
#define dpm_ex_mcu_ctrl_set(v)          (dpm_ex_mcu_reg_set(DPM_EX_MCU_CTRL_REG,v))

#define dpm_ex_mcu_set_sta_get()        (dpm_ex_mcu_reg_get(DPM_EX_MCU_SET_STA_REG))
#define dpm_ex_mcu_set_sta_set(v)       (dpm_ex_mcu_reg_set(DPM_EX_MCU_SET_STA_REG,v))

#define dpm_ex_pw_ctrl_get()           (dpm_ex_mcu_reg_get(DPM_EX_MCU_PWEN_REG))
#else
#define dpm_ex_mcu_ver_get()            (0)
#define dpm_ex_mcu_bat_cap_get() 		(0)
#define dpm_ex_mcu_vbat_set(vbat)
#define dpm_ex_mcu_ibat_set(ibat)
#define dpm_ex_mcu_vbus_set(vbus)
#define dpm_ex_mcu_ibus_set(ibus)
#define dpm_ex_mcu_ctr16_set(val)
#define dpm_ex_mcu_ctrl_get()           (0)
#define dpm_ex_mcu_ctrl_set(v)
#define dpm_ex_mcu_set_sta_get()        (0)
#define dpm_ex_mcu_set_sta_set(v)
#define dpm_ex_pw_ctrl_get()      (0)
#endif


#endif /* #ifndef __SYS_BAT_INTF_H_ */

