#######################################################################################

# environment variable of the current user to locate the AVR8 toolchain
AVRPATH = $(AVR8TOOLCHAINBINDIR)

# the type of avr microcontroller
DEVICE = atxmega128a3u

# the frequency the microcontroller is clocked with
F_CPU = 2000000

# where the firmware should be located within the flashmemory (in case you trampoline)
FLASHADDRESS = 0x0000

#######################################################################################



# Tools:
ECHO=@echo
GCC=gcc
MAKE=@make
RM=@rm -f

CC=$(AVRPATH)avr-gcc
LD=$(AVRPATH)avr-ld
OBC=@$(AVRPATH)avr-objcopy
OBD=@$(AVRPATH)avr-objdump
SIZ=@$(AVRPATH)avr-size

MYCFLAGS = -Wall -Os -g3 -ggdb -fno-move-loop-invariants -fno-tree-scev-cprop -fno-inline-small-functions -ffunction-sections -fdata-sections -I. -mmcu=$(DEVICE) -DF_CPU=$(F_CPU) $(CFLAGS)   $(DEFINES)
MYLDFLAGS = -Wl,--relax,--gc-sections -Wl,--section-start=.text=$(FLASHADDRESS) $(LDFLAGS)



all:  FIRMWARE.BIN

example.o: example.c
	$(CC) example.c -c -o example.o $(MYCFLAGS)

example.elf: example.o  apipage.a
	$(CC) example.o apipage.a -o example.elf $(MYCFLAGS) -Wl,-Map,example.map $(MYLDFLAGS)
	$(ECHO) "."
	$(SIZ) example.elf
	$(ECHO) "."

example.hex: example.elf
	$(OBC) -j .text -j .data -O ihex example.elf example.hex

FIRMWARE.BIN: example.elf example.asm
	$(OBC) -j .text -j .data -O binary example.elf FIRMWARE.BIN

example.asm: example.elf
	$(OBD) -d example.elf > example.asm

disasm: example.elf
	$(OBD) -d example.elf

flash: example.hex
	$(ECHO) "."
	$(AVRDUDE) -U flash:w:example.hex:i
	$(ECHO) "."

deepclean: clean
	$(RM) *~

clean:
	$(RM) *.o
	$(RM) *.raw
	$(RM) *.BIN
	$(RM) *.hex
	$(RM) *.asm
	$(RM) *.map
	$(RM) *.elf
