Arduino + FreeRTOS + C++ = Platform for Teaching Real-Time Software Design

So this is the latest idea that I've gotten all obsessive-compulsive over: using Arduino, FreeRTOS, and C++ to teach real-time software design and embedded software development. I like the combination as a platform to teach both introductory students (using the Arduino Uno, SW and IDE), and to advanced students (using Arduino Mega or clone, FreeRTOS, C++, Eclipse, make, etc.). I really like the progression of starting out on the Uno with Arduino 1.0 and progressing to a multitasking RTOS, interrupt driven device drivers, etc. I like FreeRTOS because it's small, understandable, and it works. I like C++ because having spent many years in commercial product development (after having spent part of graduate school teaching real-time software design) I believe you need to climb as high on the abstraction ladder as you can to control costs, yet you need to see what's going on under the hood. The combination of Arduino and FreeRTOS hits a sweet spot of "just complex enough to be worth learning about".

So I started out doing exactly that: learning the AVR architecture on the Uno using Arduino 1.0, got FreeRTOS running on a Freetronics EtherMega (a Mega clone that incorporates the equivalent of an Ethernet Shield on the same board), wrote C++ OO wrappers around the FreeRTOS facilities (Task, MutexSemaphore, PeriodicTimer, etc.), wrote interrupt-driven device drivers in C++ (Serial, SPI), and a main program using a simple unit test framework that exercises all of it and demonstrates how it works. And there's plenty of room for student projects to write software for various applications, shields, hardware control, etc.

Recent blog articles I've written on this project:

Link to tarball can be found on project page:

http://www.diag.com/navigation/downloads/Amigo.html

For years I've futzed around with stuff like an Atmel AT91 evaluation board, a couple of BeagleBoards, etc. But even though I've spent years doing commercial development with Linux on ARM and PowerPC targets, I think when you throw in a memory management unit and the machinations that Linux goes through to be platform-independent, the result is too complicated for anything other than an advanced course. An RTOS like VxWorks, for which I've done a lot of commercial development, is too big, complex, and expensive, to use in anything except an advanced class. But once students got though Arduino, and then FreeRTOS, I think they could easily progress to VxWorks, and even to Linux, without much problems. The progression of Arduino and FreeRTOS would give them the confidence that they could figure out the more complex stuff.

I know this is my thing and I don't necessarily expect anyone else to be interested in it. But here it is.

-- Chip

Interesting idea.

Some very basic projects showing its capabilities would be appreciated.

Hi,

Thanks for the links - I'll be devouring those tonight. I'm trying to incorporate some RTOS context switching into my project for school and I've been having trouble getting my Uno and Ethernet Shield to run with DuinOS. Any help, guidance, or mentorship would be greatly appreciated.

I know this is my thing and I don't necessarily expect anyone else to be interested in it. But here it is.

Thanks for the articles.
Out of curiosity, did you find any application on Arduino that you could not handle without using an "RTOS"?
Multitasking, multithreading etc are important concepts, but are they really needed in a "single-user", single-application system based on Arduino?

Sorry for my very late reply. Life intrudes.

For an platform of modest capabilities like the Arduino Uno that uses the 328P, I think an RTOS is probably overkill, and difficult to boot because of the memory constraints. You spend all your time agonizing over how big the stack for each task needs to be.

On the Arduino Mega that uses the 2560, it's completely doable and reasonably painless. But is it necessary?

There are a lot of applications that, for reasons of functionality or performance, really gain from [1] interrupt driven device drivers, and [2] multithreading as a fundamental mechanism to talk to the device drivers. Not only is it a more useful architecture, but the ability to separate functionality behind a more opaque interface, saves money in the long run (in terms of commercial development). Plus, it's a very useful teaching platform, since this approach is used a lot in the commercial world on 2560-class processors and above.

On the flip side, I'm in the midst of a paying gig in which I'm writing firmware for several products based on a PIC16F1823 microcontroller, which has a scant 2K of program memory (flash) and 96 bytes (that's bytes, not KB) of RAM. I'm writing in C, and I've developed as many as six interrupt driven device drivers on one of the products. But there's no RTOS, nor is there really space to have such a thing. It's just a task loop. So i think it totally depends on the problem you're trying to solve and the resources at your disposal. A colleague is developing for a larger PIC on a related project, and he is in fact using FreeRTOS and multitasking so that he can do slower, non-real-time tasks in the background.

At the higher end of the embedded realm, which in my work typically involves PowerPC and ARM SoCs, it's all over the map, using OSes (not necessarily RT) like CMX, C-Executive, Simple Executive, VxWorks, the late pSOS, and even a stripped down Linux. These range from very simple to extremely complex in terms of API, use model, capability, etc.

I should add that two things I've seen really increase the complexity of any embedded system: [1] a memory management unit, and [2] a file system. Once you have either of those, you've entered a much more complicated realm of system architecture and design. I'm not saying they're bad ideas. But once you have them, you're going to really want an OS of some kind and work at a higher level of abstraction.

This is very interesting. When I took a grad course in real-time programming we started by coding a simplified version of the project on bare hardware (a 486 if I recall), just to show how hard it is, then we wrote our own real time OS and developed a feature-rich version of the program. I think it was having a GUI and maintaining the real time hardware control that made the OS a blessing.

I remember struggling over coding the memory manager (which paid off when I interviewed with Microsoft's NT Kernel group and had to design a virtual memory manager on a white board in front of a panel of interviewers). Without the RTOS you simply wouldn't have a memory manager; it wouldn't be a matter of being more complex to not have the OS. I can't imagine why you'd even want to try to deal with paging within application code.

For the scope of what you can do with an arduino, I think simply using timer interrupts will let you do anything you could possibly want with real time, but I do understand that's not the point of your post.

Chip,

Good stuff! I was brought up in the embedded world back in the 80's and think interrupts vs. polling loops. I've always been surprised how small an RTOS can be to fit into a microprocessor. You really don't need much.

I'm working on a project now that involves a few serial devices (XBee being one) and would love to try it out after I get the initial design in place. Keep up the good work!

Hi Chip,

Are you still tracking this thread? I'd like to incorporate your ideas into an IDE I'm working on.

The IDE allows arduino users to use predefined declarations for tasks, either in code or as flowchart symbols
http://www.kicchip.co.uk/index.php/component/content/article/50-kompiler-pre-release-overview?start=8

I've been looking at FEMTOS too, users can select either Free RTOS or FEMTOS

It needs work but it sounds like you have allready done the parts I was planning on doing next, can you PM me?

I need to do a project that requieres RTOS on Arduino UNO with ATmega328, for what I read and what you say is not possible right? In any case, do you think theirs a way to implement it like DUINOS or something else?

I've run FreeRTOS on a 328P, so it's certainly possible. But having multiple threads, each with its own stack, is a real challenge given the tiny RAM on that target.

On an Arduino Nano 3 (AT328P uC), I run FreeRTOS 8.x with ADC sampling, filtering, PWM-DAC tasks (with 1KHz sampling frequency) + 2 UART-diagnostic tasks (= 5 tasks in total) with Eclipse + AVR Plugin as "high-level" C++ application. For teaching, a quick start can be done with the Arduino-IDE even for very beginners: AVR basic microcontroller with easy-to-understand hardware, quite inexpensive Nano-Hardware available for students, great abstraction of software complexity by C++ source-lib, but allowing all details and enhancements (like RTOS, C++, own libs) with Eclipse, make, avr-gcc, ... Arduino Nano is great !