Building AVR GCC

From Egbert's Wiki

Building your own C cross-compilers for Atmel AVR under Linux

  • Last update: May, 16 2011. Wiki reworked for Ubuntu 11.04. Most of all: use preconfigured packages.
  • April, 18 2009. This guide is heavily based upon the page written by Steve Lacy avr@slacy.com in the AVR Wiki. Building gdb and simulavr are taken from Psycogenic website which has a few very usable articles on AVR. They have also written a reusable Makefile with a lot of handy targets. A copy of this Makefile can be found here: Universal AVR Makefile.

Use precompiled packages

All cross compiler packages and tools you need are already available in the Ubuntu repositories. Use apt-get or aptitude to install.

$ sudo aptitude install geany avr-libc binutils-avr gcc-avr gdb-avr avrdude avrdude-doc simulavr

You should have a working cross-compiler environment now. Geany must be told where tools are and how they are used.

Native tools needed to build the AVR toolchain

I've done this all on Mandriva 2008.0, 2008.1, 2009.0 PWP (Official PowerPack) some time ago. Recently I decided to switch to Ububtu and apart from some Debian/Ubuntu quirks, the whole proces is more or less the same. You need a nativie C/C++ compiler (gcc) as well as lex (flex), yacc (byacc), and bison. Recent versions can be found on most standard Linux distributions. C++ ,intltool and GTK+2.0-devel are needed for Geany if you want to install from source. On Ubuntu, so called meta packages are available to install all that is needed for compiling with C and C++.

$ sudo aptitude install build-essentials geany

Then you need the source packages (see below) to build everything yourselves. Advantage: you have the latest version.

Geany is a lightweight IDE for Linux/BSD/Windows using GTK-2.0. The Windows version of the tools, WinAVR, comes with Programmers Notepad, which is not (yet?) ported to Linux. Another alternative for Linux could be KontrollerLab but has a lot of KDE dependancies. Things like Eclipse are very feature full but also slow on moderate hardware since they are written in Java.

Source tarballs needed

To be able to build the toolchain for a cross-compiler environment, source tarballs are needed for:

binutils 2.21 (or newer)
gcc-core 4.3.4 (or newer)
gmp-latest (needed to compile gcc)
mpfr-latest (needed to compile gcc)
avr-libc 1.6.8 (or newer)
avrdude-5.10 (or newer)

Note: binutils and gcc are not neccessarily in sync with each other. This has to do with the addition of some newer AVR devices.

Avr-libc seems to make a huge jump from 1.4.7 to 1.6.6. The 1.4.x branch is depreciated (info from Eric Weddington, maintainer WinAVR).

Avrdude is the de-facto tool to program AVR devices via STK200 (or Kanda) parallel port cable or intelligent programmers like the STK500 and the AVRispII.

In general: use newer versions, when available.

Download and compile binutils

Binutils most recent version is 2.21 as of this writing which is needed to be able to compile for the latest AVR chips (at90usb82 and at90usb162). The binutils webpage is at http://www.gnu.org/software/binutils/ or use a mirror near to you. Untar the binutils distribution, go into the newly created directory and create a working directory. Then configure, make and make install.

$ tar jxvf binutils-2.21.tar.bz2
$ cd binutils-2.21
$ mkdir obj-avr
$ cd obj-avr 
$ ../configure --target=avr --program-prefix="avr-"
$ make

$ sudo make install

Download and compile gcc

The latest stable release is 4.3.4 but there might be be some issues with binutils. Binutils and gcc are not allways in sync. w.r.t. the latest AVR chips. Download gcc from http://gcc.gnu.org/releases.html or from an appropriate mirror as listed on http://gcc.gnu.org/mirrors.html. To compile avr-gcc, you only need to download and compile the gcc-core source tarball. Untar the gcc distribution, go into the newly created directory, make a working directory and run configure:

$ tar jxvf gcc-core-4.3.4.tar.bz2
$ cd gcc-4.3.4
$ mkdir obj-avr
$ cd obj-avr
$ ../configure --target=avr --program-prefix="avr-" --enable-languages=c --disable-libssp
$ make

$ sudo make install

Note 1: With all releases (4.3.x inclusive) you need to add the option --disable-libssp to the configure call. If omitted, the build process will fail.

Note 2: If configure complains about GMP (Gnu Multi Precision Library) and/or MPFR, install those first. Recent versions can be obtained from http://gmplib.org/ and http://www.mpfr.org/. MPFR depends on GMP, make GMP first.

Note 3: If you need support for dwarf2 output, use the configure option --with-dwarf2.

Download and compile avr-libc

Download avr-libc from Savannah. (The most recent version is avr-libc-1.6.8). While you are there anyway, download the manuals (man pages, pdf file). Untar the avr-libc distribution, go into the newly created directory, and run:

$ ./configure --host=avr
$ make
and as root
# make install

Download and compile avrdude

Download avrdude, the "AVR Download/UploDEr" from Savannah (The current version is avrdude-5.10). Make sure libusb-devel is installed when support for USB programmers like the AVRISP-MKII is needed. Again: get the manual while you are there. Untar the avrdude tarball, go into the newly created directory and run:

$ ./configure
$ make
and as root
# make install

Download and compile geany

After long search I found a neat lightweight IDE called geany. Geany can be downloaded from http://geany.uvena.de/. RPM's are supposed to be supplied but I was not able to located them. Use the latest source kit instead. Untar the tarball and cd into the newly created directory. Geany needs GTK+2.0 and its devel package. Install gtk+2.0 and gtk+2.0_0-devel (and dependancies) first if needed.

$ ./configure
$ make
and as root
# make install
On Ubuntu is Geany available as package:
$ sudo aptitude install geany

Installing The AVR Debug Toolkit (optional)

Simulavr and gdb/ddd or insight are not strictly neccessary but they complete the picture nicely. ddd is a Motif GUI using lesstif which makes it look rather different than gnome or kde. Insight is not much better looking but is actively maintained by Cygwin people at RedHat. The debugging toolkit can be added in a later stage. To install the debugging toolkit, you need two distinct programs: the simulavr AVR-simulator and gdb, the GNU Debugger (gdb or insight), compiled for AVR targets. Some of those tools installed before must be accessible during configuration so make sure /usr/local/avr/bin is present in your PATH.

simulavr-0.1.2.5
gdb-6.7.1
ddd-3.3.11
OR
simulavr-0.1.2.5
insight-6.7.1 (This is gdb with an integrated GUI)

Download and compile simulavr

Begin by retrieving the latest release of Simulavr, from their Savannah project site. Due to recent events, you may be forced to use the version from CVS--follow the instructions on site to perform that download.

Compilation and installation should be straightforward:

$ ./configure
$ make
as root
# make install

Download and compile gdb

Get the latest gdb (or insight) source tarball from Sourceware, which already contains everything needed to debug AVR programs. Compilation and installation are also simple and very similar to all the previous tools:

$ ./configure --target=avr --program-prefix="avr-"
$ make
as root
# make install

What you end up with

GNU build/development utilities under /usr/local/avr, /usr/local/avr/lib and /usr/local/bin Remember to add /usr/local/bin to your path, if its not already there.

avr-addr2line   - Convert addresses into file names and line numbers.
avr-ar          - Create, modify, and extract from archives.
avr-as          - The portable GNU assembler.
avr-c++filt     - Demangle C++ symbols
avr-cpp         - The C Preprocessor.
avrdude         - The configurable chip programmer.
avr-gcc         - GNU project C Compiler Collection. It wraps around most of the programs below.
avr-gcc-4.2.3   - GNU C Compiler
avr-gccbug
avr-gcov 
avr-gdb         - The GNU debugger.
avr-gdbtui
avr-insight     - The GNU debugger with GUI.
avr-gprof       - The GNU Profiler.
avr-ld          - The GNU linker.
avr-man
avr-nm          - List symbols from object files.
avr-objcopy     - Copy and translate object files
avr-objdump     - Display information from object files.
avr-ranlib      - Generate index to archive.
avr-readelf     - A fancier version of objdump.
avr-size        - List section sizes and total size.
avr-strings     - Recognizes and displays possible ascii text within files.
avr-strip       - Discard symbols from object files. It shouldn't be possible to make a bloated hex, anyway.
geany           - Graphical development environment (IDE).
simulavr
simulavr-disp
simulavr-vcd

As well as the required libraries and object files to build standalone programs with no operating system. See /usr/local/avr/lib.

Chip Programming Hardware Requirements

I do all my Atmel AVR development using an STK500 development board. The code produced by avr-gcc should work with any programming board and chip, but you will need some way to interface the chip to your PC so that you can program it. The STK500 is the most flexible option, but others include the AVR Butterfly for a complete "system on a chip" or the Avrdude hardware low cost programmer that you can easily build yourself. The STK500 connects to a COM port. The STK200 programming cable connects tot the parallelport. Recently I added an avrisp-mkII clone (made by Elektor). This is an USB connected programmer with the STK500V2 commandset. Make sure usblib and usblib-devel are installed when avrdude is compiled.

Next: Simple test Back: Hoofdpagina