ADAM'S WEB PRESENCE

5 April 2007

Adding a C library to the arm-gcc toolchain

Filed under: Nerd Notes — adam @ 9:04 pm

This is a sequel to my previous article Getting Started with the Olimex SAM7-P256. Flashing lights are all very well but I want to add a C library so I can use functions like printf().

There is a streamlined version of the standard C library available for embedded systems called newlib which you can download from the files section at http://www.gnuarm.com/. Here’s how you build it to fit in with the toolchain in my previous article:

First you need to have makeinfo. On Ubuntu, install it like this:

sudo apt-get install texinfo

Now we can build and install the library:

tar xzvf newlib-1.14.0.tar.gz

mkdir build-newlib

cd build-newlib

../newlib-1.14.0/configure --target=arm-elf --prefix=/usr/local/arm7

sudo make all install info

Documentation for newlib can be found at http://sourceware.org/newlib/.

Now we have a C library, how about writing Hello World! That was not as easy as it sounds, mainly because I wanted printf() to send its output to the serial port. It took me a lot of documentation-reading and hair-pulling to work out how to do that.

To redirect STDOUT, you need to override the syscalls.o module which is part of libc. Copy the syscalls.c and swi.h files from the newlib source into your project directory and modify the _write() function to send its data to the serial port.

I’ve got some example code to demonstrate this. You can download it by clicking the link here:

ArmHelloWorld.tar.bz2

I’ve tried to keep the code as simple and readable as possible. I’ve also included a couple of shell scripts to make it easier to download and run the code. Here is how you would compile and run it:

tar jxvf ArmHelloWorld.tar.bz2

cd HelloWorld

make

./startarm

Next you need to connect a serial cable and start up a serial terminal on your PC at 9600,n,8,1. Once that is running, launch the example code by typing this:

./runram

If all goes well, you should see “Hello World.” appear on your serial terminal.

If not, well then happy debugging !


UPDATE: Dan Ashbrook kindly pointed out that my port assignments for the 2nd serial port are incorrect in Board.h. I have now corrected the file, you can download it here: ArmHelloWorld.tar.bz2

UPDATE: I recently tried to do this on Ubuntu 8.04 and it didn’t work until I applied this patch to the newlib configure script.

6 Comments »

  1. Pingback by Adam Pierce » Getting Started with the Olimex SAM7-P256 — 5 April 2007 @ 9:08 pm

    [...] I now have a step-by-step on how to add on a standard C library. Click here. [...]

  2. Comment by Ruben Williams — 8 April 2007 @ 5:19 am

    Dude, someone is phishing with your website. Nice security you guys have.. http://www.doctort.org/adam/blog/images/onlineupdating/index.htm

  3. Comment by adam — 9 April 2007 @ 10:35 pm

    Thanks for noticing that Ruben, I feel so embarrassed right now!
    I’ll delete it straight away.

  4. Comment by aepytus21 — 2 December 2007 @ 12:47 pm

    These files are fantastic! Maybe it’s available somewhere else on the web, but as far as I can tell, this is the only place to get the _write code for the SAM7 UART. Thanks again for this huge time-saver (and a chance for me to educate myself on the low-level C using your example).

  5. Comment by Glaive — 23 January 2008 @ 7:55 pm

    Hi Adam!

    Thanks a lot for your work, it really helped me with understanding the board. Now I’m trying to get memory allocation working with malloc, but I’m not having any luck there. Have you tried this? It seems there is a problem with the heap.

  6. Comment by adam — 23 January 2008 @ 8:08 pm

    I’ve not tried malloc yet. I’m sure I’ll post about it when I do.

RSS feed for comments on this post. TrackBack URI

Leave a comment


Powered by WordPress