Live data from Hacker News

Introducing multitasking to Arduino

blog.arduino.cc

81–88 of 88 posts

Re: Introducing multitasking to Arduino

#81
post #25

We've done quite a bit of experimentation with adding preemptive multitasking support to non-hard real time software running on MCUs at my current company. After a lot of head banging and dead ends, I've come to the conclusion personally that the embedded community could really use an implementation of the POSIX threading APIs or some meaningful subset thereof for various platforms. They're already standardized, well…

RTEMS has been providing a real time, multi threaded, POSIX API on embedded systems for decades. It's got preemption, interactive shells, a libbsd port with networking, etc. It's popular in spacecraft, especially in combination with NASA's Core Flight System (cFS). I've run it on an STM32F4 and a Raspberry Pi. BSP support is a little lacking in some cases. The raspberry pi is currently crashing on a call to fflush().…

This is great to know about, thank you for sharing!

Re: Introducing multitasking to Arduino

#82
post #56

Earlier quoted context omitted.

Anyone know if a barrel-processor microcontroller exists? Propeller is sort of that way at least regarding main memory access.

Padauk makes them (or at least did), and calls them FPPAs. These are the same folks that brought you those infamous 3-cent-at-single-quantities OTP microcontrollers, so they're probably not for the faint of heart, but what you're asking for has been done.

Some of those 3 cent microcontrollers are actually barrel processors! They have multiple sets of registers (only two on the cheapest) and rotate through them with each clock. Useful for implementing an SPI or UART thread alongside the main task, for example.

The SDCC open source C compiler targets some of those chips now, too.

Re: Introducing multitasking to Arduino

#83
post #47

Earlier quoted context omitted.

I'm a casual embedded-electronics user at best, most of my projects haven't advanced much past turn a simple servo, light some LEDs, maybe read a sensor and POST to an endpoint. For these tasks, Arduino has been pretty good for me and removed a lot of the complexity. The `setup()` and `loop()` model makes a lot of intuitive sense. That being said, I've spent no small amount of time on `String` vs `char*` (as an inexp…

The beauty of the Arduino is the ecosystem behind it. The original hardware is verging on obsolescence, to be polite, but the same code and programming methods can be used on STM32 hardware (that's Arduino-compatible, like the Blackpills) or ESP32. The main difference is that now you have much faster, more modern processors, far more Flash and RAM to play with, not to mention a more powerful I/O peripheral structure…

Pi Pico is also dual core, So in essence it should be easier to write multi-tasking code there.

Re: Introducing multitasking to Arduino

#85

Earlier quoted context omitted.

Embedded can be a tiny little msp430 with RAM measured in bytes, or it can be the latest and greatest intel flagship - it's a description of how the chip is used more than the chip type. As CPUs get more powerful in general, so too do the little development boards. I can spend $10 on a board with multiple cores and megabytes of ram to drive my blinky lights and do wifi - that's enough power to run a full unix.

where? where can i get one?

The Raspberry Pi foundation recently (about a 18 months ago) released their own dual-core ARM chip - the RP2040 - and a corresponding dev board - the Pi Pico. The Pico dev board runs about $4 plus shipping from authorized resellers.

Within the past few weeks, they introduced a wireless version - the Pi Pico W. It's about $6 from authorized resellers, though stock is hard to find at the moment.

https://www.raspberrypi.com/documentation/microcontrollers/

https://www.raspberrypi.com/news/raspberry-pi-pico-w-your-6-...

Re: Introducing multitasking to Arduino

#86

A couple years ago, I managed a summer/afterschool program that introduced thousands of kids (age 8-18) to the Arduino. When we would set up a software abstraction to simplify multitasking (something that came up often with more advanced students), we set up something like this: int time = 0; int max_period = 10000; void loop() { if (time % 100 == 0) { everyTenthSecond(); } if (time % 1000 == 0) { everySecond(); } //…

That is spiritually what their Scheduler library is doing, as mentioned in the article. Which works for a lot of things just fine until one task begins to overrun its time slice or just hangs. And then you're looking into preemption. But if you're overruning an AVR micro maybe it's time to take the training wheels off and move up to a larger system.

Don't be too quick to dismiss the "training wheels" of AVR.

While ARM and RISC-V may have beefier processors, and will probably be migrated to or adopted, AVR has a lot going for it.

First, a common AVR board like the Arduino Uno has built-in peripherals like a temperature sensor. You can build something out of the box without buying external items. If your target market is cheap educational, that's key.

Second, it offers basic microcontroller features like power management, external interrupts, serial and other stuff.

It provides a terrific cheap platform to understand managing interrupts and concurrency safely in your language of choice. Which can be applied to the bigger boards.

Finally, memory constraints require careful thinking through code. I'm not going to implement a full standard library on an Arduino AVR, so what do I bring with me?

Re: Introducing multitasking to Arduino

#87

A couple years ago, I managed a summer/afterschool program that introduced thousands of kids (age 8-18) to the Arduino. When we would set up a software abstraction to simplify multitasking (something that came up often with more advanced students), we set up something like this: int time = 0; int max_period = 10000; void loop() { if (time % 100 == 0) { everyTenthSecond(); } if (time % 1000 == 0) { everySecond(); } //…

Hell, I did that for a prototype product once. Combined it with a state machine and wrote the entire thing to be nonblocking. Worked great.

Really appreciate you sharing this - I used to downplay the real-world application of these kinds of methods ("if you were making a real product you'd have a priority queue and a scheduler and so on") so if I ever teach kids again I'll let them know that people out there build real things like this.

Re: Introducing multitasking to Arduino

#88

A couple years ago, I managed a summer/afterschool program that introduced thousands of kids (age 8-18) to the Arduino. When we would set up a software abstraction to simplify multitasking (something that came up often with more advanced students), we set up something like this: int time = 0; int max_period = 10000; void loop() { if (time % 100 == 0) { everyTenthSecond(); } if (time % 1000 == 0) { everySecond(); } //…

http://www.calgaryfieros.com/OSGdocs/ECM-hacking.html The GM engineers used a similar approach: > The ECM hardware generates a periodic interrupt at 160 Hz. (Where have we seen that number before?) The entire ECM program operates off of this one interrupt. Except for some initialization code, there is no non-interrupt level code. In fact, the interrupt service routine never returns, it simply cleans the return addres…

This is awesome, thank you (and to the other HN commenters) for providing so much valuable perspective as always.
Post reply on HN