STM32标准库中DMA配置详解 (标准库版)

  写博客Mark下自己对STM32中DMA功能的一些分析: 

        先看上图中下左侧的偏移地址,偏移地址是相对于DMA1_BASE的相对地址。查表可得DMA1_BASE的实际物理地址是:0x4002 0000 .故我们可以得出这些寄存器的实际地址是什么。实际上我们对寄存器配置的时候也是对这几个寄存器值进行修改。

DMA_ISR:  0x4002 0000
DMA_IFCR  0x4002 0004

DMA_CCR1:      0x4002 0008
DMA_CNDTR1: 0x4002 000C
DMA_CPAR1:    0x4002 0010
DMA_CMAR1:   0x4002 0014
接下来,我们去STM32的程序中来分析下DMA配置的详细过程:
我们主要详细的讲解下两个配置函数: DMA_Configuration()和 DMA_Init()这两个函数,废话少说,先贴两个函数的代码上来。
void DMA_Configuration(void)
{
	DMA_InitTypeDef DMA_InitStructure;
	/* DMA channel1 configuration */ 
	DMA_DeInit(DMA1_Channel1);//重置DMA的寄存器的值,配置为缺省值	
	DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)&ADC1->DR;	/*设置 DMA 外设基地址,即为转换结果的寄存器*/
	DMA_InitStructure.DMA_MemoryBaseAddr =(u32)&AD_Value;/*定义内存基地址*/
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;	/*定义AD外设作为数据传输的来源*/
	DMA_InitStructure.DMA_BufferSize = N*M;/*指定DMA通道DMA缓存的大小,即需要开辟几个内存空间*/
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;	/*寄存器地址国定*/
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;	/*设定内存地址递增,即每次DMA都是将*/	
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;/* 定义外设和内存的数据宽度*/
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;/*设定DMA工作再循环缓存模式*/
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;/*设定DMA选定的通道的软件优先级*/
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//关闭内存到内存的传输   
	DMA_Init(DMA1_Channel1, &DMA_InitStructure);
	DMA_Cmd(DMA1_Channel1, ENABLE);/* Enable DMA channel1 */
}
void DMA_Init(DMA_Channel_TypeDef* DMAy_Channelx, DMA_InitTypeDef* DMA_InitStruct)
{
  uint32_t tmpreg = 0;
/*--------------------------- DMAy Channelx CCR Configuration -----------------*/
  /* Get the DMAy_Channelx CCR value */
  tmpreg = DMAy_Channelx->CCR;
  /* Clear MEM2MEM, PL, MSIZE, PSIZE, MINC, PINC, CIRC and DIR bits */
  tmpreg &= CCR_CLEAR_Mask;
  /* Configure DMAy Channelx: data transfer, data size, priority level and mode */
  /* Set DIR bit according to DMA_DIR value */
  /* Set CIRC bit according to DMA_Mode value */
  /* Set PINC bit according to DMA_PeripheralInc value */
  /* Set MINC bit according to DMA_MemoryInc value */
  /* Set PSIZE bits according to DMA_PeripheralDataSize value */
  /* Set MSIZE bits according to DMA_MemoryDataSize value */
  /* Set PL bits according to DMA_Priority value */
  /* Set the MEM2MEM bit according to DMA_M2M value */
  tmpreg |= DMA_InitStruct->DMA_DIR | DMA_InitStruct->DMA_Mode |
            DMA_InitStruct->DMA_PeripheralInc | DMA_InitStruct->DMA_MemoryInc |
            DMA_InitStruct->DMA_PeripheralDataSize | DMA_InitStruct->DMA_MemoryDataSize |
            DMA_InitStruct->DMA_Priority | DMA_InitStruct->DMA_M2M;

  /* Write to DMAy Channelx CCR */
  DMAy_Channelx->CCR = tmpreg;

/*--------------------------- DMAy Channelx CNDTR Configuration ---------------*/
  /* Write to DMAy Channelx CNDTR */
  DMAy_Channelx->CNDTR = DMA_InitStruct->DMA_BufferSize;

/*--------------------------- DMAy Channelx CPAR Configuration ----------------*/
  /* Write to DMAy Channelx CPAR */
  DMAy_Channelx->CPAR = DMA_InitStruct->DMA_PeripheralBaseAddr;

/*--------------------------- DMAy Channelx CMAR Configuration ----------------*/
  /* Write to DMAy Channelx CMAR */
  DMAy_Channelx->CMAR = DMA_InitStruct->DMA_MemoryBaseAddr;
}

  将上面两个函数比较一下就可以知道,前者函数对于后者来说就相当于是一个中间量的过程,暂时的将需要的配置参数写入一个结构体DMA_InitTypeDef中,后面调用DMA_Init这个函数之后,重新配置物理地址中DMA的寄存器相应的位。下面附录上两个函数中的结构体参数组成。

typedef struct
{
  __IO uint32_t CCR;
  __IO uint32_t CNDTR;
  __IO uint32_t CPAR;
  __IO uint32_t CMAR;
} DMA_Channel_TypeDef;
DMA_InitTypeDef  DMA_InitStructure;
typedef struct
{
  uint32_t DMA_PeripheralBaseAddr;
  uint32_t DMA_MemoryBaseAddr;   
  uint32_t DMA_DIR;  
  uint32_t DMA_BufferSize;   
  uint32_t DMA_PeripheralInc;   
  uint32_t DMA_MemoryInc;   
  uint32_t DMA_PeripheralDataSize; 
  uint32_t DMA_MemoryDataSize;    
  uint32_t DMA_Mode;    
  uint32_t DMA_Priority;      
  uint32_t DMA_M2M;                                                  
}DMA_InitTypeDef;



相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页