STM32CubeMX USART串口DMA,IDLE 空闲中断不定长接收详解

2024-04-05 05:18

本文主要是介绍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>&copy; 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>&copy; 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>&copy; 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 空闲中断不定长接收详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/877701

相关文章

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

详解C#如何提取PDF文档中的图片

《详解C#如何提取PDF文档中的图片》提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使用,下面我们就来看看如何使用C#通过代码从PDF文档中提取图片吧... 当 PDF 文件中包含有价值的图片,如艺术画作、设计素材、报告图表等,提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘

Java中StopWatch的使用示例详解

《Java中StopWatch的使用示例详解》stopWatch是org.springframework.util包下的一个工具类,使用它可直观的输出代码执行耗时,以及执行时间百分比,这篇文章主要介绍... 目录stopWatch 是org.springframework.util 包下的一个工具类,使用它

Java进行文件格式校验的方案详解

《Java进行文件格式校验的方案详解》这篇文章主要为大家详细介绍了Java中进行文件格式校验的相关方案,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、背景异常现象原因排查用户的无心之过二、解决方案Magandroidic Number判断主流检测库对比Tika的使用区分zip

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义

springboot security快速使用示例详解

《springbootsecurity快速使用示例详解》:本文主要介绍springbootsecurity快速使用示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录创www.chinasem.cn建spring boot项目生成脚手架配置依赖接口示例代码项目结构启用s

Python中随机休眠技术原理与应用详解

《Python中随机休眠技术原理与应用详解》在编程中,让程序暂停执行特定时间是常见需求,当需要引入不确定性时,随机休眠就成为关键技巧,下面我们就来看看Python中随机休眠技术的具体实现与应用吧... 目录引言一、实现原理与基础方法1.1 核心函数解析1.2 基础实现模板1.3 整数版实现二、典型应用场景2

一文详解SpringBoot响应压缩功能的配置与优化

《一文详解SpringBoot响应压缩功能的配置与优化》SpringBoot的响应压缩功能基于智能协商机制,需同时满足很多条件,本文主要为大家详细介绍了SpringBoot响应压缩功能的配置与优化,需... 目录一、核心工作机制1.1 自动协商触发条件1.2 压缩处理流程二、配置方案详解2.1 基础YAML