Skip to main content

more options

ATMega Programming on GNU/Linux

We decided to abandon HP InfoTech's CodeVisionAVR compiler/IDE used in class in favor of an free software/open source toolchain based around avr-binutils, avr-gcc, and avr-libc for compilation, avrdude for programming the board, and GNU Make with GNU Emacs in place of an "IDE." We were running the Ubuntu flavor of the GNU/Linux operating system. This page describes our toolchain; it should be possible to replicate a similar workflow on Windows with the WinAVR bundle.

Hardware Requirements

The task of hooking up our linux box to the microcontroller proved deceptively difficult. An intriguing option was the AVR Dragon, which has a USB interface to its programming capabilities. For one reason or another, we couldn't hack it, even using AVRStudio in lab, so we returned to the tried and true STK500; we found a broken STK500 in lab that still was able to successfully program an external chip.

We needed a USB to Serial adapter to get between my laptop and the board, which turned into quite an ordeal. Not a store in Ithaca sold an affordable adapter, and we had to return all of the expensive ones we tried due to linux incompatibility. This one for $7.49 from Newegg worked perfectly, showing up as /dev/ttyUSB0 in Ubuntu after uninstalling the "brltty" braille support package, which inexplicably interferes with USB to Serial operation.

Software Requirements

Getting the requisite software in order from a terminal was a piece of cake. First, we removed the brltty package, as discussed above:

sudo apt-get remove brltty

Then, we installed everything we needed:

sudo apt-get install avr-libc binutils-avr gcc-avr avrdude make

Hello, World: Blinking an LED

We will get started with a blinking light. Here's the code:

 * blink_led.c
 * Toggles all of PORTD twice a second--this blinks the LED on the
 * ECE476 development board.
 * By Robert Ochshorn and Kyle Wesson

#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>

#define LED_TIME_RESET 500      /* toggle LED every x times through
                                   the timer loop */
int led_time;                   /* accumulator for LED timing */

/* this ISR will be called 1000 times a second */
ISR (TIMER0_OVF_vect) {
    if(--led_time == 0) {       /* decrement led_time variable and
                                   check for 0 */
        led_time = LED_TIME_RESET; /* reset led_time */
        PORTD ^= 0xff;          /* toggle PORTD */
int main(void) {
    DDRD = 0xff;                /* drive PORTD to output */
    PORTD = 0x00;               /* initialize LED on (active low)*/
    led_time = LED_TIME_RESET;  /* initialize LED reset scalar */

    OCR0 = 250;                 /* 16e6 / (250*64) = 1000 -- this
                                   gives us a 1ms timebase with our
                                   interrupt. */

    TCCR0 = 1<<WGM01 | 1<<WGM00 | /* fast PWM mode */
        1<<COM01 |                /* clear OC0 on compare match */
        0<<CS02 | 1<<CS01 | 1<<CS00; /* clk/64 prescaler */

      TIMSK = 1<<TOIE0;         /* timer/counter0 overflow interrupt
                                   enable */

    sei ();                     /* enable ISRs */
    while(1) {
                                /* do nothing more...our interrupt
                                   will do all the work */

There are several differences between this code and CodeVision C. First, the includes:

    #include <inttypes.h>
    #include <avr/io.h>
    #include <avr/interrupt.h>

On my system, these files can be found in /usr/avr/include/ -- <avr/io.h> will, when compiling with a mega32 target, include /usr/avr/include/avr/iom32.h, which defines all the port and pin names. The interrupts are also slightly different syntactically and it's worth consulting iom32.h for the exact names of the interrupt vectors.

Programming the Chip

First, we need to compile the code down to hex:

  avr-gcc -c -mmcu=atmega32 -g -Os blink_led.c
  avr-gcc -g -mmcu=atmega32 -o blink_led.elf blink_led.o
  avr-objcopy -j .text -j .data -O ihex blink_led.elf blink_led.hex

Then, we check our connections and zap our hex onto the mega32. This command is based on using a USB to Serial connection into the STK500, but can easily be modified for other configurations of programmers and chips.

  avrdude -p atmega32 -P /dev/ttyUSB0 -c stk500  -v -v  -U flash:w:blink_led.hex 

This is fine once, but running all these commands repeatedly isn't such a good time. Here's where GNU Make comes in. Based on the excellent AVR-GCC Makefile Generator utility, MFile, we were able to specify all of the commands and dependency relations in a text document called a Makefile. Once the Makefile is saved in the same directory as blink_led.c, we can simply run the command:

  make program

This Makefile will do everything from compiling the code, building the hex, and loading it onto the chip. Better yet, from within GNU Emacs, we can simply type "M-x compile" and can program the chip without even leaving emacs, our venerable text editor, psychotherapist, web browser, email client, friend, and, now, embedded programming IDE:

# AVR-GCC Makefile template, derived from the WinAVR template (which
# is public domain), believed to be neutral to any flavor of "make"
# (GNU make, BSD make, SysV make)

MCU = atmega32
FORMAT = ihex
TARGET = blink_led
OPT = s
#OPT = 3

# Name of this Makefile (used for "make depend").
MAKEFILE = Makefile

# Debugging format.
# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
# AVR (extended) COFF requires stabs, plus an avr-objcopy run.
DEBUG = stabs

# Compiler flag to set the C Standard level.
# c89   - "ANSI" C
# gnu89 - c89 plus GCC extensions
# c99   - ISO C99 standard (not yet fully implemented)
# gnu99 - c99 plus GCC extensions
CSTANDARD = -std=gnu99

# Place -D or -U options here

# Place -I options here

CWARN = -Wall -Wstrict-prototypes
CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
#CEXTRA = -Wa,-adhlns=$(<:.c=.lst)

#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs 

#Additional libraries.

# Minimalistic printf version
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min

# Floating point printf version (requires MATH_LIB = -lm below)
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt


# Minimalistic scanf version
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min

# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt


MATH_LIB = -lm

# External memory options

# 64 KB of external RAM, starting after internal RAM (ATmega128!),
# used for variables (.data/.bss) and heap (malloc()).
#EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff

# 64 KB of external RAM, starting after internal RAM (ATmega128!),
# only used for heap (malloc()).
#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff


#LDMAP = $(LDFLAGS) -Wl,-Map=$(TARGET).map,--cref

# Programming support using avrdude. Settings and variables.



# Uncomment the following if you want avrdude's erase cycle counter.
# Note that this counter needs to be initialized first using -Yn,
# see avrdude manual.

# Uncomment the following if you do /not/ wish a verification to be
# performed after programming the device.

# Increase verbosity level.  Please use this when submitting bug
# reports about avrdude. See  
# to submit bug reports.


CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
NM = avr-nm
AVRDUDE = avrdude
REMOVE = rm -f
MV = mv -f

# Define all object files.
OBJ = $(SRC:.c=.o) $(ASRC:.S=.o) 

# Define all listing files.
LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)

# Combine all necessary flags and optional flags.
# Add target processor to flags.
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS)
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)

# Default target.
all: build

build: elf hex eep

elf: $(TARGET).elf
hex: $(TARGET).hex
eep: $(TARGET).eep
lss: $(TARGET).lss 
sym: $(TARGET).sym

# Program the device.  
program: $(TARGET).hex $(TARGET).eep

# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
COFFCONVERT=$(OBJCOPY) --debugging \
--change-section-address .data-0x800000 \
--change-section-address .bss-0x800000 \
--change-section-address .noinit-0x800000 \
--change-section-address .eeprom-0x810000 

coff: $(TARGET).elf
	$(COFFCONVERT) -O coff-avr $(TARGET).elf $(TARGET).cof

extcoff: $(TARGET).elf
	$(COFFCONVERT) -O coff-ext-avr $(TARGET).elf $(TARGET).cof

.SUFFIXES: .elf .hex .eep .lss .sym

	$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@

	-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
	--change-section-lma .eeprom=0 -O $(FORMAT) $< $@

# Create extended listing file from ELF output file.
	$(OBJDUMP) -h -S $< > $@

# Create a symbol table from ELF output file.
	$(NM) -n $< > $@

# Link: create ELF output file from object files.
$(TARGET).elf: $(OBJ)
	$(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS)

# Compile: create object files from C source files.
	$(CC) -c $(ALL_CFLAGS) $< -o $@ 

# Compile: create assembler files from C source files.
	$(CC) -S $(ALL_CFLAGS) $< -o $@

# Assemble: create object files from assembler source files.
	$(CC) -c $(ALL_ASFLAGS) $< -o $@

# Target: clean project.
	$(REMOVE) $(TARGET).hex $(TARGET).eep $(TARGET).cof $(TARGET).elf \
	$(TARGET).map $(TARGET).sym $(TARGET).lss \
	$(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d)

	if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \
	then \
		sed -e '/^# DO NOT DELETE/,$$d' $(MAKEFILE) > \
			$(MAKEFILE).$$$$ && \
		$(MV) $(MAKEFILE).$$$$ $(MAKEFILE); \
	echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \
		>> $(MAKEFILE); \
	$(CC) -M -mmcu=$(MCU) $(CDEFS) $(CINCS) $(SRC) $(ASRC) >> $(MAKEFILE)

.PHONY:	all build elf hex eep lss sym program coff extcoff clean depend