about.me

Goodreads

Cross-compiling a Pie ! The Raspberry Pi Ultimate Guide

During the last few weeks I was interested in cross-compiling for the RPi. I have gone through many tutorials and blog posts to get the task done , so I tried to compile as many information as I can in this guide as a future reference and to help everybody else [The post is long, use CTRL+F for navigation].

Topics:

Getting started , setting up the cross-compilation toolchain.

Hello World! Building an RPi C application using shell.

Hello World 2! Building an RPi C application using eclipse.

Remote debugging the RPi with gdb on eclipse + RSE plugin.

Updating your kernel. RPi Kernel cross-compilation.

Extra: How does the bootloader work ??

Let’s get started with cross-compiling a Pie , there are usually 2 configurations :

either to build the tool chain yourself on your host machine;

or to get a pre-compiled toolchain (the Canadian cross).

[images from free-electrons embedded training slides]

For this guide I will be using pre-built linaro toolchain(the second way),still I encourage you to try to build the toolchain yourself for the sake of knowledge using one of those excellent tutorials : A and B

It’s important to be able to debug your code online on the RPi, so we are going to use a nice plugin called RSE to do this [source]:

create a new project with the above source, build it in debug mode (not release);

add a new connection to your RPi from the Remote Systems window (if you can’t see it , add it from Window >> Show View >> Others >> Remote Systems) , then hit the tiny button to add an ssh connection to your pi;

copy the elf executable to the pi using RSE (right click to copy it and paste it on the dir you wish from the stfp menu);

make sure to chmod+x the execultable;

now we will add a new debug configuration , right click on the project and select Debug as >> Debug Configurations and add a new C/C++ Remote Application configuration;

ensure that in Debugger >> Main tab the debugger is arm-linux-gnueabihf-gdb

back to eclipse , ensure that you have the right connection parameters in Debugger >> Connection tab where you should pick the RPi IP and the port number you previously picked.

That’s it !! click Debug and enjoy debugging🙂

Now , there is an excellent tutorial for compiling the kernel [this is not my work, I am just copying it here for future reference because it’s short and to the point ]also elinux wiki has a good compilation:

When the Raspberry Pi is first turned on, the ARM core is off, and the GPU core is on. At this point the SDRAM is disabled.

The GPU starts executing the first stage bootloader, which is stored in ROM on the SoC. The first stage bootloader reads the SD card, and loads the second stage bootloader (bootcode.bin) into the L2 cache, and runs it.

bootcode.bin enables SDRAM, and reads the third stage bootloader (loader.bin) from the SD card into RAM, and runs it.

loader.bin reads the GPU firmware (start.elf).

start.elf reads config.txt, cmdline.txt and kernel.img

loader.bin doesn’t do much. It can handle .elf files, and so is needed to load start.elf at the top of memory (ARM uses SDRAM from address zero). There is a plan to add elf loading support to bootcode.bin, which would make loader.bin unnecessary, but it’s a low priority (I guess it might save you 100ms on boot).