Arduino Forum

Development => Other Software Development => Topic started by: fat16lib on Oct 31, 2011, 03:51 pm

Title: Try FreeRTOS - compare with ChibiOS/RT
Post by: fat16lib on Oct 31, 2011, 03:51 pm
I have posted a port of FreeRTOS here http://code.google.com/p/beta-lib/downloads/list (http://code.google.com/p/beta-lib/downloads/list).  The file is FreeRTOS20111031.zip.

There is also a port of ChibiOS/RT posted as ChibiOS20111027.zip.  See http://arduino.cc/forum/index.php/topic,76932.0.html (http://arduino.cc/forum/index.php/topic,76932.0.html).

I have attempted to use the same options in both ports.

I used timer 0 for the system timer so the OS tick time is 1024 usec or 976 Hz.

I provided the same five examples in each port.
Quote
There are five examples in the FreeRTOS/examples folder.

frBlinkPrint - A simple example of three threads. A high priority thread
               blinks an LED, a medium priority thread prints a counter
               every second, and the low priority idle loop increments the
               counter.

frContextTime - Test to determine context switch time with a semaphore.

frFastLogger - Data logger optimized for 328 Arduino.  Logs two analog pins
               every two ticks (1024 usec tick).

frJitter - Test of jitter in sleep for one tick.

frMegaLogger - Data logger optimized for Mega.  Logs four analog pins
               every tick (976 samples per sec, four values per sample)
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: westfw on Nov 03, 2011, 02:53 am
Quote
I have posted a port of FreeRTOS ...
There is also a port of ChibiOS/RT

So what are YOUR observations?
I'm looking at those new ARM chips (and similar) with 1M of flash, and also concluding that it doesn't make sense to start working on them without some sort of kernel underneath.  If they were "real" computers (with MMUs), some form of linux would be an obvious choice.  These "in  between" chips (too big for a monolith, too small for linux) are a puzzle.  "Free" seems pretty important.

The logical conclusion is that the 2560 on the MEGA is in a similar situation.  A single 256k program is ... not desirable (though I'll concede that there are some smaller efforts with lots of data that could make use of that memory...)

Are there non-real-time alternatives as well?
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: fat16lib on Nov 03, 2011, 03:27 pm
I didn't comment much on FreeRTOS because I like to do things that are fast and there is this restriction:
Quote
FreeRTOS may not be used for any competitive or comparative purpose, including the publication of any form of run time or compile time metric, without the express permission of Real Time Engineers Ltd. (this is the norm within the industry and is intended to ensure information accuracy).

FreeRTOS is popular, runs on lots of chips/boards and there are books and support.  For several reasons it is not my favorite of systems I have tried.  I don't like the fact is only uses dynamic memory.

Be sure to look at the semaphore context switch examples on a scope.  There is an amazing difference.

I like ChibiOS/RT, it is extremely fast, small and uses RAM very efficiently.  The author of ChibiOS/RT, Giovanni Di Sirio, has a design/coding style that I like.

A much better recommendation is here http://www.yagarto.de/projects/chibios/index.html (http://www.yagarto.de/projects/chibios/index.html).  Michael Fischer is a true expert in ARM.  I use his YAGARTO tool-chain on ARM.  He discusses six popular RTOSes in this link.

Also here http://www.yagarto.de/projects/rtoscomp/index.html (http://www.yagarto.de/projects/rtoscomp/index.html).
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: Grumpy_Mike on Nov 03, 2011, 03:46 pm
We did have a discussion thread on RTOSs a month back, and the conclusion was they were a total waste of time especially in the arduino context.
They just suck the power out of a processor and move the debugging process into the fighting the RTOS region.
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: fat16lib on Nov 03, 2011, 04:38 pm
I have no interest in discussion.  Did anyone do a serious application that is difficult without a RTOS.

The Chibios/RT Mega data logging example has a 15 usec overhead for the context switch to the ADC task.  It takes 110 usec for each analogRead so the overhead is small.

The logger example can read four analog pins every 1.024 milliseconds, format them as CSV text, and write the data to an SD without missing points. 

SD cards can have occasional write delays of 200 milliseconds so there can be well over 100 context switches to the ADC task while a block is being written to the SD.

I have done special timer based sketches to do loggers like this but ChibiOS makes it easy.  Context save/restore can be faster in a custom solution but that speed is not required when using analogRead().

This example can log data about 100 times faster than a simple loop that does four analogRead() calls and then writes the data to the SD using print.

In this example a context switch happens every 1024 usec and takes 15 usec so the overhead is about 1.5%.
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: Grumpy_Mike on Nov 03, 2011, 05:42 pm
Quote
I have no interest in discussion.

OK bye bye  :P
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: fat16lib on Nov 03, 2011, 06:16 pm
Sorry, by discussion, I meant without facts.  Too much of the stuff in forums is just opinion.

I believe in the old saw sometimes attributed to Admiral Grace Hopper.
Quote
"One accurate measurement is worth a thousand expert opinions" - Adm Grace Murray Hopper ( Dec 9 1906 to Jan 1 1992)

She also coined the term "bug".

Here are a few more of her quotes http://womenshistory.about.com/od/quotes/a/grace_hopper.htm (http://womenshistory.about.com/od/quotes/a/grace_hopper.htm).  Not bad for someone who started programming in 1944.
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: Grumpy_Mike on Nov 04, 2011, 06:06 pm
Quote
Sorry, by discussion, I meant without facts.

Well I don't know what you mean by facts but I have been in electronics / micro controllers professionally for over 40 years. I have as a manager had to run projects both with and without an RTOS so I have seen what actually happens.
Normally it is the software engineers initially push to use an RTOS with the expectation of an easy life. Very rarely does it happen like that because the problems encountered with the project then appear to be related to exactly how the RTOS is working and how to get it to do what they want. In the mean time the extra resource in memory and CPU cycles means you have to go to at least the next processor up as the power and capacity is drained by the RTOS.
In the end the project works although about four times the effort has gone in to the project and the hardware is more expensive.
Not using an RTOS on the other hand forces the programmer into actually thinking and planning what they want to do, something that is not always popular with them. However I have found that non RTOS projects come in faster and have a less demanding CPU requirement. The actual system is often more responsive as well.

Of course some projects are so large and have many programmers working on them that an RTOS is almost mandatory. However for small projects I have found them to be totally useless.

Your specific question:-
Quote
Did anyone do a serious application that is difficult without a RTOS.

Yes I have managed and contributed to these. The largest project was an Enterprise scale access control system with up to 256 nodes spanning buildings covering an area of over three square miles. I think that ticks all your boxes for that one.

Quote
The logger example can read four analog pins every 1.024 milliseconds, format them as CSV text, and write the data to an SD without missing points. 

The point is that what ever you can do with an RTOS you can do without it, usually much faster.
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: westfw on Nov 04, 2011, 09:21 pm
Quote
In the mean time the extra resource in memory and CPU cycles means you have to go to at least the next processor up

Ah, but we already went to the "next processor up" when the m328 became standard.  And then the MEGA, and the MEGA2560, and now we're looking at seeing the next stage beyond that.

Quote
The largest project was an Enterprise scale access control system

About how much object code?  When you say you didn't use an RTOS, does that mean no  multitasking at all, or just no OTS well known RTOS?  There's so much of "modern computing" that is deeply dependent on "tasks" that I wouldn't want to try to do it without some OS.  Most of Networking, for instance.  cisco routers ran an OS back in the day when they had 512K of memory; I can't imagine having implemented what they did without the OS.  And an STM32F4 eval board is a candidate for an Arduino environment, and has MORE memory than that...

Quote
non RTOS projects come in faster and have a less demanding CPU requirement.

But we rapidly approach an era where memory and CPU cycles are SO cheap...  An Arduino Uno is overpowered for many of the tasks it is put to.  The hypothetical Arduino Due is ridiculous.

Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: Grumpy_Mike on Nov 04, 2011, 10:04 pm
Quote
Ah, but we already went to the "next processor up" when the m328 became standard.

Well we went up to another pin compatible part with the same CPU horse power. The problem on a commercial project is when this happens you have to scrap all the existing prototypes and try and claw them back.

Quote
About how much object code?

It was just over 100K when compiled.

Quote
When you say you didn't use an RTOS, does that mean no  multitasking at all,

You don't need an RTOS to do multitasking, you just need to design your code correctly.

Quote
But we rapidly approach an era where memory and CPU cycles are SO cheap.

That is an argument that I first came across 30 years ago, it is as true now as it was then.  ;)

Quote
The hypothetical Arduino Due is ridiculous.

So lets cripple it with a RTOS?
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: fat16lib on Nov 04, 2011, 11:52 pm
Grumpy_Mike,

Please share some facts with us.  Please provide numbers from any measurements that prove these statements.

Quote
In the end the project works although about four times the effort has gone in to the project and the hardware is more expensive.

Which processor and RTOS/version does does the above refer to?  Why did you allow a RTOS to used?  Didn't you do prototyping as part of the decision process?

Quote
They just suck the power out of a processor and move the debugging process into the fighting the RTOS region.

What percent of cycle go to the RTOS and where in the RTOS?  Which processor and RTOS does this refer to?  What is the nature of the application that causes so much overhead in the RTOS?

Quote
However for small projects I have found them to be totally useless.

How do you decide when a RTOS is useless?

Quote
You don't need an RTOS to do multitasking, you just need to design your code correctly.

What techniques do you use that can't be done by a RTOS?  Which RTOSes did you benchmark to prove that roll you own was required?

Quote
So lets cripple it with a RTOS?

The processor in the Due was designed for use with a RTOS.  Why do you think it will be crippled?

Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: westfw on Nov 05, 2011, 02:03 am
Well, on Mike's side, I've seen RTOS vendors recommend things like using a UART ISR to merely send a message to wake up the actual driver, which would then do the actual reading of the data from the uart.  This can make sense from a "strict real time" point of view, and might be OK for an OS with a complex tty driver, but for the typical Arduino-class serial driver it would be insane (IMNSHO.)  If you use an RTOS, you can spend a lot of time making this sort of decision, and bending code in "unnatural" and ultimately inefficient ways, just to fit the capabilities and style of the RTOS in question.  It all ... depends.

It's sorta like C++ and Java programming for the desktop being more about learning the existence and capabilities of a multitude of classes and libraries than learning the language itself.  (Sometimes I worry that Arduino is also headed that way.)

In what way is ARM CM3 "designed for use with an RTOS" ?  I'd rate most RISC microcontrollers, with their large register sets and relative small amounts of RAM, as being generally poor choices  for RTOS or even multitasking.  Too much context and not enough place to save it...
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: fat16lib on Nov 05, 2011, 02:35 pm
westfw,

Here is an example that shows why the ARM CM3 architecture is very RTOS friendly.  The example is a bit long.  I will use the internals of ChibiOS to illustrate.

The context for the ARM Cortex M3 is divided into two parts.  ChibiOS refers to the first part as the Interrupt saved context.  This part of stack frame is eight 32-bit words and is saved by the NVIC, Nested Vectored Interrupt Controller, when an interrupt occurs.

Here is the structure in ChibiOS:
Code: [Select]
struct extctx {
  regarm_t      r0;
  regarm_t      r1;
  regarm_t      r2;
  regarm_t      r3;
  regarm_t      r12;
  regarm_t      lr_thd;
  regarm_t      pc;
  regarm_t      xpsr;
};

The remainder of the context can be saved by a single instruction if a context switch is required.  Here is the structure for that part of the stack frame in ChibiOS:
Code: [Select]
struct intctx {
  regarm_t      r4;
  regarm_t      r5;
  regarm_t      r6;
  regarm_t      r7;
  regarm_t      r8;
  regarm_t      r9;
  regarm_t      r10;
  regarm_t      r11;
  regarm_t      lr;
};

Here is the context switch function that is called by ChibiOS at the ISR level if a context switch is required.
Code: [Select]
#define PUSH_CONTEXT() {                                                    \
  asm volatile ("push    {r4, r5, r6, r7, r8, r9, r10, r11, lr}"            \
                : : : "memory");                                            \
}
#define POP_CONTEXT() {                                                     \
  asm volatile ("pop     {r4, r5, r6, r7, r8, r9, r10, r11, pc}"            \
                : : : "memory");                                            \
}
__attribute__((naked))
void port_switch(Thread *ntp, Thread *otp) {
  PUSH_CONTEXT();
  asm volatile ("str     sp, [%1, #12]                          \n\t"
                "ldr     sp, [%0, #12]" : : "r" (ntp), "r" (otp));
  POP_CONTEXT();
}

When you write an ISR you decide if an event has happened that requires a task to be triggered.  In a USART handler this could be an end of message character.  If you have not received this character there is no overhead.

If you receive the end of message character, you call a ChibiOS routine and it checks if a context switch is required and does the above stack switch if necessary.  When you return from interrupt the rest of the context switch will occur in the NVIC.

On a 72 MHz STM32 the overhead for this call with a context switch is about 2 microseconds.  Most of this time is in determining which task should run, not the context switch.

I have made this as simple as possible so I am sure you will have a "but what if?".

Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: pito on Nov 05, 2011, 09:48 pm
..I've seen on a different forum (avr, arm, pic etc.) an interesting thread - a guy is asking for a help as he need a debugger (for STM32VL) which steps similarly as on AVR at asm instructions level. The other guy is answering him that the STM32 is a different league and nobody needs that actually.. and if the application is seen demanding he has to take simply a STM32 with a higher clock frequency..
An interesting point. I think the debate on rtos vs. superloop is similar - it is about productivity..
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: fat16lib on Nov 06, 2011, 12:18 am
pito,

There are lots of ways to solve a multitasking real time problem.  The solution must satisfy this condition:
Quote
Real-time means it reacts in a timely fashion, i.e. fast enough for the task at hand.

But this must also be true of the tools you use.
Quote
You can however only use it productively and safely if you understand how it works and what the limitations and pitfalls are.


Super-loops are simple so they are great for most programmers if they satisfy the real-time requirement.

Preemptive RTOSs have pitfalls that trip up the inexperienced and are probably not the best choice for most hobbyists.  This may not be for you if you don't know what priority inversion is or the difference between a semaphore and a mutex.

Here is a nice article about multitasking alternatives http://www.splatco.com/rtos_1.htm (http://www.splatco.com/rtos_1.htm).
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: pito on Nov 06, 2011, 04:26 pm
.. chibios - 390ns context switch with STM32F4 Discokit..
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: westfw on Nov 06, 2011, 11:59 pm
Quote
chibios - 390ns context switch with STM32F4 Discokit..

Counting exactly what?  fat16lib's quoted code shows 18 words of context saved and restored (including the stack pointer swap and the registers stacked by the NVIC.)  If the RAM is single-cycle 80MHz memory, that should take at least 450ns...
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: fat16lib on Nov 07, 2011, 01:10 am
The STM32 F4 used in this test has a 168 MHz clock and
Quote
RAM memory is accessed (read/write) at CPU clock speed with 0 wait states.

Push and pop run in 1 + N cycles where N is the number of registers.

The NVIC takes 12 cycles to respond or return from an interrupt. 

The actual register swap takes a lot less than 390 ns.
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: westfw on Nov 07, 2011, 01:27 am
Quote
168 MHz

oops.  I missed that part, thinking it was still in the 80MHz class.  Never mind. :-(
(in that case, 390ns sounds about right...)
(damn; I went searching through the datasheet to see if RAM was wider than 32bits or other optimization, and I forgot to check the clock  rate!)

Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: pito on Nov 07, 2011, 02:50 am
:) it depends on the compiler and its setting, however. I did with an another compiler and get slower 440ns (11.4, 2.268Meg/sec) context switch, but higher 11.11 (semaphores..) 3.01Megsec.
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: fat16lib on Nov 07, 2011, 02:29 pm
Variations due to compiler settings in tests like this are to be expected but are not important.  These tests measure the performance of a few statements in tiny functions.  Code generation for a single statement can change the results like this.  The ChibiOS/RT source has comments describing how compiler options can change performance.

OS overhead should be a small percentage of CPU use in any well designed application.   In my data-logging example using ChibiOS/RT, the OS overhead for the ADC task is about 1.5% of CPU time.  If the context switch time increased by 20% it would still be under 2% of CPU time.

The infatuation with context switch time and misuse of this feature in preemptive RTOSs is the mark of a novice.  Even though many other feature of a RTOS are more important, context switch time sells software.

Giovanni Di Sirio, the author of ChibiOS, says that a good RTOS will have features that cause you to choose it even if you are running a single thread as a super-loop.

A chip like the STM32 has so many features that need good support.  This include things like use of DMA channels with SPI, USART, ADC and other peripherals.  Proper use of the NVIC priority interrupt controller.  In ChibiOS/RT this is done in the HAL and low level driver framework.

It's a puzzle why people choose a given RTOS.  FreeRTOS is very popular even though it's context switch time is several times longer and it has no driver framework.
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: pito on Nov 07, 2011, 06:56 pm
..interestingly the author of the chibios explicitely picked up the context switch time as the number to show when first run the benchmark on F4.. :)
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: fat16lib on Nov 07, 2011, 08:12 pm
As I said, context switch time sells software even though it is almost never the most important criteria for selecting a RTOS.

In large projects the selection of a RTOS is a major task.  The most common criteria for selection usually include things like this:
Quote
available on a particular type of target processor

configurable

scalable

support for specific development languages

availability of application development tools

type of tasking model

type of inter-process communication

type of scheduling mechanism

customer support by the vendor

Here is another list from NASA
Quote
• An RTOS is a preemptive multitasking operating system intended for real-time applications.

• It must support a scheduling method that guarantees response time
- Especially to critical tasks

• Tasks must be able to be given a priority
- Static or dynamic

• An RTOS has to support predictable task synchronization mechanisms
- Shared memory mutexes / semaphores, etc.

• A system of priority inheritance has to exist

• Manages hardware and software resources.

• Deterministic: guarantees task completion at a set deadline.
- A system is deterministic if, for each possible state and each set of inputs, a unique set of outputs and next state of the system can be determined.

• Behavior time constraints should be known and minimized
- Interrupt latency (i.e., time from interrupt to task run)
- Minimal task-switching time (context switching)


See table 3 in this for criteria weights http://articles.adsabs.harvard.edu//full/2003ESASP.532E..50M/0000050.005.html (http://articles.adsabs.harvard.edu//full/2003ESASP.532E..50M/0000050.005.html)
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: Coding Badly on Nov 10, 2011, 05:31 am
Quote
Are there non-real-time alternatives as well?


I'm a big fan of the Quantum Framework.  The learning curve is steep but, in my opinion, well worth the climb.

Quote
Did anyone do a serious application that is difficult without a RTOS.


Indirectly.  I developed a support application for an air conditioner controller.  The controller developer used the Quantum Framework.  The bug rate was much lower than an alternative controller based on a preemptive kernel.  The developer was able to turnaround bug fixes in a few days (versus a few weeks), the bugs were generally trivial problems (versus serious bugs that were never properly fixed), and the controller was very responsive (versus stalling from, in some cases, a few seconds).

Directly.  I can say the same things about the support application which was also built using the Quantum Framework: easy to debug, solution expressed in a way that was easy to understand, major modifications did not introduce any serious bugs, and very responsive.
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: fat16lib on Nov 10, 2011, 02:33 pm
I was going to provide QP for test but found Quantum Leaps has a download for the Arduino http://www.state-machine.com/arduino/index.php (http://www.state-machine.com/arduino/index.php).

I am a big fan of state machines for applications like Coding Badly described.

For a while I tried to apply state machines to almost every problem.  It worked well for controlling laboratory support equipment, like super conducting magnets. 

In my world, large high energy physics experiments, this was often called slow controls.  Here is a bit about this area at the CERN LHC http://cerncourier.com/cws/article/cern/33360 (http://cerncourier.com/cws/article/cern/33360).

Here is a large experiment http://atlas.ch/ (http://atlas.ch/) at LHC

For other tasks you can't beat a preemptive RTOS.  So match the solution to the problem by prototyping the tricky part.

Once again There is no best solution for all problems.  The main categories are super-loops, round robin schedulers, cooperative multitasking systems, preemptive RTOSs, and state machines.

Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: FreeRTOS on Nov 13, 2011, 03:47 pm
I won't pretend to have read this thread other than skimming what is on this page - I just saw it was there from a Google alert, so my comments might not be that relevant.

To comment on context switch times though.  It is often quoted as the be all and end all, but in reality, you can make it as fast or slow as you like, within the limits of the hardware.  To get it really fast, just remove all the functionality (stack checking, tracing, interrupt nesting, etc.) and use a bitmap scheduler (at the cost of usability).  Alternatively, write it all in assembly code, making it harder and longer to port, and harder to test.

The Cortex-M takes a certain time to save/restore its registers.  This is done mainly in hardware, on interrupt entry, so has nothing to do with the RTOS.  What is left of the context can be saved in just one or two asm instructions, which again, are fixed execution time.  When people measure context save times, generally they measure different things on different systems, and compare apples with guava, thinking they are comparing apples with apples.

I never claim FreeRTOS to be the fastest, but one of the cleanest, smallest, best supported, etc. etec.

Regards,
Richard.


+ http://www.FreeRTOS.org
Designed for Microcontrollers.  More than 7000 downloads per month.

+ http://www.SafeRTOS.com
Certified by TÜV as meeting the requirements for safety related systems.
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: fat16lib on Nov 13, 2011, 04:47 pm
Richard Barry's FreeRTOS is certainly very popular and that is the main reason I ported it as an Arduino library.  FreeRTOS works well as an Arduino library.

Personally I prefer ChibiOS/RT to FreeRTOS on the Arduino.  It uses less RAM and the faster performance is not due to assembly code or tricks that make it hard port, it because of elegant design by Giovanni Di Sirio.

On Cortex-M I really like ChibiOS/RT.  Giovanni's support for STM32 is great.

In short I don't think Barry's comments apply to ChibiOS.  ChibiOS has many debug features, tracing, interrupt nesting, etc.

Both are good RTOSs.  You need to choose which you like by using them.
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: maniacbug on Dec 20, 2011, 07:51 pm

We did have a discussion thread on RTOSs a month back, and the conclusion was they were a total waste of time especially in the arduino context.
They just suck the power out of a processor and move the debugging process into the fighting the RTOS region.


I have been experimenting lately with FreeRTOS as a consequence of buying a Maple board, and I am coming to much the same conclusion, at least as regards an ATmega328p.

In my experiments, a stack size of 85 (as in the 323 port, and in the port from the OP on this thread) is a bit small, 100 is safer.  1500 as a heap size is about the maximum safe size.  So that's 15 tasks, minus space for any queues you'll need.

On the other hand, using my personal framework that just manages communication between objects which all call an update() within loop(), I've got active sketches running ~40 things at once, and I'm nowhere near the edge of SRAM.

So I am also concluding that FreeRTOS is overkill for 328p.  Although I am loving it on the Maple.
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: fat16lib on Dec 20, 2011, 10:44 pm
If you can run 40 things with calls to an update function from loop, you have soft real-time and this is a good approach.

You could probably do the same thing with cooperative scheduling in a RTOS since that doesn't require a stack for each thread.  It does allow priority scheduling.  That would allow 40 tasks on a 328.

Also you can always find a way to avoid a RTOS by writing enough special purpose code.  Often using a RTOS saves time if it will solve a problem without a lot of code.  

When I started doing embedded systems, the battle was over assembler or a higher level language.  The answer is that both have there place and we still do some assembler.  The same is true for RTOSs.  Old timers seem to be flat out against using a RTOS.  The fact is that often a RTOS is an easy way to solve a problem even on small chips.

A RTOS like ChibiOS or FreeRTOS is useful if you can't call an update function with a short enough period or if you need to do something at precise intervals.

Data logging to an SD card is an example where calls to an update function will not work well.  A write to an SD card can occasionally take 100 milliseconds.  If you want to take data points evenly space in time every millisecond you need something more than loop.

It is easy to solve this problem with a few lines of code using two threads and preemptive scheduling.  A high priority thread reads the data and puts it in a circular buffer and a lower priority thread writes data to the SD.  I have included this example on 328 Arduinos for both ChibiOS and FreeRTOS.

Once again you can solve this problem in a number of ways without a RTOS but it only takes a few lines of code with an RTOS.

As to FreeRTOS, I much prefer ChibiOS on small chips.  ChibiOS is much faster and uses less RAM.  

I have ported ChibiOS so loop runs as the idle task and has all the RAM that has not been statically allocated to other tasks.   This means you can start writing an application using the normal setup() and loop().  If it turns out that you have a problem that could be solved with another thread it is easy to use the ChibiOS library and add a thread.
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: maniacbug on Dec 21, 2011, 01:23 am

If you can run 40 things with calls to an update function from loop, you have soft real-time and this is a good approach.

...

Also you can always find a way to avoid a RTOS by writing enough special purpose code.  Often using a RTOS saves time if it will solve a problem without a lot of code.  

When I started doing embedded systems, the battle was over assembler or a higher level language.  The answer is that both have there place and we still do some assembler.  The same is true for RTOSs.  Old timers seem to be flat out against using a RTOS.  The fact is that often a RTOS is an easy way to solve a problem even on small chips.

A RTOS like ChibiOS or FreeRTOS is useful if you can't call an update function with a short enough period or if you need to do something at precise intervals.


Yes, that's right...  None of the things I'm currently doing demand precise intervals or long updates that require preemption.  "Soft" real-time seems good enough.

I too remember the debate between assembly and C.  One of my early projects was whole commercial release on a dual SH-2 machine in all RISC assembly, which turned out to a be horribly wrong decision.

"Often using a RTOS saves time if it will solve a problem without a lot of code."  Also helps with portability.  I'm rewriting some of my RF radio code in FreeRTOS, and it would be handy to one set of code run on all environments.

Anyway, I will try out your ChibiOS port at some point too, once I fully figure out FreeRTOS.
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: gerg on Jan 12, 2012, 12:27 am
I've enjoyed this thread. I've been looking for a scheduler for some time now and haven't really found anything I think; including some of the more popular options. I wasn't even aware of ChibiOS/RT until I read this thread. I quickly ran out and downloaded it. Read the instructions and had a something working within minutes. That's a first. Normally its a royal pain before you can even get that far.

After trying a couple of demos on my mega2560, everything just worked. Next I decided to try something a little more complex. I plugged in an LCD shield, created a couple of extra threads, and now I have the chBlinkPrint example code running concurrently with my threads, which also drive an LCD and read analog button presses for input and serial outputting the value along side the loop count. It was trivial and took all of five minutes. Uses 6984-bytes of flash. This is what a lightweight RTOS should look like.

I've yet to try SPI+ChibiOS/RT. I guess maniacbug may beat me here. I dunno. As my primary goal for SPI is to drive an RF24L01 radio too. So far its proved to be very Arduino friendly so I'm hopefully SPI support will also prove to be a gimme.

I may go back and give FreeRTOS a try again, but at this point, I'm not really seeing much reason to so do.

I've been absent for a while - busy with stuff. I hope over the next month or two I'll be working again on AVR/arduino projects again. I would certainly like to get back to some RF24L01 work as well as finally do some coding on my networking layer for it.
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: tochinet on Dec 18, 2012, 12:17 pm
Anyone other able to compare FreeRTOS ChibiOS and maybe other contenders for Arduino's better (easiest) RTOS ?
Title: Re: Try FreeRTOS - compare with ChibiOS/RT
Post by: fat16lib on Dec 18, 2012, 03:14 pm
There are dozens of little schedulers for Arduino.  Most are no longer actively supported.

Most Arduino users don't need a RTOS.  A cooperative scheduler would be sufficient.

In the near future I expect the Arduino company to add a simple cooperative scheduler.  That will be the best solution unless you have the rare app that needs a true RTOS.

If you need a RTOS, FreeRTOS is a good choice.  It is probably the most popular free open source RTOS.

I like ChibiOS/RT but that is a personal preference.  Most RTOSes offer the same basic features.