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

相关文章

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

第10章 中断和动态时钟显示

第10章 中断和动态时钟显示 从本章开始,按照书籍的划分,第10章开始就进入保护模式(Protected Mode)部分了,感觉从这里开始难度突然就增加了。 书中介绍了为什么有中断(Interrupt)的设计,中断的几种方式:外部硬件中断、内部中断和软中断。通过中断做了一个会走的时钟和屏幕上输入字符的程序。 我自己理解中断的一些作用: 为了更好的利用处理器的性能。协同快速和慢速设备一起工作

OpenHarmony鸿蒙开发( Beta5.0)无感配网详解

1、简介 无感配网是指在设备联网过程中无需输入热点相关账号信息,即可快速实现设备配网,是一种兼顾高效性、可靠性和安全性的配网方式。 2、配网原理 2.1 通信原理 手机和智能设备之间的信息传递,利用特有的NAN协议实现。利用手机和智能设备之间的WiFi 感知订阅、发布能力,实现了数字管家应用和设备之间的发现。在完成设备间的认证和响应后,即可发送相关配网数据。同时还支持与常规Sof

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

K8S(Kubernetes)开源的容器编排平台安装步骤详解

K8S(Kubernetes)是一个开源的容器编排平台,用于自动化部署、扩展和管理容器化应用程序。以下是K8S容器编排平台的安装步骤、使用方式及特点的概述: 安装步骤: 安装Docker:K8S需要基于Docker来运行容器化应用程序。首先要在所有节点上安装Docker引擎。 安装Kubernetes Master:在集群中选择一台主机作为Master节点,安装K8S的控制平面组件,如AP

嵌入式Openharmony系统构建与启动详解

大家好,今天主要给大家分享一下,如何构建Openharmony子系统以及系统的启动过程分解。 第一:OpenHarmony系统构建      首先熟悉一下,构建系统是一种自动化处理工具的集合,通过将源代码文件进行一系列处理,最终生成和用户可以使用的目标文件。这里的目标文件包括静态链接库文件、动态链接库文件、可执行文件、脚本文件、配置文件等。      我们在编写hellowor

LabVIEW FIFO详解

在LabVIEW的FPGA开发中,FIFO(先入先出队列)是常用的数据传输机制。通过配置FIFO的属性,工程师可以在FPGA和主机之间,或不同FPGA VIs之间进行高效的数据传输。根据具体需求,FIFO有多种类型与实现方式,包括目标范围内FIFO(Target-Scoped)、DMA FIFO以及点对点流(Peer-to-Peer)。 FIFO类型 **目标范围FIFO(Target-Sc

019、JOptionPane类的常用静态方法详解

目录 JOptionPane类的常用静态方法详解 1. showInputDialog()方法 1.1基本用法 1.2带有默认值的输入框 1.3带有选项的输入对话框 1.4自定义图标的输入对话框 2. showConfirmDialog()方法 2.1基本用法 2.2自定义按钮和图标 2.3带有自定义组件的确认对话框 3. showMessageDialog()方法 3.1

脏页的标记方式详解

脏页的标记方式 一、引言 在数据库系统中,脏页是指那些被修改过但还未写入磁盘的数据页。为了有效地管理这些脏页并确保数据的一致性,数据库需要对脏页进行标记。了解脏页的标记方式对于理解数据库的内部工作机制和优化性能至关重要。 二、脏页产生的过程 当数据库中的数据被修改时,这些修改首先会在内存中的缓冲池(Buffer Pool)中进行。例如,执行一条 UPDATE 语句修改了某一行数据,对应的缓

OmniGlue论文详解(特征匹配)

OmniGlue论文详解(特征匹配) 摘要1. 引言2. 相关工作2.1. 广义局部特征匹配2.2. 稀疏可学习匹配2.3. 半稠密可学习匹配2.4. 与其他图像表示匹配 3. OmniGlue3.1. 模型概述3.2. OmniGlue 细节3.2.1. 特征提取3.2.2. 利用DINOv2构建图形。3.2.3. 信息传播与新的指导3.2.4. 匹配层和损失函数3.2.5. 与Super