# Generic AVR Makefile
# Brian Mayton <bmayton@media.mit.edu>
#
# This is a basic Makefile for building programs for the AVR.  I usually start
# with this for any new project; it's pretty versatile.
#
# 'make' builds the .hex file.
# 'make install' uses the programmer to load it onto the target chip.
# 'make fuses' programs the fuse bits on the target chip.
# 'make clean' removes all autogenerated files.

# OBJECTS should list all of the object files for the program (e.g. for each
# .c file, list a corresponding .o file here.)  APPLICATION just specifies the
# name of the hex/elf files, and can be whatever you want. 
# OBJECTS = led_blink.o
OBJECTS = blink.o
APPLICATION = application

# Set up which MCU you're using, and what programmer, and the clock speed
# here.
MCU = attiny44
PROGRAMMER = usbtiny
F_CPU = 20000000UL # 20MHz

# Make sure you check these fuse settings before actually using them, using
# fuse settings for the wrong clock setup or chip can make it hard to
# reprogram.  http://www.engbedded.com/fusecalc/ is very useful!
LFUSE=0x7E

# Edit these if you want to use different compilers
CC = avr-gcc
OBJCOPY = avr-objcopy
SIZE = avr-size
AVRDUDE = avrdude

# Compiler and linker flags
CFLAGS=-mmcu=$(MCU) -Wall -DF_CPU=$(F_CPU) -I. -funsigned-char \
	-funsigned-bitfields -fpack-struct -fshort-enums -Os
LDFLAGS=-mmcu=$(MCU)

all: $(APPLICATION).hex

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

%.hex: %.elf
	$(OBJCOPY) -j .text -j .data -O ihex $< $@

$(APPLICATION).elf: $(OBJECTS)
	$(CC) $(LDFLAGS) -o $@ $^
	$(SIZE) -C --mcu=$(MCU) $@

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

install: all
	$(AVRDUDE) -p $(MCU) -c $(PROGRAMMER) -P usb -e \
		-U flash:w:$(APPLICATION).hex

fuses:
	$(AVRDUDE) -p $(MCU) -c $(PROGRAMMER) -P usb -e \
		-U lfuse:w:$(LFUSE):m

.PHONY: all clean install fuses

