1

I've been playing around with Arduino and understand that the standard runtime provides a single thread essentially.

I was wondering if it is possible to write a thin layer (library) that allows one to write code that is non blocking and one where each task yields control manually which allows the master loop to implement some form of timesharing.

I'd appreciate any:

  1. Comments on existing frameworks that can allow me to write my programs this way
  2. Any comments on how hard implementing anything of this order of magnitude would be.
2
  • 2
    Similar to arduino.stackexchange.com/questions/286/…
    – Jake C
    Aug 4, 2015 at 23:36
  • 1
    You are vaguely describing an RTOS, which while there are some out there that target Arduino, (IMHO) they aren't very practical except in a few narrow use cases.
    – Jake C
    Aug 4, 2015 at 23:41

2 Answers 2

2

I would recommend NilRTOS. I like the fact that it's really minimal. However, you might find that some libraries (which you would like to use) work in blocking mode and cannot be used with this RTOS, because they might do bad things (polling) or worse things (polling in interrupt context with other interrupts disabled). But that will be the case regardless of the thread framework you choose.

You might want to try also femtoos. I think that the code could be organized far better, which is one of the reasons I preferred NilRTOS.

2

My recommendation would be to simply code without needing blocking. I have examples here.

Petri Häkkinen coded a game - Toorum's Quest - without using any real-time library. That game (on a Uno) has multiple sprites, TV output and sound effects, all interleaved.

The basic technique is to know that you have multiple things you need to have done, from time to time, and check in the main loop if it is time to do one of them.

One issue with any sort of real-time library is, you only have limited resources. You need to be really aware, for example, of which bits of hardware are doing what (eg. timers, SPI, I2C, interrupt handlers, serial comms).

Even existing libraries suffer from the problem that one library may require (say) pin-change interrupts, and "requisition" the interrupt handlers needed for that, and another library also uses pin-change interrupts, and they cannot be linked together.

6
  • I do not see how the RTOS is a problem in the situation described. It's actually sort of typical (and, frankly, very annoying/disappointing) that 2 or more libraries contend the use of the same timer. I'd rather patch them, drop the meddling with the timer and let the RTOS call them when appropriate. If anything, this shows a shortcoming of the Arduino libraries, where there is no standardized way to do timed stuff and the author of each library is let alone to figure out what HW timer to use. Aug 5, 2015 at 0:30
  • It's not a problem that has an easy solution. Things like pin-change interrupts, for example, used by SoftwareSerial. Once you add one of the interrupt handlers you are committed to handling 8 pins. And how do you handle when one library wants timer 2 for timing an event, and another one wants it to do PWM?
    – Nick Gammon
    Aug 5, 2015 at 1:11
  • As I said: let the RTOS replace the direct use of the timer. If a library just needs a periodic signal, most likely the RTOS can replace the timer handler with a thread. Same goes for PWM. Of course it depends on the time resolution and precision required. But I think in most cases it would be sufficiently accurate. Software Serial is something beyond redemption, as it busy loops in interrupt context. It's ok for a simple project, but in a serious application, I would definitely not use it, as the delay is not even fixed, but depends on the baud rate. Aug 5, 2015 at 1:19
  • For the pin change interrupt, I think that's another short coming of the standard Arduino library. IMHO there should be a default, standard, way of running timed tasks and to get the OS to handle inputs/outputs, like providing the first/last time an input line changed or generate a pwm by sw on an arbitrary pin. I moved away from the Arduino standard libraries after noticing that thy make it harder to do apparently simple tasks. Aug 5, 2015 at 1:26
  • That horse has probably well and truly left the stable by now. A lot of libraries assume you are only going to use their library (or at least not another one with conflicting requirements). To have imposed a RTOS during the early days of design would probably have been too hard, and now it is too late. The other issue is memory. To have multiple threads you need multiple stacks, and probably multiple heaps. It's going to get rather hard to manage all this when you only have 2 KB of RAM to start with.
    – Nick Gammon
    Aug 5, 2015 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.