ADC triggered by timer

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

ADC triggered by timer

Post by nelfata » Wed Mar 18, 2015 4:39 am

Hi,
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.

User avatar
dhylands
Posts: 3227
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: ADC triggered by timer

Post by dhylands » Wed Mar 18, 2015 4:41 pm

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.

nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

Re: ADC triggered by timer

Post by nelfata » Wed Mar 18, 2015 5:11 pm

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?

User avatar
dhylands
Posts: 3227
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: ADC triggered by timer

Post by dhylands » Wed Mar 18, 2015 5:25 pm

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.

Damien
Site Admin
Posts: 616
Joined: Mon Dec 09, 2013 5:02 pm

Re: ADC triggered by timer

Post by Damien » Sat Mar 21, 2015 12:30 am

Only pyb.ADC() touches the configuration of the ADC peripheral.

nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

Re: ADC triggered by timer

Post by nelfata » Sat Mar 21, 2015 12:44 am

ok thanks guys, just needed to double check.

nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

Re: ADC triggered by timer

Post by nelfata » Thu May 07, 2015 2:29 am

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
__TIM8_CLK_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
HAL_TIM_Base_Init(&TIM8_Handle);
}

Post Reply