一、ESP32总共有3个串口,并且3个 串口管脚都是可以重映射的
ESP32规格书:(文档–>ESP32 技术规格书)
二、ESP32串口使用的基本步骤 官网有详细串口说明
- 设置通信参数波特率、数据位、停止位等 --设置参数
- 设置通讯-其他UART连接到的引脚 --设置具体的管脚及是否选择流控位
- 驱动器安装-为UART驱动程序分配ESP 32的资源 --分配接收发送空间
- 运行UART通信-发送/接收数据 --串口收发
- 使用中断-触发对特定通信事件的中断 --注册中断
- 删除驱动程序-释放esp 32的资源,如果不再需要uart通信。
- 使用UART是完成前面4个机可以实现UART的收发,最后两个是可选的
三、串口函数的介绍 按照基本步骤介绍函数说明
1、通信参数设置
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_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引脚关联
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 ------串口发送管脚
rts_io_num -------流控脚
cts_io_num --------流控脚
3、驱动安装
分配接收发送空间及函数调用参数
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 ---------发送缓存大小
queue_size ------------队列大小
uart_queue -------------串口队列指针
intr_alloc_flags --------------分配中断标记
4、UART通信
接收函数:
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 -------------接收数据缓冲地址
uint32_t length ------------接收缓冲区长度
TickType_t ticks_to_wait ---------等待时间
发送函数:
int uart_write_bytes(uart_port_t uart_num, const char* src, size_t size)
uart_port_t uart_num -------------串口号
const char* src -------------待发送数据
size_t size ---------------发送数据大小
使用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);
}