一、概述
通用異步收發送器(UART)是一種硬體特性,它使用廣泛適應的異步串行通信介面(如RS 232、RS 422、RS 485)來處理通信(即時序要求和資料幀),UART提供了一種廣泛采用和廉價的方法來實作不同設備之間的全雙工或半雙工資料交換,
ESP 32芯片有三個UART控制器(UART 0、UART 1和UART 2),它們具有一組相同的暫存器,以便于編程和靈活性,
每個UART控制器都是獨立配置的,引數包括波特率、資料位元長度、位序、停止位數、奇偶校驗位等,所有控制器都與不同廠商的UART支持設備兼容,還可以支持紅外資料關聯協議(IRDA),
ESP-IDF 編程指南——UART
二、API說明
以下 UART 介面位于 driver/include/driver/uart.h,
2.1 uart_param_config

2.2 uart_driver_install

2.3 uart_read_bytes

2.4 uart_write_bytes

2.5 uart_set_pin
ESP32的串口是支持引腳映射的,比如我的開發板串口一默認的是GPIO9和GPIO10,現在將TX、RX映射到GPIO4和GPIO5上,

三、編程流程
3.1 設定通信引數
如設定波特率、資料位、停止位等
在結構體中進行配置:
typedef struct {
int baud_rate; /*!< UART baud rate*/
uart_word_length_t data_bits; /*!< UART byte size*/
uart_parity_t parity; /*!< UART parity mode*/
uart_stop_bits_t stop_bits; /*!< UART stop bits*/
uart_hw_flowcontrol_t flow_ctrl; /*!< UART HW flow control mode (cts/rts)*/
uint8_t rx_flow_ctrl_thresh; /*!< UART HW RTS threshold*/
union {
uart_sclk_t source_clk; /*!< UART source clock selection */
bool use_ref_tick __attribute__((deprecated)); /*!< Deprecated method to select ref tick clock source, set source_clk field instead */
};
} uart_config_t;
3.2 設定通信引腳
ESP32的串口是支持引腳映射的,比如我的開發板串口一默認的是GPIO9和GPIO10,現在將TX、RX映射到GPIO4和GPIO5上,
請呼叫函式uart_set_pin()并指定驅動程式應將Tx,Rx,RTS和CTS信號路由至的GPIO引腳號,
如果要為特定信號保留當前分配的管腳號,請傳遞宏UART_PIN_NO_CHANGE,
應該為不使用的引腳指定相同的宏,
// Set UART pins(TX: IO17 (UART2 default), RX: IO16 (UART2 default), RTS: IO18, CTS: IO19)
ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, 18, 19));
3.3 驅動程式安裝
設定好通信引腳后,通過呼叫安裝驅動程式uart_driver_install()并指定以下引數:
- Tx環形緩沖區的大小
- Rx環形緩沖區的大小
- 事件佇列句柄和大小
- 分配中斷的標志
該功能將為UART驅動程式分配所需的內部資源,
// Setup UART buffered IO with event queue
const int uart_buffer_size = (1024 * 2);
QueueHandle_t uart_queue;
// Install UART driver using an event queue here
ESP_ERROR_CHECK(uart_driver_install(UART_NUM_2, uart_buffer_size, \
uart_buffer_size, 10, &uart_queue, 0));
3.4 運行UART通信
串行通信由每個UART控制器的有限狀態機(FSM)控制,
發送資料的程序涉及以下步驟:
- 將資料寫入Tx FIFO緩沖區
- FSM序列化資料
- FSM將資料發送出去
接收資料的程序類似,但是步驟相反:
- FSM處理傳入的串行流并將其并行化
- FSM將資料寫入Rx FIFO緩沖區
- 從Rx FIFO緩沖區讀取資料
因此,應用程式將被限制為分別使用uart_write_bytes()和從相應的緩沖區寫入和讀取資料uart_read_bytes(),而FSM將完成其余的作業,
3.4.1 發送
準備好要傳輸的資料后,呼叫該函式uart_write_bytes()并將資料緩沖區的地址和資料長度傳遞給該函式,該函式將資料復制到Tx環形緩沖區(立即或在有足夠空間可用之后),然后退出,當Tx FIFO緩沖區中有可用空間時,中斷服務程式(ISR)將資料從Tx環形緩沖區移至后臺的Tx FIFO緩沖區,下面的代碼演示了此功能的用法,
// Write data to UART.
char* test_str = "This is a test string.\n";
uart_write_bytes(uart_num, (const char*)test_str, strlen(test_str));
該功能uart_write_bytes_with_break()類似于uart_write_bytes()但在傳輸結束時添加了一個串行中斷信號,意味著它會在發送完資料之后,設定TX低電平一段時間(RTOS任務節拍為單位),
// Write data to UART, end with a break signal.
uart_write_bytes_with_break(uart_num, "test break\n",strlen("test break\n"), 100);
將資料寫入Tx FIFO緩沖區的另一個功能是uart_tx_chars(),不像uart_write_bytes(),此功能在可用空間之前不會阻塞,相反,它將寫入可立即放入硬體Tx FIFO中的所有資料,然后回傳已寫入的位元組數,
有一個“陪伴”功能uart_wait_tx_done(),可監視Tx FIFO緩沖區的狀態并在其為空時回傳,
// Wait for packet to be sent
const int uart_num = UART_NUM_2;
ESP_ERROR_CHECK(uart_wait_tx_done(uart_num, 100)); // wait timeout is 100 RTOS ticks (TickType_t)
3.4.2 接收
UART接收到資料并將其保存在Rx FIFO緩沖區后,需要使用函式進行讀出uart_read_bytes(),,這個函式會阻塞待在那里,直到讀滿需要的位元組,或是超時,
在讀取資料之前,您可以呼叫來檢查Rx FIFO緩沖區中可用的位元組數uart_get_buffered_data_len(),然后再讀取相應的內容,這樣就不會造成不必要的阻塞,下面給出了使用這些功能的示例,
// Read data from UART.
const int uart_num = UART_NUM_2;
uint8_t data[128];
int length = 0;
ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&length));
length = uart_read_bytes(uart_num, data, length, 100);
如果不再需要Rx FIFO緩沖區中的資料,則可以通過呼叫清除緩沖區uart_flush(),
四、串口回環輸出
這里我將GPIO4、GPIO5改成了GPIO23、GPIO18
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
/**
* This is an example which echos any data it receives on UART1 back to the sender,
* with hardware flow control turned off. It does not use UART driver event queue.
*
* - Port: UART1
* - Receive (Rx) buffer: on
* - Transmit (Tx) buffer: off
* - Flow control: off
* - Event queue: off
* - Pin assignment: see defines below
*/
#define ECHO_TEST_TXD (GPIO_NUM_23)
#define ECHO_TEST_RXD (GPIO_NUM_18)
#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
#define BUF_SIZE (1024)
static void echo_task(void *arg)
{
/* 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,
};
uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0);
uart_param_config(UART_NUM_1, &uart_config);
uart_set_pin(UART_NUM_1, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
// Configure a temporary buffer for the incoming data
uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
while (1) {
// Read data from the UART
int len = uart_read_bytes(UART_NUM_1, data, BUF_SIZE, 20 / portTICK_RATE_MS);
// Write data back to the UART
uart_write_bytes(UART_NUM_1, (const char *) data, len);
}
}
void app_main(void)
{
xTaskCreate(echo_task, "uart_echo_task", 1024, NULL, 10, NULL);
}
五、串口佇列接收
/*********************************************************************
* INCLUDES
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/uart.h"
#include "esp_log.h"
#define BUF_SIZE (1024)
#define UART_MAX_NUM_RX_BYTES (1024)
static void uartEventTask(void *pvParameters);
/*********************************************************************
* LOCAL VARIABLES
*/
static QueueHandle_t s_uart0Queue;
static const char *TAG = "board_uart";
/*********************************************************************
* PUBLIC FUNCTIONS
*/
/**
@brief 串口驅動初始化
@param 無
@return 無
*/
void UART_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
};
uart_param_config(UART_NUM_0, &uart_config); // 配置串口0引數
uart_set_pin(UART_NUM_0, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);// 配置串口0引腳
// Install UART driver, and get the queue.
uart_driver_install(UART_NUM_0, BUF_SIZE * 2, BUF_SIZE * 2, 100, &s_uart0Queue, 0); // 安裝UART驅動程式
// Create a task to handler UART event from ISR
xTaskCreate(uartEventTask, "uartEventTask", 2048, NULL, 12, NULL);
}
/*********************************************************************
* LOCAL FUNCTIONS
*/
static void uartEventTask(void *pvParameters)
{
uart_event_t event;
uint8_t *pTempBuf = (uint8_t *)malloc(UART_MAX_NUM_RX_BYTES);
for(;;)
{
// Waiting for UART event.
if(xQueueReceive(s_uart0Queue, (void *)&event, (portTickType)portMAX_DELAY))
{
bzero(pTempBuf, UART_MAX_NUM_RX_BYTES);
switch(event.type)
{
// Event of UART receving data
// We'd better handler data event fast, there would be much more data events than
// other types of events. If we take too much time on data event, the queue might be full.
case UART_DATA:
// ESP_LOGI(TAG, "[UART DATA]: %d", event.size);
uart_read_bytes(UART_NUM_0, pTempBuf, event.size, portMAX_DELAY);
uart_write_bytes(UART_NUM_0, (const char *)pTempBuf, event.size);
break;
// Event of HW FIFO overflow detected
case UART_FIFO_OVF:
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(UART_NUM_0);
xQueueReset(s_uart0Queue);
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(UART_NUM_0);
xQueueReset(s_uart0Queue);
break;
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;
// Others
default:
ESP_LOGI(TAG, "uart event type: %d", event.type);
break;
}
}
}
free(pTempBuf);
pTempBuf = NULL;
vTaskDelete(NULL);
}
void app_main(void)
{
UART_Init();
}
六、UART1列印日志
在 ESP-IDF 中任何例程中輸入idf.py menuconfig
選擇 Component config

選擇 Common ESP-related

選擇 UART for console output

從默認串口0改為 Custom

選擇 UART peripheral to use for console output 改為 UART1 輸出

將 UART TX on GPIO 和 UART RX on GPIO 改為你想要的引腳

修改完后,變成UART1列印日志

? 由 Leung 寫于 2021 年 4 月 16 日
? 參考:[ESP32]UART串口使用
ESP32 ESP-IDF UART使用模式檢測中斷報告事件實作 收發資料
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/277125.html
標籤:其他
