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

/* include */
#include "Board.h"
#include "stdio.h"
#include "g32a10xx_gpio.h"
#include "g32a10xx_misc.h"
#include "g32a10xx_eint.h"
#include "g32a10xx_dma.h"
#include "g32a10xx_syscfg.h"
#include "user_dma_config.h"
#include "g32a10xx_int.h"

/** @addtogroup Examples
  @{
  */

/** @addtogroup DMA_Examples
  @{
  */

/** @addtogroup DMA_Usart
  @{
  */

/** @defgroup DMA_Usart_Functions Functions
  @{
  */

/* USART Init */
static void Usart1_Init(void);
static void Usart2_Init(void);
/* DMA_USART_Rx Init */
static void Dma_Rx_Init(void);
/* compare Buffer */
static BOOL BufferCompare(const uint8_t* Buf1Ptr, const uint8_t* Buf2Ptr, uint8_t Size);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    uint32_t timeout = 0xFFFFU;
    uint32_t i = 0U;
	  
    Board_LedInit(LED2);
    Board_LedInit(LED3);
    Board_LedOff(LED2);
    Board_LedOff(LED3);

    Usart1_Init();
    Usart2_Init();

    /* Init TxBuf*/
    for (i = 0U; i < BUFF_SIZE; i++)
    {
        g_dmaUsartTxBuf[i] = (uint8_t)i;
    }

    /* Config channel5 of DMA */
    Dma_Disable(DMA1_CHANNEL_USART1_RX);

    Dma_Rx_Init();

    Usart_EnableDma(USART1, (uint32_t)USART_DMA_REQUEST_RX);

    /* data from usart2 send to usart1 */
    for (i = 0U; i < BUFF_SIZE; i++)
    {
        /* USART send data*/
        while (Usart_ReadStatusFlag(USART2, USART_FLAG_TXBE) == (uint8_t)RESET)
        {
            /* do nothing */
        }

        Usart_TxData(USART2, g_dmaUsartTxBuf[i]);
    }

    while ((Dma_ReadDataNumber(DMA1_CHANNEL_USART1_RX) > 0U)  && (timeout > 0U))
    {
        timeout -= 1U;
    }

    /* Data is ok then turn off LED2 */
    if (BufferCompare(g_dmaUsartTxBuf, g_dmaUsartRxBuf, BUFF_SIZE) == TRUE)
    {
        Board_LedOn(LED2);
    }
    else
    {
        Board_LedOn(LED3);
    }

    while(1)
    {
        /* do nothing */
    }
}

/*!
 * @brief       USART Configuration
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
static void Usart1_Init(void)
{
    Board_ComInit(COM1);

    /* Enable SYSCFG Clock for Remaping DMA Channel*/
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_SYSCFG);
    /* Remap USART1_RX Channel to DMA channel 5*/
    Syscfg_EnableDMAChannelRemap((uint32_t)SYSCFG_DAM_REMAP_USART1RX);
}

/*!
 * @brief       USART2 Configuration
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
static void Usart2_Init(void)
{
    Gpio_ConfigType gpioConfig;
    /* Enable GPIO clock */
    Rcm_EnableAhbPeriphClock(USASRT2_TX_GPIO_CLK);
    Rcm_EnableAhbPeriphClock(USASRT2_RX_GPIO_CLK);

    /* Enable USART2 clock */
    Rcm_EnableApb1PeriphClock(RCM_APB1_PERIPH_USART2);

    /* Connect PXx to USARTx_Tx */
    Gpio_ConfigPinAF(USASRT2_TX_GPIO_PORT, USASRT2_TX_PIN_SOURCE, USASRT2_TX_AF);

    /* Connect PXx to USARTx_Rx */
    Gpio_ConfigPinAF(USASRT2_RX_GPIO_PORT, USASRT2_RX_PIN_SOURCE, USASRT2_RX_AF);

    /* Configure USART Tx as alternate function push-pull */
    gpioConfig.mode = GPIO_MODE_AF;
    gpioConfig.pin = (uint16_t)USASRT2_TX_PIN;
    gpioConfig.speed = GPIO_SPEED_50MHz;
    gpioConfig.outtype = GPIO_OUT_TYPE_PP;
    gpioConfig.pupd = GPIO_PUPD_PU;
    Gpio_Config(USASRT2_TX_GPIO_PORT, &gpioConfig);

    /* Configure USART Rx as input floating */
    gpioConfig.pin = (uint16_t)USASRT2_RX_PIN;
    Gpio_Config(USASRT2_RX_GPIO_PORT, &gpioConfig);

    Usart_Config(USART2, &g_usart2Config);

    /* Enable USART_Interrupt_RXBNEIE */
    Usart_EnableInterrupt(USART2, USART_INT_RXBNEIE);

    Nvic_EnableIrqRequest(USART2_IRQn, 3U);

    /* Enable USART */
    Usart_Enable(USART2);
}

/*!
 * @brief       DMA usart receive Init
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
static void Dma_Rx_Init(void)
{
    /* Enable DMA clock */
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_DMA1);
    /* DMA Configure */

    Dma_Config(DMA1_CHANNEL_USART1_RX, &g_dmaUsart1RxConfig);
    /* Clear DMA TF flag*/
    Dma_ClearIntFlag((uint32_t)DMA1_INT_FLAG_TF_USART1_RX);
    /* Enable DMA Interrupt*/
    Dma_EnableInterrupt(DMA1_CHANNEL_USART1_RX, (uint32_t)DMA_INT_TFIE);

    Nvic_EnableIrqRequest(DMA1_CHANNEL_IRQ_REQUEST_USART1_RX, 2);

    Usart_ClearStatusFlag(USART1, USART_FLAG_TXC);

    Dma_Enable(DMA1_CHANNEL_USART1_RX);

}

/*!
 * @brief       Compares two buffers
 *
 * @param       Buf1Ptr:    First buffer to be compared
 *
 * @param       Buf2Ptr:    Second buffer to be compared
 *
 * @param       Size:       Buffer size
 *
 * @retval      Return TRUE if Buf1Ptr = Buf2Ptr. If not then return FALSE
 *
 * @note
 */
static BOOL BufferCompare(const uint8_t* Buf1Ptr, const uint8_t* Buf2Ptr, uint8_t Size)
{
    uint8_t i;
    BOOL isEqual = TRUE;

    if ((Buf1Ptr == NULL) || (Buf2Ptr == NULL))
    {
        isEqual = FALSE;
    }
    else
    {
        for (i = 0; i < Size; i++)
        {
            if (Buf1Ptr[i] != Buf2Ptr[i])
            {
                isEqual = FALSE;
                break;
            }
            else
            {
                /* nothing */
            }
        }
    }

    return isEqual;
}

/*!
 * @brief       DMA_Interrupt
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Dma_Isr(void)
{
    if ((uint8_t)SET == Dma_ReadStatusFlag(DMA1_FLAG_TF5))
    {
        /* do something*/
        Dma_ClearStatusFlag((uint32_t)DMA1_FLAG_TF5);
        /* Ensure the interrupt flag is cleared when exiting the function. */
        __NOP();
    }
    else
    {
        /* do nothing */
    }
}

/**@} end of group DMA_Usart_Functions */
/**@} end of group DMA_Usart */
/**@} end of group DMA_Examples */
/**@} end of group Examples */
