先上代碼:
#define COMM_COM USART2
#define COMM_COM_CLK RCC_APB1Periph_USART2
#define COMM_COM_TX_GPIO_CLK RCC_AHB1Periph_GPIOD //UART TX
#define COMM_COM_TX_PIN GPIO_Pin_5
#define COMM_COM_TX_GPIO_PORT GPIOD
#define COMM_COM_TX_SOURCE GPIO_PinSource5
#define COMM_COM_TX_AF GPIO_AF_USART2
#define COMM_COM_RX_GPIO_CLK RCC_AHB1Periph_GPIOD //UART RX
#define COMM_COM_RX_PIN GPIO_Pin_6
#define COMM_COM_RX_GPIO_PORT GPIOD
#define COMM_COM_RX_SOURCE GPIO_PinSource6
#define COMM_COM_RX_AF GPIO_AF_USART2
#define COMM_COM_IRQn USART2_IRQn
#define COMM_COM_Priority 9 //優(yōu)先級(jí)
#define COMM_COM_BaudRate 115200 //波特率
#define COMM_COM_IRQHandler USART2_IRQHandler //中斷函數(shù)接口(見(jiàn)stm32f4xx_it.c)
USART配置:
/************************************************
函數(shù)名稱 :USART_COMM_Configuration
功 能 :通信串口配置
參 數(shù) :無(wú)
返 回 值 :無(wú)
作 者 :strongerHuang
*************************************************/
void USART_COMM_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* 時(shí)鐘配置 */
RCC_AHB1PeriphClockCmd(COMM_COM_TX_GPIO_CLK | COMM_COM_RX_GPIO_CLK, ENABLE);
if((USART1 == COMM_COM) || (USART6 == COMM_COM))
RCC_APB2PeriphClockCmd(COMM_COM_CLK, ENABLE);
else
RCC_APB1PeriphClockCmd(COMM_COM_CLK, ENABLE);
/* 復(fù)用配置 */
GPIO_PinAFConfig(COMM_COM_TX_GPIO_PORT, COMM_COM_TX_SOURCE, COMM_COM_TX_AF);
GPIO_PinAFConfig(COMM_COM_RX_GPIO_PORT, COMM_COM_RX_SOURCE, COMM_COM_RX_AF);
/* 引腳配置 */
GPIO_InitStructure.GPIO_Pin = COMM_COM_TX_PIN; //USART Tx
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //復(fù)用模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(COMM_COM_TX_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = COMM_COM_RX_PIN; //USART Rx
GPIO_Init(COMM_COM_RX_GPIO_PORT, &GPIO_InitStructure);
/* NVIC配置 */
NVIC_InitStructure.NVIC_IRQChannel = COMM_COM_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = COMM_COM_Priority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* USART配置 */
USART_InitStructure.USART_BaudRate = COMM_COM_BaudRate; //波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //傳輸位數(shù)
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位
USART_InitStructure.USART_Parity = USART_Parity_No ; //校驗(yàn)位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發(fā)功能
USART_Init(COMM_COM, &USART_InitStructure);
USART_ClearFlag(COMM_COM, USART_FLAG_RXNE | USART_FLAG_TC);
USART_ITConfig(COMM_COM, USART_IT_RXNE, ENABLE); //接收中斷
USART_DMACmd(COMM_COM, USART_DMAReq_Tx, ENABLE); //使能DMA
USART_Cmd(COMM_COM, ENABLE); //使能USART
}
DMA配置中的宏定義:
#define COMM_DR_ADDRESS ((uint32_t)USART2 + 0x04)
#define COMM_DMA DMA1
#define COMM_DMA_CLK RCC_AHB1Periph_DMA1
#define COMM_TX_DMA_CHANNEL DMA_Channel_4
#define COMM_TX_DMA_STREAM DMA1_Stream6
#define COMM_TX_DMA_FLAG_TCIF DMA_FLAG_TCIF6
#define COMM_TX_DMA_IRQn DMA1_Stream6_IRQn
#define COMM_TX_DMA_Priority 8 //優(yōu)先級(jí)
#define COMM_TX_DMA_IRQHandler DMA1_Stream6_IRQHandler //中斷函數(shù)接口(見(jiàn)stm32f4xx_it.c)
#define COMM_TX_DMA_IT_TCIF DMA_IT_TCIF6
通訊串口的配置
/************************************************
函數(shù)名稱 :USART_COMM_DMA_Configuration
功 能 :通信串口的DMA配置
參 數(shù) :無(wú)
返 回 值 :無(wú)
作 者 :strongerHuang
*************************************************/
void USART_COMM_DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* 使能時(shí)鐘 */
RCC_AHB1PeriphClockCmd(COMM_DMA_CLK, ENABLE);
/* NVIC配置 */
NVIC_InitStructure.NVIC_IRQChannel = COMM_TX_DMA_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = COMM_TX_DMA_Priority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* DMA配置 */
DMA_DeInit(COMM_TX_DMA_STREAM);
DMA_InitStructure.DMA_Channel = COMM_TX_DMA_CHANNEL; //DMA通道
DMA_InitStructure.DMA_PeripheralBaseAddr = COMM_DR_ADDRESS; //外設(shè)地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)0; //內(nèi)存地址(待傳入參數(shù))
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //傳輸方向
DMA_InitStructure.DMA_BufferSize = 0; //傳輸長(zhǎng)度(待傳入?yún)?shù))
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外設(shè)遞增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //內(nèi)存遞增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //數(shù)據(jù)寬度
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //循環(huán)模式
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //優(yōu)先級(jí)
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(COMM_TX_DMA_STREAM, &DMA_InitStructure);
DMA_ClearFlag(COMM_TX_DMA_STREAM, COMM_TX_DMA_FLAG_TCIF);
DMA_ITConfig(COMM_TX_DMA_STREAM, DMA_IT_TC, ENABLE); //使能DMA傳輸完成中斷
DMA_Cmd(COMM_TX_DMA_STREAM, DISABLE); //初始化禁止
}
j接下來(lái)實(shí)現(xiàn)數(shù)據(jù)發(fā)送與接收
/************************************************
函數(shù)名稱 :COMM_SendBufByDMA
功 能 :通信串口通過(guò)DMA發(fā)送數(shù)據(jù)
參 數(shù) :Buf ------ 數(shù)據(jù)(地址)
Length --- 數(shù)據(jù)長(zhǎng)度(字節(jié))
返 回 值 :無(wú)
作 者 :strongerHuang
*************************************************/
void COMM_SendBufByDMA(uint8_t *Buf, uint16_t Length)
{
DMA_Cmd(COMM_TX_DMA_STREAM, DISABLE); //關(guān)閉DMA
//內(nèi)存地址
DMA_MemoryTargetConfig(COMM_TX_DMA_STREAM, (uint32_t)Buf, DMA_Memory_0);
DMA_SetCurrDataCounter(COMM_TX_DMA_STREAM, Length); //設(shè)置DMA傳輸長(zhǎng)度
DMA_Cmd(COMM_TX_DMA_STREAM, ENABLE); //使能DMA
}
DMA是一種在嵌入式實(shí)時(shí)任務(wù)處理中常用的功能。
而UART發(fā)送數(shù)據(jù)包,使用DMA方式能大量減輕CPU處理的時(shí)間,使其CPU資源不被大量浪費(fèi),尤其在UART收發(fā)大量數(shù)據(jù)包(如高頻率收發(fā)指令)時(shí)具有明顯優(yōu)勢(shì)。
審核編輯:湯梓紅
-
單片機(jī)
+關(guān)注
關(guān)注
6042文章
44617瀏覽量
637452 -
STM32
+關(guān)注
關(guān)注
2270文章
10923瀏覽量
357017 -
串口
+關(guān)注
關(guān)注
14文章
1557瀏覽量
76846 -
dma
+關(guān)注
關(guān)注
3文章
566瀏覽量
100823
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論