March 24, 2016

Real Time kernel patching and cross compilation

Filed under: Raspberry 3.14, linux — Tags: , , , , , , — admin @ 4:03 pm

This is a log of the recompilation of the latest available branch 4.1 for the raspberry pi, with the RT patch applied.

The target machine is a raspberry pi, with actual kernel : 4.1.13+ #826 PREEMPT.

I assume the  ‘raspberrypi_target’ directory exists. (see an old post about kernel cross compilation..)

So , you have to update the kernel sources before doing the patching.

to avoid errors while fetching the new sources, from the raspberrypi_target/linux directory, you can split the fetching of the new files like this:
for m in `seq 1 100`;do git fetch –depth=$m;done

once done,switch to the latest stable branch 'rpi-4.1.y' (at the time i write this post) :

git checkout -b rpi-4.1.y

Now its time to get the patch.

I assume you have a complete linux, branch 4.1.y … at the time i’m writing, the working patch for this version is located at :


perform a save of your original,clean directory :

from the “raspberrypi_target”  dir :

cp -a linux linux_before_patching

so, let’s get the patch:

wget https://www.kernel.org/pub/linux/kernel/projects/rt/4.1/patch-4.1.19-rt22.patch.gz

..And check for any error (do it!) before patching (option –dry-run performs a ‘blank’ patching with no file modifications ) :

from the linux dir where the patch .gz is :

zcat patch-4.1.19-rt22.patch.gz | patch -p1 –dry-run | grep FAIL

if the command returns nothing, everything is OK, now effectively apply the patch :

zcat patch-4.1.19-rt22.patch.gz | patch -p1

okay, now you have to get some base config for the configuration menu.

The simple way : use the raspberry pi connected to your network, and issue a : sudo modprobe configs

then download the config.gz file from the raspberry to your cross-compiler’s machine :

scp <username>@<ip.ip.ip.ip>:/proc/config.gz ./

now you should have the config.gz file into the /linux/ folder where you issued this command (on the compiling machine )

now deflate it :

zcat config.gz > .config

And be a little curious ! (ie :   cat .config )

Okay, time to launch the menuconfig :

make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- menuconfig

Now, the menu is on the screen. have to activate your options and customize, Then

make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabihf- -k -j5

and voila(oups ..no ..), an error throws :

HOSTCC  scripts/genksyms/lex.lex.o
arch/arm/kernel/asm-offsets.c:54:2: error: #error Your compiler is too buggy; it is known to miscompile kernels

#error Your compiler is too buggy; it is known to miscompile kernels

arch/arm/kernel/asm-offsets.c:55:2: error: #error and result in filesystem corruption and oopses.
#error and result in filesystem corruption and oopses.
make[1]: *** [arch/arm/kernel/asm-offsets.s] Error 1
make[1]: Target `__build’ not remade because of errors.
make: *** [prepare0] Error 2
HOSTCC  scripts/dtc/data.o
HOSTCC  scripts/kallsyms


Okay, lets get another cross compiler, form the github’s “raspberrypi/tools” dir :

cd to the home for your user name ( cd ~ )

then git clone https://github.com/raspberrypi/tools.git

… time to drink a cofee…

once done, the ‘tools’ folder is there.

okay, lets try rebuilding the menuconfig, this time using linaro gnueabi , from the tools.
the kernel modules will go to the ‘rtkern’ folder ..(create it : “mkdir rtkern” from your home folder )

make ARCH=arm CROSS_COMPILE=~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf- INSTALL_MOD_PATH=~/rtkern

Once again,errors, on my machine : missing some “libstdc++.so.6″ libraries, fix it ( we need the 6th version)

Okay, got it.. i used the 32bit version but i’m on a 64bit machine with ubuntu , so :

first, lets add the path, arch and destination for modules to the system :

export ARCH=arm
export INSTALL_MOD_PATH=~/rtkern
export CROSS_COMPILE=~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-

notice i specified the -x64 version of the crosscompiler.
now, from the ‘linux’ directory, try again the menuconfig, it should work fine now :

make menuconfig

>> it’s time to activate the options we want, in the menu. for the RealTime aspects, you’ll have to activate ‘KERNEL PREEMPT FULL’ , and also the HIgh-Resolution timers .

then after customisation and save+exit , in the ‘linux’ folder, execute :


make modules

make modules_install” <- the module are installed in the folder specified by the ‘INSTALL_MOD_PATH’ variable, obviously

now “cd ~/tools/mkimage

check out your built image , it should be listed when doing :

ls ~/raspberrypi_target/linux/arch/arm/boot -al

notice there is Image and zImage that have been created.

first method :

  • type "./imagetool-uncompressed.py ../../raspberrypi_target/linux/arch/arm/boot/Image"
  • the created boot image is in the current folder, named ‘kernel.img
  • now you have to replace the files into the target pi ,  from ~/raspberrypi_target/linux/arch/arm/boot/kernel.img into the /boot/ folder
    ( NOTICE : the /boot/ folder depends on the image you installed on your SD card. beware in case of a ‘noobs’ image’ , because in this case , the right ‘/boot’ folder is not at the regular place )
  • and rename/erase the target pi’s /lib/modules and /lib/firmware with the one freshly compiled,
Second method :
  • Use the mkknimg utility , located in the same folder. this one generates a kernel image from the zImage source, and the generated kernel.img file is configured to make use the of the device tree overlay (DTS) , then copy the created file to the /boot folder.
  • Remove / rename the target pi’s /boot/overlays folder by the one contained into ~/raspberrypi_target/linux/arch/arm/boot/dts/overlays

Note : more infos about the DTS , DTB files, syntax and examples here :


and for the tool to convert from .DTS to .DTB (bin) :

sudo apt-get install device-tree-compiler

.. the source of mkknlimg tool here :


once done, do not forget to “sync” to ensure the data are written onto the SD.

then plug the fresh thing into the raspberry en enjoy.

uname -a gives a “Linux raspberrypi 4.1.20-rt22 #1 PREEMPT RT


August 1, 2013

Raspberry pi : A LCD display hardware and software example

Following the previous article about the linux kernel module’s compilation, this post will expose a concrete example of hardware wiring to the Pi GPIOs , and also cover the software aspect, with the implementation of a kernel module who drives the hardware.

The basic idea, and schematic of the hardware, is here:

The Pi GPIO ports, P0 and P5 ( rev2 , raspi version) are visible.
Some GPIO outputs/inputs pins are connected to a 74hc574 latch , wich, in turn, is connected to the LCD display ( industry standard, HD44780 based display).

It’s not obvious, why there is a 74hc574 between the Pi and the LCD display. but, in fact, this ‘574 Latch is freeing 6 pins of the GPIO when the LCD does not have to be adressed. The only pin permanently occupied is the latch clock pin, wich latches the datas to THIS latch (adressing-like, on a microproc. bus). a little bit thinking will let you imagine that the latch enables a BUS-like architecture (more on this later).

The uggly hardware proto board, as usual

The 4x20 LCD display used for my proto

Well the second thing one can notice, is that the LCD display is used in 4-bit mode. nothing new here. 4 bit mode data bus is ‘GPIO-pin-efficient’ solution.
The third thing is that 2 bits of the latch remains unused ( free for other ouputs, your taste… )

I know there’s plenty of LCD connected to raspberry pi everywhere on the internet.latched LCD’s ? there must be less . latched LCD’s with complete kernel module in C code ? there should be lesser again.

A test display text

a capture showing the console in linux : INSMOD of the module, then dump of the kernel messages queue, then echo the test TEXT to /dev/mynull

that’s why this post is here : dive into that code, and help you understand what i’ve understood by diggin and trying a lot of things.
Like for the past post about kernel module’s build under linux, i’ll go straightforward to the essential.It’s up to you to dig into the links i give, and google for words or concepts you dont understand.
- And dont forget i’m a linux newbie -

coming after my hollidays : The why of a linux module : the char device, ioctl accesses and so on…


July 30, 2013

Creating a linux device driver for the raspberry PI

first (i had hard times figuring out what missed me for correct compilation of a driver module ) ,try to Install the module-assistant for debian (i assume you’re logged in as ROOT ):
$ apt-get install module-assistant
$ m-a prepare(trys to get linux-headers )

At this epoch, you get an error : cant locate 3.6.11+ linux headers ( if you run the same version as mine is ..)
in doubt, you can check your linux version :

$ uname -r

thats bullshit.. GPL, linux, and so on… looks like it’s an adventure to get something going straight easy on the linux/raspberry Pi world.. :(
OK, lets get the linux headers (wget) from here : linux-headers-3611_3611-2_armhf (huge - i eihis.Com hosts it )
then, unpack it :

$ dpkg -i linux-headers-3.6.11+_3.6.11+-2_armhf.deb

At this point, the 3.6.11+ headers (tree) should be found here : /usr/src/linux-headers-3.6.11+
So, now let’s create a link to it for Kernel’s MAKE , after removing existing (bad) ones directories links :

$ rm -r /lib/modules/3.6.11+/build
$ rm -r /lib/modules/3.6.11+/src

[may be present. in my case, it was, but i tried many things before so, anyway, in a doubt , do it ]
Now, actually create the links :

$ ln -s /usr/src/linux-headers-3.6.11+ /lib/modules/3.6.11+/build
$ ln -s /usr/src/linux-headers-3.6.11+ /lib/modules/3.6.11+/src

now time to reboot ..

$ reboot

upon new login , CD to the directory of the module you want to compile, then ‘make’ to execute the Makefile.( beware of the Makefile name : case sensitive ! )
now, in the directory where your module.c code was, you get a module.ko file. thats the kernel module file created.
the Makefile should look like this :

obj-m :=yourmodule.o
KDIR :=/lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
[tab here] $(MAKE) -C $(KDIR) M=$(PWD) modules

where :
obj-m is the name of the object you want to build. the corresponding object.c file will be found by the MAKE command…
KDIR is the build directory (where parameters for kernel build,headers, and so on are located )
PWD is the destination (output .ko file) directory ( the current directory where your makefile and yourmodule.c file are located, for instance )

in case of problem, you can start by checking if the links have been created, actually.
Doing a :

$ ls -al /lib/modules/3.6.11+/build

should return :

/lib/modules/3.6.11+/build -> /usr/src/linux-headers-3.6.11+

If everything has run, you should now have a yourmodule.ko file into the current projects directory.
wich prove that the link is existing…
If everything has run, you should now have a yourmodule.ko file into the current projects directory.
you can test it by doing :

$ insmod yourmodule.ko

And stop it by doing :

$ rmmod yourmodule

I guess you now what to put in your ‘yourmodule.c’ file. hello world program examples can be found at many places dealing with linux driver’s compilation how-to websites.
This information post uses many sources (not listed) where i’ve found, finally, the way to compile drivers with no errors on the Raspberry Pi RASPBIAN , linux version 3.6.11+.
Hope this helps !

portions of code, with adaptations ,updates, modifications, and also Informations are coming from :

http://www.tldp.org/LDP/lkmpg/2.6/lkmpg.pdf (The linux kernel Module programming guide )

http://www.linuxforu.com/2011/04/character-device-files-creation-operations/ ( full of usefull articles )

http://gauss.ececs.uc.edu/Courses/c4029/lectures/work_queue.pdf ( work queues in Linux )


cat{ } { post_423 } { } 2009-2015 EIhIS Powered by WordPress