RISC-V MCU中文社区

【分享】 蜂鸟E203与电脑实现UART通信实现代码并实现上位机控制代码分享

发表于 开源蜂鸟E203 2021-07-25 22:16:48
0
3676
3

        大家好,我们是这是一件好事队,队伍编号为CICC1859,这里想和大家分享一下蜂鸟E203与电脑的UART通信代码,并通过UART实现上位机控制。

电脑端UART代码:

#include "stdio.h"

HANDLE GetSerialPortHandle(const char *name) {
    printf("Opening %s...\n", name);
    fflush(stdout);
    HANDLE hCom = CreateFileA(name,
                       GENERIC_READ | GENERIC_WRITE,
                       0,
                       NULL,
                       OPEN_EXISTING,
                       FILE_ATTRIBUTE_NORMAL,
                       NULL);
    if (hCom == INVALID_HANDLE_VALUE) {
        printf("Open COM failed!\n");
        fflush(stdout);
        return NULL;
    }

    printf("Configuring Timeout...\n");
    fflush(stdout);
    COMMTIMEOUTS TimeOuts;
    TimeOuts.ReadIntervalTimeout = 0;
    TimeOuts.ReadTotalTimeoutMultiplier = 0;
    TimeOuts.ReadTotalTimeoutConstant = 0;
    TimeOuts.WriteTotalTimeoutMultiplier = 1;
    TimeOuts.WriteTotalTimeoutConstant = 1;
    SetCommTimeouts(hCom, &TimeOuts);

    printf("Configuring serial port...\n");
    fflush(stdout);
    DCB dcb;
    GetCommState(hCom, &dcb);
    dcb.BaudRate = 115200;
    dcb.ByteSize = 8;
    dcb.Parity = NOPARITY;
    dcb.StopBits = ONESTOPBIT;
    SetCommState(hCom, &dcb);
    return hCom;
}

void CloseSerialPortHandle(HANDLE handle) {
    CloseHandle(handle);
}

int32_t ReadSerialPort(HANDLE hCom, uint8_t *pMessage, size_t length) {
    WINBOOL bReadStat;
    DWORD wCount;
    bReadStat = ReadFile(hCom, pMessage, length, &wCount, NULL);
    if (!bReadStat)
    {
        printf("Failed to read serial port\n");
        fflush(stdout);
    }
    return bReadStat;
}

int32_t WriteSerialPort(HANDLE hCom, const uint8_t *pMessage, size_t length) {
    WINBOOL bReadStat;
    DWORD wCount;
    bReadStat = WriteFile(hCom, pMessage, length, &wCount, NULL);
    if (!bReadStat)
    {
        printf("Failed to read serial port\n");
        fflush(stdout);
    }
    return bReadStat;
}

        代码主要包含开启UARTGetSerialPortHandle(),关闭UART:CloseSerialPortHandle()以及UART读写WriteSerialPort()ReadSerialPort()

        蜂鸟E203 UART配置采用官方代码:


int32_t uart_init(UART_TypeDef *uart, uint32_t baudrate)
{
    if (__RARELY(uart == NULL)) {
        return -1;
    }

    unsigned int uart_div = SystemCoreClock / baudrate - 1;
    uart->LCR = 0x80;
    uart->DLM = (uart_div >> 8) & 0xFF;
    uart->DLL = uart_div        & 0xFF;
    uart->FCR = 0xC6;
    uart->LCR = 0x03;

    return 0;
}

int32_t uart_config_stopbit(UART_TypeDef *uart, UART_STOP_BIT stopbit)
{
    if (__RARELY(uart == NULL)) {
        return -1;
    }

    uart->LCR &= 0xFFFFFFFB;
    uart->LCR |= (stopbit << 2);

    return 0;
}

int32_t uart_enable_paritybit(UART_TypeDef *uart)
{
    if (__RARELY(uart == NULL)) {
        return -1;
    }

    uart->LCR |= 0x8;

    return 0;
}

int32_t uart_disable_paritybit(UART_TypeDef *uart)
{
    if (__RARELY(uart == NULL)) {
        return -1;
    }

    uart->LCR &= 0xFFFFFFF7;

    return 0;
}

int32_t uart_set_parity(UART_TypeDef *uart, UART_PARITY_BIT paritybit)
{
    if (__RARELY(uart == NULL)) {
        return -1;
    }

    uart->LCR &= 0xFFFFFFCF;
    uart->LCR |= (paritybit << 4);

    return 0;
}

int32_t uart_write(UART_TypeDef *uart, uint8_t val)
{
    if (__RARELY(uart == NULL)) {
        return -1;
    }
#ifndef SIMULATION_SPIKE
#ifndef SIMULATION_XLSPIKE
    while ((uart->LSR & 0x20) == 0);
#endif
    uart->THR = val;
#else
    extern void htif_putc(char ch);
    htif_putc(val);
#endif
    return 0;
}

uint8_t uart_read(UART_TypeDef *uart)
{
    uint32_t reg;
    if (__RARELY(uart == NULL)) {
        return -1;
    }
    
    while ((uart->LSR & 0x1) == 0);
    reg = uart->RBR;
    
    return (uint8_t)(reg & 0xFF);
}


int32_t uart_enable_tx_empt_int(UART_TypeDef *uart)
{
    if (__RARELY(uart == NULL)) {
        return -1;
    }

    uart->IER |= 0x2;
    return 0;
}

int32_t uart_disable_tx_empt_int(UART_TypeDef *uart)
{
    if (__RARELY(uart == NULL)) {
        return -1;
    }

    uart->IER &= 0xFFFFFFFD;
    return 0;
}

int32_t uart_set_rx_th(UART_TypeDef *uart, uint8_t th)
{
    if (__RARELY(uart == NULL)) {
        return -1;
    }

    if(th > 3) {
       th = 3;
    }

    uart->FCR &= 0xFFFFFF3F;
    uart->FCR |= (th << 6);
    return 0;
}

int32_t uart_enable_rx_th_int(UART_TypeDef *uart)
{
    if (__RARELY(uart == NULL)) {
        return -1;
    }

    uart->IER |= 0x1;
    return 0;
}

int32_t uart_disable_rx_th_int(UART_TypeDef *uart)
{
    if (__RARELY(uart == NULL)) {
        return -1;
    }

    uart->IER &= 0xFFFFFFFE;
    return 0;
}

int32_t uart_enable_rx_err_int(UART_TypeDef *uart)
{
    if (__RARELY(uart == NULL)) {
        return -1;
    }

    uart->IER |= 0x4;
    return 0;
}

int32_t uart_disable_rx_err_int(UART_TypeDef *uart)
{
    if (__RARELY(uart == NULL)) {
        return -1;
    }

    uart->IER &= 0xFFFFFFFB;
    return 0;
}

int32_t uart_get_int_status(UART_TypeDef *uart)
{

    if (__RARELY(uart == NULL)) {
        return -1;
    }

    return uart->IIR;
}

int32_t uart_get_status(UART_TypeDef *uart)
{

    if (__RARELY(uart == NULL)) {
        return -1;
    }

    return uart->LSR;
}


 

        由此配置双方UART,并在双方主函数中调用写入读取函数即可完成电脑与蜂鸟E203的通信。下面为实测图:



        双方通过UART完成了Kyber(本作品设计)的通信过程。


喜欢3
用户评论

未通过实名认证

懒的都不写签名

积分
问答
粉丝
关注
  • RV-STAR 开发板
  • RISC-V处理器设计系列课程
  • 培养RISC-V大学土壤 共建RISC-V教育生态
RV-STAR 开发板