cancel
Showing results for 
Search instead for 
Did you mean: 

Delayed in PWM and interrupt

Gabriel_b
Associate

Hi
I'm implemented the most simple pwm generation and using interruptions in F401RE with TIM1. But a delay has been in the PWM generation and the update event interrupt (underflow and overflow) the most large pulse is the PWM and the another the when interrupt occours. I tested in diferent frequencys and in another timer and the error persists.
Above has my code, i want centralize the interruption in the middle of pwm pulse.

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions

The interrupt is triggered in the middle of the pulse, but it takes time

- until it propagates to NVIC

- latency of interrupt is minimum 12 cycles in Cortex-M4

- Interrupt Service Routine (ISR) in C adds prologue, that's another source of latency

- Cube/HAL adds lots of code until the callback is called, that's another huge source of latency

- the latter two gets worse if compiler optimizations are off or at low level

All in all, if the pulse is 2*30 cycles (or 2*60, depending on the APB prescaler of the APB bus where the timer in question sits), what I see on your second picture is latency of roughly 100-200 cycles, given the last item (Cube/HAL), that sounds about right.

And, of course, this all is subject to jitter, if there are other interrupts of the same or higher priority active.

If you want any precision timing, do everything in hardware, i.e. employ timers' features to generate precisely timed pulses.

JW

View solution in original post

2 REPLIES 2

The interrupt is triggered in the middle of the pulse, but it takes time

- until it propagates to NVIC

- latency of interrupt is minimum 12 cycles in Cortex-M4

- Interrupt Service Routine (ISR) in C adds prologue, that's another source of latency

- Cube/HAL adds lots of code until the callback is called, that's another huge source of latency

- the latter two gets worse if compiler optimizations are off or at low level

All in all, if the pulse is 2*30 cycles (or 2*60, depending on the APB prescaler of the APB bus where the timer in question sits), what I see on your second picture is latency of roughly 100-200 cycles, given the last item (Cube/HAL), that sounds about right.

And, of course, this all is subject to jitter, if there are other interrupts of the same or higher priority active.

If you want any precision timing, do everything in hardware, i.e. employ timers' features to generate precisely timed pulses.

JW

Gabriel_b
Associate

Thank's you for the reply, Waclawec.jan.

A easy form to minimize this problem is delete the void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) and write the interrupt code in stm32f4xx_it.c, thus having delay ~700ns. Above my code.