STM32 DMA->内存到内存

来源:互联网 发布:广数控g72编程实例有图 编辑:程序博客网 时间:2024/06/04 19:20

基于STM32 F401 Discovery板:

DMA2在AHB1总线上



步骤一:使能DMA

#define DMA_STREAM_CLOCK         RCC_AHB1Periph_DMA2   RCC_AHB1PeriphClockCmd(DMA_STREAM_CLOCK, ENABLE);
步骤二:reset DMA Stream register:

/* Reset DMA Stream registers (for debug purpose) */  DMA_DeInit(DMA_STREAM);
步骤三:

/* Check if the DMA Stream is disabled before enabling it.     Note that this step is useful when the same Stream is used multiple times:     enabled, then disabled then re-enabled... In this case, the DMA Stream disable     will be effective only at the end of the ongoing data transfer and it will      not be possible to re-configure it before making sure that the Enable bit      has been cleared by hardware. If the Stream is used only once, this step might      be bypassed. */  while (DMA_GetCmdStatus(DMA_STREAM) != DISABLE)  {  }
步骤四:初始化DMA 结构体
/* Configure DMA Stream */  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有发送完成中断,完成一半中断,和错误中断等

步骤五:开始传输

/* DMA Stream enable */  DMA_Cmd(DMA_STREAM, ENABLE);


0 0
原创粉丝点击