rules.mk 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #
  2. # Common make definition for all examples
  3. #
  4. # libc
  5. LIBS += -lgcc -lm -lnosys
  6. ifneq ($(BOARD), spresense)
  7. LIBS += -lc
  8. endif
  9. # TinyUSB Stack source
  10. SRC_C += \
  11. src/tusb.c \
  12. src/common/tusb_fifo.c \
  13. src/device/usbd.c \
  14. src/device/usbd_control.c \
  15. src/class/msc/msc_device.c \
  16. src/class/cdc/cdc_device.c \
  17. src/class/dfu/dfu_rt_device.c \
  18. src/class/hid/hid_device.c \
  19. src/class/midi/midi_device.c \
  20. src/class/usbtmc/usbtmc_device.c \
  21. src/class/vendor/vendor_device.c \
  22. src/class/net/net_device.c \
  23. src/portable/$(VENDOR)/$(CHIP_FAMILY)/dcd_$(CHIP_FAMILY).c
  24. # TinyUSB stack include
  25. INC += $(TOP)/src
  26. #
  27. CFLAGS += $(addprefix -I,$(INC))
  28. LDFLAGS += $(CFLAGS) -fshort-enums -Wl,-T,$(TOP)/$(LD_FILE) -Wl,-Map=$@.map -Wl,-cref -Wl,-gc-sections -specs=nosys.specs -specs=nano.specs
  29. ASFLAGS += $(CFLAGS)
  30. # Assembly files can be name with upper case .S, convert it to .s
  31. SRC_S := $(SRC_S:.S=.s)
  32. # Due to GCC LTO bug https://bugs.launchpad.net/gcc-arm-embedded/+bug/1747966
  33. # assembly file should be placed first in linking order
  34. OBJ += $(addprefix $(BUILD)/obj/, $(SRC_S:.s=.o))
  35. OBJ += $(addprefix $(BUILD)/obj/, $(SRC_C:.c=.o))
  36. # Verbose mode
  37. ifeq ("$(V)","1")
  38. $(info CFLAGS $(CFLAGS) ) $(info )
  39. $(info LDFLAGS $(LDFLAGS)) $(info )
  40. $(info ASFLAGS $(ASFLAGS)) $(info )
  41. endif
  42. # Set all as default goal
  43. .DEFAULT_GOAL := all
  44. all: $(BUILD)/$(BOARD)-firmware.bin $(BUILD)/$(BOARD)-firmware.hex size
  45. uf2: $(BUILD)/$(BOARD)-firmware.uf2
  46. OBJ_DIRS = $(sort $(dir $(OBJ)))
  47. $(OBJ): | $(OBJ_DIRS)
  48. $(OBJ_DIRS):
  49. @$(MKDIR) -p $@
  50. $(BUILD)/$(BOARD)-firmware.elf: $(OBJ)
  51. @echo LINK $@
  52. @$(CC) -o $@ $(LDFLAGS) $^ -Wl,--start-group $(LIBS) -Wl,--end-group
  53. $(BUILD)/$(BOARD)-firmware.bin: $(BUILD)/$(BOARD)-firmware.elf
  54. @echo CREATE $@
  55. @$(OBJCOPY) -O binary $^ $@
  56. $(BUILD)/$(BOARD)-firmware.hex: $(BUILD)/$(BOARD)-firmware.elf
  57. @echo CREATE $@
  58. @$(OBJCOPY) -O ihex $^ $@
  59. UF2_FAMILY ?= 0x00
  60. $(BUILD)/$(BOARD)-firmware.uf2: $(BUILD)/$(BOARD)-firmware.hex
  61. @echo CREATE $@
  62. $(PYTHON) $(TOP)/tools/uf2/utils/uf2conv.py -f $(UF2_FAMILY) -c -o $@ $^
  63. # We set vpath to point to the top of the tree so that the source files
  64. # can be located. By following this scheme, it allows a single build rule
  65. # to be used to compile all .c files.
  66. vpath %.c . $(TOP)
  67. $(BUILD)/obj/%.o: %.c
  68. @echo CC $(notdir $@)
  69. @$(CC) $(CFLAGS) -c -MD -o $@ $<
  70. @# The following fixes the dependency file.
  71. @# See http://make.paulandlesley.org/autodep.html for details.
  72. @# Regex adjusted from the above to play better with Windows paths, etc.
  73. @$(CP) $(@:.o=.d) $(@:.o=.P); \
  74. $(SED) -e 's/#.*//' -e 's/^.*: *//' -e 's/ *\\$$//' \
  75. -e '/^$$/ d' -e 's/$$/ :/' < $(@:.o=.d) >> $(@:.o=.P); \
  76. $(RM) $(@:.o=.d)
  77. # ASM sources lower case .s
  78. vpath %.s . $(TOP)
  79. $(BUILD)/obj/%.o: %.s
  80. @echo AS $(notdir $@)
  81. @$(CC) -x assembler-with-cpp $(ASFLAGS) -c -o $@ $<
  82. # ASM sources upper case .S
  83. vpath %.S . $(TOP)
  84. $(BUILD)/obj/%.o: %.S
  85. @echo AS $(notdir $@)
  86. @$(CC) -x assembler-with-cpp $(ASFLAGS) -c -o $@ $<
  87. size: $(BUILD)/$(BOARD)-firmware.elf
  88. -@echo ''
  89. @$(SIZE) $<
  90. -@echo ''
  91. clean:
  92. rm -rf $(BUILD)
  93. # Flash binary using Jlink
  94. ifeq ($(OS),Windows_NT)
  95. JLINKEXE = JLink.exe
  96. else
  97. JLINKEXE = JLinkExe
  98. endif
  99. # Flash using jlink
  100. flash-jlink: $(BUILD)/$(BOARD)-firmware.hex
  101. @echo halt > $(BUILD)/$(BOARD).jlink
  102. @echo r > $(BUILD)/$(BOARD).jlink
  103. @echo loadfile $^ >> $(BUILD)/$(BOARD).jlink
  104. @echo r >> $(BUILD)/$(BOARD).jlink
  105. @echo go >> $(BUILD)/$(BOARD).jlink
  106. @echo exit >> $(BUILD)/$(BOARD).jlink
  107. $(JLINKEXE) -device $(JLINK_DEVICE) -if $(JLINK_IF) -JTAGConf -1,-1 -speed auto -CommandFile $(BUILD)/$(BOARD).jlink
  108. # flash STM32 MCU using stlink with STM32 Cube Programmer CLI
  109. flash-stlink: $(BUILD)/$(BOARD)-firmware.elf
  110. STM32_Programmer_CLI --connect port=swd --write $< --go