/**
 * \file    sys_config.c
 * \brief   System configuration
 *
 * \par     Copyright (c) 2018 UWE Technologies Corp., Ltd. All Rights Reserved.
 * \author  James
 * \date    2018-03-17
 * \version 
 * 
 * \history 
 *  Date        Note
 *  2018-03-17  Original Creation
 * 
 */

#include "libc.h"
#include "drivers.h"
#include "sys_pd_config.h"
#include "usbpd.h"

/* Global variables */

void sys_pd_cfg_port_pwr_fixed_cap_set(volatile POWER_FIXED_t *fixed, u32_t volt, u32_t curr, u32_t pwm_duty)
{
    if (NULL == fixed)
        return;
    fixed->supply_type = POWER_SUPPLY_TYPE_FIXED;
    fixed->peak_curr = PEAK_CURRENT_LVL0;
    fixed->max_curr = CURRENT_VAL(curr);
    fixed->max_volt = VOLTAGE_VAL(volt);
    fixed->pwm_duty = pwm_duty;
    fixed->reserved = 0;
    fixed->reserved2 = 0;
}

void sys_pd_cfg_port_pwr_vari_cap_set(volatile POWER_VARIABLE_t *vari, u32_t min_volt, u32_t max_volt, u32_t curr, u32_t pwm_duty, u32_t pwm_duty_max)
{
    if (NULL == vari)
        return;
    vari->supply_type = POWER_SUPPLY_TYPE_VARIABLE;
    vari->min_volt = VOLTAGE_VAL(min_volt);
    vari->max_volt = VOLTAGE_VAL(max_volt);
    vari->max_curr = CURRENT_VAL(curr);
    vari->pwm_duty = pwm_duty;
    vari->pwm_duty_max = pwm_duty_max;
}

void sys_pd_cfg_port_pwr_battery_cap_set(volatile POWER_BATTERY_t *battery, u32_t min_volt, u32_t max_volt, u32_t power, u32_t pwm_duty, u32_t pwm_duty_max)
{
    if (NULL == battery)
        return;
    battery->supply_type = POWER_SUPPLY_TYPE_BATTERY;
    battery->min_volt = VOLTAGE_VAL(min_volt);
    battery->max_volt = VOLTAGE_VAL(max_volt);
    battery->max_allow_pwr = POWER_VAL(power);
    battery->pwm_duty = pwm_duty;
    battery->pwm_duty_max = pwm_duty_max;
}

void sys_pd_cfg_port_pwr_pps_cap_set(volatile POWER_PPS_t *pps, u32_t min_volt, u32_t max_volt, u32_t max_curr, u32_t pwm_duty, u32_t pwm_duty_max)
{
    if (NULL == pps)
        return;
    pps->supply_type = POWER_SUPPLY_TYPE_PPS;
    pps->pps = 0;
    pps->resv1 = 0;
    pps->resv2 = 0;
    pps->resv3 = 0;
    pps->power_limited = 0;
    pps->min_volt = VOLTAGE_PPS(min_volt);
    pps->max_volt = VOLTAGE_PPS(max_volt);
    pps->max_curr = CURRENT_PPS(max_curr);
    pps->pwm_duty = pwm_duty;
    pps->pwm_duty_max = pwm_duty_max;
}

u32_t sys_pd_cfg_src_fixed_power_profile_index_get(u32_t voltage)
{
    u32_t idx;

    for (idx = CFG_VOLTAGE_5V_IDX; idx < CFG_MAX_PWR_CAP_NUM; idx++)
    {
        if ((POWER_SUPPLY_TYPE_FIXED == usbpd_cfg_port_src_pwr_supply_type_get(idx)) &&
            (voltage == (usbpd_cfg_port_src_fixed_pwr_volt_cap_get(idx)*VOLTAGE_DIV)))
        {
            return idx;
        }
    }
    return CFG_VOLTAGE_5V_IDX;
}


void sys_pd_cfg_src_power_profile_set(u32_t index)
{
    gp_usbpd_cfg->port_cfg.power.src.cap_num = 0;
    gp_usbpd_cfg->port_cfg.power.src.pwm_en = SYS_PWM_CTRL_VOLT_ENABLED;
    gp_usbpd_cfg->port_cfg.power.src.pwm_per = SYS_DEF_PWM_PER;
    if ((POWER_PROFILE_RESV != gp_usbpd_cfg->port_cfg.power.src.profile) &&
        (index != gp_usbpd_cfg->port_cfg.power.src.profile))
    {
        usbpd_cfg_port_src_fixed_pwr_prof_changed_flag_set(TRUE);
    }
    gp_usbpd_cfg->port_cfg.power.src.profile = index;

    memset((char *)&(gp_usbpd_cfg->port_cfg.power.src.caps[0]), 0, CFG_MAX_PWR_CAP_NUM*sizeof(POWER_FIXED_t));

    usbpd_cfg_port_pps_en_set(FALSE);

    switch (index)
    {
        case POWER_PROFILE_100W_PPS:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[1].fixed), VOLTAGE_9V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_9V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[2].fixed), VOLTAGE_15V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_15V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[3].fixed), VOLTAGE_20V, CURRENT_5000mA, SYS_DEF_PWM_DUTY_20V);
            sys_pd_cfg_port_pwr_pps_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[4].pps), 3300U, 21000U, CURRENT_5000mA, SYS_PPS_3_PWM_DUTY_MIN, SYS_PPS_3_PWM_DUTY_MAX);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 5;
            gp_usbpd_cfg->port_cfg.src_pdp = 100;
            usbpd_cfg_port_pps_en_set(TRUE);
            break;
        case POWER_PROFILE_100W_FIXED:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[1].fixed), VOLTAGE_9V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_9V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[2].fixed), VOLTAGE_12V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_12V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[3].fixed), VOLTAGE_15V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_15V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[4].fixed), VOLTAGE_20V, CURRENT_5000mA, SYS_DEF_PWM_DUTY_20V);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 5;
            gp_usbpd_cfg->port_cfg.src_pdp = 100;
            break;    
        case POWER_PROFILE_140W_FIXED:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[1].fixed), VOLTAGE_9V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_9V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[2].fixed), VOLTAGE_12V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_12V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[3].fixed), VOLTAGE_15V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_15V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[4].fixed), VOLTAGE_20V, CURRENT_5000mA, SYS_DEF_PWM_DUTY_20V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[5].fixed), 0, 0, 0);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[6].fixed), 0, 0, 0);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[7].fixed), VOLTAGE_28V, CURRENT_5000mA, SYS_DEF_PWM_DUTY_28V);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 5;
            gp_usbpd_cfg->port_cfg.power.src.epr_cap_num = 1;
            gp_usbpd_cfg->port_cfg.src_pdp = 100;
            break;
        case POWER_PROFILE_240W_FIXED:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[1].fixed), VOLTAGE_9V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_9V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[2].fixed), VOLTAGE_12V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_12V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[3].fixed), VOLTAGE_15V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_15V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[4].fixed), VOLTAGE_20V, CURRENT_5000mA, SYS_DEF_PWM_DUTY_20V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[5].fixed), 0, 0, 0);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[6].fixed), 0, 0, 0);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[7].fixed), VOLTAGE_28V, CURRENT_5000mA, SYS_DEF_PWM_DUTY_20V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[8].fixed), VOLTAGE_36V, CURRENT_5000mA, SYS_DEF_PWM_DUTY_20V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[9].fixed), VOLTAGE_48V, CURRENT_5000mA, SYS_DEF_PWM_DUTY_20V);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 5;
            gp_usbpd_cfg->port_cfg.power.src.epr_cap_num = 3;
            gp_usbpd_cfg->port_cfg.src_pdp = 100;
            break;
        case POWER_PROFILE_80W_PPS:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[1].fixed), VOLTAGE_9V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_9V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[2].fixed), VOLTAGE_15V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_15V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[3].fixed), VOLTAGE_20V, CURRENT_4000mA, SYS_DEF_PWM_DUTY_20V);
            sys_pd_cfg_port_pwr_pps_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[4].pps), 3300U, 21000U, CURRENT_4000mA, SYS_PPS_3_PWM_DUTY_MIN, SYS_PPS_3_PWM_DUTY_MAX);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 5;
            gp_usbpd_cfg->port_cfg.src_pdp = 80;
            usbpd_cfg_port_pps_en_set(TRUE);
            break;
        case POWER_PROFILE_80W_FIXED:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[1].fixed), VOLTAGE_9V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_9V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[2].fixed), VOLTAGE_12V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_12V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[3].fixed), VOLTAGE_15V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_15V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[4].fixed), VOLTAGE_20V, CURRENT_4000mA, SYS_DEF_PWM_DUTY_20V);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 5;
            gp_usbpd_cfg->port_cfg.src_pdp = 80;
            break;
        case POWER_PROFILE_65W_PPS:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[1].fixed), VOLTAGE_9V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_9V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[2].fixed), VOLTAGE_15V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_15V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[3].fixed), VOLTAGE_20V, 3250, SYS_DEF_PWM_DUTY_20V);
            sys_pd_cfg_port_pwr_pps_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[4].pps), 3300U, 21000U, 3250, SYS_PPS_3_PWM_DUTY_MIN, SYS_PPS_3_PWM_DUTY_MAX);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 5;
            gp_usbpd_cfg->port_cfg.src_pdp = 65;
            usbpd_cfg_port_pps_en_set(TRUE);
            break;
        case POWER_PROFILE_65W_FIXED:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[1].fixed), VOLTAGE_9V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_9V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[2].fixed), VOLTAGE_12V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_12V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[3].fixed), VOLTAGE_15V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_15V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[4].fixed), VOLTAGE_20V, 3250, SYS_DEF_PWM_DUTY_20V);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 5;
            gp_usbpd_cfg->port_cfg.src_pdp = 65;
            break;
        case POWER_PROFILE_60W_PPS:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[1].fixed), VOLTAGE_9V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_9V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[2].fixed), VOLTAGE_15V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_15V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[3].fixed), VOLTAGE_20V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_20V);
            sys_pd_cfg_port_pwr_pps_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[4].pps), 3300U, 21000U, CURRENT_3000mA, SYS_PPS_3_PWM_DUTY_MIN, SYS_PPS_3_PWM_DUTY_MAX);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 5;
            gp_usbpd_cfg->port_cfg.src_pdp = 60;
            usbpd_cfg_port_pps_en_set(TRUE);
            break;
        case POWER_PROFILE_60W_FIXED:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[1].fixed), VOLTAGE_9V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_9V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[2].fixed), VOLTAGE_12V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_12V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[3].fixed), VOLTAGE_15V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_15V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[4].fixed), VOLTAGE_20V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_20V);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 5;
            gp_usbpd_cfg->port_cfg.src_pdp = 60;
            break;
        case POWER_PROFILE_45W_PPS:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[1].fixed), VOLTAGE_9V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_9V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[2].fixed), VOLTAGE_15V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_15V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[3].fixed), VOLTAGE_20V, CURRENT_2250mA, SYS_DEF_PWM_DUTY_20V);
            sys_pd_cfg_port_pwr_pps_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[4].pps), 3300U, 16000U, CURRENT_3000mA, SYS_PPS_2_PWM_DUTY_MIN, SYS_PPS_2_PWM_DUTY_MAX);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 5;
            gp_usbpd_cfg->port_cfg.src_pdp = 45;
            usbpd_cfg_port_pps_en_set(TRUE);
            break;
        case POWER_PROFILE_45W_FIXED:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[1].fixed), VOLTAGE_9V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_9V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[2].fixed), VOLTAGE_12V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_12V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[3].fixed), VOLTAGE_15V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_15V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[4].fixed), VOLTAGE_20V, CURRENT_2250mA, SYS_DEF_PWM_DUTY_20V);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 5;
            gp_usbpd_cfg->port_cfg.src_pdp = 45;
            break;
        case POWER_PROFILE_30W_PPS:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[1].fixed), VOLTAGE_9V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_9V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[2].fixed), VOLTAGE_15V, CURRENT_2000mA, SYS_DEF_PWM_DUTY_15V);
            sys_pd_cfg_port_pwr_pps_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[3].pps), 3300U, 11000U, CURRENT_3000mA, SYS_PPS_1_PWM_DUTY_MIN, SYS_PPS_1_PWM_DUTY_MAX);
            sys_pd_cfg_port_pwr_pps_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[4].pps), 3300U, 16000U, CURRENT_2000mA, SYS_PPS_2_PWM_DUTY_MIN, SYS_PPS_2_PWM_DUTY_MAX);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 5;
            gp_usbpd_cfg->port_cfg.src_pdp = 30;
            usbpd_cfg_port_pps_en_set(TRUE);
            break;
        case POWER_PROFILE_30W_FIXED:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[1].fixed), VOLTAGE_9V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_9V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[2].fixed), VOLTAGE_12V, 2500, SYS_DEF_PWM_DUTY_12V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[3].fixed), VOLTAGE_15V, CURRENT_2000mA, SYS_DEF_PWM_DUTY_15V);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 4;
            gp_usbpd_cfg->port_cfg.src_pdp = 30;
            break;
        case POWER_PROFILE_18W_PPS:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[1].fixed), VOLTAGE_9V, CURRENT_2000mA, SYS_DEF_PWM_DUTY_9V);
            sys_pd_cfg_port_pwr_pps_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[2].pps), 3300U, 5900U, CURRENT_3000mA, SYS_PPS_0_PWM_DUTY_MIN, SYS_PPS_0_PWM_DUTY_MAX);
            sys_pd_cfg_port_pwr_pps_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[3].pps), 3300U, 11000U, CURRENT_2000mA, SYS_PPS_1_PWM_DUTY_MIN, SYS_PPS_1_PWM_DUTY_MAX);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 4;
            gp_usbpd_cfg->port_cfg.src_pdp = 18;
            usbpd_cfg_port_pps_en_set(TRUE);
            break;
        case POWER_PROFILE_18W_FIXED:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[1].fixed), VOLTAGE_9V, CURRENT_2000mA, SYS_DEF_PWM_DUTY_9V);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 2;
            gp_usbpd_cfg->port_cfg.src_pdp = 18;
            break;
        case POWER_PROFILE_0P5W_FIXED:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, 100, SYS_DEF_PWM_DUTY_5V);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 1;
            gp_usbpd_cfg->port_cfg.src_pdp = 0;
            break;
        case POWER_PROFILE_15W_FIXED:
        default:
            sys_pd_cfg_port_pwr_fixed_cap_set(&(gp_usbpd_cfg->port_cfg.power.src.caps[0].fixed), VOLTAGE_5V, CURRENT_3000mA, SYS_DEF_PWM_DUTY_5V);
            gp_usbpd_cfg->port_cfg.power.src.cap_num = 1;
            gp_usbpd_cfg->port_cfg.src_pdp = 15;
            break;
    }
}

/**
 * Configure sink power profile and vendor defined USB informaiton
 *
 *
 * \param index Sink operating power profile index
 * \return status_t
 *
 * \see Error code of status_t
 */
void sys_pd_cfg_snk_operating_power_profile_set(u32_t index)
{
    gp_usbpd_cfg->port_cfg.power.snk.profile = index;

    //TODO: Sink mode
    gp_usbpd_cfg->port_cfg.snk_modes = 0x6;
    gp_usbpd_cfg->port_cfg.minimum_pdp = 0;

    if (SNK_POWER_REQUIRE_15W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.opr_volt = VOLTAGE_VAL(VOLTAGE_5V);
        gp_usbpd_cfg->port_cfg.power.snk.opr_curr = CURRENT_VAL(CURRENT_3000mA);
        gp_usbpd_cfg->port_cfg.power.snk.max_opr_curr = CURRENT_VAL(CURRENT_3000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp = 15;
        gp_usbpd_cfg->port_cfg.maximum_pdp = 15;
    }
    else if (SNK_POWER_REQUIRE_18W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.opr_volt = VOLTAGE_VAL(VOLTAGE_9V);
        gp_usbpd_cfg->port_cfg.power.snk.opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.power.snk.max_opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp = 18;
        gp_usbpd_cfg->port_cfg.maximum_pdp = 18;
    }
    else if (SNK_POWER_REQUIRE_24W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.opr_volt = VOLTAGE_VAL(VOLTAGE_12V);
        gp_usbpd_cfg->port_cfg.power.snk.opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.power.snk.max_opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp = 24;
        gp_usbpd_cfg->port_cfg.maximum_pdp = 24;
    }
    else if (SNK_POWER_REQUIRE_29W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.opr_volt = VOLTAGE_VAL(VOLTAGE_14P8V);
        gp_usbpd_cfg->port_cfg.power.snk.opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.power.snk.max_opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp = 29;
        gp_usbpd_cfg->port_cfg.maximum_pdp = 29;
    }
    else if (SNK_POWER_REQUIRE_30W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.opr_volt = VOLTAGE_VAL(VOLTAGE_15V);
        gp_usbpd_cfg->port_cfg.power.snk.opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.power.snk.max_opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp = 30;
        gp_usbpd_cfg->port_cfg.maximum_pdp = 30;
    }
    else if (SNK_POWER_REQUIRE_40W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.opr_volt = VOLTAGE_VAL(VOLTAGE_20V);
        gp_usbpd_cfg->port_cfg.power.snk.opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.power.snk.max_opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp = 40;
        gp_usbpd_cfg->port_cfg.maximum_pdp = 40;
    }
    else if (SNK_POWER_REQUIRE_0P5W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.opr_volt = VOLTAGE_VAL(VOLTAGE_5V);
        gp_usbpd_cfg->port_cfg.power.snk.opr_curr = CURRENT_VAL(100);
        gp_usbpd_cfg->port_cfg.power.snk.max_opr_curr = CURRENT_VAL(100);
        gp_usbpd_cfg->port_cfg.operational_pdp = 1;
        gp_usbpd_cfg->port_cfg.maximum_pdp = 1;
    }
    else if (SNK_POWER_REQUIRE_100W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.opr_volt = VOLTAGE_VAL(VOLTAGE_20V);
        gp_usbpd_cfg->port_cfg.power.snk.opr_curr = CURRENT_VAL(CURRENT_5000mA);
        gp_usbpd_cfg->port_cfg.power.snk.max_opr_curr = CURRENT_VAL(CURRENT_5000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp = 100;
        gp_usbpd_cfg->port_cfg.maximum_pdp = 100;
    }
    else
    {
        gp_usbpd_cfg->port_cfg.power.snk.opr_volt = VOLTAGE_VAL(VOLTAGE_5V);
        gp_usbpd_cfg->port_cfg.power.snk.opr_curr = CURRENT_VAL(CURRENT_3000mA);
        gp_usbpd_cfg->port_cfg.power.snk.max_opr_curr = CURRENT_VAL(CURRENT_3000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp = 15;
        gp_usbpd_cfg->port_cfg.maximum_pdp = 15;
    }
}

void sys_pd_cfg_snk_epr_operating_power_profile_set(u32_t index)
{
    gp_usbpd_cfg->port_cfg.power.snk.epr_profile = index;

    //TODO: Sink mode
    gp_usbpd_cfg->port_cfg.snk_modes = 0x6;
    gp_usbpd_cfg->port_cfg.minimum_pdp = 0;

    if (SNK_POWER_REQUIRE_15W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_volt = VOLTAGE_VAL(VOLTAGE_5V);
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_curr = CURRENT_VAL(CURRENT_3000mA);
        gp_usbpd_cfg->port_cfg.power.snk.epr_max_opr_curr = CURRENT_VAL(CURRENT_3000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp_epr = 15;
        
    }
    else if (SNK_POWER_REQUIRE_18W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_volt = VOLTAGE_VAL(VOLTAGE_9V);
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.power.snk.epr_max_opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp_epr = 18;
       
    }
    else if (SNK_POWER_REQUIRE_24W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_volt = VOLTAGE_VAL(VOLTAGE_12V);
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.power.snk.epr_max_opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp_epr = 24;
        
    }
    else if (SNK_POWER_REQUIRE_29W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_volt = VOLTAGE_VAL(VOLTAGE_14P8V);
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.power.snk.epr_max_opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp_epr = 29;
        
    }
    else if (SNK_POWER_REQUIRE_30W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_volt = VOLTAGE_VAL(VOLTAGE_15V);
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.power.snk.epr_max_opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp_epr = 30;
        gp_usbpd_cfg->port_cfg.maximum_pdp = 30;
    }
    else if (SNK_POWER_REQUIRE_40W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_volt = VOLTAGE_VAL(VOLTAGE_20V);
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.power.snk.epr_max_opr_curr = CURRENT_VAL(CURRENT_2000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp_epr = 40;
        
    }
    else if (SNK_POWER_REQUIRE_0P5W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_volt = VOLTAGE_VAL(VOLTAGE_5V);
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_curr = CURRENT_VAL(100);
        gp_usbpd_cfg->port_cfg.power.snk.epr_max_opr_curr = CURRENT_VAL(100);
        gp_usbpd_cfg->port_cfg.operational_pdp_epr = 1;
        
    }
    else if (SNK_POWER_REQUIRE_100W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_volt = VOLTAGE_VAL(VOLTAGE_20V);
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_curr = CURRENT_VAL(CURRENT_5000mA);
        gp_usbpd_cfg->port_cfg.power.snk.epr_max_opr_curr = CURRENT_VAL(CURRENT_5000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp_epr = 100;   
    }
    else if (SNK_POWER_REQUIRE_140W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_volt = VOLTAGE_VAL(VOLTAGE_28V);
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_curr = CURRENT_VAL(CURRENT_5000mA);
        gp_usbpd_cfg->port_cfg.power.snk.epr_max_opr_curr = CURRENT_VAL(CURRENT_5000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp_epr = 140;   
    }
    else if (SNK_POWER_REQUIRE_180W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_volt = VOLTAGE_VAL(VOLTAGE_36V);
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_curr = CURRENT_VAL(CURRENT_5000mA);
        gp_usbpd_cfg->port_cfg.power.snk.epr_max_opr_curr = CURRENT_VAL(CURRENT_5000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp_epr = 180;   
    }
    else if (SNK_POWER_REQUIRE_240W == index)
    {
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_volt = VOLTAGE_VAL(VOLTAGE_48V);
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_curr = CURRENT_VAL(CURRENT_5000mA);
        gp_usbpd_cfg->port_cfg.power.snk.epr_max_opr_curr = CURRENT_VAL(CURRENT_5000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp_epr = 240;   
    }
    else
    {
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_volt = VOLTAGE_VAL(VOLTAGE_5V);
        gp_usbpd_cfg->port_cfg.power.snk.epr_opr_curr = CURRENT_VAL(CURRENT_3000mA);
        gp_usbpd_cfg->port_cfg.power.snk.epr_max_opr_curr = CURRENT_VAL(CURRENT_3000mA);
        gp_usbpd_cfg->port_cfg.operational_pdp_epr = 15;
        
    }
}

/**
 * System PD configuration initialization
 *
 * \see
 */
static void sys_pd_cfg_init(void)
{
#if 0    
#if CAPTURE_MODE_ENABLED
    gp_usbpd_cfg->capture_mode = TRUE;
    gp_usbpd_cfg->cable_plug = FALSE;
    gp_usbpd_cfg->prod_type = UPD_PRODUCT_UNDEF;
#elif CABLE_PLUG_ENABLED
    gp_usbpd_cfg->cable_plug = TRUE;
    gp_usbpd_cfg->capture_mode = FALSE;
    gp_usbpd_cfg->prod_type = UPD_CBL_PROD_ACTIVE_CBL;
#else
    gp_usbpd_cfg->capture_mode = FALSE;
    gp_usbpd_cfg->cable_plug = FALSE;
    gp_usbpd_cfg->prod_type = UPD_PRODUCT_PSD;
#endif
#endif
#if 0
    gp_usbpd_cfg->cable_plug = TRUE;
    gp_usbpd_cfg->capture_mode = FALSE;
    gp_usbpd_cfg->prod_type = UPD_CBL_PROD_ACTIVE_CBL;
#else
    gp_usbpd_cfg->capture_mode = FALSE;
    gp_usbpd_cfg->cable_plug = FALSE;
    gp_usbpd_cfg->prod_type = UPD_PRODUCT_PSD;
#endif
    
    gp_usbpd_cfg->port_num = 1;
    gp_usbpd_cfg->vd_usb.svid = PD_STANDARD_VID; 
    gp_usbpd_cfg->fw_ver = SYS_PD_FIRMWARE_VERSION;
    gp_usbpd_cfg->hw_ver = SYS_PD_HARDWARE_VERSION;

    gp_usbpd_cfg->vd_usb.vid = DEFAULT_USB_VID; 
    gp_usbpd_cfg->vd_usb.tid = DEFAULT_USB_TID; 
    gp_usbpd_cfg->vd_usb.bcd_device = DEFAULT_USB_BCD_DEVICE; 
    gp_usbpd_cfg->vd_usb.prod_id = DEFAULT_USB_PRODUCT_ID; 
    gp_usbpd_cfg->vdm_modal = DEFAULT_USB_MODE0;
    gp_usbpd_cfg->port_cfg.spec_rev = SPEC_REVISION_3_0;
    gp_usbpd_cfg->port_cfg.cbl_type = CABLE_TYPE_C;
    gp_usbpd_cfg->port_cfg.retry_cnt = UPD_MSG_RETRY_R20_MAX;

    gp_usbpd_cfg->port_cfg.init_cc_half_bit_flg = 1;
    gp_usbpd_cfg->port_cfg.init_cc_half_bit = 3;

    gp_usbpd_cfg->port_cfg.init_cc_es_flg = 1;
    gp_usbpd_cfg->port_cfg.init_cc_es = 7;

    gp_usbpd_cfg->port_cfg.init_ldo2_flg = 0;
    gp_usbpd_cfg->port_cfg.init_ldo2 = 0;

    gp_usbpd_cfg->port_cfg.runtime_rd_flg = 0;
    gp_usbpd_cfg->port_cfg.runtime_rd = 0;

    gp_usbpd_cfg->port_cfg.runtime_cc_freq_flg = 0;
    gp_usbpd_cfg->port_cfg.runtime_cc_freq = 0;

    gp_usbpd_cfg->port_cfg.runtime_ldo2_flg = 0;
    gp_usbpd_cfg->port_cfg.runtime_ldo2 = 0;

    gp_usbpd_cfg->port_cfg.fixed_batteries = 0x1;

    SAL_BIT_SET(gp_usbpd_cfg->port_cfg.src_inputs, 2);

    if (gp_usbpd_cfg->cable_plug) {
        gp_usbpd_cfg->port_cfg.def_pdr = PD_ROLE_CABLE_PLUG;
        gp_usbpd_cfg->port_cfg.vdm.vconn_detected = TRUE;
        gp_usbpd_cfg->port_cfg.vdm_en = TRUE;
        //gp_usbpd_cfg->port_cfg.vdm.sop2_ctrler_pres = FALSE;
    }
    else {
#if (DPM_POWER_ROLE_SINK_ONLY==DPM_POWER_ROLE)
        gp_usbpd_cfg->port_cfg.pwr_src = POWER_SOURCE_BATTERY;
        gp_usbpd_cfg->port_cfg.def_pdr = PD_ROLE_CONSUMER;
        gp_usbpd_cfg->port_cfg.def_usb_pr = POWER_ROLE_SINK;
        gp_usbpd_cfg->port_cfg.def_usb_dr = USB_DATA_ROLE_UFP;
        gp_usbpd_cfg->port_cfg.try_src = FALSE;
        gp_usbpd_cfg->port_cfg.try_snk = FALSE;
        gp_usbpd_cfg->port_cfg.dual_role_pwr = FALSE;
        gp_usbpd_cfg->port_cfg.usb_susp_supp = FALSE;
        gp_usbpd_cfg->port_cfg.ext_pwr = FALSE;
        gp_usbpd_cfg->port_cfg.usb_comm_cap = FALSE;
        gp_usbpd_cfg->port_cfg.drs_cap = FALSE;
        gp_usbpd_cfg->port_cfg.vdm_en = FALSE;
        gp_usbpd_cfg->port_cfg.vdm_src_startup_en = FALSE;
#elif (DPM_POWER_ROLE_SOURCE_ONLY==DPM_POWER_ROLE)
        gp_usbpd_cfg->port_cfg.pwr_src = POWER_SOURCE_AC_INPUT;
        gp_usbpd_cfg->port_cfg.def_pdr = PD_ROLE_PROVIDER;
        gp_usbpd_cfg->port_cfg.def_usb_pr = POWER_ROLE_SOURCE;
        gp_usbpd_cfg->port_cfg.def_usb_dr = USB_DATA_ROLE_DFP;
        gp_usbpd_cfg->port_cfg.try_src = FALSE;
        gp_usbpd_cfg->port_cfg.try_snk = FALSE;
        gp_usbpd_cfg->port_cfg.dual_role_pwr = FALSE;
        gp_usbpd_cfg->port_cfg.usb_susp_supp = FALSE;
        gp_usbpd_cfg->port_cfg.ext_pwr = TRUE;
        gp_usbpd_cfg->port_cfg.usb_comm_cap = FALSE;
        gp_usbpd_cfg->port_cfg.drs_cap = FALSE;
        gp_usbpd_cfg->port_cfg.vdm_en = FALSE;
        gp_usbpd_cfg->port_cfg.vdm_src_startup_en = FALSE;
#else
        gp_usbpd_cfg->port_cfg.pwr_src = POWER_SOURCE_BATTERY;
        gp_usbpd_cfg->port_cfg.def_pdr = PD_ROLE_PROVIDER_CONSUMER;
        gp_usbpd_cfg->port_cfg.def_usb_pr = POWER_ROLE_DRP;
        gp_usbpd_cfg->port_cfg.def_usb_dr = USB_DATA_ROLE_DRD;
        gp_usbpd_cfg->port_cfg.try_src = FALSE;
        gp_usbpd_cfg->port_cfg.try_snk = FALSE;
        gp_usbpd_cfg->port_cfg.dual_role_pwr = TRUE;
        gp_usbpd_cfg->port_cfg.usb_susp_supp = FALSE;
        gp_usbpd_cfg->port_cfg.ext_pwr = TRUE;
        gp_usbpd_cfg->port_cfg.usb_comm_cap = FALSE;
        gp_usbpd_cfg->port_cfg.drs_cap = TRUE;
        gp_usbpd_cfg->port_cfg.vdm_en = TRUE;
        gp_usbpd_cfg->port_cfg.vdm_src_startup_en = TRUE;
#endif
        gp_usbpd_cfg->port_cfg.pps_volt_step = SYS_PPS_VOLT_STEP;
    }

#if VCONN_ENABLED
    gp_usbpd_cfg->port_cfg.vconn_en = TRUE;
#endif

    gp_usbpd_cfg->port_cfg.power_limited = SYS_POWER_LIMITED_ENABLED;
    gp_usbpd_cfg->port_cfg.vbus_check_en = FALSE;

    gp_usbpd_cfg->port_cfg.no_pd_mode = FALSE;

    //Vendor defined information
    gp_usbpd_cfg->port_cfg.vdm.struct_vdm_ver = SVDM_REVISION_2_0; //version 2.0
    gp_usbpd_cfg->port_cfg.vdm.cbl_latency = CABLE_LATENCY_1M;
    gp_usbpd_cfg->port_cfg.vdm.modal_opr_supp = FALSE;//VENDOR_DEFINE_MODE_ENABLED;
    gp_usbpd_cfg->port_cfg.vdm.mode_pos = DEFAULT_USB_MODE_POS;
    gp_usbpd_cfg->port_cfg.vdm.vbus_curr_cap = VBUS_CURR_HANDLING_CAP_5A;
    gp_usbpd_cfg->port_cfg.vdm.vbus_thr_cbl = TRUE;
    gp_usbpd_cfg->port_cfg.vdm.cbl_term_type = 0; //VCONN not required. Cable Plugs that only support Discover Identity Commands Shall set these bits to 00b.
    gp_usbpd_cfg->port_cfg.vdm.usb_ss_sig_supp = 0; // USB 3.1 Gen1 and Gen2
    gp_usbpd_cfg->port_cfg.vdm.max_vbus_volt = VBUS_VOLT_CABLE_50V; // 50V
    
    /* Power and USB vendor defined configuration */
    sys_pd_cfg_src_power_profile_set(POWER_PROFILE_140W_FIXED);
    sys_pd_cfg_snk_operating_power_profile_set(SNK_POWER_REQUIRE_15W);
    sys_pd_cfg_snk_epr_operating_power_profile_set(SNK_POWER_REQUIRE_140W);

    gp_usbpd_cfg->port_cfg.curr_src_supply_type = CURRENT_SRC_SUPPLY_330uA;
    gp_usbpd_cfg->port_cfg.vrd_min_src = VRD_MIN_SRC_5V_3A;
    gp_usbpd_cfg->port_cfg.vrd_max_src = VRD_MAX_SRC_5V_3A;
    gp_usbpd_cfg->port_cfg.vrd_min_snk = VRD_MIN_SNK_NO_PD;
    gp_usbpd_cfg->port_cfg.vrd_max_snk = VRD_MAX_SNK_NO_PD;
    gp_usbpd_cfg->port_cfg.vra_min = VRA_MIN_5V_3A;
    gp_usbpd_cfg->port_cfg.vra_max = VRA_MAX_5V_3A;
    gp_usbpd_cfg->port_cfg.vopen = VOPEN_5V_3A;
    gp_usbpd_cfg->port_cfg.epr_mode_en = TRUE;
    // enable QC
    gp_usbpd_cfg->port_cfg.quick_charge_en = TRUE;
    gp_usbpd_cfg->port_cfg.quick_charge_ver = DPM_QC_V3;

    gp_usbpd_cfg->port_cfg.power.snk.pps_en = TRUE;

    sys_cfg_dbg_set(SYS_DEBUG_LEVEL_OFF);
}

void sys_cfg_dbg_onchange(u32_t level)
{
    usbpd_cfg_dbg_set(level);
}

void sys_cfg_onsaving(void)
{
    // clean up the flag
    usbpd_cfg_port_src_fixed_pwr_prof_changed_flag_set(FALSE);
}

/**
 * Load system configuration from flash
 *
 * \see
 */
void sys_pd_cfg_load(void)
{
    // Must init gp_usbpd_cfg pointer before call sys_pd_cfg_init
    gp_usbpd_cfg = (volatile USB_PD_CONFIG_t *)&g_sys_cfg_map[sizeof(SYS_CONFIG_t)];
#if FLASH_ENABLED
    if (sys_cfg_flash_loaded_get())
    {
        gp_usbpd_cfg->port_num = 1;
    }
    else {
        sys_pd_cfg_init();
    }
#else
    sys_pd_cfg_init();
#endif
    usbpd_cfg_dbg_set(sys_cfg_dbg_get());
    sys_cfg_onchange_hook_set(dbg, sys_cfg_dbg_onchange);
    sys_cfg_onsaving_hook_set(sys_cfg_onsaving);
}

