Building AVR GCC

From Wiki

Jump to: navigation, search

Return to AVR GCC main page

Contents

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

Note: Last Updated 2/6/2007.

Please email avr@slacy.com any updates, questions, or comments about this page. Thanks, and happy AVR-ing!

Windows Users: Please go to WinAVR to find information about the Windows-specific version of this code. Or, just install Linux!

These are abbreviated instructions for someone versed in CVS, GCC, cross-compiling, make, binutils, and all things UNIX.

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 simple avrdude low cost programmer that you can easily build.

If you are just getting started with AVR programming, please see AVR Hardware Kit for a list of some other really useful equipment, and Your First AVR Order for a list of parts to get you going.

Get the prerequisite compilation tools installed

I've done this all on a vanilla Fedora Core 4 system, but there is no reason to think it won't work on other Linux/UNIX installs. You'll need a nativie C/C++ compiler (gcc) as well as lex (flex), yacc (byacc), and bison. These can be found on most standard Linux distributions.

The Fedora Core 4 packages needed are:

flex-2.5.4a-34
bison-2.0-6
byacc-1.9-29
gcc-4.0.2-8
gcc-c++-4.0.2-8

On Fedora-based Linux systems, these can be easily and automatically installed by running this command:

  # yum install flex bison byacc gcc gcc-c++

On Ubuntu installations, this will be something like:

  # apt-get install flex bison byacc gcc gcc-c++

Download and compile binutils & GCC

Binutils most recent version is 2.17 as of 2/6/2007. The binutils webpage is http://www.gnu.org/software/binutils/ You can download the most recent version from ftp://sources.redhat.com/pub/binutils/snapshots

Untar the binutils distribution, go into the newly created directory, then run:

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

Find the most recent released version of gcc by looking on http://gcc.gnu.org/releases.html. Then, download it from an appropriate mirror as listed on http://gcc.gnu.org/mirrors.html. The latest stable release is 4.1.1 as of 2/6/2007. For support of new AVR devices, you might instead want to download a recent snapshot version (the 4.2 and 4.3 branches as of 2/6/2007). 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, and run:

# mkdir avr-gcc
# cd avr-gcc
# ../configure --target=avr --program-prefix="avr-" --enable-languages=c
# make
# make install

Note: If you are going to build gcc 4.1.1 or a similar version (even 4.2.x have this issue) you have to add the option --disable-libssp to the configure call above. If omitted, the build process will probably fail. This might be related to this warning that is printed out during the configure process:

checking for correct version of mpfr.h... buggy version of MPFR detected
checking for any version of mpfr.h... yes
*** This configuration is not supported in the following subdirectories:
     target-libmudflap target-libgomp target-libiberty target-libssp
    (Any other directories should still work fine.)

Note: If you attempt to compile gcc 4.1.1 using the configure option --with-dwarf2, the build will abort due to a bug documented in the GCC bug database -- Bug 26504. The bug was fixed for gcc 4.1.2. I downloaded a snapshot of 4.2 (gcc-4.2-20061212) and --with-dwarf2 now seems to compile cleanly.

Get avr-libc and UISP

Download avr-libc from http://savannah.nongnu.org/projects/avr-libc/. (The most recent version is avr-libc-1.4.5 as of 2/6/2007)

Grab the most recent file from http://savannah.nongnu.org/download/avr-libc/ and untar it.

# ./configure --host=avr
# make 
# make install

Download UISP, the "micro In System Programmer" from http://savannah.nongnu.org/projects/uisp/ (The most recent version is uisp-20050207 as of 2/6/2007)

Grab the most recent file from http://savannah.nongnu.org/download/uisp/ and untar it.

# ./configure
# make
# 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-gcc       - GNU project C Compiler Collection. It wraps around most of the programs below.
     
     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
     avr-fastjar   - Java something 
     avr-gasp      - assembler preprocessor 
     avr-ld        - the GNU linker 
     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. Unlike the  unix strings cmd, it will decode hex files(?)
     avr-strip     - Discard symbols from object files. It shouldn't be possible to make a bloated hex, anyway.
     uisp          - micro In System Programmer
     cc1,c99       - The honest-to-god compiler. This is where the real magic happens.

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

Automatic Build Script

This build script downloads and builds the following tools automatically:

  • binutils (2.17)
  • avr-gcc (4.2 Snapshot)
  • avr-libc (1.4.4)
  • dfu-programmer (0.3.0)

Download build_avr_toolchain.sh and make this file executable. Then call it like this:

./build_avr_toolchain.sh | tee build_avr_toolchain.log

If you do not run this script on a debian based distribution you have to uncomment all "check_for_package" calls. And make sure by yourself that the respective software is available on your system.

Unfortunately it might be nessesary to convert the file to unix linefeed encoding with a tool like: fromdos

fromdos build_avr_toolchain.sh

Compiling and Testing your first program

I recommend a program called "AVR Mirror" that mirrors input pins on output LEDs.

Personal tools