AnsweredAssumed Answered

ADC->DMA on Keil STM32F400 board

Question asked by Gerber on Jun 30, 2014
Latest reply on Jun 30, 2014 by Gerber
Hi,

It seems that DMA is not working properly. Based on the code below I don't get ADC values on the DMA destination adres "ADC_Raw"
When I check the ADC peripheral address (ADC3_RDR), I can see the analog values, so the ADC values are available for DMA.

Why are the analog values not processed by DMA? Do I miss something?
I'm using a Keil STM32F400 board. 

/*----------------------------------------------------------------------------
 *      ADC_DMA
 *---------------------------------------------------------------------------*/
  
#include "ADC_DMA.h"
#include <stm32f4xx.h>
 
//====================================================================================
//   Configuring TIM2 to trigger at 2kHz which is the ADC sampling rate
//====================================================================================
void TIM2_Config(void)
{
  TIM_TimeBaseInitTypeDef TIM2_TimeBase;
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  
  TIM_TimeBaseStructInit(&TIM2_TimeBase);
  TIM2_TimeBase.TIM_Period        = (uint16_t)49; // Trigger = CK_CNT/(49+1) = 2kHz
  TIM2_TimeBase.TIM_Prescaler     = 420;          // CK_CNT = 42MHz/420 = 100kHz
  TIM2_TimeBase.TIM_ClockDivision = 0;
  TIM2_TimeBase.TIM_CounterMode   = TIM_CounterMode_Up; 
  TIM_TimeBaseInit(TIM2, &TIM2_TimeBase);
  TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
 TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  TIM_Cmd(TIM2, ENABLE);
}
 
//====================================================================================
//   Configuring GPIO PD9 (for testing)
//====================================================================================
void GPIOH_Config(void)
{
  GPIO_InitTypeDef gpio_H;
  
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);
  
  gpio_H.GPIO_Mode  = GPIO_Mode_OUT;
  gpio_H.GPIO_OType = GPIO_OType_PP;
  gpio_H.GPIO_Pin   = GPIO_Pin_3;
  gpio_H.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  gpio_H.GPIO_Speed = GPIO_Medium_Speed;
  GPIO_Init(GPIOH, &gpio_H);
}
 
//====================================================================================
//   Configuring ADC with DMA
//====================================================================================
void ADC_Config(void)
{
  ADC_InitTypeDef       ADC_INIT;
  ADC_CommonInitTypeDef ADC_COMMON;
  DMA_InitTypeDef       DMA_INIT;
 GPIO_InitTypeDef            gpio_F;
 
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOF, ENABLE);  //Enable DMA2 clock 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);  //Enable ADC3 clock
         
  DMA_INIT.DMA_Channel = DMA_Channel_0; 
  DMA_INIT.DMA_PeripheralBaseAddr = (uint32_t)ADC3_RDR; //0x4001224C
  DMA_INIT.DMA_Memory0BaseAddr    = (uint32_t)&ADC_Raw[0];
  DMA_INIT.DMA_DIR                = DMA_DIR_PeripheralToMemory;
  DMA_INIT.DMA_BufferSize         = NS;
  DMA_INIT.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;
  DMA_INIT.DMA_MemoryInc          = DMA_MemoryInc_Enable;
  DMA_INIT.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_INIT.DMA_MemoryDataSize     = DMA_MemoryDataSize_HalfWord;
  DMA_INIT.DMA_Mode               = DMA_Mode_Circular;
  DMA_INIT.DMA_Priority           = DMA_Priority_High;
  DMA_INIT.DMA_FIFOMode           = DMA_FIFOMode_Disable;        
  DMA_INIT.DMA_FIFOThreshold      = DMA_FIFOThreshold_HalfFull;
  DMA_INIT.DMA_MemoryBurst        = DMA_MemoryBurst_Single;
  DMA_INIT.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single;
  DMA_Init(DMA2_Stream4, &DMA_INIT);
  DMA_Cmd(DMA2_Stream4, ENABLE);
 
  gpio_F.GPIO_Mode  = GPIO_Mode_AN;
  gpio_F.GPIO_Pin   = GPIO_Pin_9;
  gpio_F.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOF, &gpio_F);
 
  ADC_COMMON.ADC_Mode             = ADC_Mode_Independent;
  ADC_COMMON.ADC_Prescaler        = ADC_Prescaler_Div2;
  ADC_COMMON.ADC_DMAAccessMode    = ADC_DMAAccessMode_Disabled;
  ADC_COMMON.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInit(&ADC_COMMON);
 
  ADC_INIT.ADC_Resolution           = ADC_Resolution_12b;
 
  ADC_INIT.ADC_ScanConvMode         = DISABLE;
  ADC_INIT.ADC_ContinuousConvMode   = DISABLE; // ENABLE for max ADC sampling frequency
  ADC_INIT.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
  ADC_INIT.ADC_ExternalTrigConv     = ADC_ExternalTrigConv_T2_TRGO;
 ADC_INIT.ADC_DataAlign            = ADC_DataAlign_Right;
  ADC_INIT.ADC_NbrOfConversion      = 1;
  ADC_Init(ADC3, &ADC_INIT);
 
  ADC_RegularChannelConfig(ADC3, ADC_Channel_7, 1, ADC_SampleTime_3Cycles);
  ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);
  ADC_DMACmd(ADC3, ENABLE);
  ADC_Cmd(ADC3, ENABLE);
}

Outcomes