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

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR_Examples
@{
*/

/** @addtogroup TMR_DMA
  @{
*/

/** @defgroup TMR_DMA_Functions
  @{
*/

/* TMR1 Init */
void Tmr_Init(void);
void Gpio_Init(void);
void Dma_Init(const uint32_t Buf);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    const uint16_t ConfigBuf[3] = {800, 400, 200};

    Gpio_Init();
    Dma_Init((uint32_t)&ConfigBuf[0]);
    Tmr_Init();

    while (1)
    {
    }
}

/*!
 * @brief       GPIO Init
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Gpio_Init(void)
{
    Gpio_ConfigType Gpio_ConfigStruct;

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

    /* TMR1_CH1 & CH1N AF Configuration */
    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_CH1 */
    Gpio_ConfigStruct = g_tmrGpioConfig;
    Gpio_ConfigStruct.pin = (uint16_t)TMR1_CH1_GPIO_PIN;
    Gpio_Config(GPIOA, &Gpio_ConfigStruct);

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

/*!
 * @brief       DMA Init
 *
* @param       Buf : PWM duty cycle
 *
 * @retval      None
 *
 * @note
 */
void Dma_Init(const uint32_t Buf)
{
    /* Enable DMA clock */
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_DMA1);

    /* Set buffer address */
    g_dmaConfig.memoryAddress = Buf;
    g_dmaConfig.peripheralAddress = (uint32_t)&TMR1->CC1_R.CC1;

    /* Configure DMA channel */
    Dma_Config(DMA1_CHANNEL_2, &g_dmaConfig);
    Dma_Enable(DMA1_CHANNEL_2);
}

/*!
 * @brief       TMR Init
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Tmr_Init(void)
{
    /* Enable Timer clock */
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_TMR1);

    /* Configure TMR1 base */
    Tmr_ConfigTimeBase(TMR1, &g_tmrBaseConfig);

    /* Configure TMR1 channel 1 PWM mode */
    Tmr_OC1Config(TMR1, &g_tmrOcConfig);
    Tmr_OC1PreloadConfig(TMR1, TMR_OC_PRELOAD_ENABLE);
    Tmr_EnableAUTOReload(TMR1);

    /* Enable DMA request for channel 1 */
    Tmr_EnableDMASoure(TMR1, (uint16_t)TMR_DMA_CH1);

    /* Enable Timer and PWM outputs */
    Tmr_Enable(TMR1);
    Tmr1_EnablePWMOutputs();
}

/**@} end of group TMR_DMA_Functions */
/**@} end of group TMR_DMA */
/**@} end of group TMR_Examples */
/**@} end of group Examples */
