Some weeks ago, I wrote a blog about the way our definitions can get in the way of our creativity. Lately, we've been having a few conversations here at Microcontroller Central about the pros and cons of using an “operating system” in designs -- and I'd like to suggest that this is another example of letting our definitions get in the way of our designs.
We all tend to think of an “OS” as something that comes to us out of a box, like Windows or Linux or
Free RTOS. But that way of thinking carries a lot of baggage -- not least of which is the notion that an OS is going to burden our system with more resource- and time-overhead than we care to tolerate. The belief is that using an OS in a small system is wasteful and leads to sub-optimal system design.
But let me ask you to abandon that concept of an OS for a minute -- and think of it instead as the name we give to a software design paradigm. Instead of thinking “OS” in terms of a product, try thinking of it as the lowest layer of your software in a multi-layered software design approach, where that lowest layer is just a collection of routines in your code that provide services to your more application-oriented code.
Taking this view, I suspect that most of our microcontroller programs contain some elements of an “OS” of our own creation. Support routines for RS232 communications are one example, as are procedures for switching context from one application task to another. The "OS" is there -- it's just spread all over our code in a more or less hit-and-miss way. Spreading it out like this is OK, but it misses what may be an opportunity: to add the structure and discipline that goes with designing our software specifically to adopt a multi-layered software architecture.
By ignoring the possibility of creating layered code, we may indeed save a few resources, but we also pay a price. Lacking a formal structure, our programs may take longer to build, be harder to debug, be less reliable in the field, and will certainly be harder to maintain, if it ever comes to that. An unstructured approach may also make it more difficult to build libraries of useful service routines that can be adapted and reused over multiple projects. And in many ways, isn't a library like that what a packaged OS really is?
We sometimes need to think about an “OS” not as a packaged product, but as a collection of routines that provide services to our higher-level application tasks in an explicitly layered software architecture. And it's OK to talk about those low-level routines as “our OS,” even if we've created them ourselves!
There's good reason why our software systems have evolved to look more like the structure on the left than that on the right in the figure below.
MCU Code Structures
Structured code, with services (i.e., an OS) at the "bottom" (i.e., closest to the hardware), may have a code penalty, but organic code like that on the right can become a nightmare to maintain.
It's genuinely useful to design our software in a layered way, with OS routines “at the bottom,” rather than designing it (or just letting it grow organically) as a single, monolithic program. The more structured approach may cost us a few bytes of code, a few clock cycles in a task switch, and a few more bytes in our stack demand. But in the end, those things have to be traded off against a better, easier-to-understand design.
Rich Quinnell 3/7/2012 12:54:09 PM User Rank Blogger
Take the Flash Poll
This week's Flash Poll (March 7-13) is about which operating system you use, so it fits right in with this blog. According to Curt's definitions, probably no one should be clicking "None" on the poll, unless your code really does look like the right-hand image. If you're using a monitor program of some kind, the "In-house" option is probably best (unless you bought it, in which case the Commercial OS choice is more appropriate.
When I started with MCUs, a monitor program served as the software foundation. It should be interesting to see what folks are using these days.
Didier_Juges 3/7/2012 3:31:59 PM User Rank Blogger
Re: Take the Flash Poll
My own definition is that an Operating System is a set of routines that are compiled into an executable and loaded on the target system independently of the user code. Under that definition, I checked that I do not use an Operating System. Then I actually checked on (where else) Wikipedia for their definition of Operating System. Wikipedia says the two do not have to be compiled into separate executables, that the source code of the OS and user code can be merged and compiled into one executable.
So, I suppose that according to Wikipedia, I use a home-grown OS.
I normally refer to it as an "executive." It provides access to the underlying hardware through an abstraction layer that is the same regardless of the underlying hardware (for instance, you access timers and I2C devices the same way on the 8051 and on the Cortex-M3,) and it provides services like communication to the host through an API. But it is small and basic, taylored to the job at hand.
Microp 3/8/2012 4:38:15 AM User Rank Program Manager
Re: Take the Flash Poll
We can say an OS is a layer which can make the hardware to function efficiently. Eventhough the hardware have all the capabilities, OS can be act as a bridge between the entire application program and the hardware. For simpler projects we may not use any OS, but we may use it for critical systems where real time responses are require.
That's reasonable, Microp. Don't forget though, that an operating system/executive/kernel/monitor/run-time core can also support things like task scheduling and task-to-task messaging, which may not have much to do with our hardware at all.
The issue though, to my mind, is really about the value of taking a structured (in this case, layerd) approach to embedded software design --calling the lowest defined layer our "OS" if we wish -- and the value of doing so, even in small machines.
northstar 3/9/2012 4:36:25 AM User Rank Program Manager
CMSIS 3
Recently, ARM launched CMSIS3 (Cortex Microcontroller Software Interface Standard) at Embedded World 2012. What is new in CMSIS 3 is the RTOS support. More details are here:
That is really interesting, northstar -- many thanks for the link. I'll spend some time today trying to track down some more information and post links if I find some. If you find more info., I hope you'll post too.
I like the CMSIS library approach, but not so much the RTOS -- but that might be just for want of understanding. I'm curious about how they will be implemented.
Barry writes "An RTOS abstraction layer will, by its very nature, not add to the RTOS functionality, but will make the code bigger and the execution slower." My reaction is that we can say exactly the same thing about C -- but that has in no way impeded the growth of C usage in embedded systems.
At core, some elements of an RTOS are almost certain to be found in any moderately complex microcontroller code -- and given that, having the RTOS standardized and abstracted might not be a bad deal at all. UNLESS it comes to mean that everyone must start spending hours and weeks figuring out ways to work around it (my experience with, for example, Windows).
This article just appeared in EE Journal, probably some of you might already read it. But I would like to enclose for those who missed. It is quite interesting to see what the experts think about RTOS.
I thought the most realistic statement was: "With respect to overhead, John maintains that, especially with small, cheap processors, you really need an RTOS that's been purpose-built with a small footprint and low overhead."
I think a lot of people out there are building their own RTOS -- and about half of them without every realizing that they've done so. (This bold statement assumes that multitasking is the primary function of an RTOS (some may disagree!), and is based on the survey results here.)
Besides, if you strip Linux down to the degree that it will fit in a 4K - 8K program memory and still leave room for doing some real work -- is it really Linux anymore?
Rich Quinnell 3/14/2012 7:43:56 PM User Rank Blogger
RTOS Dead?
ANother critical point made in the article was that there are several "flavors" of real-time, including "hard" real time and "prove it" real time. This last one is especially important in safety-critical and mission-critical applications. To meet some regulatory and specification standards you need to be able to prove mathematically (not empirically) that certain events will always occur within a bounded interval. A well-designed RTOS is needed in order to both provide that certainty as well as make the mathematical proof possible.
That said, the number of applications requiring these attributes is much smaller than those able to settle for "mostly" bounded timing. Hence many developers are developing their own OS, as shown by the results of the recent OS Flash Poll here on MCC..
To save this item to your list of favorite Microcontroller Central content so you can find it later in your Profile page, click the "Save It" button next to the item.
If you found this interesting or useful, please use the links to the services below to share it with other readers. You will need a free account with each service to share an item via that service.