MCU=attiny2313
F_CPU=8000000
PROG=usbasp

CC=avr-g++
OBJCOPY=avr-objcopy
CFLAGS=-Wall -g -O -mmcu=${MCU} -DF_CPU=${F_CPU} -I. -std=gnu++11 

SRC=$(wildcard src/*.cpp)
LIB=$(wildcard lib/*/src/*.cpp)

default: clean build flash

build:
	mkdir -p bin
	${CC} ${CFLAGS} -o bin/main.bin  ${SRC} ${LIB} -w
	${CC} ${CFLAGS} -o bin/main.elf ${SRC} ${LIB} -w
	${OBJCOPY} -j .text -j .data -O ihex bin/main.bin bin/main.hex

flash:
	avrdude -V -p ${MCU} -c ${PROG} -U flash:w:bin/main.hex

clean:
	rm -f bin/*

debug: flash avarice
	avr-gdb -ex "target remote :4242" bin/main.elf

avarice:
	sleep 1
	avarice --file bin/main.elf --part ${MCU} --${DEBUGGER} :4242 &

term:
	screen /dev/ttyUSB0 1000000

eeprom:
	sh -c "avrdude -p ${MCU} -c ${PROG} -t << END \
	write eeprom 0 1 2 3 \
	dump eeprom 0 16 \
	quit \
	END"

fuse:
	avrdude -p ${MCU} -c ${PROG} -U lfuse:w:0xe4:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m 
lock:
	avrdude -p ${MCU} -c ${PROG} -U lock:w:0xC0:m