/**
 * @file hv_pm51_app_Sys.c
 * @brief API for system initialization and delay function
 * @details
 * @author HiView SoC Software Team
 * @version 0.0.1
 * @date 20230301
 * @copyright Copyright(c),2023-3, Hiview Software. All rights reserved.
 * @par History:
 * <table>
 * <tr><th>author                     <th>date           <th>description
 * <tr><td>HiView SoC Software Team   <td>20230301       <td>Create and first Version.
 * </table>
**/

#include <reg51.h>
#include "HS_PAGE_HT7315_8051_reg.h"
#include "hv_pm51_Setting.h"
#include "hv_pm51_driver_Reg.h"
#include "hv_pm51_driver_I2C.h"
#include "hv_pm51_driver_Interrupt.h"
#include "hv_pm51_driver_Timer.h"
#include "hv_pm51_driver_UART.h"
#include "hv_pm51_driver_GPIO.h"
#if ADC_KEY_ENABLE
#include "hv_pm51_driver_ADC.h"
#endif
#include "hv_pm51_app_Key.h"
#include "hv_pm51_app_Mailbox.h"
#include "hv_pm51_app_Sys.h"
#include "hv_pm51_app_PM.h"
#include "hv_pm51_app_IR.h"
#include "BoardConfig.h"
#include "TypecConfig.h"
#include "Keyboard.h"
#include "KeyboardConfig.h"

#define TYPEC_ST_ATTACH_WAIT_SNK          0x3
#define TYPEC_ST_ATTACHED_SNK             0x4

extern volatile IntFlags g_unIntFlag;
extern volatile WakeupIntFlags g_unWakeupIntFlag;
extern volatile EventIntFlags g_unEventIntFlag;
extern volatile MiscIntFlags g_unMiscIntFlag;
extern xULONG32 g_ulTimer0Cnt;
extern UCHAR8 ucPmLedMode;
extern UCHAR8 ucPmFixMode;
extern UCHAR8 ucPmCurMainChannel;
extern UCHAR8 ucPmCurSubChannel;
extern UCHAR8 ucPowerState;

static xdata BOOL s_bIsMipsBoot = HV_FALSE;
static xdata BOOL s_bMipsBootTimerStart = HV_FALSE;
static xdata BOOL s_bFactoryModeTimerStart = HV_FALSE; /* menu + power */
#ifdef HV_PROJECT_CONFIG_AUTO_POWEROFF_MINUTES
static xdata BOOL s_bStandby2DCTimerStart = HV_FALSE;
static xdata xUCHAR8 s_ucTimeoutMinutes = 0;
#endif
static xUCHAR8 s_ucMipsBootRetry = MIPS_BOOT_RETRY_COUNTS;
xdata BOOL s_bIsKeyWork = HV_FALSE; /* After receive message of mips booting up, the keys will start to work. */
#if (PM51_CONFIG_POWER_KEY_MODE & POWER_KEY_MODE_MULTI)
xdata BOOL g_bPowerKeyHold = HV_FALSE;
#endif

static void Hv_PM51_Sys_SwVerInit(void)
{
    Hv_PM51_Reg_WriteByte(PM51_DEBUG_MAIN_VERSION_ADDR, PM51_SW_MAIN_VER);
    Hv_PM51_Reg_WriteByte(PM51_DEBUG_SUB_VERSION_ADDR, PM51_SW_SUB_VER);
}

static void Hv_PM51_Sys_AuxPhyInit(void)
{
    /* AUX0 */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_drv_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_drv_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_drv_BITEND, 0xA); /* reg_dprx_ana_aux_drv */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_hys_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_hys_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_hys_BITEND, 0x1); /* reg_dprx_ana_aux_hys */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_pn_swap_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_pn_swap_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_pn_swap_BITEND, 0x0); /* reg_dprx_ana_aux_pn_swap */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_pwd_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_pwd_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_pwd_BITEND, 0x0); /* reg_dprx_ana_aux_pwd */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_res_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_res_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_res_BITEND, 0xa); /* reg_dprx_ana_aux_res */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx0_aux_en_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx0_aux_en_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx0_aux_en_BITEND, 0x1);
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx0_aux_corr_value_th_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx0_aux_corr_value_th_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx0_aux_corr_value_th_BITEND, 0x7);
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx0_aux_glitch_th_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx0_aux_glitch_th_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx0_aux_glitch_th_BITEND, 0x5);
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx0_aux_half_1us_thd_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx0_aux_half_1us_thd_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx0_aux_half_1us_thd_BITEND, 0xd);
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx0_aux_from_pin_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx0_aux_from_pin_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx0_aux_from_pin_BITEND, 0x0); /* reg_dprx_aux_from_pin */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_mcu_reg__reg_aux0_clk_en_ADDR,
                      HS_Standby__standby_mcu_reg__reg_aux0_clk_en_BITSTART,
                      HS_Standby__standby_mcu_reg__reg_aux0_clk_en_BITEND, 0x1);

    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx0_aux_time_out_clr_en_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx0_aux_time_out_clr_en_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx0_aux_time_out_clr_en_BITEND, 0x0); /*0x3*/ /* reg_dprx_aux_time_out_clr_en */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx0_aux_wake_sel_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx0_aux_wake_sel_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx0_aux_wake_sel_BITEND, 0xE01); /* 0xB: wr 0x600 = 1, wr 0x600, aux wr */
    /* AUX1 */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_drv_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_drv_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_drv_BITEND, 0xA); /* reg_dprx_ana_aux_drv */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_hys_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_hys_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_hys_BITEND, 0x1); /* reg_dprx_ana_aux_hys */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_pn_swap_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_pn_swap_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_pn_swap_BITEND, 0x0); /* reg_dprx_ana_aux_pn_swap */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_pwd_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_pwd_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_pwd_BITEND, 0x0); /* reg_dprx_ana_aux_pwd */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_res_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_res_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_res_BITEND, 0xa); /* reg_dprx_ana_aux_res */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx1_aux_en_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx1_aux_en_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx1_aux_en_BITEND, 0x1);
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx1_aux_corr_value_th_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx1_aux_corr_value_th_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx1_aux_corr_value_th_BITEND, 0x7);
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx1_aux_glitch_th_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx1_aux_glitch_th_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx1_aux_glitch_th_BITEND, 0x5);
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx1_aux_half_1us_thd_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx1_aux_half_1us_thd_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx1_aux_half_1us_thd_BITEND, 0xd);
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx1_aux_from_pin_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx1_aux_from_pin_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx1_aux_from_pin_BITEND, 0x0); /* reg_dprx_aux_from_pin */

    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx1_aux_time_out_clr_en_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx1_aux_time_out_clr_en_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx1_aux_time_out_clr_en_BITEND, 0x0); /*0x3*/ /* reg_dprx_aux_time_out_clr_en */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx1_aux_wake_sel_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx1_aux_wake_sel_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx1_aux_wake_sel_BITEND, 0xE01); /* 0xB: wr 0x600 = 1, wr 0x600, aux wr */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_mcu_reg__reg_aux1_clk_en_ADDR,
                      HS_Standby__standby_mcu_reg__reg_aux1_clk_en_BITSTART,
                      HS_Standby__standby_mcu_reg__reg_aux1_clk_en_BITEND, 0x1);
    return;
}

#if HV_BOARD_CONFIG_HDMI_HPD_5V
static void Hv_PM51_Sys_HdmiHpdGpioInit(void)
{
    /* HDMI0 */
    Hv_PM51_Gpio_Init();
    Hv_PM51_Gpio_EnableInt(GPIO6, GPIO_INTR_EDGE_N);
    Hv_PM51_Gpio_ClearIntState(GPIO6);
    /* HDMI1 */
    Hv_PM51_Gpio_EnableInt(GPIO7, GPIO_INTR_EDGE_N);
    Hv_PM51_Gpio_ClearIntState(GPIO7);
}
#endif

#if TYPEC_SSPWD_ENABLE
static void Hv_PM51_Sys_TypecWaterDropInit(void)
{
#if (HV_TYPEC_CONFIG_PM51_SSPWD_USE_GPIO_INDEX < 8)
    /* solution for Samsung mobile phone's waterdrop issue */
    Hv_PM51_Gpio_Init();
    Hv_PM51_Gpio_InitPin(HV_TYPEC_CONFIG_PM51_SSPWD_USE_GPIO_INDEX);
    Hv_PM51_Gpio_SetIODirection(HV_TYPEC_CONFIG_PM51_SSPWD_USE_GPIO_INDEX, GPIO_OUTPUT);
    Hv_PM51_Gpio_SetOutputLevel(HV_TYPEC_CONFIG_PM51_SSPWD_USE_GPIO_INDEX, GPIO_LEVEL_LOW);
#endif

#if (HV_BOARD_CONFIG_DP0_TYPE == DP_PORT_ALTMODE)
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_pwd_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_pwd_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx0_ana_aux_pwd_BITEND, 0x1);
#endif

#if (HV_BOARD_CONFIG_DP1_TYPE == DP_PORT_ALTMODE)
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_pwd_ADDR,
                      HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_pwd_BITSTART,
                      HS_Standby__standby_peri_reg__reg_dprx1_ana_aux_pwd_BITEND, 0x1);
#endif
}
#endif

void Hv_PM51_Sys_Init(void)
{
    /* Register Init */
    Hv_PM51_Reg_Init();

    /* Fixed issue of boot failed for Chip */
    Hv_PM51_Reg_WriteMask(HS_Standby__AON_REG__reg_bg_rext_rint_sel_ADDR,
                      HS_Standby__AON_REG__reg_bg_rext_rint_sel_BITSTART,
                      HS_Standby__AON_REG__reg_bg_rext_rint_sel_BITEND, 0x1);

    /* Enable IO USER Mode */
    Hv_PM51_Reg_WriteMask(HS_Standby__SH08_TOP_STBY__reg_io_user_mode_ADDR,
                      HS_Standby__SH08_TOP_STBY__reg_io_user_mode_BITSTART,
                      HS_Standby__SH08_TOP_STBY__reg_io_user_mode_BITEND, 0x1);

    /* Set GPIO clk on */
    Hv_PM51_Reg_WriteMask(HS_Standby__standby_mcu_reg__reg_gpio_clk_en_ADDR,
                      HS_Standby__standby_mcu_reg__reg_gpio_clk_en_BITSTART,
                      HS_Standby__standby_mcu_reg__reg_gpio_clk_en_BITEND, 0x1);

    /* Software version init */
    Hv_PM51_Sys_SwVerInit();

    /* AUX phy init */
    Hv_PM51_Sys_AuxPhyInit();

#if HV_BOARD_CONFIG_HDMI_HPD_5V
    /* HDMI HPD GPIO Init */
    Hv_PM51_Sys_HdmiHpdGpioInit();
#endif

#if TYPEC_SSPWD_ENABLE
    Hv_PM51_Sys_TypecWaterDropInit();
#endif

    /* Clear all interrupt flag bits */
    g_unIntFlag.All = 0;

#if PM51_DEBUG_DUMMY_REGS
    Hv_PM51_Reg_WriteByte(PM51_DEBUG_PM_STATUS_ADDR, PM51_DEBUG_PM_STATUS_NORMAL);
    Hv_PM51_Reg_WriteByte(PM51_DEBUG_RISC_BOOT_STATUS_ADDR, PM51_DEBUG_RISC_BOOT_STATUS_NOT_BOOT);
    if(ucPmLedMode == HV_MODE_ON)
    {
        Hv_PM51_Reg_WriteByte(PM51_DEBUG_LED_MODE_ADDR, PM51_DEBUG_LED_MODE_ON);
    }
    if(ucPmLedMode == HV_MODE_OFF)
    {
        Hv_PM51_Reg_WriteByte(PM51_DEBUG_LED_MODE_ADDR, PM51_DEBUG_LED_MODE_OFF);
    }
    if(ucPmFixMode == HV_MODE_ON)
    {
        Hv_PM51_Reg_WriteByte(PM51_DEBUG_AUTO_SEARCH_ADDR, PM51_DEBUG_AUTO_SEARCH_OFF);
    }
    if(ucPmFixMode == HV_MODE_OFF)
    {
        Hv_PM51_Reg_WriteByte(PM51_DEBUG_AUTO_SEARCH_ADDR, PM51_DEBUG_AUTO_SEARCH_ON);
    }
    Hv_PM51_Reg_WriteByte(PM51_DEBUG_MAIN_CHANNEL_ADDR, (ucPmCurMainChannel+1));
    Hv_PM51_Reg_WriteByte(PM51_DEBUG_SUB_CHANNEL_ADDR, (ucPmCurSubChannel+1));
#endif
}

void Hv_PM51_Sys_DelayMs(USHORT16 usMs)
{
    USHORT16 usCounts;

    /* cpu clk=26.6M */
    for(;usMs > 0;usMs--)
    {
        for(usCounts = 1650;usCounts > 0;usCounts--); /* 1650 = magic num */
    }
}

void Hv_PM51_Sys_SetKeyState(BOOL bState)
{
    s_bIsKeyWork = bState;
}

BOOL Hv_PM51_Sys_GetKeyState(void)
{
    return s_bIsKeyWork;
}

void Hv_PM51_Sys_SetMipsBootState(BOOL bState)
{
    s_bIsMipsBoot = bState;
}

BOOL Hv_PM51_Sys_GetMipsBootState(void)
{
    return s_bIsMipsBoot;
}

void Hv_PM51_Sys_SetMipsBootTimerState(BOOL bState)
{
    s_bMipsBootTimerStart = bState;
}

void Hv_PM51_Sys_SetFactoryModeTimerState(BOOL bState)
{
    s_bFactoryModeTimerStart = bState;
}
void Hv_PM51_Sys_SetStandby2DCTimerState(BOOL bState)
{
#ifdef HV_PROJECT_CONFIG_AUTO_POWEROFF_MINUTES
    s_bStandby2DCTimerStart = bState;
    s_ucTimeoutMinutes = 0;
#else
    bState = 0;
#endif
}
void Hv_PM51_Sys_ResetMipsBootRetry(void)
{
    s_ucMipsBootRetry = MIPS_BOOT_RETRY_COUNTS;
}

#if (HV_PROJECT_CONFIG_ESEGMENT_PM51 == HV_CONFIG_ON)
void Hv_PM51_Sys_UpdateHdmiEdidData(UCHAR8 ucIndex, USHORT16 usLength, const UCHAR8 *pucEdidData)
{
    xUCHAR8 ucRData8 = 0;
    xULONG32 uiEdidLabel = 0;
    xULONG32 uiEdidWordValue = 0;
    xULONG32 uiEdidAddr = (ucIndex == 0)?(HS_Standby__hdmi21_rx_edid_00__reg_00_edid_data_00_ADDR)
                           :(HS_Standby__hdmi21_rx_edid_01__reg_01_edid_data_00_ADDR);
    xULONG32 uiSegmentOffset = 0;

    xULONG32 ulTmpData = 0;

    if(pucEdidData == NULL)
    {
        return;
    }

    /* Get Segment Value */
    if(ucIndex == 0)
    {
        ucRData8 = Hv_PM51_Reg_ReadByte(HS_Standby__hdmi21_rx_eddc_00__regr_00_edid_segment_ADDR+2);
        uiSegmentOffset = ucRData8;
    }
    else
    {
        ucRData8 = Hv_PM51_Reg_ReadByte(HS_Standby__hdmi21_rx_eddc_01__regr_01_edid_segment_ADDR+2);
        uiSegmentOffset = ucRData8;
    }
    uiSegmentOffset <<= 8;

    if((uiSegmentOffset > usLength) || (uiSegmentOffset == 0))
    {
        return;
    }

    for(uiEdidLabel = 0;
        (uiEdidLabel < 256) && (uiEdidLabel < (usLength - uiSegmentOffset));
        uiEdidLabel += 4, uiEdidAddr += 4)
    {
        Hv_PM51_Reg_WriteByte(uiEdidAddr, pucEdidData[uiEdidLabel + uiSegmentOffset]);
        Hv_PM51_Reg_WriteByte(uiEdidAddr + 1, pucEdidData[uiEdidLabel + uiSegmentOffset + 1]);
        Hv_PM51_Reg_WriteByte(uiEdidAddr + 2, pucEdidData[uiEdidLabel + uiSegmentOffset + 2]);
        Hv_PM51_Reg_WriteByte(uiEdidAddr + 3, pucEdidData[uiEdidLabel + uiSegmentOffset + 3]);
    }

    return;
}
#endif

void Hv_PM51_Sys_IntHandler(void)
{
#if TIMER_ENABLE
    /* Internal Timer0 */
    if(g_unIntFlag.Bits.stIntFlagInnerTimer0)
    {
        g_unIntFlag.Bits.stIntFlagInnerTimer0 = 0;
    }

    /* Internal Timer1 */
    if(g_unIntFlag.Bits.stIntFlagInnerTimer1)
    {
        g_unIntFlag.Bits.stIntFlagInnerTimer1 = 0;
#if (DPMS_TYPEC0_ENABLE && (HV_BOARD_CONFIG_DP0_TYPE == DP_PORT_ALTMODE))
        {
            xULONG32 ulRData32;
            ulRData32 = Hv_PM51_Reg_ReadMask(HS_Standby__standby_mcu_reg__regr_power_on_ADDR,
                                   HS_Standby__standby_mcu_reg__regr_power_on_BITSTART,
                                   HS_Standby__standby_mcu_reg__regr_power_on_BITEND);
            if(ulRData32 == 0)
            {
                ulRData32 = Hv_PM51_Reg_ReadMask(HS_Standby__usb_typec_top_00__regr_00_state_c_ADDR,
                                           HS_Standby__usb_typec_top_00__regr_00_state_c_BITSTART,
                                           HS_Standby__usb_typec_top_00__regr_00_state_c_BITEND);
                if((ulRData32 == TYPEC_ST_ATTACH_WAIT_SNK) || (ulRData32 == TYPEC_ST_ATTACHED_SNK))
                {
#if DPMS_FIX_MODE_ENABLE
                    if((ucPmFixMode == 1) && (ucPmCurMainChannel != CUR_CHANNEL_DP0) \
                        && (ucPmCurSubChannel != CUR_CHANNEL_DP0))
                    {
                        /* do nothing */
                    }
                    else
#endif
                    {
                        Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_TYPE_C_CABLE, 0);
                        return;
                    }
                }
                else
                {
                    Hv_PM51_Timer_Set(INNERTIMER1, 0xFFFF);
                }
            }
        }
#endif

#if (DPMS_TYPEC1_ENABLE && (HV_BOARD_CONFIG_DP1_TYPE == DP_PORT_ALTMODE))
        {
            xULONG32 ulRData32;
            ulRData32 = Hv_PM51_Reg_ReadMask(HS_Standby__standby_mcu_reg__regr_power_on_ADDR,
                                   HS_Standby__standby_mcu_reg__regr_power_on_BITSTART,
                                   HS_Standby__standby_mcu_reg__regr_power_on_BITEND);
            if(ulRData32 == 0)
            {
                ulRData32 = Hv_PM51_Reg_ReadMask(HS_Standby__usb_typec_top_01__regr_01_state_c_ADDR,
                                           HS_Standby__usb_typec_top_01__regr_01_state_c_BITSTART,
                                           HS_Standby__usb_typec_top_01__regr_01_state_c_BITEND);
                if((ulRData32 == TYPEC_ST_ATTACH_WAIT_SNK) || (ulRData32 == TYPEC_ST_ATTACHED_SNK))
                {
#if DPMS_FIX_MODE_ENABLE
                    if((ucPmFixMode == 1) && (ucPmCurMainChannel != CUR_CHANNEL_DP0) \
                        && (ucPmCurSubChannel != CUR_CHANNEL_DP0))
                    {
                        /* do nothing */
                    }
                    else
#endif
                    {
                        Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_TYPE_C_CABLE, 1);
                        return;
                    }
                }
                else
                {
                    Hv_PM51_Timer_Set(INNERTIMER1, 0xFFFF);
                }
            }
        }
#endif
    }

    /* External timer0 */
    if(g_unIntFlag.Bits.stIntFlagExTimer0)
    {
        g_unIntFlag.Bits.stIntFlagExTimer0 = 0;

        //Hv_PM51_Uart_Print("exTimer0 INT!\r\n");
#if ADC_KEY_ENABLE
        Hv_PM51_Key_TimerHandler();
#endif
    }

    /* External timer1 */
    if(g_unIntFlag.Bits.stIntFlagExTimer1)
    {
        g_unIntFlag.Bits.stIntFlagExTimer1 = 0;
        Hv_PM51_Uart_Print("External Timer1 INT!\r\n");
        if(s_bFactoryModeTimerStart)
        {
            s_bFactoryModeTimerStart = HV_FALSE;
            if(PM51_Key_JudgeMenuSts())
            {
                xdata Msg stMsg;
                stMsg.stMsgHeader.u8MsgType = E_MAILBOX_CMD_PM_TO_RISC_FACTORY_MODE;
                stMsg.stMsgHeader.pu8MsgLen[0] = 0;
                Hv_PM51_Mailbox_Send(&stMsg);
                Hv_PM51_Uart_Print("Factory mode !\r\n");
            }
            Hv_PM51_Sys_SetKeyState(HV_TRUE);
        }
        if(s_bMipsBootTimerStart)
        {
            if(s_bIsMipsBoot)
            {
                s_bMipsBootTimerStart = HV_FALSE;
#if PM51_DEBUG_DUMMY_REGS
                Hv_PM51_Reg_WriteByte(PM51_DEBUG_RISC_BOOT_STATUS_ADDR, PM51_DEBUG_RISC_BOOT_STATUS_BOOTED);
#endif
            }
            else
            {
                if(s_ucMipsBootRetry--)
                {
                    /* reset Main Domain */
                    Hv_PM51_PM_PowerOffMainDomain();
                    Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_POWER_ON, (MIPS_BOOT_RETRY_COUNTS-s_ucMipsBootRetry));
                    s_bMipsBootTimerStart = HV_TRUE;
                    Hv_PM51_Timer_Set(EXTIMER1, MIPS_BOOT_TIMEOUT);
                    Hv_PM51_Uart_Print("MIPS Bootup retry ... \r\n");
                }
                else
                {
                    s_bMipsBootTimerStart = HV_FALSE;
                    Hv_PM51_Sys_ResetMipsBootRetry();
                    /* need to add flashing led light for error indicated */
                    Hv_PM51_Uart_Print("MIPS Bootup Failed!\r\n");
#if PM51_DEBUG_DUMMY_REGS
                    Hv_PM51_Reg_WriteByte(PM51_DEBUG_RISC_BOOT_STATUS_ADDR, PM51_DEBUG_RISC_BOOT_STATUS_FAILED);
#endif
                }
            }
            Hv_PM51_Sys_SetKeyState(HV_TRUE);
        }
        #ifdef HV_PROJECT_CONFIG_AUTO_POWEROFF_MINUTES
        if (s_bStandby2DCTimerStart)
        {
            s_ucTimeoutMinutes++;
            Hv_PM51_Uart_Print("standby 1 minute!\r\n");
             Hv_PM51_Timer_Set(EXTIMER1, STANDBY_TIMEOUT);
            if (s_ucTimeoutMinutes == HV_PROJECT_CONFIG_AUTO_POWEROFF_MINUTES)
            {
                s_bStandby2DCTimerStart = HV_FALSE;
                s_ucTimeoutMinutes = 0;
                Hv_PM51_Timer_Clear(EXTIMER1);
                Hv_PM51_Uart_Print("standby timeout, enter dc!\r\n");
                ucPowerState = POWER_STATE_OFF;
                #if PM51_CONFIG_FAKE_DC
                Hv_PM51_PM_EnterStandBy();
                Hv_PM51_PM_SetHpdLow();
                #else
                Hv_PM51_PM_EnterPowerOff();
                #endif
            }

        }
        #endif
    }
#endif    /* End of TIMER_ENABLE */

    /* UART */
    if(g_unIntFlag.Bits.stIntFlagUart)
    {
        g_unIntFlag.Bits.stIntFlagUart = 0;
        {
            UCHAR8 ucTempChar;

            if(RI)
            {
                RI = 0;
                ucTempChar = SBUF;
            }

            /* Hv_PM51_Uart_IrqPut(ucTempChar); */
        }
    }

    /* Power Key */
    if(g_unIntFlag.Bits.stIntFlagPwrKey)
    {
#if(PM51_CONFIG_POWER_KEY_MODE == POWER_KEY_MODE_NORMAL)
        xULONG32 ulRData32;

        g_unIntFlag.Bits.stIntFlagPwrKey = 0;
        Hv_PM51_Uart_Print("PWR_KEY INT!\r\n");

        ulRData32 = Hv_PM51_Reg_ReadMask(HS_Standby__standby_mcu_reg__regr_power_on_ADDR,
                    HS_Standby__standby_mcu_reg__regr_power_on_BITSTART,
                    HS_Standby__standby_mcu_reg__regr_power_on_BITEND);

        if(ulRData32)
        {
            if(s_bIsKeyWork)
            {
                if(!PM51_Key_JudgeMenuSts())
                {
                    xdata Msg stMsg;
                    stMsg.stMsgHeader.u8MsgType = E_MAILBOX_CMD_PM_TO_RISC_POWER_OFF;
                    stMsg.stMsgHeader.pu8MsgLen[0] = 0;
                    Hv_PM51_Mailbox_Send(&stMsg);
                    Hv_PM51_Uart_Print("power key no factory mode!\r\n");
                }
                else
                {
                    s_bFactoryModeTimerStart = HV_TRUE;
                    Hv_PM51_Timer_Set(EXTIMER1, 3000); /* 3s */
                    Hv_PM51_Uart_Print("Start factory mode timer!\r\n");
                }
            }
        }
        else
        {
            Hv_PM51_Uart_Print("standby to poweroff!\r\n");
            Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_STANDBY_TO_POWEROFF, 0);
            return;
        }
#elif (PM51_CONFIG_POWER_KEY_MODE & POWER_KEY_MODE_MULTI)
        xULONG32 ulRData32;

        g_unIntFlag.Bits.stIntFlagPwrKey = 0;

        ulRData32 = Hv_PM51_Reg_ReadMask(HS_Standby__standby_mcu_reg__regr_power_on_ADDR,
                    HS_Standby__standby_mcu_reg__regr_power_on_BITSTART,
                    HS_Standby__standby_mcu_reg__regr_power_on_BITEND);

        if(ulRData32)
        {
            if(s_bIsKeyWork)
            {
                xdata Msg stMsg;
                stMsg.u8Val[0] = E_POWER_KEY_VAL_0;
                stMsg.stMsgHeader.u8MsgType = E_MAILBOX_CMD_PM_TO_RISC_KEY_VAL;
                stMsg.stMsgHeader.pu8MsgLen[0] = 1;
                Hv_PM51_Mailbox_Send(&stMsg);
                g_bPowerKeyHold = HV_TRUE;
            }
        }
        else
        {
#if (PM51_CONFIG_FAKE_DC == 1)
    #if ADC_KEY_ENABLE
            Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_KEY, E_POWER_KEY_VAL_0);
    #endif
#else
            if (ucPowerState == POWER_STATE_STANDBY)
            {
                Hv_PM51_Uart_Print("standby to poweroff!\r\n");
                Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_STANDBY_TO_POWEROFF, 0);
            }
            else
            {
                Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_KEY, E_POWER_KEY_VAL_0);
            }
#endif
            return;

        }
#else
        /* do nothing */
#endif
    }

    /* MIPS */
    if(g_unIntFlag.Bits.stIntFlagMips)
    {
        g_unIntFlag.Bits.stIntFlagMips = 0;
        Hv_PM51_Uart_Print("MIPS Trigger INT!\r\n");
        Hv_PM51_Mailbox_RecvHandler();
    }

    /* Wake Up */
    if(g_unIntFlag.Bits.stIntFlagWakeup)
    {
#if ADC_KEY_ENABLE
        xULONG32 ulRData32;
#endif
        USHORT16 usWakeupIntFlag = g_unWakeupIntFlag.All;

        g_unWakeupIntFlag.All = 0;
        g_unIntFlag.Bits.stIntFlagWakeup = 0;

#if (HV_PROJECT_CONFIG_ESEGMENT_PM51 == HV_CONFIG_ON)
        if(g_unMiscIntFlag.Bits.stMiscIntHdmi0EdidSeg == 1)
        {
            g_unMiscIntFlag.Bits.stMiscIntHdmi0EdidSeg = 0;
        }

        if(g_unMiscIntFlag.Bits.stMiscIntHdmi1EdidSeg == 1)
        {
            g_unMiscIntFlag.Bits.stMiscIntHdmi1EdidSeg = 0;
        }
#endif

        if((usWakeupIntFlag & BIT_5) > 0)  /* ADC1 */
        {
#if ADC_KEY_ENABLE

            ulRData32 = Hv_PM51_Reg_ReadMask(HS_Standby__standby_mcu_reg__regr_power_on_ADDR,
                                   HS_Standby__standby_mcu_reg__regr_power_on_BITSTART,
                                   HS_Standby__standby_mcu_reg__regr_power_on_BITEND);
            if(ulRData32)
            {
                Hv_PM51_Key_Handler(E_ADC1);
            }
            else
            {
            #if PM51_CONFIG_FAKE_DC
                AdcKeyVal ulCurKeyVal;
                xUSHORT16 usCurKeyData;
                usCurKeyData = (USHORT16)Hv_PM51_Adc_ReadVal(E_ADC1);
                ulCurKeyVal = PM51_Key_GetAdcKeyVal(E_ADC1, usCurKeyData);

                if ((ulCurKeyVal == PM51_CONFIG_POWER_KEY_WITH_ADC_VALUE) && (ucPowerState == POWER_STATE_OFF))
                {
                    Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_POWER_ON, 0);
                    return;
                }
                else if ((ulCurKeyVal == PM51_CONFIG_POWER_KEY_WITH_ADC_VALUE) && (ucPowerState == POWER_STATE_STANDBY))
                {
                    Hv_PM51_Uart_Print("standby to poweroff!\r\n");
                    Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_STANDBY_TO_POWEROFF, 0);
                }
                else if(ucPowerState == POWER_STATE_STANDBY)
                {
                    #if ADC_KEY_WAKEUP_ENABLE
                    Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_KEY, ulCurKeyVal);
                    return;
                    #endif
                }
            #else
                #if ADC_KEY_WAKEUP_ENABLE
                AdcKeyVal ulCurKeyVal;
                xUSHORT16 usCurKeyData;
                usCurKeyData = (USHORT16)Hv_PM51_Adc_ReadVal(E_ADC1);
                ulCurKeyVal = PM51_Key_GetAdcKeyVal(E_ADC1, usCurKeyData);
                {
                    Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_KEY, ulCurKeyVal);
                    return;
                }
                #endif
            #endif
            }
#endif
        }
        else if((usWakeupIntFlag & BIT_4) > 0) /* ADC0 */
        {
#if ADC_KEY_ENABLE

            ulRData32 = Hv_PM51_Reg_ReadMask(HS_Standby__standby_mcu_reg__regr_power_on_ADDR,
                                   HS_Standby__standby_mcu_reg__regr_power_on_BITSTART,
                                   HS_Standby__standby_mcu_reg__regr_power_on_BITEND);
            if(ulRData32)
            {
                Hv_PM51_Key_Handler(E_ADC0);
            }
            else
            {
            #if PM51_CONFIG_FAKE_DC
                AdcKeyVal ulCurKeyVal;
                xUSHORT16 usCurKeyData;
                usCurKeyData = (USHORT16)Hv_PM51_Adc_ReadVal(E_ADC0);
                ulCurKeyVal = PM51_Key_GetAdcKeyVal(E_ADC0, usCurKeyData);

                if ((ulCurKeyVal == PM51_CONFIG_POWER_KEY_WITH_ADC_VALUE) && (ucPowerState == POWER_STATE_OFF))
                {
                    Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_POWER_ON, 0);
                    return;
                }
                else if ((ulCurKeyVal == PM51_CONFIG_POWER_KEY_WITH_ADC_VALUE) && (ucPowerState == POWER_STATE_STANDBY))
                {
                    Hv_PM51_Uart_Print("standby to poweroff!\r\n");
                    Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_STANDBY_TO_POWEROFF, 0);
                }
                else if(ucPowerState == POWER_STATE_STANDBY)
                {
                    #if ADC_KEY_WAKEUP_ENABLE
                    Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_KEY, ulCurKeyVal);
                    return;
                    #endif
                }
            #else
                #if ADC_KEY_WAKEUP_ENABLE
                AdcKeyVal ulCurKeyVal;
                xUSHORT16 usCurKeyData;
                usCurKeyData = (USHORT16)Hv_PM51_Adc_ReadVal(E_ADC0);
                ulCurKeyVal = PM51_Key_GetAdcKeyVal(E_ADC0, usCurKeyData);
                {
                    Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_KEY, ulCurKeyVal);
                    return;
                }
                #endif
            #endif
            }
#endif
        }
        else
        {
            if((usWakeupIntFlag & BIT_15) > 0)   /* type-c 1 */
            {
                Hv_PM51_Uart_Print("Wake up by type-c 1!\r\n");
                Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_TYPE_C_CABLE, 1);
                return;
            }
            else if((usWakeupIntFlag & BIT_14) > 0)  /* type-c 0 */
            {
                Hv_PM51_Uart_Print("Wake up by type-c 0!\r\n");
                Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_TYPE_C_CABLE, 0);
                return;
            }
            else if((usWakeupIntFlag & BIT_13) > 0)  /* hdmi cec 1 */
            {
                Hv_PM51_Uart_Print("Wake up by hdmi cec 1!\r\n");
                Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_HDMI_CEC, 1);
                return;
            }
            else if((usWakeupIntFlag & BIT_12) > 0)  /* hdmi cec 0 */
            {
                Hv_PM51_Uart_Print("Wake up by hdmi cec 0!\r\n");
                Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_HDMI_CEC, 0);
                return;
            }
            else if((usWakeupIntFlag & BIT_11) > 0)  /* hdmi cable 1 */
            {
                Hv_PM51_Uart_Print("Wake up by HDMI calbe 1!\r\n");
                Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_HDMI_CABLE, 1);
                return;
            }
#if (HV_BOARD_CONFIG_DISPLAYPORT_1 == PORT_VALID)
            else if((usWakeupIntFlag & BIT_10) > 0)  /* dp cable 1 */
            {
                Hv_PM51_Uart_Print("Wake up by DP calbe 1!\r\n");
#if (HV_BOARD_CONFIG_DP1_TYPE == DP_PORT_NORM)
                Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_gpio_o_ADDR, 4, 4, 0x0);
#endif
                Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_DP_CABLE, 1);
                return;
            }
#endif
            else if((usWakeupIntFlag & BIT_9) > 0)  /* rx_phy 3 */
            {
                Hv_PM51_Uart_Print("Wake up by HDMI TMDS 1!\r\n");
                Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_HDMI_TMDS, 1);
                return;
            }
            else if((usWakeupIntFlag & BIT_8) > 0)  /* rx_phy 2 */
            {
                Hv_PM51_Uart_Print("Wake up by HDMI TMDS 0!\r\n");
                Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_HDMI_TMDS, 0);
                return;
            }
            else if((usWakeupIntFlag & BIT_3) > 0)  /* hdmi calbe 0 */
            {
                Hv_PM51_Uart_Print("Wake up by HDMI calbe 0!\r\n");
                Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_HDMI_CABLE, 0);
                return;
            }
#if DPMS_DP1_AUX_ENABLE
            else if((usWakeupIntFlag & BIT_2) > 0) /* dp aux 1 */
            {
                Hv_PM51_Uart_Print("Wake up by DP aux 1!\r\n");
#if (HV_BOARD_CONFIG_DP1_TYPE == DP_PORT_NORM)
                Hv_PM51_Reg_WriteMask(HS_Standby__standby_peri_reg__reg_gpio_o_ADDR, 4, 4, 0x0);
#endif
                Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_DP_AUX, 1);
                return;
            }
#endif
#if DPMS_DP0_AUX_ENABLE
            else if((usWakeupIntFlag & BIT_1) > 0) /* dp aux 0 */
            {
                Hv_PM51_Uart_Print("Wake up by DP aux 0!\r\n");
#if (HV_BOARD_CONFIG_DP0_TYPE == DP_PORT_NORM)
#ifdef HV_BOARD_CONFIG_DP0_HPD_GPIO
                Hv_PM51_Gpio_SetOutputLevel(HV_BOARD_CONFIG_DP0_HPD_GPIO, GPIO_LEVEL_LOW);
#endif
#endif
                Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_DP_AUX, 0);
                return;
            }
#endif
#if (HV_BOARD_CONFIG_DISPLAYPORT_0 == PORT_VALID)
#if (HV_BOARD_CONFIG_DP0_TYPE == DP_PORT_NORM)
            else if((usWakeupIntFlag & BIT_0) > 0) /* dp cable 0 */
            {
                Hv_PM51_Uart_Print("Wake up by DP cable 0!\r\n");
#ifdef HV_BOARD_CONFIG_DP0_HPD_GPIO
                Hv_PM51_Gpio_SetOutputLevel(HV_BOARD_CONFIG_DP0_HPD_GPIO, GPIO_LEVEL_LOW);
#endif
                Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_DP_CABLE, 0);
                return;
            }
#endif
#endif
            else
            {
                Hv_PM51_Uart_Print("Warning! Undefined Wake up interrupt!\r\n");
            }
        }
    }


    /* GPIO SW and PWM*/
    if(g_unIntFlag.Bits.stIntFlagEvent)
    {
        USHORT16 usEventIntFlag = g_unEventIntFlag.All;
        xULONG32 ulRData32;

        g_unEventIntFlag.All = 0;
        g_unIntFlag.Bits.stIntFlagEvent = 0;
#if GPIO_ENABLE
#if IR_ENABLE
        if((usEventIntFlag & BIT_6) > 0)
        {
            /* do nothing */
        }
#elif GPIO_KEY_ENABLE
        Hv_PM51_Uart_Print("GPIO Key INT!\r\n");
        if(s_bIsKeyWork)
        {
            if((usEventIntFlag & BIT_0) > 0)
            {
                Hv_PM51_Key_Handler(GPIO0);
            }
            if((usEventIntFlag & BIT_1) > 0)
            {
                Hv_PM51_Key_Handler(GPIO1);
            }
            if((usEventIntFlag & BIT_2) > 0)
            {
                Hv_PM51_Key_Handler(GPIO2);
            }
            if((usEventIntFlag & BIT_3) > 0)
            {
                Hv_PM51_Key_Handler(GPIO3);
            }
            if((usEventIntFlag & BIT_4) > 0)
            {
                Hv_PM51_Key_Handler(GPIO4);
            }
            if((usEventIntFlag & BIT_5) > 0)
            {
                Hv_PM51_Key_Handler(GPIO5);
            }
            if((usEventIntFlag & BIT_6) > 0)
            {
                Hv_PM51_Key_Handler(GPIO6);
            }
            if((usEventIntFlag & BIT_7) > 0)
            {
                Hv_PM51_Key_Handler(GPIO7);
            }
        }
#else
        if((usEventIntFlag & 0xff) > 0)
        {
            Hv_PM51_Uart_Print("GPIO INT!\r\n");
        }
#endif

#if DPMS_DP0_GPIO_ENABLE
        if((usEventIntFlag & BIT_3) > 0)
        {
            ulRData32 = Hv_PM51_Reg_ReadMask(HS_Standby__standby_mcu_reg__regr_power_on_ADDR,
                                   HS_Standby__standby_mcu_reg__regr_power_on_BITSTART,
                                   HS_Standby__standby_mcu_reg__regr_power_on_BITEND);
            if(ulRData32)
            {
                xdata Msg stMsg;
                stMsg.u8Val[0] = E_ADC_KEY_VAL_MAX;
                stMsg.stMsgHeader.u8MsgType = E_MAILBOX_CMD_PM_TO_RISC_DP0_AUXN_INT;
                stMsg.stMsgHeader.pu8MsgLen[0] = 1;
                Hv_PM51_Mailbox_Send(&stMsg);
            }
            else
            {
                Hv_PM51_Uart_Print("Wake up by DP GPIO3 !\r\n");
                Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_DP_GPIO, 0);
                return;
            }
        }
#endif

#if DPMS_DP1_GPIO_ENABLE
        if((usEventIntFlag & BIT_2) > 0)
        {
            ulRData32 = Hv_PM51_Reg_ReadMask(HS_Standby__standby_mcu_reg__regr_power_on_ADDR,
                                   HS_Standby__standby_mcu_reg__regr_power_on_BITSTART,
                                   HS_Standby__standby_mcu_reg__regr_power_on_BITEND);
            if(ulRData32)
            {
                xdata Msg stMsg;
                stMsg.u8Val[0] = E_ADC_KEY_VAL_MAX;
                stMsg.stMsgHeader.u8MsgType = E_MAILBOX_CMD_PM_TO_RISC_DP1_AUXN_INT;
                stMsg.stMsgHeader.pu8MsgLen[0] = 1;
                Hv_PM51_Mailbox_Send(&stMsg);
            }
            else
            {
                Hv_PM51_Uart_Print("Wake up by DP GPIO2 !\r\n");
                Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_DP_GPIO, 1);
                return;
            }
        }
#endif

#endif
        if((usEventIntFlag & BIT_8) > 0)
        {
            Hv_PM51_Uart_Print("SW INT!\r\n");
        }
        if((usEventIntFlag & BIT_9) > 0)
        {
            Hv_PM51_Uart_Print("PWM INT!\r\n");
        }
    }

#if WATCHDOG_ENABLE
    /* WDG */
    if(g_unIntFlag.Bits.stIntFlagWdg)
    {
        Hv_PM51_Uart_Print("Feed dog in WDG INT!\r\n");
        g_unIntFlag.Bits.stIntFlagWdg = 0;
    }
#endif
}
