STM32的USART使用DMA--操作
注意:DMA的中斷產(chǎn)生條件中,TC=transfer complete interrupt產(chǎn)生,需要DMA的buffer滿(mǎn)才會(huì)產(chǎn)生。
本文引用地址:http://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后,再開(kāi)啟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就行了。不過(guò)在設(shè)置這一值時(shí),得先關(guān)閉DMA通道。代碼如下:
USART1的DMA接收:如果向USART1發(fā)送了數(shù)據(jù),則數(shù)據(jù)會(huì)通過(guò)DMA傳輸至初始化DMA接收通道時(shí)設(shè)置好的內(nèi)存地址處。據(jù)說(shuō)數(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è)置過(guò)一個(gè)DMA_BufferSize值嗎,只有接收滿(mǎn)了這個(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
相關(guān)推薦
技術(shù)專(zhuān)區(qū)
- FPGA
- DSP
- MCU
- 示波器
- 步進(jìn)電機(jī)
- Zigbee
- LabVIEW
- Arduino
- RFID
- NFC
- STM32
- Protel
- GPS
- MSP430
- Multisim
- 濾波器
- CAN總線(xiàn)
- 開(kāi)關(guān)電源
- 單片機(jī)
- PCB
- USB
- ARM
- CPLD
- 連接器
- MEMS
- CMOS
- MIPS
- EMC
- EDA
- ROM
- 陀螺儀
- VHDL
- 比較器
- Verilog
- 穩(wěn)壓電源
- RAM
- AVR
- 傳感器
- 可控硅
- IGBT
- 嵌入式開(kāi)發(fā)
- 逆變器
- Quartus
- RS-232
- Cyclone
- 電位器
- 電機(jī)控制
- 藍(lán)牙
- PLC
- PWM
- 汽車(chē)電子
- 轉(zhuǎn)換器
- 電源管理
- 信號(hào)放大器
評(píng)論