/**
 * @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**************************************/
/*******************************************************************/

VOID Hv_Drv_Typec_PSF_Init (void)
{
    UINT8 u8PortNum;
    UINT8 u8InitStatus = HV_TRUE;
    
    /* Load configurations */
    PSF_LoadConfig(&gasCfgStatusData);

    /*TYPEC interrupt Initialization*/
    Hv_Drv_Typec_IntInit();
       
    /*Timer module Initialization*/
    Hv_Drv_Typec_Timer_Init ();

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

    /*Initialize Internal global variables*/
    IntGlobals_PDInitialization ();

    #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();

    DPM_InitStateMachine ();  

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

    HV_LOG_DEBUG(TYPEC, "Typec PSF Init Done.");
}
/********************************************************************************************/
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
        HV_W32(0x11011A10, gasChunkSM [u8PortNum].u8ChunkState<<24 | gasPolicyEngine[u8PortNum].ePEState<<8 | gasPolicyEngine[u8PortNum].ePESubState);

#if (HV_TYPEC_CONFIG_TYPEC_ERROR_RECOVERY_NEW == 1)
        if (HV_R32(0x11011A14) == 3)
        {
            HV_W32(0x11011A14, 0);
            DPM_SetTypeCState(0, TYPEC_ERROR_RECOVERY, TYPEC_ERROR_RECOVERY_ENTRY_SS);
        }
#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) ||
            (gasPolicyEngine[u8PortNum].ePEState == ePE_SNK_WAIT_FOR_CAPABILITIES) ||
            (gasPolicyEngine[u8PortNum].ePEState == ePE_INVALIDSTATE))  //add for billboard
        {
            Hv_Vos_MSleep(10);
        }
    }
}

