Having delays shorter than 1 millisecond with FreeRTOS

Hello guys,
Is it possible to have a delay shorter than 1 millisecond with FreeRTOS?
Lets say in the microsecond order

yes, if your MCU supports that. However:

  • you must ensure that the time FreeRTOS spends in the timer isr is significantly less than your timer period
  • your timer may significantly affect the sytem performance.
1 Like

If your task needs to occasionally have a pause of a few microseconds (for I/O timing for instance) then doing that with a calibrated spin look, or reading a high count rate timer for it to change by the right amount works fine. At a few microseconds, the overhead of switching tasks is just not worth it, and the added delay of switching you back in means the delay is longer than requested or you need to adjust the delay time.

If you want to be DOING something every few microseconds, you want that action in an ISR trigger at the rate you want, and perhaps that ISR is above the level that interacts with FreeRTOS so critical sections don’t bother it. I have built control loops running on the order of 10s of microseconds with that method.

2 Likes

Thank you for your reply.
Yes, I guess it’s not easy to keep and ISR response that fast when talking about microseconds

Thank you for your reply.
Do you mean by calibrated spin loop something like:
void delay(uint32_t iterations) {
for (volatile uint32_t i = 0; i < iterations; i++) {
// Do nothing
}
}
or like a sequence of __NOP() when delays are just few microseconds?

I guess it depends on the frequency of the MCU, which is 240 MHz.
In my case, what I want to do is controlling the heating time of a thermal printer’s strobe.
The delay will be variable depending on the temperature read from the printer head, and it vary around 1 millisecond. It could go from about 800 microseconds to max 1.5 milliseconds.
So, guess I need to build a custom delay rather than using FreeRTOS built-in delay.
I am not sure if I could use calibrated spin loop in this situation and if it will allow me to get the exact delay i need for each reading temp case

Yes, I was talking about such a loop that you calibrate for actual speed.

For a variable delay of 800 us to 1500 us, I would use a dedicated timer peripheral to generate the adjustable pulse width. Ideally, you could have the timer directly generate the pulse signal, or if that isn’t possible, trigger the timer and in the ISR turn off the pulse.

That is NOT a suitable application for something like vTaskDelay.

I understand, thank you for your suggestion.
I think it might be kind of a variable PWM taken directly from the timer output. How to control this is another story.
In any case I will take the second suggestion.
Thank you again