STM32的USART使用DMA--操作
注意:DMA的中斷產生條件中,TC=transfer complete interrupt產生,需要DMA的buffer滿才會產生。
本文引用地址:http://butianyuan.cn/article/201611/316534.htm只有當DMA內存中的數據個數達到20個時候才能產生中斷。定義如下。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時,先關閉dma,設置好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;
}
------------------------部分理解參考如下內容-----------------------
USART1的DMA發(fā)送比較簡單,在要發(fā)送之前,重新設置好DMA_BufferSize的大小,然后啟動DMA就行了。不過在設置這一值時,得先關閉DMA通道。代碼如下:
USART1的DMA接收:如果向USART1發(fā)送了數據,則數據會通過DMA傳輸至初始化DMA接收通道時設置好的內存地址處。據說數據的解析會比較麻煩,因為你不知道什么時候一條完整的指令傳輸完了,怎么做可以參考這兩處: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中斷,有一點和我預料的不一樣,初始化該通道時不是也設置過一個DMA_BufferSize值嗎,只有接收滿了這個DMA_BufferSize大小的數據后,才會產生DMA1_IT_TC5中斷,而不是接收一個字節(jié)的數據就會產生一個DMA1_IT_TC5中斷。
關鍵詞:
STM32USARTDM
評論