rules.mk 4.5 KB

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