Embedded Freaks..

February 19, 2009

Programming Timeout under FreeRTOS

Filed under: programming-tips — Tags: — kunilkuda @ 11:23 am

It seems like the FreeRTOS has some support to program a timeout, but unpublished anywhere. I’ve checked the FreeRTOS source code, regarding the usage, and it seems like it’s quite safe to use it under user’s application.

Here’s what I’ve found:

xTimeOutType x_timeout; //< Var to hold the current time
portTickType openTimeout; //< Var to hold the timeout

// Set the initial time, where timeout will be started
// Set the timeout value
openTimeout = 512; //< In RTOS tick

// To check the timeout
if(xTaskCheckForTimeOut(&x_timeout, &openTimeout) == pdTRUE)
// It's timeout
} else {
// It's not timeout yet.

You can also combined the technique with do-while loop (see my previous post).


  1. As far as I can see, vTaskSetTimeOutState is not safe on a 16 bits systems if 32 bits ticks are used, unless interrupts are disabled first.

    Comment by Samuel Tardieu — April 5, 2009 @ 6:52 pm

  2. Thanks,

    I found the same thing and was wondering if this was documented somewhere.
    Seems this is the place 🙂

    I think this is perfectly safe, even on 16 bit systems. There are portENTERCRITICAL() and portEXITCRITICAL() calls around teh code accessing the data structures.

    Comment by Rob — May 5, 2011 @ 6:13 pm

  3. Aahhrrgg,

    just spend a few hours resolving a problem – If only I had read the complete code before using this …

    The thing is: I have a process in which I receive a lot of different messages but as soon as I have not received a certain message within 3 seconds I need to respond to one of the other tasks (or in common English: kick ‘m in the nuts).
    So I had the xTaskCheckForTimeOut() in the main loop of the task only to discover that this function also updates the start time I initialized with vTaskSetTimeOutState().

    So in the end nothing happened since the interval was reset on every check …
    You’ve been warned 🙂

    Comment by Rob — May 6, 2011 @ 9:10 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: