Behaviour in case of timer race condition?

Hi all,

What will happen in this case:
I have two timers that expire exactly at the same tick.

  • Is there a priority at which the two callback functions will run?
  • in case the first running callback function stops the other timer, will its callback function still run or not?

Thanks for some clarification


  • No, there is no priority in the order of the callbacks.
  • Normally, the other callback won’t run. However, that’s very implementation dependent and as best I can tell undocumented. And there is a rare case where the other callback would still run. So it’s best not to depend on any specific behavior in this case.

Hi Jeff,

thanks for the clear statement.

I am doing an ultra slow PWM with 0.1 Hz frequency given by a reload timer. When this period timer fires it starts the output (which can take some time) and starts a second timer for the on duration. When this on duration timer fires it stops the output. Now, if the on duration is calculated to be 100% the on duration is equal to the PWM period but due to the delay the on duration timer now fires shortly after the next timer event of the period timer and so turns off the output shortly after the start of the period leading to only very small duty cycle instead of 100%.

So my idea was to turn off on duration timer in period timers callback but this left the case when both fire at the same tick.

Do you have an idea how to get more safe in this case?

Thanks for any help



I think the best design would be driven by which part of the PWM really needs to be accurate. The on time? The off time? The frequency? You can’t have all three when driving with software.

There are inherent race conditions for troublesome PWM duty cycles near 0% and 100%. I would think a natively serialized approach would be cleanest. For example, a PWM task that uses vTaskDelay() and vTaskDelayUntil() judiciously.

I would detect the 100% case and just not arm the off callback.

Hi all,

thanks for your input and help. Here my answers and comments:

As the PWM is embedded into a control loop none of the four, period, high time, low time or duty cycle must be extremly accurate, thanks for the hint to that.

I did it so now that I have one FreeRTOS timer that times first high time, then low time and I have special cases for 0% and 100% where it times the period.

This works fine for me and the control loop does the job nice.

Thanks again