Channels ▼

Al Williams

Dr. Dobb's Bloggers

Arduino Real Time

January 14, 2014

Many tasks that you handle with a small microcontroller don't require an operating system. However, as the definition of "small" keeps getting bigger, it is getting more tempting to do projects where you can benefit from some help with things like multitasking and concurrency management. Besides, as small gets bigger, you also get more processing power that you can afford a little overhead to make life easier.

Look at the Arduino, just as an example. The old ATmega 8 chip had 8K words of flash and 1K of RAM. The newer versions of Arduino often use an ATmega328, which has 32K of flash and 2K of RAM. However, last time I checked, the price of an ATmega328 was actually less than an ATmega8. So if I wanted an ATmega8, I could save money by buying the larger chip and I could use 1K of RAM and 24K of flash for my operating system before I would impact what I wanted to use anyway.

I looked at this once before when I was writing LWOS, which could target several different small processors. However, there are several other Arduino-friendly RTOS choices.

A project on Google Code covers several choices including NilRTOS, ChibiOS/RT, and a FreeRTOS port. There is also the duinos port of FreeRTOS. I also found RTuinOS and I'm sure there are plenty more out there.

All of these take some setup. For example, RTuinOS uses make and has some restrictions on where you can install it. I was interested in NilRTOS since it is quite small and the installation was as simple as copying three library directories over to your Arduino libraries.

Once I did the copy, it was easy to open a few examples, including the obligatory LED blinker with two threads (one turns the LED on and the other turns it off). The download includes an HTML file with documentation. Since it is such a simple system, there isn’t much to learn.

Here's a semaphore define:


// Declare a semaphore with an inital counter value of zero.
SEMAPHORE_DECL(sem, 0);

Here's the code to set a semaphore:

   nilSemSignal(&sem);

And, of course, to test a semaphore:

  nilSemWait(&sem);

You do need to set up an initial stack for each thread:

NIL_WORKING_AREA(waThread1, 128);

The thread functions are declared via a macro and there is a static thread table (very similar to lwos). The examples cover using serial I/O, I2C, analog, and more. For a low overhead system, it seems pretty robust.

Are you using an operating system for small projects or just programming to the metal? If you aren't, how long do you think you can hold out?

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Comments:



Video