Wednesday, January 6, 2016

Real-Time Kernel for RaspberryPi 2

This post will describe how to compile a RT-kernel for RaspberryPi 2.
I'm using UBUNTU 14.04 LTS 64bit for compiling Raspbian Jessie.
After a long time, I needed a RT kernel for a new project with the new RaspberryPi2. Realizing My old post has broken links, I struggled again with the task. This time, after a successful attempt, I'm determined to put it all here step by step.

##### Tested on UBUNTU 14.04 LTS with Rasbian Jessie 4.1
# install needed packages for compiling the kernel
sudo apt-get install libncurses5-dev libncursesw5-dev
#Download Raspberry Pi tools:
git clone https://github.com/raspberrypi/tools.git
# Download source files and patches
git clone -b rpi-4.1.y --depth 1 git://github.com/raspberrypi/linux.git linux-rpi-4.1.y
# Download RT patch 
wget https://www.kernel.org/pub/linux/kernel/projects/rt/4.1/patch-4.1.15-rt17.patch.xz
xz -d patch-4.1.15-rt17.patch.xz
# patch the kernel. This step might produce errors
# if rt-patch and the kernel don't match
cd linux-rpi-4.1.y
cat ../patch-4.1.15-rt17.patch | patch -p1
#Export the following variables to specify cross-compilation options:
export ARCH=arm 
#to compile on a x64-machine you need to export:
export CROSS_COMPILE=~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-
#create a config file for Raspberry Pi 2:
make CROSS_COMPILE=${CROSS_COMPILE} ARCH=arm bcm2709_defconfig
# make changes to the config
make CROSS_COMPILE=${CROSS_COMPILE} ARCH=arm menuconfig
enable Preemption model -> Fully preemptible Kernel (RT)
change timer freq to 1000Hz
enable device tree (all options)
#Compile the kernel:
make CROSS_COMPILE=${CROSS_COMPILE} ARCH=arm -j5
#Install modules, will result in "lib" folder with modules and firmware:
mkdir kernel-rt
INSTALL_MOD_PATH=kernel-rt make CROSS_COMPILE=${CROSS_COMPILE} ARCH=arm modules_install
## Install the kernel to non-RT image:
Copy arch/arm/boot/Image to /boot/kernel7.img on SD card.
Copy arch/arm/boot/dts/bcm2709-rpi-2-b.dtb to /boot/bcm2709-rpi-2-b.dtb on SD card
# add to /boot/config.txt
echo “device_tree=bcm2709-rpi-2-b.dtb” >> /boot/config.txt
Copy (merge if necessary) kernel-rt/lib to / on SD card.

## Testing RT-Image
Insert SD card and connect RPI to power, keyboard and screen. 
Login with user pi and password raspberry
#Installing cyclictest utility on Raspberry Pi:
git clone http://git.kernel.org/pub/scm/linux/kernel/git/clrkwllms/rt-tests.git 
cd rt-tests
make all
cp ./cyclictest /usr/bin/
cd ~
sudo cyclictest -l100000 -m -n -a0 -t1 -p99 -i400 -h400 -q

# WITH NO RT RESULT:
# Total: 000100000
# Min Latencies: 00015
# Avg Latencies: 00021
# Max Latencies: 00118
# Histogram Overflows: 00000
# Histogram Overflow at cycle number:
# Thread 0:

# WITH RT RESULT:
# Total: 000100000
# Min Latencies: 00013
# Avg Latencies: 00020
# Max Latencies: 00066
# Histogram Overflows: 00000
# Histogram Overflow at cycle number:
# Thread 0: