在使用STM32F103做串口收发时,如果采用中断方式可能会丢包,但是采用DMA方式就可以避免这种问题,案例就是用DMA方式传输数据
DMA是一种不使用CPU而将数据从一片地址空间复制到另一片地址空间的总线,这样就减少了CPU的负担,使其能够更加专注于数据运算。
为了能够减少CPU的负担,DMA应该采取中断方式而非查询模式。
但是非常不幸的是,STM32F103只为DMA提供了三种中断:半步中断、完成中断和错误中断。
如果UART接收的是定帧长的数据,则可以开启DMA半步中断,并且目标地址长度为帧长两倍。这样每接收完一帧进一次中断,进行某些操作,是很理想的。
然而当遇到如同GPS一样不定帧长的数据时,如果仍用半步中断则难以确定目标地址的长度。
所以需要放弃使用DMA的中断,转而使用的是另一种比较特别的中断:UART空闲中断。
先来介绍一下UART空闲中断。
UART常用的接收中断响应有:接收数据 就绪可读中断RXNE(这是最常用的)、数据溢出中断(ORE)、奇偶校验错中断(PE)和空闲中断(IDLE)。
空闲中断是指当总线检测到一帧发完后, 总线空闲则会将此位置一,如果USART_CR1中的IDLEIE为’1’,则产生中断。
空闲中断有两个比较有意思的特点:
1、清零方式:软件清零,先读USART_SR,然后读USART_DR
2、直到RXNE被置一后IDLE才能被重读置一,即IDLE被置一并软件清零后,只有之后再次接收到数据,IDLE才能被置一。
这样就防止了总线长时间空闲而多次引发空闲中断。
单片机源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32f1xx_hal.h"
- #include "dma.h"
- #include "tim.h"
- #include "usart.h"
- #include "gpio.h"
- /* USER CODE BEGIN Includes */
- #include "stdarg.h"
- #include "string.h"
- //#include <stdlib.h>
- #ifdef __GNUC__
- /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
- set to 'Yes') calls __io_putchar() */
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
- #else
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
- #endif /* __GNUC__ */
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- extern UART_HandleTypeDef huart1;
- extern DMA_HandleTypeDef hdma_usart1_rx;
- extern DMA_HandleTypeDef hdma_usart1_tx;
- /* Private variables ---------------------------------------------------------*/
- #define BUFFER_SIZE 100
- uint8_t rx_len=0; //接收一帧数据的长度
- uint8_t recv_end_flag=0; //一帧数据接收完成标志
- uint8_t rx_buffer[BUFFER_SIZE]={"123"}; //接收数据缓存
- uint8_t tx_buffer[BUFFER_SIZE];
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- *
- * @retval None
- */
- 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_TIM2_Init();
- MX_USART1_UART_Init();
- /* USER CODE BEGIN 2 */
- //printf("tst uart DMA begin\r\n");
- //__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
- __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE); //使能IDLE中断
- HAL_UART_Receive_DMA(&huart1,rx_buffer,BUFFER_SIZE);//启动DMA接收
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- HAL_GPIO_WritePin(Tst_Pin_GPIO_Port, Tst_Pin_Pin, GPIO_PIN_RESET);
-
- if(recv_end_flag ==1)
- {
- HAL_UART_Transmit(&huart1,rx_buffer, rx_len,200);//接收数据打印出来
- rx_len=0;
- recv_end_flag=0;
- //memset(rx_buffer,0x00,BUFFER_SIZE);
- HAL_UART_Receive_DMA(&huart1,rx_buffer,BUFFER_SIZE); //重启DMA接收
- }
- HAL_GPIO_WritePin(Tst_Pin_GPIO_Port, Tst_Pin_Pin, GPIO_PIN_SET);
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
-
-
- /* USER CODE END 3 */
- }
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = 16;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**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_HSI;
- 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_0) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- HAL_RCC_MCOConfig(RCC_MCO, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1);
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* USER CODE BEGIN 4 */
- void UART_IDLE_Callback(UART_HandleTypeDef *huart)
- {
- uint16_t i = 0;
- if((__HAL_UART_GET_FLAG(huart,UART_FLAG_IDLE) != RESET))
- {
- if(huart->Instance == USART1)
- {
- __HAL_UART_CLEAR_IDLEFLAG(huart);
- i = huart->Instance->SR;
- i = huart->Instance->DR;
- i = hdma_usart1_rx.Instance->CNDTR; //读取DMA剩余传输数量
- HAL_UART_DMAStop(huart);
-
- /* 此处处理数据,主要是拷贝和置位标志位 */
- if((recv_end_flag == 0)&&(i!=BUFFER_SIZE))
- {
- rx_len = BUFFER_SIZE - i;
- //memcpy(tx_buffer,rx_buffer,rx_len);
- recv_end_flag = 1;
- }
- else
- {
- /* 清空缓存,重新接收 */
- //memset(rx_buffer,0x00,BUFFER_SIZE);
- HAL_UART_Receive_DMA(huart,(uint8_t *)&rx_buffer,BUFFER_SIZE);
- }
- }
- }
-
- }
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @param file: The file name as string.
- * @param line: The line in file as a number.
- * @retval None
- */
- void _Error_Handler(char *file, int line)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- while(1)
- {
- }
- /* 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,
- ex: 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****/
复制代码
所有资料51hei提供下载:
103_UARTDMA.rar
(445.2 KB, 下载次数: 103)
|