I do so little AVR development these days that when I start again, it takes me a short while to get back into the swing of things. So rather than digging out old notes, I've reproduced the main elements here.
To compile AVR C programs on linux you need the avr-gcc toolchain, along with a few other utilities. On Fedora one installs all of the packages required with:
$ sudo yum install avr-gcc avr-gcc avr-gdb avr-libc avrdude avr-binutils
This installs the following packages from one of the Fedora repos.
- avr-binutils.x86_64 : Cross Compiling GNU binutils targeted at avr;
- avr-gcc : Cross Compiling GNU GCC targeted at avr;
- avr-gdb : GDB for (remote) debugging avr binaries;
- avr-libc : C library for use with GCC on Atmel AVR microcontrollers;
- avrdude : Software for programming Atmel AVR Microcontroller.
Build .hex Image
Once these are installed the easiest way to build applications is by creating a 'Makefile' in the root of the project directory and using automake. Makefiles for the avr platform can be found all over the web, but my favorite is one written by Eric B. Weddington, Jorg Wunsch, et al. This file can be downloaded here: Makefile. Update the file to include the name of the object file, and sources files as outlined below:
... 70 71 # Target file name (without extension). 72 TARGET = main ... 79 80 # List C source files here. (C dependencies are automatically generated.) 81 SRC = $(TARGET).c \ foo.c \ bar.c ...
Running 'make' in the project directory should now build the project and provide a .hex file which can be flashed to the device. The makefile above will build an object file, main.hex, from the source files foo.c, bar.c and main.c.
Now we have our device image, the main.hex file, we can flash this to the device using AVRDUDE. In my case I have a simple variant of the USBasp -USB programmer for Atmel AVR controllers, and as such, flash the main.hex to the device with the following command:
$ avrdude -p m48 -c usbasp -U flash:w:main.hex
We can also program the high and low fuses straight from the command line with the following command:
sudo avrdude -p m48 -c usbasp -U lfuse:w:0xE2:m
This will program the low fuse with the immediate(m) byte 0xE2. To program the high fuse change 'lfuse' to 'hfuse'.