Chibios/RT and Arduino Timers

In the second part of this series, I discussed how to perform multiple tasks without them interfering with each other. In other words, multitasking. In the case of Arudino C/C++ it was done by setting up a simple scheduler. For Chibios/RT it was done using threads.

In both cases the tasks were simple; flashing an LED and printing to the serial port. However, for tasks like these that are done on a periodic basis a better way to execute them is to user timers.

Timers Under Arduino C/C++

For the Arduino using C/C++ there are a number of timer classes available. Typically they use the millis() function just like the scheduler discussed last time, but offer the advantage of encapsulating the code keeping track of how much time has passed inside the timer class and allowing for multiple tasks with differing periods.

The listing below demonstrates this using a timer class written by Simon Monk. Once again there are two simple tasks, flashing a LED and printing to the serial port. There’s no blinkLed() method though since the timer class has a method included to do just that.

    #include <Streaming.h>
    #include <Timer.h>
    #include <Event.h>

    #define  LED_TIMEOUT 1250
    #define  SERIAL_TIMEOUT 500

    // Uses Simon Monk's Arduino Timer library
    // https://github.com/JChristensen/Timer
    //
    Timer _timer;

    // Serial print method.
    //
    void printSerial()
    {
        static unsigned long _count = 0;

        // Update the counter and print the message.
        //
        _count += 1;
        Serial << "Hello world! " << _count << endl;
    }

    // Main routines.
    //
    void setup() 
    {
        // Open serial communications and wait for port to open:
        //
        Serial.begin(9600);
        while (!Serial);

        // Initialize the output pin.
        //
        pinMode(LED_BUILTIN, OUTPUT);

        // Initialize the timer.
        //
        _timer.oscillate(LED_BUILTIN, LED_TIMEOUT, LOW);
        _timer.every(SERIAL_TIMEOUT, printSerial);
    }

    void loop() 
    {
        _timer.update();
    }

Since the timer class is based upon the millis() it’s necessary to periodically call its update() function. In this respect it’s similar to the simple scheduler described last time but is easier to configure and understand.

Timers Under Chibios/RT

Chibios/RT also has its own timer class whose use is illustrated below. However, it’s a bit more complicated to set up and use.

    #include <ChRt.h>
    #include <Streaming.h>

    #define  LED_TIMEOUT 1250
    #define  SERIAL_TIMEOUT 500

    static virtual_timer_t led_vt;
    static virtual_timer_t serial_vt;

    // LED blink method.
    //
    static void blinkLed(void *arg) 
    { 
        static bool _lightLed = false;

        // Reset the timer.
        //
        chSysLockFromISR();
        chVTSetI(&led_vt, MS2ST(LED_TIMEOUT), blinkLed, NULL);
        chSysUnlockFromISR();

        // Toggle the LED.
        //
        digitalWrite(LED_BUILTIN, _lightLed ? HIGH : LOW);
        _lightLed = !_lightLed;

    }

    // Serial print method.
    //
    void printSerial()
    {
        static unsigned long _count = 0;
        
        // Reset the timer.
        //
        chSysLockFromISR();
        chVTSetI(&serial_vt, MS2ST(SERIAL_TIMEOUT), printSerial, NULL);
        chSysUnlockFromISR();

        // Update the counter and print the message.
        //
        _count += 1;
        Serial << "Hello chibios timers! " << _count << endl;
    }

    // Main routines.
    //
    void setup () 
    {
        chBegin(mainThread);
    }
    
    void mainThread () 
    {
        // Open serial communications and wait for port to open.
        //
        Serial.begin(9600);
        while (!Serial) ;

        // Initialize the timers.
        //
        chVTObjectInit(&led_vt);
        chVTObjectInit(&serial_vt);

        // Start the timers.
        //
        chVTSet(&led_vt, MS2ST(LED_TIMEOUT), blinkLed, NULL);
        chVTSet(&serial_vt, MS2ST(SERIAL_TIMEOUT), printSerial, NULL);
    }

    void loop() 
    {
    }

In this case, two timers are declared and configured in the mainThread() routine. When a timer goes off the associated method is called and the corresponding action is performed.

There are a few differences between the Arduino C/C++ and Chibios timer classes. First, the Chibios/RT timer is a one-shot. If it’s desired that a timer should go off periodically, it has to be reset within the timer function. Second, a Chibios/RT timer is a hardware timer whose associated method functions as an interrupt service routine so you don’t want it to perform any extended operations.

So how to they compare? The Arduino C/C++ timer class is definitely easier to set up. It also uses less memory though not as much less as you might think. The Arduino C/C++ listing above uses 2488 bytes of program space and 376 bytes of RAM. The equivalent Chibios/RT program uses 3776 bytes of program space and 444 bytes of RAM. So it may come down to which you’re most comfortable with and what other features you’re using along with the timers.

Advertisements

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

DuWayne's Place

Computers, Electronics, and Amateur Radio from KC3XM

QRP HomeBuilder - QRPHB -

Computers, Electronics, and Amateur Radio from KC3XM

Open Emitter

Computers, Electronics, and Amateur Radio from KC3XM

Ripples in the Ether

Emanations from Amateur Radio Station NT7S

m0xpd's 'Shack Nasties'

Computers, Electronics, and Amateur Radio from KC3XM

%d bloggers like this: