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教程