AnsweredAssumed Answered

STM32F303RE ADC2+DMA2 Don't working

Question asked by Mucit23 on Feb 12, 2018
Latest reply on Feb 16, 2018 by AvaTar

Hi

I am trying to develop a project via the STM32F303RE Nucle Board. In the project, I need to read analog data from channel 0 (PA4) on the ADC2 hardware.  For this I made an init code.

 

void ADC_DMA_Configuration(void)
{
ADC_InitTypeDef        ADC_InitStructure;
  ADC_CommonInitTypeDef  ADC_CommonInitStructure;
  GPIO_InitTypeDef       GPIO_InitStructure;
  DMA_InitTypeDef        DMA_InitStructure;

  /* Configure the ADC clock */
  RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div1);  
  /* Enable ADC1 clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);
  /* Enable GPIOA and GPIOC Periph clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  /* Enable DMA2 clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);

  /* ADC Channels configuration */
  /* Configure ADC2 Channel0 as analog input */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* DMA configuration */
  /* DMA2 Channel1 Init Test */
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC2->DR;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 1;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

  DMA_Init(DMA2_Channel1, &DMA_InitStructure); 

  ADC_StructInit(&ADC_InitStructure);

  /* ADC Calibration procedure */
  ADC_VoltageRegulatorCmd(ADC2, ENABLE);

  /* Insert delay equal to 10 µs */
  Delay(10);

  ADC_SelectCalibrationMode(ADC2, ADC_CalibrationMode_Single);
  ADC_StartCalibration(ADC2);

  while(ADC_GetCalibrationStatus(ADC2) != RESET );
  calibration_value_1 = ADC_GetCalibrationValue(ADC2);

  /* ADC Dual mode configuration */     
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Interleave;                                                                    
  ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;                    
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;            
  ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular;                  
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = 10;          
  ADC_CommonInit(ADC2, &ADC_CommonInitStructure);

  /* */  
  ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable;
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; 
  ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;         
  ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;   
  ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;  
  ADC_InitStructure.ADC_NbrOfRegChannel = 1;
  ADC_Init(ADC2, &ADC_InitStructure);

  /* ADC2 regular channel1 configuration */ 
  ADC_RegularChannelConfig(ADC2, ADC_Channel_1, 1, ADC_SampleTime_7Cycles5);

  /* Configures the ADC DMA */
  ADC_DMAConfig(ADC2, ADC_DMAMode_Circular);
  /* Enable the ADC DMA */
  ADC_DMACmd(ADC2, ENABLE);
   
  /* Enable ADC2  */
  ADC_Cmd(ADC2, ENABLE);

  /* wait for ADC1 ADRDY */
  while(!ADC_GetFlagStatus(ADC2, ADC_FLAG_RDY));

  /* Enable the DMA channel */
  DMA_Cmd(DMA2_Channel1, ENABLE);

  /* Start ADC2 Software Conversion */ 
  ADC_StartConversion(ADC2); 
 
}

When I run these codes I can't read adc.  I ran the same codes with ADC1 and DMA1. But ADC2 and DMA2 do not work.

Is there something I'm missing? Why does not work?

 

Thanks

Outcomes