TARGET := stm32$(HARDWARE)service


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 += -DBT6702_SERVICE

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../thirdparty/TinyStdio
INCLUDES += -IUser
INCLUDES += -IHardware
INCLUDES += -ICommands
INCLUDES += -ISettings
INCLUDES += -IConsole
INCLUDES += -Irs_485
INCLUDES += -I.

CSRC = $(wildcard ../stm32/stm32f4xx_spl/src/*.c)
CSRC += $(wildcard ../stm32/system/*.c)
CSRC += $(wildcard ../peripheral_modules/src/*.c)
CSRC += $(wildcard ../thirdparty/TinyStdio/*.c)
CSRC += $(wildcard User/*.c)
CSRC += $(wildcard Hardware/*.c)
CSRC += $(wildcard Commands/*.c)
CSRC += $(wildcard Settings/*.c)
CSRC += $(wildcard Console/*.c)
CSRC += $(wildcard rs_485/*.c)
CSRC += $(wildcard *.c)

INCLUDES += -Ileds
INCLUDES += -Ibuttons
INCLUDES += -I../modules/jumper
INCLUDES += -I../modules/d_inouts
INCLUDES += -I../modules/common/
INCLUDES += -I../modules/mbedtls_api
INCLUDES += -I../modules

CSRC += $(wildcard leds/*.c)
CSRC += $(wildcard buttons/*.c)
CSRC += $(wildcard ../modules/jumper/*.c)
CSRC += $(wildcard ../modules/d_inouts/*.c)
CSRC += $(wildcard ../modules/common/*.c)
#CSRC += $(wildcard ../modules/mbedtls_api/*.c)
CSRC += $(wildcard ../modules/*.c)

CFLAGS += -DOS_FREERTOS

#FreeRTOS
LDSCRIPT := ../modules/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 = ../modules/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/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 += -I../modules/STM32F4x7_ETH_Driver
    CSRC += $(wildcard HTTP_Server/http_server.c)
    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/core/snmp/*.c)
    CSRC += $(wildcard ../thirdparty/lwip/src/netif/*.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 ../modules/STM32F4x7_ETH_Driver/*.c)
    
CSRC += $(wildcard ../stm32/system/syscalls/syscalls.c)

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

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

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

CSRC += $(wildcard ../thirdparty/mbedTLS/library/*.c)  
    
CFLAGS += -DUSE_STDPERIPH_DRIVER -DSTM32F40_41xxx -DLOG_ENABLE -DMBEDTLS_CONFIG_FILE='<mbedtls_config.h>'
#-DPOLARSSL_CERTS_C
BUILDDIR = ../build/bt6702_service/$(TARGET)

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

FW_NAME = BT_6703xx_service

-include ../Makefile.inc.stm32

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