目录
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