Difference between sdk_os_delay_us and vTaskDelay

1,270 views
Skip to first unread message

Attila Magyar

unread,
Aug 18, 2016, 8:54:39 AM8/18/16
to esp-open-rtos mailing list
Hi,

I'm not sure about the difference between the sdk_os_delay_us() and vTaskDelay() functions. Let's say I want microsecond precision delay, should I do something like this

vTaskDelay(500000/portTICK_RATE_US) 
or
sdk_os_delay_us(500000). 

Could you tell me if there any substantial difference between these two?

thanks
Attila

Angus Gratton

unread,
Aug 19, 2016, 11:55:57 AM8/19/16
to Attila Magyar, esp-open-rtos mailing list
Hi Attila,

For microsecond precision delay, you'll want sdk_os_delay_us().

* vTaskDelay suspends the current task in the FreeRTOS scheduler, for N ticks (this is why you divide by portTICK_RATE_MS, etc when passing the argument.) The default scheduler tick is 10ms, although you can configure this tick time you generally don't want it to be too low (due to the overhead of context switching each time a tick happens.)

* sdk_os_delay_us() "busy-waits" for the specified amount of time. The processor sits in a loop until that amount of time has gone past.

vTaskDelay() is better for long or imprecise delays, because it lets another task wake up and run while the first task is suspended.

sdk_os_delay_us() is better for very precise short delays, you can also surround such a call with vTaskEnterCritical / vTaskExitCritical to disable interrupts. This will guarantee very precise timing except when the NMI triggers (which you can't prevent, unfortunately). Just bear in mind that while delaying in this fashion you are preventing other tasks or interrupt handlers in the system from running.


The final options for predictable delays are timers. You could use one of the timer peripherals and its interrupt (have a look in core/include/esp/timers.h). This gives you a predictable event that will interrupt normal task execution at a finite time.

There is also a way to hook a timer event handler into the NMI handler. I don't actually remember the details of this, it's pretty hacky and AFAIK not documented as part of esp-open-rtos... :/


Angus


On Thu, Aug 18, 2016 at 05:54:38AM -0700, Attila Magyar wrote:
> Hi,
>
> I'm not sure about the difference between the sdk_os_delay_us()
> and vTaskDelay() functions. Let's say I want microsecond precision delay,
> should I do something like this
>
> *vTaskDelay(500000/portTICK_RATE_US) *
> *or*
> *sdk_os_delay_us*(*500000). *
>
> Could you tell me if there any *s*ubstantial difference between these two?
>
> thanks
> Attila
>
> --
> You received this message because you are subscribed to the Google Groups "esp-open-rtos mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to esp-open-rto...@googlegroups.com.
> To post to this group, send email to esp-op...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/esp-open-rtos/2bf83c1a-8a6d-4bf2-bc96-af4bcc3d6960%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Attila Magyar

unread,
Aug 23, 2016, 3:38:26 PM8/23/16
to esp-open-rtos mailing list
Thanks for your detailed answer Angus, it was very helpful.
Reply all
Reply to author
Forward
0 new messages