NORDIC_TOOLS := C:/nordic_tools/nRF51_SDK9_0
SRC_PATH := C:/Firmware/strobe/src
PROJECT_NAME := strobe

export OUTPUT_FILENAME
#MAKEFILE_NAME := $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
MAKEFILE_NAME := $(MAKEFILE_LIST)
MAKEFILE_DIR := $(dir $(MAKEFILE_NAME) ) 

TEMPLATE_PATH = $(NORDIC_TOOLS)/components/toolchain/gcc
ifeq ($(OS),Windows_NT)
include $(TEMPLATE_PATH)/Makefile.windows
else
include $(TEMPLATE_PATH)/Makefile.posix
endif

MK := mkdir
RM := rm -rf

#echo suspend
ifeq ("$(VERBOSE)","1")
NO_ECHO := 
else
NO_ECHO := @
endif

# Toolchain commands
CC       		:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-gcc"
AS       		:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-as"
AR       		:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-ar" -r
LD       		:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-ld"
NM       		:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-nm"
OBJDUMP  		:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-objdump"
OBJCOPY  		:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-objcopy"
SIZE    		:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-size"

#function for removing duplicates in a list
remduplicates = $(strip $(if $1,$(firstword $1) $(call remduplicates,$(filter-out $(firstword $1),$1))))

#source common to all targets
C_SOURCE_FILES += \
$(abspath $(NORDIC_TOOLS)/components/libraries/button/app_button.c) \
$(abspath $(NORDIC_TOOLS)/examples/bsp/bsp_btn_ble.c) \
$(abspath $(NORDIC_TOOLS)/components/libraries/util/app_error.c) \
$(abspath $(NORDIC_TOOLS)/components/libraries/fifo/app_fifo.c) \
$(abspath $(NORDIC_TOOLS)/components/libraries/timer/app_timer.c) \
$(abspath $(NORDIC_TOOLS)/components/libraries/trace/app_trace.c) \
$(abspath $(NORDIC_TOOLS)/components/libraries/util/nrf_assert.c) \
$(abspath $(NORDIC_TOOLS)/components/libraries/uart/retarget.c) \
$(abspath $(NORDIC_TOOLS)/components/libraries/sensorsim/sensorsim.c) \
$(abspath $(NORDIC_TOOLS)/components/drivers_nrf/uart/app_uart_fifo.c) \
$(abspath $(NORDIC_TOOLS)/components/drivers_nrf/hal/nrf_delay.c) \
$(abspath $(NORDIC_TOOLS)/components/drivers_nrf/common/nrf_drv_common.c) \
$(abspath $(NORDIC_TOOLS)/components/drivers_nrf/gpiote/nrf_drv_gpiote.c) \
$(abspath $(NORDIC_TOOLS)/components/drivers_nrf/pstorage/pstorage.c) \
$(abspath $(NORDIC_TOOLS)/examples/bsp/bsp.c) \
$(abspath $(NORDIC_TOOLS)/components/ble/common/ble_advdata.c) \
$(abspath $(NORDIC_TOOLS)/components/ble/ble_advertising/ble_advertising.c) \
$(abspath $(NORDIC_TOOLS)/components/ble/ble_services/ble_bas/ble_bas.c) \
$(abspath $(NORDIC_TOOLS)/components/ble/common/ble_conn_params.c) \
$(abspath $(NORDIC_TOOLS)/components/ble/ble_services/ble_dis/ble_dis.c) \
$(abspath $(NORDIC_TOOLS)/components/ble/ble_services/ble_hrs/ble_hrs.c) \
$(abspath $(NORDIC_TOOLS)/components/ble/common/ble_srv_common.c) \
$(abspath $(NORDIC_TOOLS)/components/ble/device_manager/device_manager_peripheral.c) \
$(abspath $(NORDIC_TOOLS)/components/toolchain/system_nrf51.c) \
$(abspath $(NORDIC_TOOLS)/components/softdevice/common/softdevice_handler/softdevice_handler.c) \
$(abspath $(NORDIC_TOOLS)/components/ble/ble_services/ble_dfu/ble_dfu.c) \
$(abspath $(NORDIC_TOOLS)/components/libraries/bootloader_dfu/dfu_app_handler.c) \
$(abspath $(NORDIC_TOOLS)/components/libraries/bootloader_dfu/bootloader_util.c) \

#assembly files common to all targets
ASM_SOURCE_FILES  = $(abspath $(NORDIC_TOOLS)/components/toolchain/gcc/gcc_startup_nrf51.s)

#includes common to all targets
INC_PATHS  = -I$(NORDIC_TOOLS)/examples/ble_peripheral/ble_app_hrs/config
INC_PATHS += -I$(NORDIC_TOOLS)/components/drivers_nrf/config
INC_PATHS += -I$(NORDIC_TOOLS)/examples/bsp
INC_PATHS += -I$(NORDIC_TOOLS)/components/libraries/fifo
INC_PATHS += -I$(NORDIC_TOOLS)/components/libraries/util
INC_PATHS += -I$(NORDIC_TOOLS)/components/drivers_nrf/pstorage
INC_PATHS += -I$(NORDIC_TOOLS)/components/drivers_nrf/uart
INC_PATHS += -I$(NORDIC_TOOLS)/components/ble/common
INC_PATHS += -I$(NORDIC_TOOLS)/components/libraries/sensorsim
INC_PATHS += -I$(NORDIC_TOOLS)/components/ble/device_manager
INC_PATHS += -I$(NORDIC_TOOLS)/components/ble/ble_services/ble_dis
INC_PATHS += -I$(NORDIC_TOOLS)/components/device
INC_PATHS += -I$(NORDIC_TOOLS)/components/libraries/button
INC_PATHS += -I$(NORDIC_TOOLS)/components/libraries/timer
INC_PATHS += -I$(NORDIC_TOOLS)/components/softdevice/s110/headers
INC_PATHS += -I$(NORDIC_TOOLS)/components/drivers_nrf/gpiote
INC_PATHS += -I$(NORDIC_TOOLS)/components/drivers_nrf/hal
INC_PATHS += -I$(NORDIC_TOOLS)/components/toolchain/gcc
INC_PATHS += -I$(NORDIC_TOOLS)/components/toolchain
INC_PATHS += -I$(NORDIC_TOOLS)/components/drivers_nrf/common
INC_PATHS += -I$(NORDIC_TOOLS)/components/ble/ble_advertising
INC_PATHS += -I$(NORDIC_TOOLS)/components/libraries/trace
INC_PATHS += -I$(NORDIC_TOOLS)/components/ble/ble_services/ble_bas
INC_PATHS += -I$(NORDIC_TOOLS)/components/softdevice/common/softdevice_handler
INC_PATHS += -I$(NORDIC_TOOLS)/components/ble/ble_services/ble_hrs
INC_PATHS += -I$(NORDIC_TOOLS)/components/ble/ble_services/ble_dfu
INC_PATHS += -I$(NORDIC_TOOLS)/components/libraries/bootloader_dfu

OBJECT_DIRECTORY = obj_strobe
LISTING_DIRECTORY = $(OBJECT_DIRECTORY)
OUTPUT_BINARY_DIRECTORY = $(OBJECT_DIRECTORY)

# Sorting removes duplicates
BUILD_DIRECTORIES := $(sort $(OBJECT_DIRECTORY) $(OUTPUT_BINARY_DIRECTORY) $(LISTING_DIRECTORY) )

#flags common to all targets
CFLAGS  = -DBOARD_PCA10028
CFLAGS += -DSWI_DISABLE0
CFLAGS += -DSOFTDEVICE_PRESENT
CFLAGS += -DNRF51
CFLAGS += -DS110
CFLAGS += -DBLE_STACK_SUPPORT_REQD
CFLAGS += -DBLE_DFU_APP_SUPPORT
CFLAGS += -mcpu=cortex-m0
CFLAGS += -mthumb -mabi=aapcs --std=gnu99
#CFLAGS += -Wall -Werror -O3
#CFLAGS += -Wall -Werror -O0 -g3
CFLAGS += -Wall -O0 -g3
CFLAGS += -mfloat-abi=soft
# keep every function in separate section. This will allow linker to dump unused functions
CFLAGS += -ffunction-sections -fdata-sections -fno-strict-aliasing
#CFLAGS += -fno-builtin --short-enums
CFLAGS += --short-enums
#CFLAGS += -DBSP_SIMPLE

# keep every function in separate section. This will allow linker to dump unused functions
LDFLAGS += -Xlinker -Map=$(LISTING_DIRECTORY)/$(OUTPUT_FILENAME).map
LDFLAGS += -mthumb -mabi=aapcs -L $(TEMPLATE_PATH) -T$(LINKER_SCRIPT)
LDFLAGS += -mcpu=cortex-m0
# let linker to dump unused sections
LDFLAGS += -Wl,--gc-sections
# use newlib in nano version
LDFLAGS += --specs=nano.specs -lc -lnosys

# Assembler flags
ASMFLAGS += -x assembler-with-cpp
ASMFLAGS += -DBOARD_PCA10028
ASMFLAGS += -DSWI_DISABLE0
ASMFLAGS += -DSOFTDEVICE_PRESENT
ASMFLAGS += -DNRF51
ASMFLAGS += -DS110
ASMFLAGS += -DBLE_STACK_SUPPORT_REQD
ASMFLAGS += -DBLE_DFU_APP_SUPPORT

#default target - first one defined
default: strobe

# include the component makefiles
include ../src/bsp/bsp.mf
include ../src/system/system.mf

#building all targets
all: clean
	$(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e cleanobj
	$(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e strobe

#target for printing all targets
help:
	@echo following targets are available:
	@echo 	strobe
	@echo 	flash_softdevice


C_SOURCE_FILE_NAMES = $(notdir $(C_SOURCE_FILES))
C_PATHS = $(call remduplicates, $(dir $(C_SOURCE_FILES) ) )
C_OBJECTS = $(addprefix $(OBJECT_DIRECTORY)/, $(C_SOURCE_FILE_NAMES:.c=.o) )

ASM_SOURCE_FILE_NAMES = $(notdir $(ASM_SOURCE_FILES))
ASM_PATHS = $(call remduplicates, $(dir $(ASM_SOURCE_FILES) ))
ASM_OBJECTS = $(addprefix $(OBJECT_DIRECTORY)/, $(ASM_SOURCE_FILE_NAMES:.s=.o) )

vpath %.c $(C_PATHS)
vpath %.s $(ASM_PATHS)

OBJECTS = $(C_OBJECTS) $(ASM_OBJECTS)

strobe: OUTPUT_FILENAME := strobe
strobe: LINKER_SCRIPT=ble_app_hrs_gcc_nrf51.ld
strobe: $(BUILD_DIRECTORIES) $(OBJECTS)
	@echo Linking target: $(OUTPUT_FILENAME).out
	$(NO_ECHO)$(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
	$(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e finalize

## Create build directories
$(BUILD_DIRECTORIES):
	echo $(MAKEFILE_NAME)
	$(MK) $@

# Create bootloader.o using additional compile flag
$(OBJECT_DIRECTORY)/bootloader_util.o: bootloader_util.c Makefile
	@echo Compiling file: $(notdir $<)
	@echo arm-none-eabi-gcc $(CFLAGS) -fomit-frame-pointer $(INC_PATHS) -c -o $@ $<
	$(NO_ECHO)$(CC) $(CFLAGS) -fomit-frame-pointer $(INC_PATHS) -c -o $@ $<

# Create objects from C SRC files
$(OBJECT_DIRECTORY)/%.o: %.c Makefile
	@echo Compiling file: $(notdir $<)
	@echo arm-none-eabi-gcc $(CFLAGS) $(INC_PATHS) -c -o $@ $<
	$(NO_ECHO)$(CC) $(CFLAGS) $(INC_PATHS) -c -o $@ $<

# Assemble files
$(OBJECT_DIRECTORY)/%.o: %.s Makefile
	@echo Compiling file: $(notdir $<)
	$(NO_ECHO)$(CC) $(ASMFLAGS) $(INC_PATHS) -c -o $@ $<


# Link
$(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out: $(BUILD_DIRECTORIES) $(OBJECTS)
	@echo Linking target: $(OUTPUT_FILENAME).out
	$(NO_ECHO)$(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out


## Create binary .bin file from the .out file
$(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin: $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
	@echo Preparing: $(OUTPUT_FILENAME).bin
	$(NO_ECHO)$(OBJCOPY) -O binary $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin

## Create binary .hex file from the .out file
$(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex: $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
	@echo Preparing: $(OUTPUT_FILENAME).hex
	$(NO_ECHO)$(OBJCOPY) -O ihex $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex

finalize: genbin genhex echosize

genbin:
	@echo Preparing: $(OUTPUT_FILENAME).bin
	$(NO_ECHO)$(OBJCOPY) -O binary $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin

## Create binary .hex file from the .out file
genhex: 
	@echo Preparing: $(OUTPUT_FILENAME).hex
	$(NO_ECHO)$(OBJCOPY) -O ihex $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex

echosize:
	-@echo ""
	$(NO_ECHO)$(SIZE) $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
	-@echo ""

clean:
	$(RM) $(BUILD_DIRECTORIES)

cleanobj:
	$(RM) $(BUILD_DIRECTORIES)/*.o

flash: $(MAKECMDGOALS)
	@echo Flashing: $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex
	nrfjprog --reset
	nrfjprog --program $(OUTPUT_BINARY_DIRECTORY)/strobe.hex --sectorerase --family NRF51
	
## Flash softdevice
flash_softdevice: 
	@echo Flashing: s110_softdevice.hex
	nrfjprog --reset
	nrfjprog --program $(NORDIC_TOOLS)/components/softdevice/s110/hex/s110_softdevice.hex