-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
247 lines (207 loc) · 7.59 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
# Name: Makefile_STM
# Author: Daniel Nery Silva de Oliveira
# ThundeRatz Robotics Team
# 08/2018
DEVICE_FAMILY := STM32F3xx
DEVICE_TYPE := STM32F303xx
DEVICE := STM32F303RE
DEVICE_LD := STM32F303RETx
DEVICE_DEF := STM32F303xE
TARGET = main
DEBUG = 1
######################################################################
# Tune the lines below only if you know what you are doing:
# Optmization
ifeq ($(DEBUG), 1)
OPT := -Og
else
OPT := -Os
endif
# Build Directory
BUILD_DIR := build
# Source Files
CUBE_SOURCES := $(wildcard cube/*/*.c) $(wildcard cube/*/*/*/*.c) \
$(wildcard Middlewares/*/*/*/*.c) $(wildcard Middlewares/*/*/*/*/*.c)
ASM_SOURCES := $(wildcard cube/*.s)
C_SOURCES := $(wildcard src/*.c)
# Executables
COMPILE := arm-none-eabi-gcc
OBJCOPY := arm-none-eabi-objcopy
SIZE := arm-none-eabi-size
GDB := arm-none-eabi-gdb
HEX := $(OBJCOPY) -O ihex
BIN := $(OBJCOPY) -O binary -S
# Defines
AS_DEFS :=
C_DEFS := \
-DUSE_HAL_DRIVER \
-D$(DEVICE_DEF) \
-DDEBUG \
-DBLE_CONFIG_DBG_ENABLE \
# Include Paths
AS_INCLUDES :=
C_INCLUDES := \
-Icube/Drivers/CMSIS/Device/ST/$(DEVICE_FAMILY)/Include \
-Icube/Drivers/CMSIS/Include \
-Icube/Drivers/$(DEVICE_FAMILY)_HAL_Driver/Inc \
-Icube/Drivers/$(DEVICE_FAMILY)_HAL_Driver/Inc/Legacy \
-Icube/Inc \
-Iinc \
-IMiddlewares/LowPowerManager/Inc \
-IMiddlewares/STM32_BlueNRG/Interface \
-IMiddlewares/STM32_BlueNRG/LibANCS/Inc \
-IMiddlewares/STM32_BlueNRG/LibProfCentr/Inc \
-IMiddlewares/STM32_BlueNRG/LibProfPeriph/Inc \
-IMiddlewares/STM32_BlueNRG/SimpleBlueNRG_HCI/includes \
-IMiddlewares/TimerServer/inc \
-IMiddlewares/TimerServer/STM32xx_HAL_TimerServer_Drivers/inc \
# Compile Flags
FLAGS := -mthumb
ifeq ($(DEVICE_FAMILY), $(filter $(DEVICE_FAMILY),STM32F0xx STM32L0xx))
FLAGS += -mcpu=cortex-m0
else ifeq ($(DEVICE_FAMILY), $(filter $(DEVICE_FAMILY),STM32F1xx STM32L1xx STM32F2xx STM32L2xx))
FLAGS += -mcpu=cortex-m3
else ifeq ($(DEVICE_FAMILY), $(filter $(DEVICE_FAMILY),STM32F3xx STM32L3xx STM32F4xx STM32L4xx))
FLAGS += -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard
else ifeq ($(DEVICE_FAMILY), $(filter $(DEVICE_FAMILY),STM32F7xx STM32L7xx))
FLAGS += -mcpu=cortex-m7 -mfpu=fpv4-sp-d16 -mfloat-abi=hard
else
$(error Unknown Device Family $(DEVICE_FAMILY))
endif
ASFLAGS := $(FLAGS) $(AS_DEFS) $(AS_INCLUDES) -Wall -Wextra -fdata-sections -ffunction-sections $(OPT)
CFLAGS := \
$(FLAGS) $(C_DEFS) $(C_INCLUDES) \
-Wall -Wextra -fdata-sections \
-ffunction-sections -fmessage-length=0 \
$(OPT) -std=c11 -MMD -MP \
ifeq ($(DEBUG), 1)
CFLAGS += -g3
endif
# Linker Flags
LDSCRIPT := cube/$(DEVICE_LD)_FLASH.ld
LIBS := -lc -lm -lnosys
LIBDIR :=
LDFLAGS := \
$(FLAGS) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) \
$(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref \
-Wl,--gc-sections \
# Object Files
CUBE_OBJECTS := $(addprefix $(BUILD_DIR)/,$(notdir $(CUBE_SOURCES:.c=.o)))
CUBE_OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
OBJECTS := $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(CUBE_SOURCES)))
vpath %.c $(sort $(dir $(C_SOURCES)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))
######################################################################
## Build Targets
######################################################################
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
@echo "Compiling $<"
$(COMPILE) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) -MF"$(@:.o=.d)" $< -o $@
@echo
$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
@echo "Compiling $<"
@$(COMPILE) -x assembler-with-cpp -c $(CFLAGS) -MF"$(@:%.o=%.d)" $< -o $@
@echo
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) $(CUBE_OBJECTS) Makefile
@echo "Compiling $@"
@$(COMPILE) $(OBJECTS) $(CUBE_OBJECTS) $(LDFLAGS) -o $@
@$(SIZE) $@
@echo
$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
@echo "Creating $@"
@$(HEX) $< $@
@echo
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
@echo "Creating $@"
@$(BIN) $< $@
@echo
$(BUILD_DIR):
@echo "Creating build directory"
@mkdir -p $@
######################################################################
## Auxiliary Targets
######################################################################
ifndef CUBE_PATH
$(error 'CUBE_PATH not defined')
endif
# Generate Cube Files
cube:
ifeq ($(OS),Windows_NT)
@java -jar "$(CUBE_PATH)\STM32CubeMX.exe" -q .cube
else
@java -jar "$(CUBE_PATH)/STM32CubeMX" -q .cube
endif
# Prepare for compilation
# - Erases useless Makefile and duplicate main.c
prepare:
@echo "Preparing cube files"
@-rm -f cube/Src/main.c cube/Makefile
# Flashes Built files with st-flash
flash load:
@echo "Flashing $(TARGET).bin"
@st-flash --reset write $(BUILD_DIR)/$(TARGET).bin 0x08000000
# Flashes Built files with j-link
jflash:
@echo "Flashing $(TARGET).hex with J-Link"
@echo "device $(DEVICE)\nsi SWD\nspeed 4000\nconnect\nr\nh\nloadfile $(BUILD_DIR)/$(TARGET).hex\nr\ng\nexit" > .jlink-flash
ifeq ($(OS),Windows_NT)
@JLink.exe .jlink-flash
else
@JLinkExe .jlink-flash
endif
info:
@st-info --probe
reset:
@echo "Reseting device"
@st-flash reset
# Clean cube generated files
clean_cube:
@echo "Cleaning cube files"
@-rm -rf cube/Src cube/Inc cube/Drivers cube/.mxproject cube/Makefile cube/*.s cube/*.ld
# Clean build files
# - Ignores cube-related build files
clean:
@echo "Cleaning build files"
@-rm -rf $(OBJECTS) $(OBJECTS:.o=.d) $(OBJECTS:.o=.lst)
# Clean all build files
clean_all:
@echo "Cleaning all build files"
@-rm -rf $(BUILD_DIR)
# Format source code
format:
@echo "Formatting files"
@clang-format -i $(C_SOURCES) $(wildcard */*.h)
@echo "Done"
# Display help
help:
@echo "----------------------- ThunderMakefile ------------------------------"
@echo " Bem-vindo(a) ao Makefile da ThundeRatz, cheque as configuracoes "
@echo " atuais e mude o arquivo se necessario "
@echo
@echo "Opcoes:"
@echo " help: mostra essa ajuda;"
@echo " cube: gera arquivos do cube;"
@echo " prepare: prepara para compilação inicial apagando arquivos do cube;"
@echo " all: compila todos os arquivos;"
@echo " info: mostra informações sobre o uC conectado;"
@echo " flash: carrega os arquivos compilados no microcontrolador via st-link"
@echo " jflash: carrega os arquivos compilados no microcontrolador via j-link"
@echo " format: formata os arquivos .c/.h;"
@echo " clean: limpa os arquivos compilados;"
@echo " clean_all: limpa os arquivos compilados, inclusive bibliotecas da ST;"
@echo " clean_cube: limpa os arquivos gerados pelo Cube."
@echo
@echo "Configuracoes atuais:"
@echo " DEVICE_FAMILY := $(DEVICE_FAMILY)"
@echo " DEVICE_TYPE := $(DEVICE_TYPE)"
@echo " DEVICE := $(DEVICE)"
@echo " DEVICE_LD := $(DEVICE_LD)"
@echo " DEVICE_DEF := $(DEVICE_DEF)"
@echo " TARGET = $(TARGET)"
@echo " DEBUG = $(DEBUG)"
# Include dependecy files for .h dependency detection
-include $(wildcard $(BUILD_DIR)/*.d)
.PHONY: clean all flash jflash help reset \
format clean_all prepare cube info