Obtain HSE_VALUE from config.h where provided. (#12632)
[betaflight.git] / Makefile
blobe8388f7bf25b0ca23966ece03816266465b350aa
1 ###############################################################################
2 # "THE BEER-WARE LICENSE" (Revision 42):
3 # <msmith@FreeBSD.ORG> wrote this file. As long as you retain this notice you
4 # can do whatever you want with this stuff. If we meet some day, and you think
5 # this stuff is worth it, you can buy me a beer in return
6 ###############################################################################
8 # Makefile for building the betaflight firmware.
10 # Invoke this with 'make help' to see the list of supported targets.
12 ###############################################################################
15 # Things that the user might override on the commandline
18 # The target to build, see BASE_TARGETS below
19 DEFAULT_TARGET ?= STM32F405
20 TARGET ?=
21 CONFIG ?=
23 # Compile-time options
24 OPTIONS ?=
26 # compile for External Storage Bootloader support
27 EXST ?= no
29 # compile for target loaded into RAM
30 RAM_BASED ?= no
32 # reserve space for custom defaults
33 CUSTOM_DEFAULTS_EXTENDED ?= no
35 # Debugger optons:
36 # empty - ordinary build with all optimizations enabled
37 # INFO - ordinary build with debug symbols and all optimizations enabled
38 # GDB - debug build with minimum number of optimizations
39 DEBUG ?=
41 # Insert the debugging hardfault debugger
42 # releases should not be built with this flag as it does not disable pwm output
43 DEBUG_HARDFAULTS ?=
45 # Serial port/Device for flashing
46 SERIAL_DEVICE ?= $(firstword $(wildcard /dev/ttyACM*) $(firstword $(wildcard /dev/ttyUSB*) no-port-found))
48 # Flash size (KB). Some low-end chips actually have more flash than advertised, use this to override.
49 FLASH_SIZE ?=
51 ###############################################################################
52 # Things that need to be maintained as the source changes
55 FORKNAME = betaflight
57 # Working directories
58 ROOT := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
59 SRC_DIR := $(ROOT)/src/main
60 OBJECT_DIR := $(ROOT)/obj/main
61 BIN_DIR := $(ROOT)/obj
62 CMSIS_DIR := $(ROOT)/lib/main/CMSIS
63 INCLUDE_DIRS := $(SRC_DIR) \
64 $(ROOT)/src/main/target \
65 $(ROOT)/src/main/startup
66 LINKER_DIR := $(ROOT)/src/link
68 ## V : Set verbosity level based on the V= parameter
69 ## V=0 Low
70 ## V=1 High
71 include $(ROOT)/make/build_verbosity.mk
73 # Build tools, so we all share the same versions
74 # import macros common to all supported build systems
75 include $(ROOT)/make/system-id.mk
77 # developer preferences, edit these at will, they'll be gitignored
78 -include $(ROOT)/make/local.mk
80 # pre-build sanity checks
81 include $(ROOT)/make/checks.mk
83 # configure some directories that are relative to wherever ROOT_DIR is located
84 TOOLS_DIR ?= $(ROOT)/tools
85 DL_DIR := $(ROOT)/downloads
86 CONFIG_DIR ?= $(ROOT)/src/config
88 export RM := rm
90 # import macros that are OS specific
91 include $(ROOT)/make/$(OSFAMILY).mk
93 # include the tools makefile
94 include $(ROOT)/make/tools.mk
96 # Search path for sources
97 VPATH := $(SRC_DIR):$(SRC_DIR)/startup
98 FATFS_DIR = $(ROOT)/lib/main/FatFS
99 FATFS_SRC = $(notdir $(wildcard $(FATFS_DIR)/*.c))
100 CSOURCES := $(shell find $(SRC_DIR) -name '*.c')
102 ifneq ($(CONFIG),)
104 ifneq ($(TARGET),)
105 $(error TARGET or CONFIG should be specified. Not both.)
106 endif
108 INCLUDE_DIRS += $(CONFIG_DIR)/$(CONFIG)
109 CONFIG_FILE := $(CONFIG_DIR)/$(CONFIG)/config.h
111 ifeq ($(wildcard $(CONFIG_FILE)),)
112 $(error Config file not found: $(CONFIG_FILE))
113 endif
115 TARGET := $(shell grep " FC_TARGET_MCU" $(CONFIG_FILE) | awk '{print $$3}' )
116 HSE_VALUE_MHZ := $(shell grep " SYSTEM_HSE_MHZ" $(CONFIG_FILE) | awk '{print $$3}' )
117 ifneq ($(HSE_VALUE_MHZ),)
118 HSE_VALUE := $(shell echo $$(( $(HSE_VALUE_MHZ) * 1000000 )) )
119 endif
121 ifeq ($(TARGET),)
122 $(error No TARGET identified. Is the config.h valid for $(CONFIG)?)
123 endif
125 EXST_ADJUST_VMA := $(shell grep " FC_VMA_ADDRESS" $(CONFIG_FILE) | awk '{print $$3}' )
126 ifneq ($(EXST_ADJUST_VMA),)
127 EXST = yes
128 endif
130 else
131 ifeq ($(TARGET),)
132 TARGET := $(DEFAULT_TARGET)
133 endif
134 endif #CONFIG
136 # default xtal value
137 HSE_VALUE ?= 8000000
139 BASE_CONFIGS = $(sort $(notdir $(patsubst %/,%,$(dir $(wildcard $(ROOT)/src/config/*/config.h)))))
140 BASE_TARGETS = $(sort $(notdir $(patsubst %/,%,$(dir $(wildcard $(ROOT)/src/main/target/*/target.mk)))))
141 CI_TARGETS := $(BASE_TARGETS) CRAZYBEEF4SX1280 CRAZYBEEF4FR IFLIGHT_BLITZ_F722
142 include $(ROOT)/src/main/target/$(TARGET)/target.mk
144 REVISION := norevision
145 ifeq ($(shell git diff --shortstat),)
146 REVISION := $(shell git log -1 --format="%h")
147 endif
149 FC_VER_MAJOR := $(shell grep " FC_VERSION_MAJOR" src/main/build/version.h | awk '{print $$3}' )
150 FC_VER_MINOR := $(shell grep " FC_VERSION_MINOR" src/main/build/version.h | awk '{print $$3}' )
151 FC_VER_PATCH := $(shell grep " FC_VERSION_PATCH" src/main/build/version.h | awk '{print $$3}' )
153 FC_VER := $(FC_VER_MAJOR).$(FC_VER_MINOR).$(FC_VER_PATCH)
155 LD_FLAGS :=
156 EXTRA_LD_FLAGS :=
159 # Default Tool options - can be overridden in {mcu}.mk files.
161 ifeq ($(DEBUG),GDB)
162 OPTIMISE_DEFAULT := -Og
164 LTO_FLAGS := $(OPTIMISE_DEFAULT)
165 DEBUG_FLAGS = -ggdb3 -gdwarf-5 -DDEBUG
166 else
167 ifeq ($(DEBUG),INFO)
168 DEBUG_FLAGS = -ggdb3
169 endif
170 OPTIMISATION_BASE := -flto -fuse-linker-plugin -ffast-math -fmerge-all-constants
171 OPTIMISE_DEFAULT := -O2
172 OPTIMISE_SPEED := -Ofast
173 OPTIMISE_SIZE := -Os
175 LTO_FLAGS := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
176 endif
178 VPATH := $(VPATH):$(ROOT)/make/mcu
179 VPATH := $(VPATH):$(ROOT)/make
181 # start specific includes
182 ifeq ($(TARGET_MCU),)
183 $(error No TARGET_MCU specified. Is the target.mk valid for $(TARGET)?)
184 endif
186 ifeq ($(TARGET_MCU_FAMILY),)
187 $(error No TARGET_MCU_FAMILY specified. Is the target.mk valid for $(TARGET)?)
188 endif
190 TARGET_FLAGS := -D$(TARGET) -D$(TARGET_MCU_FAMILY) $(TARGET_FLAGS)
192 ifneq ($(CONFIG),)
193 TARGET_FLAGS := $(TARGET_FLAGS) -DUSE_CONFIG
194 endif
196 include $(ROOT)/make/mcu/$(TARGET_MCU_FAMILY).mk
198 # openocd specific includes
199 include $(ROOT)/make/openocd.mk
201 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
202 ifeq ($(TARGET_FLASH_SIZE),)
203 ifneq ($(MCU_FLASH_SIZE),)
204 TARGET_FLASH_SIZE := $(MCU_FLASH_SIZE)
205 else
206 $(error MCU_FLASH_SIZE not configured for target $(TARGET))
207 endif
208 endif
210 DEVICE_FLAGS := $(DEVICE_FLAGS) -DTARGET_FLASH_SIZE=$(TARGET_FLASH_SIZE)
212 ifneq ($(HSE_VALUE),)
213 DEVICE_FLAGS := $(DEVICE_FLAGS) -DHSE_VALUE=$(HSE_VALUE)
214 endif
216 TARGET_DIR = $(ROOT)/src/main/target/$(TARGET)
217 TARGET_DIR_SRC = $(notdir $(wildcard $(TARGET_DIR)/*.c))
219 .DEFAULT_GOAL := hex
221 INCLUDE_DIRS := $(INCLUDE_DIRS) \
222 $(ROOT)/lib/main/MAVLink
224 INCLUDE_DIRS := $(INCLUDE_DIRS) \
225 $(TARGET_DIR)
227 VPATH := $(VPATH):$(TARGET_DIR)
229 include $(ROOT)/make/source.mk
231 ###############################################################################
232 # Things that might need changing to use different tools
235 # Find out if ccache is installed on the system
236 CCACHE := ccache
237 RESULT = $(shell (which $(CCACHE) > /dev/null 2>&1; echo $$?) )
238 ifneq ($(RESULT),0)
239 CCACHE :=
240 endif
242 # Tool names
243 CROSS_CC := $(CCACHE) $(ARM_SDK_PREFIX)gcc
244 CROSS_CXX := $(CCACHE) $(ARM_SDK_PREFIX)g++
245 CROSS_GDB := $(ARM_SDK_PREFIX)gdb
246 OBJCOPY := $(ARM_SDK_PREFIX)objcopy
247 OBJDUMP := $(ARM_SDK_PREFIX)objdump
248 READELF := $(ARM_SDK_PREFIX)readelf
249 SIZE := $(ARM_SDK_PREFIX)size
250 DFUSE-PACK := src/utils/dfuse-pack.py
253 # Tool options.
255 CC_DEBUG_OPTIMISATION := $(OPTIMISE_DEFAULT)
256 CC_DEFAULT_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_DEFAULT)
257 CC_SPEED_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
258 CC_SIZE_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SIZE)
259 CC_NO_OPTIMISATION :=
262 # Added after GCC version update, remove once the warnings have been fixed
264 TEMPORARY_FLAGS :=
266 EXTRA_WARNING_FLAGS := -Wold-style-definition
268 CFLAGS += $(ARCH_FLAGS) \
269 $(addprefix -D,$(OPTIONS)) \
270 $(addprefix -I,$(INCLUDE_DIRS)) \
271 $(DEBUG_FLAGS) \
272 -std=gnu17 \
273 -Wall -Wextra -Werror -Wpedantic -Wunsafe-loop-optimizations -Wdouble-promotion \
274 $(EXTRA_WARNING_FLAGS) \
275 -ffunction-sections \
276 -fdata-sections \
277 -fno-common \
278 $(TEMPORARY_FLAGS) \
279 $(DEVICE_FLAGS) \
280 -D_GNU_SOURCE \
281 -DUSE_STDPERIPH_DRIVER \
282 -D$(TARGET) \
283 $(TARGET_FLAGS) \
284 -D'__FORKNAME__="$(FORKNAME)"' \
285 -D'__TARGET__="$(TARGET)"' \
286 -D'__REVISION__="$(REVISION)"' \
287 -pipe \
288 -MMD -MP \
289 $(EXTRA_FLAGS)
291 ASFLAGS = $(ARCH_FLAGS) \
292 $(DEBUG_FLAGS) \
293 -x assembler-with-cpp \
294 $(addprefix -I,$(INCLUDE_DIRS)) \
295 -MMD -MP
297 ifeq ($(LD_FLAGS),)
298 LD_FLAGS = -lm \
299 -nostartfiles \
300 --specs=nano.specs \
301 -lc \
302 -lnosys \
303 $(ARCH_FLAGS) \
304 $(LTO_FLAGS) \
305 $(DEBUG_FLAGS) \
306 -static \
307 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
308 -Wl,-L$(LINKER_DIR) \
309 -Wl,--cref \
310 -Wl,--no-wchar-size-warning \
311 -Wl,--print-memory-usage \
312 -T$(LD_SCRIPT) \
313 $(EXTRA_LD_FLAGS)
314 endif
316 ###############################################################################
317 # No user-serviceable parts below
318 ###############################################################################
320 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
321 --std=c99 --inline-suppr --quiet --force \
322 $(addprefix -I,$(INCLUDE_DIRS)) \
323 -I/usr/include -I/usr/include/linux
325 TARGET_NAME := $(TARGET)
327 ifneq ($(CONFIG),)
328 TARGET_NAME := $(TARGET_NAME)_$(CONFIG)
329 endif
331 ifeq ($(REV),yes)
332 TARGET_NAME := $(TARGET_NAME)_$(REVISION)
333 endif
335 TARGET_FULLNAME = $(FORKNAME)_$(FC_VER)_$(TARGET_NAME)
337 # Things we will build
339 TARGET_BIN = $(BIN_DIR)/$(TARGET_FULLNAME).bin
340 TARGET_HEX = $(BIN_DIR)/$(TARGET_FULLNAME).hex
341 TARGET_DFU = $(BIN_DIR)/$(TARGET_FULLNAME).dfu
342 TARGET_ZIP = $(BIN_DIR)/$(TARGET_FULLNAME).zip
343 TARGET_OBJ_DIR = $(OBJECT_DIR)/$(TARGET_NAME)
344 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).elf
345 TARGET_EXST_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME)_EXST.elf
346 TARGET_UNPATCHED_BIN = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME)_UNPATCHED.bin
347 TARGET_LST = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).lst
348 TARGET_OBJS = $(addsuffix .o,$(addprefix $(TARGET_OBJ_DIR)/,$(basename $(SRC))))
349 TARGET_DEPS = $(addsuffix .d,$(addprefix $(TARGET_OBJ_DIR)/,$(basename $(SRC))))
350 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).map
352 TARGET_EXST_HASH_SECTION_FILE = $(TARGET_OBJ_DIR)/exst_hash_section.bin
354 TARGET_EF_HASH := $(shell echo -n "$(EXTRA_FLAGS)" | openssl dgst -md5 | awk '{print $$2;}')
355 TARGET_EF_HASH_FILE := $(TARGET_OBJ_DIR)/.efhash_$(TARGET_EF_HASH)
357 CLEAN_ARTIFACTS := $(TARGET_BIN)
358 CLEAN_ARTIFACTS += $(TARGET_HEX_REV) $(TARGET_HEX)
359 CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP)
360 CLEAN_ARTIFACTS += $(TARGET_LST)
361 CLEAN_ARTIFACTS += $(TARGET_DFU)
363 # Make sure build date and revision is updated on every incremental build
364 $(TARGET_OBJ_DIR)/build/version.o : $(SRC)
366 # List of buildable ELF files and their object dependencies.
367 # It would be nice to compute these lists, but that seems to be just beyond make.
369 $(TARGET_LST): $(TARGET_ELF)
370 $(V0) $(OBJDUMP) -S --disassemble $< > $@
372 ifeq ($(EXST),no)
373 $(TARGET_BIN): $(TARGET_ELF)
374 @echo "Creating BIN $(TARGET_BIN)" "$(STDOUT)"
375 $(V1) $(OBJCOPY) -O binary $< $@
377 $(TARGET_HEX): $(TARGET_ELF)
378 @echo "Creating HEX $(TARGET_HEX)" "$(STDOUT)"
379 $(V1) $(OBJCOPY) -O ihex --set-start 0x8000000 $< $@
381 $(TARGET_DFU): $(TARGET_HEX)
382 @echo "Creating DFU $(TARGET_DFU)" "$(STDOUT)"
383 $(V1) $(PYTHON) $(DFUSE-PACK) -i $< $@
385 else
386 CLEAN_ARTIFACTS += $(TARGET_UNPATCHED_BIN) $(TARGET_EXST_HASH_SECTION_FILE) $(TARGET_EXST_ELF)
388 $(TARGET_UNPATCHED_BIN): $(TARGET_ELF)
389 @echo "Creating BIN (without checksum) $(TARGET_UNPATCHED_BIN)" "$(STDOUT)"
390 $(V1) $(OBJCOPY) -O binary $< $@
392 $(TARGET_BIN): $(TARGET_UNPATCHED_BIN)
393 @echo "Creating EXST $(TARGET_BIN)" "$(STDOUT)"
394 # Linker script should allow .bin generation from a .elf which results in a file that is the same length as the FIRMWARE_SIZE.
395 # These 'dd' commands will pad a short binary to length FIRMWARE_SIZE.
396 $(V1) dd if=/dev/zero ibs=1k count=$(FIRMWARE_SIZE) of=$(TARGET_BIN)
397 $(V1) dd if=$(TARGET_UNPATCHED_BIN) of=$(TARGET_BIN) conv=notrunc
399 @echo "Generating MD5 hash of binary" "$(STDOUT)"
400 $(V1) openssl dgst -md5 $(TARGET_BIN) > $(TARGET_UNPATCHED_BIN).md5
402 @echo "Patching MD5 hash into binary" "$(STDOUT)"
403 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",(1024*$(FIRMWARE_SIZE))-16,$$2);}' | xxd -r - $(TARGET_BIN)
404 $(V1) echo $(FIRMWARE_SIZE) | awk '{printf("-s 0x%08x -l 16 -c 16 %s",(1024*$$1)-16,"$(TARGET_BIN)");}' | xargs xxd
406 # Note: From the objcopy manual "If you do not specify outfile, objcopy creates a temporary file and destructively renames the result with the name of infile"
407 # Due to this a temporary file must be created and removed, even though we're only extracting data from the input file.
408 # If this temporary file is NOT used the $(TARGET_ELF) is modified, and running make a second time will result in
409 # a) regeneration of $(TARGET_BIN), and
410 # b) the results of $(TARGET_BIN) will not be as expected.
411 @echo "Extracting HASH section from unpatched EXST elf $(TARGET_ELF)" "$(STDOUT)"
412 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF).tmp --dump-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE) -j .exst_hash
413 rm $(TARGET_EXST_ELF).tmp
415 @echo "Patching MD5 hash into HASH section" "$(STDOUT)"
416 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",64-16,$$2);}' | xxd -r - $(TARGET_EXST_HASH_SECTION_FILE)
418 $(V1) @echo "Patching updated HASH section into $(TARGET_EXST_ELF)" "$(STDOUT)"
419 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF) --update-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE)
421 $(V1) $(READELF) -S $(TARGET_EXST_ELF)
422 $(V1) $(READELF) -l $(TARGET_EXST_ELF)
424 $(TARGET_HEX): $(TARGET_BIN)
425 $(if $(EXST_ADJUST_VMA),,$(error "EXST_ADJUST_VMA not specified"))
427 @echo "Creating EXST HEX from patched EXST BIN $(TARGET_BIN), VMA Adjust $(EXST_ADJUST_VMA)" "$(STDOUT)"
428 $(V1) $(OBJCOPY) -I binary -O ihex --adjust-vma=$(EXST_ADJUST_VMA) $(TARGET_BIN) $@
430 endif
432 $(TARGET_ELF): $(TARGET_OBJS) $(LD_SCRIPT) $(LD_SCRIPTS)
433 @echo "Linking $(TARGET_NAME)" "$(STDOUT)"
434 $(V1) $(CROSS_CC) -o $@ $(filter-out %.ld,$^) $(LD_FLAGS)
435 $(V1) $(SIZE) $(TARGET_ELF)
437 # Compile
439 ## compile_file takes two arguments: (1) optimisation description string and (2) optimisation compiler flag
440 define compile_file
441 echo "%% ($(1)) $<" "$(STDOUT)" && \
442 $(CROSS_CC) -c -o $@ $(CFLAGS) $(2) $<
443 endef
445 ifeq ($(DEBUG),GDB)
446 $(TARGET_OBJ_DIR)/%.o: %.c
447 $(V1) mkdir -p $(dir $@)
448 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
449 $(call compile_file,not optimised, $(CC_NO_OPTIMISATION)) \
451 $(call compile_file,debug,$(CC_DEBUG_OPTIMISATION)) \
453 else
454 $(TARGET_OBJ_DIR)/%.o: %.c
455 $(V1) mkdir -p $(dir $@)
456 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
457 $(call compile_file,not optimised,$(CC_NO_OPTIMISATION)) \
459 $(if $(findstring $(subst ./src/main/,,$<),$(SPEED_OPTIMISED_SRC)), \
460 $(call compile_file,speed optimised,$(CC_SPEED_OPTIMISATION)) \
462 $(if $(findstring $(subst ./src/main/,,$<),$(SIZE_OPTIMISED_SRC)), \
463 $(call compile_file,size optimised,$(CC_SIZE_OPTIMISATION)) \
465 $(call compile_file,optimised,$(CC_DEFAULT_OPTIMISATION)) \
469 endif
471 # Assemble
472 $(TARGET_OBJ_DIR)/%.o: %.s
473 $(V1) mkdir -p $(dir $@)
474 @echo "%% $(notdir $<)" "$(STDOUT)"
475 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
477 $(TARGET_OBJ_DIR)/%.o: %.S
478 $(V1) mkdir -p $(dir $@)
479 @echo "%% $(notdir $<)" "$(STDOUT)"
480 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
483 ## all : Build all currently built targets
484 all: $(CI_TARGETS)
486 $(BASE_TARGETS):
487 $(V0) @echo "Building $@" && \
488 $(MAKE) hex TARGET=$@ && \
489 echo "Building $@ succeeded."
491 $(BASE_CONFIGS):
492 $(V0) @echo "Building config $@" && \
493 $(MAKE) hex CONFIG=$@ && \
494 echo "Building config $@ succeeded."
497 TARGETS_CLEAN = $(addsuffix _clean,$(BASE_TARGETS))
498 CONFIGS_CLEAN = $(addsuffix _clean,$(BASE_CONFIGS))
500 ## clean : clean up temporary / machine-generated files
501 clean:
502 @echo "Cleaning $(TARGET_NAME)"
503 $(V0) rm -f $(CLEAN_ARTIFACTS)
504 $(V0) rm -rf $(TARGET_OBJ_DIR)
505 @echo "Cleaning $(TARGET_NAME) succeeded."
507 ## test_clean : clean up temporary / machine-generated files (tests)
508 test-%_clean:
509 $(MAKE) test_clean
511 test_clean:
512 $(V0) cd src/test && $(MAKE) clean || true
514 ## <TARGET>_clean : clean up one specific target (alias for above)
515 $(TARGETS_CLEAN):
516 $(V0) $(MAKE) -j TARGET=$(subst _clean,,$@) clean
518 ## <CONFIG>_clean : clean up one specific config (alias for above)
519 $(CONFIGS_CLEAN):
520 $(V0) $(MAKE) -j CONFIG=$(subst _clean,,$@) clean
522 ## clean_all : clean all targets
523 clean_all: $(TARGETS_CLEAN) test_clean
525 TARGETS_FLASH = $(addsuffix _flash,$(BASE_TARGETS))
527 ## <TARGET>_flash : build and flash a target
528 $(TARGETS_FLASH):
529 $(V0) $(MAKE) hex TARGET=$(subst _flash,,$@)
530 ifneq (,$(findstring /dev/ttyUSB,$(SERIAL_DEVICE)))
531 $(V0) $(MAKE) tty_flash TARGET=$(subst _flash,,$@)
532 else
533 $(V0) $(MAKE) dfu_flash TARGET=$(subst _flash,,$@)
534 endif
536 ## tty_flash : flash firmware (.hex) onto flight controller via a serial port
537 tty_flash:
538 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
539 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
540 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
542 ## dfu_flash : flash firmware (.bin) onto flight controller via a DFU mode
543 dfu_flash:
544 ifneq (no-port-found,$(SERIAL_DEVICE))
545 # potentially this is because the MCU already is in DFU mode, try anyway
546 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
547 $(V0) sleep 1
548 endif
549 $(V0) $(MAKE) $(TARGET_DFU)
550 $(V0) dfu-util -a 0 -D $(TARGET_DFU) -s :leave
552 st-flash_$(TARGET): $(TARGET_BIN)
553 $(V0) st-flash --reset write $< 0x08000000
555 ## st-flash : flash firmware (.bin) onto flight controller
556 st-flash: st-flash_$(TARGET)
558 ifneq ($(OPENOCD_COMMAND),)
559 openocd-gdb: $(TARGET_ELF)
560 $(V0) $(OPENOCD_COMMAND) & $(CROSS_GDB) $(TARGET_ELF) -ex "target remote localhost:3333" -ex "load"
561 endif
563 TARGETS_ZIP = $(addsuffix _zip,$(BASE_TARGETS))
565 ## <TARGET>_zip : build target and zip it (useful for posting to GitHub)
566 $(TARGETS_ZIP):
567 $(V0) $(MAKE) hex TARGET=$(subst _zip,,$@)
568 $(V0) $(MAKE) zip TARGET=$(subst _zip,,$@)
570 zip:
571 $(V0) zip $(TARGET_ZIP) $(TARGET_HEX)
573 binary:
574 $(V0) $(MAKE) -j $(TARGET_BIN)
576 hex:
577 $(V0) $(MAKE) -j $(TARGET_HEX)
579 TARGETS_REVISION = $(addsuffix _rev,$(BASE_TARGETS))
580 ## <TARGET>_rev : build target and add revision to filename
581 $(TARGETS_REVISION):
582 $(V0) $(MAKE) hex REV=yes TARGET=$(subst _rev,,$@)
584 CONFIGS_REVISION = $(addsuffix _rev,$(BASE_CONFIGS))
585 ## <CONFIG>_rev : build configured target and add revision to filename
586 $(CONFIGS_REVISION):
587 $(V0) $(MAKE) hex REV=yes CONFIG=$(subst _rev,,$@)
589 all_rev: $(addsuffix _rev,$(CI_TARGETS))
591 unbrick_$(TARGET): $(TARGET_HEX)
592 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
593 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
595 ## unbrick : unbrick flight controller
596 unbrick: unbrick_$(TARGET)
598 ## cppcheck : run static analysis on C source code
599 cppcheck: $(CSOURCES)
600 $(V0) $(CPPCHECK)
602 cppcheck-result.xml: $(CSOURCES)
603 $(V0) $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
605 # mkdirs
606 $(DL_DIR):
607 mkdir -p $@
609 $(TOOLS_DIR):
610 mkdir -p $@
612 ## version : print firmware version
613 version:
614 @echo $(FC_VER)
616 ## help : print this help message and exit
617 help: Makefile make/tools.mk
618 @echo ""
619 @echo "Makefile for the $(FORKNAME) firmware"
620 @echo ""
621 @echo "Usage:"
622 @echo " make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"]"
623 @echo "Or:"
624 @echo " make <target> [V=<verbosity>] [OPTIONS=\"<options>\"]"
625 @echo ""
626 @echo "Valid TARGET values are: $(BASE_TARGETS)"
627 @echo ""
628 @sed -n 's/^## //p' $?
630 ## targets : print a list of all valid target platforms (for consumption by scripts)
631 targets:
632 @echo "Valid targets: $(BASE_TARGETS)"
633 @echo "Built targets: $(CI_TARGETS)"
634 @echo "Default target: $(TARGET)"
636 configs:
637 @echo "Valid configs: $(BASE_CONFIGS)"
639 targets-ci-print:
640 @echo $(CI_TARGETS)
642 ## target-mcu : print the MCU type of the target
643 target-mcu:
644 @echo "$(TARGET_MCU_FAMILY) : $(TARGET_MCU)"
646 ## targets-by-mcu : make all targets that have a MCU_TYPE mcu
647 targets-by-mcu:
648 $(V1) for target in $${TARGETS}; do \
649 TARGET_MCU_TYPE=$$($(MAKE) -s TARGET=$${target} target-mcu); \
650 if [ "$${TARGET_MCU_TYPE}" = "$${MCU_TYPE}" ]; then \
651 if [ "$${DO_BUILD}" = 1 ]; then \
652 echo "Building target $${target}..."; \
653 $(MAKE) TARGET=$${target}; \
654 if [ $$? -ne 0 ]; then \
655 echo "Building target $${target} failed, aborting."; \
656 exit 1; \
657 fi; \
658 else \
659 echo -n "$${target} "; \
660 fi; \
661 fi; \
662 done
663 @echo
665 ## test : run the Betaflight test suite
666 ## junittest : run the Betaflight test suite, producing Junit XML result files.
667 ## test-representative: run a representative subset of the Betaflight test suite (i.e. run all tests, but run each expanded test only for one target)
668 ## test-all: run the Betaflight test suite including all per-target expanded tests
669 test junittest test-all test-representative:
670 $(V0) cd src/test && $(MAKE) $@
672 ## test_help : print the help message for the test suite (including a list of the available tests)
673 test_help:
674 $(V0) cd src/test && $(MAKE) help
676 ## test_versions : print the compiler versions used for the test suite
677 test_versions:
678 $(V0) cd src/test && $(MAKE) versions
680 ## test_% : run test 'test_%' from the test suite
681 test_%:
682 $(V0) cd src/test && $(MAKE) $@
684 $(TARGET_EF_HASH_FILE):
685 $(V1) mkdir -p $(dir $@)
686 $(V0) rm -f $(TARGET_OBJ_DIR)/.efhash_*
687 @echo "EF HASH -> $(TARGET_EF_HASH_FILE)"
688 $(V1) touch $(TARGET_EF_HASH_FILE)
690 # rebuild everything when makefile changes or the extra flags have changed
691 $(TARGET_OBJS): $(TARGET_EF_HASH_FILE) Makefile $(TARGET_DIR)/target.mk $(wildcard make/*) $(CONFIG_FILE)
693 # include auto-generated dependencies
694 -include $(TARGET_DEPS)