/*!
 * @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 "stdio.h"
#include "Board.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR_Examples
@{
*/

/** @addtogroup TMR_EncoderInterface
  @{
*/

/** @addtogroup TMR_EncoderInterface_Macros Macros
  @{
*/

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

/**@} end of group TMR_EncoderInterface_Macros*/

/** @defgroup TMR_EncoderInterface_Enumerations Enumerations
  @{
*/

/**@} end of group TMR_EncoderInterface_Enumerations */

/** @defgroup TMR_EncoderInterface_Structures Structures
  @{
*/

/**@} end of group TMR_EncoderInterface_Structures */

/** @defgroup TMR_EncoderInterface_Variables Variables
  @{
*/

uint16_t g_encoderNum = 0;
volatile uint32_t g_tick = 0;

/**@} end of group TMR_EncoderInterface_Variables */

/** @defgroup TMR_EncoderInterface_Functions Functions
  @{
*/

void Tmr_Delay(void);

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

    Board_ComInit(COM1);

    /* Enable GPIOA Periph Clock */
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_GPIOC);

    /* Config GPIO */
    Gpio_Config(GPIOC, &g_tmrEncGpioConfig);
    Gpio_ConfigPinAF(TMR1_ENC_CH1_PORT, TMR1_ENC_CH1_SOURCE, TMR1_ENC_CH1_AF);
    Gpio_ConfigPinAF(TMR1_ENC_CH2_PORT, TMR1_ENC_CH2_SOURCE, TMR1_ENC_CH2_AF);

    /* Enable TMR1,TMR3 Periph Clock */
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_TMR1);
    Rcm_EnableApb1PeriphClock(RCM_APB1_PERIPH_TMR3);

    /* Config TMR1 */
    Tmr_ConfigTimeBase(TMR1, &g_tmr1BaseConfig);

    /* Config TMR3 */
    Tmr_ConfigTimeBase(TMR3, &g_tmr3BaseConfig);

    /* Encoder */
    Tmr_ConfigEncodeInterface(TMR1, TMR_ENCODER_MODE_TI1, TMR_IC_POLARITY_RISING, TMR_IC_POLARITY_RISING);

    /* Input Capture */
    g_tmrEncIcConfig.channel = TMR_CHANNEL_1;
    Tmr_ICConfig(TMR1, &g_tmrEncIcConfig);

    g_tmrEncIcConfig.channel = TMR_CHANNEL_2;
    Tmr_ICConfig(TMR1, &g_tmrEncIcConfig);

    /* Counter */
    Tmr_SetCounter(TMR1, 0x0004);
    Tmr_ClearStatusFlag(TMR1, (uint16_t)TMR_FLAG_UPDATE);
    Tmr_ClearStatusFlag(TMR3, (uint16_t)TMR_FLAG_UPDATE);

    /* Enable TMR1,TMR3 Interrupt */
    Tmr_EnableInterrupt(TMR1, (uint16_t)TMR_INT_UPDATE);
    Tmr_EnableInterrupt(TMR3, (uint16_t)TMR_INT_UPDATE);
    Nvic_EnableIrqRequest(TMR1_BRK_UP_TRG_COM_IRQn, 0);
    Nvic_EnableIrqRequest(TMR3_IRQn, 0);

    /* Enable TMR1,TMR3 */
    Tmr_Enable(TMR1);
    Tmr_Enable(TMR3);

    while (1)
    {
        Board_LedToggle(LED2);
        Tmr_Delay();
    }
}

/*!
 * @brief       Tmr_Delay
 *
 * @param       None
 *
 * @retval      None
 */
void Tmr_Delay(void)
{
    g_tick = 0;
    while (g_tick < (uint32_t)500)
    {
    }
}

#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) == (uint8_t)RESET);

    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_EncoderInterface_Functions */
/**@} end of group TMR_EncoderInterface */
/**@} end of group TMR_Examples */
/**@} end of group Examples */
