STM32使用DMA發送串口數據


1、概述

上一篇文章《STM32使用DMA接收串口數據》講解了如何使用DMA接收數據,使用DMA外設和串口外設,使用的中斷是串口空閑中斷。本篇文章主要講解使用DMA發送數據,不會講解基礎的串口和DMA知識,直接上代碼,如果有同學對DMA和串口都不熟悉,建議看一下上篇文章《STM32使用DMA接收串口數據》。

使用DMA發送數據,首先我們要確認使用的串口有沒有DMA。

我們使用USART1串口外設,從數據手冊中可以查到,USART1的發送和接收都是支持DMA的,使用的是DMA2.

 

 

 接下來就是擼代碼的時刻了

02、代碼

DMA串口發送的代碼是在上一篇文章DMA串口接收的基礎上修改的。

void UART_Init(void)
{
  USART_InitTypeDef USART_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
 
  /* Enable GPIO clock */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  /* Enable UART1 clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  /* Connect PXx to USARTx_Tx*/
  GPIO_PinAFConfig(GPIOA, 9, GPIO_AF_USART1);
 
  /* Connect PXx to USARTx_Rx*/
  GPIO_PinAFConfig(GPIOA, 10, GPIO_AF_USART1);
 
  /* Configure USART Tx as alternate function  */
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 
  /* Configure USART Rx as alternate function  */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 
  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
 
  /* USART configuration */
  USART_Init(USART1, &USART_InitStructure);
 
  USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
 
  /* Enable the USARTx Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
 
  /*使能串口DMA接收*/
  USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
  /*使能串口DMA發送*/
  USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
 
  /* Enable USART */
  USART_Cmd(USART1, ENABLE);
}

在這里除了常規的串口配置,我們需要配置串口的DMA發送,和串口DMA接收一樣的API函數,參數修改為USART_DMAReq_Tx即可。

串口DMA發送配置

void Uart_Send_DMA_Config(void)
{
  DMA_InitTypeDef  DMA_InitStructure;
 
  /* Enable DMA clock */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
 
  /* Reset DMA Stream registers (for debug purpose) */
  DMA_DeInit(DMA2_Stream7);
 
  /* Check if the DMA Stream is disabled before enabling it.
     Note that this step is useful when the same Stream is used multiple times:
     enabled, then disabled then re-enabled... In this case, the DMA Stream disable
     will be effective only at the end of the ongoing data transfer and it will 
     not be possible to re-configure it before making sure that the Enable bit 
     has been cleared by hardware. If the Stream is used only once, this step might 
     be bypassed. */
  while (DMA_GetCmdStatus(DMA2_Stream7) != DISABLE)
  {
  }
 
  /* Configure DMA Stream */
  DMA_InitStructure.DMA_Channel = DMA_Channel_4;  //DMA請求發出通道
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;//配置外設地址
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)UART_Buffer;//配置存儲器地址
  DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;//傳輸方向配置
  DMA_InitStructure.DMA_BufferSize = (uint32_t)UART_RX_LEN;//傳輸大小
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外設地址不變
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//memory地址自增
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外設地址數據單位
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//memory地址數據單位
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//DMA模式:正常模式
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;//優先級:高
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;//FIFO 模式不使能.          
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;// FIFO 閾值選擇
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//存儲器突發模式選擇,可選單次模式、 4 節拍的增量突發模式、 8 節拍的增量突發模式或 16 節拍的增量突發模式。
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外設突發模式選擇,可選單次模式、 4 節拍的增量突發模式、 8 節拍的增量突發模式或 16 節拍的增量突發模式。
  DMA_Init(DMA2_Stream7, &DMA_InitStructure); 
 
  /* DMA Stream enable */
//  DMA_Cmd(DMA2_Stream7, ENABLE);
}

這里也是常規的DMA配置流程,不明白的同學請看文章《STM32DMA詳解》,這里值得注意的是,配置完成並沒有使能DMA2_Stream7,使能了就會立即將UART_Buffer的數據發送出去。

其他代碼處理

void USART1_IRQHandler(void)
{
  uint8_t temp;
  if(USART_GetFlagStatus(USART1, USART_FLAG_IDLE) == SET)
  {
    DealWith_UartData();
//    USART_ClearFlag(USART1, USART_FLAG_IDLE);
    temp = USART1->SR;  
    temp = USART1->DR; //清USART_IT_IDLE標志  
  }
}
 
void DealWith_UartData()
{
  DMA_Cmd(DMA2_Stream2, DISABLE);
  UART_Receive_flg = 1;
  UART_Receive_len = UART_RX_LEN - DMA_GetCurrDataCounter(DMA2_Stream2);
  UART_Buffer[UART_Receive_len] = 0;
  DMA_SetCurrDataCounter(DMA2_Stream2,UART_RX_LEN); 
  DMA_ClearFlag(DMA2_Stream2, DMA_FLAG_TCIF2);
  DMA_Cmd(DMA2_Stream2, ENABLE);
}
 
int main(void)
{
  UART_Receive_flg = 0;
 
  Uart_Reveice_DMA_Config();
  Uart_Send_DMA_Config();
  UART_Init();
 
  while (1)
  {
    if(UART_Receive_flg)
    {
      UART_Receive_flg = 0;
      Uart_Send_DMA_Start();
    }
  }
}

上面3個函數,簡單邏輯就是,當串口使用DMA接收了一定量的數據,就會通過串口DMA發送出去,串口DMA發送的代碼如下:

void Uart_Send_DMA_Start(void)
{
  DMA_SetCurrDataCounter(DMA2_Stream7,UART_Receive_len); 
  DMA_ClearFlag(DMA2_Stream7, DMA_FLAG_TCIF7);
  /* DMA Stream enable */
  DMA_Cmd(DMA2_Stream7, ENABLE);
}

03、后記

這一篇很簡單,就是DMA使用的一個延伸,上面說了這么多,也貼了很多代碼,不可能將所有代碼全部貼出來,作為軟件工程師,還是在IDE里看代碼方便,如果感興趣的話,可以到下面github鏈接下載代碼,Keil和IAR的工程文件都有。

PCB和工程代碼開源地址:

https://github.com/strongercjd/STM32F207VCT6

 

點擊查看本文所在的專輯,STM32F207教程


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM