Friday, March 29, 2013

Running your first RTEMS program on the Raspberry Pi

Running your first RTEMS program on the Raspberry Pi

So now you have RTEMS compiled and installed, what about getting an RTEMS program to actually run on the Pi ?

In case you missed anything:

Introduction to RTEMS on the Raspberry Pi Setting up an RTEMS development environment for the Raspberry Pi Compiling and Installing RTEMS for the Raspberry Pi

Did you notice the enable-tests=samples option in the configure script? It compiled some examples for you. Lets get one ready to run on the Pi:

$ arm-rtems4.11-objcopy -Obinary \
$HOME/development/rtems/bsps/4.11/arm-rtems4.11/raspberrypi/lib/rtems-4.11/tests/ticker.exe kernel.img

Now you can copy the kernel.img file on your SD card and see if it works! Remember to back up kernel.img on your SD card before you copy this.

Currently, the RTEMS port for the Raspberry Pi uses the UART port for it’s console. You will need to get a USB UART cable like the following:

http://www.adafruit.com/products/954

If you are using Ubuntu you can use the “minicom” program to connect to the serial port. If you are using Windows, you can use a program such as: uCon

http://www.umonfw.com/ucon/

However you connect to the serial port, set the serial port to 115k baud and 8N1.

When you boot the Pi with the SD card containing your kernel.img file, you should see output like this:

*** CLOCK TICK TEST ***
TA1  - rtems_clock_get_tod - 09:00:00   12/31/1988
TA2  - rtems_clock_get_tod - 09:00:00   12/31/1988
TA3  - rtems_clock_get_tod - 09:00:00   12/31/1988
TA1  - rtems_clock_get_tod - 09:00:05   12/31/1988
TA1  - rtems_clock_get_tod - 09:00:10   12/31/1988
TA2  - rtems_clock_get_tod - 09:00:10   12/31/1988
TA1  - rtems_clock_get_tod - 09:00:15   12/31/1988
TA3  - rtems_clock_get_tod - 09:00:15   12/31/1988
TA1  - rtems_clock_get_tod - 09:00:20   12/31/1988
TA2  - rtems_clock_get_tod - 09:00:20   12/31/1988
TA1  - rtems_clock_get_tod - 09:00:25   12/31/1988
TA1  - rtems_clock_get_tod - 09:00:30   12/31/1988
TA3  - rtems_clock_get_tod - 09:00:30   12/31/1988
TA2  - rtems_clock_get_tod - 09:00:30   12/31/1988
*** END OF CLOCK TICK TEST ***

The program should take 35 seconds to run.

Next: Building and running an RTEMS Application

Compiling and installing RTEMS for the Raspberry Pi

Compiling and installing RTEMS for the Raspberry Pi

OK, The RTEMS compiler is ready to go on your Ubuntu machine, so it’s time to download and build RTEMS for the Raspberry Pi.

If you have not installed the compiler, then go back and follow that step


Step 1: Download and Prepare RTEMS:

Start by check out the RTEMS head build from the git repository

$ cd $HOME/development/rtems
$ git clone git://git.rtems.org/rtems.git rtems-git

Before RTEMS can be built, the autotools scripts must be generated ( Don’t forget the ./ in front of bootstrap )

$ cd rtems-git
$ ./bootstrap

Now, RTEMS is ready to build for the Pi. When you compile RTEMS, you choose the architecture, Board Support Package (BSP) and a few other options such as POSIX API, networking, etc. When you compile RTEMS in this next step, you are not really compiling something you run on the Pi just yet. You are compiling all of the operating system libraries, drivers, and installing them along with the headers. When you build your own RTEMS application, you will then use these headers and libraries.


Step 2: Configure RTEMS:

$ cd $HOME/development/rtems
$ mkdir build-rtems-rpi
$ cd build-rtems-rpi

To build RTEMS for the Raspberry Pi use the following command:

$ ../rtems-git/configure --target=arm-rtems4.11 \
--enable-rtemsbsp=raspberrypi \
--enable-tests=samples \
--enable-networking \
--enable-posix \
--prefix=$HOME/development/rtems/bsps/4.11

When this step is is complete you should see something like this:

target architecture: arm.
available BSPs: raspberrypi.
'make all' will build the following BSPs: raspberrypi.
other BSPs can be built with 'make RTEMS_BSP="bsp1 bsp2 ..."'

config.status: creating Makefile

Step 3: Now, build and install:

$ make install

When the make and make install is complete, the RTEMS libraries, headers, and some sample programs are in:

$HOME/development/rtems/bsps/4.11/arm-rtems4.11/raspberrypi

All of these directories may seem a little complicated, but it starts to make sense when you want to have different RTEMS versions, different architectures, and different BSPs all on the same development machine, without getting anything mixed up.

So now you have a cross compiler, and have RTEMS compiled and installed, so what about getting an RTEMS program to actually run on the Pi ?

Next: Run an RTEMS program on the Raspberry Pi

Setting up an RTEMS development environment for the Raspberry Pi

Setting up an RTEMS development environment for the Raspberry Pi

For this tutorial, I’m going to use Ubuntu 12.10 64 bit (updated to 14.10 LTS on April 2014). It is possible to do RTEMS development on almost any recent version of Linux, OS X, FreeBSD, and Windows. The RTEMS project provides pre-built compilers for all RTEMS architectures for a number of different development hosts including Fedora, CentOS, Suse, and Windows.

In addition to the pre-built compilers provided, there is a tool called RTEMS Source Builder that can download, patch, build, and install the compiler for you. This is what we will use for our Ubuntu setup.

Note
  1. You will need a RTEMS compiler, an ARM bare metal GCC will not do.

  2. I have posted some instructions for setting up the environment for Windows 7 here

  3. The following instructions start with a fresh install of Ubuntu 14.10 LTS 64 bit, with all updates applied.

Install Ubuntu prerequisites:

$ sudo apt-get install build-essential
$ sudo apt-get install git
$ sudo apt-get install python-dev
$ sudo apt-get build-dep binutils gcc g++ gdb unzip git

Decide where the RTEMS tools and projects will reside

Here is the directory structure we will be using:

Path Description

$HOME/development/rtems

The base directory for all RTEMS work

$HOME/development/rtems/compiler

Where the compiler is installed

$HOME/development/rtems/rtems-git

Where the RTEMS source code is checked out

$HOME/development/rtems/bsps

Where the RTEMS board support packages/libs are installed

$HOME/development/rtems/rtems-source-builder

The RTEMS source builder tool

Table 1. Directory Structure

Lets get started on this:

$ cd $HOME
$ mkdir development
$ cd development
$ mkdir rtems
$ cd rtems
$ mkdir compiler

Build the RTEMS ARM cross compiler

The RTEMS Source builder documentation is located here:
http://www.rtems.org/ftp/pub/rtems/people/chrisj/source-builder/source-builder.html

1. Check out the RTEMS source builder tool

$ cd $HOME/development/rtems
$ git clone git://git.rtems.org/chrisj/rtems-source-builder.git

2. Double check that all source builder dependencies are present:

$ cd rtems-source-builder
$ source-builder/sb-check

This command should return the following:

RTEMS Source Builder environment is OK
Note The separate autotools build is no longer needed. The source builder will install them for you.

3. Build and install the toolchain

$ cd $HOME/development/rtems/rtems-source-builder/rtems
$ ../source-builder/sb-set-builder \
         --log=build-log.txt \
         --prefix=$HOME/development/rtems/compiler/4.11 \
          4.11/rtems-arm

After the full download and build process, the toolchain is installed in:
$HOME/development/rtems/compiler/4.11

Tip This whole process can take about 10 to 30 minutes depending on your computer specs. This has been run on a Raspberry Pi: Expect that to take all day!

4. Add the toolchain bin directory to your path, and you are ready to download and build RTEMS:

$ cd $HOME

Using your favorite editor, edit the .profile file and add the following line at the end of the file

PATH=$HOME/development/rtems/compiler/4.11/bin:$PATH

5. Now, logout and log back in. Open a terminal and type:

$ arm-rtems4.11-gcc -v

You should see the compiler version message. The compiler is installed and ready to use!

Next: How to download and build RTEMS

RTEMS on the Raspberry Pi

RTEMS on the Raspberry Pi


What is the Raspberry Pi?

The Raspberry Pi is a neat little ARM SoC (System on a Chip) based computer that runs Linux and a growing number of small/embedded operating systems. There is a $25 model A with 256MB of RAM and one USB port. There is a $35 Model B that has 512MB of RAM, 2 USB ports, and a 10/100 ethernet port. The Pi runs Debian Linux from an SD card and provides a full desktop environment.

The Raspberry Pi was created primarily as a way of encouraging kids to learn how to program, but it has quickly been adopted by over a million people as an inexpensive embedded computer.

Find out more about it here: http://www.raspberrypi.org


What is RTEMS?

RTEMS stands for Real Time Executive for Multiprocessor Systems. RTEMS is an open source real time embedded operating system. RTEMS is similar to vxWorks in size and functionality. It has many features including a network stack, file systems, POSIX APIs and more. It runs on the x86, PowerPC, m68k/Coldfire, ARM, MIPS, SPARC, and AVR architectures. RTEMS is used in many embedded applications including quite a few satellites. I have used RTEMS on few satellites for my job.

More info about RTEMS is available here: http://www.rtems.org


RTEMS on the Raspberry Pi

Once I received my Raspberry Pi (of course, I had to have one), it was not too long before I looked at what it would take to get RTEMS working on it. Most of the boards I have used RTEMS on cost $200 or more, so the chance to have an RTEMS target that everyone could buy for $35 was pretty appealing to me.

There is a great community of coders on the Raspberry Pi forums, so I studied many of the samples from the Bare Metal programming forum over at raspberrypi.org

With some help from the RTEMS community I was able to put together a bare minimal BSP that runs on the Pi. It only supports the timer and the UART at this point, but it’s a good starting point. I hope that others can find it useful and over time we can add support for all of the devices on the Pi such as the USB, Network, SD Card, etc.


Trying out RTEMS on the Raspberry Pi

Getting an RTEMS development environment up and running can sometimes be a little bit of work. In the next few blog entries, I will walk you through setting up an RTEMS development environment and getting some simple RTEMS programs running on your Raspberry Pi.

Next: Setting up an RTEMS development Environment for the Raspberry Pi