Makefile 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. include ../../../tools/top.mk
  2. # Select the board to build for.
  3. ifeq ($(BOARD),)
  4. $(info You must provide a BOARD parameter with 'BOARD=')
  5. $(info Possible values are:)
  6. $(info $(sort $(subst /.,,$(subst $(TOP)/hw/bsp/,,$(wildcard $(TOP)/hw/bsp/*/.)))))
  7. $(error BOARD not defined)
  8. else
  9. ifeq ($(wildcard $(TOP)/hw/bsp/$(BOARD)/.),)
  10. $(error Invalid BOARD specified)
  11. endif
  12. endif
  13. # Verbose mode (V=). 0: default, 1: print out CFLAG, LDFLAG 2: print all compile command
  14. ifeq ("$(V)","2")
  15. QUIET =
  16. else
  17. QUIET = @
  18. endif
  19. # If the build directory is not given, make it reflect the board name.
  20. BUILD ?= build-$(BOARD)
  21. CROSS_COMPILE = arm-none-eabi-
  22. include $(TOP)/hw/bsp/$(BOARD)/board.mk
  23. CC = $(CROSS_COMPILE)gcc
  24. CXX = $(CROSS_COMPILE)g++
  25. OBJCOPY = $(CROSS_COMPILE)objcopy
  26. SIZE = $(CROSS_COMPILE)size
  27. MKDIR = mkdir
  28. SED = sed
  29. CP = cp
  30. RM = rm
  31. INC += -Isrc \
  32. -I$(TOP)/hw \
  33. -I$(TOP)/src
  34. CFLAGS += \
  35. -fsingle-precision-constant \
  36. -fno-strict-aliasing \
  37. -Wdouble-promotion \
  38. -Wno-endif-labels \
  39. -Wstrict-prototypes \
  40. -Werror-implicit-function-declaration \
  41. -Wfloat-equal \
  42. -Wundef \
  43. -Wshadow \
  44. -Wwrite-strings \
  45. -Wsign-compare \
  46. -Wmissing-format-attribute \
  47. -Wno-deprecated-declarations \
  48. -Wnested-externs \
  49. -Wunreachable-code \
  50. -Wno-error=lto-type-mismatch \
  51. -ffunction-sections \
  52. -fdata-sections
  53. # This causes lots of warning with nrf5x build due to nrfx code
  54. # CFLAGS += -Wcast-align
  55. #Debugging/Optimization
  56. ifeq ($(DEBUG), 1)
  57. CFLAGS += -O0 -ggdb
  58. else
  59. CFLAGS += -flto -Os
  60. endif
  61. CFLAGS += $(INC) -Wall -Werror -std=gnu11 -DBOARD_$(shell echo $(BOARD) | tr '[:lower:]' '[:upper:]')
  62. LDFLAGS += $(CFLAGS) -fshort-enums -Wl,-T,$(TOP)/$(LD_FILE) -Wl,-Map=$@.map -Wl,-cref -Wl,-gc-sections -specs=nosys.specs -specs=nano.specs
  63. ifeq ("$(V)","1")
  64. $(info CFLAGS $(CFLAGS))
  65. $(info )
  66. $(info LDFLAGS $(LDFLAGS))
  67. $(info )
  68. $(info ASFLAGS $(ASFLAGS))
  69. $(info )
  70. endif
  71. LIBS = -lgcc -lc -lm -lnosys
  72. EXAMPLE_SOURCE += \
  73. src/main.c \
  74. src/msc_app.c \
  75. src/msc_disk_ram.c \
  76. src/tusb_descriptors.c
  77. SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE))
  78. LIB_SOURCE += \
  79. hw/bsp/$(BOARD)/board_$(BOARD).c \
  80. src/common/tusb_fifo.c \
  81. src/device/usbd.c \
  82. src/device/usbd_auto_desc.c \
  83. src/device/usbd_control.c \
  84. src/class/msc/msc_device.c \
  85. src/class/cdc/cdc_device.c \
  86. src/class/hid/hid_device.c \
  87. src/tusb.c \
  88. src/portable/$(VENDOR)/$(CHIP_FAMILY)/dcd_$(CHIP_FAMILY).c
  89. SRC_C += $(LIB_SOURCE)
  90. # Assembly files can be name with upper case .S, convert it to .s
  91. SRC_S := $(SRC_S:.S=.s)
  92. # Due to GCC LTO bug https://bugs.launchpad.net/gcc-arm-embedded/+bug/1747966
  93. # assembly file should be placed first in linking order
  94. OBJ += $(addprefix $(BUILD)/obj/, $(SRC_S:.s=.o))
  95. OBJ += $(addprefix $(BUILD)/obj/, $(SRC_C:.c=.o))
  96. # Set all as default goal
  97. .DEFAULT_GOAL := all
  98. all: $(BUILD)/$(BOARD)-firmware.bin size
  99. OBJ_DIRS = $(sort $(dir $(OBJ)))
  100. $(OBJ): | $(OBJ_DIRS)
  101. $(OBJ_DIRS):
  102. @$(MKDIR) -p $@
  103. $(BUILD)/$(BOARD)-firmware.elf: $(OBJ)
  104. @echo LINK $@
  105. $(QUIET)$(CC) -o $@ $(LDFLAGS) $^ -Wl,--start-group $(LIBS) -Wl,--end-group
  106. $(BUILD)/$(BOARD)-firmware.bin: $(BUILD)/$(BOARD)-firmware.elf
  107. @echo CREATE $@
  108. @$(OBJCOPY) -O binary -j .vectors -j .text -j .data $^ $@
  109. $(BUILD)/$(BOARD)-firmware.hex: $(BUILD)/$(BOARD)-firmware.elf
  110. @echo CREATE $@
  111. @$(OBJCOPY) -O ihex $^ $@
  112. # We set vpath to point to the top of the tree so that the source files
  113. # can be located. By following this scheme, it allows a single build rule
  114. # to be used to compile all .c files.
  115. vpath %.c . $(TOP)
  116. $(BUILD)/obj/%.o: %.c
  117. @echo CC $(notdir $@)
  118. $(QUIET)$(CC) $(CFLAGS) -c -MD -o $@ $<
  119. @# The following fixes the dependency file.
  120. @# See http://make.paulandlesley.org/autodep.html for details.
  121. @# Regex adjusted from the above to play better with Windows paths, etc.
  122. @$(CP) $(@:.o=.d) $(@:.o=.P); \
  123. $(SED) -e 's/#.*//' -e 's/^.*: *//' -e 's/ *\\$$//' \
  124. -e '/^$$/ d' -e 's/$$/ :/' < $(@:.o=.d) >> $(@:.o=.P); \
  125. $(RM) $(@:.o=.d)
  126. # ASM sources lower case .s
  127. vpath %.s . $(TOP)
  128. $(BUILD)/obj/%.o: %.s
  129. @echo AS $(notdir $@)
  130. $(QUIET)$(CC) -x assembler-with-cpp $(ASFLAGS) -c -o $@ $<
  131. # ASM sources upper case .S
  132. vpath %.S . $(TOP)
  133. $(BUILD)/obj/%.o: %.S
  134. @echo AS $(notdir $@)
  135. $(QUIET)$(CC) -x assembler-with-cpp $(ASFLAGS) -c -o $@ $<
  136. # Flash binary using Jlink, should be added into system path
  137. ifeq ($(OS),Windows_NT)
  138. JLINKEXE = JLink.exe
  139. else
  140. JLINKEXE = JLinkExe
  141. endif
  142. # default jlink interface is swd
  143. ifeq ($(JLINK_IF),)
  144. JLINK_IF = swd
  145. endif
  146. # Flash using jlink
  147. flash-jlink: $(BUILD)/$(BOARD)-firmware.hex
  148. @echo halt > $(BUILD)/$(BOARD).jlink
  149. @echo loadfile $^ >> $(BUILD)/$(BOARD).jlink
  150. @echo r >> $(BUILD)/$(BOARD).jlink
  151. @echo go >> $(BUILD)/$(BOARD).jlink
  152. @echo exit >> $(BUILD)/$(BOARD).jlink
  153. $(JLINKEXE) -device $(JLINK_DEVICE) -if $(JLINK_IF) -speed auto -CommandFile $(BUILD)/$(BOARD).jlink
  154. size: $(BUILD)/$(BOARD)-firmware.elf
  155. -@echo ''
  156. @$(SIZE) $<
  157. -@echo ''
  158. clean:
  159. rm -rf build-$(BOARD)