STM32F080 ADC only converts once on Timer trigger

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
carldong
Posts: 62
Joined: Wed Oct 15, 2014 6:08 pm
Has thanked: 1 time
Been thanked: 1 time

STM32F080 ADC only converts once on Timer trigger

Post by carldong »

I am trying to get ADC work on hardware triggers since I am going to use it on some time critical applications. Now I am testing trigger using TIM3_TRGO, but the ADC only converts once. I cannot find where the problem is. The following are relavent code for ADC and TIM3 initialize:

Code: Select all

static ADCConversionGroup xADCConfig = {
  /* Circular */
  FALSE,                       
  /* 2,                            /\* Current Sense, Force Sense *\/ */
  /* Test, PA7/ADC_IN7 */
  1,                           
  /* Callback */
  cbADCDSP,                       
  /* Error Callback */
  cbADCError,                         
  /* CFGR1 */
  ADC_CFGR1_EXTEN_0             
  | ADC_CFGR1_EXTSEL_1
  | ADC_CFGR1_EXTSEL_0
  | ADC_CFGR1_RES_12BIT,
  /* TR */
  0,                           
  /* SMPR */
  ADC_SMPR_SMP_1P5,
  /* ADC_SMPR_SMP_239P5, */
  ADC_CHANNELS
};

adcsample_t* vADCConfig(void) {
  adcObjectInit(&ADCD1);
  adcStart(&ADCD1, NULL);

  return adc_samples;
}

void vADCStart(void) {
  /* Test */
  palSetGroupMode(GPIOA, PAL_PORT_BIT(7),
                  0, PAL_MODE_INPUT_ANALOG);
  adcStartConversion(&ADCD1, &xADCConfig, adc_samples, ADC_BUF_DEPTH);
}

void cbADCDSP(ADCDriver* adcp, adcsample_t *buffer, size_t n) {
  (void) adcp;
  (void) n;
  /* chprintf((BaseSequentialStream*)&SD1, "ADC: %d\r\n", *buffer); */
  chprintf(&SD1, "ADC\r\n");
}

static const GPTConfig xTIM3Config = {
  /* Frequency */
  50000,
  /* Callback */
  cbTIM3,
  /* Dier */
  TIM_DIER_TIE
  | TIM_DIER_UIE
};

void vTIM3Config(void) {
  gptObjectInit(&GPTD3);
  gptStart(&GPTD3, &xTIM3Config);
  /* TRGO Event */
  GPTD3.tim->CR2 &= ~TIM_CR2_MMS;
  GPTD3.tim->CR2 |= TIM_CR2_MMS_1;
}

void vTIM3Start(void) {
  gptStartContinuous(&GPTD3, 25000);
}



The idea of these code is to start the timer, then turn on the master mode trigger event from timer update, and configure ADC1 to use that event to initiate conversion.

In main(), I just call vTIM3Config(), vADCConfig(), vTIM3Start(), vADCStart() in sequence and do infinite loop. In serial monitor, I found that ADC interrupt only enters once. However, the LED that is toggled in timer interrupt is flashing, so probably there is nothing wrong about timers.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F080 ADC only converts once on Timer trigger

Post by Giovanni »

Probably you should enable the continuous mode in CFGR1, read the STM32F0 Reference Manual for details about: one shot, continuous and discontinuous modes.

Giovanni
carldong
Posts: 62
Joined: Wed Oct 15, 2014 6:08 pm
Has thanked: 1 time
Been thanked: 1 time

Re: STM32F080 ADC only converts once on Timer trigger

Post by carldong »

I think in manual it says single mode is triggered once each event, but continuous mode is something like free-running... I will test using bare-bone code. Also, can't I use the same timer for both PWM and GPT? I don't seem to find such configuration.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F080 ADC only converts once on Timer trigger

Post by Giovanni »

You can't use the same timer for both GPT and PWM but you can use the PWM driver to generate periodic interrupts like GPT does.

Giovanni
carldong
Posts: 62
Joined: Wed Oct 15, 2014 6:08 pm
Has thanked: 1 time
Been thanked: 1 time

Re: STM32F080 ADC only converts once on Timer trigger

Post by carldong »

Now I use my bare-metal code on timers since I will need PWM and ADC trigger on the same timer. I am trying to get ADC trigger work now. I will test using stand alone code first.
carldong
Posts: 62
Joined: Wed Oct 15, 2014 6:08 pm
Has thanked: 1 time
Been thanked: 1 time

Re: STM32F080 ADC only converts once on Timer trigger

Post by carldong »

As I try to write the initialize code, I found that the problem is that I didn't set 'circular' mode in DMA. When I set that, the ADC works as normal.

Code: Select all

static ADCConversionGroup xADCConfig = {
  /* Circular */
  TRUE,                       
  /* Test, PA7/ADC_IN7 */
  1,                           
  /* Callback */
  cbADCDSP,                       
  /* Error Callback */
  cbADCError,                         
  /* CFGR1 */
  ADC_CFGR1_EXTEN_0             
  | ADC_CFGR1_EXTSEL_1
  | ADC_CFGR1_EXTSEL_0
  | ADC_CFGR1_RES_12BIT,
  /* TR */
  0,                           
  /* SMPR */
  ADC_SMPR_SMP_1P5,
  /* ADC_SMPR_SMP_239P5, */
  ADC_CHANNELS
};

adcsample_t* vADCConfig(void) {
  adcObjectInit(&ADCD1);
  adcStart(&ADCD1, NULL);

  return adc_samples;
}

void vADCStart(void) {
  /* Test */
  palSetGroupMode(GPIOA, PAL_PORT_BIT(7),
                  0, PAL_MODE_INPUT_ANALOG);
  adcStartConversion(&ADCD1, &xADCConfig, adc_samples, ADC_BUF_DEPTH);
}
Post Reply