AVR on linux

From OmegaV-wiki
Jump to: navigation, search

Toolchain

First of all, you will need a toolchain (compiler, linker, libraries etc) to build your source code to executables runnable by the avr architecture. Most linux distributions have a binary package for this.

To flash your fancy compiled executable to your AVR's memory, you can use the avrdude tool.

To install all of these on arch linux:

pacman -S avr-gcc avr-binutils avr-libc avrdude

Permission problems

A lot of people experience problems with permissions when using avr programmers. This is relatively easily fixed with some udev rules.

Additionally, when attaching an avr that acts as a virtual serial port (i.e. uart->usb->serial), the kernel will similarly mount these up as root. An easy fix is to add this rule to e.g /etc/udev/rules.d/50-serial_port.rule

SUBSYSTEMS=="tty", GROUP=="users", MODE="0666"

Then do a sudo udevadm control --reload and re-attach your AVR.

Code::Blocks

Code::Blocks is an excellent IDE is for AVR programming on linux. http://www.codeblocks.org

... but nothing beats good ol' vim

Makefile example

For the hardcore hacker only a proper makefile is good enough. The following is an example makefile for compiling, linking, packing and flashing an AVR microcontroller.

TIP: to get your mcu name type:

avrdude -p ?

Similarly, type

avrdude -c ?

to get a list of valid programmers (e.g jtagmkII)

Be sure to change the following variables:

* jtagmkII to your programmer (stk500/600 etc)
* MCU Target architecture type for compiling
* MCUSHORT Target type for flashing
* And of course the source list

And read up on avrdude if you need. <source lang=bash>

  1. Makefile, for AVR Programming


  1. Target name:

TARGET = output

  1. Target type

MCU=atmega162 PROGRAMMER=jtagmkII F_CPU=4915200UL


SRC = main.c uart.c OBJ = $(SRC:.c=.o)


  1. Compiler / Linker flags:

CFLAGS = -mmcu=$(MCU) -Wall -Os -std=gnu99 -D F_CPU=$(F_CPU) -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums LDFLAGS = -mmcu=$(MCU) -Wl,-Map=$(TARGET).map


  1. Makerules:


.PHONY: compile flash clean

compile: $(TARGET).hex $(TARGET).eep $(TARGET).lss

flash: compile avrdude -c $(PROGRAMMER) -P usb -p $(MCU) -U flash:w:$(TARGET).hex # sleep 2 # avrdude -c $(PROGRAMMER) -P usb -p $(MCU) -U eeprom:w:$(TARGET).eep

clean: rm -f $(OBJ) $(TARGET).{elf,hex,lss,map,eep}


  1. Psudorules:

%.eep: %.hex avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex $< $@

%.hex: %.elf avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@

%.lss: %.elf avr-objdump -h -S $< > $@

%.elf: $(OBJ) avr-gcc $^ $(LDFLAGS) -o $@

%.o : %.c avr-gcc $(CFLAGS) -c $< -o $@

</source>