/*******************************************************************************
 * @file     epwr.c
 * @brief    EPWR module firmware functions.
 * @version  V1.8.0
 * @date     31-Dec-2021
 *
 * @note
 * Copyright (C) 2021 Spintrol Limited. 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.
 *
 ******************************************************************************/


#include "epwr.h"




/******************************************************************************
 * @brief      Set Trip-zone filter clock dividing ratio
 *
 * @param[in]  eTZEvent : Select the trip-zone event
 *                        Please go to EPWR_TripEventEnum for details
 * @param[in]  u16ClkDiv: Dividing ratio (1~1024)
 *
 * @return     none
 *
 *******************************************************************************/
void EPWR_SetFilterClkDiv(EPWR_TripEventEnum eTZEvent, uint16_t u16ClkDiv)
{
    if ((u16ClkDiv == 0) || (u16ClkDiv > 1024))
    {
        return;
    }

    switch (eTZEvent)
    {
        case EPWR_TRIP_EVENT_TZ0:
            EPWR->EPWRTZ0CTL.bit.FLTDIV = u16ClkDiv - 1;
            break;

        case EPWR_TRIP_EVENT_TZ1:
            EPWR->EPWRTZ1CTL.bit.FLTDIV = u16ClkDiv - 1;
            break;
    }
}




/******************************************************************************
 * @brief      Set Trip-zone filter window size and threshold
 *
 * @param[in]  eTZEvent   : Select the trip-zone event
 *                          Please go to EPWR_TripEventEnum for details
 * @param[in]  u8WinSize  : Filter window size (1 ~ 32)
 * @param[in]  u8Threshold: Filter threshold value (1 ~ 32)
 *
 * @return     none
 *
 *******************************************************************************/
void EPWR_SetFilterWindow(EPWR_TripEventEnum eTZEvent, uint8_t u8WinSize, uint8_t u8Threshold)
{
    /* Invalid Window size */
    if ((u8WinSize == 0) || (u8WinSize > 32))
    {
        return;
    }

    /* Invalid Window Threshold */
    if ((u8Threshold == 0) || (u8Threshold > 32))
    {
        return;
    }

    /* Configure Filter Window */
    switch (eTZEvent)
    {
        case EPWR_TRIP_EVENT_TZ0:
            EPWR->EPWRTZ0CTL.bit.FLTWIN = u8WinSize - 1;
            EPWR->EPWRTZ0CTL.bit.FLTTH  = u8Threshold - 1;
            break;

        case EPWR_TRIP_EVENT_TZ1:
            EPWR->EPWRTZ1CTL.bit.FLTWIN = u8WinSize - 1;
            EPWR->EPWRTZ1CTL.bit.FLTTH  = u8Threshold - 1;
            break;
    }
}




/******************************************************************************
 * @brief      Set Trip-zone filter window size and threshold (nero second)
 *
 * @param[in]  eTZEvent          : Select the trip-zone event
 *                                 Please go to EPWR_TripEventEnum for details
 * @param[in]  u32SizeTimeNS     : Window Size (Time in ns)
 * @param[in]  u32ThresholdTimeNS: Threshold (Time in ns)
 *
 * @return     none
 *
 *******************************************************************************/
void EPWR_SetFilterWindowTimeNs(EPWR_TripEventEnum eTZEvent, uint32_t u32SizeTimeNS, uint32_t u32ThresholdTimeNS)
{
    uint32_t u32TimeCnt1, u32TimeCnt2;
    uint32_t u32FilterClk;

    /* EPWR clock */
    u32FilterClk = SystemCoreClock;

    switch (eTZEvent)
    {
        case EPWR_TRIP_EVENT_TZ0:
            u32FilterClk /= (EPWR->EPWRTZ0CTL.bit.FLTDIV + 1);
            break;

        case EPWR_TRIP_EVENT_TZ1:
            u32FilterClk /= (EPWR->EPWRTZ1CTL.bit.FLTDIV + 1);
            break;
    }

    /* Window size */
    u32TimeCnt1 = (((u32SizeTimeNS) * (u32FilterClk / 100000)) / 10000);
    if (u32TimeCnt1 > 32)
    {
        u32TimeCnt1 = 32;
    }

    /* Threshold */
    u32TimeCnt2 = (((u32ThresholdTimeNS) * (u32FilterClk / 100000)) / 10000);
    /* Threshold value must be bigger than WindowSize/2 */
    if ((u32TimeCnt2 * 2) < u32TimeCnt1)
    {
        u32TimeCnt2 = u32TimeCnt1 / 2 + 1;
    }
    if (u32TimeCnt2 > 32)
    {
        u32TimeCnt2 = 32;
    }

    EPWR_SetFilterWindow(eTZEvent, u32TimeCnt1, u32TimeCnt2);
}


/******************* (C) COPYRIGHT 2021 SPINTROL LIMITED ***** END OF FILE ****/
