/**
 * \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_uart.h"

u8_t sys_version_reg_get(void)
{
    u8_t version = 0;
    SYS_REG_READ(SYS_VERSION_REG, &version);

    return version;
}
#if 0
bool_t sys_control_0_reg_get(SYS_CONTROL_0_REG_t *p_ctrl)
{
    return SYS_REG_READ(SYS_CONTROL_0_REG, (u8_t *)p_ctrl);
}

bool_t sys_control_0_reg_set(SYS_CONTROL_0_REG_t *p_ctrl)
{
    return SYS_REG_WRITE(SYS_CONTROL_0_REG, (u8_t *)p_ctrl);
}

u8_t sys_src_pwr_profile_reg_get(void)
{
    u8_t profile = 0;
    SYS_REG_READ(SYS_SRC_PWR_PROFILE_REG, &profile);
    g_sys_pd_info.c2_power_profile = profile;
    return profile;
}

bool_t sys_src_pwr_profile_reg_set(u8_t profile)
{
    g_sys_pd_info.c2_power_profile = profile;
    return SYS_REG_WRITE(SYS_SRC_PWR_PROFILE_REG, &profile);;
}

u8_t sys_snk_pwr_profile_reg_get(void)
{
    u8_t profile = 0;
    SYS_REG_READ(SYS_SNK_PWR_PROFILE_REG, &profile);

    return profile;
}

bool_t sys_snk_pwr_profile_reg_set(u8_t profile)
{
    return SYS_REG_WRITE(SYS_SNK_PWR_PROFILE_REG, &profile);
}
#endif
bool_t sys_upd1_sta_reg_set(SYS_UPD_STA_REG_t *p_ctrl)
{
    return SYS_REG_WRITE(SYS_UPD1_STAS_REG, (u8_t *)p_ctrl);
}
bool_t sys_upd1_sta_reg_get(SYS_UPD_STA_REG_t *p_ctrl)
{
    return SYS_REG_READ(SYS_UPD1_STAS_REG, (u8_t *)p_ctrl);
}

bool_t sys_upd2_sta_reg_set(SYS_UPD_STA_REG_t *p_ctrl)
{
    return SYS_REG_WRITE(SYS_UPD2_STAS_REG, (u8_t *)p_ctrl);
}
bool_t sys_upd2_sta_reg_get(SYS_UPD_STA_REG_t *p_ctrl)
{
    return SYS_REG_READ(SYS_UPD2_STAS_REG, (u8_t *)p_ctrl);
}

bool_t sys_mcu_ctr_reg_get(SYS_MCU_CTR_REG_t *p_ctrl)
{
    return SYS_REG_READ(SYS_MCU_CTR_STA_REG, (u8_t *)p_ctrl);
}

bool_t sys_power_sta_reg_get(SYS_POWER_STA_REG_t *p_ctrl)
{
    return SYS_REG_READ(SYS_POWER_STAS_REG, (u8_t *)p_ctrl);
}

bool_t sys_ibat_max_reg_get(u8_t val)
{
    return SYS_REG_READ(SYS_IBAT_MAX_GET_REG, &val);
}

#if 0
bool_t sys_upd1_rate_pw_set(u8_t pw)
{
    return SYS_REG_WRITE(SYS_UPD1_RATE_PW_REG, &pw);
}
bool_t sys_upd2_rate_pw_get(u8_t pw)
{
    return SYS_REG_READ(SYS_UPD2_RATE_PW_REG, &pw);
}


#endif
bool_t sys_upd1_real_pw_set(u8_t pw)
{
    return SYS_REG_WRITE(SYS_UPD1_REAL_PW_REG, &pw);
}

bool_t sys_upd2_real_pw_get(u8_t pw)
{
    return SYS_REG_READ(SYS_UPD2_REAL_PW_REG, &pw);
}

u8_t sys_battery_cap_reg_get(void)
{
    u8_t cap = 0;
    SYS_REG_READ(SYS_BATTERY_CAP_REG, &cap);

    return cap;
}

bool_t sys_battery_cap_reg_set(u8_t cap)
{
    return SYS_REG_WRITE(SYS_BATTERY_CAP_REG, &cap);
}
#if 0
bool_t sys_event_0_reg_get(SYS_EVENT_0_REG_t *p_event)
{
    return SYS_REG_READ(SYS_EVENT_0_REG, (u8_t *)p_event);
}

bool_t sys_event_0_reg_set(SYS_EVENT_0_REG_t *p_event)
{
    return SYS_REG_WRITE(SYS_EVENT_0_REG, (u8_t *)p_event);
}

bool_t sys_event_1_reg_get(SYS_EVENT_1_REG_t *p_event)
{
    return SYS_REG_READ(SYS_EVENT_1_REG, (u8_t *)p_event);
}

bool_t sys_event_1_reg_set(SYS_EVENT_1_REG_t *p_event)
{
    return SYS_REG_WRITE(SYS_EVENT_1_REG, (u8_t *)p_event);
}

   
bool_t sys_control_1_reg_get(SYS_CONTROL_1_REG_t *p_ctrl)
{
    return SYS_REG_READ(SYS_EVENT_1_REG, (u8_t *)p_ctrl);
}

bool_t sys_control_1_reg_set(SYS_CONTROL_1_REG_t *p_ctrl)
{
    return SYS_REG_WRITE(SYS_CONTROL_1_REG, (u8_t *)p_ctrl);
}

u8_t sys_operating_pwr_reg_get(void)
{
    u8_t power = 0;
    SYS_REG_READ(SYS_OPERATING_PWR_REG, &power);
    return power;
}
#endif
unsigned short sys_ntc_val_get(void)
{
    unsigned short ntc = 0;
    SYS_REG_MREAD(SYS_NTC_VAL_0_REG, 2, (unsigned char *)(&ntc));
    return ntc;
}

bool_t sys_ntc_val_set(unsigned short ntc_val)
{
    return SYS_REG_MWRITE(SYS_NTC_VAL_0_REG, 2, (unsigned char *)(&ntc_val));
}

u32_t sys_bms_temp_get(void)
{

    u32_t temp = 0;
    SYS_REG_MREAD(SYS_BAT_BMS_TEMPVAL_0_REG, 4, (unsigned char *)(&temp));
    return temp;
}   

bool_t sys_type_c_ctrl_reg_en_get(void)
{
    SYS_TYPE_C_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
    return ctrl.en;
}

void sys_type_c_ctrl_reg_en_set(bool_t en)
{
    SYS_TYPE_C_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
    ctrl.en = en;
    SYS_REG_WRITE(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
}

unsigned char sys_type_c_ctrl_reg_pr_role_get(void)
{
    SYS_TYPE_C_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
    return ctrl.pr_role;
}

void sys_type_c_ctrl_reg_pr_role_set(unsigned char pr_role)
{
    SYS_TYPE_C_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
    ctrl.pr_role = pr_role;
    SYS_REG_WRITE(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
}

unsigned char sys_type_c_ctrl_reg_dr_role_get(void)
{
    SYS_TYPE_C_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
    return ctrl.dr_role;
}

void sys_type_c_ctrl_reg_dr_role_set(unsigned char dr_role)
{
    SYS_TYPE_C_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
    ctrl.dr_role = dr_role;
    SYS_REG_WRITE(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
}

bool_t sys_type_c_ctrl_reg_vbus_check_get(void)
{
    SYS_TYPE_C_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
    return ctrl.vbus_check;
}

void sys_type_c_ctrl_reg_vbus_check_set(bool_t en)
{
    SYS_TYPE_C_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
    ctrl.vbus_check = en;
    SYS_REG_WRITE(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
}

bool_t sys_type_c_ctrl_reg_vconn_get(void)
{
    SYS_TYPE_C_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
    return ctrl.vconn;
}

void sys_type_c_ctrl_reg_vconn_set(bool_t en)
{
    SYS_TYPE_C_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
    ctrl.vconn = en;
    SYS_REG_WRITE(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
}

bool_t sys_type_c_ctrl_reg_try_src_get(void)
{
    SYS_TYPE_C_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
    return ctrl.try_src;
}

void sys_type_c_ctrl_reg_try_src_set(bool_t en)
{
    SYS_TYPE_C_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
    ctrl.try_src = en;
    SYS_REG_WRITE(SYS_TYPE_C_CTRL_REG, (u8_t *)&ctrl);
}

bool_t sys_type_c_ctrl_reg_get(SYS_TYPE_C_CTRL_REG_t *p_ctrl)
{
    return SYS_REG_READ(SYS_TYPE_C_CTRL_REG, (u8_t *)p_ctrl);
}

bool_t sys_type_c_ctrl_reg_set(SYS_TYPE_C_CTRL_REG_t *p_ctrl)
{
    return SYS_REG_WRITE(SYS_TYPE_C_CTRL_REG, (u8_t *)p_ctrl);
}

bool_t sys_pd_ctrl_reg_en_get(void)
{
    SYS_PD_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
    return ctrl.en;
}

void sys_pd_ctrl_reg_en_set(bool_t en)
{
    SYS_PD_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
    ctrl.en = en;
    SYS_REG_WRITE(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
}

bool_t sys_pd_ctrl_reg_test_mode_get(void)
{
    SYS_PD_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
    return ctrl.test_mode;
}

void sys_pd_ctrl_reg_test_mode_set(bool_t en)
{
    SYS_PD_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
    ctrl.test_mode = en;
    SYS_REG_WRITE(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
}

bool_t sys_pd_ctrl_reg_capture_mode_get(void)
{
    SYS_PD_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
    return ctrl.capture_mode;
}

void sys_pd_ctrl_reg_capture_mode_set(bool_t en)
{
    SYS_PD_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
    ctrl.capture_mode = en;
    SYS_REG_WRITE(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
}

bool_t sys_pd_ctrl_reg_vdm_get(void)
{
    SYS_PD_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
    return ctrl.vdm;
}

void sys_pd_ctrl_reg_vdm_set(bool_t en)
{
    SYS_PD_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
    ctrl.vdm = en;
    SYS_REG_WRITE(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
}

bool_t sys_pd_ctrl_reg_vdm_src_startup_get(void)
{
    SYS_PD_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
    return ctrl.vdm_src_startup;
}

void sys_pd_ctrl_reg_vdm_src_startup_set(bool_t en)
{
    SYS_PD_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
    ctrl.vdm_src_startup = en;
    SYS_REG_WRITE(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
}

bool_t sys_pd_ctrl_reg_vdm_modal_get(void)
{
    SYS_PD_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
    return ctrl.vdm_modal;
}

void sys_pd_ctrl_reg_vdm_modal_set(bool_t en)
{
    SYS_PD_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
    ctrl.vdm_modal = en;
    SYS_REG_WRITE(SYS_PD_CTRL_REG, (u8_t *)&ctrl);
}


bool_t sys_pd_ctrl_reg_get(SYS_PD_CTRL_REG_t *p_ctrl)
{
    return SYS_REG_READ(SYS_PD_CTRL_REG, (u8_t *)p_ctrl);
}

bool_t sys_pd_ctrl_reg_set(SYS_PD_CTRL_REG_t *p_ctrl)
{
    return SYS_REG_WRITE(SYS_PD_CTRL_REG, (u8_t *)p_ctrl);
}

bool_t sys_qc_ctrl_reg_en_get(void)
{
    SYS_QC_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_QC_CTRL_REG, (u8_t *)&ctrl);
    return ctrl.en;
}

void sys_qc_ctrl_reg_en_set(bool_t en)
{
    SYS_QC_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_QC_CTRL_REG, (u8_t *)&ctrl);
    ctrl.en = en;
    SYS_REG_WRITE(SYS_QC_CTRL_REG, (u8_t *)&ctrl);
}

unsigned char sys_qc_ctrl_reg_ver_get(void)
{
    SYS_QC_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_QC_CTRL_REG, (u8_t *)&ctrl);
    return ctrl.version;
}

void sys_qc_ctrl_reg_ver_set(unsigned char ver)
{
    SYS_QC_CTRL_REG_t ctrl;
    SYS_REG_READ(SYS_QC_CTRL_REG, (u8_t *)&ctrl);
    ctrl.version = ver;
    SYS_REG_WRITE(SYS_QC_CTRL_REG, (u8_t *)&ctrl);
}

bool_t sys_qc_ctrl_reg_get(SYS_QC_CTRL_REG_t *p_ctrl)
{
    return SYS_REG_READ(SYS_QC_CTRL_REG, (u8_t *)p_ctrl);
}

bool_t sys_qc_ctrl_reg_set(SYS_QC_CTRL_REG_t *p_ctrl)
{
    return SYS_REG_WRITE(SYS_QC_CTRL_REG, (u8_t *)p_ctrl);
}

bool_t sys_adv_ctrl_0_reg_get(SYS_ADV_CTRL_0_REG_t *p_ctrl)
{
    return SYS_REG_READ(SYS_ADV_CTRL_0_REG, (u8_t *)p_ctrl);
}

bool_t sys_adv_ctrl_0_reg_set(SYS_ADV_CTRL_0_REG_t *p_ctrl)
{
    return SYS_REG_WRITE(SYS_ADV_CTRL_0_REG, (u8_t *)p_ctrl);
}

bool_t sys_adv_ctrl_1_reg_get(SYS_ADV_CTRL_1_REG_t *p_ctrl)
{
    return SYS_REG_READ(SYS_ADV_CTRL_1_REG, (u8_t *)p_ctrl);
}

bool_t sys_adv_ctrl_1_reg_set(SYS_ADV_CTRL_1_REG_t *p_ctrl)
{
    return SYS_REG_WRITE(SYS_ADV_CTRL_1_REG, (u8_t *)p_ctrl);
}

bool_t sys_type_c_status_0_reg_conn_get(void)
{
    SYS_TYPE_C_STATUS_0_REG_t sta;
    SYS_REG_READ(SYS_TYPE_C_STATUS_0_REG, (u8_t *)&sta);
    return sta.conn;
}

bool_t sys_type_c_status_0_reg_pr_mode_get(void)
{
    SYS_TYPE_C_STATUS_0_REG_t sta;
    SYS_REG_READ(SYS_TYPE_C_STATUS_0_REG, (u8_t *)&sta);
    return sta.pr_mode;
}

bool_t sys_type_c_status_0_reg_dr_mode_get(void)
{
    SYS_TYPE_C_STATUS_0_REG_t sta;
    SYS_REG_READ(SYS_TYPE_C_STATUS_0_REG, (u8_t *)&sta);
    return sta.dr_mode;
}

bool_t sys_type_c_status_0_reg_ra_detected_get(void)
{
    SYS_TYPE_C_STATUS_0_REG_t sta;
    SYS_REG_READ(SYS_TYPE_C_STATUS_0_REG, (u8_t *)&sta);
    return sta.ra_detected;
}

bool_t sys_type_c_status_0_reg_vconn_sourcing_get(void)
{
    SYS_TYPE_C_STATUS_0_REG_t sta;
    SYS_REG_READ(SYS_TYPE_C_STATUS_0_REG, (u8_t *)&sta);
    return sta.vconn_sourcing;
}

bool_t sys_type_c_status_0_reg_cc_orient_get(void)
{
    SYS_TYPE_C_STATUS_0_REG_t sta;
    SYS_REG_READ(SYS_TYPE_C_STATUS_0_REG, (u8_t *)&sta);
    return sta.cc_orient;
}

bool_t sys_type_c_status_0_reg_get(SYS_TYPE_C_STATUS_0_REG_t *p_sta)
{
    return SYS_REG_READ(SYS_TYPE_C_STATUS_0_REG, (u8_t *)p_sta);
}

bool_t sys_pd_status_reg_conn_get(void)
{
    SYS_PD_STATUS_REG_t sta;
    SYS_REG_READ(SYS_PD_STATUS_REG, (u8_t *)&sta);
    return sta.conn;
}

bool_t sys_pd_status_reg_power_nego_completed_get(void)
{
    SYS_PD_STATUS_REG_t sta;
    SYS_REG_READ(SYS_PD_STATUS_REG, (u8_t *)&sta);
    return sta.power_nego_completed;
}

unsigned char sys_pd_status_reg_pdo_pos_get(void)
{
    SYS_PD_STATUS_REG_t sta;
    SYS_REG_READ(SYS_PD_STATUS_REG, (u8_t *)&sta);
    return sta.pdo_pos;
}

bool_t sys_pd_status_reg_get(SYS_PD_STATUS_REG_t *p_sta)
{
    return SYS_REG_READ(SYS_PD_STATUS_REG, (u8_t *)p_sta);
}

bool_t sys_qc_status_reg_conn_get(void)
{
    SYS_QC_STATUS_REG_t sta;
    SYS_REG_READ(SYS_QC_STATUS_REG, (u8_t *)&sta);
    return sta.conn;
}

unsigned char sys_qc_status_reg_version_get(void)
{
    SYS_QC_STATUS_REG_t sta;
    SYS_REG_READ(SYS_QC_STATUS_REG, (u8_t *)&sta);
    return sta.version;
}

bool_t sys_qc_status_reg_get(SYS_QC_STATUS_REG_t *p_sta)
{
    return SYS_REG_READ(SYS_QC_STATUS_REG, (u8_t *)p_sta);
}

bool_t sys_type_c_status_1_reg_get(unsigned char *p_val)
{
    
    return TRUE;
}

unsigned char sys_pe_status_reg_get(void)
{
    unsigned char sta;
    SYS_REG_READ(SYS_PE_STATUS_REG, &sta);
    return sta;
}

unsigned int sys_pdo_power_get(int pos, unsigned short *p_volt, unsigned short *p_curr)
{
    u32_t result = 0;
    SYS_PDO_CTRL_REG_t pdo_ctrl = {0,0,0,0,0,0};
    pdo_ctrl.pdo_pos = pos;
    pdo_ctrl.pdo_get = 1;
    SYS_REG_WRITE(SYS_PDO_CTRL_REG,(unsigned char *)(&pdo_ctrl));
    SYS_REG_MREAD(SYS_PDO_BYTE_0_REG, 4, (unsigned char *)(&result));
    g_sys_pd_info.c2_cap = *(UPD_DATA_MSG_SOURCE_CAP_t *)&result;
    switch (g_sys_pd_info.c2_cap.supply_obj_type)
    {
        case UPD_DATA_MSG_SOURCE_CAP_FIXED_SUPPLY:
            {
                UPD_DATA_MSG_SOURCE_CAP_FIXED_SUPPLY_t *fixed = (UPD_DATA_MSG_SOURCE_CAP_FIXED_SUPPLY_t *)&result;
                *p_volt = fixed->volt * VOLTAGE_DIV;
                *p_curr = fixed->max_curr * CURRENT_DIV;
                
                result = (*p_volt);
                result = result * (*p_curr);
                result /= 1000000;
            }
            break;
        case UPD_DATA_MSG_SOURCE_CAP_BATTERY:
            {
                UPD_DATA_MSG_SOURCE_CAP_BATTERY_SUPPLY_t *bat = (UPD_DATA_MSG_SOURCE_CAP_BATTERY_SUPPLY_t *)&result;
                *p_volt = bat->max_volt * VOLTAGE_DIV;
                result = bat->max_allow_pwr * POWER_DIV * 1000;
                
                *p_curr = result / (*p_volt);
                result /= 1000000;
            }
            break;
        case UPD_DATA_MSG_SOURCE_CAP_VARIABLE_SUPPLY:
            {
                UPD_DATA_MSG_SOURCE_CAP_VARIABLE_SUPPLY_t *var = (UPD_DATA_MSG_SOURCE_CAP_VARIABLE_SUPPLY_t *)&result;
                *p_volt = var->max_volt * VOLTAGE_DIV;
                *p_curr = var->max_curr * CURRENT_DIV;
                
                result = (*p_volt);
                result = result * (*p_curr);
                result /= 1000000;
            }
            break;
        case UPD_DATA_MSG_SOURCE_CAP_APDU:
            {
                UPD_DATA_MSG_SOURCE_CAP_PPS_APDU_t *pps = (UPD_DATA_MSG_SOURCE_CAP_PPS_APDU_t *)&result;
                *p_volt = pps->max_volt * VOLTAGE_PPS_DIV;
                *p_curr = pps->max_curr * CURRENT_DIV;
                
                result = (*p_volt);
                result = result * (*p_curr);
                result /= 1000000;
            }
            break;
    }
    
    return (result);
}

unsigned int sys_request_current_get(void)
{
    u32_t result = 0;
    SYS_PDO_CTRL_REG_t pdo_ctrl = {0,0,0,0,0,0};
    pdo_ctrl.rdo_get = 1;
    SYS_REG_WRITE(SYS_PDO_CTRL_REG,(unsigned char *)(&pdo_ctrl));
    SYS_REG_MREAD(SYS_PDO_BYTE_0_REG, 4, (unsigned char *)(&result));
    switch (g_sys_pd_info.c2_cap.supply_obj_type)
    {
        case UPD_DATA_MSG_SOURCE_CAP_VARIABLE_SUPPLY:
        case UPD_DATA_MSG_SOURCE_CAP_FIXED_SUPPLY:
            {
                UPD_DATA_MSG_REQUEST_FIXED_VARIABLE_t *fixed = (UPD_DATA_MSG_REQUEST_FIXED_VARIABLE_t *)&result;
                result = fixed->max_opr_curr * CURRENT_DIV;
                g_sys_pd_info.c2_req_vbus = g_sys_pd_info.c2_rated_vbus;
            }
            break;
        case UPD_DATA_MSG_SOURCE_CAP_BATTERY:
            {
                UPD_DATA_MSG_REQUEST_BATTERY_t *bat = (UPD_DATA_MSG_REQUEST_BATTERY_t *)&result;
                if (g_sys_pd_info.c2_rated_vbus)
                {
                    result = bat->max_opr_pwr * POWER_DIV * 1000 / g_sys_pd_info.c2_rated_vbus;
                    g_sys_pd_info.c2_req_vbus = g_sys_pd_info.c2_rated_vbus;
                }
            }
            break;
        case UPD_DATA_MSG_SOURCE_CAP_APDU:
            {
                UPD_DATA_MSG_REQUEST_PPS_t *pps = (UPD_DATA_MSG_REQUEST_PPS_t *)&result;
                result = pps->opr_curr * CURRENT_PPS_DIV;
                g_sys_pd_info.c2_req_vbus = pps->output_volt * VOLTAGE_PPS_REQ_DIV;
            }
            break;
    }
    g_sys_pd_info.c2_req_ibus = result;
    
    return (result);
}


unsigned short sys_vbus_val_get(void)
{
    unsigned short result = 0;
    SYS_REG_MREAD(SYS_VBUS_VAL_0_REG, 2, (unsigned char *)(&result));
    return result;
}

unsigned short sys_ibus_val_get(void)
{
    unsigned short result = 0;
    SYS_REG_MREAD(SYS_IBUS_VAL_0_REG, 2, (unsigned char *)(&result));
    return result;
}
unsigned short sys_bat_sta_val_get(void)
{
    unsigned short result = 0;
    SYS_REG_MREAD(SYS_BAT_STA_VAL_0_REG, 2, (unsigned char *)(&result));
    return result;
}


unsigned int sys_chip_reg_read(unsigned int reg)
{
    unsigned int reg_val = 0;
    SYS_CHIP_REG_CTRL_REG_t reg_ctrl = {0,1,0};
    SYS_REG_MWRITE(SYS_CHIP_REG_ADDR_0_REG, 4, (unsigned char *)(&reg));
    SYS_REG_WRITE(SYS_CHIP_REG_CTRL_REG,(unsigned char *)(&reg_ctrl));
    SYS_REG_MREAD(SYS_CHIP_REG_VAL_0_REG, 4, (unsigned char *)(&reg_val));
    return reg_val;
}

bool_t sys_chip_reg_write(unsigned int reg, unsigned int val)
{
    SYS_CHIP_REG_CTRL_REG_t reg_ctrl = {1,0,0};
    SYS_REG_MWRITE(SYS_CHIP_REG_ADDR_0_REG, 4, (unsigned char *)(&reg));
    SYS_REG_MWRITE(SYS_CHIP_REG_VAL_0_REG, 4, (unsigned char *)(&val));
    SYS_REG_WRITE(SYS_CHIP_REG_CTRL_REG,(unsigned char *)(&reg_ctrl));
    return TRUE;
}

unsigned short sys_chip_adc_read(unsigned char channel)
{
    unsigned short adc_result = 0;
    SYS_CHIP_ADC_CTRL_REG_t adc_ctrl;
    adc_ctrl.channel = channel;
    adc_ctrl.stc = 0;
    adc_ctrl.stc_en = 0;
    SYS_REG_WRITE(SYS_CHIP_ADC_CTRL_REG,(unsigned char *)(&adc_ctrl));
    SYS_REG_MREAD(SYS_CHIP_ADC_VAL_0_REG, 2, (unsigned char *)(&adc_result));
    return adc_result;
}
bool_t sys_led_bit_map_reg_set(u8_t* bitmap)
{
   return SYS_REG_WRITE(SYS_LED_BITMAP, (u8_t *)bitmap);
}

bool_t sys_led_pwmduty_reg_set(u8_t* duty)
{
   return SYS_REG_WRITE(SYS_LED_DUTY_SET_REG, (u8_t *)duty);
}

bool_t sys_master_charge_pwr_reg_set(u8_t pwr)
{   
    return SYS_REG_WRITE(SYS_MASTER_CHARGE_PWR_REG, &pwr);
}

