STM32使用DMA发送串口数据

商务办公
我们使用USART1串口外设,从数据手册中可以查到,USART1的发送和接收都是支持DMA的,使用的是DMA2.

[[400593]]

01概述

上一篇文章《STM32使用DMA接收串口数据》讲解了如何使用DMA接收数据,使用DMA外设和串口外设,使用的中断是串口空闲中断。本篇文章主要讲解使用DMA发送数据,不会讲解基础的串口和DMA知识,直接上代码,如果有同学对DMA和串口都不熟悉,建议看一下上篇文章《STM32使用DMA接收串口数据》。

使用DMA发送数据,首先我们要确认使用的串口有没有DMA。

我们使用USART1串口外设,从数据手册中可以查到,USART1的发送和接收都是支持DMA的,使用的是DMA2.

接下来就是撸代码的时刻了

02代码

DMA串口发送的代码是在上一篇文章DMA串口接收的基础上修改的。

  1. void UART_Init(void) 
  2.   USART_InitTypeDef USART_InitStructure; 
  3.   GPIO_InitTypeDef GPIO_InitStructure; 
  4.   NVIC_InitTypeDef NVIC_InitStructure; 
  5.  
  6.   /* Enable GPIO clock */ 
  7.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 
  8.   /* Enable UART1 clock */ 
  9.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); 
  10.   /* Connect PXx to USARTx_Tx*/ 
  11.   GPIO_PinAFConfig(GPIOA, 9, GPIO_AF_USART1); 
  12.  
  13.   /* Connect PXx to USARTx_Rx*/ 
  14.   GPIO_PinAFConfig(GPIOA, 10, GPIO_AF_USART1); 
  15.  
  16.   /* Configure USART Tx as alternate function  */ 
  17.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
  18.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
  19.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
  20.  
  21.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 
  22.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  23.   GPIO_Init(GPIOA, &GPIO_InitStructure); 
  24.  
  25.   /* Configure USART Rx as alternate function  */ 
  26.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
  27.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 
  28.   GPIO_Init(GPIOA, &GPIO_InitStructure); 
  29.  
  30.   USART_InitStructure.USART_BaudRate = 115200; 
  31.   USART_InitStructure.USART_WordLength = USART_WordLength_8b; 
  32.   USART_InitStructure.USART_StopBits = USART_StopBits_1; 
  33.   USART_InitStructure.USART_Parity = USART_Parity_No; 
  34.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
  35.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; 
  36.  
  37.   /* USART configuration */ 
  38.   USART_Init(USART1, &USART_InitStructure); 
  39.  
  40.   USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); 
  41.  
  42.   /* Enable the USARTx Interrupt */ 
  43.   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 
  44.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0; 
  45.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
  46.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
  47.   NVIC_Init(&NVIC_InitStructure); 
  48.  
  49.   /*使能串口DMA接收*/ 
  50.   USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); 
  51.   /*使能串口DMA发送*/ 
  52.   USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); 
  53.  
  54.   /* Enable USART */ 
  55.   USART_Cmd(USART1, ENABLE); 

在这里除了常规的串口配置,我们需要配置串口的DMA发送,和串口DMA接收一样的API函数,参数修改为USART_DMAReq_Tx即可。

串口DMA发送配置

  1. void Uart_Send_DMA_Config(void) 
  2.   DMA_InitTypeDef  DMA_InitStructure; 
  3.  
  4.   /* Enable DMA clock */ 
  5.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); 
  6.  
  7.   /* Reset DMA Stream registers (for debug purpose) */ 
  8.   DMA_DeInit(DMA2_Stream7); 
  9.  
  10.   /* Check if the DMA Stream is disabled before enabling it. 
  11.      Note that this step is useful when the same Stream is used multiple times: 
  12.      enabled, then disabled then re-enabled... In this case, the DMA Stream disable 
  13.      will be effective only at the end of the ongoing data transfer and it will  
  14.      not be possible to re-configure it before making sure that the Enable bit  
  15.      has been cleared by hardware. If the Stream is used only once, this step might  
  16.      be bypassed. */ 
  17.   while (DMA_GetCmdStatus(DMA2_Stream7) != DISABLE) 
  18.   { 
  19.   } 
  20.  
  21.   /* Configure DMA Stream */ 
  22.   DMA_InitStructure.DMA_Channel = DMA_Channel_4;  //DMA请求发出通道 
  23.   DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;//配置外设地址 
  24.   DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)UART_Buffer;//配置存储器地址 
  25.   DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;//传输方向配置 
  26.   DMA_InitStructure.DMA_BufferSize = (uint32_t)UART_RX_LEN;//传输大小 
  27.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设地址不变 
  28.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//memory地址自增 
  29.   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设地址数据单位 
  30.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//memory地址数据单位 
  31.   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//DMA模式:正常模式 
  32.   DMA_InitStructure.DMA_Priority = DMA_Priority_High;//优先级:高 
  33.   DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;//FIFO 模式不使能.           
  34.   DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;// FIFO 阈值选择 
  35.   DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//存储器突发模式选择,可选单次模式、 4 节拍的增量突发模式、 8 节拍的增量突发模式或 16 节拍的增量突发模式。 
  36.   DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外设突发模式选择,可选单次模式、 4 节拍的增量突发模式、 8 节拍的增量突发模式或 16 节拍的增量突发模式。 
  37.   DMA_Init(DMA2_Stream7, &DMA_InitStructure);  
  38.  
  39.   /* DMA Stream enable */ 
  40. //  DMA_Cmd(DMA2_Stream7, ENABLE); 

这里也是常规的DMA配置流程,不明白的同学请看文章《STM32DMA详解》,这里值得注意的是,配置完成并没有使能DMA2_Stream7,使能了就会立即将UART_Buffer的数据发送出去。

其他代码处理

  1. void USART1_IRQHandler(void) 
  2.   uint8_t temp
  3.   if(USART_GetFlagStatus(USART1, USART_FLAG_IDLE) == SET
  4.   { 
  5.     DealWith_UartData(); 
  6. //    USART_ClearFlag(USART1, USART_FLAG_IDLE); 
  7.     temp = USART1->SR;   
  8.     temp = USART1->DR; //清USART_IT_IDLE标志   
  9.   } 
  10.  
  11. void DealWith_UartData() 
  12.   DMA_Cmd(DMA2_Stream2, DISABLE); 
  13.   UART_Receive_flg = 1; 
  14.   UART_Receive_len = UART_RX_LEN - DMA_GetCurrDataCounter(DMA2_Stream2); 
  15.   UART_Buffer[UART_Receive_len] = 0; 
  16.   DMA_SetCurrDataCounter(DMA2_Stream2,UART_RX_LEN);  
  17.   DMA_ClearFlag(DMA2_Stream2, DMA_FLAG_TCIF2); 
  18.   DMA_Cmd(DMA2_Stream2, ENABLE); 
  19.  
  20. int main(void) 
  21.   UART_Receive_flg = 0; 
  22.  
  23.   Uart_Reveice_DMA_Config(); 
  24.   Uart_Send_DMA_Config(); 
  25.   UART_Init(); 
  26.  
  27.   while (1) 
  28.   { 
  29.     if(UART_Receive_flg) 
  30.     { 
  31.       UART_Receive_flg = 0; 
  32.       Uart_Send_DMA_Start(); 
  33.     } 
  34.   } 

上面3个函数,简单逻辑就是,当串口使用DMA接收了一定量的数据,就会通过串口DMA发送出去,串口DMA发送的代码如下:

  1. void Uart_Send_DMA_Start(void) 
  2.   DMA_SetCurrDataCounter(DMA2_Stream7,UART_Receive_len);  
  3.   DMA_ClearFlag(DMA2_Stream7, DMA_FLAG_TCIF7); 
  4.   /* DMA Stream enable */ 
  5.   DMA_Cmd(DMA2_Stream7, ENABLE); 

03后记

 

这一篇很简单,就是DMA使用的一个延伸,上面说了这么多,也贴了很多代码,不可能将所有代码全部贴出来,作为软件工程师,还是在IDE里看代码方便,如果感兴趣的话,可以到下面github链接下载代码,Keil和IAR的工程文件都有。

 

责任编辑:武晓燕 来源: 知晓编程
相关推荐

2021-05-06 08:54:39

串口DMASTM32

2021-02-03 13:04:24

STM32网络控制器

2021-06-26 07:50:20

STM32串口开发环形缓冲区

2021-08-31 08:01:40

STM32DSP指令

2022-07-25 14:31:55

LiteOS开发工具

2021-02-05 08:41:44

STM32网络中断

2021-01-20 09:51:25

STM32网络接口

2021-08-26 06:29:47

STM32DWT数据观察点触发

2021-12-22 06:56:07

STM32C语言内存

2022-10-10 08:24:15

编程数据逻辑语句

2022-01-07 08:24:13

STM32枚举结构体

2021-03-06 21:21:11

STM32单片机追踪库

2021-04-22 08:39:23

哈佛结构冯洛伊曼结构ARM架构

2020-12-03 06:32:21

STM32单片机通信

2021-11-15 09:53:16

STM32PSPMSP

2021-02-01 06:39:42

模块封装库

2018-10-15 10:15:30

STM32Linux经验

2020-01-10 22:51:30

硬件软件人生第一份工作

2021-02-26 08:46:46

PHY寄存器网络

2009-10-29 15:30:06

VB.NET串口通讯
点赞
收藏

51CTO技术栈公众号