新聞中心

STM32的DMA控制器

作者: 時(shí)間:2016-11-13 來(lái)源:網(wǎng)絡(luò) 收藏
  DMA(直接存儲(chǔ)器存取)用來(lái)提供在外設(shè)和存儲(chǔ)器之間或者存儲(chǔ)器與存儲(chǔ)器之間的高速數(shù)據(jù)傳輸。無(wú)須CPU干預(yù),這就可以節(jié)省CPU的資源來(lái)做其它操作。

  兩個(gè)DMA控制器有12個(gè)通道(DMA1有7通道,DMA2有5通道),還有一個(gè)仲裁器協(xié)調(diào)各個(gè)DMA請(qǐng)求的估先權(quán)。

本文引用地址:http://butianyuan.cn/article/201611/316146.htm

  有關(guān)DMA控制器的特性、框圖、寄存器地址及含義等參考STN32資料手冊(cè),在此就不多說(shuō)了,只講述具體的設(shè)置應(yīng)用。

  結(jié)構(gòu)體(DMA_InitTypeDef)說(shuō)明:

  PeripheralBaseAddr:外設(shè)地址 CPAR

  MemoryBaseAddr: 存儲(chǔ)器地址 CMAR

  DIR:        傳輸方向(從外設(shè)讀/從存儲(chǔ)器讀)

  BufferSize:     傳輸數(shù)量

  PeripheralInc:    外設(shè)地址增量模式(傳完一個(gè)數(shù)后地址是否+1)

  MemoryInc:     存儲(chǔ)器地址增量模式

  PeripheralDataSize: 外設(shè)數(shù)據(jù)寬度(Byte / Half Word / Word)

  MemoryDataSize:  存儲(chǔ)器數(shù)據(jù)寬度

  Mode:        循環(huán)/正常

  Priority:       優(yōu)先級(jí)

  M2M:        存儲(chǔ)器到存儲(chǔ)器模式

  下面舉幾個(gè)例子,例子都是在IAR5.4平臺(tái),STM32F101R8經(jīng)過(guò)測(cè)試的:
  (在此只列出DMA的設(shè)置過(guò)程,相關(guān)的RCC、GPIO、USART、ADC等設(shè)置省略,可參照本博客的其它篇章。)

/*-------------------------------------------------------------------------------------------------*/
// 例1:USART1通過(guò)DMA1的Channel4發(fā)送數(shù)據(jù),發(fā)送20個(gè)8位數(shù)后停止
/*-------------------------------------------------------------------------------------------------*/

#define USART1_DR_Base 0x40013804
u8 USART1_Buffer_Tx[50];
void DMA_Configuration(void)
{
  DMA_InitTypeDef DMA_InitStructure;

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);      // 打開(kāi)DMA1電源

  DMA_DeInit(DMA1_Channel4);                     // 清除原有設(shè)置


  DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base;    // 外設(shè)地址:USART1的接收地址
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART1_Buffer_Tx; // 要傳送的數(shù)組起始地址
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;         // 存儲(chǔ)器到外設(shè)
  DMA_InitStructure.DMA_BufferSize = 20;                // 傳20個(gè)數(shù)
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;   // 外設(shè)地址不改變
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;      // 存儲(chǔ)器傳完一個(gè)數(shù)后地址+1
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;// 8位
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;  // 8位
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;          // 傳輸20個(gè)數(shù)后停止,進(jìn)入中斷
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;          // 優(yōu)先級(jí)
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;           // 不使用M2M
  DMA_Init(DMA1_Channel4, &DMA_InitStructure);


  DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);         // 中斷使能,傳輸20個(gè)數(shù)后停止,進(jìn)入中斷

   USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);       // USART1的DMA使能
   DMA_Cmd(DMA1_Channel4, ENABLE);                 // DMA啟動(dòng),開(kāi)始送數(shù)
}

/*---------------------------------------------------------------------------------------------------------------------------------------------*/
// 例2:ADC通過(guò)DMA1的Channel1采集數(shù)據(jù)存入ADC_RegularConvertedValueTab數(shù)組,每次采704個(gè)16位數(shù)
/*----------------------------------------------------------------------------------------------------------------------------------------------*/

void ADC_Configuration(void)
{
  ADC_InitTypeDef ADC_InitStructure;
  DMA_InitTypeDef DMA_InitStructure;

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)0x4001244C;
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_RegularConvertedValueTab;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 704;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
  DMA_Cmd(DMA1_Channel1, DISABLE);

  /* ADC1 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 1;
  ADC_Init(ADC1, &ADC_InitStructure);

  ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 1, ADC_SampleTime_13Cycles5);

  ADC_DMACmd(ADC1, ENABLE);
  ADC_ExternalTrigConvCmd(ADC1, ENABLE);
  ADC_Cmd(ADC1, ENABLE);
}



關(guān)鍵詞: STM32DMA控制

評(píng)論


技術(shù)專區(qū)

關(guān)閉