STM32的USART使用DMA--操作
注意: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;
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发送比较简单,在要发送之前,重新设置好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中断。
STM32USARTDM 相关文章:
- STM32中USART的DMA 实现(12-03)
- 实现STM32中USART的DMA(11-20)
- STM32的USART DMA传输(转)(11-19)
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)