CROSS_COMPILE ?= arm-none-eabi-
CC = $(CROSS_COMPILE)gcc
OBJCOPY = $(CROSS_COMPILE)objcopy

OPT_FLAGS := -Os

ifeq ($(ENABLE_DEBUG), 1)
CFLAGS += -DDEBUG_ME
CFLAGS += -DENABLE_DEBUG
endif

ifeq ($(CUSTOM_SER), 1)
CFLAGS += -DCUSTOM_SER
endif

ifeq ($(CONSOLE_NO_AUTO_CRLF), 1)
CFLAGS += -DCONSOLE_NO_AUTO_CRLF
endif

ifeq ($(ENABLE_DEBUG), 1)
CFLAGS += -g3 -ggdb
endif

CFLAGS += -mthumb -mcpu=cortex-m0plus -DDFU_SERIAL_LENGTH=9 \
	-DSAMD -DSAMD21E17 -DBLACKMAGIC -I../libopencm3/include \
	-Iplatforms/samd -msoft-float -ffunction-sections -fdata-sections -MD

LINKER_SCRIPT="platforms/samd/samd.ld"
LDFLAGS = -mthumb -mcpu=cortex-m0plus -msoft-float -nostartfiles -lc \
	$(CPU_FLAGS) -T$(LINKER_SCRIPT) -Wl,--gc-sections \
	-L../libopencm3/lib -lopencm3_samd -lnosys -lm -lgcc

ifeq ($(ENABLE_DEBUG), 1)
LDFLAGS += --specs=rdimon.specs
else
LDFLAGS += --specs=nano.specs
endif

VPATH += platforms/samd

SRC +=	cdcacm.c	\
	timing.c        \
	traceswo.o      \
	usbuart.c       \

all:	blackmagic_full.bin blackmagic.bin blackmagic_dfu.bin blackmagic_dfu.hex

blackmagic_dfu.bin : OBJCOPY_FLAGS := --pad-to 0x00002000 --gap-fill 0xFF -j .text -j .data
blackmagic_dfu.bin : LINKER_SCRIPT := "platforms/samd/samd_boot.ld"

blackmagic_dfu.elf: usbdfu.o
	@echo "  LD      $@"
	$(Q)$(CC) $^ -o $@ $(LDFLAGS)

blackmagic_full.bin: blackmagic_dfu.bin blackmagic.bin
	@echo "  CAT     $@"
	$(Q)cp blackmagic.bin jeff-$(shell git describe --tags --always --dirty).bin
	$(Q)cat $^ > $@

host_clean:
	$(Q)$(RM) -f blackmagic.bin blackmagic_full.bin blackmagic_dfu blackmagic_dfu.bin blackmagic_dfu.hex jeff*.bin
