STM32 DMA->内存到内存
扫描二维码
随时随地手机看文章
基于STM32 F401 Discovery板:
DMA2在AHB1总线上
步骤一:使能DMA
#defineDMA_STREAM_CLOCKRCC_AHB1Periph_DMA2
RCC_AHB1PeriphClockCmd(DMA_STREAM_CLOCK,ENABLE);
步骤二:reset DMA Stream register:
/*ResetDMAStreamregisters(fordebugpurpose)*/
DMA_DeInit(DMA_STREAM);
步骤三:
/*CheckiftheDMAStreamisdisabledbeforeenablingit.
NotethatthisstepisusefulwhenthesameStreamisusedmultipletimes:
enabled,thendisabledthenre-enabled...Inthiscase,theDMAStreamdisable
willbeeffectiveonlyattheendoftheongoingdatatransferanditwill
notbepossibletore-configureitbeforemakingsurethattheEnablebit
hasbeenclearedbyhardware.IftheStreamisusedonlyonce,thisstepmight
bebypassed.*/
while(DMA_GetCmdStatus(DMA_STREAM)!=DISABLE)
{
}
步骤四:初始化DMA 结构体
/*ConfigureDMAStream*/
DMA_InitStructure.DMA_Channel=DMA_CHANNEL;
DMA_InitStructure.DMA_PeripheralBaseAddr=(uint32_t)SRC_Const_Buffer;
DMA_InitStructure.DMA_Memory0BaseAddr=(uint32_t)DST_Buffer;
DMA_InitStructure.DMA_DIR=DMA_DIR_MemoryToMemory;
DMA_InitStructure.DMA_BufferSize=(uint32_t)BUFFER_SIZE;
DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Enable;
DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode=DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority=DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode=DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold=DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst=DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst=DMA_PeripheralBurst_Single;
DMA_Init(DMA_STREAM,&DMA_InitStructure);
其中:
DMA_PeripheralBaseAddr--》外设地址,可以选择USART,I2C,ADC等
DMA_Memory0BaseAddr-》memory地址,用于外设和内存的数据传输
DMA_DIR -》传送方向,分别有外设到内存,内存到外设,内存到内存
DMA_BufferSize -》传输的buffer size
DMA_PeripheralInc -》外设地址是否自加
DMA_MemoryInc -》内存地址是否自加
DMA_PeripheralDataSize -》外设每个data的size,分别有1byte,半字,全字
DMA_MemoryDataSize -》内存的每个data的size,如上
DMA_Mode -》DMA mode,分别是传输完了是否循环还是正常
步骤四:DMA使能中断:
DMA_ITConfig(DMA_STREAM,DMA_IT_TC,ENABLE);
其中DMA有发送完成中断,完成一半中断,和错误中断等
步骤五:开始传输
/*DMAStreamenable*/
DMA_Cmd(DMA_STREAM,ENABLE);