# Download CMSIS header files from Github on demand
CMSIS_CORE_VERSION ?= 5.9.0      # ARM Cortex-M definitions
CMSIS_CORE_REPO ?= https://github.com/ARM-software/CMSIS_5
CMSIS_DEVICE_VERSION ?= v1.2.8   # ST MCU peripheral definitions
CMSIS_DEVICE_REPO ?= https://github.com/STMicroelectronics/cmsis_device_f7

CFLAGS  = -W -Wall -Wextra -Werror -Wundef -Wshadow -Wdouble-promotion
CFLAGS += -Wformat-truncation -fno-common -Wconversion -Wno-sign-conversion
CFLAGS += -g3 -Os -ffunction-sections -fdata-sections
CFLAGS += -I. -Icmsis_core/CMSIS/Core/Include -Icmsis_device_f7/Include
CFLAGS += -mcpu=cortex-m7 -mthumb -mfloat-abi=hard -mfpu=fpv5-sp-d16
LDFLAGS ?= -Tlink.ld -nostdlib -nostartfiles --specs nano.specs -lc -lgcc -Wl,--gc-sections -Wl,-Map=$@.map

SOURCES = main.c syscalls.c sysinit.c

# Using ST-provided startup file. NOTE: this is compiler-dependant
SOURCES += cmsis_device_f7/Source/Templates/gcc/startup_stm32f746xx.s

# Mongoose-specific build flags and source code files
# Build options reference: https://mongoose.ws/documentation/#build-options
SOURCES += ../../../mongoose.c
SOURCES += ../../device-dashboard/net.c
SOURCES += ../../device-dashboard/packed_fs.c

CFLAGS += -I../../..
CFLAGS += -DMG_ARCH=MG_ARCH_NEWLIB
CFLAGS += -DMG_ENABLE_CUSTOM_MILLIS=1
CFLAGS += -DMG_ENABLE_CUSTOM_RANDOM=1
CFLAGS += -DMG_ENABLE_MIP=1
CFLAGS += -DMG_ENABLE_PACKED_FS=1
CFLAGS += $(CFLAGS_EXTRA)

# Build flashable .bin file
all build example: firmware.bin

# .bin file is made from .elf file, by concatenating .text and .data sections
firmware.bin: firmware.elf
	arm-none-eabi-objcopy -O binary $< $@

# .elf file is produced by compiling sources
firmware.elf: $(SOURCES) hal.h link.ld cmsis_core cmsis_device_f7
	arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@

# Flash .bin file to the target board via the built-in debugger
flash: firmware.bin
	st-flash --reset write $< 0x8000000

# Download ST's CMSIS headers with peripheral definitions
cmsis_device_f7/Source/Templates/gcc/startup_stm32f746xx.s: cmsis_device_f7
cmsis_device_f7:
	git clone --depth 1 -b $(CMSIS_DEVICE_VERSION) $(CMSIS_DEVICE_REPO) $@

# Download ARM's CMSIS headers with core Cortex-M definitions
cmsis_core:
	git clone --depth 1 -b $(CMSIS_CORE_VERSION) $(CMSIS_CORE_REPO) $@

# Requires env variable VCON_API_KEY set
DEVICE_URL ?= https://dash.vcon.io/api/v3/devices/5

# Upload firmware to a remote test device
update: firmware.bin
	curl --fail-with-body -su :$(VCON_API_KEY) $(DEVICE_URL)/ota --data-binary @$<

# Read serial port on a remote test device for 5 seconds, store in a
# temporary file, and check the output for expected patterns
test: CFLAGS_EXTRA += -DUART_DEBUG=USART1
test: update
	curl --fail-with-body -su :$(VCON_API_KEY) $(DEVICE_URL)/tx?t=5 | tee /tmp/output.txt
	grep 'READY, IP:' /tmp/output.txt       # Check for network init
	grep 'MQTT connected' /tmp/output.txt   # Check for MQTT connection success

clean:
	@rm -rf firmware.* *.su cmsis_core cmsis_device_f7
