forked from open-bldc/obldc2-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
381 lines (325 loc) · 10.6 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#
# Open-BLDC - Open BrushLess DC Motor Controller
# Copyright (c) 2009-2013 Piotr Esden-Tempski <[email protected]>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
################################################################################
# Please edit the Makefile.targets file when adding targets not this one here
# You will hurt yourself doing that. :)
################################################################################
NAME ?= open-bldc
VERSION := 0.1-beta
COPYRIGHT := 'Copyright (C) 2009-2013 Piotr Esden-Tempski <[email protected]>'
LICENSE := 'License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>'
PREFIX ?= arm-none-eabi
OOCD_INTERFACE ?= flossjtag
OOCD_TARGET ?= open-bldc
OOCD_SERIAL ?=
# Black magic probe specific variables
# Set the BMP_PORT to a serial port and then BMP is used for flashing
BMP_PORT ?=
# Set to 1 to get plain gdb terminal without text user interface.
DEBUG_PLAIN ?= 0
# Set to 1 to go into verbose mode
VERBOSE ?= 0
# Use 'make VERBOSE=1' for more debug output.
ifneq ($(VERBOSE),1)
Q := @
else
LDFLAGS += -Wl,--print-gc-sections
endif
TOPDIR = $(shell pwd)
CC = $(PREFIX)-gcc
LD = $(PREFIX)-gcc
AR = $(PREFIX)-ar
AS = $(PREFIX)-as
CP = $(PREFIX)-objcopy
OD = $(PREFIX)-objdump
SIZE = $(PREFIX)-size
GDB = $(PREFIX)-gdb
OOCD = openocd
LINT = splint
STYLECHECK := ./scripts/checkpatch.pl
COMPILER = $(shell which $(CC))
TOOLCHAIN_DIR = $(shell dirname $(COMPILER))/..
TOOLCHAIN_LIB_DIR = $(TOOLCHAIN_DIR)/$(PREFIX)/lib
TOOLCHAIN_INC_DIR = $(TOOLCHAIN_DIR)/$(PREFIX)/include
STAGE_LIB_DIR = ext/stage/lib
STAGE_INC_DIR = ext/stage/include
ifdef CAN_ADDR
CAN_PARAM = -DCAN_ADDR=$(CAN_ADDR)
else
CAN_PARAM =
endif
VERSION_SUFFIX = `$(TOPDIR)/scripts/setlocalversion`
BUILDDATE = `date +"%Y%m%d"`
BUILDDIR = build/$(TARGET)
BINDIR = $(BUILDDIR)/bin
OBJDIR = $(BUILDDIR)/obj
INCDIR = $(BUILDDIR)/include
DEPDIR = build/dep
INCDIRS = \
-I. \
-Isrc \
-Itest \
-Iext/libopencm3/include \
-I$(INCDIR) \
-I$(STAGE_INC_DIR)
ARCH_FLAGS = -mthumb -mcpu=cortex-m3 -msoft-float
CFLAGS += $(INCDIRS) \
$(ARCH_FLAGS) \
-Wall -Wextra -ansi -std=c99 -c \
-fno-common -Os -g -ffunction-sections \
-fdata-sections -DSTM32F1
CFLAGS += $(CAN_PARAM)
CFLAGS += -DVERSION=\"$(VERSION)\"
CFLAGS += -DVERSION_SUFFIX=\"$(VERSION_SUFFIX)\"
CFLAGS += -DBUILDDATE=\"$(BUILDDATE)\"
CFLAGS += -DPROJECT_NAME=\"$(NAME)\"
CFLAGS += -DCOPYRIGHT=\"$(COPYRIGHT)\"
CFLAGS += -DLICENSE=\"$(LICENSE)\"
CFLAGS += $($(TARGET).CFLAGS)
LDFLAGS += -Tsrc/stm32.ld -nostartfiles -Os \
-Wl,--gc-sections \
-L$(STAGE_LIB_DIR)
LDFLAGS += $(ARCH_FLAGS)
LDFLAGS += $($(TARGET).LDFLAGS)
LDLIBS += -lopencm3_stm32f1 -lc -lgcc
LDLIBS += -lgovernor
LDLIBS += $($(TARGET).LDLIBS)
CPFLAGS += -j .isr_vector -j .text -j .data
ODFLAGS += -S
SIZEFLAGS += -A -x
LINTFLAGS += -systemdirs "ext/stage/include:ext/libopencm3/include" \
-Iext/stage/include -Iext/libopencm3/include -I. -DSTM32F1
STYLECHECKFLAGS += --no-tree -f --terse --mailback
###############################################################################
# Edit after this point only when you really know what you are doing!!!
###############################################################################
-include Makefile.targets
.SUFFIXES: .elf .bin .hex .srec .lst
.SECONDEXPANSION:
.SECONDARY:
# Semihosting machinery
ifeq ($($(TARGET).SEMIHOSTING),1)
SEMIHOSTING ?= 1
else
SEMIHOSTING ?= 0
endif
ifeq ($(SEMIHOSTING),1)
$(info **** Semihosting enabled!!! ****)
CFLAGS += -DSEMIHOSTING=1
LDLIBS += --specs=rdimon.specs -lrdimon
else
CFLAGS += -DSEMIHOSTING=0
LDLIBS += -lnosys
endif
# Targets
all: ext $(patsubst %,%.all,$(TARGETS))
ext:
$(MAKE) -C ext
lint: $(patsubst %,%.lint,$(TARGETS))
STYLECHECKFILES := $(shell find . ! -path "./ext/*" -and -name '*.[ch]')
stylecheck: $(STYLECHECKFILES:=.stylecheck)
stylecheckclean: $(STYLECHECKFILES:=.stylecheckclean)
%.stylecheck: %
$(Q)$(STYLECHECK) $(STYLECHECKFLAGS) $* > $*.stylecheck; \
if [ -s $*.stylecheck ]; then \
cat $*.stylecheck; \
else \
rm -f $*.stylecheck; \
fi;
%.stylecheckclean:
$(Q)rm -f $*.stylecheck;
clean:
@echo "Cleaning up everything"
$(Q)rm -rf build
$(MAKE) -C ext clean
flash: $(DEFAULT_TARGET).flash
debug: $(DEFAULT_TARGET).debug
ifdef TARGET
%.all: $$(*).target_exists $(BINDIR)/$$(*).images $(BINDIR)/$$(*).size
@echo "*** Finished building $* target ***"
else
%.all: $$(*).target_exists
$(MAKE) TARGET=$(*) check_params
$(MAKE) TARGET=$(*) CHECKED_PARAMS=true $(*).all
endif
$(BINDIR)/%.images: $(BINDIR)/%.bin $(BINDIR)/%.hex $(BINDIR)/%.srec $(BINDIR)/%.lst
@echo "*** $* images generated ***"
%.clean: $$(*).target_exists
@echo "Cleaning target $(*)"
$(Q)rm -rf build/$(*)
ifdef TARGET
ifeq ($(BMP_PORT),)
ifeq ($(OOCD_SERIAL),)
%.flash: $$(*).target_exists $(BINDIR)/$$(*).hex
@echo " OOCD $(*).hex"
$(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \
-f board/$(OOCD_TARGET).cfg \
-c init \
-c "reset init" \
-c "stm32x mass_erase 0" \
-c "flash write_image $(BINDIR)/$*.hex" \
-c reset \
-c shutdown
else
%.flash: $$(*).target_exists $(BINDIR)/$$(*).hex
@echo " OOCD $(*).hex"
$(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \
-f board/$(OOCD_TARGET).cfg \
-c "ft2232_serial $(OOCD_SERIAL)" \
-c init \
-c "reset init" \
-c "stm32x mass_erase 0" \
-c "flash write_image $(BINDIR)/$*.hex" \
-c reset \
-c shutdown
endif
else
%.flash: $$(*).target_exists $(BINDIR)/$$(*).elf
@echo " GDB $(*).elf (flash)"
$(Q)$(GDB) --batch \
-ex 'target extended-remote $(BMP_PORT)' \
-x scripts/black_magic_probe_flash.scr \
$(BINDIR)/$*.elf
endif
else
%.flash: $$(*).target_exists
make TARGET=$(*) check_params
make TARGET=$(*) CHECKED_PARAMS=true $(*).flash
endif
ifdef TARGET
ifeq ($(BMP_PORT),)
%.debug: $$(*).target_exists $(BINDIR)/$$(*).elf
@echo "Debug is only supported when using black magic probe. Pleas set BMP_PORT environment variable."
@exit 1
else
ifeq ($(DEBUG_PLAIN), 0)
%.debug: $$(*).target_exists $(BINDIR)/$$(*).elf
@echo " GDB $(*).elf"
$(Q)$(GDB) --tui \
-ex 'target extended-remote $(BMP_PORT)' \
-x scripts/black_magic_probe_debug.scr \
$(BINDIR)/$(*).elf
else
%.debug: $$(*).target_exists $(BINDIR)/$$(*).elf
@echo " GDB $(*).elf"
$(Q)$(GDB) -ex 'target extended-remote $(BMP_PORT)' \
-x scripts/black_magic_probe_debug.scr \
$(BINDIR)/$(*).elf
endif
endif
else
%.debug: $$(*).target_exists
make TARGET=$(*) check_params
make TARGET=$(*) CHECKED_PARAMS=true $(*).debug
endif
ifdef TARGET
%.lint: $$(*).target_exists $(patsubst %.o,%.c,$(COMMON_OBJECTS)) $(patsubst %.o,%.c,$($(TARGET).OBJECTS))
@echo " LINT $(*)"
$(Q)$(LINT) $(LINTFLAGS) $(patsubst %.o,%.c,$(COMMON_OBJECTS)) $(patsubst %.o,%.c,$($(TARGET).OBJECTS))
else
%.lint: $$(*).target_exists
make TARGET=$(*) check_params
make TARGET=$(*) CHECKED_PARAMS=true $(*).lint
endif
halt:
@echo " OOCD halt"
$(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \
-f board/$(OOCD_TARGET).cfg \
-c init \
-c "reset halt" \
-c shutdown
.PHONY: doc stylecheck stylecheckclean clean
doc:
@mkdir -p doc
@doxygen doxygen.conf > /dev/null
@cp ../../art/open-bldc-logo.png doc/doxy/html/
%.size: %.elf
@echo
$(Q)$(SIZE) $(SIZEFLAGS) $<
%.elf: $(patsubst %.o,$(OBJDIR)/%.o,$(COMMON_OBJECTS)) $(patsubst %.o,$(OBJDIR)/%.o,$($(TARGET).OBJECTS)) $(INCDIR)/params.h
@echo " LD $@"
$(Q)mkdir -p $(@D)
$(Q)$(LD) $(LDFLAGS) -o $@ $(patsubst %.o,$(OBJDIR)/%.o, $(COMMON_OBJECTS)) $(patsubst %.o,$(OBJDIR)/%.o,$($(TARGET).OBJECTS)) $(LDLIBS)
%.target_exists:
@if [ "_$($(*).OBJECTS)" == "_" ] ; then \
echo "*** Target $* does not exist ***"; \
exit 1; \
fi
check_params: check_params_exist $(patsubst %,%.check_param, $($(TARGET).PARAMS))
#$(INCDIR)/config.h
check_params_exist:
@echo "checking if $(INCDIR)/params.h exists... \c"
@mkdir -p $(INCDIR)
@\
if [ ! -f $(INCDIR)/params.h ] ; then \
echo "no, I create it for you."; \
touch $(INCDIR)/params.h; \
else \
echo "yes."; \
fi; \
%.check_param:
@echo "checking param \"$(*)\" for target \"$(TARGET)\"... \c"
@\
if ! grep "^#define $(*) PARAM_$($(*))$$" $(INCDIR)/params.h > /dev/null ; then \
echo "changed/missing, updating/adding it for you."; \
mv $(INCDIR)/params.h $(INCDIR)/params.h.tmp; \
grep -v "^#define $(*) " $(INCDIR)/params.h.tmp > $(INCDIR)/params.h; \
echo "#define $(*) PARAM_$($(*))" >> $(INCDIR)/params.h; \
rm $(INCDIR)/params.h.tmp; \
else \
echo "exists and not missing."; \
fi
# Suffix rules
%.bin: %.elf
@echo " CP $@"
$(Q)$(CP) $(CPFLAGS) -Obinary $< $@
%.hex: %.elf
@echo " CP $@"
$(Q)$(CP) $(CPFLAGS) -Oihex $< $@
%.srec: %.elf
@echo " CP $@"
$(Q)$(CP) $(CPFLAGS) -Osrec $< $@
%.lst: %.elf
@echo " OD $@"
$(Q)$(OD) $(ODFLAGS) $< > $@
$(OBJDIR)/%.o: %.c
@echo " CC $@"
$(Q)mkdir -p $(@D)
$(Q)$(CC) $(CFLAGS) -DTARGET=\"$(TARGET)\" -c $*.c -o $@
$(DEPDIR)/%.d: %.c
@echo " DEP $@"
$(Q)mkdir -p $(@D)
$(Q)$(CC) -MM $(CFLAGS) $< -MF $@
$(Q)cp -f $@ [email protected]
$(Q)sed -e 's|.*:|build/$$(TARGET)/obj/$*.o:|' < [email protected] > $@
$(Q)sed -e 's/.*://' -e 's/\\$$//' < [email protected] | fmt -1 | \
sed -e 's/^ *//' -e 's/$$/:/' >> $@
$(Q)rm -f [email protected]
# include header dependency information
ifdef CHECKED_PARAMS
-include $(patsubst %.o,$(DEPDIR)/%.d,$(OBJECTS))
endif