新聞中心

STM32的USART使用DMA--操作

作者: 時(shí)間:2016-11-13 來源:網(wǎng)絡(luò) 收藏
使用STM32DMA來處理USART3的RX的多數(shù)據(jù)

注意:DMA的中斷產(chǎn)生條件中,TC=transfer complete interrupt產(chǎn)生,需要DMA的buffer滿才會(huì)產(chǎn)生。

本文引用地址:http://m.butianyuan.cn/article/201611/316534.htm

只有當(dāng)DMA內(nèi)存中的數(shù)據(jù)個(gè)數(shù)達(dá)到20個(gè)時(shí)候才能產(chǎn)生中斷。定義如下。DMA_InitStructure.DMA_BufferSize = 20;//sizeof(Ch3DMABuffer);

static usart3_dma_config(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
/* DMA1 Channel3 (triggered by USART1 Rx event) Config */
DMA_DeInit(DMA1_Channel3);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART3->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Ch3DMABuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 20;//sizeof(Ch3DMABuffer);
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_Low;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel3, &DMA_InitStructure);
/* Enable the End of Transfer Interrupt after */
DMA_ITConfig( DMA1_Channel3, DMA_IT_TC, ENABLE );
DMA_ITConfig(DMA1_Channel3,DMA_IT_TE,ENABLE);
USART_DMACmd(USART3,USART_DMAReq_Rx,ENABLE);
/* Enable DMA Channel3 */
DMA_Cmd(DMA1_Channel3, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
注意在中斷中,更新buffersize時(shí),先關(guān)閉dma,設(shè)置好buffersize后,再開啟dma
void DMA1_Channel3_IRQHandler(void)
{
uint16_t dma_len;
//static uint16_t rDMARear = sizeof(Ch3DMABuffer);
DMA_ClearITPendingBit(DMA1_IT_TC3);
DMA_ClearITPendingBit(DMA1_IT_TE3);
DMA_Cmd(DMA1_Channel3,DISABLE);
dma_len = DMA_GetCurrDataCounter(DMA1_Channel3);
//if(dma_len
usart3_rxbuff.len = dma_len;
DMA_SetCurrDataCounter(DMA1_Channel3,50);
DMA_Cmd(DMA1_Channel3,ENABLE);
usart3_rxbuff.rxend = 1;
}
------------------------部分理解參考如下內(nèi)容-----------------------

USART1的DMA發(fā)送比較簡(jiǎn)單,在要發(fā)送之前,重新設(shè)置好DMA_BufferSize的大小,然后啟動(dòng)DMA就行了。不過在設(shè)置這一值時(shí),得先關(guān)閉DMA通道。代碼如下:

DMA_ClearFlag(DMA1_FLAG_TC4); //清DMA發(fā)送完成標(biāo)志
DMA_Cmd(DMA1_Channel4, DISABLE); //停止DMA
DMA1_Channel4->CNDTR = sizeof(TxBuffer1) / sizeof(TxBuffer1[0]);//重設(shè)傳輸長度
DMA_Cmd(DMA1_Channel4, ENABLE); //啟動(dòng)DMA
USART1的DMA接收:如果向USART1發(fā)送了數(shù)據(jù),則數(shù)據(jù)會(huì)通過DMA傳輸至初始化DMA接收通道時(shí)設(shè)置好的內(nèi)存地址處。據(jù)說數(shù)據(jù)的解析會(huì)比較麻煩,因?yàn)槟悴恢朗裁磿r(shí)候一條完整的指令傳輸完了,怎么做可以參考這兩處:http://bbs.21ic.com/viewthread.php?tid=124248&highlight=USART+DMA
http://bbs.21ic.com/viewthread.php?tid=109346&highlight=USART+DMA
試了一下接收通道DMA1_Channel5的DMA1_IT_TC5中斷,有一點(diǎn)和我預(yù)料的不一樣,初始化該通道時(shí)不是也設(shè)置過一個(gè)DMA_BufferSize值嗎,只有接收滿了這個(gè)DMA_BufferSize大小的數(shù)據(jù)后,才會(huì)產(chǎn)生DMA1_IT_TC5中斷,而不是接收一個(gè)字節(jié)的數(shù)據(jù)就會(huì)產(chǎn)生一個(gè)DMA1_IT_TC5中斷。


關(guān)鍵詞: STM32USARTDM

評(píng)論


技術(shù)專區(qū)

關(guān)閉