PROJECT_NAME     := ACD52832Master
TARGETS          := nrf52832_xxaa
OUTPUT_DIRECTORY := _build

SDK_ROOT := ../../../nRF5_SDK_15.2.0
PROJ_DIR := ../../

# Comment if the project does not use DFU
#USE_ACONNO_DFU := YES

$(OUTPUT_DIRECTORY)/nrf52832_xxaa.out: \
  LINKER_SCRIPT  := ble_app_hrs_freertos_gcc_nrf52.ld

ACONNO_SRC_FILE = ../../config/buildConfig/aconnoSource.txt
ACONNO_SOURCE = $(shell cat ${ACONNO_SRC_FILE})
ACONNO_INC_FILE = ../../config/buildConfig/aconnoInclude.txt
ACONNO_INCLUDE = $(shell cat ${ACONNO_INC_FILE})

NORDIC_SRC_FILE = ../../config/buildConfig/nordicSource.txt
NORDIC_SOURCE = $(shell cat ${NORDIC_SRC_FILE})
NORDIC_INC_FILE = ../../config/buildConfig/nordicInclude.txt
NORDIC_INCLUDE = $(shell cat ${NORDIC_INC_FILE})

# Source files common to all targets
SRC_FILES +=                                            \
  $(ACONNO_SOURCE)                                      \
  $(NORDIC_SOURCE)                                      \
  $(SDK_ROOT)/modules/nrfx/mdk/gcc_startup_nrf52.S      \
  $(SDK_ROOT)/modules/nrfx/mdk/system_nrf52.c           \
  
# Include folders common to all targets
INC_FOLDERS +=                                          \
  $(ACONNO_INCLUDE)                                     \
  $(NORDIC_INCLUDE)                                     \
  $(PROJ_DIR)/nrf52832/                                 \
  $(PROJ_DIR)/nrf52832/config                                 \
  $(SDK_ROOT)/components/softdevice/s132/headers        \
  $(SDK_ROOT)/components/softdevice/s132/headers/nrf52  \

# Libraries common to all targets
LIB_FILES += \

# Optimization flags
OPT = -O3 -g3
# Uncomment the line below to enable link time optimization
#OPT += -flto

# C flags common to all targets
CFLAGS += $(OPT)
CFLAGS += -DBOARD_PCA10040
CFLAGS += -DFLOAT_ABI_HARD
CFLAGS += -DFREERTOS
CFLAGS += -DNRF52
CFLAGS += -DNRF52832_XXAA
CFLAGS += -DNRF52_PAN_74
CFLAGS += -DNRF_SD_BLE_API_VERSION=6
CFLAGS += -DS132
CFLAGS += -DSOFTDEVICE_PRESENT
CFLAGS += -mcpu=cortex-m4
CFLAGS += -mthumb -mabi=aapcs
CFLAGS += -Wall 
#CFLAGS += -Werror
CFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16
# keep every function in a separate section, this allows linker to discard unused ones
CFLAGS += -ffunction-sections -fdata-sections -fno-strict-aliasing
CFLAGS += -fno-builtin -fshort-enums
CFLAGS += -DDEBUG
CFLAGS += -DNRF_DFU_SVCI_ENABLED
CFLAGS += -DNRF_DFU_TRANSPORT_BLE=1
ifdef USE_ACONNO_DFU
CFLAGS += -DUSE_ACONNO_DFU
endif

# C++ flags common to all targets
CXXFLAGS += $(OPT)
CXXFLAGS += -DDEBUG

# Assembler flags common to all targets
ASMFLAGS += -g3
ASMFLAGS += -mcpu=cortex-m4
ASMFLAGS += -mthumb -mabi=aapcs
ASMFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16
ASMFLAGS += -DBOARD_PCA10040
ASMFLAGS += -DFLOAT_ABI_HARD
ASMFLAGS += -DFREERTOS
ASMFLAGS += -DNRF52
ASMFLAGS += -DNRF52832_XXAA
ASMFLAGS += -DNRF52_PAN_74
ASMFLAGS += -DNRF_SD_BLE_API_VERSION=6
ASMFLAGS += -DS132
ASMFLAGS += -DSOFTDEVICE_PRESENT
ASMFLAGS += -DNRF_DFU_SVCI_ENABLED
ASMFLAGS += -DNRF_DFU_TRANSPORT_BLE=1

# Linker flags
LDFLAGS += $(OPT)
LDFLAGS += -mthumb -mabi=aapcs -L$(SDK_ROOT)/modules/nrfx/mdk -T$(LINKER_SCRIPT)
LDFLAGS += -mcpu=cortex-m4
LDFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16
# let linker dump unused sections
LDFLAGS += -Wl,--gc-sections
# use newlib in nano version
LDFLAGS += --specs=nano.specs

nrf52832_xxaa: CFLAGS += -D__HEAP_SIZE=1024
nrf52832_xxaa: CFLAGS += -D__STACK_SIZE=2048
nrf52832_xxaa: ASMFLAGS += -D__HEAP_SIZE=1024
nrf52832_xxaa: ASMFLAGS += -D__STACK_SIZE=2048

# Add standard libraries at the very end of the linker input, after all objects
# that may need symbols provided by these libraries.
LIB_FILES += -lc -lnosys -lm -lstdc++


.PHONY: default help

# Default target - first one defined
default: nrf52832_xxaa

# Print all targets that can be built
help:
	@echo following targets are available:
	@echo		nrf52832_xxaa
	@echo		flash_softdevice
	@echo		sdk_config - starting external tool for editing sdk_config.h
	@echo		flash      - flashing binary
	@echo $(VARIABLE)
TEMPLATE_PATH := $(SDK_ROOT)/components/toolchain/gcc


include $(TEMPLATE_PATH)/Makefile.common

$(foreach target, $(TARGETS), $(call define_target, $(target)))
  
.PHONY: flash flash_softdevice erase

# Flash the program
flash_app: default
	@echo Flashing: $(OUTPUT_DIRECTORY)/nrf52832_xxaa.hex
	nrfjprog -f nrf52 --program $(OUTPUT_DIRECTORY)/nrf52832_xxaa.hex --sectorerase
	nrfjprog -f nrf52 --reset

# Flash softdevice
flash_softdevice:
	@echo Flashing: s132_nrf52_6.1.0_softdevice.hex
	nrfjprog -f nrf52 --program $(SDK_ROOT)/components/softdevice/s132/hex/s132_nrf52_6.1.0_softdevice.hex --sectorerase
	nrfjprog -f nrf52 --reset

# Flash the program and softdevice
flash_all:
	@echo Flashing: $(OUTPUT_DIRECTORY)/$(PROJECT_NAME).hex
	nrfjprog -f nrf52 --program $(OUTPUT_DIRECTORY)/$(PROJECT_NAME).hex --sectorerase
	nrfjprog -f nrf52 --reset

# Merge the program and softdevice
merge_soft:
	@echo Merging: $(OUTPUT_DIRECTORY)/nrf52832_xxaa.hex and s132_nrf52_6.1.0_softdevice.hex
	mergehex -m $(SDK_ROOT)/components/softdevice/s132/hex/s132_nrf52_6.1.0_softdevice.hex $(OUTPUT_DIRECTORY)/nrf52832_xxaa.hex -o $(OUTPUT_DIRECTORY)/$(PROJECT_NAME).hex

# Merge four hexes 
merge_all:
ifdef USE_ACONNO_DFU
	@echo Merging I:
	@echo DFU bootloader: $(SDK_ROOT)/examples/dfu/secure_bootloader/pca10040_ble_debug/armgcc/_build/nrf52832_xxaa_s132.hex
	@echo Application hex: $(OUTPUT_DIRECTORY)/nrf52832_xxaa.hex
	mergehex -m $(SDK_ROOT)/components/aconno/nrf52832_xxaa_s132.hex $(OUTPUT_DIRECTORY)/nrf52832_xxaa.hex -o $(OUTPUT_DIRECTORY)/mergedBLandApp.hex
	@echo 
	@echo Merging II:
	@echo Bootloader settings page: $(SDK_ROOT)/components/aconno/bootloaderSettings.hex
	@echo SoftDevice: $(SDK_ROOT)/components/softdevice/s132/hex/s132_nrf52_6.1.0_softdevice.hex
	@echo Previously merged hex: $(OUTPUT_DIRECTORY)/mergedBLandApp.hex
	mergehex -m $(SDK_ROOT)/components/aconno/bootloaderSettings.hex $(SDK_ROOT)/components/softdevice/s132/hex/s132_nrf52_6.1.0_softdevice.hex $(OUTPUT_DIRECTORY)/mergedBLandApp.hex -o $(OUTPUT_DIRECTORY)/$(PROJECT_NAME).hex
else
	make merge_soft
endif

all:
	make merge_all
	make flash_all

erase:
	nrfjprog -f nrf52 --eraseall

SDK_CONFIG_FILE := ../config/sdk_config.h
CMSIS_CONFIG_TOOL := $(SDK_ROOT)/external_tools/cmsisconfig/CMSIS_Configuration_Wizard.jar
sdk_config:
	java -jar $(CMSIS_CONFIG_TOOL) $(SDK_CONFIG_FILE)
