Timeout

Table of Contents

  1. Hello World!
  2. API
  3. Examples

The Timeout interface is used to setup an interrupt to call a function after a specified delay.

Any number of Timeout objects can be created, allowing multiple outstanding interrupts at the same time.

Hello World!

A simple program to setup a Timeout to invert an LED after a given timeout...

» Import this program

#include "mbed.h"
 
Timeout flipper;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
 
void flip() {
    led2 = !led2;
}
 
int main() {
    led2 = 1;
    flipper.attach(&flip, 2.0); // setup flipper to call flip after 2 seconds
 
    // spin in a main loop. flipper will interrupt it to call flip
    while(1) {
        led1 = !led1;
        wait(0.2);
    }
}

API

API summary

» Import latest build into a program

Public Member Functions

void  attach (void(*fptr)(void), float t)
  Attach a function to be called by the Ticker , specifiying the interval in seconds.
template<typename T >
void  attach (T *tptr, void(T::*mptr)(void), float t)
  Attach a member function to be called by the Ticker , specifiying the interval in seconds.
void  attach_us (void(*fptr)(void), unsigned int t)
  Attach a function to be called by the Ticker , specifiying the interval in micro-seconds.
template<typename T >
void  attach_us (T *tptr, void(T::*mptr)(void), unsigned int t)
  Attach a member function to be called by the Ticker , specifiying the interval in micro-seconds.
void  detach ()
  Detach the function.

Static Public Member Functions

static void  irq (uint32_t id)
  The handler registered with the underlying timer interrupt.

Warning

Note that timers are based on 32-bit int microsecond counters, so can only time up to a maximum of 2^31-1 microseconds i.e. 30 minutes. They are designed for times between microseconds and seconds. For longer times, you should consider the time()/Real time clock.

No blocking code in ISR

In ISR you should avoid any call to wait, infinitive while loop, or blocking calls in general.

No printf, malloc, or new in ISR

In ISR you should avoid any call to bulky library functions. In particular, certain library functions (like printf, malloc and new) are non re-entrant and their behaviour could be corrupted when called from an ISR.

RTOS Timer

Consider using the mbed RTOS Timer instead of a Timeout. In this way your callback function will not be executed in a ISR, giving you more freedom and safety in your code.

Examples

Attaching a member function

» Import this program

#include "mbed.h"

// A class for flip()-ing a DigitalOut 
class Flipper {
public:
    Flipper(PinName pin) : _pin(pin) {
        _pin = 0;
    }
    void flip() {
        _pin = !_pin;
    }
private:
    DigitalOut _pin;
};

DigitalOut led1(LED1);
Flipper f(LED2);
Timeout t;

int main() {
    t.attach(&f, &Flipper::flip, 2.0); // the address of the object, member function, and interval

    // spin in a main loop. flipper will interrupt it to call flip
    while(1) {
        led1 = !led1;
        wait(0.2);
    }
}



12 comments:

17 Oct 2010

That Demo code is not working on my mbed board. The Led 2 is not flashing every 2 seconds as it should do.

17 Oct 2010

my fault, confusion about timeout and ticker.

29 Nov 2010

Is there an example of using the overloaded method with the: template<typename T> void attach(T * tptr, void (T::*mptr)(void),float t). I am trying to call Timeout inside of a class method but can't figure out the syntax. Here is what I am trying to do: _timeout.attach_us(myClass, myClass::&attimeout, delay); Timeout _timeout is property of the class and attimeout is the method of the same class

29 Nov 2010

Hi Istvan,

Assuming you are trying to attach this in a method within a class, to the instance of itself, try:

_timeout.attach_us(this, &myClass::attimeout, delay);

The format is basically (address of object, address of function within object, delay);

30 Nov 2010

thanks, works perfect

13 Apr 2011

I'm posting this to warn anyone from making the same silly mistake I did. Be careful to check the scope on your Timeout object. If you declare your Timeout object within another function it's possible that the function will resolve and the timeout object will be deleted before your timeout completes.

For instance in this case your blink function will never be called.

if (1){    
        Timeout myTimeout;           //we should declare this object outside of the if statement.
        myTimeout.attach(&blink,2);  //The function blink would be called in two seconds but myTimeout is deleted before then

    }
23 Sep 2011

Hello;

  1. include "mbed.h"

Timeout flipper; DigitalOut led1(LED1); DigitalOut led2(LED2);

void flip() { led2 = !led2; }

int main() { led2 = 1; flipper.attach(&flip, 2.0); setup flipper to call flip after 2 seconds

spin in a main loop. flipper will interrupt it to call flip while(1) { led1 = !led1; wait(0.2); } }

I compiled this code but it didn't work for me. I want to make interruptions to execute functions.

THANKS.

23 Sep 2011

Hello; I had a confusion between Ticker and timeout. Now Every thing works fine for me.

:-)

26 Nov 2011

This page should note that the mbed compiler optimises global variables aggressively. So if you use a global variable to communicate between base code and Timeout interrupt it must be marked volatile:

volatile int running = 1;

led1 = DigitalOut(LED1);

void isr_code(void)
{
    running = 0;
}

int main(void)
{
   Timeout.attach(&isr_code, 10.0);
   led1 = 1
   while (running);
   led1 = 0;
   while (1) {
      wait(1);
      led1 = 1 - led1;
   }
}

in this example if running is not volatile the first loop will run forever!

07 Mar 2012

Hello,

a short question:

Is it Ok to call attach() a second/third... time before the timeout has fired or must I call detach() before and attach again?

Michael

29 Nov 2012

hi I want to design a controller to lower the speed of a motor if it exceeds a preset value. that means I have to know the know the current speed and compare with the set value. I tried to use an interruptin to count the number of pulses from a shaft encoder attached to the dc motor. but it wasn't working. can anyone help me with the cide that count the number of pulses in a second; hence determine the speed of the motor? .Thanks

29 Mar 2013

Hi You really have to fix this confusion between Ticker and Timeout !

this modified code blinks the led by calling the Interrupt Service Routine every 2 sec.

#include "mbed.h"
 
Ticker flipper;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
 
void flip() {
    led2 = !led2;
}
 
int main() {
    led2 = 1;
    flipper.attach(&flip, 2.0); // setup flipper to call flip after 2 seconds
 
    // spin in a main loop. flipper will interrupt it to call flip
    while(1) {
        led1 = !led1;
        wait(0.2);
    }
}

Posting comments for this page has been disabled