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

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR_Examples
@{
*/

/** @addtogroup TMR_6Steps
  @{
*/

/** @defgroup TMR_6Steps_Variables Variables
  @{
*/
/* The count of microseconds */
extern volatile uint32_t g_cntUs;
/* The count of milliseconds */
extern volatile uint32_t g_cntMs;
/**@} end of group TMR_6Steps_Variables */

/** @defgroup TMR_6Steps_Functions Functions
  @{
*/

void Tmr_DelayInit(void);
void Tmr_DelayMs(__IO uint32_t Ms);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 */
int main(void)
{
    Gpio_ConfigType Gpio_ConfigStruct;

    Tmr_DelayInit();

    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_TMR1); 
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_GPIOB);
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_GPIOC);

    /* TMR1 CH1 */
    Gpio_ConfigPinAF(TMR1_CH1_PORT, TMR1_CH1_PIN_SRC, TMR1_CH1_AF);
    /* TMR1 CH2 */
    Gpio_ConfigPinAF(TMR1_CH2_PORT, TMR1_CH2_PIN_SRC, TMR1_CH2_AF);
    /* TMR1 CH3 */
    Gpio_ConfigPinAF(TMR1_CH3_PORT, TMR1_CH3_PIN_SRC, TMR1_CH3_AF);
    /* TMR1 BKIN */
    Gpio_ConfigPinAF(TMR1_BKIN_PORT, TMR1_BKIN_PIN_SRC, TMR1_BKIN_AF);
    /* TMR1 CH1N */
    Gpio_ConfigPinAF(TMR1_CH1N_PORT, TMR1_CH1N_PIN_SRC, TMR1_CH1N_AF);
    /* TMR1 CH2N */
    Gpio_ConfigPinAF(TMR1_CH2N_PORT, TMR1_CH2N_PIN_SRC, TMR1_CH2N_AF);
    /* TMR1 CH3N */
    Gpio_ConfigPinAF(TMR1_CH3N_PORT, TMR1_CH3N_PIN_SRC, TMR1_CH3N_AF);

    /* Config TMR1 GPIO for complementary output PWM */
    Gpio_ConfigStruct.pin     = (uint16_t)TMR1_GPIOB_PINS;
    Gpio_ConfigStruct.mode    = GPIO_MODE_AF;
    Gpio_ConfigStruct.outtype = GPIO_OUT_TYPE_PP;
    Gpio_ConfigStruct.speed   = GPIO_SPEED_50MHz;
    Gpio_Config(GPIOB, &Gpio_ConfigStruct);

    Gpio_ConfigStruct.pin = TMR1_GPIOC_PINS;
    Gpio_Config(GPIOC, &Gpio_ConfigStruct);

    /* config TMR1 */
    Tmr_ConfigTimeBase(TMR1, &g_tmrBaseCfg);

    /* Configure channel */
    Tmr_OC1Config(TMR1, &g_tmrOc1Cfg);
    Tmr_OC2Config(TMR1, &g_tmrOc2Cfg);
    Tmr_OC3Config(TMR1, &g_tmrOc3Cfg);

    /* Configures the Break feature, dead time, Lock level, the IMOS */
    Tmr1_ConfigBDT(&g_tmrBdtCfg);

    /* Timer1 Commutation Interrupt */
    Tmr_EnableInterrupt(TMR1, (uint16_t)TMR_INT_CCU);
    NVIC_EnableIRQ(TMR1_BRK_UP_TRG_COM_IRQn);

    /* Enable Capture Compare Preload Control */
    Tmr1_EnableCCPreload();

    Tmr1_EnablePWMOutputs();
    Tmr_Enable(TMR1);

    while (1)
    {
        /* Generate the TMR1 Commutation event by software every 1 second */
        Tmr_DelayMs(100);
        Tmr_GenerateEvent(TMR1, (uint16_t)TMR_EVENT_CCU);
    }
}

/*!
 * @brief       Update SystemCoreClock variable according to Clock Register Values
 *              The SystemCoreClock variable contains the core clock (HCLK)
 *
 * @param       None
 *
 * @retval      None
 *
 */
void Tmr_DelayInit(void)
{
    SystemCoreClockUpdate();
}
/*!
 * @brief       Configures Tmr_Delay ms.
 *
 * @param       nms: Specifies the delay to be configured.
 *              This parameter can be one of following parameters:
 *              @arg nms
 *
 *
 * @retval      None
 */
void Tmr_DelayMs(__IO uint32_t Ms)
{
    SysTick_Config(SystemCoreClock / (uint32_t)1000u);

    g_cntMs = Ms;
    while (g_cntMs != (uint32_t)0)
    {
        /* nothing */
    }
}


/**@} end of group TMR_6Steps_Functions */
/**@} end of group TMR_6Steps */
/**@} end of group TMR_Examples */
/**@} end of group Examples */
