STM32使用DMA發(fā)送串口數(shù)據(jù)
01概述
上一篇文章《STM32使用DMA接收串口數(shù)據(jù)》講解了如何使用DMA接收數(shù)據(jù),使用DMA外設(shè)和串口外設(shè),使用的中斷是串口空閑中斷。本篇文章主要講解使用DMA發(fā)送數(shù)據(jù),不會(huì)講解基礎(chǔ)的串口和DMA知識(shí),直接上代碼,如果有同學(xué)對(duì)DMA和串口都不熟悉,建議看一下上篇文章《STM32使用DMA接收串口數(shù)據(jù)》。
使用DMA發(fā)送數(shù)據(jù),首先我們要確認(rèn)使用的串口有沒(méi)有DMA。
我們使用USART1串口外設(shè),從數(shù)據(jù)手冊(cè)中可以查到,USART1的發(fā)送和接收都是支持DMA的,使用的是DMA2.
接下來(lái)就是擼代碼的時(shí)刻了
02代碼
DMA串口發(fā)送的代碼是在上一篇文章DMA串口接收的基礎(chǔ)上修改的。
- 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發(fā)送*/
- USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
- /* Enable USART */
- USART_Cmd(USART1, ENABLE);
- }
在這里除了常規(guī)的串口配置,我們需要配置串口的DMA發(fā)送,和串口DMA接收一樣的API函數(shù),參數(shù)修改為USART_DMAReq_Tx即可。
串口DMA發(fā)送配置
- 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請(qǐng)求發(fā)出通道
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;//配置外設(shè)地址
- DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)UART_Buffer;//配置存儲(chǔ)器地址
- DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;//傳輸方向配置
- DMA_InitStructure.DMA_BufferSize = (uint32_t)UART_RX_LEN;//傳輸大小
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外設(shè)地址不變
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//memory地址自增
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外設(shè)地址數(shù)據(jù)單位
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//memory地址數(shù)據(jù)單位
- DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//DMA模式:正常模式
- DMA_InitStructure.DMA_Priority = DMA_Priority_High;//優(yōu)先級(jí):高
- DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;//FIFO 模式不使能.
- DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;// FIFO 閾值選擇
- DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//存儲(chǔ)器突發(fā)模式選擇,可選單次模式、 4 節(jié)拍的增量突發(fā)模式、 8 節(jié)拍的增量突發(fā)模式或 16 節(jié)拍的增量突發(fā)模式。
- DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外設(shè)突發(fā)模式選擇,可選單次模式、 4 節(jié)拍的增量突發(fā)模式、 8 節(jié)拍的增量突發(fā)模式或 16 節(jié)拍的增量突發(fā)模式。
- DMA_Init(DMA2_Stream7, &DMA_InitStructure);
- /* DMA Stream enable */
- // DMA_Cmd(DMA2_Stream7, ENABLE);
- }
這里也是常規(guī)的DMA配置流程,不明白的同學(xué)請(qǐng)看文章《STM32DMA詳解》,這里值得注意的是,配置完成并沒(méi)有使能DMA2_Stream7,使能了就會(huì)立即將UART_Buffer的數(shù)據(jù)發(fā)送出去。
其他代碼處理
- 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標(biāo)志
- }
- }
- 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個(gè)函數(shù),簡(jiǎn)單邏輯就是,當(dāng)串口使用DMA接收了一定量的數(shù)據(jù),就會(huì)通過(guò)串口DMA發(fā)送出去,串口DMA發(fā)送的代碼如下:
- 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后記
這一篇很簡(jiǎn)單,就是DMA使用的一個(gè)延伸,上面說(shuō)了這么多,也貼了很多代碼,不可能將所有代碼全部貼出來(lái),作為軟件工程師,還是在IDE里看代碼方便,如果感興趣的話,可以到下面github鏈接下載代碼,Keil和IAR的工程文件都有。