[riot-devel] vtimer_usleep_until or similar

Hauke Petersen hauke.petersen at fu-berlin.de
Tue Oct 21 17:13:39 CEST 2014

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)
     msg_t msg;

     while (1) {
         msg_receive(&msg);     /* just using the message as trigger here */

         // do something, what ever it is

And secondly create a timer thread that sends regular messages:

void *timer_thread(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!


On 21.10.2014 15:41, Daniel Amkær Sørensen wrote:
> 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.
> 2014-10-21 15:26 GMT+02:00 Martine Lenders <authmillenon at gmail.com 
> <mailto:authmillenon at gmail.com>>:
>     Hi,
>     I opened a PR for this: https://github.com/RIOT-OS/RIOT/pull/1849
>     Cheers,
>     Martine
>     2014-10-21 15:02 GMT+02:00 Daniel Amkær Sørensen
>     <daniel.amkaer at gmail.com <mailto:daniel.amkaer at gmail.com>>:
>         Hi,
>         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
>         _______________________________________________
>         devel mailing list
>         devel at riot-os.org <mailto:devel at riot-os.org>
>         http://lists.riot-os.org/mailman/listinfo/devel
>     _______________________________________________
>     devel mailing list
>     devel at riot-os.org <mailto:devel at riot-os.org>
>     http://lists.riot-os.org/mailman/listinfo/devel
> _______________________________________________
> devel mailing list
> devel at riot-os.org
> http://lists.riot-os.org/mailman/listinfo/devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/devel/attachments/20141021/4c61fadc/attachment.html>

More information about the devel mailing list