TARGET := stm32$(HARDWARE)


ifeq ($(DEBUG), 1)
	CFLAGS += -DDEBUG -g
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

ifeq ($(FLASH_ERASE), 1)
    	CFLAGS += -DFLASH_ERASE
endif

CFLAGS += -DPRINTF_$(shell echo $(PRINTF) | tr a-z A-Z)

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

INCLUDES = -I../config
INCLUDES += -I../stm32/stm32f4xx_spl/inc 
INCLUDES += -I../stm32/system
INCLUDES += -I../peripheral_modules/inc
INCLUDES += -I../user
INCLUDES += -I../thirdparty/TinyStdio
INCLUDES += -I../thirdparty/myStrings
INCLUDES += -I.

CSRC = $(wildcard ../stm32/stm32f4xx_spl/src/*.c)
CSRC += $(wildcard ../stm32/system/*.c)
CSRC += $(wildcard ../peripheral_modules/src/*.c)
CSRC += $(wildcard ../user/*.c)
CSRC += $(wildcard ../thirdparty/TinyStdio/*.c)
CSRC += $(wildcard ../thirdparty/myStrings/*.c)
CSRC += $(wildcard *.c)

INCLUDES += -Ileds
INCLUDES += -Ibuttons
INCLUDES += -Ijumper
INCLUDES += -Id_inouts
INCLUDES += -IMegaTec
INCLUDES += -Icommon
INCLUDES += -Imonitor
INCLUDES += -Ilog
INCLUDES += -Itesting
ifeq ($(HARDWARE), bt6702)
INCLUDES += -Iradius
endif
INCLUDES += -Imbedtls_api
CSRC += $(wildcard leds/*.c)
CSRC += $(wildcard buttons/*.c)
CSRC += $(wildcard jumper/*.c)
CSRC += $(wildcard d_inouts/*.c)
CSRC += $(wildcard MegaTec/*.c)
CSRC += $(wildcard common/*.c)
CSRC += $(wildcard monitor/*.c)
CSRC += $(wildcard log/*.c)
CSRC += $(wildcard testing/*.c)
ifeq ($(HARDWARE), bt6702)
CSRC += $(wildcard radius/*.c)
endif
CSRC += $(wildcard mbedtls_api/*.c)

CFLAGS += -DOS_FREERTOS

#FreeRTOS
LDSCRIPT := startup/stm32_flash.ld

INCLUDES +=  -I../thirdparty/FreeRTOS/include -I../thirdparty/FreeRTOS/portable/GCC/ARM_CM3
CSRC += $(wildcard ../thirdparty/FreeRTOS/*.c ../thirdparty/FreeRTOS/portable/GCC/ARM_CM3/*.c)
ASMSRC = startup/startup_stm32f4xx.s

# MemMang
CSRC += $(wildcard ../thirdparty/FreeRTOS/portable/MemMang/heap_4.c)
	
	
	# LwIP + Web #	
    INCLUDES += -IHTTP_Server
    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 += -I../thirdparty/lwip/src/include/netif
    INCLUDES += -I../thirdparty/lwip/src/apps/snmp/
#    INCLUDES += -I../thirdparty/lwip/src/netif/ppp
    INCLUDES += -I../thirdparty/lwip/port
    INCLUDES += -I../thirdparty/lwip/port/FreeRTOS
    INCLUDES += -I../thirdparty/lwip/system
    INCLUDES += -I../thirdparty/lwip/system/arch
    INCLUDES += -IEthernet
 # INCLUDES += -I../stm32/stm32f4x7_ethernet 
 	INCLUDES += -ISTM32F4x7_ETH_Driver
ifeq ($(HARDWARE), bt6702) 	
   CSRC += $(wildcard HTTP_Server/my_ssl_server.c)
endif
ifeq ($(HARDWARE), bt6706)  
      INCLUDES += -ITelnet_Server 
      CSRC += $(wildcard HTTP_Server/http_server.c)
      CSRC += $(wildcard Telnet_Server/*.c)
endif
    CSRC += $(wildcard HTTP_Server/trap_params.c)
    CSRC += $(wildcard HTTP_Server/web_params_api.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 ../thirdparty/lwip/src/apps/snmp/*.c)
#    CSRC += $(wildcard ../thirdparty/lwip/src/netif/ppp/*.c)
    CSRC += $(wildcard ../thirdparty/lwip/port/FreeRTOS/*.c)
    CSRC += $(wildcard Ethernet/*.c)

  #  CSRC += $(wildcard ../stm32/stm32f4x7_ethernet/*.c)
  	CSRC += $(wildcard STM32F4x7_ETH_Driver/*.c)
    
CSRC += $(wildcard ../stm32/system/syscalls/syscalls.c)

#SSL
#INCLUDES += -I../thirdparty/PolarSSL/include/polarssl/
#INCLUDES += -I../thirdparty/PolarSSL/include/

#CSRC += $(wildcard ../thirdparty/PolarSSL/library/*.c)

INCLUDES += -I../thirdparty/mbedTLS/include/

CSRC += $(wildcard ../thirdparty/mbedTLS/library/*.c)

ifeq ($(HARDWARE), bt6702)
#RADDIUS_SERVER

INCLUDES += -I../thirdparty/FreeRadius/
INCLUDES += -I../thirdparty/FreeRadius/include/
INCLUDES += -I../thirdparty/FreeRadius/lib/

CSRC += $(wildcard ../thirdparty/FreeRadius/lib/*.c)
endif
    
CFLAGS += -DUSE_STDPERIPH_DRIVER -DSTM32F40_41xxx -DLOG_ENABLE -DMBEDTLS_CONFIG_FILE='<mbedtls_config.h>'

ifeq ($(HARDWARE), bt6702)
BUILDDIR = ../build/bt6702/$(TARGET)
endif

ifeq ($(HARDWARE), bt6706)
BUILDDIR = ../build/bt6706/$(TARGET)
endif

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

ifeq ($(HARDWARE), bt6702)
FW_NAME = BT_6702xx
endif

ifeq ($(HARDWARE), bt6706)
FW_NAME = BT_6706xx
endif

-include ../Makefile.inc.stm32

#Building Web UI FS
ifeq ($(HARDWARE), bt6706)
	WUI_DIR = ../web_interface/dist/wui-6
	FSDATA_DIR = ./HTTP_Server/bt6706_fs
endif

ifeq ($(HARDWARE), bt6702)
	WUI_DIR = ../web_interface/dist/wui
	FSDATA_DIR = ./HTTP_Server/bt6702_fs
endif

$(FSDATA_DIR)/fsdata.c: $(WUI_DIR)/*
	@../docs/makefsdata.pl $(FSDATA_DIR) $(WUI_DIR)

prebuild: $(FSDATA_DIR)/fsdata.c
	@echo "Hardware version: $(HARDWARE)"
	
postbuild: $(BUILDDIR)/$(TARGET).bin
	@echo "FW start address: $(FW_FLASH_START)"
	@echo "FW CRC address: $(FW_FLASH_CRC)"
	@echo "Copying to: $(OUTPUTDIR)/$(FW_NAME).bin"
	@cp $(BUILDDIR)/$(TARGET).bin $(OUTPUTDIR)/$(FW_NAME).bin
	@$(BUILDDIR)/../../tools/cortex_crc $(OUTPUTDIR)/$(FW_NAME).bin $(FW_FLASH_START) $(FW_FLASH_CRC)
	
	
