64bit RT Kernel Compilation for Raspberry Pi 4B .

Introduction: 64bit RT Kernel Compilation for Raspberry Pi 4B .

This tutorial will cover process of building and installation of 64 bit Real Time Kernel on Raspberry Pi. RT Kernel is crucial for full functionality of ROS2 and other Real Time IOT solutions.

Kernel was installed on x64 based Raspbian which can be obtained here

Note. This tutorial although straightforward requires basic knowledge of Linux operating system.

Also due to limitations of this platform all http links lack h. To fix them simpli add "h" at the front of the link.

Supplies

x64 based PC running Linux

Raspberry Pi 4B with Raspbian 64 already installed

Connection to the Internet.

Step 1: Getting Neccesery Tools

First we need to optain neccecery dev tools.

They can be obtained by executing following commands in Linux terminal

sudo apt-get install build-essential libgmp-dev libmpfr-dev libmpc-dev libisl-dev libncurses5-dev bc git-core bison flex<br>sudo apt-get install libncurses-dev libssl-dev<br>

Step 2: Compilng Native Build Tools for Cross Compilation

Next step is to preapare and compile tools for cross compilation of our kernel.

Firs tool we will be installing is Binutils this tutorial was tested with binutils version 2.35.

cd ~/Downloads<br>wget ttps://ftp.gnu.org/gnu/binutils/binutils-2.35.tar.bz2<br>tar xf binutils-2.35.tar.bz2<br>cd binutils-2.35/<br>./configure --prefix=/opt/aarch64 --target=aarch64-linux-gnu --disable-nls<br>

After configuration finishes we need to compile programm using following commands

make -jx
sudo make install

where -jx means how many jobs you want to run i parrarell. Rule of thumb is to put it no higher than amount of threads your system has. (for example make -j16)

and finally we need to export the path

export PATH=$PATH:/opt/aarch64/bin/

Than we proceed with building and installation of GCC

cd ..<br>wget ttps://ftp.gnu.org/gnu/gcc/gcc-8.4.0/gcc-8.4.0.tar.xz<br>tar xf gcc-8.4.0.tar.xz<br>cd gcc-8.4.0/<br>./contrib/download_prerequisites<br>./configure --prefix=/opt/aarch64 --target=aarch64-linux-gnu --with-newlib --without-headers \<br> --disable-nls --disable-shared --disable-threads --disable-libssp --disable-decimal-float \<br> --disable-libquadmath --disable-libvtv --disable-libgomp --disable-libatomic \<br> --enable-languages=c --disable-multilib<br>

Than the same as before we make and install our compiler

 make all-gcc -jx
 sudo make install-gcc

If everything went smoothly following command

/opt/aarch64/bin/aarch64-linux-gnu-gcc -v

should resoult in responce similar to this.

    ux-gnu-gcc -v<br>    Using built-in specs.
    COLLECT_GCC=/opt/aarch64/bin/aarch64-linux-gnu-gcc
    COLLECT_LTO_WRAPPER=/opt/aarch64/libexec/gcc/aarch64-linux-gnu/8.4.0/lto-wrapper
    Target: aarch64-linux-gnu
    Configured with: ./configure --prefix=/opt/aarch64 --target=aarch64-linux-gnu --with-newlib --without-headers --disable-nls --disable-shared --disable-threads --disable-libssp --disable-decimal-float --disable-libquadmath --disable-libvtv --disable-libgomp --disable-libatomic --enable-languages=c --disable-multilib
    Thread model: single
    gcc version 8.4.0 (GCC)

Step 3: Patching Kernel and Configuring Kernel.

Now its time to get our kernel and RT patch.

This tutorial will use rpi kernel v 5.4 and RT patch RT32. This combination worked well for me. However everything should work fine with different versions.

mkdir ~/rpi-kernel

cd ~/rpi-kernel 

git clone ttps://github.com/raspberrypi/linux.git -b rpi-5.4.y

wget ttps://mirrors.edge.kernel.org/pub/linux/kernel/projects/rt/5.4/older/patch-5.4.54-rt32.patch.gz

mkdir kernel-out

cd linux

then unpack the patch.

gzip -cd ../patch-5.4.54-rt32.patch.gz | patch -p1 --verbose

And initialise configuration for Rpi 4B

make O=../kernel-out/ ARCH=arm64 CROSS_COMPILE=/opt/aarch64/bin/aarch64-linux-gnu- bcm2711_defconfig

Afther this we need to enter the menuconfig

make O=../kernel-out/ ARCH=arm64 CROSS_COMPILE=/opt/aarch64/bin/aarch64-linux-gnu- menuconfig

When it lauches we need to existing configuration, then go to

General -> Preemtion Model and select Real Time option.

than we save new configuration and exit the menu.

Step 4: Building RT Kernel

Now its compilation time. Note it may take a long time depending on your PC capabilities.

make -jx O=../kernel-out/ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-

As before -jx means number of jobs. After successful compilation we need to pack our Kernel and send it to Raspberry Pi. To do this we execute following commands.

export INSTALL_MOD_PATH=~/rpi-kernel/rt-kernel
export INSTALL_DTBS_PATH=~/rpi-kernel/rt-kernel
make O=../kernel-out/ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- modules_install dtbs_install
cp ../kernel-out/arch/arm64/boot/Image ../rt-kernel/boot/kernel8.img
cd $INSTALL_MOD_PATH
tar czf ../rt-kernel.tgz *
cd ..

Now our kernel should be inside rt-kernel.tgz archive and its ready to be sent and installed.

Step 5: Installing New Kernel

The easiest way of sending our kernel to raspbperry is by using scp .

We simply execute follwing command.

scp rt-kernel.tgz pi@<ipaddress>:/tmp

Now wee need to login to our pi via ssh and unpack our Kernel.

ssh pi@<ipaddress>

When logged in we copy our files using following commands.

cd /tmp
tar xzf rt-kernel.tgz cd boot sudo cp -rd * /boot/ cd ../lib sudo cp -dr * /lib/ cd ../overlays sudo cp -dr * /boot/overlays cd ../broadcom sudo cp -dr bcm* /boot/

After that what is left to do is to edit /boot/config.txt file and add following line.

kernel=kernel8.img

After rebooting pi everything should be working fine.

To check if new kernel was installed succesfully you can execute

uname -a

command


Be the First to Share

    Recommendations

    • Back to School: Student Design Challenge

      Back to School: Student Design Challenge
    • Plastic Challenge

      Plastic Challenge
    • Robots Contest

      Robots Contest

    11 Comments

    0
    alb65
    alb65

    6 months ago

    i'm trying to do this on a pi3b+ but i got black screen and activity led always on.

    I replaced bcm2711_defconfig by bcmrpi3_defconfig

    compilations are ok but can't boot :(

    any idea ?

    0
    poerob
    poerob

    Question 1 year ago

    Hi menubis!
    Thank you for your detailed tutorial. I tried it multiple times, but the outcome is always that my PI 4b freezes with a black screen after showing the "Raspberry Pi OS Welcome Logo". Patching and Compilation work without any Errors. Do you may have an idea what goes wrong here and how to fix it? Thanks in Advance!

    0
    -wiremonkey-
    -wiremonkey-

    Question 1 year ago on Step 3

    Hello, I'm a bit stuck and would love some help. I've come so far!
    In step 3, the 5.4 information is no longer working. So I found that 5.9 is the latest version. I changed the lines to:
    git clone ttps://github.com/raspberrypi/linux.git -b rpi-5.9.y
    (Good!)
    wget https://mirrors.edge.kernel.org/pub/linux/kernel/...
    (Good!)
    mkdir kernel-out
    cd linux/
    gzip -cd ../patch-5.9-rc5-rt7.patch.gz | patch -p1 --verbose
    (This is where it breaks. It gives the following message:)
    Hmm... Looks like a unified diff to me...
    can't find file to patch at input line 5
    Perhaps you used the wrong -p or --strip option?
    The text leading up to this was:
    --------------------------
    |diff --git a/Documentation/RCU/checklist.rst b/Documentation/RCU/checklist.rst
    |index 2efed9926c3f9..54a79d03438a0 100644
    |--- a/Documentation/RCU/checklist.rst
    |+++ b/Documentation/RCU/checklist.rst
    --------------------------
    File to patch:
    ---------------
    Not sure what I've done wrong.

    Any help would be amazing! I am not great with this stuff, good enough to follow your instructions and try a few things, but that about it, so please be explicit in your direction.
    Thank you,
    Tyler

    0
    poerob
    poerob

    Answer 1 year ago

    The Error Message basically means that the patch utility does not where to patch some specific code, so the patch is incompatible to the current kernel version you downloaded. There are multiple rt patches for 5.9 at kernel.org, just try another patch, maybe also from the "older" folder.

    0
    Menubis
    Menubis

    Answer 1 year ago

    edit.
    Ok, so they archived 5.4 patch so the path was no longer valid.
    I've updated the path.
    I haven't tryed with newer 5.9 however i'll make atempt in the near future.

    0
    -wiremonkey-
    -wiremonkey-

    Reply 1 year ago

    Ok great, thanks for this. I'll try again.
    Are you running linuxcnc by any chance?

    0
    Menubis
    Menubis

    Reply 1 year ago

    No, im using rt for robotic aplications (regulators, data gathering loops, etc).

    0
    -wiremonkey-
    -wiremonkey-

    Question 1 year ago on Step 5

    Hmmmm, OK, so I then tried the latest version of 5.4, 5.9 was maybe too ambitious. S
    5.4.61-rt37 and I got past the gzip step. Then I got some errors when running...

    make O=../kernel-out/ ARCH=arm64 CROSS_COMPILE=/opt/aarch64/bin/aarch64-linux-gnu- bcm2711_defconfig
    --------------------------
    scripts/Kconfig.include:36: linker '/opt/aarch64/bin/aarch64-linux-gnu-ld' not found
    make[2]: *** [/home/pi/rpi-kernel/linux/scripts/kconfig/Makefile:90: bcm2711_defconfig] Error 1
    make[1]: *** [/home/pi/rpi-kernel/linux/Makefile:590: bcm2711_defconfig] Error 2
    make[1]: Leaving directory '/home/pi/rpi-kernel/kernel-out'
    make: *** [Makefile:179: sub-make] Error 2

    If anyone has a clue, please steer me in the right direction.
    Thanks!