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

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR_Examples
@{
*/

/** @addtogroup TMR_DMABurst
  @{
  */

/** @defgroup TMR_DMABurst_Functions Functions
  @{
  */

/* DMA Init */
void Tmr_DmaInit(const uint32_t Buf);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 */
int main(void)
{
    const uint16_t ConfigBuf[6] = {999, 0, 800, 700, 600, 500};
    Gpio_ConfigType gpioConfigStruct;

    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_TMR1);
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_GPIOA);
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_GPIOB);

    /* TMR1_CH1 */
    Gpio_ConfigPinAF(TMR1_CH1_GPIO_PORT, TMR1_CH1_PIN_SOURCE, TMR1_CH1_GPIO_AF);
    /* TMR1_CH2 */
    Gpio_ConfigPinAF(TMR1_CH2_GPIO_PORT, TMR1_CH2_PIN_SOURCE, TMR1_CH2_GPIO_AF);
    /* TMR1_CH3 */
    Gpio_ConfigPinAF(TMR1_CH3_GPIO_PORT, TMR1_CH3_PIN_SOURCE, TMR1_CH3_GPIO_AF);
    /* TMR1_CH1N */
    Gpio_ConfigPinAF(TMR1_CH1N_GPIO_PORT, TMR1_CH1N_PIN_SOURCE, TMR1_CH1N_GPIO_AF);
    /* TMR1_CH2N */
    Gpio_ConfigPinAF(TMR1_CH2N_GPIO_PORT, TMR1_CH2N_PIN_SOURCE, TMR1_CH2N_GPIO_AF);
    /* TMR1_CH3N */
    Gpio_ConfigPinAF(TMR1_CH3N_GPIO_PORT, TMR1_CH3N_PIN_SOURCE, TMR1_CH3N_GPIO_AF);
    /* TMR1_CH4 */
    Gpio_ConfigPinAF(TMR1_CH4_GPIO_PORT, TMR1_CH4_PIN_SOURCE, TMR1_CH4_GPIO_AF);

    /* Config TMR1 GPIO for complementary output PWM */
    gpioConfigStruct = g_tmrGpioConfig;
    gpioConfigStruct.pin = (uint16_t)TMR1_GPIOA_PIN_LIST;
    Gpio_Config(GPIOA, &gpioConfigStruct);

    gpioConfigStruct.pin = TMR1_GPIOB_PIN_LIST;
    Gpio_Config(GPIOB, &gpioConfigStruct);

    Tmr_DmaInit((uint32_t)&ConfigBuf[0]);

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

    /* Configure channel */
    Tmr_OC1Config(TMR1, &g_tmrOcConfig);
    Tmr_OC2Config(TMR1, &g_tmrOcConfig);
    Tmr_OC3Config(TMR1, &g_tmrOcConfig);
    Tmr_OC4Config(TMR1, &g_tmrOcConfig);

    /* Configures the TMR1 DMA interface */
    Tmr_ConfigDMA(TMR1, TMR_DMABASE_AUTORLD, TMR_DMA_BURSTLENGHT_6TRANSFERS);
    Tmr_EnableDMASoure(TMR1, (uint16_t)TMR_DMA_UPDATE);

    Tmr_Enable(TMR1);

    /* Enable TMRx PWM output */
    Tmr1_EnablePWMOutputs();

    while (1)
    {
    }
}

/*!
 * @brief       DMA Init
 *
 * @param       None
 *
 * @retval      None
 */
void Tmr_DmaInit(const uint32_t Buf)
{
    /* DMA Configure */
    Dma_ConfigType dmaConfig;

    /* Enable DMA clock */
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_DMA1);

    dmaConfig = g_dmaConfig;
    dmaConfig.memoryAddress = Buf;
    dmaConfig.peripheralAddress = (uint32_t)&TMR1->DMADDR_R.DMADDR;

    Dma_Config(DMA1_CHANNEL_5, &dmaConfig);

    Dma_Enable(DMA1_CHANNEL_5);
}

/**@} end of group TMR_DMABurst_Functions */
/**@} end of group TMR_DMABurst */
/**@} end of group TMR_Examples */
/**@} end of group Examples */
