# Passerelle UART pour mettre a jour le firmware du module ST67W611M1 (voir gcc/ncp_loader/main.c).
# Makefile autonome : lance depuis la racine avec  make -f gcc/makefile_ncp_loader
# Produit gcc/ncp_loader/build/ncp_loader.{elf,hex}.

CC      := arm-none-eabi-gcc
OBJCOPY := arm-none-eabi-objcopy
SIZE    := arm-none-eabi-size

OUT     := gcc/ncp_loader/build
HAL     := Drivers/STM32H7RSxx_HAL_Driver

# Memes options que le Boot (gcc/makefile_boot) pour le coeur et les definitions de la puce.
ARCH    := -mcpu=cortex-m7 -mthumb -mfpu=fpv5-sp-d16 -mfloat-abi=hard
DEFS    := -DCORE_CM7 -DST -DSTM32H7S7xx -DUSE_HAL_DRIVER -DUSE_STM32H7S78_DK -DUSE_PWR_LDO_SUPPLY__N -DUSE_FLOATING_POINT
INCS    := -IBoot/Core/Inc \
           -I$(HAL)/Inc \
           -IDrivers/CMSIS/Include \
           -IDrivers/CMSIS/Device/ST/STM32H7RSxx/Include
CFLAGS  := $(ARCH) $(DEFS) $(INCS) -Os -g -Wall -std=gnu11 -ffunction-sections -fdata-sections -fno-exceptions
LDFLAGS := $(ARCH) -Os -g --specs=nosys.specs --specs=nano.specs -Wl,--gc-sections -static \
           -T Boot/stm32h7s7xx_flash.ld -Wl,-Map=$(OUT)/ncp_loader.map

SRCS := gcc/ncp_loader/main.c \
        Boot/Core/Src/system_stm32h7rsxx.c \
        $(HAL)/Src/stm32h7rsxx_hal.c \
        $(HAL)/Src/stm32h7rsxx_hal_cortex.c \
        $(HAL)/Src/stm32h7rsxx_hal_gpio.c \
        $(HAL)/Src/stm32h7rsxx_hal_pwr.c \
        $(HAL)/Src/stm32h7rsxx_hal_pwr_ex.c \
        $(HAL)/Src/stm32h7rsxx_hal_rcc.c \
        $(HAL)/Src/stm32h7rsxx_hal_rcc_ex.c \
        $(HAL)/Src/stm32h7rsxx_hal_dma.c \
        $(HAL)/Src/stm32h7rsxx_hal_dma_ex.c \
        $(HAL)/Src/stm32h7rsxx_hal_exti.c \
        $(HAL)/Src/stm32h7rsxx_hal_flash.c \
        $(HAL)/Src/stm32h7rsxx_hal_flash_ex.c
ASRC := gcc/startup_stm32h7s7l8hxh_boot.s

OBJS := $(SRCS:%.c=$(OUT)/%.o) $(ASRC:%.s=$(OUT)/%.o)

.PHONY: all clean
all: $(OUT)/ncp_loader.hex
	@$(SIZE) $(OUT)/ncp_loader.elf

$(OUT)/%.o: %.c
	@echo Compiling $<
	@mkdir -p $(@D)
	@$(CC) $(CFLAGS) -MMD -MP -c $< -o $@

$(OUT)/%.o: %.s
	@echo Assembling $<
	@mkdir -p $(@D)
	@$(CC) $(ARCH) -g -x assembler-with-cpp -c $< -o $@

$(OUT)/ncp_loader.elf: $(OBJS)
	@echo Linking $@
	@$(CC) $(LDFLAGS) $(OBJS) -o $@

$(OUT)/ncp_loader.hex: $(OUT)/ncp_loader.elf
	@$(OBJCOPY) -O ihex $< $@

clean:
	rm -rf $(OUT)

-include $(OBJS:.o=.d)
