TARGET=i2c-cm3-usb

PREFIX  ?= arm-none-eabi-
CC      := $(PREFIX)gcc
SIZE    := $(PREFIX)size
OBJCOPY := $(PREFIX)objcopy

CFLAGS+= -mcpu=cortex-m0 -mthumb
CFLAGS+= -ffunction-sections -fdata-sections -fstack-usage
CFLAGS+= -I./libopencm3/include
CFLAGS+= -DSTM32F0

LDFLAGS+= -L./libopencm3/lib
LDFLAGS+= -T stm32f042x6.ld
LDFLAGS+= -static -nostartfiles
LDFLAGS+= -Wl,--gc-sections

LDLIBS+= -lopencm3_stm32f0

all: $(TARGET).bin

$(TARGET).elf: $(TARGET).c
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LDLIBS)

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

flash: $(TARGET).bin
	dfu-util -a 0 -s 0x08000000:leave -D $<
#	st-flash write $< 0x08000000
#	openocd -f interface/stlink.cfg -f target/stm32f0x.cfg -c "program $< verify reset exit 0x08000000"

size: $(TARGET).elf
	$(SIZE) $<

tar:
	-rm *.elf *.bin *.su
	tar -caf ../$(TARGET).tar.xz -C .. $(notdir $(CURDIR))

clean:
	rm *.elf *.bin *.su
