/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.3
 *
 * @date        2025-05-31
 *
 * @attention
 *
 *  Copyright (C) 2023-2025 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "main.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup IWDT
  @{
*/

/** @defgroup IWDT_Functions Functions
  @{
*/

/* Key process */
BOOL KeyPressed(void);
/* IWDT init */
void IWDT_Init(uint32_t freqHIRC);
/* LIRC frequency Measurment */
uint32_t LIRC_Measurment(void);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 */
int main(void)
{
    uint32_t freqLIRC;

    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDInit(LED3);
    APM_MINI_PBInit(BUTTON_KEY1, BUTTON_MODE_EINT);
    APM_DelayInit(APM_DELAY_MS);

    APM_MINI_LEDOff(LED2);
    APM_MINI_LEDOff(LED3);

    /* Process when reset occured */
    if(RCM_ReadResetFlag(RCM_RTS_FLAG_IWDTRST) == SET)
    {
        APM_MINI_LEDToggle(LED3);
        RCM_ClearResetFlag(RCM_RTS_FLAG_IWDTRST);
    }

    freqLIRC = LIRC_Measurment();
    IWDT_Init(freqLIRC);

    while(1)
    {
        APM_Delay(300);
        APM_MINI_LEDToggle(LED2);
        IWDT_ReloadCounter();
    }
}

/*!
 * @brief       IWDT init. Set IWDT timeout = 0.5s
 *
 * @param       freqHIRC:    LIRC frequency in HZ
 *
 * @retval      None
 */
void IWDT_Init(uint32_t freqLIRC)
{
    uint32_t iwdtReloadValue = 0;
    IWDT_Enable();

    IWDT_EnableWriteAccess();
    /*
     * IWDT timeout = 0.5S = (Counter Reload Value * DIV) / freqLIRC;
     * Counter Reload Value = (freqLIRC * 0.5) / DIV = freqLIRC / (2 * DIV)
     */
    iwdtReloadValue = freqLIRC / 512;
    /* limit iwdt reload value between 0 to 255 */
    if (iwdtReloadValue > 255)
    {
        iwdtReloadValue = 255;
    }
    IWDT_SetDivider(IWDT_DIV_256);
    IWDT_SetReloadCounter(iwdtReloadValue);

    IWDT_ReloadCounter();
}

/*!
 * @brief       Measure the LIRC frequency
 *
 * @param       None
 *
 * @retval      LIRC frequency in HZ
 */
uint32_t LIRC_Measurment(void)
{
    uint16_t icValue[2];
    uint32_t freqLIRC;
    uint32_t freqMasterClk;
    TMR1_ICConfig_T icConfig;

    freqMasterClk = RCM_GetMasterClockFreq();

    WUPT_EnableLIRCMeasurement();

    icConfig.channel = TMR1_CHANNEL_1;
    icConfig.div = TMR1_IC_DIV_8;
    icConfig.filter = 0;
    icConfig.polarity = TMR1_IC_POLARITY_FALLING;
    icConfig.selection = TMR1_IC_SELECT_DIRECTTI;
    TMR1_ConfigInputCapture(TMR1, &icConfig);
    TMR1_Enable(TMR1);

    while(TMR1_ReadStatusFlag(TMR1, TMR1_FLAG_CH1CC) == RESET);
    icValue[0] = TMR1_ReadCompareCapture(TMR1, TMR1_CHANNEL_1);
    TMR1_ClearStatusFlag(TMR1, TMR1_FLAG_CH1CC);

    while(TMR1_ReadStatusFlag(TMR1, TMR1_FLAG_CH1CC) == RESET);
    icValue[1] = TMR1_ReadCompareCapture(TMR1, TMR1_CHANNEL_1);
    TMR1_ClearStatusFlag(TMR1, TMR1_FLAG_CH1CC);

    TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_1);
    TMR1_Disable(TMR1);

    freqLIRC = (8 * freqMasterClk) / (icValue[1] - icValue[0]);

    WUPT_DisableLIRCMeasurement();

    return freqLIRC;
}

/**@} end of group IWDT_Functions */
/**@} end of group IWDT */
/**@} end of group Examples */
