/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.2
 *
 * @date        2023-03-31
 *
 * @attention
 *
 *  Copyright (C) 2023 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 TMR1_InputCapture
  @{
*/

/** @defgroup TMR1_InputCapture_Variables Variables
  @{
*/

uint32_t freqLIRC = 0;
uint32_t icValue[2];
uint32_t icOffset = 0;
uint32_t clockFreqTMR1 = 0;
volatile uint32_t overflowCNT = 0;

/**@} end of group TMR1_InputCapture_Variables */

/** @defgroup TMR1_InputCapture_Functions Functions
  @{
*/

/* TMR1 Init */
void TMR1_Init(void);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 */
int main(void)
{
    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDOff(LED2);

    APM_DelayInit(APM_DELAY_MS);

    TMR1_Init();

    /* TMR1 clock = Master clock  */
    clockFreqTMR1 = RCM_GetMasterClockFreq();

    while(1)
    {
        /* if LIRC clock frequency in [1k ~ 150k]hz  */
        if((freqLIRC < 150000) && (freqLIRC > 1000))
        {
            APM_MINI_LEDOn(LED2);
        }
        else
        {
            APM_MINI_LEDToggle(LED2);
        }
        APM_Delay(500);
    }
}

/*!
 * @brief       TMR1 Init
 *
 * @param       None
 *
 * @retval      None
 */
void TMR1_Init(void)
{
    TMR1_ICConfig_T icConfig;
    GPIO_Config_T gpioConfig;

    /* GPIO configuration  */
    gpioConfig.mode = GPIO_MODE_IN_FLOATING;
    gpioConfig.pin = GPIO_PIN_3;
    gpioConfig.speed = GPIO_SPEED_10MHz;
    GPIO_Config(GPIOC, &gpioConfig);

    /* Set Input Capture Channel */
    icConfig.channel = TMR1_CHANNEL_3;
    /* Set Input Capture filter */
    icConfig.filter = 0;
    /* Set Input Capture divider */
    icConfig.div = TMR1_IC_DIV_1;
    /* Set Input Capture polarity */
    icConfig.polarity = TMR1_IC_POLARITY_FALLING;
    /* Set Input Capture selection */
    icConfig.selection = TMR1_IC_SELECT_DIRECTTI;
    TMR1_ConfigInputCapture(TMR1, &icConfig);

    TMR1_EnableInterrupt(TMR1, TMR1_INT_UPDATE | TMR1_INT_CH3CC);
    NVIC_EnableIRQRequest(TMR1_UT_IRQn, 0x0f);
    NVIC_EnableIRQRequest(TMR1_CC_IRQn, 0x0f);

    /* Enable TMR1  */
    TMR1_Enable(TMR1);
}

/*!
 * @brief       TMR1 capturer campare interrupt server routine
 *
 * @param       None
 *
 * @retval      None
 */
void TMR1_CC_Isr(void)
{
    if(TMR1_ReadStatusFlag(TMR1,TMR1_FLAG_CH3CC) == SET)
    {
        /* Read capture value */
        icValue[icOffset] = TMR1_ReadCompareCapture(TMR1, TMR1_CHANNEL_3);
        if(icOffset == 0)
        {
            overflowCNT = 0;
            icOffset++;
            TMR1_ClearStatusFlag(TMR1, TMR1_FLAG_UPDATE);
        }
        else
        {
            /* calculate the LIRC clock frequency */
            freqLIRC = (clockFreqTMR1) / (icValue[1] + overflowCNT * 65536 - icValue[0]);
            icOffset = 0;
        }
        /* Clear Channel3 Input Capture flag  */
        TMR1_ClearStatusFlag(TMR1, TMR1_FLAG_CH3CC);
    }
}

/*!
 * @brief       TMR1 Update interrupt server routine
 *
 * @param       None
 *
 * @retval      None
 */
void TMR1_UT_Isr(void)
{
    if(TMR1_ReadStatusFlag(TMR1, TMR1_FLAG_UPDATE) == SET)
    {
        overflowCNT++;
        TMR1_ClearStatusFlag(TMR1, TMR1_FLAG_UPDATE);
    }
}

/**@} end of group TMR1_InputCapture_Functions */
/**@} end of group TMR1_InputCapture */
/**@} end of group Examples */
