BOARD ?= xmas-snoopy
CROSS ?= riscv-none-embed-
CC = $(CROSS)gcc
OBJCOPY = $(CROSS)objcopy
ICEPROG = iceprog
DFU_UTIL = dfu-util

BOARD_DEFINE=BOARD_$(shell echo $(BOARD) | tr a-z\- A-Z_)
CFLAGS=-Wall -Wextra -Wno-unused-parameter -Os -march=rv32im -mno-div -mabi=ilp32 -ffreestanding -flto -nostartfiles -fomit-frame-pointer -Wl,--gc-section --specs=nano.specs -D$(BOARD_DEFINE) -I. -I../common

NO2USB_FW_VERSION=0
include ../../gateware/cores/no2usb/fw/fw.mk
CFLAGS += $(INC_no2usb)

LNK=lnk-app.lds

HEADERS=\
	config.h \
	console.h \
	i2c.h \
	led_ctrl.h \
	pmu.h \
	spi.h \
	usb_dev.h \
	usb_desc_ids.h \
	usb_str_app.gen.h \
	utils.h \
	$(NULL)

SOURCES=\
	start.S \
	console_dummy.c \
	i2c.c \
	led_ctrl.c \
	main.c \
	pmu.c \
	spi.c \
	usb_dev.c \
	usb_desc_app.c \
	utils.c \
	$(NULL)

HEADERS += $(HEADERS_no2usb)
SOURCES += $(SOURCES_no2usb)


all: xmas-snoopy.bin


xmas-snoopy.elf: $(LNK) $(HEADERS) $(SOURCES)
	$(CC) $(CFLAGS) -Wl,-Bstatic,-T,$(LNK),--strip-debug -o $@ $(SOURCES)


%.bin: %.elf
	$(OBJCOPY) -O binary $< $@

prog: xmas-snoopy.bin
	$(ICEPROG) -o 640k $<

dfuprog: xmas-snoopy.bin
ifeq ($(DFU_SERIAL),)
	$(DFU_UTIL) -R -d 1d50:6146 -a 1 -D $<
else
	$(DFU_UTIL) -R -S $(DFU_SERIAL) -a 1 -D $<
endif


clean:
	rm -f *.bin *.hex *.elf *.o *.gen.h

.PHONY: prog dfuprog clean
