Monday, January 21, 2008

Building a cross compiler for AVR Microcontrollers

Why we need a cross -compiler? Cross-compilers enable us to develop on one platform( host) while actually building for another platform( target). For building this what we need is a compiler that knows how to write machine code for our target.

Giving a basic introduction about what compiler does, what are the components of a compiler it will be easy to understand the build process. The basic goal of a compiler is to produce the bytecode for target CPU or microcontroller. The main components of a compiler are

parser: converts the language source code (eg: C, C++) into the target assembly langauge. So, Parser needs to know the target assembly langauage.

Assembler: converts the assembly language code into the bytecode (object file) that the CPU executes.

Linker: combines the object files into an executable application. Different operating system and CPu combinations generally use different encapsulation mechanism.
Eg: Executable Link Format(ELF) - usually used by PowerPC processors.

Standard C Library: If we are using functions from the C library( eg, printf) , we have to use some standard C library. Newlib and Glibc are the most common one. Newlib was developed by redhat and Glibc is GNU C library.

So, we need to build three components from the GNU tools available.

1. Binutils - includes the basic binary utilities such as the assembler, the linker, the strip utility etc..Strip utility is used for removing the unwanted information from object file and executable such as symbol table, debugging information.

2. GCC - have C preprocessor and assembly translator.

3. glibc or newlibc

Now, we can start building the cross compiler for AVR. For that first we have to create an installation folder in our linux system. We can use /usr/local/AVR

Now go to this link : http://www.nongnu.org/avr-libc/user-manual/install_tools.html

Downlaod the Binutils source, GCC source and AVR Libc source(Three Zip files )from the links provided there to /usr/local/AVR folder. Untar that Zip in the same folders. Now you will get 3 folders inside the /usr/local/AVR

binutils-x.xx
gcc-x.x.x
avr-libc-x.x.x

I used the following versions:
binutils-2.15, gcc-2.7.3, avr-libc-1.0.5. Now build the three components as explained below.

Build the Binutils:

Execute the following commands in the shell prompt to install the Bintuils for AVR.
configure -
Configure the build for AVR and specify the bin directory or install directory.
make - compile the code.
make install - Install in /usr/local/AVR/bin


$ cd binutils-2.15
$ ./configure --prefix=/usr/loca/AVR --target=avr
$ make
$ make install

The /usr/local/AVR/bin directory now contains AVR versions of ld, as, ar and the other binutils executables. Add the /usr/local/AVR/bin directory to your PATH now. You can apply the modification system-wide by adding:

PATH="$PATH:/usr/local/AVR/bin" to the /etc/profile file.

Build the GCC:

$ cd gcc-2.7.3
$ ./configure --prefix=/usr/loca/AVR --target=avr --enable-languages="c,c++" --disbale-nls
$ make
$ make install

Build the AVR Libc:

$ cd avr-libc-1.0.5
$ export PREFIX=/usr/local/AVR
$ sh ./doconf
$ ./domake
$ cd build
$ make install
So, we have installed the avr-gcc cross compiler for the AVR series microcontrollers succesfully..!!

Now after verifying that the PATH variable"/usr/local/AVR/bin" is set system wide( means from anyfolder we can use the avr-gcc and binutils tools), we can start compiling C and C++ files for the AVR.

No comments: