ESP32晶片有三個UART控制器(UART0, UART1和UART2),其中UART0(GPIO3用於U0RXD,GPIO1用於U0TXD)用作下載、調試串口,引腳不可改變;
UART1和UART2的引腳是可以設置的。
UART1默認引腳是GPIO9用作U1RXD,GPIO10用作U1TXD,但是這兩個引腳也是用於外接flash的,因此在使用UART1的時候需要設置其他引腳;
UART2默認引腳是GPIO16用作U2RXD,GPIO17用作U2TXD。
2、API
在components/driver/include/driver/uart.h中可以查看api;
在examples/peripherals/uart中也可以參考官方的各種串口例程。
2.1、安裝uart驅動esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_buffer_size, int queue_size, QueueHandle_t* uart_queue, int intr_alloc_flags);
這裡要注意參數:uart_queue屬於freertos裡面的隊列句柄,在這裡表示用於指示來自串口底層中斷的隊列消息。
2.2、uart參數配置esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config);
esp_err_t uart_set_rx_full_threshold(uart_port_t uart_num, int threshold);
esp_err_t uart_set_pin(uart_port_t uart_num, int tx_io_num, int rx_io_num, int rts_io_num, int cts_io_num);
int uart_read_bytes(uart_port_t uart_num, uint8_t* buf, uint32_t length, TickType_t ticks_to_wait);
int uart_write_bytes(uart_port_t uart_num, const char* src, size_t size);
#define EX_UART_NUM UART_NUM_1 //串口1
#define TXD_PIN (GPIO_NUM_17) //txd使用gpio17
#define RXD_PIN (GPIO_NUM_16) //rxd使用gpio16
#define BUF_SIZE (128) //緩衝區定義
static QueueHandle_t uart_queue; //隊列句柄
static uint8_t uartbuf[BUF_SIZE];
void uart_comm_init(void)
{
/* Configure parameters of an UART driver,
* communication pins and install the driver */
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB,
};
//Install UART driver, and get the queue.
uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart_queue, 0);
uart_param_config(EX_UART_NUM, &uart_config);
uart_set_rx_full_threshold(EX_UART_NUM,126);
//Set UART pins (using UART0 default pins ie no changes.)
uart_set_pin(EX_UART_NUM, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
//Create a task to handler UART event from ISR
xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, 12, NULL); //創建串口任務
}
static void uart_event_task(void *pvParameters)
{
uart_event_t event;
for(;;) {
//阻塞接收串口隊列,
//這個隊列在底層發送,用戶只需在應用層接收即可
if(xQueueReceive(uart_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
switch(event.type) {//各種串口事件
case UART_DATA:
ESP_LOGI(TAG, "[UART DATA]: %d", event.size);
uart_read_bytes(EX_UART_NUM, uartbuf, event.size, portMAX_DELAY); //阻塞接收
ESP_LOGI(TAG, "[DATA EVT]:");
uart_write_bytes(EX_UART_NUM, uartbuf, event.size);//原樣發送
break;
//Event of HW FIFO overflow detected
case UART_FIFO_OVF: //硬體fifo溢出
ESP_LOGI(TAG, "hw fifo overflow");
// If fifo overflow happened, you should consider adding flow control for your application.
// The ISR has already reset the rx FIFO,
// As an example, we directly flush the rx buffer here in order to read more data.
uart_flush_input(EX_UART_NUM);
xQueueReset(uart_queue);
break;
//Event of UART ring buffer full
case UART_BUFFER_FULL: //環形緩衝區滿
ESP_LOGI(TAG, "ring buffer full");
// If buffer full happened, you should consider encreasing your buffer size
// As an example, we directly flush the rx buffer here in order to read more data.
uart_flush_input(EX_UART_NUM);
xQueueReset(uart_queue);
break;
//Event of UART RX break detected
case UART_BREAK:
ESP_LOGI(TAG, "uart rx break");
break;
//Event of UART parity check error
case UART_PARITY_ERR:
ESP_LOGI(TAG, "uart parity error");
break;
//Event of UART frame error
case UART_FRAME_ERR:
ESP_LOGI(TAG, "uart frame error");
break;
//UART_PATTERN_DET
case UART_PATTERN_DET:
break;
//Others
default:
ESP_LOGI(TAG, "uart event type: %d", event.type);
break;
}
}
}
vTaskDelete(NULL);
}
初始化串口參數,(隊列指針、緩衝區、引腳等);
任務中阻塞等待串口隊列 。
————————END————————
推薦閱讀:
【玩轉ESP32】10、創建用戶分區表,數據讀寫存儲
【玩轉ESP32】9、ESP32 作為TCP客戶端連接伺服器(非阻塞)
【玩轉ESP32】8、ESP32 Guru Meditation Error報錯定位分析