Cross compiling to Raspberry Pi

Please don’t ask for the reasons, but the goal of today’s action was to get a cross compile environment running on Windows OS in order to compile to my RPi3 running Raspbian “jessie” (Raspbian GNU/Linux 8).

I write this blog post as a shortcut for people that like to do the same: Use the resources and comfortable IDE on a PC and do the code writing here, compile, link and finally transfer the executable binary to the Raspberry Pi and execute it there.

IDE

Downloaded Eclipse CDT as IDE for C/C++ programming.
https://eclipse.org/ide/

Eclipse IDE for C/C++ Developers
Version: Neon.2 Release (4.6.2)

Precompiled GNU Toolchain for Windows32

Linaro provides several precompiled toolchains. It is up to you to select the right one.

It needs to fit the system architecture on the Raspberry Pi and the remote operating system you are running on (Raspian “jessie”).

There is a list of Linaro releases here:
https://releases.linaro.org/archive/13.11/components/toolchain/binaries/

For Windows32 as host/development system, I chose
gcc-linaro-arm-linux-gnueabihf-4.8-2013.11_win32.zip

Download it and extract it on your windows filesystem.

Buildtools

You additionally need build tools like “make”.

This can be downloaded from here:
https://gnuarmeclipse.github.io/windows-build-tools/#download

Put the buildtool’s bin directory into your Windows PATH environment variable to make sure they are found by Eclipse. You possibly may need to restart Eclipse. If “make” ist not found when compiling some source files, then the reason is probably that the path to the buildtools is not configured properly.

Configuring Eclipse CDT

Create a new C project named “hello_world”, choose project type “Empty Project”. Toolchain “Cgross GCC” should be selected.

If you are ask for “Cross compiler prefix”, you enter

arm-linux-gnueabihf-

Note: Don’t forget the hyphen (“-“) at the end.

For “Cross compiler path”, navigate to the “bin” directory of your toolchain installation. In my case, it is

C:\gcc-linaro-arm-linux-gnueabihf-4.8-2013.11_win32\bin

Note: There are two bin directories in the toolchain installation. Make sure to select the one that contains binary executables starting with the filename “prefix arm-linux-gnueabihf-“, e.g. “arm-linux-gnueabihf-gcc.exe”.

Hello World Application

Create a small hello_world.c file.

#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("Hello World.\n");
    exit(EXIT_SUCCESS);
}

Compile it in Eclipse using Project -> Build All, and that’s it (almost).

00:40:57 **** Build of configuration Debug for project hello_world ****
make all 
Building file: ../hello_world.c
Invoking: Cross GCC Compiler
arm-linux-gnueabihf-gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"hello_world.d" -MT"hello_world.o" -o "hello_world.o" "../hello_world.c"
Finished building: ../hello_world.c
 
Building target: hello_world
Invoking: Cross GCC Linker
arm-linux-gnueabihf-gcc  -o "hello_world"  ./hello_world.o   
Finished building target: hello_world
 

00:40:59 Build Finished (took 1s.449ms)

Looks good. You can see that arm-linux-gnueabihf-gcc was both used for compiling and linking.

Transfer binary executable to Raspberry Pi

The binary is located in the project subfolder debug. Transfer it to the remote Raspberry Pi. Eclipse provides a way to build connections to remote systems using New -> Other -> Remote System Explorer. You can use this to transfer files without having to leave the IDE.

On Rasperry Pi, make the file “executable” and execute it:

pi@rpi3:~ $ chmod u+x hello_world
pi@rpi3:~ $ ./hello_world
Hello World.

Works. Fine. Go to bed now.

SPI communication issues with Raspbian “Jessie”

Now that we are in the middle of autumn in Germany, and the daylight gets less and less, I decided to re-activate this little project of mine.

First thing was to update my Raspberry Pi (1) to Raspbian “Jessie”.

First check of my CC1101 Socket Driver Stack – and: Of course, it did not work.

Reason was that with Raspbian “Jessie” the way to enable the SPI kernel driver has changed. You can enable it using the “Advanced options” in the “raspi-config” configuration tool. Check out the description here: http://www.raspberrypi-spy.co.uk/2014/08/enabling-the-spi-interface-on-the-raspberry-pi/

Okay, after a reboot

  • lsmod shows the SPI kernel driver spi_bcm2835
  • The device files /dev/spidev0.0 and /dev/spidev0.1 exist

After that, the CC1101 Socket Driver Stack still did not work. Nothing was received over the air. Doing some analysis revealed that the ioctl calls responsible for the SPI data transfers between RPi and CC1101 failed.

Reason was that Raspbian “Jessie” uses a newer kernel than “Wheezy” and that the main ioctl message structure got a new field named “pad”

After I did take some more care to initialize this message structure in the code, everything worked will again.

And now the bad new: When analyzing the issue, I also killed my cross-compile installation  on Mac OS. It still does not work until now 😦