SimonLiu的ESP8266与AliOS Things 学习教程系列之十八:uart0和uart1打印的进一步优化以及不同波特率设置

欢迎加入交流群: ESP8266 AliOS Things 群 号: 107723112
系列文章目录:
SimonLiu的ESP8266与AliOS Things 学习教程系列目录

1. 小贴士: 如果使用串口接收外设数据,请先关闭CLI,否则可能导致收取数据丢帧或者发生其他错误。串口接收函数是app_entry.c中的uart_poll_func(void *arg)

在4. SimonLiu的ESP8266与AliOS Things 学习教程系列之四:uart0和uart1串口打印一文中SimonLiu提供了一个把LOG()重定向到uart1的方法。但是某些情况下,我们还是需要同时使用uart0和uart1,那么如何实现呢?

1. 简单版本

  • 向uart0发送单个字符
    uart0_write_char( char c), 例如 uart0_write_char('a')
  • 向uart0发送字符串
void uart0_sendStr(const char *str)
{
    while(*str){
        uart0_write_char(*str++);
    }
}

2. 复杂版本但是有bug

首先
platform\mcu\esp8266\hal\uart.c文件中,找到int32_t hal_uart_send()函数,把uart0_write_char(pdata[i]);
修改为如下内容:

        if (uart->port == 0)
            uart0_write_char(pdata[i]);
        else
            uart1_write_char(pdata[i]);

然后下面的代码就实现了uart0和uart1的输出。但是有意思的是,这样改还有个bug,就是LOG()输出的信息,前面的部分[******]输出到uart1,而message from LOG输出到了uart0。

#include 

#include 
#include "aos/hal/uart.h"
#include "driver/gpio.h"
#include "uart.h"
#include "ulog/ulog.h"

int application_start(int argc, char *argv[])
{

    uart_config_t uartConfig0;
    uartConfig0.baud_rate = 9600; 
    uartConfig0.parity = 0;
    uartConfig0.stop_bits = 1;
    uart_dev_t uart0;
    uart0.port = 0; 
    uart0.config = uartConfig0;
    hal_uart_init(&uart0);

    uart_config_t uartConfig1;
    uartConfig1.baud_rate = 115200; 
    uart_dev_t uart1;
    uart1.port = 1; 
    uart1.config = uartConfig1;
    hal_uart_init(&uart1);

    while(1) {
        printf("\r\n message from printf(), to uart1 \r\n");
        LOG("message from LOG");
        const char message0[] = "\r\n hal_uart_send message to uart0\r\n";
        const char message1[] = "\r\n hal_uart_send message to uart1\r\n";
        hal_uart_send(&uart0, message0, strlen(message0), 0);
        hal_uart_send(&uart1, message1, strlen(message1), 0);
        aos_msleep(1000);
    };
}

3.关于两个串口设置不同的波特率

串口的初始化apiplatform/mcu/esp8266/bsp/driver/uart.c中,目前的代码是默认如果不设置,uart0波特率是921600。但是一旦初始化了uart1,uart0的波特率会被改为和uart1一样。
如果要设置不同的波特率,需要修改如下函数

void
uart_init_new(uart_dev_t *uart)
{
    UART_WaitTxFifoEmpty(UART0);
    UART_WaitTxFifoEmpty(UART1);

    if (uart == NULL)
    {
        return;
    }

    if (uart->port == 1)
    {
        //printf("port= 1\n ");

        //uart1 setting
        UART_ConfigTypeDef uart_config;
        uart_config.baud_rate = uart->config.baud_rate;
        uart_config.data_bits = UART_WordLength_8b;
        uart_config.parity = USART_Parity_None;
        uart_config.stop_bits = USART_StopBits_1;
        uart_config.flow_ctrl = USART_HardwareFlowControl_None;
        uart_config.UART_RxFlowThresh = 120;
        uart_config.UART_InverseMask = UART_None_Inverse;
        /*注意修改以下部分*/
        UART_ParamConfig(UART1, &uart_config);

        //uart2 setting for log
        // uart_config.baud_rate = uart->config.baud_rate;
        // UART_ParamConfig(UART1, &uart_config);

        // UART_IntrConfTypeDef uart_intr;
        // uart_intr.UART_IntrEnMask = UART_RXFIFO_TOUT_INT_ENA | UART_FRM_ERR_INT_ENA | UART_RXFIFO_FULL_INT_ENA | UART_TXFIFO_EMPTY_INT_ENA;
        // uart_intr.UART_RX_FifoFullIntrThresh = 100;
        // uart_intr.UART_RX_TimeOutIntrThresh = 10;
        // uart_intr.UART_TX_FifoEmptyIntrThresh = 20;
        // UART_IntrConfig(UART0, &uart_intr);
        /*注意修改以上部分*/
        UART_SetPrintPort(UART1);
        UART_intr_handler_register(uart0_rx_isr, NULL);
        ETS_UART_INTR_ENABLE();
    }
    else
    {
        //printf("port= 0 \n ");
        UART_ConfigTypeDef uart_config;
        /*注意修改以下部分*/
        uart_config.baud_rate = BIT_RATE_9600;
        // uart_config.baud_rate = BIT_RATE_921600;
        /*注意修改以上部分*/
        uart_config.data_bits = UART_WordLength_8b;
        uart_config.parity = USART_Parity_None;
        uart_config.stop_bits = USART_StopBits_1;
        uart_config.flow_ctrl = USART_HardwareFlowControl_None;
        uart_config.UART_RxFlowThresh = 120;
        uart_config.UART_InverseMask = UART_None_Inverse;
        UART_ParamConfig(UART0, &uart_config);

        UART_IntrConfTypeDef uart_intr;
        uart_intr.UART_IntrEnMask = UART_RXFIFO_TOUT_INT_ENA | UART_FRM_ERR_INT_ENA | UART_RXFIFO_FULL_INT_ENA | UART_TXFIFO_EMPTY_INT_ENA;
        uart_intr.UART_RX_FifoFullIntrThresh = 10;
        uart_intr.UART_RX_TimeOutIntrThresh = 2;
        uart_intr.UART_TX_FifoEmptyIntrThresh = 20;
        UART_IntrConfig(UART0, &uart_intr);

        UART_SetPrintPort(UART0);
        UART_intr_handler_register(uart0_rx_isr, NULL);
        ETS_UART_INTR_ENABLE();
    }


    /*
    UART_SetWordLength(UART0,UART_WordLength_8b);
    UART_SetStopBits(UART0,USART_StopBits_1);
    UART_SetParity(UART0,USART_Parity_None);
    UART_SetBaudrate(UART0,74880);
    UART_SetFlowCtrl(UART0,USART_HardwareFlowControl_None,0);
    */
}

经测试,uart0波特率只能通过类似uart_config.baud_rate = BIT_RATE_9600;的方式修改,使用uart_config.baud_rate = uart->config.baud_rate;会导致两个串口都乱码,很奇怪。原因不明。

你可能感兴趣的:(AliOS,Things,ESP8266)