DEVICE = atmega644p
AVRDUDE = avrdude  
#F_CPU   = 16000000
F_CPU   = 2000000


CFLAGS = -O1 -ffreestanding -funroll-loops 
#CFLAGS +=-fpack-struct -mcall-prologues -fno-tree-scev-cprop -ffunction-sections -fdata-sections -fwhole-program
#CFLAGS += -fpack-struct -fwhole-program -fpack-struct -ffunction-sections -fdata-sections -mcall-prologues -fno-tree-scev-cprop -ffreestanding -Wl,--relax,--gc-sections
#CFLAGS += -fpack-struct -ffunction-sections -fdata-sections -Wl,--relax,--gc-sections


COMPILE = avr-gcc -Wall -I../common/ -I. $(CFLAGS)  -mmcu=$(DEVICE) -DF_CPU=$(F_CPU)


OBJECTS = ../common/nRF905/nRF905.cpp ../common/nRF905/nRF905_spi.cpp ../common/codecrypt/chacha.cpp ../common/cpusleep.cpp main.cpp 

all:	main.hex

.cpp.o:
	$(COMPILE) -std=c++11 -c $< -o $@
.c.o:
	$(COMPILE) -c $< -o $@
	
.S.o:
	$(COMPILE) -x assembler-with-cpp -c $< -o $@


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

install:	all
	$(AVRDUDE) -c avrisp2 -P usb -p $(DEVICE) -U flash:w:main.hex:i



clean:
	rm -f main.hex main.lst main.obj main.cof main.list main.map main.eep.hex main.bin *.o main.s

main.bin:	$(OBJECTS)
	$(COMPILE) -o main.bin $(OBJECTS)

main.hex:	main.bin
	rm -f main.hex main.eep.hex
	avr-objcopy -j .text -j .data -O ihex main.bin main.hex
	./checksize main.bin


disasm:	main.bin
	avr-objdump -d main.bin
