Table of Contents

  1. Hello World!
  2. API
  3. Examples

Mbed OS 2 and Mbed OS 5

This is the handbook for Mbed OS 2. If you’re working with Mbed OS 5, please see the new handbook. For the latest TimeOut API, please see TimeOut.

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 program

00001 #include "mbed.h"
00003 Timeout flipper;
00004 DigitalOut led1(LED1);
00005 DigitalOut led2(LED2);
00007 void flip() {
00008     led2 = !led2;
00009 }
00011 int main() {
00012     led2 = 1;
00013     flipper.attach(&flip, 2.0); // setup flipper to call flip after 2 seconds
00015     // spin in a main loop. flipper will interrupt it to call flip
00016     while(1) {
00017         led1 = !led1;
00018         wait(0.2);
00019     }
00020 }


API summary

Import library

Public Member Functions

void  attach (Callback< void()> func, float t)
  Attach a function to be called by the Ticker , specifying the interval in seconds.
template<typename T , typename M >
  MBED_DEPRECATED_SINCE ("mbed-os-5.1","The attach function does not support cv-qualifiers. Replaced by ""attach(callback(obj, method), t).") void attach(T *obj
  Attach a member function to be called by the Ticker , specifying the interval in seconds.

Static Public Member Functions

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

Protected Member Functions

void  insert ( timestamp_t timestamp)
  Set relative timestamp of the internal event.
void  insert_absolute ( us_timestamp_t timestamp)
  Set absolute timestamp of the internal event.
void  remove ()
  Remove timestamp.

Protected Attributes

us_timestamp_t   _delay
  Time delay (in microseconds) for re-setting the multi-shot callback.
Callback< void()>  _function
bool  _lock_deepsleep
  Flag which indicates if deep-sleep should be disabled.

Private Member Functions

  MBED_DEPRECATED ("Invalid copy construction of a NonCopyable resource.") NonCopyable(const NonCopyable &)
  NonCopyable copy constructor.
  MBED_DEPRECATED ("Invalid copy assignment of a NonCopyable resource.") NonCopyable &operator
  NonCopyable copy assignment operator.


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.


Attaching a member function

Import program

00001 #include "mbed.h"
00003 // A class for flip()-ing a DigitalOut 
00004 class Flipper {
00005 public:
00006     Flipper(PinName pin) : _pin(pin) {
00007         _pin = 0;
00008     }
00009     void flip() {
00010         _pin = !_pin;
00011     }
00012 private:
00013     DigitalOut _pin;
00014 };
00016 DigitalOut led1(LED1);
00017 Flipper f(LED2);
00018 Timeout t;
00020 int main() {
00021     t.attach(&f, &Flipper::flip, 2.0); // the address of the object, member function, and interval
00023     // spin in a main loop. flipper will interrupt it to call flip
00024     while(1) {
00025         led1 = !led1;
00026         wait(0.2);
00027     }
00028 }