/**
 * \file    sys_bat_intf.c
 * \brief   DPM battery interface
 *
 * \par     Copyright (c) 2016 UWE Technologies Corp., Ltd. All Rights Reserved.
 * \author  James
 * \date    2016-02-29
 * \version 
 * 
 * \history 
 *  Date        Note
 *  2016-02-29  Original Creation
 * 
 */

#include "libc.h"
#include "drivers.h"
#include "usbpd.h"
#include "usbpd_config.h"
#include "sys_config.h"
#include "sys_pd_hook.h"
#include "sys_ex_mcu_ctr.h"

bool_t dpm_ex_mcu_reg_set(u8_t reg, u8_t val)
{
    bool_t res = FALSE;
    s8_t try_cnt = DPM_EX_MCU_TRY_CNT;

try_again:
    if (try_cnt) try_cnt--;
    drv_i2c_reg_set(SYS_MCU_CTRL_I2C,DPM_EX_MCU_BASE_ADDR,reg,val);
    rtc_delay_us(8);

    if (I2C_OK == g_i2c_info[SYS_MCU_CTRL_I2C].last_status)
    {
        res = TRUE;
    }
    else if (try_cnt)
    {
        goto try_again;
    }

    return res;
}


u8_t dpm_ex_mcu_reg_get(u8_t reg)
{
    u8_t res = 0;
    s8_t try_cnt = DPM_EX_MCU_TRY_CNT;

try_again:
    if (try_cnt) try_cnt--;
    res = drv_i2c_reg_get(SYS_MCU_CTRL_I2C,DPM_EX_MCU_BASE_ADDR,reg);
    rtc_delay_us(8);

    if (I2C_OK != g_i2c_info[SYS_MCU_CTRL_I2C].last_status)
    {
        if(try_cnt)
        {
            goto try_again;

        }
    }
    return res;
}


#if 1  /* modified by James Gan - 2018-12-20 */
u8_t dpm_ex_mcu_reg_mget(u8_t reg, u8_t cnt, u8_t *p_val)
{
    u8_t res = FALSE;
    s8_t try_cnt = DPM_EX_MCU_TRY_CNT;

try_again:
    if (try_cnt) try_cnt--;
    drv_i2c_reg_mget(SYS_MCU_CTRL_I2C,DPM_EX_MCU_BASE_ADDR,reg,cnt,p_val);
    rtc_delay_us(8);

    if (I2C_OK == g_i2c_info[SYS_MCU_CTRL_I2C].last_status)
    {
        res = TRUE;
        goto exit_l;
    }
    else if (try_cnt)
    {
        goto try_again;
    }

exit_l:
    return res;
}

u8_t dpm_ex_mcu_reg_mset(u8_t reg, u8_t cnt, u8_t *p_val)
{
    u8_t res = FALSE;
    s8_t try_cnt = DPM_EX_MCU_TRY_CNT;
try_again:
    if (try_cnt) try_cnt--;
    drv_i2c_reg_mset(SYS_MCU_CTRL_I2C,DPM_EX_MCU_BASE_ADDR,reg,cnt,p_val);
    rtc_delay_us(8);

    if (I2C_OK == g_i2c_info[SYS_MCU_CTRL_I2C].last_status)
    {
        res = TRUE;
        goto exit_l;
    }
    else if (try_cnt)
    {
        goto try_again;
    }


exit_l:
    return res;
}
#endif /* #if 1 */
void dpm_ex_mcu_ctr16_set(u16_t val)
{
    u8_t aval[2];
    aval[0] = (u8_t)(val >> 8);
    aval[1] = (u8_t)(val & 0xFF);
    dpm_ex_mcu_reg_mset(DPM_EX_MCU_CTRL_REG, 2, &aval[0]);
}

void dpm_ex_mcu_vbat_set(u16_t vbat)
{
    u8_t aval[2];
    aval[0] = (u8_t)(vbat >> 8);
    aval[1] = (u8_t)(vbat & 0xFF);
    dpm_ex_mcu_reg_mset(DPM_EX_MCU_VBAT_HI_REG, 2, &aval[0]);
}

void dpm_ex_mcu_ibat_set(u16_t ibat)
{
    u8_t aval[2];
    aval[0] = (u8_t)(ibat >> 8);
    aval[1] = (u8_t)(ibat & 0xFF);
    dpm_ex_mcu_reg_mset(DPM_EX_MCU_IBAT_HI_REG, 2, &aval[0]);
}

void dpm_ex_mcu_vbus_set(u16_t vbus)
{
    u8_t aval[2];
    aval[0] = (u8_t)(vbus >> 8);
    aval[1] = (u8_t)(vbus & 0xFF);
    dpm_ex_mcu_reg_mset(DPM_EX_MCU_VBUS_HI_REG, 2, &aval[0]);
}

void dpm_ex_mcu_ibus_set(u16_t ibus)
{
    u8_t aval[2];
    aval[0] = (u8_t)(ibus >> 8);
    aval[1] = (u8_t)(ibus & 0xFF);
    dpm_ex_mcu_reg_mset(DPM_EX_MCU_IBUS_HI_REG, 2, &aval[0]);
}


#if 0  /* deleted by James Gan - 2020-05-18 */
u16_t dpm_ex_mcu_vbat_get(void)
{
    u8_t aval[2];
    u16_t volt;

    dpm_ex_mcu_reg_mget(DPM_EX_MCU_VBAT_HI_REG, 2, &aval[0]);
    volt = aval[0];
    volt <<= 8;
    volt += aval[1];
    return volt;
}

u16_t dpm_ex_mcu_ibat_get(void)
{
    u8_t aval[2];
    u16_t current;

    dpm_ex_mcu_reg_mget(DPM_EX_MCU_IBAT_HI_REG, 2, &aval[0]);
    current = aval[0];
    current <<= 8;
    current += aval[1];
    return current;
}

u16_t dpm_ex_mcu_vbus_get(void)
{
    u8_t aval[2];
    u16_t volt;

    dpm_ex_mcu_reg_mget(DPM_EX_MCU_VBUS_HI_REG, 2, &aval[0]);
    volt = aval[0];
    volt <<= 8;
    volt += aval[1];
    return volt;
}

u16_t dpm_ex_mcu_ibus_get(void)
{
    u8_t aval[2];
    u16_t current;

    dpm_ex_mcu_reg_mget(DPM_EX_MCU_IBUS_HI_REG, 2, &aval[0]);
    current = aval[0];
    current <<= 8;
    current += aval[1];
    return current;
}
#endif /* #if 0 */


