串口通讯是嵌入式系统中最常用的通讯方式。
STM32的串口接收普通的方式是在串口读数据寄存器非空RXNE中断(Read data register not empty)中1个字节1个字节的接收串口数据,一帧数据的接收完成可以使用结束帧判断,也可以使用定时器计时当定时器溢出时认为一帧数据已经接收完成(一帧数据中各byte的时间间隔很短<1mS,因此可以将定时器设置为3mS溢出,在RXNE中断中清0定时器,若产生定时器溢出中断表明已经3mS没有接收到数据了,判定已结束一帧数据接收)。
如上方式每一个字节的接收都会触发RXNE中断进行接收字节的保存,因此若一直接收数据会占用较多的CPU时间,从而影响其它业务程序的执行。比较好的方式是使用DMA+串口空闲中断实现数据的接收,STM32只需要进一次中断即可。直接存储器存取(DMA)是用来提供在外设和存储器之间或者存储器和存储器之间的高速数据传输。无须CPU干预,数据可以通过DMA快速地移动,从而可以节省CPU的资源来做其他操作。
工具准备:
STM32CubeMX(Verison5.6.1)
KEIL5(uVision V5.26.2.0)
正点原子潘多拉IOT开发板(芯片型号:STM32L475VET6,也可以使用其它STM32芯片)
设计需求:
- LED状态指示灯指示系统运行状态(正常为闪烁)
- 可实现串口DMA数据接收,并将接收到的数据原封不动的返回
硬件配置:
- 使用板载的绿色LED灯作为系统运行状态指示灯,硬件引脚为PE8
- 使用潘多拉IOT开发板开发板的USART1,硬件引脚为PA9(TX),PA10(RX)
USART1配置如下:
波特率:128000bps
数据位:8
校验位:无
停止位:1
<Part 1 工程生成>
1、打开STM32CubeMX后选择Start My Project from MCU
2、根据芯片型号创建工程
3、选择系统时钟源为外部晶振(可自由配置)
这里需要解释的是选择外部高速时钟HSE时有2个时钟源选项。其中Crystal/Ceramic Resonator表示使用晶振/陶瓷振荡器,Bypass Clock Sourc表示使用旁路时钟源,即不使用晶振,外部直接供给一个可靠的时钟源。
4、配置程序的调试方式,一般选JTAG或SWD(根据自己项目的调试方式选就可以)
5、配置系统时钟频率,最终配置系统时钟频率为80MHz
6、配置USART1基本信息
7、配置USART1 DMA接收
需要配置的模式为USART1接收DMA,方向从外设到内存(即将USART1接收到的数据搬运到用户定义的内存空间中),数据宽度为1Byte,DMA的优先级设置为High,模式为Normal即搬运完用户指定的字节数后停止DMA工作,Circle循环模式则在搬运完成后又从头开始搬运,因此循环接收时会覆盖掉用户定义的内存空间中保存的之前接收到的数据。
8、设置USART1中断优先级
NVIC在此处默认为4bit为抢占优先级,0bit为从优先级,即不支持配置从优先级,抢占优先级的配置范围为0~15(0优先级为最高,此处将串口中断优先级设置为3,也算是优先级比较高了)
9、配置系统运行指示灯PE8的管脚信息
配置系统运行指示灯的意义在于,可以直观的观察到程序是否正常运行。这里需要注意的是,在GPIO端口被配置为输出时,其弱上拉和弱下拉电阻是不可用的。
10、配置工程生成相关配置
<Part 2 工程修改>
需要修改的文件有如下几个:
main.c
usart.c
usart.h
stm32l4xx_it.c
main.c中修改部分如下红框所示:
/* USER CODE BEGIN Header */
/********************************************************************************* @file : main.c* @brief : Main program body******************************************************************************* @attention** <h2><center>© Copyright (c) 2020 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********************************************************************************/
/* USER CODE END Header *//* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "usart.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes *//* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV */
/* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 *//* USER CODE END 0 *//*** @brief The application entry point.* @retval int*/
int main(void)
{/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_DMA_Init();MX_USART1_UART_Init();/* USER CODE BEGIN 2 */HAL_Delay(100); __HAL_UART_CLEAR_IDLEFLAG(&huart1); /* 使能IDLE中断前先清除其中断标志位,以免使能后就立刻进入中断 */ HAL_Delay(100); __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE); /* 使能IDLE串口空闲中断 */ /* USART1使能串口DMA接收,最多接收DMA_MAX_RECV_SIZE个字节,接收内容存储于gu8_dma_recv_buff_a缓冲区 */HAL_Delay(100); HAL_UART_Receive_DMA(&huart1, gu8_dma_recv_buff_a, DMA_MAX_RECV_SIZE); /* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */HAL_GPIO_TogglePin(LED_G_GPIO_Port, LED_G_Pin); /* 翻转状态指示灯引脚电平 */HAL_Delay(300); /* 每300mS翻转一次 */}/* USER CODE END 3 */
}/*** @brief System Clock Configuration* @retval None*/
void SystemClock_Config(void)
{RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};/** Initializes the CPU, AHB and APB busses clocks */RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;RCC_OscInitStruct.HSEState = RCC_HSE_ON;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;RCC_OscInitStruct.PLL.PLLM = 1;RCC_OscInitStruct.PLL.PLLN = 20;RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){Error_Handler();}/** Initializes the CPU, AHB and APB busses clocks */RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK){Error_Handler();}PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK){Error_Handler();}/** Configure the main internal regulator output voltage */if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK){Error_Handler();}
}/* USER CODE BEGIN 4 *//* USER CODE END 4 *//*** @brief This function is executed in case of error occurrence.* @retval None*/
void Error_Handler(void)
{/* USER CODE BEGIN Error_Handler_Debug *//* User can add his own implementation to report the HAL error return state *//* USER CODE END Error_Handler_Debug */
}#ifdef USE_FULL_ASSERT
/*** @brief Reports the name of the source file and the source line number* where the assert_param error has occurred.* @param file: pointer to the source file name* @param line: assert_param error line source number* @retval None*/
void assert_failed(uint8_t *file, uint32_t line)
{ /* USER CODE BEGIN 6 *//* User can add his own implementation to report the file name and line number,tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT *//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
usart.c中修改部分如下红框所示:
/********************************************************************************* File Name : USART.c* Description : This file provides code for the configuration* of the USART instances.******************************************************************************* @attention** <h2><center>© Copyright (c) 2020 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 */volatile uint8_t gu8_dma_recv_buff_a[DMA_MAX_RECV_SIZE] = {0}; /* DMA接收缓冲区 */
/* USER CODE END 0 */UART_HandleTypeDef huart1;
DMA_HandleTypeDef hdma_usart1_rx;/* USART1 init function */void MX_USART1_UART_Init(void)
{huart1.Instance = USART1;huart1.Init.BaudRate = 128000;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();}}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 = GPIO_PIN_9|GPIO_PIN_10;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;GPIO_InitStruct.Alternate = GPIO_AF7_USART1;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);/* USART1 DMA Init *//* USART1_RX Init */hdma_usart1_rx.Instance = DMA1_Channel5;hdma_usart1_rx.Init.Request = DMA_REQUEST_2;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 interrupt Init */HAL_NVIC_SetPriority(USART1_IRQn, 3, 0);HAL_NVIC_EnableIRQ(USART1_IRQn);/* USER CODE BEGIN USART1_MspInit 1 *//* USER CODE END USART1_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, GPIO_PIN_9|GPIO_PIN_10);/* USART1 DMA DeInit */HAL_DMA_DeInit(uartHandle->hdmarx);/* USART1 interrupt Deinit */HAL_NVIC_DisableIRQ(USART1_IRQn);/* USER CODE BEGIN USART1_MspDeInit 1 *//* USER CODE END USART1_MspDeInit 1 */}
} /* USER CODE BEGIN 1 *//*******************************************
* Function Name : UART_IDLE_Callback
* Creat Date : 2020/11/05
* Author/Corporation : Jason
* Description : usart idle callback function
* Para : huart: the usart user use
* Return Code : null
------------------------------
* Revision History
* Date Revised by Description
* 2020/11/05 Jason the first verison
*******************************************/
void UART_IDLE_Callback(UART_HandleTypeDef *huart)
{if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE)){/* 计算接收到的字节数 */uint8_t u8_recv_num = DMA_MAX_RECV_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx); __HAL_UART_CLEAR_IDLEFLAG(huart); /* 清除串口空闲中断标志位 */ HAL_UART_DMAStop(huart); /* 停止串口DMA接收 */ /* 使用阻塞发送的方式将接收到的数据原封不动的返回至上位机 */HAL_UART_Transmit(&huart1, gu8_dma_recv_buff_a, u8_recv_num, 1000);/* 重新使能USART1 DMA接收,最多接收DMA_MAX_RECV_SIZE个字节,接收内容存储于gu8_dma_recv_buff_a缓冲区 */HAL_UART_Receive_DMA(&huart1, gu8_dma_recv_buff_a, DMA_MAX_RECV_SIZE); }
}
/* 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) 2020 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;/* USER CODE BEGIN Private defines */
#define DMA_MAX_RECV_SIZE 128 /* DMA接收数据最大长度 */
/* USER CODE END Private defines */void MX_USART1_UART_Init(void);/* USER CODE BEGIN Prototypes */
extern volatile uint8_t gu8_dma_recv_buff_a[DMA_MAX_RECV_SIZE];
/* USER CODE END Prototypes */#ifdef __cplusplus
}
#endif
#endif /*__ usart_H *//*** @}*//*** @}*//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
stm32l4xx_it.c中修改部分如下红框所示:
/* USER CODE BEGIN Header */
/********************************************************************************* @file stm32l4xx_it.c* @brief Interrupt Service Routines.******************************************************************************* @attention** <h2><center>© Copyright (c) 2020 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********************************************************************************/
/* USER CODE END Header *//* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32l4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD *//* USER CODE END TD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 *//* USER CODE END 0 *//* External variables --------------------------------------------------------*/
extern DMA_HandleTypeDef hdma_usart1_rx;
extern UART_HandleTypeDef huart1;
/* USER CODE BEGIN EV */
extern void UART_IDLE_Callback(UART_HandleTypeDef *huart); /* 串口空闲中断回调函数 */
/* USER CODE END EV *//******************************************************************************/
/* Cortex-M4 Processor Interruption and Exception Handlers */
/******************************************************************************/
/*** @brief This function handles Non maskable interrupt.*/
void NMI_Handler(void)
{/* USER CODE BEGIN NonMaskableInt_IRQn 0 *//* USER CODE END NonMaskableInt_IRQn 0 *//* USER CODE BEGIN NonMaskableInt_IRQn 1 *//* USER CODE END NonMaskableInt_IRQn 1 */
}/*** @brief This function handles Hard fault interrupt.*/
void HardFault_Handler(void)
{/* USER CODE BEGIN HardFault_IRQn 0 *//* USER CODE END HardFault_IRQn 0 */while (1){/* USER CODE BEGIN W1_HardFault_IRQn 0 *//* USER CODE END W1_HardFault_IRQn 0 */}
}/*** @brief This function handles Memory management fault.*/
void MemManage_Handler(void)
{/* USER CODE BEGIN MemoryManagement_IRQn 0 *//* USER CODE END MemoryManagement_IRQn 0 */while (1){/* USER CODE BEGIN W1_MemoryManagement_IRQn 0 *//* USER CODE END W1_MemoryManagement_IRQn 0 */}
}/*** @brief This function handles Prefetch fault, memory access fault.*/
void BusFault_Handler(void)
{/* USER CODE BEGIN BusFault_IRQn 0 *//* USER CODE END BusFault_IRQn 0 */while (1){/* USER CODE BEGIN W1_BusFault_IRQn 0 *//* USER CODE END W1_BusFault_IRQn 0 */}
}/*** @brief This function handles Undefined instruction or illegal state.*/
void UsageFault_Handler(void)
{/* USER CODE BEGIN UsageFault_IRQn 0 *//* USER CODE END UsageFault_IRQn 0 */while (1){/* USER CODE BEGIN W1_UsageFault_IRQn 0 *//* USER CODE END W1_UsageFault_IRQn 0 */}
}/*** @brief This function handles System service call via SWI instruction.*/
void SVC_Handler(void)
{/* USER CODE BEGIN SVCall_IRQn 0 *//* USER CODE END SVCall_IRQn 0 *//* USER CODE BEGIN SVCall_IRQn 1 *//* USER CODE END SVCall_IRQn 1 */
}/*** @brief This function handles Debug monitor.*/
void DebugMon_Handler(void)
{/* USER CODE BEGIN DebugMonitor_IRQn 0 *//* USER CODE END DebugMonitor_IRQn 0 *//* USER CODE BEGIN DebugMonitor_IRQn 1 *//* USER CODE END DebugMonitor_IRQn 1 */
}/*** @brief This function handles Pendable request for system service.*/
void PendSV_Handler(void)
{/* USER CODE BEGIN PendSV_IRQn 0 *//* USER CODE END PendSV_IRQn 0 *//* USER CODE BEGIN PendSV_IRQn 1 *//* USER CODE END PendSV_IRQn 1 */
}/*** @brief This function handles System tick timer.*/
void SysTick_Handler(void)
{/* USER CODE BEGIN SysTick_IRQn 0 *//* USER CODE END SysTick_IRQn 0 */HAL_IncTick();/* USER CODE BEGIN SysTick_IRQn 1 *//* USER CODE END SysTick_IRQn 1 */
}/******************************************************************************/
/* STM32L4xx Peripheral Interrupt Handlers */
/* Add here the Interrupt Handlers for the used peripherals. */
/* For the available peripheral interrupt handler names, */
/* please refer to the startup file (startup_stm32l4xx.s). */
/******************************************************************************//*** @brief This function handles DMA1 channel5 global interrupt.*/
void DMA1_Channel5_IRQHandler(void)
{/* USER CODE BEGIN DMA1_Channel5_IRQn 0 *//* USER CODE END DMA1_Channel5_IRQn 0 */HAL_DMA_IRQHandler(&hdma_usart1_rx);/* USER CODE BEGIN DMA1_Channel5_IRQn 1 *//* USER CODE END DMA1_Channel5_IRQn 1 */
}/*** @brief This function handles USART1 global interrupt.*/
void USART1_IRQHandler(void)
{/* USER CODE BEGIN USART1_IRQn 0 *//* HAL_UART_IRQHandler中不包括串口空闲中断回调函数,因此需要自己实现UART_IDLE_Callback */UART_IDLE_Callback(&huart1); /* USER CODE END USART1_IRQn 0 */HAL_UART_IRQHandler(&huart1);/* USER CODE BEGIN USART1_IRQn 1 *//* USER CODE END USART1_IRQn 1 */
}/* USER CODE BEGIN 1 *//* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
修改完成后编译,下载到开发板中。
<Part 3 试验部分>
使用正点原子串口调试助手和开发板进行通讯,发现可以将发送的内容原封不动的返回至上位机,符合程序设计意图,成功实现DMA+串口空闲中断接收!
实验结果如下图所示: