When exactly do you choose to use a RTOS (instead of a non-OS approach)?

Started by pozz December 9, 2017
On Sat, 9 Dec 2017 17:05:51 +0100, pozz <pozzugno@gmail.com> wrote:

Mainly it's a question of complexity.  If you need complex services,
say networking, threading, UI, storage, there a lot to be said for an
RTOS - getting those right is a ton of work, and you probably have
better things to do.  If you have a simple device, that needs only
basic functionality, say there's only a limited amount of ROM based
code, a couple of interrupts to handle, and mainly just interaction
with whatever you're controlling, do without.  There is, of course, a
huge grey area between those two, and RTOS come in a number of levels
of complexity as well.
On 09/12/2017 16:05, pozz wrote:
>
I use it where I have a number of 'tasks' which then interact with each other. If your system is a pure state machine then there is no need for an RTOS. -- Mike Perkins Video Solutions Ltd www.videosolutions.ltd.uk
Il 10/12/2017 06:34, Robert Wessel ha scritto:
> On Sat, 9 Dec 2017 17:05:51 +0100, pozz <pozzugno@gmail.com> wrote: > > Mainly it's a question of complexity. If you need complex services, > say networking, threading, UI, storage, there a lot to be said for an > RTOS - getting those right is a ton of work, and you probably have > better things to do.
In the past I worked with networking (lwip), UI (emwin) and storage (fatfs) without an RTOS, even if one at a time. All of them can be used with and without a RTOS. The "infinite loop" method worked well for these. Maybe the only serious drawback I noticed is that MCU is continuously polling something. This isn't efficient if you need low power consumption. > threading Good point. Multi-threading is a good thing... but it's not simple to move from a "bare-metal" model to a "multi-threading" approach. You have to face unseen problems, like sharing resources among different threads.
> If you have a simple device, that needs only > basic functionality, say there's only a limited amount of ROM based > code, a couple of interrupts to handle, and mainly just interaction > with whatever you're controlling, do without. There is, of course, a > huge grey area between those two, and RTOS come in a number of levels > of complexity as well. >
On Monday, December 11, 2017 at 8:19:31 AM UTC-5, pozz wrote:
[]
> > In the past I worked with networking (lwip), UI (emwin) and storage > (fatfs) without an RTOS, even if one at a time. All of them can be used > with and without a RTOS. The "infinite loop" method worked well for these. >
Both Mike and Robert made good points. You need to examine your requirements.
> Maybe the only serious drawback I noticed is that MCU is continuously > polling something. This isn't efficient if you need low power consumption. >
Was this in your loop application? An RTOS may have features for low power that would be difficult to create in a simple loop implementation.
> > > threading > > Good point. Multi-threading is a good thing... but it's not simple to > move from a "bare-metal" model to a "multi-threading" approach. You > have to face unseen problems, like sharing resources among different > threads. >
Yes it can be a little more work, but the Grand Do All Loop can have the other problem: Each routine in the given state is called whether it needs to execute or not. So you still may be delayed in getting to that one function that has to run NOW. My point is, the design planning needed for multi-threaded RTOS is more complex, but how often have you coded a bare metal multi-threaded system. If you want the benefits of threading, there is a cost to pay. Overall, the system can be more efficient with a multithreaded RTOS. One last comment: some folks gravitate automatically to the Do All loop. Don't choose that just because you think an RTOS is too much overhead (e.g. task swapping). In many cases, the overhead of an RTOS is a lot lower than you might think. and the brain power needed to program in an RTOS environment can be lower too. I remember working on one system that you had to count the number of instructions you were executing between checkpoints because the Do All loop was trying to maintain a clock. A real pain to make changes! So the pain can go both ways. HTH, ed
pozz wrote:
>
These days? So the world is pretty much divided into RasPi-sized things and Arduino-sized things. So I'd be sort of tempted to not use an RTOS at all. I don't know what the practical upper limit for connecting Arduinos to a RasPi are but it's got to be a substantial number. If you'd need more than an Arduino provides for hard realtime then that's different. -- Les Cargill
pozz wrote:
> Il 10/12/2017 06:34, Robert Wessel ha scritto: >> On Sat, 9 Dec 2017 17:05:51 +0100, pozz <pozzugno@gmail.com> wrote: >> >> Mainly it's a question of complexity.&#2013266080; If you need complex services, >> say networking, threading, UI, storage, there a lot to be said for an >> RTOS - getting those right is a ton of work, and you probably have >> better things to do. > > In the past I worked with networking (lwip), UI (emwin) and storage > (fatfs) without an RTOS, even if one at a time.&#2013266080; All of them can be
used
> with and without a RTOS.&#2013266080; The "infinite loop" method worked well for
these.
> > Maybe the only serious drawback I noticed is that MCU is continuously > polling something.&#2013266080; This isn't efficient if you need low power
consumption.
> > > > threading > > Good point.&#2013266080; Multi-threading is a good thing... but it's not simple to
> move from a "bare-metal" model to a "multi-threading" approach.&#2013266080; You > have to face unseen problems, like sharing resources among different > threads. > >
It can get fiddly ( especially with respect to shared resources between ISRs and "app space" ) but you can always divide things into control blocks and have one "thread" per control block. Each control block contains almost all the state for one "thread". "Almost" - you'll still need stuff to talk to ISRs. This will work in or outside of an RTOS. If an RTOS offers a killer service - something that would be too expensive to recreate ( I'm thinking of power management ) - then you can migrate to an RTOS with a minimum of fuss. I'd suggest making the RTOS non-preemptive in that case. And since you now have stuff partitioned into control blocks, might as well make it state machines. That way you can put code in to hedge race conditions and make it all truly event-driven. Plus, out of hardware timers? Write a hardware timer-queue mux with callbacks. And if you're careful with C++ compilers, the actual code looks suspiciously the same - the local state per thread is all class members rather than control_block[curblock].<something>. The main thing is how allocation is managed - what's the ctors()/dtors() story?
>> If you have a simple device, that needs only >> basic functionality, say there's only a limited amount of ROM based >> code, a couple of interrupts to handle, and mainly just interaction >> with whatever you're controlling, do without.&#2013266080; There is, of course,
a
>> huge grey area between those two, and RTOS come in a number of levels >> of complexity as well. >> >
-- Les Cargill
Mike Perkins wrote:
> On 09/12/2017 16:05, pozz wrote: >> > > I use it where I have a number of 'tasks' which then interact with each > other. > > If your system is a pure state machine then there is no need for an RTOS. >
Indeed. Indeed. -- Les Cargill
On 2017-12-11, pozz <pozzugno@gmail.com> wrote:

> Good point. Multi-threading is a good thing... but it's not simple > to move from a "bare-metal" model to a "multi-threading" approach. > You have to face unseen problems, like sharing resources among > different threads.
I don't see how it's "unforseen". It's not much different than sharing resources among different interrupt routines and the foreground process in a "bare-metal" approach. -- Grant
On Mon, 11 Dec 2017 14:19:28 +0100, pozz <pozzugno@gmail.com> wrote:

>Il 10/12/2017 06:34, Robert Wessel ha scritto: >> On Sat, 9 Dec 2017 17:05:51 +0100, pozz <pozzugno@gmail.com> wrote: >> >> Mainly it's a question of complexity. If you need complex services, >> say networking, threading, UI, storage, there a lot to be said for an >> RTOS - getting those right is a ton of work, and you probably have >> better things to do. > >In the past I worked with networking (lwip), UI (emwin) and storage >(fatfs) without an RTOS, even if one at a time. All of them can be used >with and without a RTOS. The "infinite loop" method worked well for these. > >Maybe the only serious drawback I noticed is that MCU is continuously >polling something. This isn't efficient if you need low power consumption. > > > > threading > >Good point. Multi-threading is a good thing... but it's not simple to >move from a "bare-metal" model to a "multi-threading" approach. You >have to face unseen problems, like sharing resources among different >threads. > > >> If you have a simple device, that needs only >> basic functionality, say there's only a limited amount of ROM based >> code, a couple of interrupts to handle, and mainly just interaction >> with whatever you're controlling, do without. There is, of course, a >> huge grey area between those two, and RTOS come in a number of levels >> of complexity as well. >>
An RTOS can be quite primitive. Just allocate a private stack for each task and a small control block. The simplest system just used a 3 byte control bytes for each task, 2 bytes for the saved stack pointer and a single byte for the task state (mainly runnable/non-runnable bit). After each interupt or system call, just scan the task status bytes in a fixed priority order to find the first task in runnable state, load that stack pointer and perform return from interrupt and off you go with the new task :-) . A more elaborate design would check if the same task is going to continue after a system call and continue without doing a (software)interrupt register save/restore each time.