/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @date        2026-02-25
 *
 * @attention
 *
 *  Copyright (C) 2026 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
 *
 *  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 Semiconductor Software License Agreement for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "user_tmr_config.h"
#include "Board.h"
#include "stdio.h"
#include "inttypes.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR_Examples
  @{
*/

/** @addtogroup TMR_InputCapture
  @{
*/

/** @defgroup TMR_InputCapture_Macros Macros
  @{
*/

/* printf function configs to USART1 */
#define DEBUG_USART  USART1

/**@} end of group TMR_InputCapture_Macros */

/** @defgroup TMR_InputCapture_Functions Functions
  @{
  */

/* TMR1 Input capture Init */
void Tmr1_InputCaptureInit(void);
/* COC Init */
void Tmr_ClockOutputInit(void);
/* Tmr_Delay */
static void Delays(void);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    /* input capture Value*/
    uint16_t icValue[2];
    /* TMR1 clock Frequent*/
    uint32_t clockFreqTMR1;
    /* LSI frequent*/
    uint32_t freqLSI;

    Board_LedInit(LED2);
    Board_LedInit(LED3);
    Board_ComInit(COM1);

    Tmr1_InputCaptureInit();
    Tmr_ClockOutputInit();
    /*  TMR1 clock = Master clock  */
    clockFreqTMR1 = Rcm_ReadSysClkFreq();

    /* Turn LED2 on */
    Board_LedOn(LED2);
    /* Turn LED3 off */
    Board_LedOff(LED3);

    /*  Clear Channel3 Input Capture flag  */
    Tmr_ClearStatusFlag(TMR1, (uint16_t)TMR_FLAG_CH3);
    /*  Wait end of capture */
    while (Tmr_ReadStatusFlag(TMR1, TMR_FLAG_CH3) == (uint16_t)RESET)
    {
        /* nothing */
    }
    /*  Read capture value */
    icValue[0] = Tmr_ReadCaputer3(TMR1);

    Tmr_ClearStatusFlag(TMR1, (uint16_t)TMR_FLAG_CH3);
    while (Tmr_ReadStatusFlag(TMR1, TMR_FLAG_CH3) == (uint16_t)RESET)
    {
        /* nothing */
    }
    icValue[1] = Tmr_ReadCaputer3(TMR1);

    /*  calculate the LSI clock frequency  */
    freqLSI = (uint32_t)(clockFreqTMR1) / ((uint32_t)icValue[1] - (uint32_t)icValue[0]);

    printf("freq : %" PRId32 "\r\n", freqLSI);
    /*  if LSI clock frequency in [39~41]Khz  */
    if ((freqLSI < (uint32_t)45000) && (freqLSI > (uint32_t)39000))
    {
        Board_LedOff(LED2);
    }
    else
    {
        /* nothing */
    }

    for (;;)
    {
        Delays();

        Board_LedToggle(LED3);
    }
}

/*!
 * @brief       TMR1 Input_Capture Init
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Tmr1_InputCaptureInit(void)
{
    /* Enable Clock*/
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_GPIOA);
    /* Enable Clock*/
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_GPIOC);
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_SYSCFG);
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_TMR1);

    /*  Connect TMR1 to CH3 */
    Gpio_ConfigPinAF(TMR1_CH3_GPIO_PORT, TMR1_CH3_PIN_SOURCE, TMR1_CH3_AF);
    Gpio_Config(TMR1_CH3_GPIO_PORT, &g_tmr1_ch3_gpio_config);

    Tmr_ConfigTimeBase(TMR1, &g_tmr1_timebase_config);

    /* Connect InputCapture channel to TMR1_CHANNEL_3*/
    Tmr_ICConfig(TMR1, &g_tmr1_ic3_config);

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

/*!
 * @brief       COC Init
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Tmr_ClockOutputInit(void)
{
    /*  Connect RCM Clock output */
    Gpio_ConfigPinAF(COC_GPIO_PORT, COC_PIN_SOURCE, COC_AF);
    Gpio_Config(COC_GPIO_PORT, &g_coc_gpio_config);

    /*  LSI clock as clock output  */
    Rcm_ConfigCoc(RCM_COC_LSI);

    /*  Enable LSI clock*/
    Rcm_EnableLsi();
}
/*!
 * @brief       Tmr_Delay
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
static void Delays(void)
{
    volatile uint32_t delay = 0xffff5;

    while ((uint32_t)0u != delay)
    {
        delay = delay - (uint32_t)1u;
    };
}

#if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @param       *f:  pointer to a FILE that can recording all information
*              needed to control a stream
*
* @retval      The characters that need to be send.
*
* @note
*/
int fputc(int ch, FILE* f)
{
    /* send a byte of data to the serial port */
    Usart_TxData(DEBUG_USART, (uint8_t)ch);

    /* wait for the data to be send  */
    while (Usart_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == (uint8_t)RESET)
    {
        /* nothing */
    }

    return (ch);
}

#elif defined (__GNUC__)

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @retval      The characters that need to be send.
*
* @note
*/
int __io_putchar(int ch)
{
    /* send a byte of data to the serial port */
    Usart_TxData(DEBUG_USART, ch);

    /* wait for the data to be send  */
    while (Usart_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET)
    {
        /* nothing */
    }

    return ch;
}

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       file:  Meaningless in this function.
*
* @param       *ptr:  Buffer pointer for data to be sent.
*
* @param       len:  Length of data to be sent.
*
* @retval      The characters that need to be send.
*
* @note
*/
int _write(int file, char* ptr, int len)
{
    int i;
    for (i = 0; i < len; i++)
    {
        __io_putchar(*ptr++);
    }

    return len;
}

#else
#pragma message("Not supported compiler type")
#endif

/**@} end of group TMR_InputCapture_Functions */
/**@} end of group TMR_InputCapture */
/**@} end of group TMR_Examples */
/**@} end of group Examples */
