/*
 * UART.c
 *
 *  Created on: 2024Äê5ÔÂ18ÈÕ
 *      Author: 34509
 */
#include "UART.h"

void USARTx_SendByte(USART_TypeDef* pUSARTx, uint8_t data)
{
    if (pUSARTx == J5_0_USART) {
        rt_pin_write(USART3_DE, write);
    }
    else if (pUSARTx == J1_USART) {
        rt_pin_write(J1_DE, write);
    }
    else if (pUSARTx == J2_USART) {
        rt_pin_write(J2_DE, write);
    }
    else if (pUSARTx == J3_USART) {
        rt_pin_write(J3_DE, write);
    }
    else if (pUSARTx == J4_USART) {
        rt_pin_write(J4_DE, write);
    }
    else {
        return;
    }

    USART_SendData(pUSARTx, data);
    while(USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);

    if (pUSARTx == J5_0_USART) {
        rt_pin_write(USART3_DE, read);
    }
    else if (pUSARTx == J1_USART) {
        rt_pin_write(J1_DE, read);
    }
    else if (pUSARTx == J2_USART) {
        rt_pin_write(J2_DE, read);
    }
    else if (pUSARTx == J3_USART) {
        rt_pin_write(J3_DE, read);
    }
    else if (pUSARTx == J4_USART) {
        rt_pin_write(J4_DE, read);
    }
    else {
        return;
    }
}

void USARTx_SendByte_str(USART_TypeDef* pUSARTx, uint8_t data)
{
    USART_SendData(pUSARTx, data);
    while(USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);

}

void USARTx_SendStr(USART_TypeDef* pUSARTx, char *str)
{
    if (pUSARTx == J5_0_USART) {
        rt_pin_write(USART3_DE, write);
    }
    else if (pUSARTx == J1_USART) {
        rt_pin_write(J1_DE, write);
    }
    else if (pUSARTx == J2_USART) {
        rt_pin_write(J2_DE, write);
    }
    else if (pUSARTx == J3_USART) {
        rt_pin_write(J3_DE, write);
    }
    else if (pUSARTx == J4_USART) {
        rt_pin_write(J4_DE, write);
    }
    else {
        return;
    }

    uint8_t i = 0;
    do
    {
        USARTx_SendByte_str(pUSARTx, *(str+i));
       i++;
    }while(*(str+i) != '\0');
    while(USART_GetFlagStatus(pUSARTx, USART_FLAG_TC) == RESET);

    if (pUSARTx == J5_0_USART) {
        rt_pin_write(USART3_DE, read);
    }
    else if (pUSARTx == J1_USART) {
        rt_pin_write(J1_DE, read);
    }
    else if (pUSARTx == J2_USART) {
        rt_pin_write(J2_DE, read);
    }
    else if (pUSARTx == J3_USART) {
        rt_pin_write(J3_DE, read);
    }
    else if (pUSARTx == J4_USART) {
        rt_pin_write(J4_DE, read);
    }
    else {
        return;
    }
}

void USARTx_SendStr_Len(USART_TypeDef* pUSARTx, char *str, int len)
{
    if (pUSARTx == J5_0_USART) {
        rt_pin_write(USART3_DE, write);
    }
    else if (pUSARTx == J1_USART) {
        rt_pin_write(J1_DE, write);
    }
    else if (pUSARTx == J2_USART) {
        rt_pin_write(J2_DE, write);
    }
    else if (pUSARTx == J3_USART) {
        rt_pin_write(J3_DE, write);
    }
    else if (pUSARTx == J4_USART) {
        rt_pin_write(J4_DE, write);
    }
    else {
        return;
    }

    uint8_t i = 0;
    do
    {
        USARTx_SendByte_str(pUSARTx, *(str+i));
       i++;
    }while(--len);
    while(USART_GetFlagStatus(pUSARTx, USART_FLAG_TC) == RESET);

    if (pUSARTx == J5_0_USART) {
        rt_pin_write(USART3_DE, read);
    }
    else if (pUSARTx == J1_USART) {
        rt_pin_write(J1_DE, read);
    }
    else if (pUSARTx == J2_USART) {
        rt_pin_write(J2_DE, read);
    }
    else if (pUSARTx == J3_USART) {
        rt_pin_write(J3_DE, read);
    }
    else if (pUSARTx == J4_USART) {
        rt_pin_write(J4_DE, read);
    }
    else {
        return;
    }
}
