rules.mk 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # ---------------------------------------
  2. # Common make rules for all examples
  3. # ---------------------------------------
  4. ifeq ($(CROSS_COMPILE),xtensa-esp32s2-elf-)
  5. # Espressif IDF use CMake build system, this add wrapper target to call idf.py
  6. .PHONY: all clean flash
  7. .DEFAULT_GOAL := all
  8. all:
  9. idf.py -B$(BUILD) -DBOARD=$(BOARD) build
  10. clean:
  11. idf.py -B$(BUILD) -DBOARD=$(BOARD) clean
  12. flash:
  13. idf.py -B$(BUILD) -DBOARD=$(BOARD) flash
  14. else
  15. # GNU Make build system
  16. # libc
  17. LIBS += -lgcc -lm -lnosys
  18. ifneq ($(BOARD), spresense)
  19. LIBS += -lc
  20. endif
  21. # TinyUSB Stack source
  22. SRC_C += \
  23. src/tusb.c \
  24. src/common/tusb_fifo.c \
  25. src/device/usbd.c \
  26. src/device/usbd_control.c \
  27. src/class/audio/audio_device.c \
  28. src/class/cdc/cdc_device.c \
  29. src/class/dfu/dfu_rt_device.c \
  30. src/class/hid/hid_device.c \
  31. src/class/midi/midi_device.c \
  32. src/class/msc/msc_device.c \
  33. src/class/net/net_device.c \
  34. src/class/usbtmc/usbtmc_device.c \
  35. src/class/vendor/vendor_device.c \
  36. src/portable/$(VENDOR)/$(CHIP_FAMILY)/dcd_$(CHIP_FAMILY).c
  37. # TinyUSB stack include
  38. INC += $(TOP)/src
  39. CFLAGS += $(addprefix -I,$(INC))
  40. # TODO Skip nanolib for MSP430
  41. ifeq ($(BOARD), msp_exp430f5529lp)
  42. LDFLAGS += $(CFLAGS) -fshort-enums -Wl,-T,$(TOP)/$(LD_FILE) -Wl,-Map=$@.map -Wl,-cref -Wl,-gc-sections
  43. else
  44. LDFLAGS += $(CFLAGS) -fshort-enums -Wl,-T,$(TOP)/$(LD_FILE) -Wl,-Map=$@.map -Wl,-cref -Wl,-gc-sections -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. OBJ += $(addprefix $(BUILD)/obj/, $(SRC_S:.s=.o))
  52. OBJ += $(addprefix $(BUILD)/obj/, $(SRC_C:.c=.o))
  53. # Verbose mode
  54. ifeq ("$(V)","1")
  55. $(info CFLAGS $(CFLAGS) ) $(info )
  56. $(info LDFLAGS $(LDFLAGS)) $(info )
  57. $(info ASFLAGS $(ASFLAGS)) $(info )
  58. endif
  59. # Set all as default goal
  60. .DEFAULT_GOAL := all
  61. all: $(BUILD)/$(BOARD)-firmware.bin $(BUILD)/$(BOARD)-firmware.hex size
  62. uf2: $(BUILD)/$(BOARD)-firmware.uf2
  63. OBJ_DIRS = $(sort $(dir $(OBJ)))
  64. $(OBJ): | $(OBJ_DIRS)
  65. $(OBJ_DIRS):
  66. @$(MKDIR) -p $@
  67. $(BUILD)/$(BOARD)-firmware.elf: $(OBJ)
  68. @echo LINK $@
  69. @$(CC) -o $@ $(LDFLAGS) $^ -Wl,--start-group $(LIBS) -Wl,--end-group
  70. $(BUILD)/$(BOARD)-firmware.bin: $(BUILD)/$(BOARD)-firmware.elf
  71. @echo CREATE $@
  72. @$(OBJCOPY) -O binary $^ $@
  73. $(BUILD)/$(BOARD)-firmware.hex: $(BUILD)/$(BOARD)-firmware.elf
  74. @echo CREATE $@
  75. @$(OBJCOPY) -O ihex $^ $@
  76. UF2_FAMILY ?= 0x00
  77. $(BUILD)/$(BOARD)-firmware.uf2: $(BUILD)/$(BOARD)-firmware.hex
  78. @echo CREATE $@
  79. $(PYTHON) $(TOP)/tools/uf2/utils/uf2conv.py -f $(UF2_FAMILY) -c -o $@ $^
  80. # We set vpath to point to the top of the tree so that the source files
  81. # can be located. By following this scheme, it allows a single build rule
  82. # to be used to compile all .c files.
  83. vpath %.c . $(TOP)
  84. $(BUILD)/obj/%.o: %.c
  85. @echo CC $(notdir $@)
  86. @$(CC) $(CFLAGS) -c -MD -o $@ $<
  87. @# The following fixes the dependency file.
  88. @# See http://make.paulandlesley.org/autodep.html for details.
  89. @# Regex adjusted from the above to play better with Windows paths, etc.
  90. @$(CP) $(@:.o=.d) $(@:.o=.P); \
  91. $(SED) -e 's/#.*//' -e 's/^.*: *//' -e 's/ *\\$$//' \
  92. -e '/^$$/ d' -e 's/$$/ :/' < $(@:.o=.d) >> $(@:.o=.P); \
  93. $(RM) $(@:.o=.d)
  94. # ASM sources lower case .s
  95. vpath %.s . $(TOP)
  96. $(BUILD)/obj/%.o: %.s
  97. @echo AS $(notdir $@)
  98. @$(CC) -x assembler-with-cpp $(ASFLAGS) -c -o $@ $<
  99. # ASM sources upper case .S
  100. vpath %.S . $(TOP)
  101. $(BUILD)/obj/%.o: %.S
  102. @echo AS $(notdir $@)
  103. @$(CC) -x assembler-with-cpp $(ASFLAGS) -c -o $@ $<
  104. size: $(BUILD)/$(BOARD)-firmware.elf
  105. -@echo ''
  106. @$(SIZE) $<
  107. -@echo ''
  108. .PHONY: clean
  109. clean:
  110. $(RM) -rf $(BUILD)
  111. # Print out the value of a make variable.
  112. # https://stackoverflow.com/questions/16467718/how-to-print-out-a-variable-in-makefile
  113. print-%:
  114. @echo $* = $($*)
  115. # Flash binary using Jlink
  116. ifeq ($(OS),Windows_NT)
  117. JLINKEXE = JLink.exe
  118. else
  119. JLINKEXE = JLinkExe
  120. endif
  121. JLINK_IF ?= swd
  122. # Flash using jlink
  123. flash-jlink: $(BUILD)/$(BOARD)-firmware.hex
  124. @echo halt > $(BUILD)/$(BOARD).jlink
  125. @echo r > $(BUILD)/$(BOARD).jlink
  126. @echo loadfile $^ >> $(BUILD)/$(BOARD).jlink
  127. @echo r >> $(BUILD)/$(BOARD).jlink
  128. @echo go >> $(BUILD)/$(BOARD).jlink
  129. @echo exit >> $(BUILD)/$(BOARD).jlink
  130. $(JLINKEXE) -device $(JLINK_DEVICE) -if $(JLINK_IF) -JTAGConf -1,-1 -speed auto -CommandFile $(BUILD)/$(BOARD).jlink
  131. # flash STM32 MCU using stlink with STM32 Cube Programmer CLI
  132. flash-stlink: $(BUILD)/$(BOARD)-firmware.elf
  133. STM32_Programmer_CLI --connect port=swd --write $< --go
  134. # flash with pyocd
  135. flash-pyocd: $(BUILD)/$(BOARD)-firmware.hex
  136. pyocd flash -t $(PYOCD_TARGET) $<
  137. pyocd reset -t $(PYOCD_TARGET)
  138. endif # Make target