/**
 * \file    sys_reg.c
 * \brief   System control register process engine
 *
 * \par     Copyright (c) 2020 UWE Technologies Corp., Ltd. All Rights Reserved.
 * \author  James
 * \date    2020-03-27
 * \version 
 * 
 * \history 
 *  Date        Note
 *  2020-03-27  Original Creation
 * 
 */

#include "libc.h"
#include "drivers.h"
#include "usbpd.h"
#include "sys_pd_config.h"
#include "sys_pd_hook.h"
#include "sys_qc_hook.h"
#include "sys_reg.h"
//#include "sys_bat_intf.h"
#include "sys_gpio.h"
#if CLI_ENABLED
#include "cli_cmd_usbpd.h"
#endif
//#include "sys_uart.h"

bool_t sys_version_reg_get(unsigned char *p_val)
{
    *p_val = (unsigned char)SYS_SLA_VERSION;
    return TRUE;
}

bool_t sys_control_0_reg_get(unsigned char *p_val)
{
    //*(SYS_CONTROL_0_REG_t *)p_val = *(SYS_CONTROL_0_REG_t *)(&(g_sys_pd_info.c1_ctrl));
    return TRUE;
}

bool_t sys_control_0_reg_set(unsigned char *p_val)
{

    return TRUE;
}

bool_t sys_src_pwr_profile_reg_get(unsigned char *p_val)
{
    *p_val = (unsigned char)usbpd_cfg_port_src_fixed_pwr_prof_get();
    return TRUE;
}

bool_t sys_src_pwr_profile_reg_set(unsigned char *p_val)
{
    //u32_t C2_power_idx = usbpd_cfg_port_src_fixed_pwr_prof_get();
    sys_pd_cfg_src_power_profile_set(*p_val);
    if (usbpd_cfg_port_src_fixed_pwr_prof_changed_flag_get()) 
    {   

        if ((dpm_cable_detect_done_get()) && 
            (dpm_current_pwr_mode_get()) && 
            (pe_pd_connected_get()))
        {
#if 0
            //dpm_source_cap_change_set(TRUE);
            if((gp_usbpd_cfg->port_cfg.power.src.profile >= POWER_PROFILE_45W_FIXED)&&
                (gp_usbpd_cfg->port_cfg.power.src.profile < POWER_PROFILE_CUSTOMIZE_FIXED))
            {
                dpm_source_cap_change_set(TRUE);
            }
            else if((gp_usbpd_cfg->port_cfg.power.src.profile >= POWER_PROFILE_0P5W_FIXED)&&
                (gp_usbpd_cfg->port_cfg.power.src.profile < POWER_PROFILE_45W_FIXED))
            {
                dpm_hard_reset_req_set(TRUE);

            }
            dpm_hrst_due_to_no_current_flag_set(FALSE);
            if (PE_SRC_READY == pe_state_get())
            {
                pe_state_machine();
            }
#endif
            pe_error_recovery_directed_set(TRUE);
            dpm_cc_state_machine();
        }

        usbpd_cfg_port_src_fixed_pwr_prof_changed_flag_set(FALSE);
#if CLI_ENABLED
	    if (sys_cfg_dbg_get())
	    {

	    }
#endif /* CLI_ENABLED */
    }

    return TRUE;
}

bool_t sys_snk_pwr_profile_reg_get(unsigned char *p_val)
{
    *p_val = usbpd_cfg_port_snk_pwr_prof_get();
    return TRUE;
}

bool_t sys_snk_pwr_profile_reg_set(unsigned char *p_val)
{
    if (*p_val != usbpd_cfg_port_snk_pwr_prof_get())
    {
        sys_pd_cfg_snk_operating_power_profile_set(*p_val);
        dpm_new_pwrlevel_req_set(TRUE);
    }
    return TRUE;
}

bool_t sys_battery_cap_reg_get(unsigned char *p_val)
{
   // *p_val = sys_bat_capacity_get();
      
    return TRUE;
}

bool_t sys_battery_cap_reg_set(unsigned char *p_val)
{
    //sys_bat_capacity_set(*p_val);
    return TRUE;
}

bool_t sys_event_0_reg_get(unsigned char *p_val)
{
   // *p_val = *(unsigned char *)(&(g_sys_pd_info.event0));
    return TRUE;
}

bool_t sys_event_0_reg_set(unsigned char *p_val)
{
    //*(unsigned char *)(&(g_sys_pd_info.event0)) = *p_val;
    return TRUE;
}

bool_t sys_event_1_reg_get(unsigned char *p_val)
{
    //*p_val = *(unsigned char *)(&(g_sys_pd_info.event1));
    return TRUE;
}

bool_t sys_event_1_reg_set(unsigned char *p_val)
{
    //*(unsigned char *)(&(g_sys_pd_info.event1)) = *p_val;
    return TRUE;
}

bool_t sys_control_1_reg_get(unsigned char *p_val)
{
    *p_val = 0;
    return TRUE;
}

bool_t sys_control_1_reg_set(unsigned char *p_val)
{

    
    return TRUE;
}

bool_t sys_operating_pwr_reg_get(unsigned char *p_val)
{
   //*p_val = (unsigned char)( (dpm_vbus_volt_get() * sys_pd_ibus_get()) / 1000000);//(g_sys_pd_info.c1_current_pwr);
    return TRUE;
}

bool_t sys_request_pwr_reg_get(unsigned char *p_val)
{
    //*p_val = (unsigned char)(g_sys_pd_info.c1_current_pwr_req);
    return TRUE;
}

bool_t sys_usba1_qc_sta_reg_get(unsigned char *p_val)
{
    //*p_val = (unsigned char)(dpm_qc_state_get(&g_usba1_qc_info));
    return TRUE;
}
bool_t sys_ntc_val_0_reg_get(unsigned char *p_val)
{
    //*p_val = SAL_U16_LSB_BYTE(g_sys_pd_info.ntc_volt);

    return TRUE;
}

bool_t sys_ntc_val_1_reg_get(unsigned char *p_val)
{
    //*p_val = SAL_U16_MSB_BYTE(g_sys_pd_info.ntc_volt);

    return TRUE;
}

bool_t sys_ntc_val_0_reg_set(unsigned char *p_val)
{

    return TRUE;
}

bool_t sys_ntc_val_1_reg_set(unsigned char *p_val)
{

    return TRUE;
}



bool_t sys_type_c_ctrl_reg_get(unsigned char *p_val)
{
    SYS_TYPE_C_CTRL_REG_t *p_ctrl = (SYS_TYPE_C_CTRL_REG_t *)p_val;
    p_ctrl->pr_role = usbpd_cfg_port_def_pr_get();
    p_ctrl->dr_role = usbpd_cfg_port_def_dr_get();
    p_ctrl->vbus_check = usbpd_cfg_port_vbus_check_get();
    p_ctrl->vconn = usbpd_cfg_port_vconn_en_get();
    p_ctrl->try_src = usbpd_cfg_port_try_src_get();
    p_ctrl->en = dpm_port_disabled_get() ? FALSE : TRUE;
    return TRUE;
}

bool_t sys_type_c_ctrl_reg_set(unsigned char *p_val)
{
    SYS_TYPE_C_CTRL_REG_t *p_ctrl = (SYS_TYPE_C_CTRL_REG_t *)p_val;

    if (usbpd_cfg_port_def_pr_get() != p_ctrl->pr_role)
    {
        usbpd_cfg_port_def_pr_set(p_ctrl->pr_role);
        if (dpm_cable_detect_done_get())
        {
            pe_error_recovery_directed_set(TRUE);
        }
#if CLI_ENABLED
	    if (sys_cfg_dbg_get())
	    {
            printf("PR changed to %s\n", g_cli_sys_cfg_def_pr[p_ctrl->pr_role]);
	    }
#endif /* CLI_ENABLED */
    }
    usbpd_cfg_port_def_dr_set(p_ctrl->dr_role);
    usbpd_cfg_port_vbus_check_set(p_ctrl->vbus_check);
    usbpd_cfg_port_vconn_en_set(p_ctrl->vconn);
    usbpd_cfg_port_try_src_set(p_ctrl->try_src);
    dpm_port_disabled_set(!p_ctrl->en);
    if (!p_ctrl->en)
    {
        dpm_cc_state_release();
        dpm_cc_state_init();
        pe_state_release();
        pe_state_init();
        dpm_info_reset();
        dpm_rp_rd_clear();
#if CLI_ENABLED
	    if (sys_cfg_dbg_get())
	    {
            drv_uart_putstr("Port disabled\n");
	    }
#endif /* CLI_ENABLED */
    }
    return TRUE;
}

bool_t sys_pd_ctrl_reg_get(unsigned char *p_val)
{
    SYS_PD_CTRL_REG_t *p_ctrl = (SYS_PD_CTRL_REG_t *)p_val;
    p_ctrl->test_mode = usbpd_cfg_test_mode_get();
    p_ctrl->capture_mode = usbpd_cfg_capture_mode_get();
    p_ctrl->vdm = usbpd_cfg_port_vdm_en_get();
    p_ctrl->vdm_src_startup = usbpd_cfg_port_vdm_src_startup_en_get();
    p_ctrl->vdm_modal = usbpd_cfg_vdm_modal_get();
    p_ctrl->en = usbpd_cfg_port_no_pd_mode_get() ? FALSE : TRUE;
    p_ctrl->reserved = 0;
    return TRUE;
}

bool_t sys_pd_ctrl_reg_set(unsigned char *p_val)
{
    SYS_PD_CTRL_REG_t *p_ctrl = (SYS_PD_CTRL_REG_t *)p_val;
    usbpd_cfg_test_mode_set(p_ctrl->test_mode);
    usbpd_cfg_capture_mode_set(p_ctrl->capture_mode);
    usbpd_cfg_port_vdm_en_set(p_ctrl->vdm);
    usbpd_cfg_port_vdm_src_startup_en_set(p_ctrl->vdm_src_startup);
    usbpd_cfg_vdm_modal_set(p_ctrl->vdm_modal);
    if (usbpd_cfg_port_no_pd_mode_get() == p_ctrl->en)
    {
        usbpd_cfg_port_no_pd_mode_set(!p_ctrl->en);
        if (!p_ctrl->en)
        {
            dpm_cc_state_release();
            dpm_cc_state_init();
            pe_state_release();
            pe_state_init();
        }
        dpm_port_init();
    }
    return TRUE;
}

bool_t sys_qc_ctrl_reg_get(unsigned char *p_val)
{
    SYS_QC_CTRL_REG_t *p_ctrl = (SYS_QC_CTRL_REG_t *)p_val;
    p_ctrl->en = usbpd_cfg_port_quick_charge_en_get();
    p_ctrl->version = usbpd_cfg_port_quick_charge_ver_get();
    p_ctrl->reserved = 0;
    return TRUE;
}

bool_t sys_qc_ctrl_reg_set(unsigned char *p_val)
{
    SYS_QC_CTRL_REG_t *p_ctrl = (SYS_QC_CTRL_REG_t *)p_val;
    usbpd_cfg_port_quick_charge_en_set(p_ctrl->en);
    usbpd_cfg_port_quick_charge_ver_set(p_ctrl->version);
#if SYS_QUICK_CHARGE_ENABLED
    if (usbpd_cfg_port_quick_charge_en_get() != p_ctrl->en)
    {
        usbpd_cfg_port_quick_charge_en_set(p_ctrl->en);
        if (p_ctrl->en)
        {
            if (!dpm_port_dead_battery_get())
            {
                //dpm_qc_enable_set(&g_usbc_qc_info, TRUE, FALSE);
            }
        }
        else 
        {
            sys_qc_disable();
        }
    }
#endif /* SYS_QUICK_CHARGE_ENABLED */
    return TRUE;
}

bool_t sys_adv_ctrl_0_reg_get(unsigned char *p_val)
{
    SYS_ADV_CTRL_0_REG_t *p_ctrl = (SYS_ADV_CTRL_0_REG_t *)p_val;
    p_ctrl->cc1_dis = usbpd_cfg_port_cc1_dis_get();
    p_ctrl->cc2_dis = usbpd_cfg_port_cc2_dis_get();
    p_ctrl->err_recovery = 0;
    p_ctrl->prs_req = 0;
    p_ctrl->drs_req = 0;
    p_ctrl->vcs_req = 0;
    p_ctrl->soft_reset = 0;
    p_ctrl->hard_reset = 0;
    return TRUE;
}

bool_t sys_adv_ctrl_0_reg_set(unsigned char *p_val)
{
    SYS_ADV_CTRL_0_REG_t *p_ctrl = (SYS_ADV_CTRL_0_REG_t *)p_val;
    usbpd_cfg_port_cc1_dis_set(p_ctrl->cc1_dis);
    usbpd_cfg_port_cc2_dis_set(p_ctrl->cc2_dis);
    if (p_ctrl->err_recovery)
    {
        pe_error_recovery_directed_set(TRUE);
    }
    if (p_ctrl->prs_req)
    {
        dpm_pr_swp_req_set(TRUE);
    }
    if (p_ctrl->drs_req)
    {
        dpm_dr_swp_req_set(TRUE);
    }
    if (p_ctrl->vcs_req)
    {
        dpm_vconn_swp_req_set(TRUE);
    }
    if (p_ctrl->soft_reset)
    {
        if (dpm_current_pwr_mode_get())
        {
            pe_src_send_soft_reset();
        }
        else {
            pe_snk_send_soft_reset();
        }
    }
    if (p_ctrl->hard_reset)
    {
        dpm_source_ocp_flag_set(FALSE);
        sys_pd_led_state_set(SYS_PD_LED_STATE_ON);
        if (pe_pd_connected_get())
        {
            dpm_hard_reset_req_set(TRUE);
        }
        else {
            DRV_REG_FIELD_SET(DPM_PRL_REG,HRST_REQ_FRM_PLCENG,TRUE);
        }
    }
    return TRUE;
}

bool_t sys_adv_ctrl_1_reg_get(unsigned char *p_val)
{
    *p_val = 0;
    return TRUE;
}

bool_t sys_adv_ctrl_1_reg_set(unsigned char *p_val)
{
    SYS_ADV_CTRL_1_REG_t *p_ctrl = (SYS_ADV_CTRL_1_REG_t *)p_val;

    if (p_ctrl->get_src_cap)
    {
        dpm_get_src_cap_req_set(TRUE);
    }
    if (p_ctrl->get_snk_cap)
    {
        dpm_get_snk_cap_req_set(TRUE);
    }
    if (p_ctrl->goto_min)
    {
        dpm_goto_min_req_set(TRUE);
    }
    if (p_ctrl->new_power_level)
    {
        dpm_new_power_level_req();
    }
    return TRUE;
}

bool_t sys_pdo_ctrl_reg_get(unsigned char *p_val)
{
    *p_val = 0;
    return TRUE;
}

static unsigned int s_pdo = 0;
bool_t sys_pdo_ctrl_reg_set(unsigned char *p_val)
{
    SYS_PDO_CTRL_REG_t *p_ctrl = (SYS_PDO_CTRL_REG_t *)p_val;
    if (p_ctrl->pdo_get)
    {
        if (p_ctrl->pdo_pos)
        {
            s_pdo = usbpd_cfg_port_src_pdo_get(p_ctrl->pdo_pos-1);
        }
    }
    else if (p_ctrl->pdo_apply)
    {
        usbpd_cfg_port_src_fixed_pwr_prof_changed_flag_set(TRUE);
    }
    else if (p_ctrl->pdo_set)
    {
        if (p_ctrl->pdo_pos)
        {
            usbpd_cfg_port_src_pdo_set(p_ctrl->pdo_pos-1, s_pdo);
        }
    }
    else if (p_ctrl->pdo_clear)
    {
        s_pdo = 0;
        if (p_ctrl->pdo_pos)
        {
            usbpd_cfg_port_src_pdo_set(p_ctrl->pdo_pos-1, s_pdo);
        }
    }
    else if (p_ctrl->rdo_get)
    {
        s_pdo = *(dpm_current_rdo_get());
    }
    return TRUE;
}

bool_t sys_pdo_byte_0_reg_get(unsigned char *p_val)
{
    *p_val = SAL_U32_BYTE0(s_pdo);
    return TRUE;
}

bool_t sys_pdo_byte_0_reg_set(unsigned char *p_val)
{
    s_pdo = (unsigned int)(*p_val);
    return TRUE;
}

bool_t sys_pdo_byte_1_reg_get(unsigned char *p_val)
{
    *p_val = SAL_U32_BYTE1(s_pdo);
    return TRUE;
}

bool_t sys_pdo_byte_1_reg_set(unsigned char *p_val)
{
    s_pdo |= ((unsigned int)(*p_val)) << 8;
    return TRUE;
}

bool_t sys_pdo_byte_2_reg_get(unsigned char *p_val)
{
    *p_val = SAL_U32_BYTE2(s_pdo);
    return TRUE;
}

bool_t sys_pdo_byte_2_reg_set(unsigned char *p_val)
{
    s_pdo |= ((unsigned int)(*p_val)) << 16;
    return TRUE;
}

bool_t sys_pdo_byte_3_reg_get(unsigned char *p_val)
{
    *p_val = SAL_U32_BYTE3(s_pdo);
    return TRUE;
}

bool_t sys_pdo_byte_3_reg_set(unsigned char *p_val)
{
    s_pdo |= ((unsigned int)(*p_val)) << 24;
    return TRUE;
}

bool_t sys_type_c_status_0_reg_get(unsigned char *p_val)
{
    SYS_TYPE_C_STATUS_0_REG_t *p_ctrl = (SYS_TYPE_C_STATUS_0_REG_t *)p_val;
    p_ctrl->conn = dpm_cable_detect_done_get();
    p_ctrl->pr_mode = dpm_current_pwr_mode_get();
    p_ctrl->dr_mode = dpm_dfp_mode_get();
    p_ctrl->ra_detected = dpm_cbl_plug_detected_get();
    p_ctrl->vconn_sourcing = dpm_presently_vconn_source_get();
    p_ctrl->cc_orient = dpm_orient_select_get();
  //  p_ctrl->charging = g_sys_pd_info.charging_flg;
    p_ctrl->ocp = dpm_source_ocp_flag_get();
    return TRUE;
}

bool_t sys_pd_status_reg_get(unsigned char *p_val)
{
    SYS_PD_STATUS_REG_t *p_ctrl = (SYS_PD_STATUS_REG_t *)p_val;
    p_ctrl->conn = pe_pd_connected_get();
   // p_ctrl->power_nego_completed = g_sys_pd_info.pd_power_nego_completed;
    p_ctrl->pdo_pos = dpm_src_obj_pos_get();
    p_ctrl->reserved = 0;
    return TRUE;
}

bool_t sys_qc_status_reg_get(unsigned char *p_val)
{
   // SYS_QC_STATUS_REG_t *p_ctrl = (SYS_QC_STATUS_REG_t *)p_val;
#if 0//SYS_QUICK_CHARGE_ENABLED
    p_ctrl->conn = (((DPM_QC_SRC_CONNECTED <= dpm_qc_state_get(&g_usbc_qc_info)) && (dpm_qc_state_get(&g_usbc_qc_info) <= DPM_QC_SRC_CONNECTED_QC3)) || (dpm_qc_state_get(&g_usbc_qc_info) >= DPM_QC_SNK_CONNECTED_9V)); 
    if (((DPM_QC_SNK_CONNECTED_QC3 <= dpm_qc_state_get(&g_usbc_qc_info)) && (dpm_qc_state_get(&g_usbc_qc_info) <= DPM_QC_SNK_QC3_DEC_REQ)) || (dpm_qc_state_get(&g_usbc_qc_info) == DPM_QC_SRC_CONNECTED_QC3))
    {
        p_ctrl->version = DPM_QC_V3;
    }
    else 
    {
        p_ctrl->version = DPM_QC_V2;
    }
#else
    //p_ctrl->conn = 0;
    //p_ctrl->version= 0;

#endif /* #if SYS_QUICK_CHARGE_ENABLED */
   // p_ctrl->reserved = 0;
    return TRUE;
}

bool_t sys_type_c_status_1_reg_get(unsigned char *p_val)
{
    SYS_TYPE_C_STATUS_REG_t *p_ctrl = (SYS_TYPE_C_STATUS_REG_t *)p_val;
    p_ctrl->cc_prev_state = dpm_cc_state_prev_get();
    p_ctrl->cc_state = dpm_cc_state_get();
    return TRUE;
}

bool_t sys_pe_status_reg_get(unsigned char *p_val)
{
    //*p_val = (unsigned char)pe_state_get();
    return TRUE;
}

bool_t sys_vbus_val_0_reg_get(unsigned char *p_val)
{
   // *p_val = SAL_U16_LSB_BYTE(dpm_vbus_volt_get());

    return TRUE;
}

bool_t sys_vbus_val_1_reg_get(unsigned char *p_val)
{
    //*p_val = SAL_U16_MSB_BYTE(dpm_vbus_volt_get());
   // printf("vbustest2: %u\n", g_sys_pd_info.vbus_current);
    return TRUE;
}

bool_t sys_ibus_val_0_reg_get(unsigned char *p_val)
{
   // *p_val = SAL_U16_LSB_BYTE(sys_pd_ibus_get());
    //printf("vbustest3: %u\n", g_sys_pd_info.ibus_current);
    return TRUE;
}

bool_t sys_ibus_val_1_reg_get(unsigned char *p_val)
{
   // *p_val = SAL_U16_MSB_BYTE(sys_pd_ibus_get());
    //printf("vbustest4: %u\n", g_sys_pd_info.ibus_current);
    return TRUE;
}

bool_t sys_ibat_val_0_reg_get(unsigned char *p_val)
{
  //  *p_val = SAL_U16_LSB_BYTE(g_sys_bat_info.current_ibat_val);
    return TRUE;
}

bool_t sys_ibat_val_1_reg_get(unsigned char *p_val)
{
  //  *p_val = SAL_U16_MSB_BYTE(g_sys_bat_info.current_ibat_val);
    return TRUE;
}
bool_t sys_master_led_state_get(unsigned char *p_val)
{
#if 1
	//extern bool_t g_oled_init_configured;
	//*p_val = g_oled_init_configured;
	//printf("lcd power on test 3 %u\n", g_sys_pd_info.c2_usba_sta.lcddisplay_en);
#else
    *p_val = g_sys_pd_info.led_state;
#endif
    return TRUE;
}

bool_t sys_chip_reg_ctrl_reg_get(unsigned char *p_val)
{
    *p_val = 0;
    return TRUE;
}

static unsigned int s_reg_addr = 0;
static unsigned int s_reg_val = 0;
bool_t sys_chip_reg_addr_0_reg_get(unsigned char *p_val)
{
    *p_val = SAL_U32_BYTE0(s_reg_addr);
    return TRUE;
}

bool_t sys_chip_reg_addr_1_reg_get(unsigned char *p_val)
{
    *p_val = SAL_U32_BYTE1(s_reg_addr);
    return TRUE;
}

bool_t sys_chip_reg_addr_2_reg_get(unsigned char *p_val)
{
    *p_val = SAL_U32_BYTE2(s_reg_addr);
    return TRUE;
}

bool_t sys_chip_reg_addr_3_reg_get(unsigned char *p_val)
{
    *p_val = SAL_U32_BYTE3(s_reg_addr);
    return TRUE;
}

bool_t sys_chip_reg_val_0_reg_get(unsigned char *p_val)
{
    *p_val = SAL_U32_BYTE0(s_reg_val);
    return TRUE;
}

bool_t sys_chip_reg_val_1_reg_get(unsigned char *p_val)
{
    *p_val = SAL_U32_BYTE1(s_reg_val);
    return TRUE;
}

bool_t sys_chip_reg_val_2_reg_get(unsigned char *p_val)
{
    *p_val = SAL_U32_BYTE2(s_reg_val);
    return TRUE;
}

bool_t sys_chip_reg_val_3_reg_get(unsigned char *p_val)
{
    *p_val = SAL_U32_BYTE3(s_reg_val);
    return TRUE;
}

bool_t sys_bms_temp_val_0_reg_get(unsigned char *p_val)
{


    return TRUE;
}

bool_t sys_bms_temp_val_1_reg_get(unsigned char *p_val)
{


    return TRUE;
}

bool_t sys_bms_temp_val_2_reg_get(unsigned char *p_val)
{


    return TRUE;
}

bool_t sys_bms_temp_val_3_reg_get(unsigned char *p_val)
{

    return TRUE;
}

bool_t sys_chip_reg_ctrl_reg_set(unsigned char *p_val)
{
    SYS_CHIP_REG_CTRL_REG_t *p_ctrl = (SYS_CHIP_REG_CTRL_REG_t *)p_val;
    if (0x40000000 & s_reg_addr)
    {
        if (p_ctrl->read)
        {
            s_reg_val = DRV_REG_GET(s_reg_addr);
        }
        else if (p_ctrl->write)
        {
            DRV_REG_SET(s_reg_addr, s_reg_val);
        }
        else
        {
            return FALSE;
        }
        return TRUE;
    }
    else 
    {
        return FALSE;
    }
}

bool_t sys_chip_reg_addr_0_reg_set(unsigned char *p_val)
{
    s_reg_addr = (unsigned int)(*p_val);
    return TRUE;
}

bool_t sys_chip_reg_addr_1_reg_set(unsigned char *p_val)
{
    s_reg_addr |= ((unsigned int)(*p_val)) << 8;
    return TRUE;
}

bool_t sys_chip_reg_addr_2_reg_set(unsigned char *p_val)
{
    s_reg_addr |= ((unsigned int)(*p_val)) << 16;
    return TRUE;
}

bool_t sys_chip_reg_addr_3_reg_set(unsigned char *p_val)
{
    s_reg_addr |= ((unsigned int)(*p_val)) << 24;
    return TRUE;
}

bool_t sys_chip_reg_val_0_reg_set(unsigned char *p_val)
{
    s_reg_val = (unsigned int)(*p_val);
    return TRUE;
}

bool_t sys_chip_reg_val_1_reg_set(unsigned char *p_val)
{
    s_reg_val |= ((unsigned int)(*p_val)) << 8;
    return TRUE;
}

bool_t sys_chip_reg_val_2_reg_set(unsigned char *p_val)
{
    s_reg_val |= ((unsigned int)(*p_val)) << 16;
    return TRUE;
}

bool_t sys_chip_reg_val_3_reg_set(unsigned char *p_val)
{
    s_reg_val |= ((unsigned int)(*p_val)) << 24;
    return TRUE;
}

bool_t sys_chip_adc_ctrl_reg_get(unsigned char *p_val)
{
    *p_val = 0;
    return TRUE;
}

static unsigned short s_adc_result = 0;
bool_t sys_chip_adc_ctrl_reg_set(unsigned char *p_val)
{
    SYS_CHIP_ADC_CTRL_REG_t *p_adc_ctrl = (SYS_CHIP_ADC_CTRL_REG_t *)p_val;

    if (p_adc_ctrl->stc_en)
        drv_adc_stc_set(p_adc_ctrl->stc);

    s_adc_result = drv_adc_read_multi(p_adc_ctrl->channel,3);
    return TRUE;
}
bool_t sys_led_bit_map_reg_set(unsigned char *p_val)
{
#if SYS_LED_ENABLED
    if(!g_sys_pd_info.c1_ctrl.low_current_mode)
    {

        u32_t led_bitmap = 0; 
        if(!drv_gpio_en_get(SYS_IO_LED1))
        {
        drv_gpio_en_set(SYS_IO_LED1,1);
        drv_gpio_oe_set(SYS_IO_LED1,1);


        }
        if(!drv_gpio_en_get(SYS_IO_LED4))
        {
        // LED4 PIN
        drv_gpio_en_set(SYS_IO_LED4,1);
        drv_gpio_oe_set(SYS_IO_LED4,1);

        }

        if(!drv_gpio_en_get(SYS_IO_LED2))
        {

        drv_gpio_en_set(SYS_IO_LED2,1);
        drv_gpio_oe_set(SYS_IO_LED2,1);
        }

        if(!drv_gpio_en_get(SYS_IO_LED3))
        {
        drv_gpio_en_set(SYS_IO_LED3,1);
        drv_gpio_oe_set(SYS_IO_LED3,1);

        }
        //u32_t* led_bitmap= (u32_t*)p_val;
        led_bitmap =(u32_t)(*p_val); 
        sys_gpio_bat_cap_led_control(led_bitmap);

    }
    else
    {

        g_sys_pd_info.led_bit_map = (u32_t)(*p_val);
    }
    
#endif
    return TRUE;
}

bool_t sys_led_pwmduty_reg_set(unsigned char *p_val)
{
#if 0
    u32_t duty = 0; 
    //u32_t* led_bitmap= (u32_t*)p_val;
    duty =(u32_t)(*p_val); 
    sys_gpio_led_pwmduty_control(duty);
#endif   
    return TRUE;
}

bool_t sla_master_charge_pwr_reg_set(unsigned char *p_val)
{

    
    return TRUE;
}

bool_t sys_bms_ctrl_reg_set(unsigned char *p_val)
{

    return TRUE;
}

bool_t sys_ibat_level_reg_set(unsigned char *p_val)
{

    {/*
       switch (ibat_setting)
       {
           case SYS_IBAT_SET_0A:
                break;
           case SYS_IBAT_SET_1P0A:
                SYS_BAT_REG_SET(IBAT_LIM_SET_REG, SYS_BAT_IBAT_LIM_VAL_TO_REG(IBT_CURRENT_1000mA));
                if(!g_i2c_info[SYS_BAT_PWR_CTRL_I2C].last_status)
                {
                    g_sys_pd_info.ibat_setted_level = SYS_IBAT_SET_1P0A;
                }
                break;                
           case SYS_IBAT_SET_2P5A:
                SYS_BAT_REG_SET(IBAT_LIM_SET_REG, SYS_BAT_IBAT_LIM_VAL_TO_REG(IBT_CURRENT_2500mA));
                if(!g_i2c_info[SYS_BAT_PWR_CTRL_I2C].last_status)
                {
                    g_sys_pd_info.ibat_setted_level = SYS_IBAT_SET_2P5A;
                }
                break;
           case SYS_IBAT_SET_3P5A: 
                SYS_BAT_REG_SET(IBAT_LIM_SET_REG, SYS_BAT_IBAT_LIM_VAL_TO_REG(IBT_CURRENT_3500mA));
                if(!g_i2c_info[SYS_BAT_PWR_CTRL_I2C].last_status)
                {
                    g_sys_pd_info.ibat_setted_level = SYS_IBAT_SET_3P5A;
                }
                break;
           case SYS_IBAT_SET_5P0A:
                //g_sys_pd_info.ibat_setted_level = SYS_IBAT_SET_5P0A;
                SYS_BAT_REG_SET(IBAT_LIM_SET_REG, SYS_BAT_IBAT_LIM_VAL_TO_REG(IBT_CURRENT_4850mA));
                if(!g_i2c_info[SYS_BAT_PWR_CTRL_I2C].last_status)
                {
                    g_sys_pd_info.ibat_setted_level = SYS_IBAT_SET_5P0A;
                }
                break;
           default:
                break;
            
       }*/
    }
    return TRUE;
}

bool_t sys_usba1_curr_set(unsigned char *p_val)
{

    return TRUE;
}

bool_t sys_usba_sta_set(unsigned char *p_val)
{

  
    return TRUE;
}

bool_t sys_chip_adc_val_0_reg_get(unsigned char *p_val)
{
    *p_val = SAL_U16_LSB_BYTE(s_adc_result);
    return TRUE;
}

bool_t sys_chip_adc_val_1_reg_get(unsigned char *p_val)
{
    *p_val = SAL_U16_MSB_BYTE(s_adc_result);
    return TRUE;
}
bool_t sys_bat_sta_val_0_reg_get(unsigned char *p_val)
{
    
    return TRUE;
}

bool_t sys_bat_sta_val_1_reg_get(unsigned char *p_val)
{


    return TRUE;
}

bool_t sys_reg_read(unsigned char reg, unsigned char *p_val)
{
    bool_t ret = FALSE;
    switch (reg)
    {
        case SYS_VERSION_REG :
            ret = sys_version_reg_get(p_val);
            break;

        case SYS_CONTROL_0_REG       :
            ret =  sys_snk_pwr_profile_reg_get(p_val);///1;//sys_control_0_reg_get(p_val);
            break;

        case SYS_SRC_PWR_PROFILE_REG :
            ret = sys_src_pwr_profile_reg_get(p_val);
            break;

        case SYS_SNK_PWR_PROFILE_REG :
            ret = sys_snk_pwr_profile_reg_get(p_val);
            break;

        case SYS_BATTERY_CAP_REG     :
            ret = sys_battery_cap_reg_get(p_val);
            break;

        case SYS_EVENT_0_REG         :
            ret = sys_event_0_reg_get(p_val);
            break;

        case SYS_EVENT_1_REG         :
            ret = sys_event_1_reg_get(p_val);
            break;

        case SYS_CONTROL_1_REG       :
            ret = sys_control_1_reg_get(p_val);
            break;

        case SYS_OPERATING_PWR_REG   :
            ret = sys_operating_pwr_reg_get(p_val);
            break;

        case SYS_NTC_VAL_0_REG      :
            ret = sys_ntc_val_0_reg_get(p_val);
            break;

        case SYS_NTC_VAL_1_REG      :
            ret = sys_ntc_val_1_reg_get(p_val);
            break;


        case SYS_TYPE_C_CTRL_REG     :
            ret = sys_type_c_ctrl_reg_get(p_val);
            break;

        case SYS_PD_CTRL_REG         :
            ret = sys_pd_ctrl_reg_get(p_val);
            break;

        case SYS_QC_CTRL_REG         :
            ret = sys_qc_ctrl_reg_get(p_val);
            break;

        case SYS_ADV_CTRL_0_REG      :
            ret = sys_adv_ctrl_0_reg_get(p_val);
            break;

        case SYS_ADV_CTRL_1_REG      :
            ret = sys_adv_ctrl_1_reg_get(p_val);
            break;

        case SYS_PDO_CTRL_REG        :
            ret = sys_pdo_ctrl_reg_get(p_val);
            break;

        case SYS_PDO_BYTE_0_REG      :
            ret = sys_pdo_byte_0_reg_get(p_val);
            break;

        case SYS_PDO_BYTE_1_REG      :
            ret = sys_pdo_byte_1_reg_get(p_val);
            break;

        case SYS_PDO_BYTE_2_REG      :
            ret = sys_pdo_byte_2_reg_get(p_val);
            break;

        case SYS_PDO_BYTE_3_REG      :
            ret = sys_pdo_byte_3_reg_get(p_val);
            break;

        case SYS_TYPE_C_STATUS_0_REG :     
            ret = sys_type_c_status_0_reg_get(p_val);
            break;

        case SYS_PD_STATUS_REG       :
            ret = sys_pd_status_reg_get(p_val);
            break;

        case SYS_QC_STATUS_REG       :
            ret = sys_qc_status_reg_get(p_val);
            break;

        case SYS_TYPE_C_STATUS_1_REG :
            ret = sys_type_c_status_1_reg_get(p_val);
            break;

        case SYS_PE_STATUS_REG       :
            ret = sys_pe_status_reg_get(p_val);
            break;

        case SYS_VBUS_VAL_0_REG      :
            ret = sys_vbus_val_0_reg_get(p_val);
           // printf("vbustest1: %u\n", dpm_vbus_volt_get());
            break;

        case SYS_VBUS_VAL_1_REG      :
            ret = sys_vbus_val_1_reg_get(p_val);
           // printf("vbustest2: %u\n", dpm_vbus_volt_get());
            break;

        case SYS_IBUS_VAL_0_REG      :
            ret = sys_ibus_val_0_reg_get(p_val);
           // printf("vbustest2: %u\n", sys_pd_ibus_get());
            break;

        case SYS_IBUS_VAL_1_REG      :
            ret = sys_ibus_val_1_reg_get(p_val);
           // printf("vbustest2: %u\n", sys_pd_ibus_get());
            break;


        case SYS_CHIP_REG_CTRL_REG   :
            ret = sys_chip_reg_ctrl_reg_get(p_val);
            break;

        case SYS_CHIP_REG_ADDR_0_REG :
            ret = sys_chip_reg_addr_0_reg_get(p_val);
            break;

        case SYS_CHIP_REG_ADDR_1_REG :
            ret = sys_chip_reg_addr_1_reg_get(p_val);
            break;

        case SYS_CHIP_REG_ADDR_2_REG :
            ret = sys_chip_reg_addr_2_reg_get(p_val);
            break;

        case SYS_CHIP_REG_ADDR_3_REG :
            ret = sys_chip_reg_addr_3_reg_get(p_val);
            break;

        case SYS_CHIP_REG_VAL_0_REG  :
            ret = sys_chip_reg_val_0_reg_get(p_val);
            break;

        case SYS_CHIP_REG_VAL_1_REG  :
            ret = sys_chip_reg_val_1_reg_get(p_val);
            break;

        case SYS_CHIP_REG_VAL_2_REG  :
            ret = sys_chip_reg_val_2_reg_get(p_val);
            break;

        case SYS_CHIP_REG_VAL_3_REG  :
            ret = sys_chip_reg_val_3_reg_get(p_val);
            break;

        case SYS_CHIP_ADC_CTRL_REG   :
            ret = sys_chip_adc_ctrl_reg_get(p_val);
            break;

        case SYS_CHIP_ADC_VAL_0_REG  :
            ret = sys_chip_adc_val_0_reg_get(p_val);
            break;

        case SYS_CHIP_ADC_VAL_1_REG  :
            ret = sys_chip_adc_val_1_reg_get(p_val);
            break;
        case SYS_BAT_STA_VAL_0_REG  :
            ret = sys_bat_sta_val_0_reg_get(p_val);
            break;
        case SYS_BAT_STA_VAL_1_REG  :
            ret = sys_bat_sta_val_1_reg_get(p_val);
            break;
        case SYS_BAT_BMS_TEMPVAL_0_REG  :
            ret = sys_bms_temp_val_0_reg_get(p_val);
            break;

        case SYS_BAT_BMS_TEMPVAL_1_REG  :
            ret = sys_bms_temp_val_1_reg_get(p_val);
            break;

        case SYS_BAT_BMS_TEMPVAL_2_REG  :
            ret = sys_bms_temp_val_2_reg_get(p_val);
            break;

        case SYS_BAT_BMS_TEMPVAL_3_REG  :
            ret = sys_bms_temp_val_3_reg_get(p_val);
            break; 
        case SYS_IBAT_VAL_0_REG  :
            ret = sys_ibat_val_0_reg_get(p_val);
            break;
        case SYS_IBAT_VAL_1_REG  :
            ret = sys_ibat_val_1_reg_get(p_val);
            break; 
        case SYS_GET_LED_STATE_REG  :
            ret = sys_master_led_state_get(p_val);
            break; 
        case SYS_REQUEST_POWER_REG  :
            ret = sys_request_pwr_reg_get(p_val);
            break; 
        case SYS_USBA1_QC_STA_REG  :
            ret = sys_usba1_qc_sta_reg_get(p_val);
            break; 
        default:
            break;
    }
    return ret;
}

bool_t sys_reg_write(unsigned char reg, unsigned char *p_val)
{
    bool_t ret = FALSE;
    switch (reg)
    {
        case SYS_CONTROL_0_REG       :
            ret = sys_control_0_reg_set(p_val);
            break;

        case SYS_SRC_PWR_PROFILE_REG :
            ret = 1;//sys_src_pwr_profile_reg_set(p_val);
            break;

        case SYS_SNK_PWR_PROFILE_REG :
            ret = sys_snk_pwr_profile_reg_set(p_val);
            break;

        case SYS_BATTERY_CAP_REG     :
            ret = sys_battery_cap_reg_set(p_val);
            break;

        case SYS_EVENT_0_REG         :
            ret = sys_event_0_reg_set(p_val);
            break;

        case SYS_EVENT_1_REG         :
            ret = sys_event_1_reg_set(p_val);
            break;

        case SYS_CONTROL_1_REG       :
            ret = sys_control_1_reg_set(p_val);
            break;

        case SYS_NTC_VAL_0_REG      :
            ret = sys_ntc_val_0_reg_set(p_val);
            break;

        case SYS_NTC_VAL_1_REG      :
            ret = sys_ntc_val_1_reg_set(p_val);
            break;


        case SYS_TYPE_C_CTRL_REG     :
            ret = sys_type_c_ctrl_reg_set(p_val);
            break;

        case SYS_PD_CTRL_REG         :
            ret = sys_pd_ctrl_reg_set(p_val);
            break;

        case SYS_QC_CTRL_REG         :
            ret = sys_qc_ctrl_reg_set(p_val);
            break;

        case SYS_ADV_CTRL_0_REG      :
            ret = sys_adv_ctrl_0_reg_set(p_val);
            break;

        case SYS_ADV_CTRL_1_REG      :
            ret = sys_adv_ctrl_1_reg_set(p_val);
            break;

        case SYS_PDO_CTRL_REG        :
            ret = sys_pdo_ctrl_reg_set(p_val);
            break;

        case SYS_PDO_BYTE_0_REG      :
            ret = sys_pdo_byte_0_reg_set(p_val);
            break;

        case SYS_PDO_BYTE_1_REG      :
            ret = sys_pdo_byte_1_reg_set(p_val);
            break;

        case SYS_PDO_BYTE_2_REG      :
            ret = sys_pdo_byte_2_reg_set(p_val);
            break;

        case SYS_PDO_BYTE_3_REG      :
            ret = sys_pdo_byte_3_reg_set(p_val);
            break;

        case SYS_CHIP_REG_CTRL_REG   :
            ret = sys_chip_reg_ctrl_reg_set(p_val);
            break;

        case SYS_CHIP_REG_ADDR_0_REG :
            ret = sys_chip_reg_addr_0_reg_set(p_val);
            break;

        case SYS_CHIP_REG_ADDR_1_REG :
            ret = sys_chip_reg_addr_1_reg_set(p_val);
            break;

        case SYS_CHIP_REG_ADDR_2_REG :
            ret = sys_chip_reg_addr_2_reg_set(p_val);
            break;

        case SYS_CHIP_REG_ADDR_3_REG :
            ret = sys_chip_reg_addr_3_reg_set(p_val);
            break;

        case SYS_CHIP_REG_VAL_0_REG  :
            ret = sys_chip_reg_val_0_reg_set(p_val);
            break;

        case SYS_CHIP_REG_VAL_1_REG  :
            ret = sys_chip_reg_val_1_reg_set(p_val);
            break;

        case SYS_CHIP_REG_VAL_2_REG  :
            ret = sys_chip_reg_val_2_reg_set(p_val);
            break;

        case SYS_CHIP_REG_VAL_3_REG  :
            ret = sys_chip_reg_val_3_reg_set(p_val);
            break;

        case SYS_CHIP_ADC_CTRL_REG   :
            ret = sys_chip_adc_ctrl_reg_set(p_val);
            break;
        case SYS_LED_BITMAP          :
            ret = sys_led_bit_map_reg_set(p_val);
            break;
        case SYS_LED_DUTY_SET_REG    :
            ret = sys_led_pwmduty_reg_set(p_val);
            break; 
        case SYS_MASTER_CHARGE_PWR_REG    :
            ret = sla_master_charge_pwr_reg_set(p_val);
            break;
        case SYS_BMS_CTRL_REG    :
            ret = sys_bms_ctrl_reg_set(p_val);
            break;
        case SYS_IBT_LIMIT_REG    :
            ret = sys_ibat_level_reg_set(p_val);
            break;
        case SYS_USBA1_CURRENT_REG    :
            ret = sys_usba1_curr_set(p_val);
            break;
         case SYS_USBA_STA_REG    :
            ret = sys_usba_sta_set(p_val);
            break;
        default:
            break;
    }
    return ret;
}


