AnsweredAssumed Answered

STM32F3 ADC/DMA problem

Question asked by klockiewicz.florian on May 15, 2014
Latest reply on May 16, 2014 by Clive One
Hello,
I'm a new user of STM32F3 discovery board,
My aim is to acquire 3 voltage at 36kHz (current sensing with shunt resistor) and to update dutycycle of 3 PWM;

Following advices from this forum (mainly from clive1) and STM32F3 discovery examples i managed to make PWMs work but i'm stuck with ADC and DMA.

I think i'm missing something about some DMA interrupt handler... ADC acquisition works only once and is never refreshed.

my code looks like this :
main.c
/* Includes ------------------------------------------------------------------*/
#include "main.h"
 
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;
__IO uint16_t CCR1_Val = 40961; //__IO uint16_t CCR1_Val = 40961;
__IO uint16_t CCR2_Val = 27309;//__IO uint16_t CCR2_Val = 27309;
__IO uint16_t CCR3_Val = 27027;//333Hz//__IO uint16_t CCR3_Val = 13654;
__IO uint16_t CCR4_Val = 225;//40kHz//__IO uint16_t CCR4_Val = 6826;
 
uint16_t PrescalerValue = 0;
 
__IO uint16_t  ADC1ConvertedValue = 0, ADC1ConvertedVoltage = 0, calibration_value = 0;
__IO uint32_t TimingDelay = 0;
ADC_InitTypeDef         ADC_InitStructure;
ADC_CommonInitTypeDef   ADC_CommonInitStructure;
GPIO_InitTypeDef        GPIO_InitStructure;
DMA_InitTypeDef         DMA_InitStructure;
volatile unsigned short ADC_Val[3], DutyCnt=0;
volatile unsigned short A, B, C;
 
/* Private function prototypes -----------------------------------------------*/
static void RCC_Configuration(void);
static void GPIO_Configuration(void);
static void NVIC_Configuration(void);
static void TIM_Config_TimeBase(void);
static void TIM_Config_PWM(void);
static void ADC_Config(void);
/* Private functions ---------------------------------------------------------*/
 
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /* RCC Configuration */
  RCC_Configuration();
   
  /* GPIO Configuration */
  GPIO_Configuration();
   
  /* NVIC Configuration */
  NVIC_Configuration();
   
  /* PWM Timer Configuration */
  TIM_Config_PWM();
   
  /* TimeBase Configuration */
  TIM_Config_TimeBase();
   
  /* ADC Configuration */
  ADC_Config();
   
  /* Infinite loop */
  while (1)
  {
  }
}
 
void Delay(__IO uint32_t nTime)
{
  TimingDelay = nTime;
 
  while(TimingDelay != 0);
}
 
static void RCC_Configuration(void)
{
  RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2);
   
  /* Enable GPIO Clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD | RCC_AHBPeriph_GPIOC, ENABLE);
   
  /* Enable DMA1 */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
   
  /* TIM3/4 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4, ENABLE);
                                                                          
  /* Enable ADC1 Clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);
}
 
static void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure_PWM;
 
  /* ---------------------------------------------------------- */ 
  /* Configure PD.12, 13, 14 as PWM Output (Alternate Function) */
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_2);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_2);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_2);
    
  GPIO_InitStructure_PWM.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14;
  GPIO_InitStructure_PWM.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure_PWM.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure_PWM.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure_PWM.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOD, &GPIO_InitStructure_PWM);
  /* ---------------------------------------------------------- */
   
}
 
static void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
 
  /* Enable the TIM3 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
 
static void TIM_Config_TimeBase(void)
{
  /* Initialize Leds mounted on STM32F3-Discovery EVAL board */
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);
  STM_EVAL_LEDInit(LED5);
  STM_EVAL_LEDInit(LED6);
 
  /* Turn on LED3, LED4, LED5 and LED6 */
  STM_EVAL_LEDOn(LED3);
  STM_EVAL_LEDOn(LED4);
  STM_EVAL_LEDOn(LED5);
  STM_EVAL_LEDOn(LED6);
   
  /* Compute the prescaler value */
  PrescalerValue = (uint16_t) ((SystemCoreClock) / 9000000) - 1;
 
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 65535;
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
 
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
 
  /* Prescaler configuration */
  TIM_PrescalerConfig(TIM3, PrescalerValue, TIM_PSCReloadMode_Immediate);
 
  /* Output Compare Timing Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
 
  TIM_OC1Init(TIM3, &TIM_OCInitStructure);
 
  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
 
  /* Output Compare Timing Mode configuration: Channel2 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
 
  TIM_OC2Init(TIM3, &TIM_OCInitStructure);
 
  TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
 
  /* Output Compare Timing Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
 
  TIM_OC3Init(TIM3, &TIM_OCInitStructure);
 
  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
 
  /* Output Compare Timing Mode configuration: Channel4 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
 
  TIM_OC4Init(TIM3, &TIM_OCInitStructure);
 
  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
    
  /* TIM Interrupts enable */
  TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
 
  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);
}
 
static void TIM_Config_PWM(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  TIM_OCInitTypeDef TIM_OCInitStructure2;
   
  TIM_TimeBaseStructure.TIM_Prescaler = 2 - 1; //72 to 36 MHz
  TIM_TimeBaseStructure.TIM_Period = 1000 - 1; //36 MHz to 36 kHz
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
   
  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  TIM_OCInitStructure2.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure2.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure2.TIM_Pulse = 1000 / 4; //25%
  TIM_OCInitStructure2.TIM_OCPolarity = TIM_OCPolarity_High;
   
  TIM_OC1Init(TIM4, &TIM_OCInitStructure2);
  TIM_OC2Init(TIM4, &TIM_OCInitStructure2);
  TIM_OC3Init(TIM4, &TIM_OCInitStructure2);
   
  TIM_Cmd(TIM4, ENABLE); 
}
 
static void ADC_Config(void)
{
   /* Configure the ADC clock */
  RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2);
   
  /* Enable ADC1 clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);
       
  /* Setup SysTick Timer for 1 µsec interrupts  */
  if (SysTick_Config(SystemCoreClock / 1000000))
  {
    /* Capture error */
    while (1)
    {}
  }
   
  /* DMA configuration */ 
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
   
    DMA_InitStructure.DMA_BufferSize = 3;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC_Val[0];
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
 
    DMA_Init(DMA1_Channel1, &DMA_InitStructure);
   
  /* ADC Channel configuration */
  /* GPIOC Periph clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
 
  /* Configure ADC Channel 6 & 7 as analog input */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_3 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
   
  ADC_StructInit(&ADC_InitStructure);
 
  /* Calibration procedure */ 
  ADC_VoltageRegulatorCmd(ADC1, ENABLE);
 
  /* Insert delay equal to 10 µs */
  Delay(10);
 
  ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);
  ADC_StartCalibration(ADC1);
   
  while(ADC_GetCalibrationStatus(ADC1) != RESET );
  calibration_value = ADC_GetCalibrationValue(ADC1);
      
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;                                                                   
  ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;                   
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;         
  ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular;             
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0;
  ADC_CommonInit(ADC1, &ADC_CommonInitStructure);
   
  ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable;
  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 = 3;
  ADC_Init(ADC1, &ADC_InitStructure);
   
  /* ADC1 regular channel 6, 7 & 9 configuration */
  ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 1, ADC_SampleTime_19Cycles5); //PC0
  ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 2, ADC_SampleTime_19Cycles5); //PC1
  ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 3, ADC_SampleTime_19Cycles5); //PC3
   
  DMA_Cmd(DMA1_Channel1, ENABLE);
  ADC_DMACmd(ADC1, ENABLE);
    
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);
  ADC_GetConversionValue(ADC1);
  /* wait for ADRDY */
  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY));
   
  /* Start ADC1 Software Conversion */
  ADC_StartConversion(ADC1);
}
 
 
#ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
  /* Infinite loop */
  while (1)
  {
  }
}
#endif

stm32f30x_it.c
/* Includes ------------------------------------------------------------------*/
#include "main.h"
 
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint16_t capture = 0;
extern __IO uint16_t CCR1_Val;
extern __IO uint16_t CCR2_Val;
extern __IO uint16_t CCR3_Val;
extern __IO uint16_t CCR4_Val;
extern __IO uint16_t ADC1ConvertedValue;
extern __IO uint16_t ADC1ConvertedVoltage;
extern __IO uint16_t calibration_value;
extern __IO uint32_t TimingDelay;
extern __IO uint16_t A, B, C, ADC_Val[3], DutyCnt;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
 
/******************************************************************************/
/*            Cortex-M4 Processor Exceptions Handlers                         */
/******************************************************************************/
 
/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}
 
/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}
 
/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}
 
/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}
 
/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}
 
/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}
 
/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}
 
/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}
 
/**                                                                                              
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
  TimingDelay--;
}
 
/******************************************************************************/
/*                 STM32F30x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f30x.s).                                            */
/******************************************************************************/
 
/**
  * @brief  This function handles TIM3 global interrupt request.
  * @param  None
  * @retval None
  */
void TIM3_IRQHandler(void)
{
 
  if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
  }
  else if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
  }
  else if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
     
    /* Interrupt frequency = 333 Hz */
    STM_EVAL_LEDToggle(LED5);
     
    TIM4->CCR1 = (DutyCnt+ 333)%1000;
    TIM4->CCR2 = (DutyCnt+ 666)%1000;
    TIM4->CCR3 = DutyCnt++%1000;
    capture = TIM_GetCapture3(TIM3);
    TIM_SetCompare3(TIM3, capture + CCR3_Val);
  }
  else
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
     
    /* Interrupt frequency = 40 kHz */
    STM_EVAL_LEDToggle(LED6);
    A = (ADC_Val[0]*3000)/0xFFF;
    B = (ADC_Val[1]*3000)/0xFFF;
    C = (ADC_Val[2]*3000)/0xFFF;
 
    capture = TIM_GetCapture4(TIM3);
    TIM_SetCompare4(TIM3, capture + CCR4_Val);
  }
}

thanks for your help!

Regards,

Outcomes