DEVICE     = atmega64a
DEVICEDUDE = m64 # device passed to avrdude (this differs sometimes from the avr-gcc device name)
CLOCK      = 12000000
PROGRAMMER = -c dragon_jtag
FUSES      = -U hfuse:w:0xd9:m -U lfuse:w:0x24:m
SOURCES    = $(shell find -name '*.cpp' -or -name '*.S')
OBJECTS    = $(SOURCES:.cpp=.o)

AVRDUDE = avrdude $(PROGRAMMER) -p $(DEVICEDUDE)
COMPILE = avr-g++ -Wall -Os -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)
COMPILE += -I -I. -I./lib/
COMPILE += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
COMPILE += -ffunction-sections -fdata-sections -Wl,--gc-sections
COMPILE += -Wl,--relax -mcall-prologues
COMPILE += -fno-exceptions -std=c++14

C_OPTIONS = -std=gnu99 -Wstrict-prototypes

# symbolic targets:
all: $(SOURCES) main.hex

.cpp.o:
	$(COMPILE) -c $< -o $@

.S.o:
	$(COMPILE) -x assembler-with-cpp -c $< -o $@
# "-x assembler-with-cpp" should not be necessary since this is the default
# file type for the .S (with capital S) extension. However, upper case
# characters are not always preserved on Windows. To ensure WinAVR
# compatibility define the file type manually.

.cpp.s:
	$(COMPILE) -S $< -o $@

flash:	all
	$(AVRDUDE) -U flash:w:main.hex:i

# if you use a bootloader, change the command below appropriately:
load: all
	bootloadHID main.hex

clean:
	find -name '*.d' -exec rm {} +
	find -name '*.o' -exec rm {} +
	rm -f main.hex main.elf

# file targets:
main.elf: $(OBJECTS)
	$(COMPILE) -o main.elf $(OBJECTS)

main.hex: main.elf
	rm -f main.hex
	avr-objcopy -j .text -j .data -O ihex main.elf main.hex
	avr-size --format=avr --mcu=$(DEVICE) main.elf
# If you have an EEPROM section, you must also create a hex file for the
# EEPROM and add it to the "flash" target.

# Targets for code debugging and analysis:
disasm:	main.elf
	avr-objdump -d main.elf

cpp:
	$(COMPILE) -E main.c