You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
6.6 KiB

5 years ago
5 years ago
  1. include $(ROOT)/Makefile.config
  2. REAL_PWD=$(shell echo $$PWD)
  3. NRFJPROG ?= nrfjprog
  4. OPENOCD ?= sudo openocd
  5. OPENOCD_IFACE ?= interface/stlink-v2.cfg
  6. OPENOCD_TARGET ?= target/$(NRF_TARGET).cfg
  7. ifeq ("$(PROJECT_NAME)","")
  8. PROJECT_NAME := $(lastword $(subst /, , $(REAL_PWD)))
  9. endif
  10. MAKEFILE_NAME := $(MAKEFILE_LIST)
  11. MAKEFILE_DIR := $(dir $(MAKEFILE_NAME) )
  12. TEMPLATE_PATH = $(NRF5_SDK)/components/toolchain/gcc
  13. ifeq ($(OS),Windows_NT)
  14. include $(TEMPLATE_PATH)/Makefile.windows
  15. else
  16. include $(TEMPLATE_PATH)/Makefile.posix
  17. endif
  18. MK := mkdir
  19. RM := rm -rf
  20. ifeq ("$(VERBOSE)","1")
  21. NO_ECHO :=
  22. else
  23. NO_ECHO := @
  24. endif
  25. # Toolchain commands
  26. CC := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-gcc'
  27. AS := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-as'
  28. AR := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-ar' -r
  29. LD := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-ld'
  30. NM := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-nm'
  31. OBJDUMP := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-objdump'
  32. OBJCOPY := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-objcopy'
  33. SIZE := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-size'
  34. SED := /bin/env sed
  35. #function for removing duplicates in a list
  36. remduplicates = $(strip $(if $1,$(firstword $1) $(call remduplicates,$(filter-out $(firstword $1),$1))))
  37. #source common to all targets
  38. C_SOURCE_FILES += \
  39. $(NRF5_SDK)/components/toolchain/system_$(NRF_TARGET).c \
  40. #assembly files common to all targets
  41. ASM_SOURCE_FILES = $(NRF5_SDK)/components/toolchain/gcc/gcc_startup_$(NRF_TARGET).s
  42. #includes common to all targets
  43. INC_PATHS += -I$(NRF5_SDK)/components/toolchain/gcc
  44. INC_PATHS += -I$(NRF5_SDK)/components/toolchain
  45. INC_PATHS += -I$(NRF5_SDK)/components/toolchain/CMSIS/Include/
  46. INC_PATHS += -I.
  47. INC_PATHS += -I$(NRF5_SDK)/components/device
  48. INC_PATHS_NODUP = $(call remduplicates, $(INC_PATHS))
  49. OBJECT_DIRECTORY = _build
  50. LISTING_DIRECTORY = $(OBJECT_DIRECTORY)
  51. OUTPUT_BINARY_DIRECTORY = $(OBJECT_DIRECTORY)
  52. OUTPUT_FILENAME := firmware-$(NRF_TARGET)
  53. # Sorting removes duplicates
  54. BUILD_DIRECTORIES := $(sort $(OBJECT_DIRECTORY) $(OUTPUT_BINARY_DIRECTORY) $(LISTING_DIRECTORY) )
  55. # Set target specific CFLAGS, LDFLAGS and ASMFLAGS
  56. include $(ROOT)/Makefile.$(NRF_TARGET)
  57. #default target - first one defined
  58. default: firmware
  59. #building all targets
  60. all:
  61. $(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e firmware
  62. #target for printing all targets
  63. help:
  64. @echo following targets are available:
  65. @echo firmware
  66. # Allow duplicate source files for easier dependency management
  67. C_SOURCE_FILE_NAMES_DUP = $(notdir $(C_SOURCE_FILES))
  68. C_SOURCE_FILE_NAMES = $(call remduplicates, $(C_SOURCE_FILE_NAMES_DUP))
  69. C_PATHS = $(call remduplicates, $(dir $(C_SOURCE_FILES) ) )
  70. C_OBJECTS = $(addprefix $(OBJECT_DIRECTORY)/, $(C_SOURCE_FILE_NAMES:.c=.o) )
  71. ASM_SOURCE_FILE_NAMES = $(notdir $(ASM_SOURCE_FILES))
  72. ASM_PATHS = $(call remduplicates, $(dir $(ASM_SOURCE_FILES) ))
  73. ASM_OBJECTS = $(addprefix $(OBJECT_DIRECTORY)/, $(ASM_SOURCE_FILE_NAMES:.s=.o) )
  74. vpath %.c $(C_PATHS)
  75. vpath %.s $(ASM_PATHS)
  76. OBJECTS = $(C_OBJECTS) $(ASM_OBJECTS)
  77. firmware: $(LINKER_SCRIPT)
  78. firmware: $(PROJECT_NAME).includes $(PROJECT_NAME).files
  79. firmware: $(BUILD_DIRECTORIES) $(OBJECTS)
  80. @echo Linking target: $(OUTPUT_FILENAME).out
  81. $(NO_ECHO)$(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
  82. $(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e finalize
  83. ## Create build directories
  84. $(BUILD_DIRECTORIES):
  85. echo $(MAKEFILE_NAME)
  86. $(MK) $@
  87. # Create objects from C SRC files
  88. $(OBJECT_DIRECTORY)/%.o: %.c
  89. @echo Compiling file: $(notdir $<)
  90. $(NO_ECHO)$(CC) $(CFLAGS) $(INC_PATHS) -c -o $@ $<
  91. # Depend file, editing a header file triggers rebuild on affected sources
  92. .depend: $(C_SOURCE_FILE_NAMES)
  93. @echo Building depend file
  94. @rm -f ./.depend
  95. $(NO_ECHO)$(CC) $(CFLAGS) $(INC_PATHS) -MM $^ > ./.depend;
  96. @$(SED) -i -r 's/^(\w)/$(OBJECT_DIRECTORY)\/\1/g' .depend
  97. @# Generate QtCreator files
  98. @# Generate .includes file
  99. @rm -f $(PROJECT_NAME).includes
  100. @echo $(INC_PATHS_NODUP) | $(SED) -r 's/ /\n/g' | $(SED) 's/^-I//' > $(PROJECT_NAME).includes
  101. @# Generate .files file
  102. @rm -f $(PROJECT_NAME).files
  103. @tr '\n' ' ' < .depend | sed 's/\\//g' | sed 's/[^ ]*://g' | sed 's/\s\+/\n/g' | sort | uniq > $(PROJECT_NAME).files
  104. depend: .depend
  105. -include .depend
  106. # Assemble files
  107. $(OBJECT_DIRECTORY)/%.o: %.s
  108. @echo Compiling file: $(notdir $<)
  109. $(NO_ECHO)$(CC) $(ASMFLAGS) $(INC_PATHS) -c -o $@ $<
  110. # Link
  111. $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out: check_sdk_path $(BUILD_DIRECTORIES) $(OBJECTS)
  112. @echo Linking target: $(OUTPUT_FILENAME).out
  113. $(NO_ECHO)$(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
  114. ## Create binary .bin file from the .out file
  115. $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin: $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
  116. @echo Preparing: $(OUTPUT_FILENAME).bin
  117. $(NO_ECHO)$(OBJCOPY) -O binary $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin
  118. ## Create binary .hex file from the .out file
  119. $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex: $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
  120. @echo Preparing: $(OUTPUT_FILENAME).hex
  121. $(NO_ECHO)$(OBJCOPY) -O ihex $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex
  122. finalize: genbin genhex echosize
  123. genbin:
  124. @echo Preparing: $(OUTPUT_FILENAME).bin
  125. $(NO_ECHO)$(OBJCOPY) -O binary $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin
  126. ## Create binary .hex file from the .out file
  127. genhex:
  128. @echo Preparing: $(OUTPUT_FILENAME).hex
  129. $(NO_ECHO)$(OBJCOPY) -O ihex $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex
  130. echosize:
  131. -@echo ''
  132. $(NO_ECHO)$(SIZE) $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
  133. -@echo ''
  134. clean:
  135. $(RM) $(BUILD_DIRECTORIES)
  136. cleanobj:
  137. $(RM) $(BUILD_DIRECTORIES)/*.o
  138. deploy: $(MAKECMDGOALS)
  139. @echo Flashing: $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex
  140. $(NRFJPROG) --erasepage 0x0-0x80000 -f $(NRF_TARGET)
  141. $(NRFJPROG) --program $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex -f $(NRF_TARGET)
  142. $(NRFJPROG) --reset -f $(NRF_TARGET)
  143. deploy-openocd: $(MAKECMDGOALS)
  144. @echo Flashing: $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex
  145. $(OPENOCD) -f $(OPENOCD_IFACE) -f $(OPENOCD_TARGET) -c "init" -c "reset halt" -c "program $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex 0 verify" -c "reset run" -c "exit"
  146. erase-openocd: $(MAKECMDGOALS)
  147. @echo Erasing...
  148. $(OPENOCD) -f $(OPENOCD_IFACE) -f $(OPENOCD_TARGET) -c "init; reset halt; $(NRF_TARGET) mass_erase; shutdown"
  149. ## Flash softdevice (TODO)