#
#	(c) 2021 Dmitry Grinberg   https://dmitry.gr
#	Non-commercial use only OR licensing@dmitry.gr
#

SOURCES		= loader2.c loader.S
LDFLAGS		= 
CCFLAGS		= -fno-math-errno -flto		#LTO does make things smaller

ZWT_ADDR = 0x20041ffc
CCFLAGS	+= -Os -Wall -Wextra -Werror -mthumb -fsingle-precision-constant -ffast-math -march=armv6-m -mcpu=cortex-m0plus -I. -mfloat-abi=soft
CCFLAGS	+= -ffunction-sections -fdata-sections -fomit-frame-pointer -Wno-unused-function -Wno-unused-parameter
CCFLAGS	+= -D"err_str(...)=pr(__VA_ARGS__)"
CCFLAGS	+= -DZWT_ADDR=$(ZWT_ADDR) -DTICKS_PER_SECOND=200000000U
LDFLAGS += -Wl,--gc-sections -Wl,-T $(LKR) -lm -Wl,--allow-multiple-definition 
CC		= arm-none-eabi-gcc
LKR		= linker_rp2040.lkr
CMD		= sudo CortexProg info targetsel 0x01002927 write $(APP).bin 0x10000000

#for lto
LDFLAGS += $(CCFLAGS)

APP		= bootloader

#no changes below please

OBJS	= $(patsubst %.c,%.o,$(patsubst %.S,%.o,$(SOURCES)))

all: $(APP).bin

$(APP).elf: $(OBJS)
	$(CC) -o $@ $^ $(LDFLAGS)

%.o : %.c Makefile
	$(CC) $(CCFLAGS) -c $< -o $@

%.o : %.S Makefile
	$(CC) $(CCFLAGS) -c $< -o $@

clean:
	rm -f $(APP) $(OBJS)

%.bin: %.elf
	arm-none-eabi-objcopy -O binary $< $@ -j.text -j.rodata -j.data -j.loader -j.ramcode
	rpbless < $@ > $@.tmp
	mv $@.tmp $@

flash: $(APP).bin
	$(CMD)

trace: $(APP).bin
	$(CMD) trace $(ZWT_ADDR)


