Mannie Peterson (aka FellFromTree) has developed an embedded operating system called HeliOS that’s designed specifically for 8-bit and 32-bit Arduino boards, and can easily be used from the Arduino IDE.
HeliOS is said to have only 21 function calls and implements cooperative and event-driven multitasking, task notification/messaging, timers, and memory management. It’s a non-preemptive multitasking kernel so you won’t have to deal with mutexes.
The developer explains how scheduling works with HeliOS:
HeliOS uses a run-time balanced strategy which ensures tasks with shorter run-times are prioritized over tasks with longer run-times. This ensures all running tasks receive approximately equal total run-time without using context switching. The other multitasking option available in HeliOS is event driven multitasking, which uses the wait/notify and timer interfaces. Mixing cooperative and event driven tasks in HeliOS is not a problem.
To give it a try, simply open the Arduino IDE Library Manager, search for “HeliOS” and click install it. There will be some example including a blink sketch using cooperative multitasking:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <HeliOS_Arduino.h> int ledState = 0; void taskBlink(int id_) { if(ledState) { digitalWrite(LED_BUILTIN, LOW); ledState = 0; } else { digitalWrite(LED_BUILTIN, HIGH); ledState = 1; } } void setup() { xHeliOSSetup(); pinMode(LED_BUILTIN, OUTPUT); int id = xTaskAdd("TASKBLINK", &taskBlink); xTaskWait(id); xTaskSetTimer(id, 1000000); } void loop() { xHeliOSLoop(); } |
While HeliOS has been designed for Arduino AVR, SAM, and SAMD architectures, it should be possible, and fairly easy, to port it to other microcontrollers with Arduino support. The operating system, which the developer says is better described as a “multitasking kernel for embedded systems”, is meant to be a lightweight as possible, and for instance, has a much lower footprint than FreeRTOS.
You’ll find the source code and documentation on Github and the Arduino Project Hub.
Jean-Luc started CNX Software in 2010 as a part-time endeavor, before quitting his job as a software engineering manager, and starting to write daily news, and reviews full time later in 2011.
Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress
I really don’t think this is necessary, seeing that freertos already exists as a library for Arduino..
As long as it is maintained, and ported over to other controllers, I don’t see why this wouldn’t make for another viable option.
I like the fact that it is simple and open source — it might actually be worth the effort to try and wrap my head around it.
Probably best applied to 32-bit. In simpler cases, multi-tasking can usually be addressed fairly easily using hardware events.
hm.. looks like freertos to me..
and shouldn’t ‘int ledstate’ in that example be ‘volatile int ledstate’ ?
No. In the Arduino IDE, as in C/C++ “int ledState = 0;” types the global variable “ledState” as an integer and initializes it to zero, which would have been the case by default without the “=0”, but some feel that explicitly initializing variables when they are declared is a good habit to learn.
However… I later did notice that in the explanation for the HeliOS “Blink” example on their GitHub page that there is a “volatile int ledState = 0;” declaration. I don’t know if this is a typo, intentional, or just from habit, but there is really no need to use “volatile” to protect the “ledState” global variable from compiler optimization in this case. Regardless, the code should run with or without the “volatile” declaration anyway. Notice that in the screenshot of the blink code in the CNX post above, the “volatile” declaration is gone! That’s what I was going by when I wrote my first reply above.