Mistake on this page?
Report an issue in GitHub or email us

RtosTimer

Deprecated: The EventQueue has superseded the RtosTimer. The RtosTimer and EventQueue duplicate the functionality of timing events outside of interrupt context; however, the EventQueue has additional features to handle deferring other events to multiple contexts.

Use the RtosTimer class to create and control timer functions in the system. A timer function is called when a time period expires, so both one-shot and periodic timers are possible. You can start, restart or stop a timer.

The thread osTimerThread handles timers. Callback functions run under the control of this thread and may use CMSIS-RTOS API calls.

RtosTimer class reference

Public Member Functions
 RtosTimer (void(*func)(void const *argument), os_timer_type type=osTimerPeriodic, void *argument=NULL)
 RtosTimer (mbed::Callback< void()> func, os_timer_type type=osTimerPeriodic)
template<typename T , typename M >
 RtosTimer (T *obj, M method, os_timer_type type=osTimerPeriodic)
osStatus stop (void)
osStatus start (uint32_t millisec)

RtosTimer example

Control the timing of four LEDs.

#include "mbed.h"
#include "rtos.h"

DigitalOut LEDs[4] = {
    DigitalOut(LED1), DigitalOut(LED2), DigitalOut(LED3), DigitalOut(LED4)
};

void blink(void const *n) {
    LEDs[(int)n] = !LEDs[(int)n];
}

int main(void) {
    RtosTimer led_1_timer(blink, osTimerPeriodic, (void *)0);
    RtosTimer led_2_timer(blink, osTimerPeriodic, (void *)1);
    RtosTimer led_3_timer(blink, osTimerPeriodic, (void *)2);
    RtosTimer led_4_timer(blink, osTimerPeriodic, (void *)3);
    
    led_1_timer.start(2000);
    led_2_timer.start(1000);
    led_3_timer.start(500);
    led_4_timer.start(250);
    
    Thread::wait(osWaitForever);
}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.