Merge pull request #11123 from KarateBrot/dynNotchGainFix
[betaflight.git] / Makefile
blobeb458444f275617e0f84876dc86c6b29b0459c10
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 VALID_TARGETS below
19 TARGET ?= STM32F405
21 # Compile-time options
22 OPTIONS ?=
24 # compile for OpenPilot BootLoader support
25 OPBL ?= no
27 # compile for External Storage Bootloader support
28 EXST ?= no
30 # compile for target loaded into RAM
31 RAM_BASED ?= no
33 # reserve space for custom defaults
34 CUSTOM_DEFAULTS_EXTENDED ?= no
36 # Debugger optons:
37 # empty - ordinary build with all optimizations enabled
38 # INFO - ordinary build with debug symbols and all optimizations enabled
39 # GDB - debug build with minimum number of optimizations
40 DEBUG ?=
42 # Insert the debugging hardfault debugger
43 # releases should not be built with this flag as it does not disable pwm output
44 DEBUG_HARDFAULTS ?=
46 # Serial port/Device for flashing
47 SERIAL_DEVICE ?= $(firstword $(wildcard /dev/ttyACM*) $(firstword $(wildcard /dev/ttyUSB*) no-port-found))
49 # Flash size (KB). Some low-end chips actually have more flash than advertised, use this to override.
50 FLASH_SIZE ?=
53 ###############################################################################
54 # Things that need to be maintained as the source changes
57 FORKNAME = betaflight
59 # Working directories
60 ROOT := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
61 SRC_DIR := $(ROOT)/src/main
62 OBJECT_DIR := $(ROOT)/obj/main
63 BIN_DIR := $(ROOT)/obj
64 CMSIS_DIR := $(ROOT)/lib/main/CMSIS
65 INCLUDE_DIRS := $(SRC_DIR) \
66 $(ROOT)/src/main/target \
67 $(ROOT)/src/main/startup
68 LINKER_DIR := $(ROOT)/src/link
70 ## V : Set verbosity level based on the V= parameter
71 ## V=0 Low
72 ## V=1 High
73 include $(ROOT)/make/build_verbosity.mk
75 # Build tools, so we all share the same versions
76 # import macros common to all supported build systems
77 include $(ROOT)/make/system-id.mk
79 # developer preferences, edit these at will, they'll be gitignored
80 -include $(ROOT)/make/local.mk
82 # pre-build sanity checks
83 include $(ROOT)/make/checks.mk
85 # configure some directories that are relative to wherever ROOT_DIR is located
86 TOOLS_DIR ?= $(ROOT)/tools
87 DL_DIR := $(ROOT)/downloads
89 export RM := rm
91 # import macros that are OS specific
92 include $(ROOT)/make/$(OSFAMILY).mk
94 # include the tools makefile
95 include $(ROOT)/make/tools.mk
97 # default xtal value for F4 targets
98 HSE_VALUE ?= 8000000
100 # used for turning on features like VCP and SDCARD
101 FEATURES =
103 # used to disable features based on flash space shortage (larger number => more features disabled)
104 FEATURE_CUT_LEVEL_SUPPLIED := $(FEATURE_CUT_LEVEL)
105 FEATURE_CUT_LEVEL =
107 # The list of targets to build for 'pre-push'
108 ifeq ($(OSFAMILY), macosx)
109 # SITL is not buildable on MacOS
110 PRE_PUSH_TARGET_LIST ?= $(UNIFIED_TARGETS) STM32F4DISCOVERY_DEBUG test-representative
111 else
112 PRE_PUSH_TARGET_LIST ?= $(UNIFIED_TARGETS) SITL STM32F4DISCOVERY_DEBUG test-representative
113 endif
115 include $(ROOT)/make/targets.mk
117 REVISION := norevision
118 ifeq ($(shell git diff --shortstat),)
119 REVISION := $(shell git log -1 --format="%h")
120 endif
122 FC_VER_MAJOR := $(shell grep " FC_VERSION_MAJOR" src/main/build/version.h | awk '{print $$3}' )
123 FC_VER_MINOR := $(shell grep " FC_VERSION_MINOR" src/main/build/version.h | awk '{print $$3}' )
124 FC_VER_PATCH := $(shell grep " FC_VERSION_PATCH" src/main/build/version.h | awk '{print $$3}' )
126 FC_VER := $(FC_VER_MAJOR).$(FC_VER_MINOR).$(FC_VER_PATCH)
128 # Search path for sources
129 VPATH := $(SRC_DIR):$(SRC_DIR)/startup
130 USBFS_DIR = $(ROOT)/lib/main/STM32_USB-FS-Device_Driver
131 USBPERIPH_SRC = $(notdir $(wildcard $(USBFS_DIR)/src/*.c))
132 FATFS_DIR = $(ROOT)/lib/main/FatFS
133 FATFS_SRC = $(notdir $(wildcard $(FATFS_DIR)/*.c))
135 CSOURCES := $(shell find $(SRC_DIR) -name '*.c')
137 LD_FLAGS :=
138 EXTRA_LD_FLAGS :=
141 # Default Tool options - can be overridden in {mcu}.mk files.
143 ifeq ($(DEBUG),GDB)
144 OPTIMISE_DEFAULT := -Og
146 LTO_FLAGS := $(OPTIMISE_DEFAULT)
147 DEBUG_FLAGS = -ggdb3 -DDEBUG
148 else
149 ifeq ($(DEBUG),INFO)
150 DEBUG_FLAGS = -ggdb3
151 endif
152 OPTIMISATION_BASE := -flto -fuse-linker-plugin -ffast-math -fmerge-all-constants
153 OPTIMISE_DEFAULT := -O2
154 OPTIMISE_SPEED := -Ofast
155 OPTIMISE_SIZE := -Os
157 LTO_FLAGS := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
158 endif
160 VPATH := $(VPATH):$(ROOT)/make/mcu
161 VPATH := $(VPATH):$(ROOT)/make
163 # start specific includes
164 include $(ROOT)/make/mcu/$(TARGET_MCU).mk
166 # openocd specific includes
167 include $(ROOT)/make/openocd.mk
169 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
170 ifeq ($(TARGET_FLASH_SIZE),)
171 ifneq ($(MCU_FLASH_SIZE),)
172 TARGET_FLASH_SIZE := $(MCU_FLASH_SIZE)
173 else
174 $(error MCU_FLASH_SIZE not configured for target $(TARGET))
175 endif
176 endif
178 DEVICE_FLAGS := $(DEVICE_FLAGS) -DTARGET_FLASH_SIZE=$(TARGET_FLASH_SIZE)
180 ifneq ($(HSE_VALUE),)
181 DEVICE_FLAGS := $(DEVICE_FLAGS) -DHSE_VALUE=$(HSE_VALUE)
182 endif
184 ifneq ($(FEATURE_CUT_LEVEL_SUPPLIED),)
185 DEVICE_FLAGS := $(DEVICE_FLAGS) -DFEATURE_CUT_LEVEL=$(FEATURE_CUT_LEVEL_SUPPLIED)
186 else ifneq ($(FEATURE_CUT_LEVEL),)
187 DEVICE_FLAGS := $(DEVICE_FLAGS) -DFEATURE_CUT_LEVEL=$(FEATURE_CUT_LEVEL)
188 endif
190 TARGET_DIR = $(ROOT)/src/main/target/$(BASE_TARGET)
191 TARGET_DIR_SRC = $(notdir $(wildcard $(TARGET_DIR)/*.c))
193 ifeq ($(OPBL),yes)
194 TARGET_FLAGS := -DOPBL $(TARGET_FLAGS)
195 .DEFAULT_GOAL := binary
196 else
197 .DEFAULT_GOAL := hex
198 endif
200 ifeq ($(CUSTOM_DEFAULTS_EXTENDED),yes)
201 TARGET_FLAGS += -DUSE_CUSTOM_DEFAULTS=
202 EXTRA_LD_FLAGS += -Wl,--defsym=USE_CUSTOM_DEFAULTS_EXTENDED=1
203 endif
205 INCLUDE_DIRS := $(INCLUDE_DIRS) \
206 $(ROOT)/lib/main/MAVLink
208 INCLUDE_DIRS := $(INCLUDE_DIRS) \
209 $(TARGET_DIR)
211 VPATH := $(VPATH):$(TARGET_DIR)
213 include $(ROOT)/make/source.mk
215 ###############################################################################
216 # Things that might need changing to use different tools
219 # Find out if ccache is installed on the system
220 CCACHE := ccache
221 RESULT = $(shell (which $(CCACHE) > /dev/null 2>&1; echo $$?) )
222 ifneq ($(RESULT),0)
223 CCACHE :=
224 endif
226 # Tool names
227 CROSS_CC := $(CCACHE) $(ARM_SDK_PREFIX)gcc
228 CROSS_CXX := $(CCACHE) $(ARM_SDK_PREFIX)g++
229 CROSS_GDB := $(ARM_SDK_PREFIX)gdb
230 OBJCOPY := $(ARM_SDK_PREFIX)objcopy
231 OBJDUMP := $(ARM_SDK_PREFIX)objdump
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 CFLAGS += $(ARCH_FLAGS) \
250 $(addprefix -D,$(OPTIONS)) \
251 $(addprefix -I,$(INCLUDE_DIRS)) \
252 $(DEBUG_FLAGS) \
253 -std=gnu11 \
254 -Wall -Wextra -Wunsafe-loop-optimizations -Wdouble-promotion \
255 -ffunction-sections \
256 -fdata-sections \
257 -fno-common \
258 -pedantic \
259 $(TEMPORARY_FLAGS) \
260 $(DEVICE_FLAGS) \
261 -D_GNU_SOURCE \
262 -DUSE_STDPERIPH_DRIVER \
263 -D$(TARGET) \
264 $(TARGET_FLAGS) \
265 -D'__FORKNAME__="$(FORKNAME)"' \
266 -D'__TARGET__="$(TARGET)"' \
267 -D'__REVISION__="$(REVISION)"' \
268 -save-temps=obj \
269 -MMD -MP \
270 $(EXTRA_FLAGS)
272 ASFLAGS = $(ARCH_FLAGS) \
273 $(DEBUG_FLAGS) \
274 -x assembler-with-cpp \
275 $(addprefix -I,$(INCLUDE_DIRS)) \
276 -MMD -MP
278 ifeq ($(LD_FLAGS),)
279 LD_FLAGS = -lm \
280 -nostartfiles \
281 --specs=nano.specs \
282 -lc \
283 -lnosys \
284 $(ARCH_FLAGS) \
285 $(LTO_FLAGS) \
286 $(DEBUG_FLAGS) \
287 -static \
288 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
289 -Wl,-L$(LINKER_DIR) \
290 -Wl,--cref \
291 -Wl,--no-wchar-size-warning \
292 -Wl,--print-memory-usage \
293 -T$(LD_SCRIPT) \
294 $(EXTRA_LD_FLAGS)
295 endif
297 ###############################################################################
298 # No user-serviceable parts below
299 ###############################################################################
301 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
302 --std=c99 --inline-suppr --quiet --force \
303 $(addprefix -I,$(INCLUDE_DIRS)) \
304 -I/usr/include -I/usr/include/linux
307 TARGET_BASENAME = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET)_$(REVISION)
310 # Things we will build
312 TARGET_BIN = $(TARGET_BASENAME).bin
313 TARGET_HEX = $(TARGET_BASENAME).hex
314 TARGET_DFU = $(TARGET_BASENAME).dfu
315 TARGET_ZIP = $(TARGET_BASENAME).zip
316 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).elf
317 TARGET_EXST_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET)_EXST.elf
318 TARGET_UNPATCHED_BIN = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET)_UNPATCHED.bin
319 TARGET_LST = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).lst
320 TARGET_OBJS = $(addsuffix .o,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
321 TARGET_DEPS = $(addsuffix .d,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
322 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).map
324 TARGET_EXST_HASH_SECTION_FILE = $(OBJECT_DIR)/$(TARGET)/exst_hash_section.bin
326 CLEAN_ARTIFACTS := $(TARGET_BIN)
327 CLEAN_ARTIFACTS += $(TARGET_HEX)
328 CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP)
329 CLEAN_ARTIFACTS += $(TARGET_LST)
330 CLEAN_ARTIFACTS += $(TARGET_DFU)
332 # Make sure build date and revision is updated on every incremental build
333 $(OBJECT_DIR)/$(TARGET)/build/version.o : $(SRC)
335 # List of buildable ELF files and their object dependencies.
336 # It would be nice to compute these lists, but that seems to be just beyond make.
338 $(TARGET_LST): $(TARGET_ELF)
339 $(V0) $(OBJDUMP) -S --disassemble $< > $@
341 ifeq ($(EXST),no)
342 $(TARGET_BIN): $(TARGET_ELF)
343 @echo "Creating BIN $(TARGET_BIN)" "$(STDOUT)"
344 $(V1) $(OBJCOPY) -O binary $< $@
346 $(TARGET_HEX): $(TARGET_ELF)
347 @echo "Creating HEX $(TARGET_HEX)" "$(STDOUT)"
348 $(V1) $(OBJCOPY) -O ihex --set-start 0x8000000 $< $@
350 $(TARGET_DFU): $(TARGET_HEX)
351 @echo "Creating DFU $(TARGET_DFU)" "$(STDOUT)"
352 $(V1) $(PYTHON) $(DFUSE-PACK) -i $< $@
354 else
355 CLEAN_ARTIFACTS += $(TARGET_UNPATCHED_BIN) $(TARGET_EXST_HASH_SECTION_FILE) $(TARGET_EXST_ELF)
357 $(TARGET_UNPATCHED_BIN): $(TARGET_ELF)
358 @echo "Creating BIN (without checksum) $(TARGET_UNPATCHED_BIN)" "$(STDOUT)"
359 $(V1) $(OBJCOPY) -O binary $< $@
361 $(TARGET_BIN): $(TARGET_UNPATCHED_BIN)
362 @echo "Creating EXST $(TARGET_BIN)" "$(STDOUT)"
363 # Linker script should allow .bin generation from a .elf which results in a file that is the same length as the FIRMWARE_SIZE.
364 # These 'dd' commands will pad a short binary to length FIRMWARE_SIZE.
365 $(V1) dd if=/dev/zero ibs=1k count=$(FIRMWARE_SIZE) of=$(TARGET_BIN)
366 $(V1) dd if=$(TARGET_UNPATCHED_BIN) of=$(TARGET_BIN) conv=notrunc
368 @echo "Generating MD5 hash of binary" "$(STDOUT)"
369 $(V1) openssl dgst -md5 $(TARGET_BIN) > $(TARGET_UNPATCHED_BIN).md5
371 @echo "Patching MD5 hash into binary" "$(STDOUT)"
372 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",(1024*$(FIRMWARE_SIZE))-16,$$2);}' | xxd -r - $(TARGET_BIN)
373 $(V1) echo $(FIRMWARE_SIZE) | awk '{printf("-s 0x%08x -l 16 -c 16 %s",(1024*$$1)-16,"$(TARGET_BIN)");}' | xargs xxd
375 # 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"
376 # Due to this a temporary file must be created and removed, even though we're only extracting data from the input file.
377 # If this temporary file is NOT used the $(TARGET_ELF) is modified, and running make a second time will result in
378 # a) regeneration of $(TARGET_BIN), and
379 # b) the results of $(TARGET_BIN) will not be as expected.
380 @echo "Extracting HASH section from unpatched EXST elf $(TARGET_ELF)" "$(STDOUT)"
381 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF).tmp --dump-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE)
382 rm $(TARGET_EXST_ELF).tmp
384 @echo "Patching MD5 hash into HASH section" "$(STDOUT)"
385 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",64-16,$$2);}' | xxd -r - $(TARGET_EXST_HASH_SECTION_FILE)
387 @echo "Patching updated HASH section into $(TARGET_EXST_ELF)" "$(STDOUT)"
388 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF) --update-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE)
390 $(TARGET_HEX): $(TARGET_BIN)
391 $(if $(EXST_ADJUST_VMA),,$(error "EXST_ADJUST_VMA not specified"))
393 @echo "Creating EXST HEX from patched EXST BIN $(TARGET_BIN), VMA Adjust $(EXST_ADJUST_VMA)" "$(STDOUT)"
394 $(V1) $(OBJCOPY) -I binary -O ihex --adjust-vma=$(EXST_ADJUST_VMA) $(TARGET_BIN) $@
396 endif
398 $(TARGET_ELF): $(TARGET_OBJS) $(LD_SCRIPT)
399 @echo "Linking $(TARGET)" "$(STDOUT)"
400 $(V1) $(CROSS_CC) -o $@ $(filter-out %.ld,$^) $(LD_FLAGS)
401 $(V1) $(SIZE) $(TARGET_ELF)
403 # Compile
405 ## compile_file takes two arguments: (1) optimisation description string and (2) optimisation compiler flag
406 define compile_file
407 echo "%% ($(1)) $<" "$(STDOUT)" && \
408 $(CROSS_CC) -c -o $@ $(CFLAGS) $(2) $<
409 endef
411 ifeq ($(DEBUG),GDB)
412 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
413 $(V1) mkdir -p $(dir $@)
414 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
415 $(call compile_file,not optimised, $(CC_NO_OPTIMISATION)) \
417 $(call compile_file,debug,$(CC_DEBUG_OPTIMISATION)) \
419 else
420 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
421 $(V1) mkdir -p $(dir $@)
422 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
423 $(call compile_file,not optimised,$(CC_NO_OPTIMISATION)) \
425 $(if $(findstring $(subst ./src/main/,,$<),$(SPEED_OPTIMISED_SRC)), \
426 $(call compile_file,speed optimised,$(CC_SPEED_OPTIMISATION)) \
428 $(if $(findstring $(subst ./src/main/,,$<),$(SIZE_OPTIMISED_SRC)), \
429 $(call compile_file,size optimised,$(CC_SIZE_OPTIMISATION)) \
431 $(call compile_file,optimised,$(CC_DEFAULT_OPTIMISATION)) \
435 endif
437 # Assemble
438 $(OBJECT_DIR)/$(TARGET)/%.o: %.s
439 $(V1) mkdir -p $(dir $@)
440 @echo "%% $(notdir $<)" "$(STDOUT)"
441 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
443 $(OBJECT_DIR)/$(TARGET)/%.o: %.S
444 $(V1) mkdir -p $(dir $@)
445 @echo "%% $(notdir $<)" "$(STDOUT)"
446 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
449 ## all : Build all currently built targets
450 all: $(CI_TARGETS)
452 ## all_all : Build all targets (including legacy / unsupported)
453 all_all: $(VALID_TARGETS)
455 ## unified : build all Unified Targets
456 unified: $(UNIFIED_TARGETS)
458 ## unified_zip : build all Unified Targets as zip files (for posting on GitHub)
459 unified_zip: $(addsuffix _clean,$(UNIFIED_TARGETS)) $(addsuffix _zip,$(UNIFIED_TARGETS))
461 ## legacy : Build legacy targets
462 legacy: $(LEGACY_TARGETS)
464 ## unsupported : Build unsupported targets
465 unsupported: $(UNSUPPORTED_TARGETS)
467 ## pre-push : The minimum verification that should be run before pushing, to check if CI has a chance of succeeding
468 pre-push:
469 $(MAKE) $(addsuffix _clean,$(PRE_PUSH_TARGET_LIST)) $(PRE_PUSH_TARGET_LIST) EXTRA_FLAGS=-Werror
471 ## targets-group-1 : build some targets
472 targets-group-1: $(GROUP_1_TARGETS)
474 ## targets-group-2 : build some targets
475 targets-group-2: $(GROUP_2_TARGETS)
477 ## targets-group-rest: build the rest of the targets (not listed in the other groups)
478 targets-group-rest: $(GROUP_OTHER_TARGETS)
480 $(VALID_TARGETS):
481 $(V0) @echo "Building $@" && \
482 $(MAKE) binary hex TARGET=$@ && \
483 echo "Building $@ succeeded."
485 $(NOBUILD_TARGETS):
486 $(MAKE) TARGET=$@
488 TARGETS_CLEAN = $(addsuffix _clean,$(VALID_TARGETS))
490 ## clean : clean up temporary / machine-generated files
491 clean:
492 @echo "Cleaning $(TARGET)"
493 $(V0) rm -f $(CLEAN_ARTIFACTS)
494 $(V0) rm -rf $(OBJECT_DIR)/$(TARGET)
495 @echo "Cleaning $(TARGET) succeeded."
497 ## test_clean : clean up temporary / machine-generated files (tests)
498 test-%_clean:
499 $(MAKE) test_clean
501 test_clean:
502 $(V0) cd src/test && $(MAKE) clean || true
504 ## <TARGET>_clean : clean up one specific target (alias for above)
505 $(TARGETS_CLEAN):
506 $(V0) $(MAKE) -j TARGET=$(subst _clean,,$@) clean
508 ## clean_all : clean all valid targets
509 clean_all: $(TARGETS_CLEAN) test_clean
511 TARGETS_FLASH = $(addsuffix _flash,$(VALID_TARGETS))
513 ## <TARGET>_flash : build and flash a target
514 $(TARGETS_FLASH):
515 $(V0) $(MAKE) hex TARGET=$(subst _flash,,$@)
516 ifneq (,$(findstring /dev/ttyUSB,$(SERIAL_DEVICE)))
517 $(V0) $(MAKE) tty_flash TARGET=$(subst _flash,,$@)
518 else
519 $(V0) $(MAKE) dfu_flash TARGET=$(subst _flash,,$@)
520 endif
522 ## tty_flash : flash firmware (.hex) onto flight controller via a serial port
523 tty_flash:
524 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
525 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
526 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
528 ## dfu_flash : flash firmware (.bin) onto flight controller via a DFU mode
529 dfu_flash:
530 ifneq (no-port-found,$(SERIAL_DEVICE))
531 # potentially this is because the MCU already is in DFU mode, try anyway
532 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
533 $(V0) sleep 1
534 endif
535 $(V0) $(MAKE) $(TARGET_DFU)
536 $(V0) dfu-util -a 0 -D $(TARGET_DFU) -s :leave
538 st-flash_$(TARGET): $(TARGET_BIN)
539 $(V0) st-flash --reset write $< 0x08000000
541 ## st-flash : flash firmware (.bin) onto flight controller
542 st-flash: st-flash_$(TARGET)
544 ifneq ($(OPENOCD_COMMAND),)
545 openocd-gdb: $(TARGET_ELF)
546 $(V0) $(OPENOCD_COMMAND) & $(CROSS_GDB) $(TARGET_ELF) -ex "target remote localhost:3333" -ex "load"
547 endif
549 TARGETS_ZIP = $(addsuffix _zip,$(VALID_TARGETS))
551 ## <TARGET>_zip : build target and zip it (useful for posting to GitHub)
552 $(TARGETS_ZIP):
553 $(V0) $(MAKE) hex TARGET=$(subst _zip,,$@)
554 $(V0) $(MAKE) zip TARGET=$(subst _zip,,$@)
556 zip:
557 $(V0) zip $(TARGET_ZIP) $(TARGET_HEX)
559 binary:
560 $(V0) $(MAKE) -j $(TARGET_BIN)
562 hex:
563 $(V0) $(MAKE) -j $(TARGET_HEX)
565 unbrick_$(TARGET): $(TARGET_HEX)
566 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
567 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
569 ## unbrick : unbrick flight controller
570 unbrick: unbrick_$(TARGET)
572 ## cppcheck : run static analysis on C source code
573 cppcheck: $(CSOURCES)
574 $(V0) $(CPPCHECK)
576 cppcheck-result.xml: $(CSOURCES)
577 $(V0) $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
579 # mkdirs
580 $(DL_DIR):
581 mkdir -p $@
583 $(TOOLS_DIR):
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 make/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>\"]"
597 @echo "Or:"
598 @echo " make <target> [V=<verbosity>] [OPTIONS=\"<options>\"]"
599 @echo ""
600 @echo "Valid TARGET values are: $(VALID_TARGETS)"
601 @echo ""
602 @sed -n 's/^## //p' $?
604 ## targets : print a list of all valid target platforms (for consumption by scripts)
605 targets:
606 @echo "Valid targets: $(VALID_TARGETS)"
607 @echo "Built targets: $(CI_TARGETS)"
608 @echo "Unified targets: $(UNIFIED_TARGETS)"
609 @echo "Legacy targets: $(LEGACY_TARGETS)"
610 @echo "Unsupported targets: $(UNSUPPORTED_TARGETS)"
611 @echo "Default target: $(TARGET)"
612 @echo "targets-group-1: $(GROUP_1_TARGETS)"
613 @echo "targets-group-2: $(GROUP_2_TARGETS)"
614 @echo "targets-group-rest: $(GROUP_OTHER_TARGETS)"
616 @echo "targets-group-1: $(words $(GROUP_1_TARGETS)) targets"
617 @echo "targets-group-2: $(words $(GROUP_2_TARGETS)) targets"
618 @echo "targets-group-rest: $(words $(GROUP_OTHER_TARGETS)) targets"
619 @echo "total in all groups $(words $(CI_TARGETS)) targets"
621 ## target-mcu : print the MCU type of the target
622 target-mcu:
623 @echo $(TARGET_MCU)
625 ## targets-by-mcu : make all targets that have a MCU_TYPE mcu
626 targets-by-mcu:
627 $(V1) for target in $${TARGETS}; do \
628 TARGET_MCU_TYPE=$$($(MAKE) -s TARGET=$${target} target-mcu); \
629 if [ "$${TARGET_MCU_TYPE}" = "$${MCU_TYPE}" ]; then \
630 if [ "$${DO_BUILD}" = 1 ]; then \
631 echo "Building target $${target}..."; \
632 $(MAKE) TARGET=$${target}; \
633 if [ $$? -ne 0 ]; then \
634 echo "Building target $${target} failed, aborting."; \
635 exit 1; \
636 fi; \
637 else \
638 echo -n "$${target} "; \
639 fi; \
640 fi; \
641 done
642 @echo
644 ## targets-f3 : make all F3 targets
645 targets-f3:
646 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F3 TARGETS="$(VALID_TARGETS)" DO_BUILD=1
648 targets-f3-print:
649 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F3 TARGETS="$(VALID_TARGETS)"
651 ## targets-f4 : make all F4 targets
652 targets-f4:
653 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4 TARGETS="$(VALID_TARGETS)" DO_BUILD=1
655 targets-f4-print:
656 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4 TARGETS="$(VALID_TARGETS)"
658 targets-ci-f4-print:
659 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4 TARGETS="$(CI_TARGETS)"
661 ## targets-f7 : make all F7 targets
662 targets-f7:
663 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7 TARGETS="$(VALID_TARGETS)" DO_BUILD=1
665 targets-f7-print:
666 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7 TARGETS="$(VALID_TARGETS)"
668 targets-ci-f7-print:
669 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7 TARGETS="$(CI_TARGETS)"
671 ## test : run the Betaflight test suite
672 ## junittest : run the Betaflight test suite, producing Junit XML result files.
673 ## 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)
674 ## test-all: run the Betaflight test suite including all per-target expanded tests
675 test junittest test-all test-representative:
676 $(V0) cd src/test && $(MAKE) $@
678 ## test_help : print the help message for the test suite (including a list of the available tests)
679 test_help:
680 $(V0) cd src/test && $(MAKE) help
682 ## test_versions : print the compiler versions used for the test suite
683 test_versions:
684 $(V0) cd src/test && $(MAKE) versions
686 ## test_% : run test 'test_%' from the test suite
687 test_%:
688 $(V0) cd src/test && $(MAKE) $@
691 # rebuild everything when makefile changes
692 $(TARGET_OBJS): Makefile $(TARGET_DIR)/target.mk $(wildcard make/*)
694 # include auto-generated dependencies
695 -include $(TARGET_DEPS)