【STM32cubeide HAL库】DMA + 空闲中断 实现UART不定长数据接收(自用)

目录

1.stm32cubeide配置

2.DMA运行逻辑

3 .串口中断

4.调试

5.关于dma


1.stm32cubeide配置

本篇选用stm32f103c8t6最小系统,配置向导如下:

1.1 SYS

1.2 RCC

开启外部高速晶振(根据板子外设选择外部时钟)

1.3 UART

选择串口2,开启串口2全局中断

1.4配置dma

2.DMA运行逻辑

需要下面几个函数:
  HAL_UARTEx_ReceiveToIdle_DMA(&huart2, U2_rx_buffer, sizeof(U2_rx_buffer));//串口中断+dma
  __HAL_DMA_DISABLE_IT(&hdma_usart2_rx,DMA_IT_HT);//关闭dma接收半满中断函数
  void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
//需要使用该回调函数

2.1 函数说明 

HAL_UARTEx_ReceiveToIdle_DMA()

函数可以实现uart串口空闲中断,但是该函数中调用 status =  UART_Start_Receive_DMA(huart, pData, Size);函数会使能dma的接收中断(传输完成、半传输、传输错误),导致dma中断调用回调函数,容易出现问题。

/**
  * @brief Receive an amount of data in DMA mode till either the expected number of data is received or an IDLE event occurs.
  * @note   Reception is initiated by this function call. Further progress of reception is achieved thanks
  *         to DMA services, transferring automatically received data elements in user reception buffer and
  *         calling registered callbacks at half/end of reception. UART IDLE events are also used to consider
  *         reception phase as ended. In all cases, callback execution will indicate number of received data elements.
  * @note   When the UART parity is enabled (PCE = 1), the received data contain
  *         the parity bit (MSB posit
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值