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

/* Includes */
#include "Board.h"
#include "stdio.h"
#include "g32a10xx_gpio.h"
#include "g32a10xx_usart.h"
#include "g32a10xx_misc.h"
#include "g32a10xx_eint.h"
#include "user_usart_config.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup USART_Examples
  @{
*/

/** @addtogroup USART_Polling
  @{
*/

/** @defgroup USART_Polling_Macros Macros
  @{
*/

/* printf function configs to USART2 */
#define DEBUG_USART  USART1

#define BUFF_SIZE       (32)

/**@} end of group USART_Polling_Macros */

/** @defgroup USART_Polling_Variables Variables
  @{
*/

/* USART1 receive buffer */
static uint8_t g_rxDataBufUsart1[BUFF_SIZE] = {0};
/* USART2 transmit buffer */
static uint8_t g_txDataBufUsart2[BUFF_SIZE] = {0};

/**@} end of group USART_Polling_Variables */

/** @defgroup USART_Polling_Functions Functions
  @{
*/

/* USART Init */
void Usart_Init2(void);
/* Usart_Delay */
void Usart_Delay(uint32_t count);
/* Buffer compare */
BOOL Usart_BufferCompare(const uint8_t* Buf1Ptr, const uint8_t* Buf2Ptr, uint8_t Size);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    int i = 0;
    BOOL state = TRUE;
    /* Init g_txDataBufUsart2 */
    for (i = 0; i < BUFF_SIZE; i++)
    {
        g_txDataBufUsart2[i] = (uint8_t)i;
    }

    Board_LedInit(LED2);
    Board_LedInit(LED3);
    Board_ComInit(COM1);
    Usart_Init2();
    Usart_DisableInterrupt(EVAL_COM1, USART_INT_RXBNEIE);
    Usart_DisableInterrupt(USART2, USART_INT_RXBNEIE);
   
    for (i = 0; i < BUFF_SIZE; i++)
    {
        /* Wait until end of transmission */
        while (Usart_ReadStatusFlag(USART2, USART_FLAG_TXBE) == (uint8_t)RESET)
        {
            /* nothing */
        }

        Usart_TxData(USART2, g_txDataBufUsart2[i]);

        /* Wait until end of reception */
        while (Usart_ReadStatusFlag(USART1, USART_FLAG_RXBNE) == (uint8_t)RESET)
        {
            /* nothing */
        }

        g_rxDataBufUsart1[i] = (uint8_t)Usart_RxData(USART1);
    }

    /* Verify data */
    state = Usart_BufferCompare(g_rxDataBufUsart1, g_txDataBufUsart2, BUFF_SIZE);

    /* Data is ok then turn off LED2 */
    if (state == TRUE)
    {
        Board_LedOn(LED2);
        printf("The DATA IS PASS, LED2 ON\r\n");
    }
    else
    {
        /* nothing */
    }

    while (1)
    {
        Board_LedToggle(LED3);
        Usart_Delay(0x7ffff);
    }
}

/*!
 * @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
 */
BOOL Usart_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       USART2 Configuration
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Usart_Init2(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);

    /* EVAL_USARTs configured as follow: */
    Usart_Config(USART2, &g_usart2Config);

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

    Nvic_EnableIrqRequest(USART2_IRQn, 3);

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

/*!
 * @brief       Usart_Delay
 *
 * @param       count:  delay count
 *
.
 * @retval      None
 *
 * @note
 */
void Usart_Delay(uint32_t count)
{
    volatile uint32_t delay = count;

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

#if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @param       *f:  pointer to a FILE that can recording all information
*              needed to control a stream
*
* @retval      The characters that need to be send.
*
* @note
*/
int fputc(int ch, FILE* f)
{
    /* send a byte of data to the serial port */
    Usart_TxData(DEBUG_USART, (uint8_t)ch);

    /* wait for the data to be send  */
    while (Usart_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == (uint8_t)RESET)
    {
        /* nothing */
    }

    return (ch);
}

#elif defined (__GNUC__)

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @retval      The characters that need to be send.
*
* @note
*/
int __io_putchar(int ch)
{
    /* send a byte of data to the serial port */
    Usart_TxData(DEBUG_USART, ch);

    /* wait for the data to be send  */
    while (Usart_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET)
    {
        /* nothing */
    }

    return ch;
}

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       file:  Meaningless in this function.
*
* @param       *ptr:  Buffer pointer for data to be sent.
*
* @param       len:  Length of data to be sent.
*
* @retval      The characters that need to be send.
*
* @note
*/
int _write(int file, char* ptr, int len)
{
    int i;
    for (i = 0; i < len; i++)
    {
        __io_putchar(*ptr++);
    }

    return len;
}

#else
#pragma message("Not supported compiler type")
#endif

/**@} end of group USART_Polling_Functions */
/**@} end of group USART_Polling */
/**@} end of group USART_Examples */
/**@} end of group Examples */
