battery - fix BATTERY_NOT_PRESENT detection, detection logic change (#13599)
[betaflight.git] / Makefile
blob914b832cc68860e048529cda908beb4457bcf165
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
67 MAKE_SCRIPT_DIR := $(ROOT)/mk
69 ## V : Set verbosity level based on the V= parameter
70 ## V=0 Low
71 ## V=1 High
72 include $(MAKE_SCRIPT_DIR)/build_verbosity.mk
74 # Build tools, so we all share the same versions
75 # import macros common to all supported build systems
76 include $(MAKE_SCRIPT_DIR)/system-id.mk
78 # developer preferences, edit these at will, they'll be gitignored
79 ifneq ($(wildcard $(MAKE_SCRIPT_DIR)/local.mk),)
80 include $(MAKE_SCRIPT_DIR)/local.mk
81 endif
83 # pre-build sanity checks
84 include $(MAKE_SCRIPT_DIR)/checks.mk
86 # basic target list
87 BASE_TARGETS := $(sort $(notdir $(patsubst %/,%,$(dir $(wildcard $(ROOT)/src/main/target/*/target.mk)))))
89 # configure some directories that are relative to wherever ROOT_DIR is located
90 TOOLS_DIR ?= $(ROOT)/tools
91 DL_DIR := $(ROOT)/downloads
92 CONFIG_DIR ?= $(BETAFLIGHT_CONFIG)
93 ifeq ($(CONFIG_DIR),)
94 CONFIG_DIR := $(ROOT)/src/config
95 endif
96 DIRECTORIES := $(DL_DIR) $(TOOLS_DIR)
98 export RM := rm
100 # import macros that are OS specific
101 include $(MAKE_SCRIPT_DIR)/$(OSFAMILY).mk
103 # include the tools makefile
104 include $(MAKE_SCRIPT_DIR)/tools.mk
106 # Search path for sources
107 VPATH := $(SRC_DIR):$(SRC_DIR)/startup
108 FATFS_DIR = $(ROOT)/lib/main/FatFS
109 FATFS_SRC = $(notdir $(wildcard $(FATFS_DIR)/*.c))
110 CSOURCES := $(shell find $(SRC_DIR) -name '*.c')
112 FC_VER_MAJOR := $(shell grep " FC_VERSION_MAJOR" src/main/build/version.h | awk '{print $$3}' )
113 FC_VER_MINOR := $(shell grep " FC_VERSION_MINOR" src/main/build/version.h | awk '{print $$3}' )
114 FC_VER_PATCH := $(shell grep " FC_VERSION_PATCH" src/main/build/version.h | awk '{print $$3}' )
116 FC_VER := $(FC_VER_MAJOR).$(FC_VER_MINOR).$(FC_VER_PATCH)
118 # import config handling
119 include $(MAKE_SCRIPT_DIR)/config.mk
121 ifeq ($(CONFIG),)
122 ifeq ($(TARGET),)
123 TARGET := $(DEFAULT_TARGET)
124 endif
125 endif
127 # default xtal value
128 HSE_VALUE ?= 8000000
130 CI_TARGETS := $(BASE_TARGETS) $(filter CRAZYBEEF4SX1280 CRAZYBEEF4FR IFLIGHT_BLITZ_F722 NUCLEOF446 SPRACINGH7EXTREME SPRACINGH7RF, $(BASE_CONFIGS))
131 include $(ROOT)/src/main/target/$(TARGET)/target.mk
133 REVISION := norevision
134 ifeq ($(shell git diff --shortstat),)
135 REVISION := $(shell git log -1 --format="%h")
136 endif
138 LD_FLAGS :=
139 EXTRA_LD_FLAGS :=
142 # Default Tool options - can be overridden in {mcu}.mk files.
144 ifeq ($(DEBUG),GDB)
145 OPTIMISE_DEFAULT := -Og
147 LTO_FLAGS := $(OPTIMISE_DEFAULT)
148 DEBUG_FLAGS = -ggdb2 -gdwarf-5 -DDEBUG
149 else
150 ifeq ($(DEBUG),INFO)
151 DEBUG_FLAGS = -ggdb2
152 endif
153 OPTIMISATION_BASE := -flto -fuse-linker-plugin -ffast-math -fmerge-all-constants
154 OPTIMISE_DEFAULT := -O2
155 OPTIMISE_SPEED := -Ofast
156 OPTIMISE_SIZE := -Os
158 LTO_FLAGS := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
159 endif
161 VPATH := $(VPATH):$(MAKE_SCRIPT_DIR)/mcu
162 VPATH := $(VPATH):$(MAKE_SCRIPT_DIR)
164 # start specific includes
165 ifeq ($(TARGET_MCU),)
166 $(error No TARGET_MCU specified. Is the target.mk valid for $(TARGET)?)
167 endif
169 ifeq ($(TARGET_MCU_FAMILY),)
170 $(error No TARGET_MCU_FAMILY specified. Is the target.mk valid for $(TARGET)?)
171 endif
173 TARGET_FLAGS := -D$(TARGET) -D$(TARGET_MCU_FAMILY) $(TARGET_FLAGS)
175 ifneq ($(CONFIG),)
176 TARGET_FLAGS := $(TARGET_FLAGS) -DUSE_CONFIG
177 endif
179 include $(MAKE_SCRIPT_DIR)/mcu/$(TARGET_MCU_FAMILY).mk
181 # openocd specific includes
182 include $(MAKE_SCRIPT_DIR)/openocd.mk
184 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
185 ifeq ($(TARGET_FLASH_SIZE),)
186 ifneq ($(MCU_FLASH_SIZE),)
187 TARGET_FLASH_SIZE := $(MCU_FLASH_SIZE)
188 else
189 $(error MCU_FLASH_SIZE not configured for target $(TARGET))
190 endif
191 endif
193 DEVICE_FLAGS := $(DEVICE_FLAGS) -DTARGET_FLASH_SIZE=$(TARGET_FLASH_SIZE)
195 ifneq ($(HSE_VALUE),)
196 DEVICE_FLAGS := $(DEVICE_FLAGS) -DHSE_VALUE=$(HSE_VALUE)
197 endif
199 TARGET_DIR = $(ROOT)/src/main/target/$(TARGET)
200 TARGET_DIR_SRC = $(notdir $(wildcard $(TARGET_DIR)/*.c))
202 .DEFAULT_GOAL := hex
204 INCLUDE_DIRS := $(INCLUDE_DIRS) \
205 $(ROOT)/lib/main/MAVLink
207 INCLUDE_DIRS := $(INCLUDE_DIRS) \
208 $(TARGET_DIR)
210 VPATH := $(VPATH):$(TARGET_DIR)
212 include $(MAKE_SCRIPT_DIR)/source.mk
214 ###############################################################################
215 # Things that might need changing to use different tools
218 # Find out if ccache is installed on the system
219 CCACHE := ccache
220 RESULT = $(shell (which $(CCACHE) > /dev/null 2>&1; echo $$?) )
221 ifneq ($(RESULT),0)
222 CCACHE :=
223 endif
225 # Tool names
226 CROSS_CC := $(CCACHE) $(ARM_SDK_PREFIX)gcc
227 CROSS_CXX := $(CCACHE) $(ARM_SDK_PREFIX)g++
228 CROSS_GDB := $(ARM_SDK_PREFIX)gdb
229 OBJCOPY := $(ARM_SDK_PREFIX)objcopy
230 OBJDUMP := $(ARM_SDK_PREFIX)objdump
231 READELF := $(ARM_SDK_PREFIX)readelf
232 SIZE := $(ARM_SDK_PREFIX)size
233 DFUSE-PACK := src/utils/dfuse-pack.py
236 # Tool options.
238 CC_DEBUG_OPTIMISATION := $(OPTIMISE_DEFAULT)
239 CC_DEFAULT_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_DEFAULT)
240 CC_SPEED_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
241 CC_SIZE_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SIZE)
242 CC_NO_OPTIMISATION :=
245 # Added after GCC version update, remove once the warnings have been fixed
247 TEMPORARY_FLAGS :=
249 EXTRA_WARNING_FLAGS := -Wold-style-definition
251 CFLAGS += $(ARCH_FLAGS) \
252 $(addprefix -D,$(OPTIONS)) \
253 $(addprefix -I,$(INCLUDE_DIRS)) \
254 $(DEBUG_FLAGS) \
255 -std=gnu17 \
256 -Wall -Wextra -Werror -Wpedantic -Wunsafe-loop-optimizations -Wdouble-promotion \
257 $(EXTRA_WARNING_FLAGS) \
258 -ffunction-sections \
259 -fdata-sections \
260 -fno-common \
261 $(TEMPORARY_FLAGS) \
262 $(DEVICE_FLAGS) \
263 -D_GNU_SOURCE \
264 -DUSE_STDPERIPH_DRIVER \
265 -D$(TARGET) \
266 $(TARGET_FLAGS) \
267 -D'__FORKNAME__="$(FORKNAME)"' \
268 -D'__TARGET__="$(TARGET)"' \
269 -D'__REVISION__="$(REVISION)"' \
270 $(CONFIG_REVISION_DEFINE) \
271 -pipe \
272 -MMD -MP \
273 $(EXTRA_FLAGS)
275 ASFLAGS = $(ARCH_FLAGS) \
276 $(DEBUG_FLAGS) \
277 -x assembler-with-cpp \
278 $(addprefix -I,$(INCLUDE_DIRS)) \
279 -MMD -MP
281 ifeq ($(LD_FLAGS),)
282 LD_FLAGS = -lm \
283 -nostartfiles \
284 --specs=nano.specs \
285 -lc \
286 -lnosys \
287 $(ARCH_FLAGS) \
288 $(LTO_FLAGS) \
289 $(DEBUG_FLAGS) \
290 -static \
291 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
292 -Wl,-L$(LINKER_DIR) \
293 -Wl,--cref \
294 -Wl,--no-wchar-size-warning \
295 -Wl,--print-memory-usage \
296 -T$(LD_SCRIPT) \
297 $(EXTRA_LD_FLAGS)
298 endif
300 ###############################################################################
301 # No user-serviceable parts below
302 ###############################################################################
304 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
305 --std=c99 --inline-suppr --quiet --force \
306 $(addprefix -I,$(INCLUDE_DIRS)) \
307 -I/usr/include -I/usr/include/linux
309 TARGET_NAME := $(TARGET)
311 ifneq ($(CONFIG),)
312 TARGET_NAME := $(TARGET_NAME)_$(CONFIG)
313 endif
315 ifeq ($(REV),yes)
316 TARGET_NAME := $(TARGET_NAME)_$(REVISION)
317 endif
319 TARGET_FULLNAME = $(FORKNAME)_$(FC_VER)_$(TARGET_NAME)
321 # Things we will build
323 TARGET_BIN = $(BIN_DIR)/$(TARGET_FULLNAME).bin
324 TARGET_HEX = $(BIN_DIR)/$(TARGET_FULLNAME).hex
325 TARGET_DFU = $(BIN_DIR)/$(TARGET_FULLNAME).dfu
326 TARGET_ZIP = $(BIN_DIR)/$(TARGET_FULLNAME).zip
327 TARGET_OBJ_DIR = $(OBJECT_DIR)/$(TARGET_NAME)
328 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).elf
329 TARGET_EXST_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME)_EXST.elf
330 TARGET_UNPATCHED_BIN = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME)_UNPATCHED.bin
331 TARGET_LST = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).lst
332 TARGET_OBJS = $(addsuffix .o,$(addprefix $(TARGET_OBJ_DIR)/,$(basename $(SRC))))
333 TARGET_DEPS = $(addsuffix .d,$(addprefix $(TARGET_OBJ_DIR)/,$(basename $(SRC))))
334 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).map
336 TARGET_EXST_HASH_SECTION_FILE = $(TARGET_OBJ_DIR)/exst_hash_section.bin
338 TARGET_EF_HASH := $(shell echo -n "$(EXTRA_FLAGS)" | openssl dgst -md5 -r | awk '{print $$1;}')
339 TARGET_EF_HASH_FILE := $(TARGET_OBJ_DIR)/.efhash_$(TARGET_EF_HASH)
341 CLEAN_ARTIFACTS := $(TARGET_BIN)
342 CLEAN_ARTIFACTS += $(TARGET_HEX_REV) $(TARGET_HEX)
343 CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP)
344 CLEAN_ARTIFACTS += $(TARGET_LST)
345 CLEAN_ARTIFACTS += $(TARGET_DFU)
347 # Make sure build date and revision is updated on every incremental build
348 $(TARGET_OBJ_DIR)/build/version.o : $(SRC)
350 # List of buildable ELF files and their object dependencies.
351 # It would be nice to compute these lists, but that seems to be just beyond make.
353 $(TARGET_LST): $(TARGET_ELF)
354 $(V0) $(OBJDUMP) -S --disassemble $< > $@
356 ifeq ($(EXST),no)
357 $(TARGET_BIN): $(TARGET_ELF)
358 @echo "Creating BIN $(TARGET_BIN)" "$(STDOUT)"
359 $(V1) $(OBJCOPY) -O binary $< $@
361 $(TARGET_HEX): $(TARGET_ELF)
362 @echo "Creating HEX $(TARGET_HEX)" "$(STDOUT)"
363 $(V1) $(OBJCOPY) -O ihex --set-start 0x8000000 $< $@
365 $(TARGET_DFU): $(TARGET_HEX)
366 @echo "Creating DFU $(TARGET_DFU)" "$(STDOUT)"
367 $(V1) $(PYTHON) $(DFUSE-PACK) -i $< $@
369 else
370 CLEAN_ARTIFACTS += $(TARGET_UNPATCHED_BIN) $(TARGET_EXST_HASH_SECTION_FILE) $(TARGET_EXST_ELF)
372 $(TARGET_UNPATCHED_BIN): $(TARGET_ELF)
373 @echo "Creating BIN (without checksum) $(TARGET_UNPATCHED_BIN)" "$(STDOUT)"
374 $(V1) $(OBJCOPY) -O binary $< $@
376 $(TARGET_BIN): $(TARGET_UNPATCHED_BIN)
377 @echo "Creating EXST $(TARGET_BIN)" "$(STDOUT)"
378 # Linker script should allow .bin generation from a .elf which results in a file that is the same length as the FIRMWARE_SIZE.
379 # These 'dd' commands will pad a short binary to length FIRMWARE_SIZE.
380 $(V1) dd if=/dev/zero ibs=1k count=$(FIRMWARE_SIZE) of=$(TARGET_BIN)
381 $(V1) dd if=$(TARGET_UNPATCHED_BIN) of=$(TARGET_BIN) conv=notrunc
383 @echo "Generating MD5 hash of binary" "$(STDOUT)"
384 $(V1) openssl dgst -md5 $(TARGET_BIN) > $(TARGET_UNPATCHED_BIN).md5
386 @echo "Patching MD5 hash into binary" "$(STDOUT)"
387 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",(1024*$(FIRMWARE_SIZE))-16,$$2);}' | xxd -r - $(TARGET_BIN)
388 $(V1) echo $(FIRMWARE_SIZE) | awk '{printf("-s 0x%08x -l 16 -c 16 %s",(1024*$$1)-16,"$(TARGET_BIN)");}' | xargs xxd
390 # 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"
391 # Due to this a temporary file must be created and removed, even though we're only extracting data from the input file.
392 # If this temporary file is NOT used the $(TARGET_ELF) is modified, and running make a second time will result in
393 # a) regeneration of $(TARGET_BIN), and
394 # b) the results of $(TARGET_BIN) will not be as expected.
395 @echo "Extracting HASH section from unpatched EXST elf $(TARGET_ELF)" "$(STDOUT)"
396 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF).tmp --dump-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE) -j .exst_hash
397 rm $(TARGET_EXST_ELF).tmp
399 @echo "Patching MD5 hash into HASH section" "$(STDOUT)"
400 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",64-16,$$2);}' | xxd -r - $(TARGET_EXST_HASH_SECTION_FILE)
402 $(V1) @echo "Patching updated HASH section into $(TARGET_EXST_ELF)" "$(STDOUT)"
403 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF) --update-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE)
405 $(V1) $(READELF) -S $(TARGET_EXST_ELF)
406 $(V1) $(READELF) -l $(TARGET_EXST_ELF)
408 $(TARGET_HEX): $(TARGET_BIN)
409 $(if $(EXST_ADJUST_VMA),,$(error "EXST_ADJUST_VMA not specified"))
411 @echo "Creating EXST HEX from patched EXST BIN $(TARGET_BIN), VMA Adjust $(EXST_ADJUST_VMA)" "$(STDOUT)"
412 $(V1) $(OBJCOPY) -I binary -O ihex --adjust-vma=$(EXST_ADJUST_VMA) $(TARGET_BIN) $@
414 endif
416 $(TARGET_ELF): $(TARGET_OBJS) $(LD_SCRIPT) $(LD_SCRIPTS)
417 @echo "Linking $(TARGET_NAME)" "$(STDOUT)"
418 $(V1) $(CROSS_CC) -o $@ $(filter-out %.ld,$^) $(LD_FLAGS)
419 $(V1) $(SIZE) $(TARGET_ELF)
421 # Compile
423 ## compile_file takes two arguments: (1) optimisation description string and (2) optimisation compiler flag
424 define compile_file
425 echo "%% ($(1)) $<" "$(STDOUT)" && \
426 $(CROSS_CC) -c -o $@ $(CFLAGS) $(2) $<
427 endef
429 ifeq ($(DEBUG),GDB)
430 $(TARGET_OBJ_DIR)/%.o: %.c
431 $(V1) mkdir -p $(dir $@)
432 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
433 $(call compile_file,not optimised, $(CC_NO_OPTIMISATION)) \
435 $(call compile_file,debug,$(CC_DEBUG_OPTIMISATION)) \
437 else
438 $(TARGET_OBJ_DIR)/%.o: %.c
439 $(V1) mkdir -p $(dir $@)
440 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
441 $(call compile_file,not optimised,$(CC_NO_OPTIMISATION)) \
443 $(if $(findstring $(subst ./src/main/,,$<),$(SPEED_OPTIMISED_SRC)), \
444 $(call compile_file,speed optimised,$(CC_SPEED_OPTIMISATION)) \
446 $(if $(findstring $(subst ./src/main/,,$<),$(SIZE_OPTIMISED_SRC)), \
447 $(call compile_file,size optimised,$(CC_SIZE_OPTIMISATION)) \
449 $(call compile_file,optimised,$(CC_DEFAULT_OPTIMISATION)) \
453 endif
455 # Assemble
456 $(TARGET_OBJ_DIR)/%.o: %.s
457 $(V1) mkdir -p $(dir $@)
458 @echo "%% $(notdir $<)" "$(STDOUT)"
459 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
461 $(TARGET_OBJ_DIR)/%.o: %.S
462 $(V1) mkdir -p $(dir $@)
463 @echo "%% $(notdir $<)" "$(STDOUT)"
464 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
467 ## all : Build all currently built targets
468 all: $(CI_TARGETS)
470 $(BASE_TARGETS):
471 $(V0) @echo "Building target $@" && \
472 $(MAKE) hex TARGET=$@ && \
473 echo "Building $@ succeeded."
475 TARGETS_CLEAN = $(addsuffix _clean,$(BASE_TARGETS))
477 CONFIGS_CLEAN = $(addsuffix _clean,$(BASE_CONFIGS))
479 ## clean : clean up temporary / machine-generated files
480 clean:
481 @echo "Cleaning $(TARGET_NAME)"
482 $(V0) rm -f $(CLEAN_ARTIFACTS)
483 $(V0) rm -rf $(TARGET_OBJ_DIR)
484 @echo "Cleaning $(TARGET_NAME) succeeded."
486 ## test_clean : clean up temporary / machine-generated files (tests)
487 test-%_clean:
488 $(MAKE) test_clean
490 test_clean:
491 $(V0) cd src/test && $(MAKE) clean || true
493 ## <TARGET>_clean : clean up one specific target (alias for above)
494 $(TARGETS_CLEAN):
495 $(V0) $(MAKE) -j TARGET=$(subst _clean,,$@) clean
497 ## <CONFIG>_clean : clean up one specific target (alias for above)
498 $(CONFIGS_CLEAN):
499 $(V0) $(MAKE) -j CONFIG=$(subst _clean,,$@) clean
501 ## clean_all : clean all targets
502 clean_all: $(TARGETS_CLEAN) test_clean
504 ## configs : Hydrate configuration
505 configs: configs
507 TARGETS_FLASH = $(addsuffix _flash,$(BASE_TARGETS))
509 ## <TARGET>_flash : build and flash a target
510 $(TARGETS_FLASH):
511 $(V0) $(MAKE) hex TARGET=$(subst _flash,,$@)
512 ifneq (,$(findstring /dev/ttyUSB,$(SERIAL_DEVICE)))
513 $(V0) $(MAKE) tty_flash TARGET=$(subst _flash,,$@)
514 else
515 $(V0) $(MAKE) dfu_flash TARGET=$(subst _flash,,$@)
516 endif
518 ## tty_flash : flash firmware (.hex) onto flight controller via a serial port
519 tty_flash:
520 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
521 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
522 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
524 ## dfu_flash : flash firmware (.bin) onto flight controller via a DFU mode
525 dfu_flash:
526 ifneq (no-port-found,$(SERIAL_DEVICE))
527 # potentially this is because the MCU already is in DFU mode, try anyway
528 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
529 $(V0) sleep 1
530 endif
531 $(V0) $(MAKE) $(TARGET_DFU)
532 $(V0) dfu-util -a 0 -D $(TARGET_DFU) -s :leave
534 st-flash_$(TARGET): $(TARGET_BIN)
535 $(V0) st-flash --reset write $< 0x08000000
537 ## st-flash : flash firmware (.bin) onto flight controller
538 st-flash: st-flash_$(TARGET)
540 ifneq ($(OPENOCD_COMMAND),)
541 openocd-gdb: $(TARGET_ELF)
542 $(V0) $(OPENOCD_COMMAND) & $(CROSS_GDB) $(TARGET_ELF) -ex "target remote localhost:3333" -ex "load"
543 endif
545 TARGETS_ZIP = $(addsuffix _zip,$(BASE_TARGETS))
547 ## <TARGET>_zip : build target and zip it (useful for posting to GitHub)
548 $(TARGETS_ZIP):
549 $(V0) $(MAKE) hex TARGET=$(subst _zip,,$@)
550 $(V0) $(MAKE) zip TARGET=$(subst _zip,,$@)
552 zip:
553 $(V0) zip $(TARGET_ZIP) $(TARGET_HEX)
555 binary:
556 $(V0) $(MAKE) -j $(TARGET_BIN)
558 hex:
559 $(V0) $(MAKE) -j $(TARGET_HEX)
561 TARGETS_REVISION = $(addsuffix _rev,$(BASE_TARGETS))
562 ## <TARGET>_rev : build target and add revision to filename
563 $(TARGETS_REVISION):
564 $(V0) $(MAKE) hex REV=yes TARGET=$(subst _rev,,$@)
566 all_rev: $(addsuffix _rev,$(CI_TARGETS))
568 unbrick_$(TARGET): $(TARGET_HEX)
569 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
570 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
572 ## unbrick : unbrick flight controller
573 unbrick: unbrick_$(TARGET)
575 ## cppcheck : run static analysis on C source code
576 cppcheck: $(CSOURCES)
577 $(V0) $(CPPCHECK)
579 cppcheck-result.xml: $(CSOURCES)
580 $(V0) $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
582 # mkdirs
583 $(DIRECTORIES):
584 mkdir -p $@
586 ## version : print firmware version
587 version:
588 @echo $(FC_VER)
590 ## help : print this help message and exit
591 help: Makefile mk/tools.mk
592 @echo ""
593 @echo "Makefile for the $(FORKNAME) firmware"
594 @echo ""
595 @echo "Usage:"
596 @echo " make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"] [EXTRA_FLAGS=\"<extra_flags>\"]"
597 @echo "Or:"
598 @echo " make <target> [V=<verbosity>] [OPTIONS=\"<options>\"] [EXTRA_FLAGS=\"<extra_flags>\"]"
599 @echo "Or:"
600 @echo " make <config-target> [V=<verbosity>] [OPTIONS=\"<options>\"] [EXTRA_FLAGS=\"<extra_flags>\"]"
601 @echo ""
602 @echo "To pupulate configuration targets:"
603 @echo " make configs"
604 @echo ""
605 @echo "Valid TARGET values are: $(BASE_TARGETS)"
606 @echo ""
607 @sed -n 's/^## //p' $?
609 ## targets : print a list of all valid target platforms (for consumption by scripts)
610 targets:
611 @echo "Valid targets: $(BASE_TARGETS)"
612 @echo "Built targets: $(CI_TARGETS)"
613 @echo "Default target: $(TARGET)"
615 targets-ci-print:
616 @echo $(CI_TARGETS)
618 ## target-mcu : print the MCU type of the target
619 target-mcu:
620 @echo "$(TARGET_MCU_FAMILY) : $(TARGET_MCU)"
622 ## targets-by-mcu : make all targets that have a MCU_TYPE mcu
623 targets-by-mcu:
624 $(V1) for target in $${TARGETS}; do \
625 TARGET_MCU_TYPE=$$($(MAKE) -s TARGET=$${target} target-mcu); \
626 if [ "$${TARGET_MCU_TYPE}" = "$${MCU_TYPE}" ]; then \
627 if [ "$${DO_BUILD}" = 1 ]; then \
628 echo "Building target $${target}..."; \
629 $(MAKE) TARGET=$${target}; \
630 if [ $$? -ne 0 ]; then \
631 echo "Building target $${target} failed, aborting."; \
632 exit 1; \
633 fi; \
634 else \
635 echo -n "$${target} "; \
636 fi; \
637 fi; \
638 done
639 @echo
641 ## test : run the Betaflight test suite
642 ## junittest : run the Betaflight test suite, producing Junit XML result files.
643 ## 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)
644 ## test-all: run the Betaflight test suite including all per-target expanded tests
645 test junittest test-all test-representative:
646 $(V0) cd src/test && $(MAKE) $@
648 ## test_help : print the help message for the test suite (including a list of the available tests)
649 test_help:
650 $(V0) cd src/test && $(MAKE) help
652 ## test_versions : print the compiler versions used for the test suite
653 test_versions:
654 $(V0) cd src/test && $(MAKE) versions
656 ## test_% : run test 'test_%' from the test suite
657 test_%:
658 $(V0) cd src/test && $(MAKE) $@
660 $(TARGET_EF_HASH_FILE):
661 $(V1) mkdir -p $(dir $@)
662 $(V0) rm -f $(TARGET_OBJ_DIR)/.efhash_*
663 @echo "EF HASH -> $(TARGET_EF_HASH_FILE)"
664 $(V1) touch $(TARGET_EF_HASH_FILE)
666 # rebuild everything when makefile changes or the extra flags have changed
667 $(TARGET_OBJS): $(TARGET_EF_HASH_FILE) Makefile $(TARGET_DIR)/target.mk $(wildcard make/*) $(CONFIG_FILE)
669 # include auto-generated dependencies
670 -include $(TARGET_DEPS)