TARGET = iap

INCLUDES = -I../stm32/stm32f4xx_spl/inc 
INCLUDES += -I../stm32/system
INCLUDES += -I../config

#----
INCLUDES += -I.
INCLUDES += -I../thirdparty/lwip/src/include
INCLUDES += -I../thirdparty/lwip/src/include/netif
INCLUDES += -I../thirdparty/lwip/src/include/lwip
INCLUDES += -I../thirdparty/lwip/src/include/lwip/apps
#INCLUDES += -Ilwip/port/STM32F4x7
#INCLUDES += -Ilwip/port/STM32F4x7/Standalone
#INCLUDES += -Ilwip/port/STM32F4x7/arch
INCLUDES += -I../thirdparty/lwip/port/Standalone
INCLUDES += -I../thirdparty/lwip/system
INCLUDES += -I../thirdparty/lwip/system/arch
INCLUDES += -IModules
INCLUDES += -IModules/Ethernet
INCLUDES += -IModules/Console
INCLUDES += -I../stm32/stm32f4x7_ethernet
INCLUDES += -IUser
INCLUDES += -I../thirdparty/TinyStdio
#----

CSRC = $(wildcard *.c ../stm32/system/*.c)
CSRC += $(wildcard ../stm32/stm32f4xx_spl/src/*.c)

#----
CSRC += $(wildcard ../thirdparty/lwip/src/*.c)
CSRC += $(wildcard ../thirdparty/lwip/src/api/*.c)
CSRC += $(wildcard ../thirdparty/lwip/src/core/*.c)
CSRC += $(wildcard ../thirdparty/lwip/src/core/ipv4/*.c)
CSRC += $(wildcard ../thirdparty/lwip/src/netif/*.c)
#CSRC += $(wildcard lwip/port/STM32F4x7/Standalone/*.c)
CSRC += $(wildcard ../thirdparty/lwip/port/Standalone/*.c)
CSRC += $(wildcard Modules/*.c)
CSRC += $(wildcard Modules/Ethernet/httpserver.c)
CSRC += $(wildcard Modules/Ethernet/netconf.c)
CSRC += $(wildcard ../stm32/stm32f4x7_ethernet/*.c)
CSRC += $(wildcard User/*.c)
CSRC += $(wildcard ../thirdparty/TinyStdio/*.c)

#---

CSRC += $(wildcard syscalls/syscalls.c)

CFLAGS = -DUSE_STDPERIPH_DRIVER -DSTM32F40_41xxx -DPRINTF_CUSTOM
CFLAGS += -DHARDWARE_$(shell echo $(HARDWARE) | tr a-z A-Z)

ifeq ($(SWOTRACE), 1)
	CFLAGS += -DSWOTRACE
endif

ifeq ($(MAC),)
	#Print default MAC
	MAC = $(shell awk '/\#define DEVICE_MAC/{print $$3}' ./Modules/settings_api.h )
else
    #Set device MAC
	CFLAGS += -DDEVICE_MAC='"$(MAC)"'
endif


	LDSCRIPT := ../projects/gcc/stm32_flash.ld
	
	ASMSRC = ../projects/gcc/startup_stm32f4xx.s


BUILDDIR = ../build/iap/$(TARGET)

FW_FLASH_START = $(shell awk '/IAP_FLASH_FIRST_PAGE_ADDRESS/{print $$3}' ../config/common_config.h )
FW_FLASH_CRC = $(shell awk '/IAP_FLASH_CRC_ADDRESS/{print $$3}' ../config/common_config.h )

-include ../Makefile.inc.stm32

#Building Web UI FS
WUI_DIR = ../web_interface/dist/upload
FSDATA_DIR = ./Modules/Ethernet

$(FSDATA_DIR)/fsdata.c: $(WUI_DIR)/*
	mkdir -p $(FSDATA_DIR)
	../docs/makefsdata.pl $(FSDATA_DIR) $(WUI_DIR)


prebuild: $(FSDATA_DIR)/fsdata.c
	@grep -wl --include *.h --include *.c "DEVICE_MAC" ./ -R | xargs touch
	@grep -wl --include *.h --include *.c "SWOTRACE" ./ ../shared -R | xargs touch
	
postbuild:
	@echo "Device MAC address: $(MAC)"
	@echo "FW start address: $(FW_FLASH_START)"
	@echo "FW CRC address: $(FW_FLASH_CRC)"
	@$(BUILDDIR)/../../tools/cortex_crc $(OUTPUTDIR)/$(TARGET).bin $(FW_FLASH_START) $(FW_FLASH_CRC)