Update README.md
[betaflight.git] / Makefile
blob2c2cca5857704775d2bbe40395f24399cb7595b9
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 External Storage Bootloader support
25 EXST ?= no
27 # compile for target loaded into RAM
28 RAM_BASED ?= no
30 # reserve space for custom defaults
31 CUSTOM_DEFAULTS_EXTENDED ?= no
33 # Debugger optons:
34 # empty - ordinary build with all optimizations enabled
35 # INFO - ordinary build with debug symbols and all optimizations enabled
36 # GDB - debug build with minimum number of optimizations
37 DEBUG ?=
39 # Insert the debugging hardfault debugger
40 # releases should not be built with this flag as it does not disable pwm output
41 DEBUG_HARDFAULTS ?=
43 # Serial port/Device for flashing
44 SERIAL_DEVICE ?= $(firstword $(wildcard /dev/ttyACM*) $(firstword $(wildcard /dev/ttyUSB*) no-port-found))
46 # Flash size (KB). Some low-end chips actually have more flash than advertised, use this to override.
47 FLASH_SIZE ?=
49 ###############################################################################
50 # Things that need to be maintained as the source changes
53 FORKNAME = betaflight
55 # Working directories
56 ROOT := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
57 SRC_DIR := $(ROOT)/src/main
58 OBJECT_DIR := $(ROOT)/obj/main
59 BIN_DIR := $(ROOT)/obj
60 CMSIS_DIR := $(ROOT)/lib/main/CMSIS
61 INCLUDE_DIRS := $(SRC_DIR) \
62 $(ROOT)/src/main/target \
63 $(ROOT)/src/main/startup
64 LINKER_DIR := $(ROOT)/src/link
66 ## V : Set verbosity level based on the V= parameter
67 ## V=0 Low
68 ## V=1 High
69 include $(ROOT)/make/build_verbosity.mk
71 # Build tools, so we all share the same versions
72 # import macros common to all supported build systems
73 include $(ROOT)/make/system-id.mk
75 # developer preferences, edit these at will, they'll be gitignored
76 -include $(ROOT)/make/local.mk
78 # pre-build sanity checks
79 include $(ROOT)/make/checks.mk
81 # configure some directories that are relative to wherever ROOT_DIR is located
82 TOOLS_DIR ?= $(ROOT)/tools
83 DL_DIR := $(ROOT)/downloads
85 export RM := rm
87 # import macros that are OS specific
88 include $(ROOT)/make/$(OSFAMILY).mk
90 # include the tools makefile
91 include $(ROOT)/make/tools.mk
93 # default xtal value for F4 targets
94 HSE_VALUE ?= 8000000
96 # used for turning on features like VCP and SDCARD
97 FEATURES =
99 # used to disable features based on flash space shortage (larger number => more features disabled)
100 FEATURE_CUT_LEVEL_SUPPLIED := $(FEATURE_CUT_LEVEL)
101 FEATURE_CUT_LEVEL =
103 # The list of targets to build for 'pre-push'
104 ifeq ($(OSFAMILY), macosx)
105 # SITL is not buildable on MacOS
106 PRE_PUSH_TARGET_LIST ?= $(UNIFIED_TARGETS) STM32F4DISCOVERY_DEBUG test-representative
107 else
108 PRE_PUSH_TARGET_LIST ?= $(UNIFIED_TARGETS) SITL STM32F4DISCOVERY_DEBUG test-representative
109 endif
111 include $(ROOT)/make/targets.mk
113 REVISION := norevision
114 ifeq ($(shell git diff --shortstat),)
115 REVISION := $(shell git log -1 --format="%h")
116 endif
118 FC_VER_MAJOR := $(shell grep " FC_VERSION_MAJOR" src/main/build/version.h | awk '{print $$3}' )
119 FC_VER_MINOR := $(shell grep " FC_VERSION_MINOR" src/main/build/version.h | awk '{print $$3}' )
120 FC_VER_PATCH := $(shell grep " FC_VERSION_PATCH" src/main/build/version.h | awk '{print $$3}' )
122 FC_VER := $(FC_VER_MAJOR).$(FC_VER_MINOR).$(FC_VER_PATCH)
124 # Search path for sources
125 VPATH := $(SRC_DIR):$(SRC_DIR)/startup
126 FATFS_DIR = $(ROOT)/lib/main/FatFS
127 FATFS_SRC = $(notdir $(wildcard $(FATFS_DIR)/*.c))
129 CSOURCES := $(shell find $(SRC_DIR) -name '*.c')
131 LD_FLAGS :=
132 EXTRA_LD_FLAGS :=
135 # Default Tool options - can be overridden in {mcu}.mk files.
137 ifeq ($(DEBUG),GDB)
138 OPTIMISE_DEFAULT := -Og
140 LTO_FLAGS := $(OPTIMISE_DEFAULT)
141 DEBUG_FLAGS = -ggdb3 -gdwarf-5 -DDEBUG
142 else
143 ifeq ($(DEBUG),INFO)
144 DEBUG_FLAGS = -ggdb3
145 endif
146 OPTIMISATION_BASE := -flto -fuse-linker-plugin -ffast-math -fmerge-all-constants
147 OPTIMISE_DEFAULT := -O2
148 OPTIMISE_SPEED := -Ofast
149 OPTIMISE_SIZE := -Os
151 LTO_FLAGS := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
152 endif
154 VPATH := $(VPATH):$(ROOT)/make/mcu
155 VPATH := $(VPATH):$(ROOT)/make
157 # start specific includes
158 include $(ROOT)/make/mcu/$(TARGET_MCU).mk
160 # openocd specific includes
161 include $(ROOT)/make/openocd.mk
163 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
164 ifeq ($(TARGET_FLASH_SIZE),)
165 ifneq ($(MCU_FLASH_SIZE),)
166 TARGET_FLASH_SIZE := $(MCU_FLASH_SIZE)
167 else
168 $(error MCU_FLASH_SIZE not configured for target $(TARGET))
169 endif
170 endif
172 DEVICE_FLAGS := $(DEVICE_FLAGS) -DTARGET_FLASH_SIZE=$(TARGET_FLASH_SIZE)
174 ifneq ($(HSE_VALUE),)
175 DEVICE_FLAGS := $(DEVICE_FLAGS) -DHSE_VALUE=$(HSE_VALUE)
176 endif
178 ifneq ($(FEATURE_CUT_LEVEL_SUPPLIED),)
179 DEVICE_FLAGS := $(DEVICE_FLAGS) -DFEATURE_CUT_LEVEL=$(FEATURE_CUT_LEVEL_SUPPLIED)
180 else ifneq ($(FEATURE_CUT_LEVEL),)
181 DEVICE_FLAGS := $(DEVICE_FLAGS) -DFEATURE_CUT_LEVEL=$(FEATURE_CUT_LEVEL)
182 endif
184 TARGET_DIR = $(ROOT)/src/main/target/$(BASE_TARGET)
185 TARGET_DIR_SRC = $(notdir $(wildcard $(TARGET_DIR)/*.c))
187 .DEFAULT_GOAL := hex
189 ifeq ($(CUSTOM_DEFAULTS_EXTENDED),yes)
190 TARGET_FLAGS += -DUSE_CUSTOM_DEFAULTS=
191 EXTRA_LD_FLAGS += -Wl,--defsym=USE_CUSTOM_DEFAULTS_EXTENDED=1
192 endif
194 INCLUDE_DIRS := $(INCLUDE_DIRS) \
195 $(ROOT)/lib/main/MAVLink
197 INCLUDE_DIRS := $(INCLUDE_DIRS) \
198 $(TARGET_DIR)
200 VPATH := $(VPATH):$(TARGET_DIR)
202 include $(ROOT)/make/source.mk
204 ###############################################################################
205 # Things that might need changing to use different tools
208 # Find out if ccache is installed on the system
209 CCACHE := ccache
210 RESULT = $(shell (which $(CCACHE) > /dev/null 2>&1; echo $$?) )
211 ifneq ($(RESULT),0)
212 CCACHE :=
213 endif
215 # Tool names
216 CROSS_CC := $(CCACHE) $(ARM_SDK_PREFIX)gcc
217 CROSS_CXX := $(CCACHE) $(ARM_SDK_PREFIX)g++
218 CROSS_GDB := $(ARM_SDK_PREFIX)gdb
219 OBJCOPY := $(ARM_SDK_PREFIX)objcopy
220 OBJDUMP := $(ARM_SDK_PREFIX)objdump
221 READELF := $(ARM_SDK_PREFIX)readelf
222 SIZE := $(ARM_SDK_PREFIX)size
223 DFUSE-PACK := src/utils/dfuse-pack.py
226 # Tool options.
228 CC_DEBUG_OPTIMISATION := $(OPTIMISE_DEFAULT)
229 CC_DEFAULT_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_DEFAULT)
230 CC_SPEED_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
231 CC_SIZE_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SIZE)
232 CC_NO_OPTIMISATION :=
235 # Added after GCC version update, remove once the warnings have been fixed
237 TEMPORARY_FLAGS :=
239 EXTRA_WARNING_FLAGS := -Wold-style-definition
241 CFLAGS += $(ARCH_FLAGS) \
242 $(addprefix -D,$(OPTIONS)) \
243 $(addprefix -I,$(INCLUDE_DIRS)) \
244 $(DEBUG_FLAGS) \
245 -std=gnu17 \
246 -Wall -Wextra -Werror -Wpedantic -Wunsafe-loop-optimizations -Wdouble-promotion \
247 $(EXTRA_WARNING_FLAGS) \
248 -ffunction-sections \
249 -fdata-sections \
250 -fno-common \
251 $(TEMPORARY_FLAGS) \
252 $(DEVICE_FLAGS) \
253 -D_GNU_SOURCE \
254 -DUSE_STDPERIPH_DRIVER \
255 -D$(TARGET) \
256 $(TARGET_FLAGS) \
257 -D'__FORKNAME__="$(FORKNAME)"' \
258 -D'__TARGET__="$(TARGET)"' \
259 -D'__REVISION__="$(REVISION)"' \
260 -pipe \
261 -MMD -MP \
262 $(EXTRA_FLAGS)
264 ASFLAGS = $(ARCH_FLAGS) \
265 $(DEBUG_FLAGS) \
266 -x assembler-with-cpp \
267 $(addprefix -I,$(INCLUDE_DIRS)) \
268 -MMD -MP
270 ifeq ($(LD_FLAGS),)
271 LD_FLAGS = -lm \
272 -nostartfiles \
273 --specs=nano.specs \
274 -lc \
275 -lnosys \
276 $(ARCH_FLAGS) \
277 $(LTO_FLAGS) \
278 $(DEBUG_FLAGS) \
279 -static \
280 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
281 -Wl,-L$(LINKER_DIR) \
282 -Wl,--cref \
283 -Wl,--no-wchar-size-warning \
284 -Wl,--print-memory-usage \
285 -T$(LD_SCRIPT) \
286 $(EXTRA_LD_FLAGS)
287 endif
289 ###############################################################################
290 # No user-serviceable parts below
291 ###############################################################################
293 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
294 --std=c99 --inline-suppr --quiet --force \
295 $(addprefix -I,$(INCLUDE_DIRS)) \
296 -I/usr/include -I/usr/include/linux
298 TARGET_BASENAME = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET)
301 # Things we will build
303 TARGET_BIN = $(TARGET_BASENAME).bin
304 TARGET_HEX = $(TARGET_BASENAME).hex
305 TARGET_HEX_REV = $(TARGET_BASENAME)_$(REVISION).hex
306 TARGET_DFU = $(TARGET_BASENAME).dfu
307 TARGET_ZIP = $(TARGET_BASENAME).zip
308 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).elf
309 TARGET_EXST_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET)_EXST.elf
310 TARGET_UNPATCHED_BIN = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET)_UNPATCHED.bin
311 TARGET_LST = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).lst
312 TARGET_OBJS = $(addsuffix .o,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
313 TARGET_DEPS = $(addsuffix .d,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
314 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).map
316 TARGET_EXST_HASH_SECTION_FILE = $(OBJECT_DIR)/$(TARGET)/exst_hash_section.bin
318 CLEAN_ARTIFACTS := $(TARGET_BIN)
319 CLEAN_ARTIFACTS += $(TARGET_HEX_REV) $(TARGET_HEX)
320 CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP)
321 CLEAN_ARTIFACTS += $(TARGET_LST)
322 CLEAN_ARTIFACTS += $(TARGET_DFU)
324 # Make sure build date and revision is updated on every incremental build
325 $(OBJECT_DIR)/$(TARGET)/build/version.o : $(SRC)
327 # List of buildable ELF files and their object dependencies.
328 # It would be nice to compute these lists, but that seems to be just beyond make.
330 $(TARGET_LST): $(TARGET_ELF)
331 $(V0) $(OBJDUMP) -S --disassemble $< > $@
333 ifeq ($(EXST),no)
334 $(TARGET_BIN): $(TARGET_ELF)
335 @echo "Creating BIN $(TARGET_BIN)" "$(STDOUT)"
336 $(V1) $(OBJCOPY) -O binary $< $@
338 $(TARGET_HEX): $(TARGET_ELF)
339 @echo "Creating HEX $(TARGET_HEX)" "$(STDOUT)"
340 $(V1) $(OBJCOPY) -O ihex --set-start 0x8000000 $< $@
342 $(TARGET_DFU): $(TARGET_HEX)
343 @echo "Creating DFU $(TARGET_DFU)" "$(STDOUT)"
344 $(V1) $(PYTHON) $(DFUSE-PACK) -i $< $@
346 else
347 CLEAN_ARTIFACTS += $(TARGET_UNPATCHED_BIN) $(TARGET_EXST_HASH_SECTION_FILE) $(TARGET_EXST_ELF)
349 $(TARGET_UNPATCHED_BIN): $(TARGET_ELF)
350 @echo "Creating BIN (without checksum) $(TARGET_UNPATCHED_BIN)" "$(STDOUT)"
351 $(V1) $(OBJCOPY) -O binary $< $@
353 $(TARGET_BIN): $(TARGET_UNPATCHED_BIN)
354 @echo "Creating EXST $(TARGET_BIN)" "$(STDOUT)"
355 # Linker script should allow .bin generation from a .elf which results in a file that is the same length as the FIRMWARE_SIZE.
356 # These 'dd' commands will pad a short binary to length FIRMWARE_SIZE.
357 $(V1) dd if=/dev/zero ibs=1k count=$(FIRMWARE_SIZE) of=$(TARGET_BIN)
358 $(V1) dd if=$(TARGET_UNPATCHED_BIN) of=$(TARGET_BIN) conv=notrunc
360 @echo "Generating MD5 hash of binary" "$(STDOUT)"
361 $(V1) openssl dgst -md5 $(TARGET_BIN) > $(TARGET_UNPATCHED_BIN).md5
363 @echo "Patching MD5 hash into binary" "$(STDOUT)"
364 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",(1024*$(FIRMWARE_SIZE))-16,$$2);}' | xxd -r - $(TARGET_BIN)
365 $(V1) echo $(FIRMWARE_SIZE) | awk '{printf("-s 0x%08x -l 16 -c 16 %s",(1024*$$1)-16,"$(TARGET_BIN)");}' | xargs xxd
367 # 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"
368 # Due to this a temporary file must be created and removed, even though we're only extracting data from the input file.
369 # If this temporary file is NOT used the $(TARGET_ELF) is modified, and running make a second time will result in
370 # a) regeneration of $(TARGET_BIN), and
371 # b) the results of $(TARGET_BIN) will not be as expected.
372 @echo "Extracting HASH section from unpatched EXST elf $(TARGET_ELF)" "$(STDOUT)"
373 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF).tmp --dump-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE) -j .exst_hash
374 rm $(TARGET_EXST_ELF).tmp
376 @echo "Patching MD5 hash into HASH section" "$(STDOUT)"
377 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",64-16,$$2);}' | xxd -r - $(TARGET_EXST_HASH_SECTION_FILE)
379 # For some currently unknown reason, OBJCOPY, with only input/output files, will generate a file around 2GB for the H730 unless we remove an unused-section
380 # As a workaround drop the ._user_heap_stack section, which is only used during build to show errors if there's not enough space for the heap/stack.
381 # The issue can be seen with `readelf -S $(TARGET_EXST_ELF)' vs `readelf -S $(TARGET_ELF)`
382 $(V1) @echo "Patching updated HASH section into $(TARGET_EXST_ELF)" "$(STDOUT)"
383 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF) --remove-section ._user_heap_stack --update-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE)
385 $(V1) $(READELF) -S $(TARGET_EXST_ELF)
386 $(V1) $(READELF) -l $(TARGET_EXST_ELF)
388 $(TARGET_HEX): $(TARGET_BIN)
389 $(if $(EXST_ADJUST_VMA),,$(error "EXST_ADJUST_VMA not specified"))
391 @echo "Creating EXST HEX from patched EXST BIN $(TARGET_BIN), VMA Adjust $(EXST_ADJUST_VMA)" "$(STDOUT)"
392 $(V1) $(OBJCOPY) -I binary -O ihex --adjust-vma=$(EXST_ADJUST_VMA) $(TARGET_BIN) $@
394 endif
396 $(TARGET_ELF): $(TARGET_OBJS) $(LD_SCRIPT) $(LD_SCRIPTS)
397 @echo "Linking $(TARGET)" "$(STDOUT)"
398 $(V1) $(CROSS_CC) -o $@ $(filter-out %.ld,$^) $(LD_FLAGS)
399 $(V1) $(SIZE) $(TARGET_ELF)
401 # Compile
403 ## compile_file takes two arguments: (1) optimisation description string and (2) optimisation compiler flag
404 define compile_file
405 echo "%% ($(1)) $<" "$(STDOUT)" && \
406 $(CROSS_CC) -c -o $@ $(CFLAGS) $(2) $<
407 endef
409 ifeq ($(DEBUG),GDB)
410 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
411 $(V1) mkdir -p $(dir $@)
412 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
413 $(call compile_file,not optimised, $(CC_NO_OPTIMISATION)) \
415 $(call compile_file,debug,$(CC_DEBUG_OPTIMISATION)) \
417 else
418 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
419 $(V1) mkdir -p $(dir $@)
420 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
421 $(call compile_file,not optimised,$(CC_NO_OPTIMISATION)) \
423 $(if $(findstring $(subst ./src/main/,,$<),$(SPEED_OPTIMISED_SRC)), \
424 $(call compile_file,speed optimised,$(CC_SPEED_OPTIMISATION)) \
426 $(if $(findstring $(subst ./src/main/,,$<),$(SIZE_OPTIMISED_SRC)), \
427 $(call compile_file,size optimised,$(CC_SIZE_OPTIMISATION)) \
429 $(call compile_file,optimised,$(CC_DEFAULT_OPTIMISATION)) \
433 endif
435 # Assemble
436 $(OBJECT_DIR)/$(TARGET)/%.o: %.s
437 $(V1) mkdir -p $(dir $@)
438 @echo "%% $(notdir $<)" "$(STDOUT)"
439 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
441 $(OBJECT_DIR)/$(TARGET)/%.o: %.S
442 $(V1) mkdir -p $(dir $@)
443 @echo "%% $(notdir $<)" "$(STDOUT)"
444 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
447 ## all : Build all currently built targets
448 all: $(CI_TARGETS)
450 ## all_all : Build all targets (including legacy / unsupported)
451 all_all: $(VALID_TARGETS)
453 ## unified : build all Unified Targets
454 unified: $(UNIFIED_TARGETS)
456 ## unified_zip : build all Unified Targets as zip files (for posting on GitHub)
457 unified_zip: $(addsuffix _clean,$(UNIFIED_TARGETS)) $(addsuffix _zip,$(UNIFIED_TARGETS))
459 ## legacy : Build legacy targets
460 legacy: $(LEGACY_TARGETS)
462 ## unsupported : Build unsupported targets
463 unsupported: $(UNSUPPORTED_TARGETS)
465 ## pre-push : The minimum verification that should be run before pushing, to check if CI has a chance of succeeding
466 pre-push:
467 $(MAKE) $(addsuffix _clean,$(PRE_PUSH_TARGET_LIST)) $(PRE_PUSH_TARGET_LIST) EXTRA_FLAGS=-Werror
469 ## targets-group-1 : build some targets
470 targets-group-1: $(GROUP_1_TARGETS)
472 ## targets-group-2 : build some targets
473 targets-group-2: $(GROUP_2_TARGETS)
475 ## targets-group-rest: build the rest of the targets (not listed in the other groups)
476 targets-group-rest: $(GROUP_OTHER_TARGETS)
478 $(VALID_TARGETS):
479 $(V0) @echo "Building $@" && \
480 $(MAKE) hex TARGET=$@ && \
481 echo "Building $@ succeeded."
483 $(NOBUILD_TARGETS):
484 $(MAKE) TARGET=$@
486 TARGETS_CLEAN = $(addsuffix _clean,$(VALID_TARGETS))
488 ## clean : clean up temporary / machine-generated files
489 clean:
490 @echo "Cleaning $(TARGET)"
491 $(V0) rm -f $(CLEAN_ARTIFACTS)
492 $(V0) rm -rf $(OBJECT_DIR)/$(TARGET)
493 @echo "Cleaning $(TARGET) succeeded."
495 ## test_clean : clean up temporary / machine-generated files (tests)
496 test-%_clean:
497 $(MAKE) test_clean
499 test_clean:
500 $(V0) cd src/test && $(MAKE) clean || true
502 ## <TARGET>_clean : clean up one specific target (alias for above)
503 $(TARGETS_CLEAN):
504 $(V0) $(MAKE) -j TARGET=$(subst _clean,,$@) clean
506 ## clean_all : clean all valid targets
507 clean_all: $(TARGETS_CLEAN) test_clean
509 TARGETS_FLASH = $(addsuffix _flash,$(VALID_TARGETS))
511 ## <TARGET>_flash : build and flash a target
512 $(TARGETS_FLASH):
513 $(V0) $(MAKE) hex TARGET=$(subst _flash,,$@)
514 ifneq (,$(findstring /dev/ttyUSB,$(SERIAL_DEVICE)))
515 $(V0) $(MAKE) tty_flash TARGET=$(subst _flash,,$@)
516 else
517 $(V0) $(MAKE) dfu_flash TARGET=$(subst _flash,,$@)
518 endif
520 ## tty_flash : flash firmware (.hex) onto flight controller via a serial port
521 tty_flash:
522 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
523 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
524 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
526 ## dfu_flash : flash firmware (.bin) onto flight controller via a DFU mode
527 dfu_flash:
528 ifneq (no-port-found,$(SERIAL_DEVICE))
529 # potentially this is because the MCU already is in DFU mode, try anyway
530 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
531 $(V0) sleep 1
532 endif
533 $(V0) $(MAKE) $(TARGET_DFU)
534 $(V0) dfu-util -a 0 -D $(TARGET_DFU) -s :leave
536 st-flash_$(TARGET): $(TARGET_BIN)
537 $(V0) st-flash --reset write $< 0x08000000
539 ## st-flash : flash firmware (.bin) onto flight controller
540 st-flash: st-flash_$(TARGET)
542 ifneq ($(OPENOCD_COMMAND),)
543 openocd-gdb: $(TARGET_ELF)
544 $(V0) $(OPENOCD_COMMAND) & $(CROSS_GDB) $(TARGET_ELF) -ex "target remote localhost:3333" -ex "load"
545 endif
547 TARGETS_ZIP = $(addsuffix _zip,$(VALID_TARGETS))
549 ## <TARGET>_zip : build target and zip it (useful for posting to GitHub)
550 $(TARGETS_ZIP):
551 $(V0) $(MAKE) hex TARGET=$(subst _zip,,$@)
552 $(V0) $(MAKE) zip TARGET=$(subst _zip,,$@)
554 zip:
555 $(V0) zip $(TARGET_ZIP) $(TARGET_HEX)
557 binary:
558 $(V0) $(MAKE) -j $(TARGET_BIN)
560 hex:
561 $(V0) $(MAKE) -j $(TARGET_HEX)
563 TARGETS_REVISION = $(addsuffix _rev,$(VALID_TARGETS))
564 ## <TARGET>_rev : build target and add revision to filename
565 $(TARGETS_REVISION):
566 $(V0) $(MAKE) hex_rev TARGET=$(subst _rev,,$@)
568 hex_rev: hex
569 $(V0) mv -f $(TARGET_HEX) $(TARGET_HEX_REV)
571 all_rev: $(addsuffix _rev,$(CI_TARGETS))
573 unbrick_$(TARGET): $(TARGET_HEX)
574 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
575 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
577 ## unbrick : unbrick flight controller
578 unbrick: unbrick_$(TARGET)
580 ## cppcheck : run static analysis on C source code
581 cppcheck: $(CSOURCES)
582 $(V0) $(CPPCHECK)
584 cppcheck-result.xml: $(CSOURCES)
585 $(V0) $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
587 # mkdirs
588 $(DL_DIR):
589 mkdir -p $@
591 $(TOOLS_DIR):
592 mkdir -p $@
594 ## version : print firmware version
595 version:
596 @echo $(FC_VER)
598 ## help : print this help message and exit
599 help: Makefile make/tools.mk
600 @echo ""
601 @echo "Makefile for the $(FORKNAME) firmware"
602 @echo ""
603 @echo "Usage:"
604 @echo " make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"]"
605 @echo "Or:"
606 @echo " make <target> [V=<verbosity>] [OPTIONS=\"<options>\"]"
607 @echo ""
608 @echo "Valid TARGET values are: $(VALID_TARGETS)"
609 @echo ""
610 @sed -n 's/^## //p' $?
612 ## targets : print a list of all valid target platforms (for consumption by scripts)
613 targets:
614 @echo "Valid targets: $(VALID_TARGETS)"
615 @echo "Built targets: $(CI_TARGETS)"
616 @echo "Unified targets: $(UNIFIED_TARGETS)"
617 @echo "Legacy targets: $(LEGACY_TARGETS)"
618 @echo "Unsupported targets: $(UNSUPPORTED_TARGETS)"
619 @echo "Default target: $(TARGET)"
620 @echo "targets-group-1: $(GROUP_1_TARGETS)"
621 @echo "targets-group-2: $(GROUP_2_TARGETS)"
622 @echo "targets-group-rest: $(GROUP_OTHER_TARGETS)"
624 @echo "targets-group-1: $(words $(GROUP_1_TARGETS)) targets"
625 @echo "targets-group-2: $(words $(GROUP_2_TARGETS)) targets"
626 @echo "targets-group-rest: $(words $(GROUP_OTHER_TARGETS)) targets"
627 @echo "total in all groups $(words $(CI_TARGETS)) targets"
629 targets-ci-print:
630 @echo $(CI_TARGETS)
632 ## target-mcu : print the MCU type of the target
633 target-mcu:
634 @echo $(TARGET_MCU)
636 ## targets-by-mcu : make all targets that have a MCU_TYPE mcu
637 targets-by-mcu:
638 $(V1) for target in $${TARGETS}; do \
639 TARGET_MCU_TYPE=$$($(MAKE) -s TARGET=$${target} target-mcu); \
640 if [ "$${TARGET_MCU_TYPE}" = "$${MCU_TYPE}" ]; then \
641 if [ "$${DO_BUILD}" = 1 ]; then \
642 echo "Building target $${target}..."; \
643 $(MAKE) TARGET=$${target}; \
644 if [ $$? -ne 0 ]; then \
645 echo "Building target $${target} failed, aborting."; \
646 exit 1; \
647 fi; \
648 else \
649 echo -n "$${target} "; \
650 fi; \
651 fi; \
652 done
653 @echo
655 ## targets-f4 : make all F4 targets
656 targets-f4:
657 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4 TARGETS="$(VALID_TARGETS)" DO_BUILD=1
659 targets-f4-print:
660 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4 TARGETS="$(VALID_TARGETS)"
662 targets-ci-f4-print:
663 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4 TARGETS="$(CI_TARGETS)"
665 ## targets-f7 : make all F7 targets
666 targets-f7:
667 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7 TARGETS="$(VALID_TARGETS)" DO_BUILD=1
669 targets-f7-print:
670 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7 TARGETS="$(VALID_TARGETS)"
672 targets-ci-f7-print:
673 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7 TARGETS="$(CI_TARGETS)"
675 ## test : run the Betaflight test suite
676 ## junittest : run the Betaflight test suite, producing Junit XML result files.
677 ## 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)
678 ## test-all: run the Betaflight test suite including all per-target expanded tests
679 test junittest test-all test-representative:
680 $(V0) cd src/test && $(MAKE) $@
682 ## test_help : print the help message for the test suite (including a list of the available tests)
683 test_help:
684 $(V0) cd src/test && $(MAKE) help
686 ## test_versions : print the compiler versions used for the test suite
687 test_versions:
688 $(V0) cd src/test && $(MAKE) versions
690 ## test_% : run test 'test_%' from the test suite
691 test_%:
692 $(V0) cd src/test && $(MAKE) $@
695 # rebuild everything when makefile changes
696 $(TARGET_OBJS): Makefile $(TARGET_DIR)/target.mk $(wildcard make/*)
698 # include auto-generated dependencies
699 -include $(TARGET_DEPS)