找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4642|回复: 0
收起左侧

STM32F103 UART1 DMA不定长收发源码

[复制链接]
ID:81176 发表于 2018-12-12 17:14 | 显示全部楼层 |阅读模式
在使用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才能被置一。
   这样就防止了总线长时间空闲而多次引发空闲中断。

单片机源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "stm32f1xx_hal.h"
  4. #include "dma.h"
  5. #include "tim.h"
  6. #include "usart.h"
  7. #include "gpio.h"

  8. /* USER CODE BEGIN Includes */
  9. #include "stdarg.h"
  10. #include "string.h"
  11. //#include <stdlib.h>
  12. #ifdef __GNUC__

  13.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  14. set to 'Yes') calls __io_putchar() */
  15. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  16. #else

  17.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  18. #endif /* __GNUC__ */
  19. /* USER CODE END Includes */

  20. /* Private variables ---------------------------------------------------------*/

  21. /* USER CODE BEGIN PV */
  22. extern UART_HandleTypeDef huart1;
  23. extern DMA_HandleTypeDef hdma_usart1_rx;
  24. extern DMA_HandleTypeDef hdma_usart1_tx;
  25. /* Private variables ---------------------------------------------------------*/
  26. #define BUFFER_SIZE 100
  27. uint8_t rx_len=0;  //接收一帧数据的长度
  28. uint8_t recv_end_flag=0;   //一帧数据接收完成标志
  29. uint8_t rx_buffer[BUFFER_SIZE]={"123"};  //接收数据缓存
  30. uint8_t tx_buffer[BUFFER_SIZE];
  31. /* USER CODE END PV */

  32. /* Private function prototypes -----------------------------------------------*/
  33. void SystemClock_Config(void);

  34. /* USER CODE BEGIN PFP */
  35. /* Private function prototypes -----------------------------------------------*/

  36. /* USER CODE END PFP */

  37. /* USER CODE BEGIN 0 */

  38. /* USER CODE END 0 */

  39. /**
  40.   * @brief  The application entry point.
  41.   *
  42.   * @retval None
  43.   */
  44. int main(void)
  45. {
  46.   /* USER CODE BEGIN 1 */
  47.   /* USER CODE END 1 */

  48.   /* MCU Configuration----------------------------------------------------------*/

  49.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  50.   HAL_Init();

  51.   /* USER CODE BEGIN Init */

  52.   /* USER CODE END Init */

  53.   /* Configure the system clock */
  54.   SystemClock_Config();

  55.   /* USER CODE BEGIN SysInit */

  56.   /* USER CODE END SysInit */

  57.   /* Initialize all configured peripherals */
  58.   MX_GPIO_Init();
  59.   MX_DMA_Init();
  60.   MX_TIM2_Init();
  61.   MX_USART1_UART_Init();
  62.   /* USER CODE BEGIN 2 */
  63.   //printf("tst uart DMA begin\r\n");
  64.   //__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
  65.         __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);                         //使能IDLE中断
  66.         HAL_UART_Receive_DMA(&huart1,rx_buffer,BUFFER_SIZE);//启动DMA接收
  67.   /* USER CODE END 2 */

  68.   /* Infinite loop */
  69.   /* USER CODE BEGIN WHILE */
  70.   while (1)
  71.   {
  72.           HAL_GPIO_WritePin(Tst_Pin_GPIO_Port, Tst_Pin_Pin, GPIO_PIN_RESET);
  73.          
  74.                 if(recv_end_flag ==1)
  75.                 {
  76.                         HAL_UART_Transmit(&huart1,rx_buffer, rx_len,200);//接收数据打印出来
  77.                         rx_len=0;
  78.                         recv_end_flag=0;
  79.                         //memset(rx_buffer,0x00,BUFFER_SIZE);
  80.                         HAL_UART_Receive_DMA(&huart1,rx_buffer,BUFFER_SIZE);                //重启DMA接收
  81.                 }       
  82.                 HAL_GPIO_WritePin(Tst_Pin_GPIO_Port, Tst_Pin_Pin, GPIO_PIN_SET);
  83.   /* USER CODE END WHILE */

  84.   /* USER CODE BEGIN 3 */
  85.   
  86.   
  87.   /* USER CODE END 3 */

  88.         }
  89. }

  90. /**
  91.   * @brief System Clock Configuration
  92.   * @retval None
  93.   */
  94. void SystemClock_Config(void)
  95. {

  96.   RCC_OscInitTypeDef RCC_OscInitStruct;
  97.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  98.     /**Initializes the CPU, AHB and APB busses clocks
  99.     */
  100.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  101.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  102.   RCC_OscInitStruct.HSICalibrationValue = 16;
  103.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  104.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  105.   {
  106.     _Error_Handler(__FILE__, __LINE__);
  107.   }

  108.     /**Initializes the CPU, AHB and APB busses clocks
  109.     */
  110.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  111.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  112.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  113.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  114.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  115.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  116.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  117.   {
  118.     _Error_Handler(__FILE__, __LINE__);
  119.   }

  120.   HAL_RCC_MCOConfig(RCC_MCO, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1);

  121.     /**Configure the Systick interrupt time
  122.     */
  123.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  124.     /**Configure the Systick
  125.     */
  126.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  127.   /* SysTick_IRQn interrupt configuration */
  128.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  129. }

  130. /* USER CODE BEGIN 4 */
  131. void UART_IDLE_Callback(UART_HandleTypeDef *huart)
  132. {
  133.         uint16_t i = 0;
  134.   if((__HAL_UART_GET_FLAG(huart,UART_FLAG_IDLE) != RESET))  
  135.   {
  136.                 if(huart->Instance == USART1)
  137.                 {
  138.                         __HAL_UART_CLEAR_IDLEFLAG(huart);
  139.                         i = huart->Instance->SR;
  140.                         i = huart->Instance->DR;
  141.                         i = hdma_usart1_rx.Instance->CNDTR;         //读取DMA剩余传输数量
  142.                         HAL_UART_DMAStop(huart);
  143.                        
  144.                         /* 此处处理数据,主要是拷贝和置位标志位 */
  145.                         if((recv_end_flag == 0)&&(i!=BUFFER_SIZE))
  146.                         {
  147.                                 rx_len = BUFFER_SIZE - i;
  148.                                 //memcpy(tx_buffer,rx_buffer,rx_len);
  149.                                 recv_end_flag = 1;
  150.                         }
  151.                         else
  152.                         {
  153.                                 /* 清空缓存,重新接收 */
  154.                                 //memset(rx_buffer,0x00,BUFFER_SIZE);
  155.                                 HAL_UART_Receive_DMA(huart,(uint8_t *)&rx_buffer,BUFFER_SIZE);
  156.                         }
  157.                 }
  158.   }
  159.   
  160. }  
  161. /* USER CODE END 4 */

  162. /**
  163.   * @brief  This function is executed in case of error occurrence.
  164.   * @param  file: The file name as string.
  165.   * @param  line: The line in file as a number.
  166.   * @retval None
  167.   */
  168. void _Error_Handler(char *file, int line)
  169. {
  170.   /* USER CODE BEGIN Error_Handler_Debug */
  171.   /* User can add his own implementation to report the HAL error return state */
  172.   while(1)
  173.   {
  174.   }
  175.   /* USER CODE END Error_Handler_Debug */
  176. }

  177. #ifdef  USE_FULL_ASSERT
  178. /**
  179.   * @brief  Reports the name of the source file and the source line number
  180.   *         where the assert_param error has occurred.
  181.   * @param  file: pointer to the source file name
  182.   * @param  line: assert_param error line source number
  183.   * @retval None
  184.   */
  185. void assert_failed(uint8_t* file, uint32_t line)
  186. {
  187.   /* USER CODE BEGIN 6 */
  188.   /* User can add his own implementation to report the file name and line number,
  189.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  190.   /* USER CODE END 6 */
  191. }
  192. #endif /* USE_FULL_ASSERT */

  193. /**
  194.   * @}
  195.   */

  196. /**
  197.   * @}
  198.   */

  199. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
103_UARTDMA.rar (445.2 KB, 下载次数: 103)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表