LPC2478 Hello World

It is time to setup the basic development environment so that it is possible to compile a simple hello world program. The Olimex LPC2478 stk comes with a cdrom containing most of the necessary software but if you feel like experimenting you can always find other versions at Snapgear or uClinux.

You will need to extract two different tar balls, either the ones provided on the cdrom or else by downloading them. When writing this post, I tried to use arm-linux-tools-20080623.tar.gz to build uClinux but ran into some unexpected behaviours. The objcopy generated extremly large vmlinux.bin files so if this happens, try the provided arm tools instead (arm-linux-tools-20061213.tar.gz).
NOTE: The uClinix dist delivered with the olimex board comes patched so it might be a good idea using it initially.

I assume that the development environment should be placed under /opt/arm so let’s start by creating that directory.
%> mkdir /opt/arm
%> cd /opt/arm

Extract the necessary tarballs.
%> tar -xvzf .../Utils/arm-linux-tools-20061213.tar.gz
%> tar -xvzf .../uClinux/uClinux-dist-lpc_2478_stk-20081007.tgz

For some reason, genromfs was not included with the tools chain delivered with the Olimex development board so if you can’t locate it, fetch it and build it like this.

%> wget http://www.uclinux.org/pub/uClinux/uclinux-elf-tools/gcc-3/uclinux-tools-20040603/genromfs-0.5.1.tar.gz
%> tar -xvzf genromfs-0.5.1.tar.gz
%> cd genromfs-0.5.1
%> make
%> cd ..

Before building uClinux we need to setup the PATH,
%> export PATH=/opt/arm/usr/local/bin:/opt/arm/genromfs-0.5.1:$PATH
%> cd uClinux-dist-lpc_2478_stk

and clean it before building.

%> make distclean
%> \rm -f tools/ucfront-g++
%> \rm -f tools/ucfront-gcc
%> \rm -f tools/ucfront-ld
%> \rm -f tools/cksum

Finally, it is time to build uClinux.
%> make menuconfig

Menuconfig

Menu config

Now, just set Vendor/Product to NXP/LPC2468 by choosing Vendor/Product Selection —> Vendor -> NXP. LPC2468 will automatically be choosen since it is the only alternative.

NXP and LPC2468

NXP and LPC2468

Select Exit->Exit->Yes.

%> make dep
%> make

Hopefully everything went well and you probably have got a couple of images directly under the subdirectory images. Most likely romfs_5.img and vmlinux.bin. We need to add the uClinux tools directory to our path as well.
%> export PATH=/opt/arm/uClinux-dist-lpc_2478_stk/tools:$PATH

The new image files that we have created might be a good idea to use so you probably should copy them to tftps home catalogue so that you can boot from them instead.
%> cp images/romfs_5.img /home/tftp/romfs_new.img
%> cp images/vmlinux.bin /home/tftp/vmlinux_new.bin

Finally it is time to create our hello world program and the associated makefile for building it. Create some work directory, like /projects/Olimex/HelloWorld.

Create helloworld.c and the Makefile (or download them from here).

#include <stdio.h>
int main(int argc, char** argv) {
printf("\r\nHello world!\r\n");
return 0;
}

and create the Makefile

CC=ucfront-gcc arm-linux-gcc
LD=ucfront-gcc arm-linux-gcc

CFLAGS=-msoft-float -D__PIC__ -fpic -msingle-pic-base -Dlinux -D__linux__ -Dunix -D__uClinux__
LDFLAGS=-Wl,--fatal-warnings -Wl,-elf2flt -msoft-float -L/opt/arm/uClinux-dist-lpc_2478_stk/uClibc/lib
SOURCES=helloworld.c
OBJECTS=$(SOURCES:.c=.o)

TARGET=helloworld
.PHONY=all
all: $(TARGET)

%.o : %.c
 $(CC) $(CFLAGS) -c $<

$(TARGET): $(OBJECTS)
 $(LD) $(LDFLAGS) -o $@ $(OBJECTS) -lc

.PHONY=clean
clean:
 \rm -f *.o
 \rm -f *~

Remember that Makefiles are very obnoxious when it comes to whitespacing, all commands must start with a tab character and not spaces so if you get an error about missing separator, check that line if it contains leading spaces instead of a tab.

Try compiling the hello world application.
%> make

Hopefully the output will look something like this:

ucfront-gcc arm-linux-gcc -msoft-float -D__PIC__ -fpic -msingle-pic-base -Dlinux -D__linux__ -Dunix -D__uClinux__ -c helloworld.c
ucfront-gcc arm-linux-gcc -Wl,--fatal-warnings -Wl,-elf2flt -msoft-float -L/opt/arm/uClinux-dist-lpc_2478_stk/uClibc/lib -o helloworld helloworld.o -lc

Since we are going to use tftp to get the helloworld application to the dev-board, copy the program to tftps home directory.
%> cp helloworld /home/tftp/

Let’s try to get it over to our development board, so first we need to do a short recap on what was done in “Getting started with Olimex LPC2478″ but the short version is:

  • Connect ethernet and rs232 cable to development board
  • Write ‘picocom -b 115200 /dev/ttyUSB0′ in a terminal
  • Add power to the development board and hit any key while beeing in the picocom terminal within 3 seconds.
  • tftpboot 0xa0800000 192.168.1.100:romfs_new.img
  • tftpboot 0xa0008000 192.168.1.100:vmlinux_new.bin
  • go a0008000

Since we are going to use tftp to get helloworld to the development board we need to start the eth0.
# /sbin/ifconfig eth0 192.168.1.20 up

You can test if ethernet is working by typing
# ping -c 1 192.168.1.100

Almost everything is read only so change directory to /var and fetch the helloworld application from the tftp server
# cd /var
# tftp -g -r helloworld 192.168.1.100
# chmod +x helloworld
# ./helloworld

Hello world!
#

That’s about it.

As I wrote previously, I had some problems with the tools chain and extremely large images. I still haven’t figured out why it occured and I can not find any bug reported on objcopy but this is how it looked when listing the size.
%> ls -la images/
....
-rw-r--r-- 1 anders anders 2686629728 2011-02-13 19:53 vmlinux.bin

As you can see, vmlinux.bin is quite huge.

Another odd problem I had when writing this post was that printf only sent garbage characters to the console. After a lot of searching I found out that the CFLAGS in the Makefile needed -D__PIC__ -fpic -msingle-pic-base.

This entry was posted in LPC2478. Bookmark the permalink.
-->