rules.mk 4.9 KB

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