/*!
 * @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"

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR_Examples
@{
*/

/** @addtogroup TMR_SinglePulse
  @{
*/

/** @defgroup TMR_SinglePulse_Functions Functions
  @{
*/

/* TMR3 Init */
void Tmr3_Init(void);
/* Tmr_Delay */
void Tmr_Delay(void);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    Board_ButtonInit(BUTTON_KEY1, BUTTON_MODE_EINT);
    Board_LedInit(LED2);
    Board_LedInit(LED3);
    Tmr3_Init();

    /* Turn LED2 on */
    Gpio_ClearBit(LED2_GPIO_PORT, (uint16_t)LED2_PIN);
    /* Turn LED3 off */
    Gpio_SetBit(LED3_GPIO_PORT, (uint16_t)LED3_PIN);

    for (;;)
    {
        Tmr_Delay();
        Board_LedToggle(LED2);
        Board_LedToggle(LED3);
    }
}

/*!
 * @brief       TMR3 Init
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Tmr3_Init(void)
{
    /* Enable Clock */
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_GPIOA);
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_SYSCFG);
    Rcm_EnableApb1PeriphClock(RCM_APB1_PERIPH_TMR3);

    /* Connect TMR3 to CH1 */
    Gpio_ConfigPinAF(TMR3_CH1_GPIO_PORT, TMR3_CH1_PIN_SOURCE, TMR3_CH1_GPIO_AF);
    Gpio_Config(TMR3_CH1_GPIO_PORT, &g_tmr3_Ch1GpioConfig);

    /* Config TimeBase */
    Tmr_ConfigTimeBase(TMR3, &g_tmr3_BaseConfig);

    /* Config PWM Output */
    Tmr_OC1Config(TMR3, &g_tmr3_OC1PWMConfig);
    
    /* Set One Pulse Mode */
    Tmr_SelectOnePulseMode(TMR3, TMR_OPMODE_SINGLE);

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

/*!
 * @brief       Tmr_Delay
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Tmr_Delay(void)
{
    volatile uint32_t delay = 0xffff5;

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

/*!
 * @brief       The interrupt will happen when the button is press ,
 *              and TMR3 will generate one pulse.
 * @param
 *
 * @retval
 *
 * @note        This function should put into EINT0_1_IRQHandler() in g32a10xx_int.c
 */
void  Tmr_ButKeyOnePluseIsr(void)
{
    if (Eint_ReadStatusFlag((uint32_t)EINT_LINE0) == (uint8_t)SET)
    {
        /* The interrupt flag bit must be cleared first. */
        Eint_ClearStatusFlag((uint32_t)EINT_LINE0);
        Tmr_Enable(TMR3);
    }
    else
    {
        /* nothing */
    }
}

/**@} end of group TMR_SinglePulse_Functions */
/**@} end of group TMR_SinglePulse */
/**@} end of group TMR_Examples */
/**@} end of group Examples */
