Adding a C library to the arm-gcc toolchain
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:
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.

[...] I now have a step-by-step on how to add on a standard C library. Click here. [...]
Dude, someone is phishing with your website. Nice security you guys have.. http://www.doctort.org/adam/blog/images/onlineupdating/index.htm
Thanks for noticing that Ruben, I feel so embarrassed right now!
I’ll delete it straight away.
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).
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.
I’ve not tried malloc yet. I’m sure I’ll post about it when I do.