ESP32-IDF USART 专题


第一节主要介绍 ESP32-IDF 相关 API,在第二节介绍如何在 ESP32 上使用 USART,第三节为实例演示。

一、基本介绍

API 参考路径 esp-idf/components/esp_driver_uart/include/driver/uart.h

1、配置结构体

1.1 uart_config_t

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 */
#if (SOC_UART_LP_NUM >= 1)
        lp_uart_sclk_t lp_source_clk;       /*!< LP_UART source clock selection */
#endif
    };
} uart_config_t;

该结构体用于 USART 初始化设置。

  • baud_rate:设置波特率
  • data_bits:数据位长度
  • parity:设置校验位模式
  • stop_bits:设置停止位长度
  • flow_ctrl:硬件流控制
  • rx_flow_ctrl_thresh:设置 UART 硬件流控制(RTS/CTS)的阈值。当接收到的数据达到这个阈值时,UART 硬件会自动发送 RTS 信号,请求发送方暂停发送数据,直到接收到的数据被处理完毕。这样可以避免数据溢出,提高数据传输的稳定性。
  • source_clk:时钟源,见下:
typedef enum {
   
   
    UART_SCLK_APB = SOC_MOD_CLK_APB,           /* 使用 APB 时钟 */
    UART_SCLK_REF_TICK = SOC_MOD_CLK_REF_TICK, /* 使用外部时钟 */
    UART_SCLK_DEFAULT = SOC_MOD_CLK_APB,       /* 默认使用 APB 时钟 */
} soc_periph_uart_clk_src_legacy_t;

1.2 uart_intr_config_t

typedef struct {
   
   
    uint32_t intr_enable_mask;          /*!< UART interrupt enable mask, choose from UART_XXXX_INT_ENA_M under UART_INT_ENA_REG(i), connect with bit-or operator*/
    uint8_t  rx_timeout_thresh;         /*!< UART timeout interrupt threshold (unit: time of sending one byte)*/
    uint8_t  txfifo_empty_intr_thresh;  /*!< UART TX empty interrupt threshold.*/
    uint8_t  rxfifo_full_thresh;        /*!< UART RX full interrupt threshold.*/
} uart_intr_config_t;

该结构体用于 USART 中断配置。

  • intr_enable_mask:中断使能掩码,通过位运算(如按位或)可以连接多个 UART 中断使能标志,例如,对应于 UART_TXUART_RXUART_ERROR 等不同的中断类型。
  • rx_timeout_thresh:接收超时阈值,单位为发送一个字节的时间。当在此阈值内没有接收到新数据时,将触发超时中断,用于处理接收操作的超时情况。
  • txfifo_empty_intr_thresh:发送 FIFO 空中断阈值,指定了 UART 发送 FIFO 缓冲区为空时触发的中断阈值。当 FIFO 中的数据量低于此阈值时,将触发与发送操作相关的中断,可以用于确认数据是否成功发送。
  • rxfifo_full_thresh:接收 FIFO 满中断阈值,在 FIFO 满的情况下,如果再次接收到数据,将触发接收满中断,用于处理接收数据缓冲区可能溢出的情况。

1.3 uart_event_t

typedef struct {
   
   
    uart_event_type_t type; /*!< UART event type */
    size_t size;            /*!< UART data size for UART_DATA event*/
    bool timeout_flag;      /*!< UART data read timeout flag for UART_DATA event (no new data received during configured RX TOUT)*/
    /*!< If the event is caused by FIFO-full interrupt, then there will be no event with the timeout flag before the next byte coming.*/
} uart_event_t;

该结构体用于表示 UART 事件的相关信息。

  • type:事件类型
  • size:存放 UART_DATA 事件的有效数据大小
  • timeout_flag:指示 UART 数据读取是否超时。当接收过程中没有在配置的接收超时时间内接收到新的数据时,这个标志会被设置为 true。如果事件是由 FIFO满中断引起的,则在接下来的字节到达之前不会有带超时标志的事件。

2、常用 API

2.1 驱动相关

2.1.1 uart_driver_install
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:UART 端口号,最大为 UART_NUM_MAX - 1
    • rx_buffer_size:UART 接收环形缓冲区大小
    • tx_buffer_size:UART 发送环形缓冲区大小。如果设置为零,驱动程序将不使用 TX 缓冲区,TX 函数将阻塞任务直到所有数据都被发送出去。
    • queue_size:UART 事件队列大小
    • uart_queue:UART 事件队列句柄(输出参数)。成功后,此处将写入新的队列句柄以提供访问 UART 事件的权限。如果设置为 NULL,则驱动程序将不使用事件队列。
    • intr_alloc_flags:分配中断时使用的标志。一个或多个(按位或运算)ESP_INTR_FLAG_* 值(见 esp_intr_alloc.h)。
  • 作用
    • 安装 UART 驱动并设置 UART 为默认配置。UART ISR 处理器将被附加到与该函数正在运行的相同 CPU 内核上。
  • 返回值
    • ESP_OK 成功
    • ESP_FAIL 参数错误
2.1.2 uart_driver_delete
esp_err_t uart_driver_delete(uart_port_t uart_num)
  • 参数
    • uart_num:UART 端口号,最大为 UART_NUM_MAX - 1
  • 作用
    • 卸载 UART 驱动。
  • 返回值
    • ESP_OK 成功
    • ESP_FAIL 参数错误
2.1.3 uart_is_driver_installed
bool uart_is_driver_installed(uart_port_t uart_num)
  • 参数
    • uart_num:UART 端口号,最大为 UART_NUM_MAX - 1
  • 作用
    • 检查是否已安装驱动程序。
  • 返回值
    • true:驱动程序已安装
    • false:驱动程序未安装

2.2 中断相关

2.2.1 uart_clear_intr_status
esp_err_t uart_clear_intr_status(uart_port_t uart_num, uint32_t clr_mask)
  • 参数
    • uart_num:UART 端口号,最大为 UART_NUM_MAX - 1
    • clr_mask:中断状态要清除的位掩码。
  • 作用
    • 清除 UART 中断标志位。
  • 返回值
    • ESP_OK 成功
    • ESP_FAIL 参数错误
2.2.2 uart_enable_intr_mask
esp_err_t uart_enable_intr_mask(uart_port_t uart_num, uint32_t enable_mask)
  • 参数
    • uart_num:UART 端口号,最大为 UART_NUM_MAX - 1
    • enable_mask:启动位的位掩码。
  • 作用
    • 设置 UART 中断标志位。
  • 返回值
    • ESP_OK 成功
    • ESP_FAIL 参数错误

同理,禁用为:esp_err_t uart_disable_intr_mask(uart_port_t uart_num, uint32_t disable_mask)

2.2.3 uart_enable_rx_intr
esp_err_t uart_enable_rx_intr(uart_port_t uart_num)
  • 参数
    • uart_num:UART 端口号,最大为 UART_NUM_MAX - 1
  • 作用
    • 使能 UART RX 中断(RX_FULLRX_TIMEOUT)。
  • 返回值
    • ESP_OK 成功
    • ESP_FAIL 参数错误

禁用为:esp_err_t uart_disable_rx_intr(uart_port_t uart_num)

2.2.5 uart_enable_tx_intr
esp_err_t uart_enable_tx_intr(uart_port_t uart_num, int enable, int thresh)
  • 参数
    • uart_num:UART 端口号,最大为 UART_NUM_MAX - 1
    • enable:1: 启用; 0: 禁用
    • thresh:TX 中断的阈值(0 ~ UART_HW_FIFO_LEN(uart_num)
  • 作用
    • 使能 UART TX 中断(TX_FULLTX_TIMEOUT)。
  • 返回值
    • ESP_OK 成功
    • ESP_FAIL 参数错误
2.2.6 uart_intr_config
esp_err_t uart_intr_config(uart_port_t uart_num, const uart_intr_config_t *intr_conf)
  • 参数
    • uart_num:UART 端口号,最大为 UART_NUM_MAX - 1
    • intr_conf :UART 中断配置
  • 作用
    • 配置 UART TX 中断。
  • 返回值
    • ESP_OK 成功
    • ESP_FAIL 参数错误

2.3 参数设置

2.3.1 uart_set_word_length
esp_err_t uart_set_word_length(uart_port_t uart_num, uart_word_length_t data_bit)
  • 参数
    • uart_num:UART 端口号,最大为 UART_NUM_MAX - 1
    • data_bit:数据位长度
  • 作用
    • 设置 UART 数据位长度。
  • 返回值
    • ESP_OK 成功
    • ESP_FAIL 参数错误

读取使用函数:esp_err_t uart_get_word_length(uart_port_t uart_num, uart_word_length_t *data_bit)

2.3.2 uart_set_stop_bits
esp_err_t uart_set_stop_bits(uart_port_t uart_num, uart_stop_bits_t stop_bits)
  • 参数
    • uart_num:UART 端口号,最大为 UART_NUM_MAX - 1
    • stop_bits:停止位长度
  • 作用
    • 设置 UART 停止位长度。
  • 返回值
    • ESP_OK 成功
    • ESP_FAIL 参数错误

读取使用函数:esp_err_t uart_get_stop_bits(uart_port_t uart_num, uart_stop_bits_t* stop_bits)

2.3.3 uart_set_parity
esp_err_t uart_set_parity(uart_port_t uart_num, uart_parity_t parity_mode)
  • 参数
    • uart_num:UART 端口号,最大为 UART_NUM_MAX - 1
    • parity_mode:奇偶校验模式
  • 作用
    • 设置 UART 奇偶校验模式。
  • 返回值
    • ESP_OK 成功
    • ESP_FAIL 参数错误

读取使用函数:esp_err_t uart_get_parity, uart_port_t uart_num, uart_parity_t* parity_mode

2.3.4 uart_set_baudrate
esp_err_t uart_set_baudrate(uart_port_t uart_num, uint32_t baudrate)
  • 参数
    • uart_num:UART 端口号,最大为 UART_NUM_MAX - 1
    • baudrate:波特率
  • 作用
    • 设置 UART 波特率。
  • 返回值
    • ESP_OK 成功
    • ESP_FAIL 参数错误

读取使用函数:esp_err_t uart_get_baudrate(uart_port_t uart_num, uint32_t *baudrate)

2.3.5 uart_set_pin
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_num:UART 端口号,最大为 UART_NUM_MAX - 1
    • tx_io_num:tx 引脚号
    • rx_io_num:rx 引脚号
    • rts_io_num:rts 引脚号
    • cts_io_num:cts 引脚号
  • 作用
    • 将 UART 外设的信号分配给 GPIO 引脚。
  • 返回值
    • ESP_OK 成功
    • ESP_FAIL 参数错误

内部信号可以输出到多个 GPIO 引脚。只能有一个 GPIO 引脚连接输入信号。

2.3.6 uart_set_mode
esp_err_t uart_set_mode(uart_port_t uart_num, uart_mode_t mode)
  • 参数
    • uart_num:UART 端口号,最大为 UART_NUM_MAX - 1
    • mode:UART 要设置的模式
  • 作用
    • 设置 UART 通信模式。
  • 返回值
    • ESP_OK 成功
    • ESP_FAIL 参数错误
2.3.7 uart_param_config
esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config)
  • 参数
    • uart_num:UART 端口号,最大为 UART_NUM_MAX - 1
    • uart_config:UART 配置参数
  • 作用
    • 设置 UART 配置参数。
  • 返回值
    • ESP_OK 成功
    • ESP_FAIL 参数错误

2.4 通信

2.4.1 uart_wait_tx_done
es
由于ESP32-CAM是一个基于ESP32芯片的开发板,因此在STM32上开发ESP32-CAM需要使用ESP32的开发工具链和库。以下是一个简单的ESP32-CAM在STM32上开发案例: 1. 配置开发环境 首先,需要下载和安装ESP32的开发工具链和库。可以使用Arduino IDE或者ESP-IDF进行开发。在本案例中,我们使用ESP-IDF进行开发。另外,还需要安装STM32的开发环境,例如Keil或者STM32CubeIDE。 2. 连接ESP32-CAM和STM32 ESP32-CAM通过UART接口与STM32通信。因此,需要将ESP32-CAM的TXD和RXD引脚连接到STM32的RX和TX引脚上。此外,还需要将ESP32-CAM的GND和STM32的GND连接在一起。 3. 编写代码 在STM32中,需要编写一个简单的程序来读取ESP32-CAM发送的数据。以下是一个示例代码: ``` #include "stm32f1xx_hal.h" UART_HandleTypeDef huart1; void SystemClock_Config(void); int main(void) { HAL_Init(); SystemClock_Config(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_USART1_CLK_ENABLE(); GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; HAL_UART_Init(&huart1); while (1) { uint8_t data; if (HAL_UART_Receive(&huart1, &data, 1, 100) == HAL_OK) { // 处理接收到的数据 } } } void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_OFF; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } ``` 以上代码仅用于读取ESP32-CAM发送的数据,需要根据具体需求进行修改。 4. 测试程序 将编写好的程序烧录到STM32上,然后将ESP32-CAM连接到STM32。启动程序后,可以通过ESP32-CAM发送数据来测试程序是否正常工作。 以上是一个简单的ESP32-CAM在STM32上开发案例。需要注意的是,由于ESP32和STM32是不同的芯片,因此在开发过程中需要注意硬件和软件的兼容性。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值