TIP: Click on subject to list as thread! ANSI
echo: rberrypi
to: FOLDEROL
from: COMPUTER NERD KEV
date: 2021-01-08 23:38:00
subject: Re: RT Kernel

Folderol  wrote:
> Does anyone know if it is reasonably easy to get an RT kernel running on the
Pi?
> I'm using devuan beowulf to get a small footprint install *without* either
> systemd or pulseaudio.
>
> Actually, this is already better than the official OS when running purely
with
> ALSA.

You don't say what you're actually doing. I gather that it's some
software audio synth, and I don't know anything about that
application. I'm guessing it's probably existing Linux software
though, so suggestions such as building for bare-metal or another
OS are presumably not worthwhile.

There's a lot of info here, though not Pi-specific. Setting CPU
frequency scaling to "performance" was important for my application
(I also needed to disable video output on HDMI/Composite):
https://wiki.linuxaudio.org/wiki/system_configuration

Real-Time patches for the Linux Kernel:
PREEMPT_RT: https://wiki.linuxfoundation.org/realtime/start
Xenomai
RTAI

There is a PREEMPT_RT patched Linux distro for the Pi (this one
worked alright for me on a Pi Zero and seems to be fairly actively
developed, there might be others that I've forgotten about):
https://guysoft.wordpress.com/2017/10/09/realtimepi/

When I say "worked alright", I mean that it booted and probably
did everything that could be expected from it. Fiddling with the
scheduler settings and process priority still wasn't enough to
prevent _any_ task switching by the scheduler during the
timing-critical part of the code I was trying to run. Either
PREEMPT_RT or one of the other projects had some special calls
that controlled the kernel at a deeper level from user-space, but
the documentation was too light for me to really figure it out.

Projects such as LinuxCNC are possibly optimised for the real-time
Linux kernel in more advanced ways that I couldn't figure out myself.

In Debian/Devuan it may be possible to install a pre-built Real-Time
patched kernel as a package, depending on which Pi model you're using
(more details in these questions please!). Based on this page:
https://wiki.debian.org/RaspberryPi

This should work on the Pi3:
https://packages.debian.org/buster/kernel/linux-image-4.19.0-8-rt-arm64

And this with the Pi2:
https://packages.debian.org/buster/kernel/linux-image-4.19.0-12-rt-armmp

It doesn't look like there's a PREEMPT_RT armel package to suit the
Pi 1 and Pi Zero. I'm not sure which Debian arch. the Pi4 is
compatible with (arm64, like the Pi3?).

[you can probably ignore the rest if you are trying to run audio
 software, because I doubt it will work]

It's also possible on the Pi to disable all the system interrupts
from user-space (newest versions of the example code are later in
the thread):
https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=52393

I used a variation of that, where before disbling the interrupts
and starting the protected bit of code, I checked that there weren't
any "tick" interrupts due before it would be finished. That at the
very least keeps the system clock accurate, if not also preventing
other problems.

RPi OS's default dwc_otg USB driver written by the Pi Foundation
crashes when the FIQ (Fast Interrupt) is disabled while USB device
is connected (testing on a Pi Zero). At one point there was an
undocumented setting to tell it not to use FIQ, but it doesn't seem
to work anymore. I'm using the alternative dwc2 driver (an "official"
Linux one) which doesn't have that problem. You configure that by
adding this line to /boot/config.txt:
dtoverlay=dwc2,dr_mode=otg

Note that disabling interrupts is probably only a good idea if your
program is doing everything you need by itself at that time. I don't
imagine that it would work if you're outputting audio through ALSA,
or writing to a file, or anything else "Linuxy" at the same time.

The "proper" way to disable interrupts is within a linux kernel
driver module, using function calls such as local_irq_disable. But
you're probably not going to want to split this program you're trying
to run into one piece that runs in user-space and another that runs
in kernel-space. I've been trying to do that for my application, and
as of yesterday have given up for the moment.

--
__          __
#_ < |\| |< _#

--- SoupGate-Win32 v1.05
* Origin: Agency HUB, Dunedin - New Zealand | FidoUsenet Gateway (3:770/3)

SOURCE: echomail via QWK@docsplace.org

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.