Skip to content

Instantly share code, notes, and snippets.

@edwardhotchkiss
Created March 5, 2014 23:30
Show Gist options
  • Save edwardhotchkiss/9378977 to your computer and use it in GitHub Desktop.
Save edwardhotchkiss/9378977 to your computer and use it in GitHub Desktop.
ATtiny85 avr-gcc / avrdude Makefile
DEVICE = attiny85
CLOCK = 8000000
PROGRAMMER = stk500v1
PORT = /dev/tty.usbmodem1421
BAUD = 19200
FILENAME = main
COMPILE = avr-gcc -Wall -Os -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)
all: usb clean build upload
usb:
ls /dev/cu.*
build:
$(COMPILE) -c $(FILENAME).c -o $(FILENAME).o
$(COMPILE) -o $(FILENAME).elf $(FILENAME).o
avr-objcopy -j .text -j .data -O ihex $(FILENAME).elf $(FILENAME).hex
avr-size --format=avr --mcu=$(DEVICE) $(FILENAME).elf
upload:
avrdude -v -p $(DEVICE) -c $(PROGRAMMER) -P $(PORT) -b $(BAUD) -U flash:w:$(FILENAME).hex:i
clean:
rm main.o
rm main.elf
rm main.hex
@mikedom
Copy link

mikedom commented Mar 21, 2015

This is great. Thanks for the concise makefile!

@krmerchant
Copy link

Really Nice! Most useful Makefile I've used!

@vyorkin
Copy link

vyorkin commented Mar 17, 2020

avr-size --format=avr --mcu=atmega8a main.elf
avr-size: invalid argument to --format: avr

avr-size: supported targets: elf32-avr elf32-little elf32-big plugin srec symbolsrec verilog tekhex binary ihex
 △ learn/avr/makefile avr-size --version
GNU size (GNU Binutils) 2.31.1

According to man avr-size the --format option might be either berkley or sysv...
I'm on NixOS, pkgs installed:

    pkgsCross.avr.avrlibc
    pkgsCross.avr.buildPackages.binutils
    pkgsCross.avr.buildPackages.gcc

    avrdude
    avr8burnomat

    simavr
    avra

Here is how my fork of your Makefile currently looks like:

LIBC       := $(shell nix-build --no-out-link "<nixpkgs>" -A pkgsCross.avr.avrlibc)
ARGS       := -I $(LIBC)/avr/include -L$(LIBC)/avr/lib/avr4 

DEVICE     = m8
MCU        = atmega8
CLOCK      = 8000000
PROGRAMMER = avrisp2
PORT	   = usb:000200012345
BAUD       = 19200
FILENAME   = main
COMPILE    = avr-gcc -Wall -Os -DF_CPU=$(CLOCK) -mmcu=$(MCU) $(ARGS)

all: clean build upload
	
build:
	echo $(LIBC)
	$(COMPILE) -c $(FILENAME).c -o $(FILENAME).o
	$(COMPILE) -o $(FILENAME).elf $(FILENAME).o
	avr-objcopy -j .text -j .data -O ihex $(FILENAME).elf $(FILENAME).hex
	avr-size $(FILENAME).elf

upload:
	sudo avrdude -v -p $(DEVICE) -c $(PROGRAMMER) -P $(PORT) -b $(BAUD) -U flash:w:$(FILENAME).hex:i 

clean:
	rm -f main.o
	rm -f main.elf
	rm -f main.hex

.PHONY: all clean build upload

@scruss
Copy link

scruss commented Mar 28, 2020

@edwardhotchkiss - thank you! This is really simple and it works, unlike every other avr-gcc makefile on the web.

@vyorkin - your avr-size appears to be just regular GNU size, as I get a similar error on Ubuntu if I

size --format=avr --mcu=atmega8a main.elf

instead of

avr-size --format=avr --mcu=atmega8a main.elf

avr-size --version returns GNU size (GNU Binutils) 2.26.20160125 for me, though, and the avr-size binary is huge compared to the regular one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment