/*******************************************************************************
 * @file     epwr.h
 * @brief    EPWR module header file.
 * @version  V4.0.3
 * @date     30-July-2026
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. All rights reserved.
 *
 * @attention
 * THIS SOFTWARE JUST PROVIDES CUSTOMERS WITH CODING INFORMATION REGARDING
 * THEIR PRODUCTS, WHICH AIMS AT SAVING TIME FOR THEM. SPINTROL SHALL NOT BE
 * LIABLE FOR THE USE OF THE SOFTWARE. SPINTROL DOES NOT GUARANTEE THE
 * CORRECTNESS OF THIS SOFTWARE AND RESERVES THE RIGHT TO MODIFY THE SOFTWARE
 * WITHOUT NOTIFICATION.
 *
 ******************************************************************************/

#ifndef EPWR_H
#define EPWR_H

#ifdef __cplusplus
extern "C" {
#endif


#include "spc1168.h"




/**
 *  @brief  EPWR Trip Zone Event
 */
typedef enum
{
    EPWR_TRIP_EVENT_TZ0                       = 0,      /*!< TZ0 event */
    EPWR_TRIP_EVENT_TZ1                       = 1       /*!< TZ1 event */
} EPWR_TripEventEnum;




/**
 *  @brief  EPWR Trip Zone Event Output selection
 */
typedef enum
{
    EPWR_OUTPUT_ORIGINAL                        = 0,    /*!< Original output                                                    */
    EPWR_OUTPUT_SYNC                            = 1,    /*!< Synchronous output with PWM clock                                  */
    EPWR_OUTPUT_FILTERED                        = 2,    /*!< Digital filter output                                              */
    EPWR_OUTPUT_LATCHED                         = 3,    /*!< Latched digital filter output                                      */
    EPWR_OUTPUT_ORIGINAL_OR_LATCHED             = 4,    /*!< Original output or Latched digital filter output                   */
    EPWR_OUTPUT_SYNC_OR_LATCHED                 = 5,    /*!< Synchronous output with PWM clock or Latched digital filter output */
    EPWR_OUTPUT_FILTERED_OR_LATCHED             = 6,    /*!< Digital filter output or the latched one                           */
    EPWR_OUTPUT_ORIGINAL_OR_FILTERED_OR_LATCHED = 7,    /*!< Original output or Synchronous output with PWM clock or Latched digital filter output */
} EPWR_OutputSelEnum;




/******************************************************************************
 * @brief      Get filtered Trip-zone event status
 *
 * @param[in]  eTZEvent: Select the trip-zone event
 *                       Please go to EPWR_TripEventEnum for details
 *
 * @return     0 - ePower trip-zone event not happen
 *             1 - ePower trip-zone event happened
 *
 *******************************************************************************/
#define EPWR_GetFilteredTripEventStatus(eTZEvent) ((EPWR->EPWRTZFLT.all & (0x1UL << eTZEvent)) >> (eTZEvent))




/******************************************************************************
 * @brief      Get latched Trip-zone event status which was also filtered
 *
 * @param[in]  eTZEvent: Select the trip-zone event
 *                       Please go to EPWR_TripEventEnum for details
 *
 * @return     0 - ePower trip-zone event not happen
 *             1 - ePower trip-zone event happened
 *
 *******************************************************************************/
#define EPWR_GetTripEventStatus(eTZEvent)   ((EPWR->EPWRTZSTS.all & (0x1UL << eTZEvent)) >> (eTZEvent))




/******************************************************************************
 * @brief      Clear latched Trip-zone event status
 *
 * @param[in]  eTZEvent: Select the trip-zone event
 *                       Please go to EPWR_TripEventEnum for details
 *
 * @return     none
 *
 *******************************************************************************/
#define EPWR_ClearTripEventStatus(eTZEvent) (EPWR->EPWRTZCLR.all = (0x1UL << (eTZEvent)))




/******************************************************************************
 * @brief      Reset Trip-zone filters
 *
 * @param[in]  eTZEvent: Select the trip-zone event
 *                       Please go to EPWR_TripEventEnum for details
 *
 * @return     none
 *
 *******************************************************************************/
__STATIC_INLINE void EPWR_ResetFilter(EPWR_TripEventEnum eTZEvent)
{
    switch (eTZEvent)
    {
        case EPWR_TRIP_EVENT_TZ0:
            EPWR->EPWRTZ0CTL.all |= EPWRTZ0CTL_ALL_FLTRST_RESET;
            break;

        case EPWR_TRIP_EVENT_TZ1:
            EPWR->EPWRTZ1CTL.all |= EPWRTZ1CTL_ALL_FLTRST_RESET;
            break;
    }
}




/******************************************************************************
 * @brief      Enable PWMSYNC clear Trip-zone event latched status
 *
 * @param[in]  eTZEvent: Select the trip-zone event
 *                       Please go to EPWR_TripEventEnum for details
 *
 * @return     none
 *
 *******************************************************************************/
__STATIC_INLINE void EPWR_EnablePWMSyncClearTripEventStatus(EPWR_TripEventEnum eTZEvent)
{
    switch (eTZEvent)
    {
        case EPWR_TRIP_EVENT_TZ0:
            EPWR->EPWRTZ0CTL.bit.SYNCCLREN = EPWRTZ0CTL_BIT_SYNCCLREN_ENABLE;
            break;

        case EPWR_TRIP_EVENT_TZ1:
            EPWR->EPWRTZ1CTL.bit.SYNCCLREN = EPWRTZ1CTL_BIT_SYNCCLREN_ENABLE;
            break;
    }
}




/******************************************************************************
 * @brief      Disable PWMSYNC clear Trip-zone event latched status
 *
 * @param[in]  eTZEvent: Select the trip-zone event
 *                       Please go to EPWR_TripEventEnum for details
 *
 * @return     none
 *
 *******************************************************************************/
__STATIC_INLINE void EPWR_DisablePWMSyncClearTripEventStatus(EPWR_TripEventEnum eTZEvent)
{
    switch (eTZEvent)
    {
        case EPWR_TRIP_EVENT_TZ0:
            EPWR->EPWRTZ0CTL.bit.SYNCCLREN = EPWRTZ0CTL_BIT_SYNCCLREN_DISABLE;
            break;

        case EPWR_TRIP_EVENT_TZ1:
            EPWR->EPWRTZ1CTL.bit.SYNCCLREN = EPWRTZ1CTL_BIT_SYNCCLREN_DISABLE;
            break;
    }
}




/******************************************************************************
 * @brief      Set EPWR Trip-zone event input active low
 *
 * @param[in]  eTZEvent: Select the trip-zone event
 *                       Please go to EPWR_TripEventEnum for details
 *
 * @return     none
 *
 *******************************************************************************/
__STATIC_INLINE void EPWR_SetTripEventInputActiveLow(EPWR_TripEventEnum eTZEvent)
{
    switch (eTZEvent)
    {
        case EPWR_TRIP_EVENT_TZ0:
            EPWR->EPWRTZ0CTL.bit.INPOL = EPWRTZ0CTL_BIT_INPOL_ACTIVE_LOW;
            break;

        case EPWR_TRIP_EVENT_TZ1:
            EPWR->EPWRTZ1CTL.bit.INPOL = EPWRTZ1CTL_BIT_INPOL_ACTIVE_LOW;
            break;
    }
}




/******************************************************************************
 * @brief      Set EPWR Trip-zone event input active high
 *
 * @param[in]  eTZEvent: Select the trip-zone event
 *                       Please go to EPWR_TripEventEnum for details
 *
 * @return     none
 *
 *******************************************************************************/
__STATIC_INLINE void EPWR_SetTripEventInputActiveHigh(EPWR_TripEventEnum eTZEvent)
{
    switch (eTZEvent)
    {
        case EPWR_TRIP_EVENT_TZ0:
            EPWR->EPWRTZ0CTL.bit.INPOL = EPWRTZ0CTL_BIT_INPOL_ACTIVE_HIGH;
            break;

        case EPWR_TRIP_EVENT_TZ1:
            EPWR->EPWRTZ1CTL.bit.INPOL = EPWRTZ1CTL_BIT_INPOL_ACTIVE_HIGH;
            break;
    }
}




/******************************************************************************
 * @brief      Enable Trip-zone event input
 *
 * @param[in]  eTZEvent: Select the trip-zone event
 *                       Please go to EPWR_TripEventEnum for details
 *
 * @return     none
 *
 *******************************************************************************/
__STATIC_INLINE void EPWR_EnableTripEvent(EPWR_TripEventEnum eTZEvent)
{
    switch (eTZEvent)
    {
        case EPWR_TRIP_EVENT_TZ0:
            EPWR->EPWRTZ0CTL.bit.IE = EPWRTZ0CTL_BIT_IE_ENABLE;
            break;

        case EPWR_TRIP_EVENT_TZ1:
            EPWR->EPWRTZ1CTL.bit.IE = EPWRTZ1CTL_BIT_IE_ENABLE;
            break;
    }
}




/******************************************************************************
 * @brief      Disable Trip-zone event input
 *
 * @param[in]  eTZEvent: Select the trip-zone event
 *                       Please go to EPWR_TripEventEnum for details
 *
 * @return     none
 *
 *******************************************************************************/
__STATIC_INLINE void EPWR_DisableTripEvent(EPWR_TripEventEnum eTZEvent)
{
    switch (eTZEvent)
    {
        case EPWR_TRIP_EVENT_TZ0:
            EPWR->EPWRTZ0CTL.bit.IE = EPWRTZ0CTL_BIT_IE_DISABLE;
            break;

        case EPWR_TRIP_EVENT_TZ1:
            EPWR->EPWRTZ1CTL.bit.IE = EPWRTZ1CTL_BIT_IE_DISABLE;
            break;
    }
}




/******************************************************************************
 * @brief      Set EPWR Trip-zone event output active low
 *
 * @param[in]  eTZEvent: Select the trip-zone event
 *                       Please go to EPWR_TripEventEnum for details
 *
 * @return     none
 *
 *******************************************************************************/
__STATIC_INLINE void EPWR_SetTripEventOutputActiveLow(EPWR_TripEventEnum eTZEvent)
{
    switch (eTZEvent)
    {
        case EPWR_TRIP_EVENT_TZ0:
            EPWR->EPWRTZ0CTL.bit.POL4PWM = EPWRTZ0CTL_BIT_POL4PWM_ACTIVE_LOW;
            break;

        case EPWR_TRIP_EVENT_TZ1:
            EPWR->EPWRTZ1CTL.bit.POL4PWM = EPWRTZ1CTL_BIT_POL4PWM_ACTIVE_LOW;
            break;
    }
}




/******************************************************************************
 * @brief      Set EPWR Trip-zone event output active high
 *
 * @param[in]  eTZEvent: Select the trip-zone event
 *                       Please go to EPWR_TripEventEnum for details
 *
 * @return     none
 *
 *******************************************************************************/
__STATIC_INLINE void EPWR_SetTripEventOutputActiveHigh(EPWR_TripEventEnum eTZEvent)
{
    switch (eTZEvent)
    {
        case EPWR_TRIP_EVENT_TZ0:
            EPWR->EPWRTZ0CTL.bit.POL4PWM = EPWRTZ0CTL_BIT_POL4PWM_ACTIVE_HIGH;
            break;

        case EPWR_TRIP_EVENT_TZ1:
            EPWR->EPWRTZ1CTL.bit.POL4PWM = EPWRTZ1CTL_BIT_POL4PWM_ACTIVE_HIGH;
            break;
    }
}




/******************************************************************************
 * @brief      Set Trip-zone event output type
 *
 * @param[in]  eTZEvent  : Select the trip-zone event
 *                         Please go to EPWR_TripEventEnum for details
 * @param[in]  eOutputSel: Select the trip-zone output
 *                         Please go to EPWR_OutputSelEnum for details
 *
 * @return     none
 *
 *******************************************************************************/
__STATIC_INLINE void EPWR_SetTripEventOutputType(EPWR_TripEventEnum eTZEvent, EPWR_OutputSelEnum eOutputSel)
{
    switch (eTZEvent)
    {
        case EPWR_TRIP_EVENT_TZ0:
            EPWR->EPWRTZ0CTL.bit.SEL4PWM = eOutputSel;
            break;

        case EPWR_TRIP_EVENT_TZ1:
            EPWR->EPWRTZ1CTL.bit.SEL4PWM = eOutputSel;
            break;
    }
}




/******************************************************************************
 * @brief      Select PWM Synchronization signal to clear Trip-zone event latched status
 *
 * @param[in]  eTZEvent : Select the trip-zone event
 *                        Please go to EPWR_TripEventEnum for details
 * @param[in]  ePWMIndex: PWM module index
 *                        Please go to PWM_SelEnum for details
 *
 * @return     none
 *
 * @example    EPWR_SetSyncEvent(EPWR_TRIP_EVENT_TZ0, SEL_PWM0);
 *
 *******************************************************************************/
__STATIC_INLINE void EPWR_SetSyncEvent(EPWR_TripEventEnum eTZEvent, uint8_t ePWMIndex)
{
    switch (eTZEvent)
    {
        case EPWR_TRIP_EVENT_TZ0:
            EPWR->EPWRCTL.bit.SYNCSEL0 = ePWMIndex;
            break;

        case EPWR_TRIP_EVENT_TZ1:
            EPWR->EPWRCTL.bit.SYNCSEL1 = ePWMIndex;
            break;
    }
}




/******************************************************************************
 * @brief      Enable the power module connected with EPWR
 *
 * @param[in]  none
 *
 * @return     none
 *
 *******************************************************************************/
#define EPWR_Enable()                       (EPWR->EPWRCTL.all |= EPWRCTL_ALL_EN_ENABLE)




/******************************************************************************
 * @brief      Disable the power module connected with EPWR
 *
 * @param[in]  none
 *
 * @return     none
 *
 *******************************************************************************/
#define EPWR_Disable()                      (EPWR->EPWRCTL.bit.EN = EPWRCTL_BIT_EN_DISABLE)




/******************************************************************************
 * @brief      Enable communication interface between the power module and EPWR
 *
 * @param[in]  none
 *
 * @return     none
 *
 *******************************************************************************/
#define EPWR_EnableComInterface()           (EPWR->EPWRCTL.all |= EPWRCTL_ALL_ENSPI_ENABLE)




/******************************************************************************
 * @brief      Disable communication interface between the power module and EPWR
 *
 * @param[in]  none
 *
 * @return     none
 *
 *******************************************************************************/
#define EPWR_DisableComInterface()           (EPWR->EPWRCTL.bit.ENSPI = EPWRCTL_BIT_ENSPI_DISABLE)




/******************************************************************************
 * @brief      Enable Transmit data to the power module
 *
 * @param[in]  none
 *
 * @return     none
 *
 *******************************************************************************/
#define EPWR_EnableTransmitData()           (EPWR->EPWRCTL.all |= EPWRCTL_ALL_SPIDIR_SEND_DATA)




/******************************************************************************
 * @brief      Enable Receive data from the power module
 *
 * @param[in]  none
 *
 * @return     none
 *
 *******************************************************************************/
#define EPWR_EnableReceiveData()             (EPWR->EPWRCTL.bit.SPIDIR = EPWRCTL_BIT_SPIDIR_RECEIVE_DATA)




/******************************************************************************
 * @brief      Enable write access to the protected EPWR registers
 *
 * @param[in]  none
 *
 * @return     none
 *
 *******************************************************************************/
#define EPWR_WALLOW()                       (EPWR->EPWRREGKEY.all = 0x1ACCE551)




/******************************************************************************
 * @brief      Disable write access to the protected EPWR registers
 *
 * @param[in]  none
 *
 * @return     none
 *
 *******************************************************************************/
#define EPWR_WDIS()                         (EPWR->EPWRREGKEY.all = 0x0)




/**
 *  @brief  EPWR Public Function Declaration
 */
void EPWR_SetFilterClkDiv(EPWR_TripEventEnum eTZEvent, uint16_t u16ClkDiv);
void EPWR_SetFilterWindow(EPWR_TripEventEnum eTZEvent, uint8_t u8WinSize, uint8_t u8Threshold);
void EPWR_SetFilterWindowTimeNs(EPWR_TripEventEnum eTZEvent, uint32_t u32SizeTimeNS, uint32_t u32ThresholdTimeNS);


#ifdef __cplusplus
}
#endif /* extern "C" */

#endif /* EPWR_H */


/******************* Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd. ***** END OF FILE ****/
