Chibios/RT and the Arudino

One of the things I’ve been playing with lately is the Chibios Real Time operating system (RTOS). I’ve had the opportunity to work with it on a project for work. Then I found out there’s a port for the Arduino, so that became a natural learning tool.

But why? Why use an RTOS, especially for something as simple as an Arduino? What can I do with an RTOS that I can’t do with straight code? That’s a legitimate question because if I’m already comfortable with coding C/C++ on an Arduino, why would I complicate my life?

So over the next few posts I’m going to examine that question by taking some things commonly done on the Arduino and trying to do the same thing in Chibios. This is not intended to be a Chibios tutorial though I hope to learn something in the process. And since I am by no means an RTOS expert I welcome any suggestions for improvement.

Setting Things Up

For these posts, I’ll be using an Arudino Nano, the Arduino V1.8.3 IDE, and the Chibios/RT (ChRt) library.

To install the ChRt library, download the .ZIP file from the ChRt repository. From within the Arduino IDE, select Sketch -> Include Library -> Add .ZIP Library. Select the .ZIP file you just downloaded and click OK. Then restart the IDE to make the library available.

Add Chibios/RT Library

Hello Blink

Blink is the “Hello World” of embedded programming so I see no reason not to start there. A typical Arduino version is shown below:

    void setup() 
    {
        pinMode(LED_BUILTIN, OUTPUT);
    }

    void loop() 
    {
        digitalWrite(LED_BUILTIN, HIGH);
        delay(500);
        digitalWrite(LED_BUILTIN, LOW);
        delay(500);
    }

A direct equivalent using the ChRt library is:

    #include <ChRt.h>

    void setup () 
    {
        chBegin(mainThread);
    }
    
    void mainThread () 
    {
        pinMode(LED_BUILTIN, OUTPUT);
    }

    void loop() 
    {
        digitalWrite(LED_BUILTIN, HIGH);
        chThdSleepMilliseconds(500);
        digitalWrite(LED_BUILTIN, LOW);
        chThdSleepMilliseconds(500);
    }

Because of their simplicity I don’t believe one has an advantage over the other. The major difference is that the Arduino delay() function is a blocking call while the Chibios chThdSleepMissiseconds() call is not. Doesn’t make a difference here but it will come into play next time when I start taking a look at threads.

Advertisements

1 Comment

    Trackbacks

    1. Chibios/RT and Arduino Multitasking | Computer/Electronics Workbench

    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: