AnsweredAssumed Answered

stm32f4 triggered ADC, Data corruption

Question asked by khan.zarar on Mar 27, 2014
Latest reply on Jul 13, 2015 by Clive One
Hi,

I have been playing with ADC of stm32f4, i triggered ADC with TIM4_CC4 event to sample at 8Khz, using DMA. All individual components are working well but once combined i get alternate samples missing, i observe it using debugger. Data is correct as i am giving sinusoidal signal and data values seems to be correct but every alternate sample is simply 0. I am posting my code if anyone could guide, it would be great help.

ADC_InitTypeDef       ADC_InitStructure;
  ADC_CommonInitTypeDef ADC_CommonInitStructure;
  DMA_InitTypeDef       DMA_InitStructure;


  /* RCC clock settings and GPIO settings for ADC3 channel 11 */
  RCC_Configuration_ADC3();
  GPIO_Configuration_ADC3();
  TIM4_Configuration(); // set at 8KHz

  /* NVIC interrupt enable for DMA2 Stream0 */
  ADC3_DMA2_Stream0_Interrupt_Enable();

  /* RCC clock settings for DMA2 */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);

  /* DMA2 Stream0 channel2 configuration **************************************/
  DMA_InitStructure.DMA_Channel = DMA_Channel_2;
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCRxBuffer1;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  DMA_InitStructure.DMA_BufferSize = BUFFER_SIZE;
  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_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  DMA_Init(DMA2_Stream0, &DMA_InitStructure);

  /* Enable Double Buffer Mode */
  DMA_DoubleBufferModeConfig(DMA2_Stream0,(uint32_t)&ADCRxBuffer2,DMA_Memory_0);

  /* Clear Transfer Complete flag bit for DMA2_Stream0 transfer Complete */
  DMA_ClearITPendingBit(DMA2_Stream0,DMA_FLAG_TCIF0);

  DMA_DoubleBufferModeCmd(DMA2_Stream0,ENABLE);
  DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE);
  DMA_Cmd(DMA2_Stream0, ENABLE);

  /* ADC Common Init **********************************************************/
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInit(&ADC_CommonInitStructure);

  /* ADC3 Init ****************************************************************/
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T4_CC4;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;
  ADC_Init(ADC3, &ADC_InitStructure);

  /* ADC3 regular channel11 configuration *************************************/
  ADC_RegularChannelConfig(ADC3, ADC_Channel_11, 1, ADC_SampleTime_3Cycles);

 /* Enable DMA request after last transfer (Single-ADC mode) */
  ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);

  /* Enable ADC3 DMA */
  ADC_DMACmd(ADC3, ENABLE);

  /* Enable ADC3 */
  ADC_Cmd(ADC3, ENABLE);

Outcomes