cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F303RE ADC2+DMA2 Don't working

ferhatyol-23
Senior
Posted on February 12, 2018 at 16:09

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.

0690X00000604NIQAY.jpg

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

Thanks

8 REPLIES 8
Posted on February 12, 2018 at 17:13

>>

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

Nothing jumping out immediately. Code not compilable/complete.

Probe DMA, ADC in Peripheral View mode, inspect registers, see if errors flagging, addresses as expected.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 13, 2018 at 08:33

in Debug Mode, I see the ADC2->DR register. This register always appears to be zero. I guess the ADC2 hardware is not working. I don't understand what the problem is.

What is missing in the code? The ADC1 Hardware is working in the same way. But ADC2 is not working. 

ferhatyol-23
Senior
Posted on February 14, 2018 at 07:53

Is there anyone who can help? I can not run ADC2.

AvaTar
Lead
Posted on February 14, 2018 at 10:00

I don't have your board.

But the F3 DSP Lib (STM32F30x_DSP_StdPeriph_Lib) contains a working dual-mode ADC example (ADC1+2) you can work with, and compare.

Posted on February 16, 2018 at 10:27

But I am running ADC with Single ended Mode.What is the difference between ADC1 and ADC2?Because the same codes work with ADC1.But ADC1 does not match any channel PA4.

Turvey.Clive.002

When I watch the ADC2 registers with Debug, I see thatADC2->DR Register always Zero.Is there a configuration code with ADC2?

Thanks

Posted on February 16, 2018 at 11:14

But I am running ADC with Single ended Mode.

Really ?

  ADC_CommonInitStructure.ADC_Mode =

ADC_Mode_Interleave

;       
Posted on February 16, 2018 at 14:00

Thank you for the reply, Yes, I made a mistake there. ADC_Mode Should Be 'ADC_Mode_Independent'. But I can't understand how the ADC1 works with these settings.

Posted on February 16, 2018 at 18:21

But I can't understand how the ADC1 works with these settings.

If you refer to your code given in the first post that initializes ADC2, me neither