AnsweredAssumed Answered

STM32F373c SDADC Init problem

Question asked by bauer.philipp on Dec 17, 2014
Hi guys,
I'm trying to get a second SDADC (SDADC3) to work, in addition to SDADC1.
I took the Init for SDADC1 out of the std-lib examples and it works fine. For the second SDADC i want to have the same attitudes as SDADC1.
I try to copy the Code as well, but it doesn't work. I try to trigger the both SDADC's with timer 19, but only the SDADC1 EOC interrupt occurs.

Here is my Code i programm so far.
#include "stm32f37x.h"
#include "stm32f37x_conf.h"
 
 
 
 
volatile uint16_t TimingDelay;
void Delay(__IO uint32_t nTime);
 
 
void TimingDelay_Decrement(void);
 
void Init_Pwr(void);
void Init_Clc(void);
void Init_Gpio(void);
void Init_Timer19(void);
 
static uint32_t SDADC1_Config(void);
static uint32_t SDADC3_Config(void);
 
 
 
 
/* Var Def's*/
uint8_t dummy1;
uint8_t dummy2;
int16_t InjectedConvData1;
int16_t InjectedConvData3;
float a=324.1234;
float b=121.146;
float c= 0.12345;
float erg;
 
 
 
/*Defines*/
#define SDADC_INIT_TIMEOUT        30                /* about two SDADC clock cycles after INIT is set */
#define SDADC_CAL_TIMEOUT         4*30720   /*  ~5.12 ms at 6 MHz  in a single calibration sequence */
 
 
 
 
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
     
SystemInit();  
     
Init_Pwr();         /* Power's the ADC */
Init_Clc();     /* Init Core Clocks */
Init_Gpio();        /* Init IO Pins */
Init_Timer19(); /* Init Timer 19 for Triggering SDADC*/
     
  if(SDADC1_Config() != 0)
    {
    dummy1=10;
    }
    else
    {
    dummy1=1;
    }
     
      if(SDADC3_Config() != 0)
    {
    dummy2=10;
    }
    else
    {
    dummy2=1;
    }
     
    //SDADC_SoftwareStartInjectedConv(SDADC3);
  while (1)
  {
 
  }
}
 
 
/**
  * @brief  Powers The SDADC Analog Parts
  * @param  None
  * @retval None
  */
void Init_Pwr(void)
{
     /* PWR APB1 interface clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
     
  /* Enable_SDADC analog interface */
  PWR_SDADCAnalogCmd(PWR_SDADCAnalog_1, ENABLE);
     
    /* Enable_SDADC analog interface */
  PWR_SDADCAnalogCmd(PWR_SDADCAnalog_3, ENABLE);
}
 
/**
  * @brief  Clock Initialisation
  * @param  None
  * @retval None
  */
void Init_Clc(void)
    RCC_ClocksTypeDef RCC_Clocks;
   
    /* SysTick end of count event each 1ms */
     
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
     
    /* Set the SDADC divider: The SDADC should run @6MHz */
  /* If Sysclk is 72MHz, SDADC divider should be 12 */
  RCC_SDADCCLKConfig(RCC_SDADCCLK_SYSCLK_Div12);
}
 
/**
  * @brief  IO_Initialisation
  * @param  None
  * @retval None
  */
void Init_Gpio(void)
{
    GPIO_InitTypeDef        GPIO_InitStructure;
     
 
 /* GPIOC Periph clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
 /* GPIOE Periph clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);
     /* GPIOE Periph clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);
     
  /* Configure PC0 and PC1 in output pushpull mode */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
     
        /* Reset initial */
    GPIO_SetBits(GPIOC,GPIO_Pin_0 | GPIO_Pin_1);
     
    /* Pins for SDADC1*/
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
     
    /* Pins for SDADC3*/
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_Init(GPIOD, &GPIO_InitStructure);   
 
}
 
 
 
/**
  * @brief  Timer Initialisation, to trigger SDADC1/SDADC3.
  * @param  None
  * @retval None
  */
void Init_Timer19(void)
{
  TIM_OCInitTypeDef TIM_OCInitStructure;
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
 
  /* Enable TIM19 clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM19, ENABLE);
 
  /* TIM19 Configuration */
  TIM_DeInit(TIM19);
 
  /* Fills each TIM_TimeBaseInitStruct member with its default value */
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
 
  /* 8 KHz **/
  TIM_TimeBaseStructure.TIM_Period = 1000;
  TIM_TimeBaseStructure.TIM_Prescaler = 8;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM19, &TIM_TimeBaseStructure);
 
  /* PWM1 Mode configuration: Channel2 (OC2) */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 1000/2;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  TIM_OC2Init(TIM19, &TIM_OCInitStructure);
 
 
      /* PWM1 Mode configuration: Channel3 (OC3) */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 1000/4;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  TIM_OC3Init(TIM19, &TIM_OCInitStructure);
  /* Enable TIM19 counter */
  TIM_Cmd(TIM19, ENABLE);
}
 
 
static uint32_t SDADC1_Config(void)
{
  SDADC_AINStructTypeDef SDADC_AINStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  uint32_t SDADCTimeout = 0;
   
  /* MPX2102_SDADC APB2 interface clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SDADC1, ENABLE);
 
 
 
  /* Select External reference: The reference voltage selection is available
     only in SDADC1 and therefore to select the VREF for SDADC2/SDADC3, SDADC1
     clock must be already enabled */
  SDADC_VREFSelect(SDADC_VREF_Ext);
  /* Insert delay equal to ~5 ms */
  Delay(5);
   
  /* Enable MPX2102_SDADC */
  SDADC_Cmd(SDADC1, ENABLE);
 
  /* Enter initialization mode */
  SDADC_InitModeCmd(SDADC1, ENABLE);
  SDADCTimeout = SDADC_INIT_TIMEOUT;
  /* wait for INITRDY flag to be set */
  while((SDADC_GetFlagStatus(SDADC1, SDADC_FLAG_INITRDY) == RESET) && (--SDADCTimeout != 0));
 
  if(SDADCTimeout == 0)
  {
    /* INITRDY flag is not set */
    return 1;
  }
 
  /* Analog Input configuration conf0: use differential mode and gain = 4 */
  SDADC_AINStructure.SDADC_InputMode = SDADC_InputMode_Diff;
  SDADC_AINStructure.SDADC_Gain = 1;
  SDADC_AINStructure.SDADC_CommonMode = SDADC_CommonMode_VSSA;
  SDADC_AINStructure.SDADC_Offset = 0;
  SDADC_AINInit(SDADC1, SDADC_Conf_0, &SDADC_AINStructure);
 
  /* select MPX2102_SDADC channel 8 to use conf0 */
  SDADC_ChannelConfig(SDADC1, SDADC_Channel_8, SDADC_Conf_0);
 
  /* select channel 8 */
  SDADC_InjectedChannelSelect(SDADC1,SDADC_Channel_8);
  /* Select an external trigger */
  SDADC_ExternalTrigInjectedConvConfig(SDADC1, SDADC_ExternalTrigInjecConv_T19_CC2);
  /* Select rising edge */
  SDADC_ExternalTrigInjectedConvEdgeConfig(SDADC1, SDADC_ExternalTrigInjecConvEdge_Rising);
 
  /* Exit initialization mode */
  SDADC_InitModeCmd(SDADC1, DISABLE);
 
  /* Enable end of injected conversion interrupt */
  SDADC_ITConfig(SDADC1, SDADC_IT_JEOC, ENABLE);
 
  /* NVIC Configuration */
  NVIC_InitStructure.NVIC_IRQChannel = SDADC1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
 
  /* configure calibration to be performed on conf0 */
  SDADC_CalibrationSequenceConfig(SDADC1, SDADC_CalibrationSequence_1);
  /* start MPX2102_SDADC Calibration */
  SDADC_StartCalibration(SDADC1);
  /* Set calibration timeout: 5.12 ms at 6 MHz in a single calibration sequence */
  SDADCTimeout = SDADC_CAL_TIMEOUT;
  /* wait for MPX2102_SDADC Calibration process to end */
  while((SDADC_GetFlagStatus(SDADC1, SDADC_FLAG_EOCAL) == RESET) && (--SDADCTimeout != 0));
 
  if(SDADCTimeout == 0)
  {
    /* EOCAL flag can not set */
    return 2;
  }
 
  return 0;
}
 
 
 
 
 
static uint32_t SDADC3_Config(void)
{
  SDADC_AINStructTypeDef SDADC_AINStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  uint32_t SDADCTimeout = 0;
   
      
     
  /* SDADC3 APB2 interface clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SDADC3, ENABLE);
 
  Delay(5);
   
  /* Enable SDADC3 */
  SDADC_Cmd(SDADC3, ENABLE);
 
  /* Enter initialization mode */
  SDADC_InitModeCmd(SDADC3, ENABLE);
  SDADCTimeout = SDADC_INIT_TIMEOUT;
  /* wait for INITRDY flag to be set */
  while((SDADC_GetFlagStatus(SDADC3, SDADC_FLAG_INITRDY) == RESET) && (--SDADCTimeout != 0));
 
  if(SDADCTimeout == 0)
  {
    /* INITRDY flag is not set */
    return 1;
  }
 
  /* Analog Input configuration conf0: use differential mode and gain = 4 */
  SDADC_AINStructure.SDADC_InputMode = SDADC_InputMode_Diff;
  SDADC_AINStructure.SDADC_Gain = 1;
  SDADC_AINStructure.SDADC_CommonMode = SDADC_CommonMode_VSSA;
  SDADC_AINStructure.SDADC_Offset = 0;
  SDADC_AINInit(SDADC3, SDADC_Conf_0, &SDADC_AINStructure);
 
  /* select MPX2102_SDADC channel 6 to use conf0 */
  SDADC_ChannelConfig(SDADC3, SDADC_Channel_6, SDADC_Conf_0);
 
  /* select channel 6 PD8/PD9 */
  SDADC_InjectedChannelSelect(SDADC3,SDADC_Channel_6);
     
     
  /* Select an external trigger */
  SDADC_ExternalTrigInjectedConvConfig(SDADC3, SDADC_ExternalTrigInjecConv_T19_CC3);
  /* Select rising edge */
  SDADC_ExternalTrigInjectedConvEdgeConfig(SDADC3, SDADC_ExternalTrigInjecConvEdge_Rising);
 
     
  /* Exit initialization mode */
  SDADC_InitModeCmd(SDADC3, DISABLE);
 
  /* Enable end of injected conversion interrupt */
  SDADC_ITConfig(SDADC3, SDADC_IT_JEOC, ENABLE);
 
  /* NVIC Configuration */
  NVIC_InitStructure.NVIC_IRQChannel = SDADC3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 12;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 12;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
 
  /* configure calibration to be performed on conf0 */
  SDADC_CalibrationSequenceConfig(SDADC3, SDADC_CalibrationSequence_1);
  /* start MPX2102_SDADC Calibration */
  SDADC_StartCalibration(SDADC3);
  /* Set calibration timeout: 5.12 ms at 6 MHz in a single calibration sequence */
  SDADCTimeout = SDADC_CAL_TIMEOUT;
  /* wait for MPX2102_SDADC Calibration process to end */
  while((SDADC_GetFlagStatus(SDADC3, SDADC_FLAG_EOCAL) == RESET) && (--SDADCTimeout != 0));
 
  if(SDADCTimeout == 0)
  {
    /* EOCAL flag can not set */
    return 2;
  }
 
  return 0;
}
 
 
 
 
 
/**
  * @brief  Delay Function nTime of 1 equals 1 ms!!
  * @param  None
  * @retval None
  */
void Delay(__IO uint32_t nTime)
{
  TimingDelay = nTime;
 
  while(TimingDelay != 0);
}
 
 
/**
  * @brief  Decrements the TimingDelay variable.
  * @param  None
  * @retval None
  */
void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
  {
    TimingDelay--;
  }
}
 
 
 
 
 
 
 
/* Interrupt Section */
 
 
 
/**
  * @brief  This function handles SDADC1 interrupt request.
  * @param  None
  * @retval : None
  */
void SDADC1_IRQHandler(void)
{
  uint32_t ChannelIndex;
    GPIO_SetBits(GPIOC,GPIO_Pin_0);
  if(SDADC_GetFlagStatus(SDADC1, SDADC_FLAG_JEOC) != RESET)
  {
    /* Get the converted value */
    InjectedConvData1 = SDADC_GetInjectedConversionValue(SDADC1, &ChannelIndex);
         
 
  }
    GPIO_ResetBits(GPIOC,GPIO_Pin_0);
}
 
 
/**
  * @brief  This function handles SDADC3 interrupt request.
  * @param  None
  * @retval:None
  */
void SDADC3_IRQHandler(void)
{
  uint32_t ChannelIndex3;
    GPIO_SetBits(GPIOC,GPIO_Pin_1);
     
  if(SDADC_GetFlagStatus(SDADC3, SDADC_FLAG_JEOC) != RESET)
  {
    /* Get the converted value */
    InjectedConvData3 = SDADC_GetInjectedConversionValue(SDADC3, &ChannelIndex3);
         
 
  }
    GPIO_ResetBits(GPIOC,GPIO_Pin_1);
}
 
 
 
/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
TimingDelay_Decrement();
}
 

Outcomes