AnsweredAssumed Answered

STM32F303VC ADC+DMA problem

Question asked by Vycas.Arturas.001 on Jun 18, 2015
Latest reply on Jun 19, 2015 by Vycas.Arturas.001
Hi everyone, i am facing a problem where i have to read 5 or more ADC channels. Of course i want to use DMA for that, but i am having some kind of problem with it. The code seems to be working, DMA_TC interrupt is generated - i have a variable which is incremented every time DMA transfer is completed. But the result i get from ADC read are bad. Right now i am trying to read five ADC channels, PA0-PA4 , and PF4, which corresponds to the ADC1_Channel1 to ADC1_Channel5, but i am always reading bad values. I have buffer which can hold 5 values from ADC read, but i only get 3 values in my buffer instead of 5, and those are crappy. 

ADC_ConvertedValue[0] = 0xF920F93; ADC_ConvertedValue [1] = 0xF910F66, ADC_ConvertedValue[2] = 0x0F10, ADC_ConvertedValue[3] and [4] shows 0x0000. 

Actually all channels should read something like ADC_ConvertedValue[2]. Seems that this value is correct, all the channels should read pretty much the same value.

Could anyone help me to see what i have missed and i why do i get crappy results? The code is below.

#include "stm32f30x.h"


#define buffersize 5


/* Private variables */
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_ClocksTypeDef RCC_Struct;


/* Global Variables */
unsigned char clock; // global variable to hold clock value to know from where processor is clocked.
/*             - 0x00: HSI used as system clock
*              - 0x04: HSE used as system clock
*              - 0x08: PLL used as system clock
*/
uint32_t ADC_ConvertedValue[buffersize];


/* Function prototypes */
void DMA_Configuration(void);
void ADC_Configuration(void);
void GPIO_Configuration(void);
void Interrupt_Configuration(void);
void DelayNop(unsigned short nopcount);

unsigned char DMA_Conversion_Counter=0;


void DMA1_Channel1_IRQHandler(void)
{
  if(DMA_GetITStatus(DMA1_IT_TC1) != RESET)
  {
  DMA_ClearITPendingBit(DMA1_IT_TC1);
//  DMA_ClearFlag(DMA1_IT_TC1); // Clear DMA transfer complete flag
  DMA_Conversion_Counter++;
  DMA1->IFCR |= 0x00;
  }
}


int main(void)
{
     /* Configure System Clock to be 48MHz*/
     RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
     while(!RCC_GetFlagStatus(RCC_FLAG_HSIRDY ));
     RCC_PLLCmd(DISABLE);
     RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_12);
     RCC_PLLCmd(ENABLE);
     while(!RCC_GetFlagStatus(RCC_FLAG_PLLRDY));
     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);


     RCC_GetClocksFreq(&RCC_Struct);
     clock = RCC_GetSYSCLKSource();


     /* Configure Clocks */
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); // Enable clock for port A.
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); // Enable clock for port B.
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE); // Enable clock for port B.
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // Enable TIM3 clock.
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE); // Enable ADC clock
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); // Enable DMA1 clock
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);


       ADC_DeInit(ADC1); // DeInit ADC before any configuration
       /* Call all configuration functions here */
       ADC_Configuration();
       GPIO_Configuration();
       DMA_Configuration();
       Interrupt_Configuration();


       DMA_Cmd(DMA1_Channel1, ENABLE);


       DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);


     while(1)
     {
          ADC_StartConversion(ADC1); // Start conversion
          DelayNop(20000); // Dumb delay
     }
}


void DMA_Configuration(void)
{
  // Reset DMA strctuure
  DMA_DeInit(DMA1_Channel1);


  /* DMA1 channel1 configuration */
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(ADC1->DR);
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC_ConvertedValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = buffersize;
  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_VeryHigh;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;  // Memory to memory transfer disable
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
}


void ADC_Configuration(void)
{
  // DelayNop(20000); // Dumb delay


  // Configure ADC for temp sensor
  ADC_InitStructure.ADC_Resolution= ADC_Resolution_12b;
  ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable;
  ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigEventEdge_None;
  ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigInjecEventEdge_None; // This parameter is ignored if ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfRegChannel = buffersize; // This parameter is ignored if Single channel mode is selected
  ADC_Init(ADC1, &ADC_InitStructure);


  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_Clock = ADC_Clock_SynClkModeDiv1;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
  ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0x00;
  ADC_CommonInit(ADC1, &ADC_CommonInitStructure);


   /* Configure channels */
  ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_61Cycles5);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 2, ADC_SampleTime_61Cycles5);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 3, ADC_SampleTime_61Cycles5);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 4, ADC_SampleTime_61Cycles5);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 5, ADC_SampleTime_61Cycles5);
//  ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 6, ADC_SampleTime_61Cycles5);
//  ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 7, ADC_SampleTime_61Cycles5);
  ADC_DMACmd(ADC1, ENABLE);
  ADC_Cmd(ADC1, ENABLE);
  while(!(ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY)));
}


void Interrupt_Configuration(void)
{
  /* Configure NVIC for DMA1 channel */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); // 1 bit for preemtion, 3 bits for subpriority
  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}


void GPIO_Configuration(void)
{
     /* Pin config for ADC */
       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
       GPIO_Init(GPIOA, &GPIO_InitStructure);


       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
       GPIO_Init(GPIOF, &GPIO_InitStructure);


//       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
//       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
//       GPIO_Init(GPIOC, &GPIO_InitStructure);
}


void DelayNop(unsigned short nopcount)
{
  unsigned short a = nopcount;
  while(a--) asm("nop");
}




Outcomes