/*!
 * @file        bsp_usart.c
 *
 * @brief       Usart board support package body
 *
 * @version     V1.0.0
 *
 * @date        2022-09-20
 *
 * @attention
 *
 *  Copyright (C) 2022 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  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 SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "bsp_usart.h"
#include <stdio.h>

/* Private includes *******************************************************/

/* Private macro **********************************************************/

/* Private typedef ********************************************************/

/* Private variables ******************************************************/

/* Private function prototypes ********************************************/

/* External variables *****************************************************/

/* External functions *****************************************************/

/*!
 * @brief   UART GPIO initialization
 *
 * @param   None
 *
 * @retval  None
 */
static void UART_GPIO_Init(void)
{
    GPIO_Config_T gpioConfig;

    SYSCTRL_Unlock();
    SYSCTRL_EnableAHBPeriphClock(SYSCTRL_AHB_GPIO);
    SYSCTRL_Lock();

    GPIO_ConfigStructInit(&gpioConfig);
    gpioConfig.mode = GPIO_MODE_AF;
    gpioConfig.speed = GPIO_OS_HIGH;
    gpioConfig.type = GPIO_OT_PP;
    gpioConfig.pullEn = DISABLE;
    gpioConfig.polarity = GPIO_POL_UP;
    gpioConfig.drive = GPIO_DS_HIGH;

    gpioConfig.pin = GPIO_PIN_0;
    GPIO_Config(&gpioConfig);

    gpioConfig.pin = GPIO_PIN_1;
    GPIO_Config(&gpioConfig);

    GPIO_ConfigPinReuse(GPIO_PIN_0, GPIO_REUSE_AF3);
    GPIO_ConfigPinReuse(GPIO_PIN_1, GPIO_REUSE_AF3);
}

/*!
 * @brief       USART Init
 *
 * @param       baudRate
 *
 * @retval      None
 *
 * @note
 */
void USART_Init(void)
{
    UART_GPIO_Init();
    
    UART_Config_T uartConfig;

    SYSCTRL_Unlock();
    SYSCTRL_EnableAPBPeriphBCGClock(SYSCTRL_APB_UART);
    SYSCTRL_EnableAPBPeriphWCGClock(SYSCTRL_APB_UART);
    SYSCTRL_Lock();

    UART_ConfigStructInit(&uartConfig);

    uartConfig.baudrate = 0x23;
    uartConfig.stopbit = UART_STOPBIT_1B;
    uartConfig.parity = UART_PARITY_EVEN;
    uartConfig.parityen = DISABLE;
    uartConfig.sequence = UART_TXSEQ_LSB;
    uartConfig.txen = ENABLE;
    uartConfig.rxen = ENABLE;
    UART_Config(&uartConfig);
}

#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)
{
    while ((UART_ReadFlag(UART_FLAG_TXBSY) == SET));
    /* send a byte of data to the serial port */
    UART_SendData((uint8_t)ch);

    /* wait for the data to be send  */
    while (UART_ReadFlag(UART_FLAG_TXC) == RESET);
    
    UART_ClearFlag(UART_CLEAR_TXC);

    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)
{
    while ((UART_ReadFlag(UART_FLAG_TXBSY) == SET));
    /* send a byte of data to the serial port */
    UART_SendData((uint8_t)ch);

    /* wait for the data to be send  */
    while (UART_ReadFlag(UART_FLAG_TXC) == RESET);
    UART_ClearFlag(UART_CLEAR_TXC);

    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
#warning Not supported compiler type
#endif
