【ESP32学习笔记】#外设篇#(3)串口通信(UART)

一、简介

  • 官方文档:UART
  • ESP32 有 3 个 UART 控制器(UART0UART1UART2),它们具有一组相同的寄存器。
  • 每个 UART 控制器均可独立配置参数,如波特率、数据位长度、位排序、停止位数量、奇偶校验位等。
  • 支持异步通信(RS232 和 RS485)和 IrDA。
  • 通信速率可达到 5 Mbps。
  • 支持 CTS 和 RTS 信号的硬件管理以及软件流控(XON 和 XOFF)。
  • 3 个接口均可被 DMA 访问或者 CPU 直接访问。
  • 全双工异步通信
  • 支持 UART 唤醒模式
  • 支持波特率自检测功能
  • UART 有两个时钟源:80-MHz APB_CLK 以及参考时钟 REF_TICK
  • 3 个 UART 的发送 FIFO 以及接收 FIFO 共享 1024 × 8-bit RAM。
    UART 共享 RAM 图:
    在这里插入图片描述

二、配置和使用

1、初始化示例:

const int uart_num = UART_NUM_0;
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,	// 硬件流控模式(cts/rts)
    // .rx_flow_ctrl_thresh = 122,	// 硬件 RTS 阈值
    // .source_clk = ,	// 源时钟选择
};
/* 设置 UART 通讯参数 */
uart_param_config(uart_num, &uart_config);

/* 设置 UART 引脚 */
uart_set_pin(uart_num , GPIO_NUM_1, GPIO_NUM_3, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);

/* 安装驱动程序 */
const int uart_buffer_size = (1024 * 2);
QueueHandle_t uart_queue;
uart_driver_install(uart_num , uart_buffer_size, uart_buffer_size, 10, &uart_queue, 0));

// uart_isr_free(uart_num ); //释放中断服务函数

2、发送示例:

char* test_str = "Hello World.\n";

// 发送函数1
uart_write_bytes(uart_num, (const char*)test_str, strlen(test_str));

// 发送函数2 uart_tx_chars 函数不会在 TX FIFO 中等待足够的空间

// 发送函数3 传输结束时增加一个串行中断信号 相当于帧与帧之间增加间隔
uart_write_bytes_with_break(uart_num, "test break\n",strlen("test break\n"), 100);

// 等待发送完成
ESP_ERROR_CHECK(uart_wait_tx_done(uart_num, 100));

3、接收示例:

// Read data from UART.
const int uart_num = UART_NUM_0;
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);

4、自定义中断示例:

const int uart_num = UART_NUM_0;
uart_isr_handle_t handle;
// 重新注册自定义中断服务函数,不使用内部中断处理程序
uart_isr_register(uart_num, uart_irq_handler, &uart_num, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, &handle);
// 使能中断接收 	打开 rxfifo接收满中断,rxfifo超时中断(默认超时时间为10个byte)
uart_enable_rx_intr(uart_num ); 
uart_set_rx_timeout(uart_num, 20); //配置接收超时中断时间,单位为按照当前波特率传输1个bytes的时间

// 中断处理
static void IRAM_ATTR uart_irq_handler(void *arg) {
    
    
	volatile uart_dev_t *uart = arg;
	uint8_t recSize=0;
	int uartRxCount = 0;
	uint8_t uartRxBuf[100];

	uart->int_clr.rxfifo_full = 1;
	uart->int_clr.frm_err = 1;
	if(uart->int_st.rxfifo_tout) //检查是否产生超时中断
	{
    
    
		uart->int_clr.rxfifo_tout = 1;
		recSize = uart->status.rxfifo_cnt;
		if(recSize!=0){
    
    
			while(uart->status.rxfifo_cnt){
    
    
				uartRxBuf[uartRxCount++] = uart->fifo.rw_byte; 
			}
		} 
	} 
}

三、官方使用示例

peripherals/uart/

代码示例 描述
uart_echo 配置 UART 设置、安装 UART 驱动程序以及通过 UART1 接口读取/写入。
uart_events 使用模式检测中断报告各种通信事件。
uart_async_rxtxtasks 通过同一个 UART 在两个独立的 FreeRTOS 任务中发送和接收数据。
uart_select 对 UART 文件描述符使用同步 I/O 多路复用。
uart_echo_rs485 设置 UART 驱动程序以在半双工模式下通过 RS485 接口进行通信。此示例类似于外设/uart/uart_echo,但允许通过连接到 ESP32 引脚的 RS485 接口芯片进行通信。
nmea0183_parser 通过解析通过 UART 外设从 GPS 接收到的 NMEA0183 数据来获取 GPS 信息。

四、API

driver/uart.h

API 描述
uart_driver_install 安装 UART 驱动程序并将 UART 设置为默认配置
uart_driver_delete 卸载UART驱动
uart_is_driver_installed 检查驱动程序是否安装
uart_set_word_length 设置 UART 数据位
uart_get_word_length 获取 UART 数据位配置
uart_set_stop_bits 设置 UART 停止位
uart_get_stop_bits 获取 UART 停止位配置
uart_set_parity 设置 UART 奇偶校验模式
uart_get_parity 获取 UART 奇偶校验模式配置
uart_set_baudrate 设置 UART 波特率
uart_get_baudrate 获取 UART 波特率配置
uart_set_line_inverse 设置 UART 线路反转模式
uart_set_hw_flow_ctrl 设置硬件流控制
uart_set_sw_flow_ctrl 设置软件流控制
uart_clear_intr_status 清除 UART 中断状态
uart_enable_intr_mask 设置 UART 中断使能
uart_disable_intr_mask 清除 UART 中断使能位
uart_enable_rx_intr 启用 UART RX 中断 (RX_FULL & RX_TIMEOUT INTERRUPT)
uart_disable_rx_intr 禁用 UART RX 中断 (RX_FULL & RX_TIMEOUT INTERRUPT)
uart_disable_tx_intr 禁用 UART TX 中断 (TX_FULL & TX_TIMEOUT INTERRUPT)
uart_enable_tx_intr 启用 UART TX 中断 (TX_FULL & TX_TIMEOUT INTERRUPT)
uart_isr_register 注册 UART 中断处理程序 (ISR)
uart_isr_free 释放由 uart_isr_register 注册的 UART 中断处理程序
uart_set_pin 设置 UART 引脚编号
uart_set_rts 手动设置 UART RTS 引脚电平
uart_set_dtr 手动设置 UART DTR 引脚电平
uart_set_tx_idle_num 在 tx FIFO 为空后设置 UART 空闲间隔
uart_param_config 设置 UART 配置参数
uart_intr_config 配置 UART 中断
uart_wait_tx_done 等到 UART TX FIFO 为空
uart_tx_chars 从给定的缓冲区和长度将数据发送到 UART 端口
uart_write_bytes 从给定的缓冲区和长度向 UART 端口发送数据
uart_write_bytes_with_break 从给定的缓冲区和长度向 UART 端口发送数据
uart_read_bytes UART 从 UART 缓冲区读取字节
uart_flush uart_flush_input 的别名。UART 环形缓冲区刷新。会删除 UART RX 缓冲区中的所有数据。
uart_flush_input 清除输入缓冲区,删除环形缓冲区中的所有数据。
uart_get_buffered_data_len UART 获取 RX 环形缓冲区缓存的数据长度
uart_disable_pattern_det_intr UART 禁用模式检测功能。专为“AT 命令”等应用而设计。当硬件检测到一系列相同的字符时,就会触发中断。
uart_enable_pattern_det_intr UART 使能模式检测功能。专为“AT 命令”等应用而设计。当硬件检测到一系列相同的字符时,就会触发中断。
uart_enable_pattern_det_baud_intr UART 使能模式检测功能。专为“AT 命令”等应用而设计。当硬件检测到一系列相同的字符时,就会触发中断。
uart_pattern_pop_pos 返回缓冲区中最近检测到的模式位置
uart_pattern_get_pos 返回缓冲区中最近检测到的模式位置。
uart_pattern_queue_reset 分配一个给定长度的新内存来保存记录在 rx 缓冲区中检测到的模式位置
uart_set_mode UART 设置通信模式
uart_set_rx_full_threshold 设置 RX fifo full 的 UART 阈值
uart_set_tx_empty_threshold 为 TX fifo 为空设置 UART 阈值
uart_set_rx_timeout UART 设置 TOUT 功能的阈值超时
uart_get_collision_flag 返回 RS485 模式的碰撞检测标志 函数将碰撞检测标志返回到由碰撞标志指向的变量中。
uart_set_wakeup_threshold 设置浅睡眠唤醒的 RX 引脚信号边沿数
uart_get_wakeup_threshold 获取浅睡眠唤醒的 RX 引脚信号边沿数
uart_wait_tx_idle_polling 等到 UART tx 内存为空并且最后一个字符发送正常(轮询模式)
uart_set_loop_back 配置TX信号环回RX模块,仅供测试使用
uart_set_always_rx_timeout 配置 UART RX 超时中断的行为

五、报错:

问题一:

uart: uart_read_bytes(1156): uart driver error

解决:
没有初始化该串口,初始化一下


问题二:

uart: uart_driver_install(1284): uart tx buffer length error(>128 or 0)

解决:
发送缓冲区太小。应 >128 or 0
注:Rx_buffer_size 应该大于 UART_FIFO_LEN。Tx_buffer_size 应为零或大于 UART_FIFO_LEN


问题三:

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled

uart_driver_install函数:

#define UART_FULL_THRESH_DEFAULT        (120)

uart_intr_config_t uart_intr = {
    
    
    .intr_enable_mask = UART_INTR_CONFIG_FLAG,
    .rxfifo_full_thresh = UART_FULL_THRESH_DEFAULT,
    .rx_timeout_thresh = UART_TOUT_THRESH_DEFAULT,
    .txfifo_empty_intr_thresh = UART_EMPTY_THRESH_DEFAULT,
};

默认值 120,只能接收到120个字节就中断。

Guess you like

Origin blog.csdn.net/p1279030826/article/details/117519574