/*!
 * @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 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 in an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTIES 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 "Board.h"
#include "stdio.h"
#include "user_tmr_config.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR_Examples
@{
*/

/** @addtogroup TMR_ComplementaryOutput
  @{
  */

/** @defgroup TMR_ComplementaryOutput_Macros Macros
  @{
*/

/**@} end of group TMR_ComplementaryOutput_Macros */

/** @defgroup TMR_ComplementaryOutput_Enumerations Enumerations
  @{
*/

/**@} end of group TMR_ComplementaryOutput_Enumerations */

/** @defgroup TMR_ComplementaryOutput_Structures Structures
  @{
*/

/**@} end of group TMR_ComplementaryOutput_Structures */

/** @defgroup TMR_ComplementaryOutput_Variables Variables
  @{
*/

/**@} end of group TMR_ComplementaryOutput_Variables */

/** @defgroup TMR_ComplementaryOutput_Functions Functions
  @{
  */

/* TMR1 Init */
void Tmr1_Init(void);
/* Tmr_Delay */
void Tmr_Delay(void);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    Board_LedInit(LED2);
    Board_LedInit(LED3);
    Tmr1_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       TMR1 Init
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Tmr1_Init(void)
{
    Gpio_ConfigType   gpioconfig;

    /* Enable Clock*/
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_GPIOA);
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_SYSCFG);
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_TMR1);

    /* Configure GPIO AF for TMR1 CH1 and CH1N */
    Gpio_ConfigPinAF(TMR1_CH1_GPIO_PORT, TMR1_CH1_PIN_SOURCE, TMR1_CH1_GPIO_AF);
    Gpio_ConfigPinAF(TMR1_CH1N_GPIO_PORT, TMR1_CH1N_PIN_SOURCE, TMR1_CH1N_GPIO_AF);

    /* Configure GPIO for TMR1_CH1N */
    gpioconfig = g_tmr1GpioConfig;
    gpioconfig.pin = (uint16_t)TMR1_CH1N_GPIO_PIN;
    Gpio_Config(GPIOA, &gpioconfig);

    /* Configure GPIO for TMR1_CH1 */
    gpioconfig.pin = (uint16_t)TMR1_CH1_GPIO_PIN;
    Gpio_Config(GPIOA, &gpioconfig);

    /* Configure TMR1 base timer */
    Tmr_ConfigTimeBase(TMR1, &g_tmr1BaseConfig);

    /* Configure TMR1 channel 1 for complementary PWM output */
    Tmr_OC1Config(TMR1, &g_tmr1OcConfig);

    /* Configure TMR1 break & dead-time function */
    Tmr1_ConfigBDT(&g_tmr1BdtConfig);

    /* Enable TMR1 PWM outputs */
    Tmr1_EnablePWMOutputs();

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

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

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

/**@} end of group TMR_ComplementaryOutput_Functions */
/**@} end of group TMR_ComplementaryOutput */
/**@} end of group TMR_Examples */
/**@} end of group Examples */
