rules.mk 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # ---------------------------------------
  2. # Common make rules for all examples
  3. # ---------------------------------------
  4. # Set all as default goal
  5. .DEFAULT_GOAL := all
  6. # ESP32-Sx and RP2040 has its own CMake build system
  7. ifeq (,$(findstring $(FAMILY),esp32s2 esp32s3 rp2040))
  8. # ---------------------------------------
  9. # Compiler Flags
  10. # ---------------------------------------
  11. # libc
  12. LIBS += -lgcc -lm -lnosys
  13. ifneq ($(BOARD), spresense)
  14. LIBS += -lc
  15. endif
  16. # TinyUSB Stack source
  17. SRC_C += \
  18. src/tusb.c \
  19. src/common/tusb_fifo.c \
  20. src/device/usbd.c \
  21. src/device/usbd_control.c \
  22. src/class/audio/audio_device.c \
  23. src/class/cdc/cdc_device.c \
  24. src/class/dfu/dfu_device.c \
  25. src/class/dfu/dfu_rt_device.c \
  26. src/class/hid/hid_device.c \
  27. src/class/midi/midi_device.c \
  28. src/class/msc/msc_device.c \
  29. src/class/net/ecm_rndis_device.c \
  30. src/class/net/ncm_device.c \
  31. src/class/usbtmc/usbtmc_device.c \
  32. src/class/video/video_device.c \
  33. src/class/vendor/vendor_device.c
  34. # TinyUSB stack include
  35. INC += $(TOP)/src
  36. CFLAGS += $(addprefix -I,$(INC))
  37. # LTO makes it difficult to analyze map file for optimizing size purpose
  38. # We will run this option in ci
  39. ifeq ($(NO_LTO),1)
  40. CFLAGS := $(filter-out -flto,$(CFLAGS))
  41. endif
  42. LDFLAGS += $(CFLAGS) -Wl,-T,$(TOP)/$(LD_FILE) -Wl,-Map=$@.map -Wl,-cref -Wl,-gc-sections
  43. ifneq ($(SKIP_NANOLIB), 1)
  44. LDFLAGS += -specs=nosys.specs -specs=nano.specs
  45. endif
  46. ASFLAGS += $(CFLAGS)
  47. # Assembly files can be name with upper case .S, convert it to .s
  48. SRC_S := $(SRC_S:.S=.s)
  49. # Due to GCC LTO bug https://bugs.launchpad.net/gcc-arm-embedded/+bug/1747966
  50. # assembly file should be placed first in linking order
  51. # '_asm' suffix is added to object of assembly file
  52. OBJ += $(addprefix $(BUILD)/obj/, $(SRC_S:.s=_asm.o))
  53. OBJ += $(addprefix $(BUILD)/obj/, $(SRC_C:.c=.o))
  54. # Verbose mode
  55. ifeq ("$(V)","1")
  56. $(info CFLAGS $(CFLAGS) ) $(info )
  57. $(info LDFLAGS $(LDFLAGS)) $(info )
  58. $(info ASFLAGS $(ASFLAGS)) $(info )
  59. endif
  60. # ---------------------------------------
  61. # Rules
  62. # ---------------------------------------
  63. all: $(BUILD)/$(PROJECT).bin $(BUILD)/$(PROJECT).hex size
  64. uf2: $(BUILD)/$(PROJECT).uf2
  65. OBJ_DIRS = $(sort $(dir $(OBJ)))
  66. $(OBJ): | $(OBJ_DIRS)
  67. $(OBJ_DIRS):
  68. ifeq ($(CMDEXE),1)
  69. @$(MKDIR) $(subst /,\,$@)
  70. else
  71. @$(MKDIR) -p $@
  72. endif
  73. $(BUILD)/$(PROJECT).elf: $(OBJ)
  74. @echo LINK $@
  75. @$(CC) -o $@ $(LDFLAGS) $^ -Wl,--start-group $(LIBS) -Wl,--end-group
  76. $(BUILD)/$(PROJECT).bin: $(BUILD)/$(PROJECT).elf
  77. @echo CREATE $@
  78. @$(OBJCOPY) -O binary $^ $@
  79. $(BUILD)/$(PROJECT).hex: $(BUILD)/$(PROJECT).elf
  80. @echo CREATE $@
  81. @$(OBJCOPY) -O ihex $^ $@
  82. # UF2 generation, iMXRT need to strip to text only before conversion
  83. ifeq ($(FAMILY),imxrt)
  84. $(BUILD)/$(PROJECT).uf2: $(BUILD)/$(PROJECT).elf
  85. @echo CREATE $@
  86. @$(OBJCOPY) -O ihex -R .flash_config -R .ivt $^ $(BUILD)/$(PROJECT)-textonly.hex
  87. $(PYTHON) $(TOP)/tools/uf2/utils/uf2conv.py -f $(UF2_FAMILY_ID) -c -o $@ $(BUILD)/$(PROJECT)-textonly.hex
  88. else
  89. $(BUILD)/$(PROJECT).uf2: $(BUILD)/$(PROJECT).hex
  90. @echo CREATE $@
  91. $(PYTHON) $(TOP)/tools/uf2/utils/uf2conv.py -f $(UF2_FAMILY_ID) -c -o $@ $^
  92. endif
  93. copy-artifact: $(BUILD)/$(PROJECT).bin $(BUILD)/$(PROJECT).hex $(BUILD)/$(PROJECT).uf2
  94. # We set vpath to point to the top of the tree so that the source files
  95. # can be located. By following this scheme, it allows a single build rule
  96. # to be used to compile all .c files.
  97. vpath %.c . $(TOP)
  98. $(BUILD)/obj/%.o: %.c
  99. @echo CC $(notdir $@)
  100. @$(CC) $(CFLAGS) -c -MD -o $@ $<
  101. # ASM sources lower case .s
  102. vpath %.s . $(TOP)
  103. $(BUILD)/obj/%_asm.o: %.s
  104. @echo AS $(notdir $@)
  105. @$(CC) -x assembler-with-cpp $(ASFLAGS) -c -o $@ $<
  106. # ASM sources upper case .S
  107. vpath %.S . $(TOP)
  108. $(BUILD)/obj/%_asm.o: %.S
  109. @echo AS $(notdir $@)
  110. @$(CC) -x assembler-with-cpp $(ASFLAGS) -c -o $@ $<
  111. endif # GNU Make
  112. size: $(BUILD)/$(PROJECT).elf
  113. -@echo ''
  114. @$(SIZE) $<
  115. -@echo ''
  116. # linkermap must be install previously at https://github.com/hathach/linkermap
  117. linkermap: $(BUILD)/$(PROJECT).elf
  118. @linkermap -v $<.map
  119. .PHONY: clean
  120. clean:
  121. ifeq ($(CMDEXE),1)
  122. rd /S /Q $(subst /,\,$(BUILD))
  123. else
  124. $(RM) -rf $(BUILD)
  125. endif
  126. # ---------------------------------------
  127. # Flash Targets
  128. # ---------------------------------------
  129. # Jlink binary
  130. ifeq ($(OS),Windows_NT)
  131. JLINKEXE = JLink.exe
  132. else
  133. JLINKEXE = JLinkExe
  134. endif
  135. # Jlink Interface
  136. JLINK_IF ?= swd
  137. # Flash using jlink
  138. flash-jlink: $(BUILD)/$(PROJECT).hex
  139. @echo halt > $(BUILD)/$(BOARD).jlink
  140. @echo r > $(BUILD)/$(BOARD).jlink
  141. @echo loadfile $^ >> $(BUILD)/$(BOARD).jlink
  142. @echo r >> $(BUILD)/$(BOARD).jlink
  143. @echo go >> $(BUILD)/$(BOARD).jlink
  144. @echo exit >> $(BUILD)/$(BOARD).jlink
  145. $(JLINKEXE) -device $(JLINK_DEVICE) -if $(JLINK_IF) -JTAGConf -1,-1 -speed auto -CommandFile $(BUILD)/$(BOARD).jlink
  146. # Flash STM32 MCU using stlink with STM32 Cube Programmer CLI
  147. flash-stlink: $(BUILD)/$(PROJECT).elf
  148. STM32_Programmer_CLI --connect port=swd --write $< --go
  149. flash-xfel: $(BUILD)/$(PROJECT).bin
  150. xfel ddr
  151. xfel write 0x80000000 $<
  152. xfel exec 0x80000000
  153. # Flash using pyocd
  154. PYOCD_OPTION ?=
  155. flash-pyocd: $(BUILD)/$(PROJECT).hex
  156. pyocd flash -t $(PYOCD_TARGET) $(PYOCD_OPTION) $<
  157. pyocd reset -t $(PYOCD_TARGET)
  158. # Flash using openocd
  159. OPENOCD_OPTION ?=
  160. flash-openocd: $(BUILD)/$(PROJECT).elf
  161. openocd $(OPENOCD_OPTION) -c "program $< verify reset exit"
  162. # flash with Black Magic Probe
  163. # This symlink is created by https://github.com/blacksphere/blackmagic/blob/master/driver/99-blackmagic.rules
  164. BMP ?= /dev/ttyBmpGdb
  165. flash-bmp: $(BUILD)/$(PROJECT).elf
  166. $(GDB) --batch -ex 'target extended-remote $(BMP)' -ex 'monitor swdp_scan' -ex 'attach 1' -ex load $<
  167. debug-bmp: $(BUILD)/$(PROJECT).elf
  168. $(GDB) -ex 'target extended-remote $(BMP)' -ex 'monitor swdp_scan' -ex 'attach 1' $<
  169. #-------------- Artifacts --------------
  170. # Create binary directory
  171. $(BIN):
  172. @$(MKDIR) -p $@
  173. # Copy binaries .elf, .bin, .hex, .uf2 to BIN for upload
  174. # due to large size of combined artifacts, only uf2 is uploaded for now
  175. copy-artifact: $(BIN)
  176. @$(CP) $(BUILD)/$(PROJECT).uf2 $(BIN)
  177. #@$(CP) $(BUILD)/$(PROJECT).bin $(BIN)
  178. #@$(CP) $(BUILD)/$(PROJECT).hex $(BIN)
  179. #@$(CP) $(BUILD)/$(PROJECT).elf $(BIN)
  180. # Print out the value of a make variable.
  181. # https://stackoverflow.com/questions/16467718/how-to-print-out-a-variable-in-makefile
  182. print-%:
  183. @echo $* = $($*)