Hi Andy,
I didn't witness the discussion in the space so I'm not sure exactly
what was discussed, hopefully these comments aren't too off-base.
On Sun, Jun 01, 2014 at 07:02:07PM +1000, Andy Gelme wrote:
> A simple way to start without having to write a kernel module is to use
> "user level interrupts", where a user process can register a function to
> handle a hardware interrupt ...
From the point of view of an interrupt-oriented coding style, I
totally agree with this approach. However, from the point of view of
interrupts providing timing and latency improvements there are some
traps.
I think the specific User Level Interrupt API functionality mentioned
was only ever a part of SGI's REACT real-time linux. :(
Looking in the WiringPi source reveals that registering a WiringPi
interrupt spawns a thread which opens the gpio's sysfs file entry (ie
/sys/class/gpio/gpioXX) and then calls poll() on it, to block and wait
for it to change.
https://git.drogon.net/?p=wiringPi;a=blob;f=wiringPi/wiringPi.c;h=4660a67b47b2c21a9a60a2f8548d6f8b4d2548b4;hb=HEAD#l1259
The difference from SGI's User Level Interrupt is that all user code
(and WiringPi code) runs in the user process. On the kernel side the
gpio interrupt triggers an update from an ISR, but then the Linux
scheduler must schedule the WiringPi process so it can wake up and
respond. Relative to each other, these two operations aren't
guaranteed to fit within any particular time window.
I think the advantage to WiringPi interrupts is to write code that's
more readable written when in an interrupt handler style, and to avoid
burning CPU cycles busy-polling a pin.
> The user-level interrupt approach will work in most cases, where very
> low-latency is not a requirement, i.e you don't mind up-to 75
> microseconds latency.
Absolutely. Though I'd be interested to know how the wiringPi users
measured 75 microseconds of latency. From my experience I'd expect
this to be median latency on a relatively quiet system, not the worst
case latency they claim.
I gave a talk at the LCA2014 Arduino MiniConf "Arduino vs Raspberry Pi
- Decisions, Decisions"
The slides are here:
http://projectgus.com/talks/arduino_vs_pi/
... but they're not super intuitive by themselves unfortunately (I
meant to turn them into a blog post but haven't finished it yet):
The DSO captures in the slides show measurements of jitter & worst
case latency in Raspbian. Even with some basic tweaks (like setting
real-time process priority), regular 10 millisecond delays were as
good as it got in those tests.
There are some other more involved tweaks people have used (including
porting the Linux realtime patchset to Raspberry Pi.) Back in January
I think that project was pretty experimental, but things might have
matured since then.
Despite all that, I know some people have had good luck using WiringPi
and interrupts for fairly low latency stuff. For example a Freetronics
customer ported the IRTemp Arduino library that you wrote to WiringPi
(and that clock runs at a fairly high speed):
https://gist.github.com/unprolix/7298736
... and this works, however my guess is it only works most of the
time. Especially if the system becomes loaded up with other processes,
USB or network traffic, etc. then it may miss samples or suddenly stop
working entirely. It works well enough for whatever the author had
in mind, though, so that's good.
> If you need to and are willing to get your hands dirty, then writing a
> Linux kernel module, a hardware interrupt handler and asynchronously
> notify (signal) your user process may be the way to go.
Agreed! It's a shame there's no easier way to get that kind of
performance. :(
Another solution, which I'm sure you discussed in the space as well,
is to combine the complex, non-real-time, system running on the
Raspberry Pi with an attached smaller, simpler, real-time system like
an Arduino. This way each part can deal with the aspect of the problem
that it's best suited for.
This is probably my preferred approach in my own projects, I've found
the other approach quickly develops a feeling of "round peg jammed
into square hole". :)
Cheers,
Angus