How to trigger the ADC with capture/compare event of TIM2?

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

Moderator: RoccoMarco

Post Reply
neums
Posts: 137
Joined: Fri May 08, 2015 1:09 pm
Has thanked: 10 times
Been thanked: 5 times

How to trigger the ADC with capture/compare event of TIM2?

Post by neums »

Hello,
i am working with the STM32F303CC. I want to trigger the ADC with the capture/compare event of the TIM2, but i don't know exactly how to initialize/configure the ADC/Timer

Code: Select all

const ADCConversionGroup adcgrpcfg1 = {
  TRUE,
  ADC_GRP1_NUM_CHANNELS,
  NULL,
  NULL,
  ADC_CFGR_EXTSEL_SRC(3) | ADC_CFGR_EXTEN_RISING,            // CFGR
  ADC_TR(0, 4095),          // TR1
  0,                        // CCR ADC_CCR_DELAY(4) | ADC_CCR_CKMODE_AHB_DIV2
  {                         // SMPR[2]
    ADC_SMPR1_SMP_AN7(ADC_SMPR_SMP_601P5),
    0
  },
  {                         // SQR[4]
  ADC_SQR1_SQ1_N(ADC_CHANNEL_IN7),
  0,
  0,
  0
  }
};

static const GPTConfig gpt2cfg1 = {
  frequency:    100000U,
  callback:     NULL,
};


i call it with

Code: Select all

adcStart(&ADCD1, NULL);
adcStartConversion(&ADCD1, &adcgrpcfg1, adc_buffer, ADC_GRP1_BUF_DEPTH);

GPTD2.tim->EGR = STM32_TIM_EGR_CC2G
GPTD2.tim->CCMR1= STM32_TIM_CCMR1_OC2CE | STM32_TIM_CCMR1_OC2M(6)
gptStart(&GPTD2, &gpt2cfg1);
gptStartContinuous(&GPTD2, time);


Is there a specific order for calling the Driver? Are the configurations correct?
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: How to trigger the ADC with capture/compare event of TIM

Post by Giovanni »

Hi,

I can't test it right now but few notes:

1) You cannot change the TIM registers before gptStart() because the timer clock is off.
2) The trigger source should be TIM2 TRGO (4), CC2 is not used by the GPT driver so it cannot trigger anything.

Version 3.0 has in the GPT configuration structure the settings for extra registers so there is no need to write registers manually, the DAC demo has an example of GPT with TRGO triggering, you may use the same code.

Giovanni
neums
Posts: 137
Joined: Fri May 08, 2015 1:09 pm
Has thanked: 10 times
Been thanked: 5 times

Re: How to trigger the ADC with capture/compare event of TIM

Post by neums »

Hi Giovanni,
thank you a lot for support. I used TRGO now and solved the problem:

Code: Select all

static const GPTConfig gpt2cfg1 = {
  frequency:    100000U,
  callback:     NULL,
  cr2:          TIM_CR2_MMS_1,  /* MMS = 010 = TRGO on Update Event.        */
  dier:         0U
};

static void adc1_isr(ADCDriver *adcp, adcsample_t *buffer, size_t n)
{
  dac_buffer[0] = adc_buffer[0];
  palTogglePad(GPIOE, GPIOE_LED1);
}

const ADCConversionGroup adcgrpcfg1 = {
  TRUE,
  ADC_GRP1_NUM_CHANNELS,
  adc1_isr,
  NULL,
  ADC_CFGR_EXTSEL_SRC(11) | ADC_CFGR_EXTEN_RISING | ADC_CFGR_DISCNUM_VAL(1) | ADC_CFGR_DISCEN_ENABLED,            // CFGR
  ADC_TR(0, 4095),          // TR1
  0,                        // CCR ADC_CCR_DELAY(4) | ADC_CCR_CKMODE_AHB_DIV2
  {                         // SMPR[2]
    ADC_SMPR1_SMP_AN7(ADC_SMPR_SMP_7P5),
    0
  },
  {                         // SQR[4]
  ADC_SQR1_SQ1_N(ADC_CHANNEL_IN7),
  0,
  0,
  0
  }
};


Start the timer with fpr getting a samling rate of 10kHz:

Code: Select all

gptStart(&GPTD2, &gpt2cfg1);
gptStartContinuous(&GPTD2, 10);


Start the ADC:

Code: Select all

adcStart(&ADCD1, NULL);
adcStartConversion(&ADCD1, &adcgrpcfg1, adc_buffer, ADC_GRP1_BUF_DEPTH);

with ADC_GRP1_BUF_DEPTH == 1 in my case. The programm is breakable with the debugger in adc1_isr. I measured the toggling LED in adc1_isr with an oscilloscope and verified the sampling rate.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: How to trigger the ADC with capture/compare event of TIM

Post by Giovanni »

Great :)

Giovanni
neums
Posts: 137
Joined: Fri May 08, 2015 1:09 pm
Has thanked: 10 times
Been thanked: 5 times

Re: How to trigger the ADC with capture/compare event of TIM

Post by neums »

Hi,
when i trigger the ADC with TRGO of timer 2 then one non expected trigger interrupt is generated when the timer gets started with gptStartContinuous(). I expected that the interrupt occurs when the timer have an overflow. Is there a possibility to eliminate this first interrupt? Or is it possible to enable the capture and compare trigger event of the timer2? I don't see this in the driver. Is there a purpose to implement the capture&compare function?
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: How to trigger the ADC with capture/compare event of TIM

Post by Giovanni »

It could be a problem in the GPT driver, maybe there is some status to reset. Putting this in queue, it is quite filled right now...

Giovanni
neums
Posts: 137
Joined: Fri May 08, 2015 1:09 pm
Has thanked: 10 times
Been thanked: 5 times

Re: How to trigger the ADC with capture/compare event of TIM

Post by neums »

Thanks,
and the Capture&Compare trigger functionality?
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: How to trigger the ADC with capture/compare event of TIM

Post by Giovanni »

That could be done using the PWM driver but I never tested it.

Giovanni
Post Reply