Saturday 19 January 2013

plugging in the pi

One of my Christmas gifts was a raspberry pi - it's a little single chip computer which costs about 30 pounds. It's aimed at encouraging school kids to start programming and so comes with a complete python programming environment. It also has an external connector which can be used to control external devices such as stepper motors.
I'm interested in compiling some C++ apps for it and I'll keep my notes on the process here. It's mainly for my benefit as I will only get a few hours here and there to play with it and without a log I will likely forget what I did the previous time.

Setup

Nothing special. Download Raspian wheezy http://www.raspberrypi.org/downloads and copy it onto a SD card. Pop it into the pi, connect a keyboard, display and ethernet connection and power it up. In the config menu, make sure ssh is enabled as we'll be mainly accessing via a shell from another computer.
The pi gets an IP address automatically using DHCP - generally that won't change too often, but I would prefer it not to change at all. To ensure that is the case, just use the DHCP settings in the router admin page and add a reservation for the pi. That means a given address will be reserved for a given MAC address.

cross-compiler setup

The pi is a fully fledged linux environment and is capable of compiling c++ code ... but it's not a very fast device and the access to the SD card is quite slow so compiling anything of note is likely to be painful. The answer is to compile the code on a more powerful PC and then transfer the resulting binaries to the pi. This is called cross compiling because the compiled binaries will not run on the system that compiled them (in this case, the binaries will be arm6 machine code whilst the PC runs Intel x86 code.)
My main machine runs windows 8 ... but setting up a cross compiling environment under windows is a pain, so instead I chose to set-up a lubuntu linux system running under vmware player. I gave the VM 2Gb RAM and a 20Gb disk which seems plenty. The host is an intel i7 with 24Gb RAM, so it can easily run this vm. I chose lubuntu because it needs less resources than fully fledged ubuntu and so the same VM should run happily on my laptop too.

The good thing about using linux on a PC as a host cross-compile system is that somebody else has done the hard work of making a compiler that runs on an x86 PC platform but generates code to run on an ARM6 platform. This is stored in a git repository, so we need to install git and then clone the repository.

First, from the command line (LXTerminal under Applications) , install git by:

sudo apt-get install git
once this is complete, we're going to make a directory called rpi and get the tools into that directory. Use the following commands

mkdir rpi 
cd rpi
git clone git://github.com/raspberrypi/tools.git
A quick sanity check that this has worked is to run the following:

./tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gcc --version

which should produce output that looks like this:


arm-linux-gnueabihf-gcc (crosstool-NG linaro-1.13.1+bzr2458 - Linaro GCC 2012.08) 4.7.2 20120731 (prerelease)
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

That's just running the cross compiler and asking it to display its version. If you get the above result then we're ready to go.

Simple hello world test

We should test the compiler works using the simplest program that we can and it would be rude not to run a time honoured hello world test. I'm going to make a directory called src in the rpi directory which will contain all my projects, so first I will make that directory plus one for hello world.

mkdir src
cd src
mkdir helloworld
cd helloworld

now, I'm going to open a text editor (I use vi but everyone has their favourite) and create the file helloworld.cpp with the following content:

#include <stdio.h>

int main(int argc, char* argv[])
{
    printf("hello world\n");
    return 0;
}

Now, use the following command to compile the file using the cross compiler:
../../tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++ helloworld.cpp

This will result in a file named a.out being created.

Transfer via ssh/scp

We need to copy the file over to the rpi. We could do this by copying it onto the SD card but copying via ssh is much easier. First ssh to the pi in a new console window and accept the identity. My pi is at address 192.168.0.114, yours will be different. Use the following command (with the ip address of your pi of course):
ssh pi@192.168.0.114
by default the password is raspberry.

Now, back in you original console window, use the following command to transfer the a.out file to the pi (again, change the ip address):
scp a.out pi@192.168.0.114:a.out

Now, on the pi, execute the program with
./a.out

and if all has gone well, you should see 'hello world' printed on the screen. For reference, the output from my terminal is as follows.

pi@raspberrypi ~ $ ./a.out

hello world
pi@raspberrypi ~ $ 


No comments:

Post a Comment