vtimer_usleep_until or similar


I have a feature proposal in the interest of performing a periodic task. The feature exists already in FreeRTOS, so there is plenty of inspiration.

The function would be similar to vtimer_usleep, except that you call it and it will calculate the right time interval to sleep.

More info here: http://www.freertos.org/vtaskdelayuntil.html

Best Regards Daniel Amkær Sørensen

Hi, I opened a PR for this: https://github.com/RIOT-OS/RIOT/pull/1849

Cheers, Martine

Thanks for the quick response.

I have some input. I’m looking for the functionality which the FreeRTOS function implements. FreeRTOS calculates the time to sleep based on the previous waking time, and updates the pointed variable accordingly.

“Pointer to a variable that holds the time at which the task was last unblocked. The variable must be initialised with the current time prior to its first use (see the example below). Following this the variable is automatically updated within vTaskDelayUntil().”

This ensures that one call to the function each loop makes the function run with a chosen frequency.


Hi again,

Okay, thanks. :slight_smile:

Is it better to use the hwtimer for forced periodicity, or is it not meant for that?

Hi Daniel,

doing something periodical is unfortunately so far RIOT is not super friendly with. This is due to the fact that RIOT is designed around a tickless-scheduling design, which means we don’t have means to find out in which point of time a thread started to run and we don’t have a ``discrete system time in ticks as in FreeRTOS. This means, that we can not (easily) just substract some offset ticks from a sleep value.

For the long-term we are planning to remodel our timer architecture, and it is planned to introduce something as timer_msg_periodic(..) to send periodic messages from the timer to a task.

For now I would suggest the following solution to your problem (although it introduces a little overhead):

Let your function that you need to run periodically run in it’s own thread, something like this:

void *periodic_function_thread(void *arg) { ��� (void)arg; ��� msg_t msg;

��� while (1) { ��� ��� msg_receive(&msg); ��� /* just using the message as trigger here */ ��� ��� ��� ��� // do something, what ever it is ��� ��� something_periodical(); ��� } }��� ���

And secondly create a timer thread that sends regular messages:

void *timer_thread(void *arg) { ��� (void)arg; ��� msg_t msg; ��� ��� msg.type = TIMEOUT;

��� while (1) { ��� ��� msg_send(&msg, periodic_task_pid, 0); ��� ��� vtimer_usleep(SLEEP_TIME_IN_US - OFFSET); ��� } �� }

It is very important, that the timer_thread has the higher priority. I don’t know about your specific applications and the requirements you have on the precision of the timing. To improve this, you can use the OFFSET to compensate for the scheduling delay of the timer thread - best measure this our for your concrete platform.

Hope this helps, let me know if there are any questions!

Cheers, Hauke

Hi Daniel,

Hi Kaspar,

I’ll check it out tomorrow. What happens if the interval is passed already when calling the sleep function?


Hi Hauke,

Thanks for your insight and support. I’ll check out your suggestion.