/**
 * @file hv_drv_TypecMain.c
 * @brief  PSF stack main source file,This file contains the "main" and initialization function for PSF. 
 * @details This file provides the following functions: \n
 *          (1) \n
 *          (2) \n
 *          (3)  \n
 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2022-08-25
 * @copyright Copyright(c),2022-8, Hiview Software. All rights reserved.
 * @par History:
 * <table>
 * <tr><th>Author                         <th>Date         <th>Change Description
 * <tr><td>Hiview SoC Software Team       <td>2022-08-25   <td>init
 * </table>
*/

#include <hv_drv_TypecStdInc.h>
#include "hv_vos_Comm.h"

/*******************************************************************/
/******************* Functions**************************************/
/*******************************************************************/

UINT8 Hv_PSF_Init (void)
{
    UINT8 u8PortNum;
    UINT8 u8InitStatus = HV_TRUE;

    /*Initialize PSF Stack and Structure version*/
    IntGlobals_StackStructVersion ();
    
    /* Load configurations */
    HV_PSF_HOOK_BOOT_TIME_CONFIG(&gasCfgStatusData);

    /*TYPEC interrupt Initialization*/
    Hv_Drv_Typec_IntInit();
       
    /*Timer module Initialization*/
    u8InitStatus &= PDTimer_Init ();

    //if take the polling method to read VBUS
    HV_PSF_HOOK_POLLING_READ_VBUS_INIT();

    for (u8PortNum = SET_TO_ZERO; u8PortNum < CONFIG_PD_PORT_COUNT; u8PortNum++)
    {
        /*If Timer and HW module of SOC are not initialized properly disable all the ports*/
        if (HV_TRUE != u8InitStatus)
        {
            DPM_DISABLE_CONFIGURED_PORT_EN(u8PortNum);
        }

        /*Initialize orientation pin*/
        HV_PSF_HOOK_GPIO_FUNC_INIT(u8PortNum, eORIENTATION_FUNC);
    }
        
    /*Initialize Internal global variables*/
    IntGlobals_PDInitialization ();
    
    UPD_CheckAndDisablePorts ();    
    
    #if (HV_TRUE == CONFIG_HOOK_DEBUG_MSG)
        /*Initialize debug hardware*/
        HV_PSF_HOOK_DEBUG_INIT();
    #endif
    
    /* Disable the global interrupt */
    HV_PSF_HOOK_DISABLE_GLOBAL_INTERRUPT();
        
    for (u8PortNum = SET_TO_ZERO; u8PortNum < CONFIG_PD_PORT_COUNT; u8PortNum++)
    {
        if (UPD_PORT_ENABLED == DPM_GET_CONFIGURED_PORT_EN(u8PortNum))
        {
            /*Port Power Initialization*/
            u8InitStatus &= PWRCTRL_Init (u8PortNum);
        }
    }
    
    for (u8PortNum = SET_TO_ZERO; u8PortNum < CONFIG_PD_PORT_COUNT; u8PortNum++)
    {
        if (UPD_PORT_ENABLED == DPM_GET_CONFIGURED_PORT_EN(u8PortNum))
        {
            /* Initialize the Port's IRQ*/
            // extern function init
            
            /*Initialize the Port's DC_DC Alert */
            //HV_PSF_HOOK_GPIO_FUNC_INIT(u8PortNum, eI2C_DC_DC_ALERT_FUNC);        
        }
    }    
    
    DPM_InitStateMachine ();  

    /* Enable the global interrupt */
    HV_PSF_HOOK_ENABLE_GLOBAL_INTERRUPT();

    HV_LOG_DEBUG(TYPEC, "Typec PSF Init Done.");
    
    return u8InitStatus;

}
/********************************************************************************************/
void Hv_PSF_RUN ()
{
    UINT8 u8PortNum;
    for (u8PortNum = SET_TO_ZERO; u8PortNum < CONFIG_PD_PORT_COUNT; u8PortNum++)
    {
        if (UPD_PORT_ENABLED == DPM_GET_CONFIGURED_PORT_EN(u8PortNum))
        {
           DPM_RunStateMachine (u8PortNum);
           Hv_PSF_PollingVBUS (u8PortNum);
        }

#if TYPEC_DEBUG_TRACE_EN
        UPDIntr_PollingHandler(u8PortNum);
#endif
        if ((gasPolicyEngine[u8PortNum].ePEState == ePE_SRC_DISABLED) ||
            (gasPolicyEngine[u8PortNum].ePEState == ePE_SRC_STARTUP) ||
            (gasPolicyEngine[u8PortNum].ePEState == ePE_SRC_READY) ||
            (gasPolicyEngine[u8PortNum].ePEState == ePE_SNK_READY) ||
            (gasPolicyEngine[u8PortNum].ePEState == ePE_SNK_DISCOVERY))
        {
            Hv_Vos_MSleep(10);
        }
    }
}
/*********************************************************************************************/

/*********************************************************************************************/
void Hv_PSF_UPDIrqHandler (UINT8 u8PortNum)
{
    UPDIntr_AlertHandler (u8PortNum);
}
/*********************************************************************************************/

/*********************************************************************************************/
void Hv_PSF_PDTimerHandler (void)
{

}
/*********************************************************************************************/

