/**
 * @file main.c
 * @brief Main entry of PM51 app.
 * @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 "hv_pm51_Reg.h"
#include "hv_pm51_Setting.h"
#include "HS_PAGE_HT7315_8051_reg.h"
#include "hv_pm51_driver_Reg.h"
#include "hv_pm51_driver_UART.h"
#include "hv_pm51_driver_Interrupt.h"
#if GPIO_ENABLE
#include "hv_pm51_driver_GPIO.h"
#endif
#if TIMER_ENABLE
#include "hv_pm51_driver_Timer.h"
#endif
#if WATCHDOG_ENABLE
#include "hv_pm51_driver_WDG.h"
#endif
#if MI2C_ENABLE
#include "hv_pm51_driver_I2C.h"
#endif
#if GPIO_ENABLE && IR_ENABLE
#include "hv_pm51_app_IR.h"
#endif
#if (ADC_KEY_ENABLE || GPIO_KEY_ENABLE)
#include "hv_pm51_app_Key.h"
#endif
#include "hv_pm51_app_Sys.h"
#include "hv_pm51_app_PM.h"
#include "hv_pm51_app_Mailbox.h"
#include "hv_pm51_app_Pinshare.h"
extern UCHAR8 ucPowerState;

void main(void)
{
    /* System Init */
    Hv_PM51_Sys_Init();

#if UART_PRINT_ENABLE
    /* UART Init */
    Hv_PM51_Uart_Init(E_UART_MODE1, E_BR_115200);
#endif
    Hv_PM51_Uart_Print("\r\nHT7315 PM51 Application Start!\r\n");
    PM51_Key_SetLed(E_KEY_LED_OFF);
    /* Interrupt Init */
    Hv_PM51_Uart_Print("Interrupt Init Done !\r\n");
    Hv_PM51_Int_Init();
    Hv_PM51_Pinshare_Init();
    /* stby 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);
#if WATCHDOG_ENABLE
    /* Watch Dog Init */
    Hv_PM51_Wdg_SetTimer(DEFAULT_FEED_DOG_TIME);
    Hv_PM51_Wdg_SetEnable(WDG_ENABLE);
#endif

#if POWER_ON_CHECK_ENABLE
    Hv_PM51_PM_PowerOnCheckPmu();
#endif

    /* External Timer1 Init */
    Hv_PM51_Timer_Init(EXTIMER1, E_TIMER_MODE_NULL);
#if PM51_CONFIG_FAKE_DC
    if (ucPowerState != POWER_STATE_OFF)
#endif
    {
        /* Boot Up Sequense */
        Hv_PM51_PM_BootUpSequense(E_WAKE_UP_TYPE_POWER_ON, 0);
    }

#if (ADC_KEY_ENABLE || GPIO_KEY_ENABLE)
    Hv_PM51_Key_Init();
#endif

#if IR_ENABLE
    /* IR Init */
    Hv_PM51_IR_Init();
    /* add for fakedc with ir. */
    Hv_PM51_Sys_SetKeyState(HV_TRUE);
#endif

    /* MailBox Init */
    Hv_PM51_Mailbox_Init();

#if DPMS_DP0_GPIO_ENABLE
    Hv_PM51_Gpio_Init();
    Hv_PM51_Gpio_InitPin(DPMS_DP0_GPIO);
    Hv_PM51_Gpio_SetIODirection(DPMS_DP0_GPIO, GPIO_INPUT);
#endif

#if DPMS_DP1_GPIO_ENABLE
    Hv_PM51_Gpio_Init();
    Hv_PM51_Gpio_InitPin(DPMS_DP1_GPIO);
    Hv_PM51_Gpio_SetIODirection(DPMS_DP1_GPIO, GPIO_INPUT);
#endif

    while(HV_TRUE)
    {
        Hv_PM51_PM_EnterIdle();

        Hv_PM51_Sys_IntHandler();

#if (GPIO_ENABLE && IR_ENABLE)
        Hv_PM51_IR_SendKeyVal();
#endif
        Hv_PM51_Mailbox_SendHandler();
    }

    /* never go here */
    while(HV_TRUE);
}
