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

/** @addtogroup Examples
  @{
  */

/** @addtogroup USART_Interrupt
  @{
  */

/** @defgroup USART_Interrupt_Macros Macros
  @{
*/

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

/**@} end of group USART_Interrupt_Macros */

/** @defgroup USART_Interrupt_Functions Functions
  @{
  */

/* USART Write Data */
void Usart_Write(USART_T* UsartPtr, const uint8_t* DataPtr);
void  Usart_Receive_Isr(void);

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

    /* Enable GPIO clock */
    Rcm_EnableAhbPeriphClock(EVAL_COM1_TX_GPIO_CLK);

    /* Enable COM1 clock */
    Rcm_EnableApb2PeriphClock(EVAL_COM1_CLK);

    /* Connect PXx to USARTx_Tx */
    Gpio_ConfigPinAF(EVAL_COM1_TX_GPIO_PORT, EVAL_COM1_TX_SOURCE, EVAL_COM1_TX_AF);

    /* Connect PXx to USARRX_Rx */
    Gpio_ConfigPinAF(EVAL_COM1_RX_GPIO_PORT, EVAL_COM1_RX_SOURCE, EVAL_COM1_RX_AF);

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

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

    /* USART_Config */
    Usart_Config(EVAL_COM1, &g_usart1Config);

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

    Nvic_EnableIrqRequest(EVAL_COM1_IRQn, 2);

    /* Enable USART */
    Usart_Enable(EVAL_COM1);

    /* EVAL_COM1 Send data to PC, and you need to open serial assistant to observe */
    Usart_Write(EVAL_COM1, (uint8_t*)"HELLO G32A EVAL BOARD\r\n");
    while (1)
    {
    }
}

/*!
 * @brief        serial port tramsimt data
 *
 * @param        pointer to date that need to be sent
 *
 * @retval       None
 *
 * @note
 */
void Usart_Write(USART_T* UsartPtr, const uint8_t* DataPtr)
{
    const uint8_t* p_data = DataPtr;

    while ((p_data != (void*)0) && (*p_data != 0U))
    {
        while (Usart_ReadStatusFlag(UsartPtr, USART_FLAG_TXBE) == (uint8_t)RESET)
        {
            /* nothing */
        }
        Usart_TxData(UsartPtr, *p_data);
        p_data++;
    }
}

/*!
 * @brief        This function handles USART1 RX interrupt Handler
 *
 * @param        None
 *
 * @retval       None
 *
 * @note        This function need to put into  void USART1_IRQHandler(void)
 */
void  Usart_Receive_Isr(void)
{
    uint8_t dat = 0;

    if (Usart_ReadStatusFlag(EVAL_COM1, USART_FLAG_RXBNE) == (uint8_t)SET)
    {
        dat = (uint8_t)Usart_RxData(EVAL_COM1);

        printf("%c", dat);
    }
    else
    {
        /* do nothing */
    }
}

#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_Interrupt_Functions */
/**@} end of group USART_Interrupt */
/**@} end of group USART_Examples */
/**@} end of group Examples */
