歡迎加入交流群:ESP8266 AliOS Things 群 號: 107723112
所有教程請看 AliOS Things 入門教程系列
此教程在SimonLiu的CSDN博客同步更新。
小貼士: 如果使用串口接收外設(shè)數(shù)據(jù)关带,請先關(guān)閉CLI妨退,否則可能導(dǎo)致收取數(shù)據(jù)丟幀或者發(fā)生其他錯誤西轩。
在SimonLiu的ESP8266與AliOS Things 學習教程系列之四:uart0和uart1串口打印一文中SimonLiu提供了一個把LOG()重定向到uart1的方法竹挡。但是某些情況下,我們還是需要同時使用uart0和uart1,那么如何實現(xiàn)呢?
1. 簡單版本
- 向uart0發(fā)送單個字符
uart0_write_char( char c)
瞄桨, 例如uart0_write_char('a')
- 向uart0發(fā)送字符串
void uart0_sendStr(const char *str)
{
while(*str){
uart0_write_char(*str++);
}
}
2. 復(fù)雜版本但是有bug
首先
platform\mcu\esp8266\hal\uart.c
文件中,找到int32_t hal_uart_send()
函數(shù)讶踪,把uart0_write_char(pdata[i]);
修改為如下內(nèi)容:
if (uart->port == 0)
uart0_write_char(pdata[i]);
else
uart1_write_char(pdata[i]);
然后下面的代碼就實現(xiàn)了uart0和uart1的輸出讲婚。但是有意思的是,這樣改還有個bug俊柔,就是LOG()輸出的信息筹麸,前面的部分[******]<V>
輸出到uart1,而message from LOG
輸出到了uart0雏婶。
#include <stdio.h>
#include <aos/kernel.h>
#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.關(guān)于兩個串口設(shè)置不同的波特率
串口的初始化api
在platform/mcu/esp8266/bsp/driver/uart.c
中物赶,目前的代碼是默認如果不設(shè)置,uart0
波特率是921600
留晚。但是一旦初始化了uart1
,uart0
的波特率會被改為和uart1
一樣酵紫。
如果要設(shè)置不同的波特率,需要修改如下函數(shù)
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);
*/
}
經(jīng)測試错维,uart0
波特率只能通過類似uart_config.baud_rate = BIT_RATE_9600;
的方式修改奖地,使用uart_config.baud_rate = uart->config.baud_rate;
會導(dǎo)致兩個串口都亂碼,很奇怪赋焕。原因不明参歹。