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/ipv4
INCLUDES += -Ilwip/port/STM32F4x7
INCLUDES += -Ilwip/port/STM32F4x7/Standalone
INCLUDES += -Ilwip/port/STM32F4x7/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 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

prebuild:
	@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)

