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

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR_Examples
@{
*/

/** @addtogroup TMR_PWMInput
  @{
*/

/** @defgroup TMR_PWMInput_Macros Macros
  @{
*/

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

/**@} end of group TMR_PWMInput_Macros*/

/** @defgroup TMR_PWMInput_Variables Variables
  @{
*/

volatile uint8_t g_irqFlag = 0;
volatile uint32_t g_overflowCNT = 0;
volatile uint32_t g_ic2Value = 0;
volatile uint32_t g_ic1Value = 0;
volatile uint32_t g_dutyCycle = 0;
volatile uint32_t g_frequency = 0;
volatile uint32_t g_hclk = 0;
volatile uint32_t g_highLev = 0;
volatile uint32_t g_lowLev = 0;
volatile uint32_t g_peroid = 0;

volatile uint32_t tick = 0;

/**@} end of group TMR_PWMInput_Variables */

/** @defgroup TMR_PWMInput_Functions Functions
  @{
*/

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 */
int main(void)
{
    uint32_t print_duty;
    uint32_t print_freq;
    uint32_t print_high;
    uint32_t print_low;
    uint32_t print_per;

    g_hclk = Rcm_ReadHclkFreq();

    /* Init LED */
    Board_LedInit(LED2);

    /* COM1 Init */
    Board_ComInit(COM1);

    /* Enable Periph Clock */
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_TMR1);
    Rcm_EnableApb1PeriphClock(RCM_APB1_PERIPH_TMR3);
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_GPIOA);

    /* TMR3 channel2 for PA7 */
    Gpio_Config(TMR3_CH2_GPIO_PORT, &g_tmr3_Ch2GpioConfig);
    Gpio_ConfigPinAF(TMR3_CH2_GPIO_PORT, TMR3_CH2_PIN_SOURCE, TMR3_CH2_GPIO_AF);

    /* TMR3 PWM Input mode */
    Tmr_PWMConfig(TMR3, &g_tmr3_PWMInputConfig);

    /* TMR3 Slave Mode */
    Tmr_SelectInputTrigger(TMR3, TMR_TS_TI2FP2);
    Tmr_SelectSlaveMode(TMR3, TMR_SLAVEMODE_RESET);
    Tmr_EnableMasterSlaveMode(TMR3);

    /* Enable TMR3 Interrupt */
    Tmr_Enable(TMR3);
    Nvic_EnableIrqRequest(TMR3_IRQn, 0);
    Tmr_EnableInterrupt(TMR3, (uint16_t)TMR_INT_CH1);
    Tmr_EnableInterrupt(TMR3, (uint16_t)TMR_INT_CH2);
    Tmr_EnableInterrupt(TMR3, (uint16_t)TMR_INT_UPDATE);

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

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

    while (1)
    {
        if (g_irqFlag > 1u)
        {
            print_duty = g_dutyCycle;
            print_freq = g_frequency;
            print_high = g_highLev;
            print_low  = g_lowLev;
            print_per  = g_peroid;

            printf("g_dutyCycle: %d %%, g_frequency: %d Hz\r\n", print_duty, print_freq);
            printf("g_highLev: %u us, g_lowLev: %u us, g_peroid: %u us\r\n", print_high, print_low, print_per);
            Board_LedToggle(LED2);
            g_irqFlag = 0;
            Tmr_Delay();
        }
        else
        {
            /* nothing */
        }
    }
}

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

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

int fputc(int ch, FILE* f)
{
    Usart_TxData(DEBUG_USART, (uint8_t)ch);
    while (Usart_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == (uint8_t)RESET)
    {
        /* nothing */
    }
    return (ch);
}

#elif defined (__GNUC__)

int __io_putchar(int ch)
{
    Usart_TxData(DEBUG_USART, ch);
    while (Usart_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == (uint8_t)RESET)
    {
        /* nothing */
    }
    return ch;
}

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