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