运用STM32的DMA来处理USART3的RX的多数据
留意:DMA的中止发生条件中,TC=transfer complete interrupt发生,需求DMA的buffer满才会发生。
只要当DMA内存中的数据个数到达20个时分才干发生中止。界说如下。DMA_InitStructure.DMA_BufferSize = 20;//sizeof(Ch3DMABuffer);
static usart3_dma_config(void)
{
DMA_InitTypeDef DMA_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);
}
留意在中止中,更新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发送比较简单,在要发送之前,从头设置好DMA_BufferSize的巨细,然后发动DMA就行了。不过在设置这一值时,得先封闭DMA通道。代码如下:
USART1的DMA接纳:假如向USART1发送了数据,则数据会经过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中止,而不是接纳一个字节的数据就会发生一个DMA1_IT_TC5中止。
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ceping/baogao/326015.html