-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile-stm32
250 lines (189 loc) · 10.7 KB
/
Makefile-stm32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# Adjast for your board
# Manually add them which Arduino IDE gives automotically
## NUCLEO-F411RE
MCU ?= cortex-m4
FPU ?= fpv4-sp-d16
ISAFLAGS = -mcpu=$(MCU) -mfpu=$(FPU) -mfloat-abi=hard -mthumb
VARIANT = NUCLEO_F4x1RE
HAL_SRC = STM32F4xx
ARD_CFLAGS := -DSTM32F4xx -DARDUINO=10813 -DARDUINO_NUCLEO_F411RE -DARDUINO_ARCH_STM32 -DBOARD_NAME="NUCLEO_F411RE" -DSTM32F411xE -DHAL_UART_MODULE_ENABLED
MASS_OPTION = -O "NODE_F411RE,NUCLEO"
## Select the serial port for your board
#MONITOR_PORT ?= /dev/ttyACM0
#MONITOR_PORT ?= /dev/ttyUSB0
# List library names you only use in your sources
ARDUINO_LIBS ?= SoftwareSerial SD SeeedGrayOLED USBcore Wire
USER_LIB_PATH ?= $(HOME)/sketchbook/libraries
# File name of your sources
SRC := main.cpp
# File name of generated binary to upload to Arduino
TARGET := uploadimg
ELF := $(TARGET).elf
BIN := $(TARGET).bin
HEX := $(TARGET).hex
# Change them where Arduino IDE is installed
ARM_GCC_PATH := $(HOME)/.arduino15/packages/STM32/tools/xpack-arm-none-eabi-gcc/9.2.1-1.1/bin/
ARDUINO_DIR = $(HOME)/.arduino15/packages/STM32/hardware/stm32/1.9.0
ARDUINO_CORE_PATH = $(ARDUINO_DIR)/cores/arduino
ARDUINO_VAR_PATH ?= $(ARDUINO_DIR)/variants
TOOLS_DIR = $(HOME)/.arduino15/packages/STM32/tools
CMSIS_DIR = $(TOOLS_DIR)/CMSIS/5.5.1
# Only have to edit above
#############################################################################
# Others below are for building and linking libraries automatically.
#SHELL = /bin/bash -xue
OBJDIR = .
CORE_LIB = $(OBJDIR)/libcore.a
CC := $(ARM_GCC_PATH)/arm-none-eabi-gcc
CXX := $(ARM_GCC_PATH)/arm-none-eabi-g++
OBJCOPY := $(ARM_GCC_PATH)/arm-none-eabi-objcopy
AR := $(ARM_GCC_PATH)/arm-none-eabi-gcc-ar
MASS_TOOL := $(TOOLS_DIR)/STM32Tools/1.4.0/tools/linux/massStorageCopy.sh
all: $(CORE_LIB) $(ELF) $(BIN) $(HEX)
CFLAGS_STD = -std=gnu11 -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500
CXXFLAGS_STD = -std=gnu++14 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -fno-use-cxa-atexit
LOCAL_C_SRCS ?= $(wildcard *.c)
LOCAL_CPP_SRCS ?= $(wildcard *.cpp)
LOCAL_CC_SRCS ?= $(wildcard *.cc)
LOCAL_PDE_SRCS ?= $(wildcard *.pde)
LOCAL_INO_SRCS ?= $(wildcard *.ino)
LOCAL_AS_SRCS ?= $(wildcard *.S)
LOCAL_SRCS = $(LOCAL_C_SRCS) $(LOCAL_CPP_SRCS) \
$(LOCAL_CC_SRCS) $(LOCAL_PDE_SRCS) \
$(LOCAL_INO_SRCS) $(LOCAL_AS_SRCS)
ARDUINO_LIB_PATH = $(ARDUINO_DIR)/libraries
STM32_HAL_PATH = $(ARDUINO_DIR)/system
ARDUINO_LIBS += $(filter $(notdir $(wildcard $(ARDUINO_LIB_PATH)/*)), \
$(shell sed -ne 's/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p' $(LOCAL_SRCS)))
ARDUINO_LIBS += $(filter $(notdir $(wildcard $(USER_LIB_PATH)/*)), \
$(shell sed -ne 's/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p' $(LOCAL_SRCS)))
USER_LIBS := $(sort $(wildcard $(patsubst %,$(USER_LIB_PATH)/%,$(ARDUINO_LIBS))))
USER_LIB_NAMES := $(patsubst $(USER_LIB_PATH)/%,%,$(USER_LIBS))
SYS_LIBS := $(sort $(wildcard $(patsubst %,$(ARDUINO_LIB_PATH)/%,$(filter-out $(USER_LIB_NAMES),$(ARDUINO_LIBS)))))
SYS_LIB_NAMES := $(patsubst $(ARDUINO_LIB_PATH)/%,%,$(SYS_LIBS))
rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
get_library_includes = $(if $(and $(wildcard $(1)/src), $(wildcard $(1)/library.properties)), \
-I$(1)/src, \
$(addprefix -I,$(1) $(wildcard $(1)/utility)))
SYS_INCLUDES := $(foreach lib, $(SYS_LIBS), $(call get_library_includes,$(lib)))
SYS_INCLUDES += -I$(ARDUINO_CORE_PATH)/stm32
USER_INCLUDES := $(foreach lib, $(USER_LIBS), $(call get_library_includes,$(lib)))
HAL_LIB_PATH = $(ARDUINO_DIR)/libraries/SrcWrapper/src
HAL_DRV_PATH = $(STM32_HAL_PATH)/Drivers/$(HAL_SRC)_HAL_Driver/Src
HAL_PATH = $(HAL_LIB_PATH) $(HAL_DRV_PATH)
HAL_C_SRCS += $(call rwildcard ,$(HAL_LIB_PATH),*.c)
HAL_CPP_SRCS += $(call rwildcard ,$(HAL_LIB_PATH),*.cpp)
HAL_AS_SRCS += $(call rwildcard ,$(HAL_LIB_PATH),*.S)
DRV_C_SRCS += $(call rwildcard ,$(HAL_DRV_PATH),*.c)
VAL_C_SRCS += $(call rwildcard ,$(ARDUINO_VAR_PATH)/$(VARIANT),*.c)
VAL_CPP_SRCS += $(call rwildcard ,$(ARDUINO_VAR_PATH)/$(VARIANT),*.cpp)
HAL_INCLUDES += -I$(STM32_HAL_PATH)/Drivers/$(HAL_SRC)_HAL_Driver/Inc
HAL_INCLUDES += -I$(STM32_HAL_PATH)/Drivers/$(HAL_SRC)_HAL_Driver/Src
HAL_INCLUDES += -I$(STM32_HAL_PATH)/$(HAL_SRC)
HAL_INCLUDES += -I$(STM32_HAL_PATH)/Drivers/CMSIS/Device/ST/$(HAL_SRC)/Include
HAL_INCLUDES += -I$(STM32_HAL_PATH)/Drivers/CMSIS/Device/ST/$(HAL_SRC)/Source/Templates/gcc
HAL_INCLUDES += -I$(STM32_HAL_PATH)/Middlewares/ST/STM32_USB_Device_Library/Core/Inc
HAL_INCLUDES += -I$(STM32_HAL_PATH)/Middlewares/ST/STM32_USB_Device_Library/Cort/Src
HAL_INCLUDES += -I$(STM32_HAL_PATH)/Middlewares/OpenAMP
HAL_INCLUDES += -I$(STM32_HAL_PATH)/Middlewares/OpenAMP/open-amp/lib/include
HAL_INCLUDES += -I$(STM32_HAL_PATH)/Middlewares/OpenAMP/libmetal/lib/include
HAL_INCLUDES += -I$(STM32_HAL_PATH)/Middlewares/OpenAMP/virtual_driver
HAL_OBJ_FILES = $(HAL_C_SRCS:.c=.c.o) $(HAL_CPP_SRCS:.cpp=.cpp.o) $(HAL_AS_SRCS:.S=.S.o)
DRV_OBJ_FILES = $(DRV_C_SRCS:.c=.c.o) $(DRV_CPP_SRCS:.cpp=.cpp.o) $(DRV_AS_SRCS:.S=.S.o)
VAL_OBJ_FILES = $(VAL_C_SRCS:.c=.c.o) $(VAL_CPP_SRCS:.cpp=.cpp.o) $(VAL_AS_SRCS:.S=.S.o)
CORE_C_SRCS = $(call rwildcard ,$(ARDUINO_CORE_PATH),*.c)
CORE_CPP_SRCS = $(call rwildcard ,$(ARDUINO_CORE_PATH),*.cpp)
CORE_AS_SRCS = $(call rwildcard ,$(ARDUINO_CORE_PATH),*.S)
CORE_INCLUDES += -I$(ARDUINO_CORE_PATH)/avr
CORE_INCLUDES += -I$(ARDUINO_CORE_PATH)/stm32
CORE_INCLUDES += -I$(ARDUINO_CORE_PATH)/stm32/LL
CORE_INCLUDES += -I$(ARDUINO_CORE_PATH)/stm32/usb
CORE_INCLUDES += -I$(ARDUINO_CORE_PATH)/stm32/OpenAMP
CORE_INCLUDES += -I$(ARDUINO_CORE_PATH)/stm32/usb/hid
CORE_INCLUDES += -I$(ARDUINO_CORE_PATH)/stm32/usb/cdc
TOOL_INC_PATH = -I$(CMSIS_DIR)/CMSIS/Core/Include
TOOL_INC_PATH += -I$(CMSIS_DIR)/CMSIS/DSP/Include
INCLUDES = $(ARD_CFLAGS) $(CORE_INCLUDES) $(SYS_INCLUDES) $(HAL_INCLUDES) \
$(TOOL_INC_PATH) $(USER_INCLUDES) \
-I$(ARDUINO_CORE_PATH) -I$(ARDUINO_VAR_PATH)/$(VARIANT)
CPPFLAGS += $(ISAFLAGS) -Os $(INCLUDES)
CORE_OBJ_FILES = $(CORE_C_SRCS:.c=.c.o) $(CORE_CPP_SRCS:.cpp=.cpp.o) $(CORE_AS_SRCS:.S=.S.o)
MKDIR = mkdir -p
$(OBJDIR)/core/%.c.o: $(ARDUINO_CORE_PATH)/%.c $(COMMON_DEPS) | $(OBJDIR)
@$(MKDIR) $(dir $@)
$(CC) -MMD -c $(CPPFLAGS) $(CFLAGS_STD) $< -o $@
$(OBJDIR)/core/%.c.o: $(ARDUINO_VAR_PATH)/$(VARIANT)/%.c $(COMMON_DEPS) | $(OBJDIR)
@$(MKDIR) $(dir $@)
$(CC) -MMD -c $(CPPFLAGS) $(CFLAGS_STD) $< -o $@
$(OBJDIR)/core/%.cpp.o: $(ARDUINO_CORE_PATH)/%.cpp $(COMMON_DEPS) | $(OBJDIR)
@$(MKDIR) $(dir $@)
$(CXX) -MMD -c $(CPPFLAGS) $(CXXFLAGS_STD) $< -o $@
$(OBJDIR)/core/%.cpp.o: $(ARDUINO_VAR_PATH)/$(VARIANT)/%.cpp $(COMMON_DEPS) | $(OBJDIR)
@$(MKDIR) $(dir $@)
$(CXX) -MMD -c $(CPPFLAGS) $(CXXFLAGS_STD) $< -o $@
$(OBJDIR)/core/%.S.o: $(ARDUINO_CORE_PATH)/%.S $(COMMON_DEPS) | $(OBJDIR)
@$(MKDIR) $(dir $@)
$(CC) -MMD -c $(CPPFLAGS) $(ASFLAGS) $< -o $@
VAL_OBJS = $(patsubst $(ARDUINO_VAR_PATH)/$(VARIANT)/%, $(OBJDIR)/core/%,$(VAL_OBJ_FILES))
CORE_OBJS = $(patsubst $(ARDUINO_CORE_PATH)/%, $(OBJDIR)/core/%,$(CORE_OBJ_FILES))
$(OBJDIR)/hal/%.c.o: $(HAL_LIB_PATH)/%.c $(COMMON_DEPS) | $(OBJDIR)
@$(MKDIR) $(dir $@)
$(CC) -MMD -c $(CPPFLAGS) $(CFLAGS_STD) $< -o $@
$(OBJDIR)/hal/%.c.o: $(HAL_DRV_PATH)/%.c $(COMMON_DEPS) | $(OBJDIR)
@$(MKDIR) $(dir $@)
$(CC) -MMD -c $(CPPFLAGS) $(CFLAGS_STD) $< -o $@
$(OBJDIR)/hal/%.cpp.o: $(HAL_LIB_PATH)/%.cpp $(COMMON_DEPS) | $(OBJDIR)
@$(MKDIR) $(dir $@)
$(CXX) -MMD -c $(CPPFLAGS) $(CXXFLAGS_STD) $< -o $@
$(OBJDIR)/hal/%.S.o: $(HAL_LIB_PATH)/%.S $(COMMON_DEPS) | $(OBJDIR)
@$(MKDIR) $(dir $@)
$(CC) -MMD -c $(CPPFLAGS) $(ASFLAGS) $< -o $@
HAL_OBJS = $(patsubst $(HAL_LIB_PATH)/%, $(OBJDIR)/hal/%,$(HAL_OBJ_FILES))
DRV_OBJS = $(patsubst $(HAL_DRV_PATH)/%, $(OBJDIR)/hal/%,$(DRV_OBJ_FILE))
get_library_files = $(if $(and $(wildcard $(1)/src), $(wildcard $(1)/library.properties)), \
$(call rwildcard,$(1)/src/,*.$(2)), \
$(wildcard $(1)/*.$(2) $(1)/utility/*.$(2)))
LIB_C_SRCS := $(foreach lib, $(SYS_LIBS), $(call get_library_files,$(lib),c))
LIB_CPP_SRCS := $(foreach lib, $(SYS_LIBS), $(call get_library_files,$(lib),cpp))
LIB_AS_SRCS := $(foreach lib, $(SYS_LIBS), $(call get_library_files,$(lib),S))
USER_LIB_CPP_SRCS := $(foreach lib, $(USER_LIBS), $(call get_library_files,$(lib),cpp))
USER_LIB_C_SRCS := $(foreach lib, $(USER_LIBS), $(call get_library_files,$(lib),c))
USER_LIB_AS_SRCS := $(foreach lib, $(USER_LIBS), $(call get_library_files,$(lib),S))
LIB_OBJS = $(patsubst $(ARDUINO_LIB_PATH)/%.c,$(OBJDIR)/libs/%.c.o,$(LIB_C_SRCS)) \
$(patsubst $(ARDUINO_LIB_PATH)/%.cpp,$(OBJDIR)/libs/%.cpp.o,$(LIB_CPP_SRCS)) \
$(patsubst $(ARDUINO_LIB_PATH)/%.S,$(OBJDIR)/libs/%.S.o,$(LIB_AS_SRCS))
USER_LIB_OBJS = $(patsubst $(USER_LIB_PATH)/%.cpp,$(OBJDIR)/userlibs/%.cpp.o,$(USER_LIB_CPP_SRCS)) \
$(patsubst $(USER_LIB_PATH)/%.c,$(OBJDIR)/userlibs/%.c.o,$(USER_LIB_C_SRCS)) \
$(patsubst $(USER_LIB_PATH)/%.S,$(OBJDIR)/userlibs/%.S.o,$(USER_LIB_AS_SRCS))
$(OBJDIR)/libs/%.c.o: $(ARDUINO_LIB_PATH)/%.c $(COMMON_DEPS) | $(OBJDIR)
@$(MKDIR) $(dir $@)
$(CC) -MMD -c $(CPPFLAGS) $(CFLAGS_STD) $< -o $@
$(OBJDIR)/libs/%.cpp.o: $(ARDUINO_LIB_PATH)/%.cpp $(COMMON_DEPS) | $(OBJDIR)
@$(MKDIR) $(dir $@)
$(CXX) -MMD -c $(CPPFLAGS) $(CXXFLAGS_STD) $< -o $@
$(OBJDIR)/libs/%.S.o: $(ARDUINO_LIB_PATH)/%.S $(COMMON_DEPS) | $(OBJDIR)
@$(MKDIR) $(dir $@)
$(CC) -MMD -c $(CPPFLAGS) $(ASFLAGS) $< -o $@
$(OBJDIR)/userlibs/%.c.o: $(USER_LIB_PATH)/%.c
@$(MKDIR) $(dir $@)
$(CC) -MMD -c $(CPPFLAGS) $(CFLAGS_STD) $< -o $@
$(OBJDIR)/userlibs/%.cpp.o: $(USER_LIB_PATH)/%.cpp
@$(MKDIR) $(dir $@)
$(CXX) -MMD -c $(CPPFLAGS) $(CXXFLAGS_STD) $< -o $@
$(OBJDIR)/userlibs/%.S.o: $(USER_LIB_PATH)/%.S
@$(MKDIR) $(dir $@)
$(CC) -MMD -c $(CPPFLAGS) $(ASFLAGS) $< -o $@
LDFLAGS = $(ISAFLAGS) -Os --specs=nano.specs -Wl,--defsym=LD_FLASH_OFFSET=0 -Wl,--defsym=LD_MAX_SIZE=524288 -Wl,--defsym=LD_MAX_DATA_SIZE=131072 -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--entry=Reset_Handler -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--default-script=$(ARDUINO_DIR)/variants/$(VARIANT)/ldscript.ld -Wl,--script=$(ARDUINO_DIR)/system/ldscript.ld -L$(CMSIS_DIR)/CMSIS/DSP/Lib/GCC/ -larm_cortexM4lf_math -Wl,--start-group
$(CORE_LIB): $(VAL_OBJS) $(CORE_OBJS) $(HAL_OBJS) $(DRV_OBJS) $(LIB_OBJS) $(USER_LIB_OBJS)
$(AR) rcs $@ $^
# Building arduino binary image
$(ELF): $(SRC) $(CORE_LIB)
$(CC) $(INCLUDES) $(LDFLAGS) $^ -lc -Wl,--end-group -lm -lgcc -lstdc++ -o $@
$(BIN): $(ELF)
$(OBJCOPY) -O binary $< $@
$(HEX): $(ELF)
$(OBJCOPY) -O ihex -R .eeprom $< $@
upload-mass: $(BIN)
$(MASS_TOOL) -I $< $(MASS_OPTION)
clean:
rm -fr $(BIN) $(HEX) $(CORE_LIB) $(OBJDIR)/core $(OBJDIR)/hal $(OBJDIR)/libs $(OBJDIR)/platformlibs $(OBJDIR)/userlibs