MAKE: Installing the AVR Toolchain for RH9

June 14, 2006, 09:40 PM

This serves as a reference guide for installing the AVR toolchain on Red Hat 9 and is part of the development process for AVR Microcontrollers. I hope you find this handy. --Thomas Ng

Hardware Requirements:
Computer with RedHat 9 or Fedora Core 4/5 installed on it and 'root' access to the system.


Software Downloads:
GNU Binutils - (version 2.16.1) - http://sources.redhat.com/binutils/
GCC - (version 3.4.6, older stable version) - http://gcc.gnu.org/
AVR Libc - (version 1.4.4) - http://savannah.nongnu.org/projects/avr-libc/
UISP - Programmer (20050207) - http://savannah.nongnu.org/projects/uisp/

NOTE: Always install the latest software packages to ensure you have the latest bug fixes (unless otherwise noted)


Installation Steps

Setup your path and environment variables

  1. Setup your $PATH variable to include "/usr/local/avr" and "/usr/local/avr/bin." You will be installing the AVR toolchain to this directory, so if it doesn't already exist, make a folder called avr in /usr/local/

Install GNU Binutils:

  1. Download the GNU binutils package from http://sources.redhat.com/binutils/
  2. Untar/ungzip/unbzip the file and cd into the directory
    # bunzip2 -c binutils-2.16.1.tar.bz2 | tar xf -
    # cd binutils-2.16.1

  3. Make a directory called obj-avr and CD into it. This is a good idea and helps you keep your source tree clean.
  4. Next, configure the installation for the AVR platform and set your target directory:
    # ../configure --target=avr \
        --prefix= /usr/local/avr \
        --disable-nls

  5. Next, compile and install the binutils:
    # make && make install

  6. If all went well the installation will complete and the binutils are installed. This step must be completed before you continue. If there was a bug or some error, search google for potential answers or check the binutil support forums.

Install GCC:

  1. Download the GCC package from http://gcc.gnu.org/. The "core" package should work out just fine. You can download the larger package to support C++ in addition to C and obj-C++.
  2. First, decompress the image and cd into the gcc directory.
    #bunzip2 -c gcc- core-3.4.6.tar.bz2 | tar xf -

  3. The remainder of the install process is similar to the binutil process:
    # mkdir obj-avr
    # cd obj-avr
    # ../configure --prefix=/usr/local/avr \
        --target=avr \
        --enable-languages=c \
        --disable-nls \
        --with-dwarf2
    # make && make install

  4. Besure to note any errors that occur between steps and fix them before you issue the next command. If all went well, avr-gcc will be available to your system when the make process is complete.

Install AVR libc:

  1. Download the AVR libc package from http://savannah.gnu.org/projects/avr-libc/. .
  2. Similar instructions as previous:
    # gunzip -c avr-libc-1.4.4.tar.gz | tar xf -
    # cd avr-libc-1.4.4
    # ./configure --build=`./config.guess` \
        --host=avr \
        --prefix=/usr/local/avr
    # make && make install

  3. NOTE: I had to make a change to the configure script to make it install the AVR C libraries in the correct place. Here is the diff output incase you need to make the same change:
    #diff configure configure.orig
    257c257
    < ac_default_prefix=/usr/local
    ---
    > ac_default_prefix=/usr/local/avr

  4. If the make process is clean, the install will occur and you are almost there!


Install UISP (programmer):

  1. Download the UISP package from http://savannah.nongnu.org/projects/uisp/ . .
  2. Now issue the following commands to setup and install the UISP:
    # gunzip -c uisp-20050207.tar.gz | tar xf -
    # cd uisp-20050207
    # mkdir obj-avr
    # cd obj-avr
    # ../configure --prefix=/usr/local/avr
    # make && make install

  3. Once again, look for errors in the build process. If there are none, UISP should be successfully installed.

Makefiles

  1. Use the following sample Makefile to build your projects:
    PRG = demo # Change "demo" above to the name of your project with NO extension.


    # You should not have to change anything below here, but go ahead and
    # try to understand what is going on.

    OBJ = $(PRG).o
    MCU_TARGET = atmega128
    OPTIMIZE = -O2 # options are 1, 2, 3, s

    DEFS =
    LIBS =

    CC = avr-gcc

    # Override is only needed by avr-lib build system.

    override CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS)
    override LDFLAGS = -Wl,-Map,$(PRG).map

    OBJCOPY = avr-objcopy
    OBJDUMP = avr-objdump

    all: $(PRG).elf lst text eeprom

    $(PRG).elf: $(OBJ)
    $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)

    clean:
    rm -rf *.o $(PRG).elf *.eps *.png *.pdf *.bak
    rm -rf *.lst *.map $(EXTRA_CLEAN_FILES) *~

    program: $(PRG).hex
    uisp -dprog=stk200 -dpart=atmega128 -dlpt=/dev/parport0 --erase --upload if=$(PRG).hex


    lst: $(PRG).lst

    %.lst: %.elf
    $(OBJDUMP) -h -S $< > $@

    # Rules for building the .text rom images

    text: hex bin srec

    hex: $(PRG).hex
    bin: $(PRG).bin
    srec: $(PRG).srec

    %.hex: %.elf
    $(OBJCOPY) -j .text -j .data -O ihex $< $@

    %.srec: %.elf
    $(OBJCOPY) -j .text -j .data -O srec $< $@

    %.bin: %.elf
    $(OBJCOPY) -j .text -j .data -O binary $< $@

    # Rules for building the .eeprom rom images

    eeprom: ehex ebin esrec

    ehex: $(PRG)_eeprom.hex
    ebin: $(PRG)_eeprom.bin
    esrec: $(PRG)_eeprom.srec

    %_eeprom.hex: %.elf
    $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@

    %_eeprom.srec: %.elf
    $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $< $@

    %_eeprom.bin: %.elf
    $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@

    # Every thing below here is used by avr-libc's build system and can be ignored
    # by the casual user.

    FIG2DEV = fig2dev
    EXTRA_CLEAN_FILES = *.hex *.bin *.srec

    dox: eps png pdf

    eps: $(PRG).eps
    png: $(PRG).png
    pdf: $(PRG).pdf

    %.eps: %.fig
    $(FIG2DEV) -L eps $< $@

    %.pdf: %.fig
    $(FIG2DEV) -L pdf $< $@

    %.png: %.fig
    $(FIG2DEV) -L png $< $@