前面講解過(guò)什么是DMA? STM32如何配置DMA?,今天和大家分享DMA實(shí)現(xiàn)串口數(shù)據(jù)的收發(fā)。
直接存儲(chǔ)器訪問(wèn)(Direct Memory Access),簡(jiǎn)稱(chēng)DMA。DMA是CPU一個(gè)用于數(shù)據(jù)從一個(gè)地址空間到另一地址空間“搬運(yùn)”(拷貝)的組件,數(shù)據(jù)拷貝過(guò)程不需CPU干預(yù),數(shù)據(jù)拷貝結(jié)束則通知CPU處理。因此,大量數(shù)據(jù)拷貝時(shí),使用DMA可以釋放CPU資源。DMA數(shù)據(jù)拷貝過(guò)程,典型的有:
串口(uart)是一種低速的串行異步通信,適用于低速通信場(chǎng)景,通常使用的波特率小于或等于115200bps。對(duì)于小于或者等于115200bps波特率的,而且數(shù)據(jù)量不大的通信場(chǎng)景,一般沒(méi)必要使用DMA,或者說(shuō)使用DMA并未能充分發(fā)揮出DMA的作用。
對(duì)于數(shù)量大,或者波特率提高時(shí),必須使用DMA以釋放CPU資源,因?yàn)楦卟ㄌ芈士赡軒?lái)這樣的問(wèn)題:
因此,高波特率場(chǎng)景下,串口非常有必要使用DMA。
關(guān)于STM32串口使用DMA,不乏一些開(kāi)發(fā)板例程及網(wǎng)絡(luò)上一些博主的使用教程。使用步驟、流程、配置基本大同小異,正確性也沒(méi)什么毛病,但都是一些基本的Demo例子,作為學(xué)習(xí)過(guò)程沒(méi)問(wèn)題;實(shí)際項(xiàng)目使用缺乏嚴(yán)謹(jǐn)性,數(shù)據(jù)量大時(shí)可能導(dǎo)致數(shù)據(jù)異常。
測(cè)試平臺(tái):
關(guān)鍵步驟
【1】初始化串口
【2】使能串口DMA接收模式,使能串口空閑中斷
【3】配置DMA參數(shù),使能DMA通道buf半滿(mǎn)(傳輸一半數(shù)據(jù))中斷、buf溢滿(mǎn)(傳輸數(shù)據(jù)完成)中斷
為什么需要使用DMA 通道buf半滿(mǎn)中斷?
很多串口DMA模式接收的教程、例子,基本是使用了“空間中斷”+“DMA傳輸完成中斷”來(lái)接收數(shù)據(jù)。實(shí)質(zhì)上這是存在風(fēng)險(xiǎn)的,當(dāng)DMA傳輸數(shù)據(jù)完成,CPU介入開(kāi)始拷貝DMA通道buf數(shù)據(jù),如果此時(shí)串口繼續(xù)有數(shù)據(jù)進(jìn)來(lái),DMA繼續(xù)搬運(yùn)數(shù)據(jù)到buf,就有可能將數(shù)據(jù)覆蓋,因?yàn)镈MA數(shù)據(jù)搬運(yùn)是不受CPU控制的,即使你關(guān)閉了CPU中斷。
嚴(yán)謹(jǐn)?shù)淖龇ㄐ枰鲭pbuf,CPU和DMA各自一塊內(nèi)存交替訪問(wèn),即是'乒乓緩存” ,處理流程步驟應(yīng)該是這樣:
【1】第一步,DMA先將數(shù)據(jù)搬運(yùn)到buf1,搬運(yùn)完成通知CPU來(lái)拷貝buf1數(shù)據(jù) 【2】第二步,DMA將數(shù)據(jù)搬運(yùn)到buf2,與CPU拷貝buf1數(shù)據(jù)不會(huì)沖突 【3】第三步,buf2數(shù)據(jù)搬運(yùn)完成,通知CPU來(lái)拷貝buf2數(shù)據(jù) 【4】執(zhí)行完第三步,DMA返回執(zhí)行第一步,一直循環(huán)
STM32F0系列DMA不支持雙緩存(以具體型號(hào)為準(zhǔn))機(jī)制,但提供了一個(gè)buf'半滿(mǎn)中斷'
,即是數(shù)據(jù)搬運(yùn)到buf大小的一半時(shí),可以產(chǎn)生一個(gè)中斷信號(hào)。基于這個(gè)機(jī)制,我們可以實(shí)現(xiàn)雙緩存功能,只需將buf空間開(kāi)辟大一點(diǎn)即可。
【1】第一步,DMA將數(shù)據(jù)搬運(yùn)完成buf的前一半時(shí),產(chǎn)生“半滿(mǎn)中斷”,CPU來(lái)拷貝buf前半部分?jǐn)?shù)據(jù) 【2】第二步,DMA繼續(xù)將數(shù)據(jù)搬運(yùn)到buf的后半部分,與CPU拷貝buf前半部數(shù)據(jù)不會(huì)沖突 【3】第三步,buf后半部分?jǐn)?shù)據(jù)搬運(yùn)完成,觸發(fā)“溢滿(mǎn)中斷”,CPU來(lái)拷貝buf后半部分?jǐn)?shù)據(jù) 【4】執(zhí)行完第三步,DMA返回執(zhí)行第一步,一直循環(huán)
UART2 DMA模式接收配置代碼如下,與其他外設(shè)使用DMA的配置基本一致,留意關(guān)鍵配置:
void bsp_uart2_dmarx_config(uint8_t *mem_addr, uint32_t mem_size)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel5);
DMA_Cmd(DMA1_Channel5, DISABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->RDR);/* UART2接收數(shù)據(jù)地址 */
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)mem_addr; /* 接收buf */
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; /* 傳輸方向:外設(shè)->內(nèi)存 */
DMA_InitStructure.DMA_BufferSize = mem_size; /* 接收buf大小 */
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; /* 連續(xù)模式 */
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel5, DMA_IT_TC|DMA_IT_HT|DMA_IT_TE, ENABLE);/* 使能DMA半滿(mǎn)、溢滿(mǎn)、錯(cuò)誤中斷 */
DMA_ClearFlag(DMA1_IT_TC5); /* 清除相關(guān)狀態(tài)標(biāo)識(shí) */
DMA_ClearFlag(DMA1_IT_HT5);
DMA_Cmd(DMA1_Channel5, ENABLE);
}
DMA 錯(cuò)誤中斷
“DMA_IT_TE”
,一般用于前期調(diào)試使用,用于檢查DMA出現(xiàn)錯(cuò)誤的次數(shù),發(fā)布軟件可以不使能該中斷。
基于上述描述機(jī)制,DMA方式接收串口數(shù)據(jù),有三種中斷場(chǎng)景需要CPU去將buf數(shù)據(jù)拷貝到fifo中,分別是:
前兩者場(chǎng)景,前面文章已經(jīng)描述。串口空閑中斷指的是,數(shù)據(jù)傳輸完成后,串口監(jiān)測(cè)到一段時(shí)間內(nèi)沒(méi)有數(shù)據(jù)進(jìn)來(lái),則觸發(fā)產(chǎn)生的中斷信號(hào)。
數(shù)據(jù)傳輸過(guò)程是隨機(jī)的,數(shù)據(jù)大小也是不定的,存在幾類(lèi)情況:
因此,我們需根據(jù)“DMA通道buf大小”、“DMA通道buf剩余空間大小”、“上一次接收的總數(shù)據(jù)大小”
來(lái)計(jì)算當(dāng)前接收的數(shù)據(jù)大小。
/* 獲取DMA通道接收buf剩余空間大小 */
uint16_t DMA_GetCurrDataCounter(DMA_Channel_TypeDef* DMAy_Channelx);
DMA通道buf溢滿(mǎn)場(chǎng)景計(jì)算
接收數(shù)據(jù)大小 = DMA通道buf大小 - 上一次接收的總數(shù)據(jù)大小
DMA通道buf溢滿(mǎn)中斷處理函數(shù):
void uart_dmarx_done_isr(uint8_t uart_id)
{
uint16_t recv_size;
recv_size = s_uart_dev[uart_id].dmarx_buf_size - s_uart_dev[uart_id].last_dmarx_size;
fifo_write(&s_uart_dev[uart_id].rx_fifo,
(const uint8_t *)&(s_uart_dev[uart_id].dmarx_buf[s_uart_dev[uart_id].last_dmarx_size]), recv_size);
s_uart_dev[uart_id].last_dmarx_size = 0;
}
DMA通道buf半滿(mǎn)場(chǎng)景計(jì)算
接收數(shù)據(jù)大小 = DMA通道接收總數(shù)據(jù)大小 - 上一次接收的總數(shù)據(jù)大小
DMA通道接收總數(shù)據(jù)大小 = DMA通道buf大小 - DMA通道buf剩余空間大小
DMA通道buf半滿(mǎn)中斷處理函數(shù):
void uart_dmarx_half_done_isr(uint8_t uart_id)
{
uint16_t recv_total_size;
uint16_t recv_size;
if(uart_id == 0)
{
recv_total_size = s_uart_dev[uart_id].dmarx_buf_size - bsp_uart1_get_dmarx_buf_remain_size();
}
else if (uart_id == 1)
{
recv_total_size = s_uart_dev[uart_id].dmarx_buf_size - bsp_uart2_get_dmarx_buf_remain_size();
}
recv_size = recv_total_size - s_uart_dev[uart_id].last_dmarx_size;
fifo_write(&s_uart_dev[uart_id].rx_fifo,
(const uint8_t *)&(s_uart_dev[uart_id].dmarx_buf[s_uart_dev[uart_id].last_dmarx_size]), recv_size);
s_uart_dev[uart_id].last_dmarx_size = recv_total_size;/* 記錄接收總數(shù)據(jù)大小 */
}
串口空閑中斷場(chǎng)景計(jì)算
串口空閑中斷場(chǎng)景的接收數(shù)據(jù)計(jì)算與“DMA通道buf半滿(mǎn)場(chǎng)景”計(jì)算方式是一樣的。
串口空閑中斷處理函數(shù):
void uart_dmarx_idle_isr(uint8_t uart_id)
{
uint16_t recv_total_size;
uint16_t recv_size;
if(uart_id == 0)
{
recv_total_size = s_uart_dev[uart_id].dmarx_buf_size - bsp_uart1_get_dmarx_buf_remain_size();
}
else if (uart_id == 1)
{
recv_total_size = s_uart_dev[uart_id].dmarx_buf_size - bsp_uart2_get_dmarx_buf_remain_size();
}
recv_size = recv_total_size - s_uart_dev[uart_id].last_dmarx_size;
s_UartTxRxCount[uart_id*2+1] += recv_size;
fifo_write(&s_uart_dev[uart_id].rx_fifo,
(const uint8_t *)&(s_uart_dev[uart_id].dmarx_buf[s_uart_dev[uart_id].last_dmarx_size]), recv_size);
s_uart_dev[uart_id].last_dmarx_size = recv_total_size;
}
注:串口空閑中斷處理函數(shù),除了將數(shù)據(jù)拷貝到串口接收f(shuō)ifo中,還可以增加特殊處理,如作為串口數(shù)據(jù)傳輸完成標(biāo)識(shí)、不定長(zhǎng)度數(shù)據(jù)處理等等。
將有效數(shù)據(jù)拷貝到fifo中,除了需知道有效數(shù)據(jù)大小外,還需知道數(shù)據(jù)存儲(chǔ)于DMA 接收buf的偏移地址。有效數(shù)據(jù)偏移地址只需記錄上一次接收的總大小即,可,在DMA通道buf全滿(mǎn)中斷處理函數(shù)將該值清零,因?yàn)橄乱淮螖?shù)據(jù)將從buf的開(kāi)頭存儲(chǔ)。
在DMA通道buf溢滿(mǎn)中斷處理函數(shù)中將數(shù)據(jù)偏移地址清零:
void uart_dmarx_done_isr(uint8_t uart_id)
{
/* todo */
s_uart_dev[uart_id].last_dmarx_size = 0;
}
經(jīng)過(guò)前面的處理步驟,已將串口數(shù)據(jù)拷貝至接收f(shuō)ifo,應(yīng)用程序任務(wù)只需從fifo獲取數(shù)據(jù)進(jìn)行處理。前提是,處理效率必須大于DAM接收搬運(yùn)數(shù)據(jù)的效率,否則導(dǎo)致數(shù)據(jù)丟失或者被覆蓋處理。
關(guān)鍵步驟
【1】初始化串口
【2】使能串口DMA發(fā)送模式
【3】配置DMA發(fā)送通道,這一步無(wú)需在初始化設(shè)置,有數(shù)據(jù)需要發(fā)送時(shí)才配置使能DMA發(fā)送通道
UART2 DMA模式發(fā)送配置代碼如下,與其他外設(shè)使用DMA的配置基本一致,留意關(guān)鍵配置:
void bsp_uart2_dmatx_config(uint8_t *mem_addr, uint32_t mem_size)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel4);
DMA_Cmd(DMA1_Channel4, DISABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->TDR);/* UART2發(fā)送數(shù)據(jù)地址 */
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)mem_addr; /* 發(fā)送數(shù)據(jù)buf */
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; /* 傳輸方向:內(nèi)存->外設(shè) */
DMA_InitStructure.DMA_BufferSize = mem_size; /* 發(fā)送數(shù)據(jù)buf大小 */
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; /* 單次模式 */
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC|DMA_IT_TE, ENABLE); /* 使能傳輸完成中斷、錯(cuò)誤中斷 */
DMA_ClearFlag(DMA1_IT_TC4); /* 清除發(fā)送完成標(biāo)識(shí) */
DMA_Cmd(DMA1_Channel4, ENABLE); /* 啟動(dòng)DMA發(fā)送 */
}
串口待發(fā)送數(shù)據(jù)存于發(fā)送fifo中,發(fā)送處理函數(shù)需要做的的任務(wù)就是循環(huán)查詢(xún)發(fā)送fifo是否存在數(shù)據(jù),如存在則將該數(shù)據(jù)拷貝到DMA發(fā)送buf中,然后啟動(dòng)DMA傳輸。前提是需要等待上一次DMA傳輸完畢,即是DMA接收到DMA傳輸完成中斷信號(hào)'DMA_IT_TC'
。
串口發(fā)送處理函數(shù):
void uart_poll_dma_tx(uint8_t uart_id)
{
uint16_t size = 0;
if (0x01 == s_uart_dev[uart_id].status)
{
return;
}
size = fifo_read(&s_uart_dev[uart_id].tx_fifo, s_uart_dev[uart_id].dmatx_buf,
s_uart_dev[uart_id].dmatx_buf_size);
if (size != 0)
{
s_UartTxRxCount[uart_id*2+0] += size;
if (uart_id == 0)
{
s_uart_dev[uart_id].status = 0x01; /* DMA發(fā)送狀態(tài) */
bsp_uart1_dmatx_config(s_uart_dev[uart_id].dmatx_buf, size);
}
else if (uart_id == 1)
{
s_uart_dev[uart_id].status = 0x01; /* DMA發(fā)送狀態(tài),必須在使能DMA傳輸前置位,否則有可能DMA已經(jīng)傳輸并進(jìn)入中斷 */
bsp_uart2_dmatx_config(s_uart_dev[uart_id].dmatx_buf, size);
}
}
}
“DMA_IT_TC”
中斷可能比“發(fā)送狀態(tài)標(biāo)識(shí)置位”先執(zhí)行,導(dǎo)致程序誤判DMA一直處理發(fā)送狀態(tài)(發(fā)送標(biāo)識(shí)無(wú)法被清除)。注:關(guān)于DMA發(fā)送數(shù)據(jù)啟動(dòng)函數(shù),有些博客文章描述只需改變DMA發(fā)送buf的大小即可;經(jīng)過(guò)測(cè)試發(fā)現(xiàn),該方法在發(fā)送數(shù)據(jù)量較小時(shí)可行,數(shù)據(jù)量大后,導(dǎo)致發(fā)送失敗,而且不會(huì)觸發(fā)DMA發(fā)送完成中斷。因此,可靠辦法是:每次啟動(dòng)DMA發(fā)送,重新配置DMA通道所有參數(shù)。該步驟只是配置寄存器過(guò)程,實(shí)質(zhì)上不會(huì)占用很多CPU執(zhí)行時(shí)間。
DMA傳輸完成中斷處理函數(shù):
void uart_dmatx_done_isr(uint8_t uart_id)
{
s_uart_dev[uart_id].status = 0; /* 清空DMA發(fā)送狀態(tài)標(biāo)識(shí) */
}
上述串口發(fā)送處理函數(shù)可以在幾種情況調(diào)用:
void thread(void)
{
uart_poll_dma_tx(DEV_UART1);
uart_poll_dma_tx(DEV_UART2);
}
void TIMx_IRQHandler(void)
{
uart_poll_dma_tx(DEV_UART1);
uart_poll_dma_tx(DEV_UART2);
}
void DMA1_Channel4_5_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC4))
{
UartDmaSendDoneIsr(UART_2);
DMA_ClearFlag(DMA1_FLAG_TC4);
uart_poll_dma_tx(DEV_UART2);
}
}
每次拷貝多少數(shù)據(jù)量到DMA發(fā)送buf:
關(guān)于這個(gè)問(wèn)題,與具體應(yīng)用場(chǎng)景有關(guān),遵循的原則就是:只要發(fā)送fifo的數(shù)據(jù)量大于等于DMA發(fā)送buf的大小,就應(yīng)該填滿(mǎn)DMA發(fā)送buf,然后啟動(dòng)DMA傳輸,這樣才能充分發(fā)揮會(huì)DMA性能。因此,需兼顧每次DMA傳輸?shù)男屎痛跀?shù)據(jù)流實(shí)時(shí)性,參考著幾類(lèi)實(shí)現(xiàn):
/* 串口設(shè)備數(shù)據(jù)結(jié)構(gòu) */
typedef struct
{
uint8_t status; /* 發(fā)送狀態(tài) */
_fifo_t tx_fifo; /* 發(fā)送fifo */
_fifo_t rx_fifo; /* 接收f(shuō)ifo */
uint8_t *dmarx_buf; /* dma接收緩存 */
uint16_t dmarx_buf_size;/* dma接收緩存大小*/
uint8_t *dmatx_buf; /* dma發(fā)送緩存 */
uint16_t dmatx_buf_size;/* dma發(fā)送緩存大小 */
uint16_t last_dmarx_size;/* dma上一次接收數(shù)據(jù)大小 */
}uart_device_t;
/* 串口注冊(cè)初始化函數(shù) */
void uart_device_init(uint8_t uart_id)
{
if (uart_id == 1)
{
/* 配置串口2收發(fā)fifo */
fifo_register(&s_uart_dev[uart_id].tx_fifo, &s_uart2_tx_buf[0],
sizeof(s_uart2_tx_buf), fifo_lock, fifo_unlock);
fifo_register(&s_uart_dev[uart_id].rx_fifo, &s_uart2_rx_buf[0],
sizeof(s_uart2_rx_buf), fifo_lock, fifo_unlock);
/* 配置串口2 DMA收發(fā)buf */
s_uart_dev[uart_id].dmarx_buf = &s_uart2_dmarx_buf[0];
s_uart_dev[uart_id].dmarx_buf_size = sizeof(s_uart2_dmarx_buf);
s_uart_dev[uart_id].dmatx_buf = &s_uart2_dmatx_buf[0];
s_uart_dev[uart_id].dmatx_buf_size = sizeof(s_uart2_dmatx_buf);
bsp_uart2_dmarx_config(s_uart_dev[uart_id].dmarx_buf,
sizeof(s_uart2_dmarx_buf));
s_uart_dev[uart_id].status = 0;
}
}
/* 串口發(fā)送函數(shù) */
uint16_t uart_write(uint8_t uart_id, const uint8_t *buf, uint16_t size)
{
return fifo_write(&s_uart_dev[uart_id].tx_fifo, buf, size);
}
/* 串口讀取函數(shù) */
uint16_t uart_read(uint8_t uart_id, uint8_t *buf, uint16_t size)
{
return fifo_read(&s_uart_dev[uart_id].rx_fifo, buf, size);
}
依賴(lài)的fifo參考該文章:
【1】?通用環(huán)形緩沖區(qū)模塊
代碼倉(cāng)庫(kù):https://github.com/Prry/stm32f0-uart-dma
串口&DMA底層配置:
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include 'stm32f0xx.h'
#include 'bsp_uart.h'
/**
* @brief
* @param
* @retval
*/
static void bsp_uart1_gpio_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
#if 0
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
#else
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
#endif
}
/**
* @brief
* @param
* @retval
*/
static void bsp_uart2_gpio_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/**
* @brief
* @param
* @retval
*/
void bsp_uart1_init(void)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
bsp_uart1_gpio_init();
/* 使能串口和DMA時(shí)鐘 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitStructure.USART_BaudRate = 57600;
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_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); /* 使能空閑中斷 */
USART_OverrunDetectionConfig(USART1, USART_OVRDetection_Disable);
USART_Cmd(USART1, ENABLE);
USART_DMACmd(USART1, USART_DMAReq_Rx|USART_DMAReq_Tx, ENABLE); /* 使能DMA收發(fā) */
/* 串口中斷 */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* DMA中斷 */
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2_3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief
* @param
* @retval
*/
void bsp_uart2_init(void)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
bsp_uart2_gpio_init();
/* 使能串口和DMA時(shí)鐘 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_InitStructure.USART_BaudRate = 57600;
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_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); /* 使能空閑中斷 */
USART_OverrunDetectionConfig(USART2, USART_OVRDetection_Disable);
USART_Cmd(USART2, ENABLE);
USART_DMACmd(USART2, USART_DMAReq_Rx|USART_DMAReq_Tx, ENABLE); /* 使能DMA收發(fā) */
/* 串口中斷 */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* DMA中斷 */
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void bsp_uart1_dmatx_config(uint8_t *mem_addr, uint32_t mem_size)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel2);
DMA_Cmd(DMA1_Channel2, DISABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART1->TDR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)mem_addr;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; /* 傳輸方向:內(nèi)存->外設(shè) */
DMA_InitStructure.DMA_BufferSize = mem_size;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel2, DMA_IT_TC|DMA_IT_TE, ENABLE);
DMA_ClearFlag(DMA1_IT_TC2); /* 清除發(fā)送完成標(biāo)識(shí) */
DMA_Cmd(DMA1_Channel2, ENABLE);
}
void bsp_uart1_dmarx_config(uint8_t *mem_addr, uint32_t mem_size)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel3);
DMA_Cmd(DMA1_Channel3, DISABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART1->RDR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)mem_addr;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; /* 傳輸方向:外設(shè)->內(nèi)存 */
DMA_InitStructure.DMA_BufferSize = mem_size;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel3, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel3, DMA_IT_TC|DMA_IT_HT|DMA_IT_TE, ENABLE);/* 使能DMA半滿(mǎn)、全滿(mǎn)、錯(cuò)誤中斷 */
DMA_ClearFlag(DMA1_IT_TC3);
DMA_ClearFlag(DMA1_IT_HT3);
DMA_Cmd(DMA1_Channel3, ENABLE);
}
uint16_t bsp_uart1_get_dmarx_buf_remain_size(void)
{
return DMA_GetCurrDataCounter(DMA1_Channel3); /* 獲取DMA接收buf剩余空間 */
}
void bsp_uart2_dmatx_config(uint8_t *mem_addr, uint32_t mem_size)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel4);
DMA_Cmd(DMA1_Channel4, DISABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->TDR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)mem_addr;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; /* 傳輸方向:內(nèi)存->外設(shè) */
DMA_InitStructure.DMA_BufferSize = mem_size;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC|DMA_IT_TE, ENABLE);
DMA_ClearFlag(DMA1_IT_TC4); /* 清除發(fā)送完成標(biāo)識(shí) */
DMA_Cmd(DMA1_Channel4, ENABLE);
}
void bsp_uart2_dmarx_config(uint8_t *mem_addr, uint32_t mem_size)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel5);
DMA_Cmd(DMA1_Channel5, DISABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->RDR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)mem_addr;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; /* 傳輸方向:外設(shè)->內(nèi)存 */
DMA_InitStructure.DMA_BufferSize = mem_size;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel5, DMA_IT_TC|DMA_IT_HT|DMA_IT_TE, ENABLE);/* 使能DMA半滿(mǎn)、全滿(mǎn)、錯(cuò)誤中斷 */
DMA_ClearFlag(DMA1_IT_TC5);
DMA_ClearFlag(DMA1_IT_HT5);
DMA_Cmd(DMA1_Channel5, ENABLE);
}
uint16_t bsp_uart2_get_dmarx_buf_remain_size(void)
{
return DMA_GetCurrDataCounter(DMA1_Channel5); /* 獲取DMA接收buf剩余空間 */
}
壓力測(cè)試:
聯(lián)系客服