CC			= arm-none-eabi-gcc
LD			= arm-none-eabi-gcc
OBJCOPY		= arm-none-eabi-objcopy
LKR			= arm_lib.lkr
LTO			= -flto
CCFLAGS		= $(LTO) -march=armv6-m -mcpu=cortex-m0 -Os -g -ggdb3 -I. -ffunction-sections -fdata-sections -ffixed-r9 -DUSE_TRAPS=0 -fpic -Wno-multichar -Wall  -DBUILD_FOR_THUMB_1 -isystem ../dal/kernel/hw/RP2040/
LDFLAGS		= $(LTO) -Os -g -ggdb3 -Wl,--gc-sections -Wl,-T $(LKR) -march=armv6-m -mcpu=cortex-m0 -fpic -ffixed-r9

RSRC		= amdc0000
PRC			= uart_driver.prc
SRCS		+= main.c port.c libpalmos.c librepalm.c crt.S
SLIBS		=


#add SDK
CCFLAGS		+=  -I../../SDK
CCFLAGS		+=  -I../../SDK/Core
CCFLAGS		+=  -I../../SDK/Core/System
CCFLAGS		+=  -I../../SDK/Core/Hardware
CCFLAGS		+=  -I../../SDK/Core/UI
CCFLAGS		+=  -I../../SDK/Dynamic
CCFLAGS		+=  -I../../SDK/libraries

#add repalm
CCFLAGS		+=  -isystem ../dal


#resources
EXTRA_RSRCS	+= amdi0000.bin



#do not touch below
OBJS		= $(patsubst %.S,%.o,$(patsubst %.c,%.o,$(SRCS)))
INCS		= $(wildcard *.h)

$(PRC): $(RSRC).bin $(EXTRA_RSRCS)
	../../tools/mkrom/mkprc SerialDriver-generic vdrv uart $^ > $@

$(RSRC).bin: $(RSRC).elf  Makefile
	$(OBJCOPY) -I elf32-littlearm -O binary $(RSRC).elf $(RSRC).bin -j.vec -j.text -j.rodata -j.data

$(RSRC).elf: $(OBJS) $(LKR) Makefile $(SLIBS)
	$(LD) -o $(RSRC).elf $(LDFLAGS) $(OBJS) $(SLIBS)

libpalmos.c: ../dal/libpalmos.c
	cp $< $@

librepalm.c: ../dal/librepalm.c
	cp $< $@

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

crt.S: Makefile UartDriver.libdescr
	../../tools/libtools/crtgen.sh v6m UartDriver.libdescr >crt.S

clean:
	rm -f $(OBJS) $(RSRC).bin $(RSRC).elf $(PRC) crt.S libpalmos.c librepalm.c


