cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f4 triggered ADC, Data corruption

usama
Associate II
Posted on March 27, 2014 at 15:55

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);

#stm32f4 #stm32 #discovery #selective-cut-n-paste #adc-and-dma
10 REPLIES 10
Posted on July 13, 2015 at 16:56

A bit off-topic. Well I suspect your choice was cost driven, rather than trying several alternatives and picking the most effective one. CooCox has an issue with supporting CMSIS properly, and that a lot of the software they use for debugging is not terribly robust.

You should pick something that works for you, be willing to try other things so you can quantify good vs bad.

The standard peripheral libraries for the STM32F4, and STM32F4-DISCO come with a selection of examples. I've posted many other examples here on the forum. At the end of the day working with the hardware/software requires a good solid understanding of the platform, and I gain that by reading, and rereading the manuals.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..