blob: 95a9c9879729586ca6f6b96cf39a48f22d51b858 [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001# ---------------------------------------
2# Common make rules for all examples
3# ---------------------------------------
4
5# Set all as default goal
6.DEFAULT_GOAL := all
7
8# ESP32-Sx and RP2040 has its own CMake build system
9ifeq (,$(findstring $(FAMILY),esp32s2 esp32s3 rp2040))
10
11# ---------------------------------------
12# Compiler Flags
13# ---------------------------------------
14
15LIBS_GCC ?= -lgcc -lm -lnosys
16
17# libc
18LIBS += $(LIBS_GCC)
19
20ifneq ($(BOARD), spresense)
21LIBS += -lc
22endif
23
24# TinyUSB Stack source
25SRC_C += \
26 src/tusb.c \
27 src/common/tusb_fifo.c \
28 src/device/usbd.c \
29 src/device/usbd_control.c \
30 src/class/audio/audio_device.c \
31 src/class/cdc/cdc_device.c \
32 src/class/dfu/dfu_device.c \
33 src/class/dfu/dfu_rt_device.c \
34 src/class/hid/hid_device.c \
35 src/class/midi/midi_device.c \
36 src/class/msc/msc_device.c \
37 src/class/net/ecm_rndis_device.c \
38 src/class/net/ncm_device.c \
39 src/class/usbtmc/usbtmc_device.c \
40 src/class/video/video_device.c \
41 src/class/vendor/vendor_device.c
42
43# TinyUSB stack include
44INC += $(TOP)/src
45
46CFLAGS += $(addprefix -I,$(INC))
47
48# LTO makes it difficult to analyze map file for optimizing size purpose
49# We will run this option in ci
50ifeq ($(NO_LTO),1)
51CFLAGS := $(filter-out -flto,$(CFLAGS))
52endif
53
54ifneq ($(LD_FILE),)
55LDFLAGS_LD_FILE ?= -Wl,-T,$(TOP)/$(LD_FILE)
56endif
57
58LDFLAGS += $(CFLAGS) $(LDFLAGS_LD_FILE) -Wl,-Map=$@.map -Wl,-cref -Wl,-gc-sections
59ifneq ($(SKIP_NANOLIB), 1)
60LDFLAGS += -specs=nosys.specs -specs=nano.specs
61endif
62
63ASFLAGS += $(CFLAGS)
64
65# Assembly files can be name with upper case .S, convert it to .s
66SRC_S := $(SRC_S:.S=.s)
67
68# Due to GCC LTO bug https://bugs.launchpad.net/gcc-arm-embedded/+bug/1747966
69# assembly file should be placed first in linking order
70# '_asm' suffix is added to object of assembly file
71OBJ += $(addprefix $(BUILD)/obj/, $(SRC_S:.s=_asm.o))
72OBJ += $(addprefix $(BUILD)/obj/, $(SRC_C:.c=.o))
73
74# Verbose mode
75ifeq ("$(V)","1")
76$(info CFLAGS $(CFLAGS) ) $(info )
77$(info LDFLAGS $(LDFLAGS)) $(info )
78$(info ASFLAGS $(ASFLAGS)) $(info )
79endif
80
81# ---------------------------------------
82# Rules
83# ---------------------------------------
84
85all: $(BUILD)/$(PROJECT).bin $(BUILD)/$(PROJECT).hex size
86
87uf2: $(BUILD)/$(PROJECT).uf2
88
89OBJ_DIRS = $(sort $(dir $(OBJ)))
90$(OBJ): | $(OBJ_DIRS)
91$(OBJ_DIRS):
92ifeq ($(CMDEXE),1)
93 @$(MKDIR) $(subst /,\,$@)
94else
95 @$(MKDIR) -p $@
96endif
97
98$(BUILD)/$(PROJECT).elf: $(OBJ)
99 @echo LINK $@
100 @$(CC) -o $@ $(LDFLAGS) $^ -Wl,--start-group $(LIBS) -Wl,--end-group
101
102$(BUILD)/$(PROJECT).bin: $(BUILD)/$(PROJECT).elf
103 @echo CREATE $@
104 @$(OBJCOPY) -O binary $^ $@
105
106$(BUILD)/$(PROJECT).hex: $(BUILD)/$(PROJECT).elf
107 @echo CREATE $@
108 @$(OBJCOPY) -O ihex $^ $@
109
110# UF2 generation, iMXRT need to strip to text only before conversion
111ifeq ($(FAMILY),imxrt)
112$(BUILD)/$(PROJECT).uf2: $(BUILD)/$(PROJECT).elf
113 @echo CREATE $@
114 @$(OBJCOPY) -O ihex -R .flash_config -R .ivt $^ $(BUILD)/$(PROJECT)-textonly.hex
115 $(PYTHON) $(TOP)/tools/uf2/utils/uf2conv.py -f $(UF2_FAMILY_ID) -c -o $@ $(BUILD)/$(PROJECT)-textonly.hex
116else
117$(BUILD)/$(PROJECT).uf2: $(BUILD)/$(PROJECT).hex
118 @echo CREATE $@
119 $(PYTHON) $(TOP)/tools/uf2/utils/uf2conv.py -f $(UF2_FAMILY_ID) -c -o $@ $^
120endif
121
122copy-artifact: $(BUILD)/$(PROJECT).bin $(BUILD)/$(PROJECT).hex $(BUILD)/$(PROJECT).uf2
123
124# We set vpath to point to the top of the tree so that the source files
125# can be located. By following this scheme, it allows a single build rule
126# to be used to compile all .c files.
127vpath %.c . $(TOP)
128$(BUILD)/obj/%.o: %.c
129 @echo CC $(notdir $@)
130 @$(CC) $(CFLAGS) -c -MD -o $@ $<
131
132# ASM sources lower case .s
133vpath %.s . $(TOP)
134$(BUILD)/obj/%_asm.o: %.s
135 @echo AS $(notdir $@)
136 @$(CC) -x assembler-with-cpp $(ASFLAGS) -c -o $@ $<
137
138# ASM sources upper case .S
139vpath %.S . $(TOP)
140$(BUILD)/obj/%_asm.o: %.S
141 @echo AS $(notdir $@)
142 @$(CC) -x assembler-with-cpp $(ASFLAGS) -c -o $@ $<
143
144endif # GNU Make
145
146size: $(BUILD)/$(PROJECT).elf
147 -@echo ''
148 @$(SIZE) $<
149 -@echo ''
150
151# linkermap must be install previously at https://github.com/hathach/linkermap
152linkermap: $(BUILD)/$(PROJECT).elf
153 @linkermap -v $<.map
154
155.PHONY: clean
156clean:
157ifeq ($(CMDEXE),1)
158 rd /S /Q $(subst /,\,$(BUILD))
159else
160 $(RM) -rf $(BUILD)
161endif
162
163# ---------------------------------------
164# Flash Targets
165# ---------------------------------------
166
167# Jlink binary
168ifeq ($(OS),Windows_NT)
169 JLINKEXE = JLink.exe
170else
171 JLINKEXE = JLinkExe
172endif
173
174# Jlink Interface
175JLINK_IF ?= swd
176
177# Flash using jlink
178flash-jlink: $(BUILD)/$(PROJECT).hex
179 @echo halt > $(BUILD)/$(BOARD).jlink
180 @echo r > $(BUILD)/$(BOARD).jlink
181 @echo loadfile $^ >> $(BUILD)/$(BOARD).jlink
182 @echo r >> $(BUILD)/$(BOARD).jlink
183 @echo go >> $(BUILD)/$(BOARD).jlink
184 @echo exit >> $(BUILD)/$(BOARD).jlink
185 $(JLINKEXE) -device $(JLINK_DEVICE) -if $(JLINK_IF) -JTAGConf -1,-1 -speed auto -CommandFile $(BUILD)/$(BOARD).jlink
186
187# Flash STM32 MCU using stlink with STM32 Cube Programmer CLI
188flash-stlink: $(BUILD)/$(PROJECT).elf
189 STM32_Programmer_CLI --connect port=swd --write $< --go
190
191# Flash using pyocd
192PYOCD_OPTION ?=
193flash-pyocd: $(BUILD)/$(PROJECT).hex
194 pyocd flash -t $(PYOCD_TARGET) $(PYOCD_OPTION) $<
195 pyocd reset -t $(PYOCD_TARGET)
196
197# Flash using openocd
198OPENOCD_OPTION ?=
199flash-openocd: $(BUILD)/$(PROJECT).elf
200 openocd $(OPENOCD_OPTION) -c "program $< verify reset exit"
201
202# flash with Black Magic Probe
203# This symlink is created by https://github.com/blacksphere/blackmagic/blob/master/driver/99-blackmagic.rules
204BMP ?= /dev/ttyBmpGdb
205
206flash-bmp: $(BUILD)/$(PROJECT).elf
207 $(GDB) --batch -ex 'target extended-remote $(BMP)' -ex 'monitor swdp_scan' -ex 'attach 1' -ex load $<
208
209debug-bmp: $(BUILD)/$(PROJECT).elf
210 $(GDB) -ex 'target extended-remote $(BMP)' -ex 'monitor swdp_scan' -ex 'attach 1' $<
211
212#-------------- Artifacts --------------
213
214# Create binary directory
215$(BIN):
216 @$(MKDIR) -p $@
217
218# Copy binaries .elf, .bin, .hex, .uf2 to BIN for upload
219# due to large size of combined artifacts, only uf2 is uploaded for now
220copy-artifact: $(BIN)
221 @$(CP) $(BUILD)/$(PROJECT).uf2 $(BIN)
222 #@$(CP) $(BUILD)/$(PROJECT).bin $(BIN)
223 #@$(CP) $(BUILD)/$(PROJECT).hex $(BIN)
224 #@$(CP) $(BUILD)/$(PROJECT).elf $(BIN)
225
226# Print out the value of a make variable.
227# https://stackoverflow.com/questions/16467718/how-to-print-out-a-variable-in-makefile
228print-%:
229 @echo $* = $($*)