2009-09-15

Building GCC toolchain for AVR

Recently, along with my Arduino-ethernet-shield I bought a Arduino-Diecimilia fitted with a ATMega328, only to find out that my compiler set-up wouldn't work. A quick investigation (here and here) showed me that my current gcc toolchain (4.2.2) didn't support the '328.
OpenSuse repositories provide an avr-gcc toolchain that can be used with the Arduino IDE for Arduinos fitted with the ATMega8 (very old) to the ATMega168 (old), for the ATMega328 you must build the toolchain yourself (at least for now).
I tried to follow general cross gcc building instructions (more on this on another day), but it didn't work. Using gcc bleeding edge of technology sometimes has its problems. Also there are some patches that must be applied to the source.
So I decided to follow the build script provided by the AVR-Freaks forum. You must become a member in order to download the script. It is still not the latest version of GCC 4.4.1, but it is recent enough (4.3.3) for ATMega328.
The pre-requisites are quite important, you can install binutils-devel (BFD support) from the Opensuse repositories and download the mpfr from here. Compile and install mpfr.

$./configure
$make
$su
#make install
#exit
$

Then run the scripts in order:

$ ./getfiles.sh
$ ./get-patches.sh
$ su
# ./buildavr-no-insight.sh
# ./buildinsight.sh
# exit
$

The build scripts ask you a couple of questions and in the end suggest some clean-up, once it starts you have time for few coffees...
Finally test your configuration:

$ avr-ld -v
GNU ld (GNU Binutils) 2.19.1 + coff-avr-patch (20050630)
$ avr-gcc -v
Using built-in specs.
Target: avr
Configured with: ../../source/gcc-4.3.3/configure -v --target=avr --disable-nls --prefix=/usr/local/avr --with-gnu-ld --with-gnu-as --enable-languages=c,c++ --disable-libssp --with-dwarf2
Thread model: single
gcc version 4.3.3 (GCC)

And you're good to go...

UPDATE: 2011.03.13
I recently installed OpenSUSE 11.3 and redid the installation. It works very well but you still need to do the following (these changes are, or may not be OpenSUSE specific):
- as su create and edit a /etc/bash.bashrc.local, add the following lines:

# User specific environment and startup programs
PREFIX=/usr/local/avr
export PREFIX

PATH=$PATH:$HOME/bin:$PREFIX/bin
export PATH

This will enable all users to access the build tools.
Then you need to do the following:

# chmod 777 /var/lock
# chmod 777 /dev/ttyUSB0
# chmod 777 /dev/ttyS0
# chmod 777 /dev/ttyS1

these last two are for my serialports.. I need to remind myself of doing it for minicom.
and finally create a udev rule for avrisp mk ii.
created a file 89-usbprog.rules in the rules.d directory (as su) and add the lines:

# udev rules file for some usb connected mcu programmers

# Atmel devices
#
ATTR{idVendor}=="03eb", ATTR{idProduct}=="2104", MODE="0666", GROUP="users", SYMLINK+="avrispmkII"
ATTR{idVendor}=="03eb", ATTR{idProduct}=="2103", MODE="0666", GROUP="users", SYMLINK+="avrunknown-%n"
ATTR{idVendor}=="03eb", ATTR{idProduct}=="2107", MODE="0666", GROUP="users", SYMLINK+="avrdragon-%n"

# Microchip devices
#
ATTR{idVendor}=="04d8", ATTR{idProduct}=="0033", MODE="0666", GROUP="users", SYMLINK+="pickit2"

as seen here with minor adaptations and your done...
test it with the arduino-IDE, compile and download the blink example.
test avrisp with a simple command:
avrdude -p t15 -c avrisp2 -P usb

PS. this solution also works

1 comment:

Jeronimo said...

Hi Joao, great tip about the avr-gcc compilation. I had the same problem with the 328 but I followed a confuse path to solve it.
Regards,
Jeronimo
www.blogdoje.com.br
AVR, Arduino & ARM