本文主要是介绍STM32CubeMX USART串口DMA,IDLE 空闲中断不定长接收详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用的STM32CubeMX版本为:
首先是串口配置:
补充DMA配置,之前漏了,这里用最新版本的CUBEMX的截图补充一下:
中断配置
DMA的模式选择NORMAL就行
这里默认就好~
大功告成之后生成工程代码~
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
HAL_UART_Receive_DMA(&huart1,RxDMABuf_1,RXBUF_1_SIZE);
需要自行添加在void MX_USART1_UART_Init(void)内。其中RxDMABuf_1与RXBUF_1_SIZE是自定义的接收缓冲数组与数组大小。
void MX_USART1_UART_Init(void)
{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;huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;if (HAL_UART_Init(&huart1) != HAL_OK){_Error_Handler(__FILE__, __LINE__);}__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);//使能idle中断HAL_UART_Receive_DMA(&huart1,RxDMABuf_1,RXBUF_1_SIZE);//打开DMA接收,数据存入rx_buffer数组中。
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{GPIO_InitTypeDef GPIO_InitStruct;if(uartHandle->Instance==USART1){/* USER CODE BEGIN USART1_MspInit 0 *//* USER CODE END USART1_MspInit 0 *//* USART1 clock enable */__HAL_RCC_USART1_CLK_ENABLE();/**USART1 GPIO Configuration PA9 ------> USART1_TXPA10 ------> USART1_RX */GPIO_InitStruct.Pin = IOT_TX_Pin | IOT_RX_Pin;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_PULLUP;//GPIO_PULLUP;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;GPIO_InitStruct.Alternate = GPIO_AF1_USART1;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);/* USART1 DMA Init *//* USART1_RX Init */hdma_usart1_rx.Instance = DMA1_Channel3;hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;hdma_usart1_rx.Init.Mode = DMA_NORMAL;hdma_usart1_rx.Init.Priority = DMA_PRIORITY_HIGH;if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK){_Error_Handler(__FILE__, __LINE__);}__HAL_LINKDMA(uartHandle,hdmarx,hdma_usart1_rx);/* USART1_TX Init */hdma_usart1_tx.Instance = DMA1_Channel2;hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE;hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE;hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;hdma_usart1_tx.Init.Mode = DMA_NORMAL;hdma_usart1_tx.Init.Priority = DMA_PRIORITY_HIGH;if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK){_Error_Handler(__FILE__, __LINE__);}__HAL_LINKDMA(uartHandle,hdmatx,hdma_usart1_tx);/* USART1 interrupt Init */HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);HAL_NVIC_EnableIRQ(USART1_IRQn);}}
自定义空闲中断回调函数,放在void USART1_IRQHandler(void)内。
void UART_IDLE_Callback(UART_HandleTypeDef *huart)
{uint32_t tmp1;uint32_t temp;tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE);if( tmp1 != RESET){__HAL_UART_CLEAR_IDLEFLAG(huart);//清除标志位temp = huart->Instance->ISR; //清除状态寄存器SR,读取SR寄存器可以实现清除SR寄存器的功能temp = huart->Instance->RDR; //读取数据寄存器中的数据HAL_UART_DMAStop(huart); //if(huart->Instance == USART1){/* get rx data len */DMA_Usart1_RxSize = RXBUF_1_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);// 获取DMA中传输的数据个数//DMA_Usart1_RxSize = RXBUF_1_SIZE - huart->hdmarx->Instance->CNDTR; //获取DMA数据长度if(RxBufSize_1 == 0 && DMA_Usart1_RxSize != 0){memcpy(RxBuf_1,RxDMABuf_1,DMA_Usart1_RxSize);RxBufSize_1 = DMA_Usart1_RxSize;}HAL_UART_Receive_DMA(&huart1,RxDMABuf_1,RXBUF_1_SIZE);//打开DMA接收,数据存入rx_buffer数组中。 }}
}
自定义发送和接收函数:
uint8_t Uart_GetRxSize(UART_HandleTypeDef *huart,uint8_t *buf)
{uint8_t Size;if(huart->Instance == USART1){if(RxBufSize_1 > 0){Size = RxBufSize_1;memcpy(buf,RxDMABuf_1,RxBufSize_1);RxBufSize_1 = 0;return Size;}}return 0;
}uint8_t Uart_SendData(UART_HandleTypeDef *huart,uint8_t *buf,uint8_t Size)
{static uint8_t DMA_TX_BUF_1[RXBUF_1_SIZE] = {0};if(Size == 0 )return 0;if(huart->Instance == USART1 && (huart->hdmatx->Instance->CNDTR == 0) && Size <RXBUF_1_SIZE ){memcpy(DMA_TX_BUF_1,buf,Size);HAL_UART_Transmit_DMA(&huart1,DMA_TX_BUF_1,Size);return 1;}return 0;
}
亲测可用~
阿里云幸运卷,戳我领取!
忽然发现HAL库的串口DMA接收很容易受到异常数据的干扰,导致无法再次进入DMA中断,现得到解决办法,贴在另一个文章中:
https://blog.csdn.net/tiantangmoke/article/details/103308851
隔了一段时间,在这里贴上完整代码,版本不同可能略有不同。
使用的是stm32F030的串口1和串口2
usart.c
/********************************************************************************* File Name : USART.c* Description : This file provides code for the configuration* of the USART instances.******************************************************************************* @attention** <h2><center>© Copyright (c) 2019 STMicroelectronics.* All rights reserved.</center></h2>** This software component is licensed by ST under BSD 3-Clause license,* the "License"; You may not use this file except in compliance with the* License. You may obtain a copy of the License at:* opensource.org/licenses/BSD-3-Clause********************************************************************************//* Includes ------------------------------------------------------------------*/
#include "usart.h"/* USER CODE BEGIN 0 */
#include <string.h>
#define RXBUF_1_SIZE 255
#define RXBUF_2_SIZE 255
volatile uint8_t DMA_Usart1_RxSize=0;
volatile uint8_t DMA_Usart2_RxSize=0;
volatile uint8_t recv_end_flag=0;
uint8_t RxDMABuf_1[RXBUF_1_SIZE];
uint8_t RxDMABuf_2[RXBUF_2_SIZE];volatile uint8_t RxBuf_1_LOCK = 0;
volatile uint8_t RxBuf_2_LOCK = 0;
uint8_t RxBuf_1[RXBUF_1_SIZE];
uint8_t RxBuf_2[RXBUF_2_SIZE];
volatile uint8_t RxBufSize_1 = 0;
volatile uint8_t RxBufSize_2 = 0;
/* USER CODE END 0 */UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
DMA_HandleTypeDef hdma_usart1_rx;
DMA_HandleTypeDef hdma_usart1_tx;
DMA_HandleTypeDef hdma_usart2_rx;
DMA_HandleTypeDef hdma_usart2_tx;/* USART1 init function */void MX_USART1_UART_Init(void)
{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;huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;if (HAL_UART_Init(&huart1) != HAL_OK){Error_Handler();}__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);//ê1?üidle?D??HAL_UART_Receive_DMA(&huart1,RxDMABuf_1,RXBUF_1_SIZE);//′ò?aDMA?óê?£?êy?Y′?è?rx_bufferêy×é?D?£
}
/* USART2 init function */void MX_USART2_UART_Init(void)
{huart2.Instance = USART2;huart2.Init.BaudRate = 9600;huart2.Init.WordLength = UART_WORDLENGTH_8B;huart2.Init.StopBits = UART_STOPBITS_1;huart2.Init.Parity = UART_PARITY_NONE;huart2.Init.Mode = UART_MODE_TX_RX;huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;huart2.Init.OverSampling = UART_OVERSAMPLING_16;huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;if (HAL_UART_Init(&huart2) != HAL_OK){Error_Handler();}__HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);//ê1?üidle?D??HAL_UART_Receive_DMA(&huart2,RxDMABuf_2,RXBUF_2_SIZE);}void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{GPIO_InitTypeDef GPIO_InitStruct = {0};if(uartHandle->Instance==USART1){/* USER CODE BEGIN USART1_MspInit 0 *//* USER CODE END USART1_MspInit 0 *//* USART1 clock enable */__HAL_RCC_USART1_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();/**USART1 GPIO Configuration PA9 ------> USART1_TXPA10 ------> USART1_RX */GPIO_InitStruct.Pin = IOT_TX_Pin|IOT_RX_Pin;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_PULLUP;//GPIO_PULLUP;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;GPIO_InitStruct.Alternate = GPIO_AF1_USART1;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);/* USART1 DMA Init *//* USART1_RX Init */hdma_usart1_rx.Instance = DMA1_Channel3;hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;hdma_usart1_rx.Init.Mode = DMA_NORMAL;hdma_usart1_rx.Init.Priority = DMA_PRIORITY_HIGH;if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK){Error_Handler();}__HAL_LINKDMA(uartHandle,hdmarx,hdma_usart1_rx);/* USART1_TX Init */hdma_usart1_tx.Instance = DMA1_Channel2;hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE;hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE;hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;hdma_usart1_tx.Init.Mode = DMA_NORMAL;hdma_usart1_tx.Init.Priority = DMA_PRIORITY_HIGH;if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK){Error_Handler();}__HAL_LINKDMA(uartHandle,hdmatx,hdma_usart1_tx);/* USART1 interrupt Init */HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);HAL_NVIC_EnableIRQ(USART1_IRQn);/* USER CODE BEGIN USART1_MspInit 1 *//* USER CODE END USART1_MspInit 1 */}else if(uartHandle->Instance==USART2){/* USER CODE BEGIN USART2_MspInit 0 *//* USER CODE END USART2_MspInit 0 *//* USART2 clock enable */__HAL_RCC_USART2_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();/**USART2 GPIO Configuration PA2 ------> USART2_TXPA3 ------> USART2_RX */GPIO_InitStruct.Pin = RS485_TX_Pin|RS485_RX_Pin;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_PULLUP;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;GPIO_InitStruct.Alternate = GPIO_AF1_USART2;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);/* USART2 DMA Init *//* USART2_RX Init */hdma_usart2_rx.Instance = DMA1_Channel5;hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE;hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE;hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;hdma_usart2_rx.Init.Mode = DMA_NORMAL;hdma_usart2_rx.Init.Priority = DMA_PRIORITY_HIGH;if (HAL_DMA_Init(&hdma_usart2_rx) != HAL_OK){Error_Handler();}__HAL_LINKDMA(uartHandle,hdmarx,hdma_usart2_rx);/* USART2_TX Init */hdma_usart2_tx.Instance = DMA1_Channel4;hdma_usart2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;hdma_usart2_tx.Init.PeriphInc = DMA_PINC_DISABLE;hdma_usart2_tx.Init.MemInc = DMA_MINC_ENABLE;hdma_usart2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;hdma_usart2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;hdma_usart2_tx.Init.Mode = DMA_NORMAL;hdma_usart2_tx.Init.Priority = DMA_PRIORITY_HIGH;if (HAL_DMA_Init(&hdma_usart2_tx) != HAL_OK){Error_Handler();}__HAL_LINKDMA(uartHandle,hdmatx,hdma_usart2_tx);/* USART2 interrupt Init */HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);HAL_NVIC_EnableIRQ(USART2_IRQn);/* USER CODE BEGIN USART2_MspInit 1 *//* USER CODE END USART2_MspInit 1 */}
}void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{if(uartHandle->Instance==USART1){/* USER CODE BEGIN USART1_MspDeInit 0 *//* USER CODE END USART1_MspDeInit 0 *//* Peripheral clock disable */__HAL_RCC_USART1_CLK_DISABLE();/**USART1 GPIO Configuration PA9 ------> USART1_TXPA10 ------> USART1_RX */HAL_GPIO_DeInit(GPIOA, IOT_TX_Pin|IOT_RX_Pin);/* USART1 DMA DeInit */HAL_DMA_DeInit(uartHandle->hdmarx);HAL_DMA_DeInit(uartHandle->hdmatx);/* USART1 interrupt Deinit */HAL_NVIC_DisableIRQ(USART1_IRQn);/* USER CODE BEGIN USART1_MspDeInit 1 *//* USER CODE END USART1_MspDeInit 1 */}else if(uartHandle->Instance==USART2){/* USER CODE BEGIN USART2_MspDeInit 0 *//* USER CODE END USART2_MspDeInit 0 *//* Peripheral clock disable */__HAL_RCC_USART2_CLK_DISABLE();/**USART2 GPIO Configuration PA2 ------> USART2_TXPA3 ------> USART2_RX */HAL_GPIO_DeInit(GPIOA, RS485_TX_Pin|RS485_RX_Pin);/* USART2 DMA DeInit */HAL_DMA_DeInit(uartHandle->hdmarx);HAL_DMA_DeInit(uartHandle->hdmatx);/* USART2 interrupt Deinit */HAL_NVIC_DisableIRQ(USART2_IRQn);/* USER CODE BEGIN USART2_MspDeInit 1 *//* USER CODE END USART2_MspDeInit 1 */}
} /* USER CODE BEGIN 1 */
void UART_IDLE_Callback(UART_HandleTypeDef *huart)
{uint32_t temp;if( __HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET){__HAL_UART_CLEAR_IDLEFLAG(huart);//??3y±ê????temp = huart->Instance->ISR; //??3y×′ì???′??÷SR,?áè?SR??′??÷?éò?êμ????3ySR??′??÷μ?1|?ütemp = huart->Instance->RDR; //?áè?êy?Y??′??÷?Dμ?êy?Ytemp = temp;HAL_UART_DMAStop(huart); //if(huart->Instance == USART1){/* get rx data len */DMA_Usart1_RxSize = RXBUF_1_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);// ??è?DMA?D′?ê?μ?êy?Y??êy//DMA_Usart1_RxSize = RXBUF_1_SIZE - huart->hdmarx->Instance->CNDTR; //??è?DMAêy?Y3¤?èif( DMA_Usart1_RxSize > 1 && RxBuf_1_LOCK == 0){memcpy(RxBuf_1 + RxBufSize_1 ,RxDMABuf_1,DMA_Usart1_RxSize);RxBufSize_1 += DMA_Usart1_RxSize; }HAL_UART_Receive_DMA(&huart1,RxDMABuf_1,RXBUF_1_SIZE);//′ò?aDMA?óê?£?êy?Y′?è?rx_bufferêy×é?D?£ }if(huart->Instance == USART2){/* get rx data len */DMA_Usart2_RxSize = RXBUF_2_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart2_rx);// ??è?DMA?D′?ê?μ?êy?Y??êyif(DMA_Usart2_RxSize > 1 && RxBuf_2_LOCK == 0){memcpy(RxBuf_2 + RxBufSize_2,RxDMABuf_2,DMA_Usart2_RxSize);RxBufSize_2 += DMA_Usart2_RxSize;}HAL_UART_Receive_DMA(&huart2,RxDMABuf_2,RXBUF_2_SIZE);//′ò?aDMA?óê?£?êy?Y′?è?rx_bufferêy×é?D?£ }}
}uint8_t Uart_GetRxSize(UART_HandleTypeDef *huart,uint8_t *buf)
{uint8_t Size = 0;if(huart->Instance == USART1){RxBuf_1_LOCK = 1; //?ó??£?ò??a?ú′|àíêy?Yμ?ê±oò£?′??ú???D?D???ü??á?êy?Yif(RxBufSize_1 > 0){Size = RxBufSize_1;RxBuf_1[RxBufSize_1] = 0;memcpy(buf,RxBuf_1,RxBufSize_1);RxBufSize_1 = 0;}RxBuf_1_LOCK = 0;}if(huart->Instance == USART2){RxBuf_2_LOCK = 1;if(RxBufSize_2 > 0){Size = RxBufSize_2;memcpy(buf,RxBuf_2,RxBufSize_2);RxBufSize_2 = 0;}RxBuf_2_LOCK = 0;}return Size;
}uint8_t Uart_SendData(UART_HandleTypeDef *huart,uint8_t *buf,uint8_t Size)
{static uint8_t DMA_TX_BUF_1[RXBUF_1_SIZE] = {0};static uint8_t DMA_TX_BUF_2[RXBUF_2_SIZE] = {0};if(Size == 0 )return 0;if(huart->Instance == USART1 && (huart->hdmatx->Instance->CNDTR == 0) && Size <RXBUF_1_SIZE ){memcpy(DMA_TX_BUF_1,buf,Size);HAL_UART_Transmit_DMA(&huart1,DMA_TX_BUF_1,Size);return 1;}if(huart->Instance == USART2 && (huart->hdmatx->Instance->CNDTR == 0) && Size <RXBUF_2_SIZE ){memcpy(DMA_TX_BUF_2,buf,Size);HAL_UART_Transmit_DMA(&huart2,DMA_TX_BUF_2,Size);return 1;}return 0;
}
/* USER CODE END 1 *//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
usart.h
/********************************************************************************* File Name : USART.h* Description : This file provides code for the configuration* of the USART instances.******************************************************************************* @attention** <h2><center>© Copyright (c) 2019 STMicroelectronics.* All rights reserved.</center></h2>** This software component is licensed by ST under BSD 3-Clause license,* the "License"; You may not use this file except in compliance with the* License. You may obtain a copy of the License at:* opensource.org/licenses/BSD-3-Clause********************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __usart_H
#define __usart_H
#ifdef __cplusplusextern "C" {
#endif/* Includes ------------------------------------------------------------------*/
#include "main.h"/* USER CODE BEGIN Includes *//* USER CODE END Includes */extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;/* USER CODE BEGIN Private defines *//* USER CODE END Private defines */void MX_USART1_UART_Init(void);
void MX_USART2_UART_Init(void);
void UART_IDLE_Callback(UART_HandleTypeDef *huart);
uint8_t Uart_SendData(UART_HandleTypeDef *huart,uint8_t *buf,uint8_t Size);
uint8_t Uart_GetRxSize(UART_HandleTypeDef *huart,uint8_t *buf);/* USER CODE BEGIN Prototypes *//* USER CODE END Prototypes */#ifdef __cplusplus
}
#endif
#endif /*__ usart_H *//*** @}*//*** @}*//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
stm32xxxx_it.c
/*** @brief This function handles USART1 global interrupt.*/
void USART1_IRQHandler(void)
{/* USER CODE BEGIN USART1_IRQn 0 *//* USER CODE END USART1_IRQn 0 */HAL_UART_IRQHandler(&huart1);/* USER CODE BEGIN USART1_IRQn 1 */UART_IDLE_Callback(&huart1);/* USER CODE END USART1_IRQn 1 */
}/*** @brief This function handles USART2 global interrupt.*/
void USART2_IRQHandler(void)
{/* USER CODE BEGIN USART2_IRQn 0 *//* USER CODE END USART2_IRQn 0 */HAL_UART_IRQHandler(&huart2);/* USER CODE BEGIN USART2_IRQn 1 */UART_IDLE_Callback(&huart2);/* USER CODE END USART2_IRQn 1 */
}
dma.c
/********************************************************************************* File Name : dma.c* Description : This file provides code for the configuration* of all the requested memory to memory DMA transfers.******************************************************************************* @attention** <h2><center>© Copyright (c) 2019 STMicroelectronics.* All rights reserved.</center></h2>** This software component is licensed by ST under BSD 3-Clause license,* the "License"; You may not use this file except in compliance with the* License. You may obtain a copy of the License at:* opensource.org/licenses/BSD-3-Clause********************************************************************************//* Includes ------------------------------------------------------------------*/
#include "dma.h"/* USER CODE BEGIN 0 *//* USER CODE END 0 *//*----------------------------------------------------------------------------*/
/* Configure DMA */
/*----------------------------------------------------------------------------*//* USER CODE BEGIN 1 *//* USER CODE END 1 *//** * Enable DMA controller clock*/
void MX_DMA_Init(void)
{/* DMA controller clock enable */__HAL_RCC_DMA1_CLK_ENABLE();/* DMA interrupt init *//* DMA1_Channel1_IRQn interrupt configuration */HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);/* DMA1_Channel2_3_IRQn interrupt configuration */HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0, 0);HAL_NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);/* DMA1_Channel4_5_IRQn interrupt configuration */HAL_NVIC_SetPriority(DMA1_Channel4_5_IRQn, 0, 0);HAL_NVIC_EnableIRQ(DMA1_Channel4_5_IRQn);}/* USER CODE BEGIN 2 *//* USER CODE END 2 *//*** @}*//*** @}*//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
这篇关于STM32CubeMX USART串口DMA,IDLE 空闲中断不定长接收详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!