Page 1 of 1

ADC triggered by timer

Posted: Wed Mar 18, 2015 4:39 am
by nelfata
I created a module to collect ADC data using interrupts that is triggered by a timer (TIM8) with TRGO.
I added ADC_IRQHandler() in stm32f4xx_it.c which should call the corresponding ADC HAL handler.
For some reason as soon as I enable TIM8, the code hangs. I disabled the TIM8 handlers (thinking the MPY has some potential issues with that) but with no luck.
I am wondering if someone would know if the ADC interrupt/timer could be causing some contention, or perhaps some issue within the interrupt stack (no allocation being done, only calling the STM HAL ADC handler).

Thank you in advance.

Re: ADC triggered by timer

Posted: Wed Mar 18, 2015 4:41 pm
by dhylands
The usual reason for hangs is that the IRQ is firing and not being cleared.

If the IRQ doesn't get cleared then as soon as the IRQ handler is finished it gets immediately executed again (because the IRQ is still pending).

You can put prints in your IRQ handler to see if this is the case.

Re: ADC triggered by timer

Posted: Wed Mar 18, 2015 5:11 pm
by nelfata
Thank you for the reply.
It is working now, it must have been some initialization issue.
So using TIM8 should not be a problem as long as it does not get configured as a PWM.
Also if the pyb.ADC() is not called it should not be attempting to configure the ADC.
Am I mistaken?

Re: ADC triggered by timer

Posted: Wed Mar 18, 2015 5:25 pm
by dhylands
I wouldn't have expected anything to mess with ADC stuff without pyb.ADC() being called, but at this point wouldn't necessarily rule anything out.

Although it looks like the LCD code might be using an ADC.

In theory you should be able to use the same timer for multiple purposes, as long as all of the purposes are ok with the timer running at a given frequency. This is where the difference between a Timer and a TimerChannel becomes significant.

Re: ADC triggered by timer

Posted: Sat Mar 21, 2015 12:30 am
by Damien
Only pyb.ADC() touches the configuration of the ADC peripheral.

Re: ADC triggered by timer

Posted: Sat Mar 21, 2015 12:44 am
by nelfata
ok thanks guys, just needed to double check.

Re: ADC triggered by timer

Posted: Thu May 07, 2015 2:29 am
by nelfata
Hi again,
to follow up on the ADC timer trigger. I am trying to oversample the input analog signal and after trying to increase the TIM8 frequency, I am hitting a limit of 66.66KHz (measured on a scope).
I am wondering if there is something badly configured, as I intend to trigger the ADC to a rate between 100 to 500KHz.

If someone could take a quick look perhaps he might be able to spot a problem. Thank you.

Here is the configuration code:

void timer_tim8_init(uint freq) {
// TIM clock enable

// Timer runs at SystemCoreClock / 2
// Compute the prescaler value so TIM triggers at freq-Hz
uint32_t period = MAX(1, timer_get_source_freq(8) / freq);
uint32_t prescaler = 1;
while (period > 0xffff) {
period >>= 1;
prescaler <<= 1;

// Time base clock configuration
TIM8_Handle.Instance = TIM8;
TIM8_Handle.Init.Period = period - 1;
TIM8_Handle.Init.Prescaler = prescaler - 1;
TIM8_Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; // unused for TIM8
TIM8_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; // unused for TIM8