-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMakefile
263 lines (217 loc) · 9.29 KB
/
Makefile
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
251
252
253
254
255
256
257
258
259
260
261
262
263
#-------------------------------------------------------------------------------
# Provide default TARGET if none was specified
# Determine if target is F3 or F4 class processor (also verifies TARGET is valid)
#-------------------------------------------------------------------------------
TARGET ?= CRAZYBEEF3FS
F4_TARGETS := OMNIBUSF4 NOX MATEKF411RX
F3_TARGETS := OMNIBUS CRAZYBEEF3FS
IS_F4_TARGET = 0
IS_F3_TARGET = 0
# if the result of searching for $(TARGET) in $(F4_TARGETS) is not empty
ifneq ($(filter $(TARGET),$(F4_TARGETS)),)
IS_F4_TARGET = 1
else ifneq ($(filter $(TARGET),$(F3_TARGETS)),)
IS_F3_TARGET = 1
else
$(error TARGET must be in: $(F4_TARGETS) $(F3_TARGETS))
endif
#-------------------------------------------------------------------------------
# STM32CubeMXProgrammer
# To install on Mac OS I had to download and unzip the package and run:
# java -jar SetupSTM32CubeProgrammer-2.4.0.exe
# This launched the installer executable
#-------------------------------------------------------------------------------
ifeq ($(OS),Windows_NT)
STM_CUBE_PROGRAMMER = STM32_Programmer_CLI.exe
else
STM_CUBE_PROGRAMMER = /Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app/Contents/MacOs/bin/STM32_Programmer_CLI
endif
#-------------------------------------------------------------------------------
# GNU ARM Embedded Toolchain
#-------------------------------------------------------------------------------
CC=arm-none-eabi-gcc
CXX=arm-none-eabi-g++
LD=arm-none-eabi-ld
AR=arm-none-eabi-ar
AS=arm-none-eabi-as
CP=arm-none-eabi-objcopy
OD=arm-none-eabi-objdump
NM=arm-none-eabi-nm
SIZE=arm-none-eabi-size
A2L=arm-none-eabi-addr2line
#-------------------------------------------------------------------------------
# Working directories
#-------------------------------------------------------------------------------
DEBUG ?= 0
ifeq ($(DEBUG), 1)
OBJECT_DIR = gcc_debug/$(TARGET)
else
OBJECT_DIR = gcc_release/$(TARGET)
endif
#-------------------------------------------------------------------------------
# Source files (SOURCES), Include folders (INCLUDE_DIRS), Defines (DEFINES)
#-------------------------------------------------------------------------------
BASE_DIR = .
#BASE_DIR = ..
include source.mk
include stm32cubemx.mk
#-------------------------------------------------------------------------------
# Object List
#-------------------------------------------------------------------------------
OBJECTS=$(addsuffix .o,$(addprefix $(OBJECT_DIR)/,$(basename $(SOURCES))))
DEPS=$(addsuffix .d,$(addprefix $(OBJECT_DIR)/,$(basename $(SOURCES))))
#-------------------------------------------------------------------------------
# Target Output Files
#-------------------------------------------------------------------------------
TARGET_ELF=$(OBJECT_DIR)/$(TARGET).elf
TARGET_HEX=$(OBJECT_DIR)/$(TARGET).hex
TARGET_BIN=$(OBJECT_DIR)/$(TARGET).bin
#-------------------------------------------------------------------------------
# Flags
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#######################################
# CFLAGS
#######################################
# cpu
CPU = -mcpu=cortex-m4
# fpu
FPU = -mfpu=fpv4-sp-d16
# float-abi
FLOAT-ABI = -mfloat-abi=hard
# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
# Default Tool options - can be overridden in {mcu}.mk files.
#
CC_DEBUG_OPTIMISATION := -ggdb3 -Og -DDEBUG
CC_DEFAULT_OPTIMISATION := -O2
CC_SPEED_OPTIMISATION := -Ofast -DSPEED_OPTIMISED_SRC
CC_SIZE_OPTIMISATION := -Os
CC_NONE_OPTIMISATION := -O0
# compile gcc flags
CFLAGS = $(MCU) $(addprefix -D,$(DEFINES)) $(addprefix -I,$(INCLUDE_DIRS)) -Wall -Wextra -Wunsafe-loop-optimizations -Wdouble-promotion -fdata-sections -ffunction-sections
# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
# C++ compile flags are just lie C flags but with some additional items
CXXFLAGS= $(CFLAGS) -fno-rtti -fno-exceptions -U__STRICT_ANSI__
ASMFLAGS= -x assembler-with-cpp
#-------------------------------------------------------------------------------
# LDFLAGS
#######################################
# libraries
LIBS = -lc -lm -lnosys
LIBDIR =
LDFLAGS = $(MCU) --specs=nano.specs --specs=nosys.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(OBJECT_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
#-------------------------------------------------------------------------------
# Build
#
# $@ The name of the target file (the one before the colon)
# $< The name of the first (or only) prerequisite file (the first one after the colon)
# $^ The names of all the prerequisite files (space separated)
# $* The stem (the bit which matches the % wildcard in the rule definition.
#
#-------------------------------------------------------------------------------
$(TARGET_HEX): $(TARGET_ELF) $(TARGET_BIN)
$(CP) -O ihex --set-start 0x0000000 $< $@
$(TARGET_BIN): $(TARGET_ELF)
$(CP) -O binary $< $@
$(TARGET_ELF): $(OBJECTS)
$(CXX) -o $@ $^ $(LDFLAGS)
$(SIZE) $(TARGET_ELF)
# Compile
SEPARATE_BUILDS ?= 1
ifeq ($(SEPARATE_BUILDS), 1)
ifeq ($(DEBUG), 1)
$(OBJECT_DIR)/%.o: %.cpp
@mkdir -p $(dir $@)
@echo "%% (debug) $(notdir $<)" "$(STDOUT)" && \
$(CXX) -c -o $@ $(CXXFLAGS) $(CC_DEBUG_OPTIMISATION) $<
else
$(OBJECT_DIR)/%.o: %.cpp
@mkdir -p $(dir $@)
$(if $(findstring $<,$(SPEED_OPTIMISED_SRC)), \
echo "%% (speed optimised) $(notdir $<)" "$(STDOUT)" && \
$(CXX) -c -o $@ $(CXXFLAGS) $(CC_SPEED_OPTIMISATION) $<, \
$(if $(findstring $<,$(SIZE_OPTIMISED_SRC)), \
echo "%% (size optimised) $(notdir $<)" "$(STDOUT)" && \
$(CXX) -c -o $@ $(CXXFLAGS) $(CC_SIZE_OPTIMISATION) $<, \
$(if $(findstring $<,$(NONE_OPTIMISED_SRC)), \
echo "%% (not optimised) $(notdir $<)" "$(STDOUT)" && \
$(CXX) -c -o $@ $(CXXFLAGS) $(CC_NONE_OPTIMISATION) $<, \
$(if $(findstring $<,$(DEBUG_OPTIMISED_SRC)), \
echo "%% (debug optimised) $(notdir $<)" "$(STDOUT)" && \
$(CXX) -c -o $@ $(CXXFLAGS) $(CC_DEBUG_OPTIMISATION) $<, \
echo "%% $(notdir $<)" "$(STDOUT)" && \
$(CXX) -c -o $@ $(CXXFLAGS) $(CC_DEFAULT_OPTIMISATION) $<))))
endif
ifeq ($(DEBUG), 1)
$(OBJECT_DIR)/%.o: %.c
@mkdir -p $(dir $@)
@echo "%% (debug) $(notdir $<)" "$(STDOUT)" && \
$(CC) -c -o $@ $(CFLAGS) $(CC_DEBUG_OPTIMISATION) $<
else
$(OBJECT_DIR)/%.o: %.c
@mkdir -p $(dir $@)
$(if $(findstring $<,$(SPEED_OPTIMISED_SRC)), \
echo "%% (speed optimised) $(notdir $<)" "$(STDOUT)" && \
$(CC) -c -o $@ $(CFLAGS) $(CC_SPEED_OPTIMISATION) $<, \
$(if $(findstring $<,$(SIZE_OPTIMISED_SRC)), \
echo "%% (size optimised) $(notdir $<)" "$(STDOUT)" && \
$(CC) -c -o $@ $(CFLAGS) $(CC_SIZE_OPTIMISATION) $<, \
$(if $(findstring $<,$(NONE_OPTIMISED_SRC)), \
echo "%% (not optimised) $(notdir $<)" "$(STDOUT)" && \
$(CC) -c -o $@ $(CFLAGS) $(CC_NONE_OPTIMISATION) $<, \
$(if $(findstring $<,$(DEBUG_OPTIMISED_SRC)), \
echo "%% (debug optimised) $(notdir $<)" "$(STDOUT)" && \
$(CC) -c -o $@ $(CFLAGS) $(CC_DEBUG_OPTIMISATION) $<, \
echo "%% $(notdir $<)" "$(STDOUT)" && \
$(CC) -c -o $@ $(CFLAGS) $(CC_DEFAULT_OPTIMISATION) $<))))
endif
else
$(OBJECT_DIR)/%.o: %.cpp
@mkdir -p $(dir $@)
@echo %% $(notdir $<)
@$(CXX) -c -o $@ $(CXXFLAGS) $(CC_DEFAULT_OPTIMISATION) $<
$(OBJECT_DIR)/%.o: %.c
@mkdir -p $(dir $@)
@echo %% $(notdir $<)
@$(CC) -c -o $@ $(CFLAGS) $(CC_DEFAULT_OPTIMISATION) $<
endif
$(OBJECT_DIR)/%.o: %.s
@mkdir -p $(dir $@)
@echo %% $(notdir $<)
@$(CC) -c -o $@ $(ASMFLAGS) $<
$(OBJECT_DIR)/%.o: %.S
@mkdir -p $(dir $@)
@echo %% $(notdir $<)
@$(CC) -c -o $@ $(ASMFLAGS) $<
#-------------------------------------------------------------------------------
# Recipes
#-------------------------------------------------------------------------------
.PHONY: all flash clean
clean:
rm -rf $(OBJECTS) $(DEPS) $(TARGET_ELF) $(TARGET_HEX) $(OBJECT_DIR)/output.map
ifeq ($(TARGET),OMNIBUSF4)
flash: $(TARGET_ELF)
openocd -d2 -f interface/stlink.cfg -c "transport select hla_swd" -f target/stm32f4x.cfg -c "reset_config none" -c "program $(TARGET_ELF) verify reset; shutdown;"
else
flash: $(TARGET_ELF)
# STM32_Programmer_CLI.exe --connect port=USB1 -w $(TARGET_ELF) --start
$(STM_CUBE_PROGRAMMER) --connect port=USB1 -w $(TARGET_ELF) --start
endif
# Flash using ST-Link adapter
stlinkflash: $(TARGET_ELF)
openocd -d2 -f interface/stlink.cfg -c "transport select hla_swd" -f target/stm32f4x.cfg -c "reset_config none" -c "program $(TARGET_ELF) verify reset; shutdown;"
# Flash F3 device using ST-Link adapter
stlinkflashf3: $(TARGET_ELF)
openocd -d2 -f interface/stlink.cfg -c "transport select hla_swd" -f target/stm32f3x.cfg -c "reset_config none" -c "program $(TARGET_ELF) verify reset; shutdown;"
# Flash using STM32CubeProgrammer via USB
download: $(TARGET_ELF)
# STM32_Programmer_CLI.exe --connect port=USB1 -w $(TARGET_ELF) --start
# /Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app/Contents/MacOs/bin/STM32_Programmer_CLI --connect port=USB1 -w $(TARGET_ELF) --start
$(STM_CUBE_PROGRAMMER) --connect port=USB1 -w $(TARGET_ELF) --start
# Choose one of the following "all"
#all: $(TARGET_BIN)
all: $(TARGET_HEX)
$(DEPS):
include $(wildcard $(DEPS))