一、ESP32總共有3個串口,并且3個 串口管腳都是可以重映射的
1、ESP32串口使用的基本步驟
2、ESP32串口函數(shù)介紹
3、例子代碼
二、ESP32串口使用的基本步驟 官網(wǎng)有詳細串口說明
使用UART是完成前面4個機可以實現(xiàn)UART的收發(fā),最后兩個是可選的
三、串口函數(shù)的介紹 按照基本步驟介紹函數(shù)說明
1、通信參數(shù)設置
uart_config_t uart_config = { .baud_rate = 115200, //波特率 .data_bits = UART_DATA_8_BITS, //數(shù)據(jù)位數(shù) .parity = UART_PARITY_DISABLE, //奇偶控制 .stop_bits = UART_STOP_BITS_1, //停止位 .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS, //流控位 .rx_flow_ctrl_thresh = UART_HW_FLOWCTRL_DISABLE,//控制模式};
esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config)
uart_port_t uart_num -----串口號 UART0 UART1 UART2
const uart_config_t *uart_config -----串口配置信息
2、設置通信
設置UART和具體的物理GPIO引腳關聯(lián)
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)
uart_port_t uart_num ------串口號 UART0 UART1 UART2
rx_io_num ------串口接收管腳
tx_io_num ------串口發(fā)送管腳
rts_io_num -------流控腳
cts_io_num --------流控腳
3、驅(qū)動安裝
分配接收發(fā)送空間及函數(shù)調(diào)用參數(shù)
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_num -------串口號
rx_buffer_size --------接收緩存大小
tx_buffer_size ---------發(fā)送緩存大小
queue_size ------------隊列大小
uart_queue -------------串口隊列指針
intr_alloc_flags --------------分配中斷標記
4、UART通信
接收函數(shù):
int uart_read_bytes(uart_port_t uart_num, uint8_t* buf, uint32_t length, TickType_t ticks_to_wait)
uart_port_t uart_num -------------串口號
uint8_t* buf -------------接收數(shù)據(jù)緩沖地址
uint32_t length ------------接收緩沖區(qū)長度
TickType_t ticks_to_wait ---------等待時間
發(fā)送函數(shù):
int uart_write_bytes(uart_port_t uart_num, const char* src, size_t size)
uart_port_t uart_num -------------串口號
const char* src -------------待發(fā)送數(shù)據(jù)
size_t size ---------------發(fā)送數(shù)據(jù)大小
這里只有使用4個步驟就可以正常通信
四、完整例子
/* UART Echo Example This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ #include <stdio.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/uart.h" #include "string.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_4) //#define ECHO_TEST_RXD (GPIO_NUM_5) #define ECHO_TXD0 (GPIO_NUM_1) #define ECHO_RXD0 (GPIO_NUM_3) #define ECHO_TXD1 (GPIO_NUM_23) #define ECHO_RXD1 (GPIO_NUM_22) #define ECHO_TXD2 (GPIO_NUM_21) #define ECHO_RXD2 (GPIO_NUM_19) #define ECHO_TEST_RTS (UART_PIN_NO_CHANGE) #define ECHO_TEST_CTS (UART_PIN_NO_CHANGE) #define BUF_SIZE (1024) static void Uart0Recv() { /* 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); uart_set_pin(UART_NUM_0, ECHO_TXD0, ECHO_RXD0, ECHO_TEST_RTS, ECHO_TEST_CTS); uart_driver_install(UART_NUM_0, BUF_SIZE * 2, 0, 0, NULL, 0); // 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_0, data, BUF_SIZE, 20 / portTICK_RATE_MS); if(len > 0) uart_write_bytes(UART_NUM_0, (const char *) "uart0:", strlen("uart1:")); // Write data back to the UART uart_write_bytes(UART_NUM_0, (const char *) data, len); } } static void Uart1Recv() { /* 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_1, &uart_config); uart_set_pin(UART_NUM_1, ECHO_TXD1, ECHO_RXD1, ECHO_TEST_RTS, ECHO_TEST_CTS); uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0); // 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 if(len > 0) uart_write_bytes(UART_NUM_1, (const char *) "uart1:", strlen("uart1:")); uart_write_bytes(UART_NUM_1, (const char *) data, len); } } static void Uart2Recv() { /* 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_2, &uart_config); uart_set_pin(UART_NUM_2, ECHO_TXD2, ECHO_RXD2, ECHO_TEST_RTS, ECHO_TEST_CTS); uart_driver_install(UART_NUM_2, BUF_SIZE * 2, 0, 0, NULL, 0); // 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_2, data, BUF_SIZE, 20 / portTICK_RATE_MS); // Write data back to the UART if(len > 0) uart_write_bytes(UART_NUM_2, (const char *) "uart2:", strlen("uart1:")); uart_write_bytes(UART_NUM_2, (const char *) data, len); } } void app_main() { xTaskCreate(Uart0Recv, "uart_echo_task", 1024, NULL, 10, NULL); xTaskCreate(Uart1Recv, "Uart1Recv", 1024, NULL, 11, NULL); xTaskCreate(Uart2Recv, "Uart1Recv", 1024, NULL, 11, NULL); }到這里基本就完成了3個串口的程序編寫
我使用的是紅色框哪里面的6個腳,ESP32的管腳可以重映射
聯(lián)系客服