Fix flash_scan (#13789)
[betaflight.git] / Makefile
blobbab6e70c7435551c897f927be60a0c6dbbadf3e1
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. Only builds touched files.
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
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_EXCLUDED_TARGETS := $(sort $(notdir $(patsubst %/,%,$(dir $(wildcard $(ROOT)/src/main/target/*/.exclude)))))
131 CI_TARGETS := $(filter-out $(CI_EXCLUDED_TARGETS), $(BASE_TARGETS)) $(filter CRAZYBEEF4SX1280 CRAZYBEEF4FR IFLIGHT_BLITZ_F722 NUCLEOF446 SPRACINGH7EXTREME SPRACINGH7RF, $(BASE_CONFIGS))
132 include $(ROOT)/src/main/target/$(TARGET)/target.mk
134 REVISION := norevision
135 ifeq ($(shell git diff --shortstat),)
136 REVISION := $(shell git log -1 --format="%h")
137 endif
139 LD_FLAGS :=
140 EXTRA_LD_FLAGS :=
143 # Default Tool options - can be overridden in {mcu}.mk files.
145 DEBUG_MIXED = no
147 ifeq ($(DEBUG),INFO)
148 DEBUG_MIXED = yes
149 endif
150 ifeq ($(DEBUG),GDB)
151 DEBUG_MIXED = yes
152 endif
154 ifeq ($(DEBUG),GDB)
155 OPTIMISE_DEFAULT := -Og
157 LTO_FLAGS := $(OPTIMISE_DEFAULT)
158 DEBUG_FLAGS = -ggdb2 -gdwarf-5 -DDEBUG
159 else
160 ifeq ($(DEBUG),INFO)
161 DEBUG_FLAGS = -ggdb2
162 endif
163 OPTIMISATION_BASE := -flto=auto -fuse-linker-plugin -ffast-math -fmerge-all-constants
164 OPTIMISE_DEFAULT := -O2
165 OPTIMISE_SPEED := -Ofast
166 OPTIMISE_SIZE := -Os
168 LTO_FLAGS := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
169 endif
171 VPATH := $(VPATH):$(MAKE_SCRIPT_DIR)/mcu
172 VPATH := $(VPATH):$(MAKE_SCRIPT_DIR)
174 # start specific includes
175 ifeq ($(TARGET_MCU),)
176 $(error No TARGET_MCU specified. Is the target.mk valid for $(TARGET)?)
177 endif
179 ifeq ($(TARGET_MCU_FAMILY),)
180 $(error No TARGET_MCU_FAMILY specified. Is the target.mk valid for $(TARGET)?)
181 endif
183 TARGET_FLAGS := -D$(TARGET) -D$(TARGET_MCU_FAMILY) $(TARGET_FLAGS)
185 ifneq ($(CONFIG),)
186 TARGET_FLAGS := $(TARGET_FLAGS) -DUSE_CONFIG
187 endif
189 include $(MAKE_SCRIPT_DIR)/mcu/$(TARGET_MCU_FAMILY).mk
191 # openocd specific includes
192 include $(MAKE_SCRIPT_DIR)/openocd.mk
194 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
195 ifeq ($(TARGET_FLASH_SIZE),)
196 ifneq ($(MCU_FLASH_SIZE),)
197 TARGET_FLASH_SIZE := $(MCU_FLASH_SIZE)
198 else
199 $(error MCU_FLASH_SIZE not configured for target $(TARGET))
200 endif
201 endif
203 DEVICE_FLAGS := $(DEVICE_FLAGS) -DTARGET_FLASH_SIZE=$(TARGET_FLASH_SIZE)
205 ifneq ($(HSE_VALUE),)
206 DEVICE_FLAGS := $(DEVICE_FLAGS) -DHSE_VALUE=$(HSE_VALUE)
207 endif
209 TARGET_DIR = $(ROOT)/src/main/target/$(TARGET)
210 TARGET_DIR_SRC = $(notdir $(wildcard $(TARGET_DIR)/*.c))
212 .DEFAULT_GOAL := hex
214 INCLUDE_DIRS := $(INCLUDE_DIRS) \
215 $(ROOT)/lib/main/MAVLink
217 INCLUDE_DIRS := $(INCLUDE_DIRS) \
218 $(TARGET_DIR)
220 VPATH := $(VPATH):$(TARGET_DIR)
222 include $(MAKE_SCRIPT_DIR)/source.mk
224 ###############################################################################
225 # Things that might need changing to use different tools
228 # Find out if ccache is installed on the system
229 CCACHE := ccache
230 RESULT = $(shell (which $(CCACHE) > /dev/null 2>&1; echo $$?) )
231 ifneq ($(RESULT),0)
232 CCACHE :=
233 endif
235 # Tool names
236 CROSS_CC := $(CCACHE) $(ARM_SDK_PREFIX)gcc
237 CROSS_CXX := $(CCACHE) $(ARM_SDK_PREFIX)g++
238 CROSS_GDB := $(ARM_SDK_PREFIX)gdb
239 OBJCOPY := $(ARM_SDK_PREFIX)objcopy
240 OBJDUMP := $(ARM_SDK_PREFIX)objdump
241 READELF := $(ARM_SDK_PREFIX)readelf
242 SIZE := $(ARM_SDK_PREFIX)size
243 DFUSE-PACK := src/utils/dfuse-pack.py
246 # Tool options.
248 CC_DEBUG_OPTIMISATION := $(OPTIMISE_DEFAULT)
249 CC_DEFAULT_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_DEFAULT)
250 CC_SPEED_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
251 CC_SIZE_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SIZE)
252 CC_NO_OPTIMISATION :=
255 # Added after GCC version update, remove once the warnings have been fixed
257 TEMPORARY_FLAGS :=
259 EXTRA_WARNING_FLAGS := -Wold-style-definition
261 CFLAGS += $(ARCH_FLAGS) \
262 $(addprefix -D,$(OPTIONS)) \
263 $(addprefix -I,$(INCLUDE_DIRS)) \
264 $(DEBUG_FLAGS) \
265 -std=gnu17 \
266 -Wall -Wextra -Werror -Wpedantic -Wunsafe-loop-optimizations -Wdouble-promotion \
267 $(EXTRA_WARNING_FLAGS) \
268 -ffunction-sections \
269 -fdata-sections \
270 -fno-common \
271 $(TEMPORARY_FLAGS) \
272 $(DEVICE_FLAGS) \
273 -D_GNU_SOURCE \
274 -DUSE_STDPERIPH_DRIVER \
275 -D$(TARGET) \
276 $(TARGET_FLAGS) \
277 -D'__FORKNAME__="$(FORKNAME)"' \
278 -D'__TARGET__="$(TARGET)"' \
279 -D'__REVISION__="$(REVISION)"' \
280 $(CONFIG_REVISION_DEFINE) \
281 -pipe \
282 -MMD -MP \
283 $(EXTRA_FLAGS)
285 ASFLAGS = $(ARCH_FLAGS) \
286 $(DEBUG_FLAGS) \
287 -x assembler-with-cpp \
288 $(addprefix -I,$(INCLUDE_DIRS)) \
289 -MMD -MP
291 ifeq ($(LD_FLAGS),)
292 LD_FLAGS = -lm \
293 -nostartfiles \
294 --specs=nano.specs \
295 -lc \
296 -lnosys \
297 $(ARCH_FLAGS) \
298 $(LTO_FLAGS) \
299 $(DEBUG_FLAGS) \
300 -static \
301 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
302 -Wl,-L$(LINKER_DIR) \
303 -Wl,--cref \
304 -Wl,--no-wchar-size-warning \
305 -Wl,--print-memory-usage \
306 -T$(LD_SCRIPT) \
307 $(EXTRA_LD_FLAGS)
308 endif
310 ###############################################################################
311 # No user-serviceable parts below
312 ###############################################################################
314 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
315 --std=c99 --inline-suppr --quiet --force \
316 $(addprefix -I,$(INCLUDE_DIRS)) \
317 -I/usr/include -I/usr/include/linux
319 TARGET_NAME := $(TARGET)
321 ifneq ($(CONFIG),)
322 TARGET_NAME := $(TARGET_NAME)_$(CONFIG)
323 endif
325 ifeq ($(REV),yes)
326 TARGET_NAME := $(TARGET_NAME)_$(REVISION)
327 endif
329 TARGET_FULLNAME = $(FORKNAME)_$(FC_VER)_$(TARGET_NAME)
331 # Things we will build
333 TARGET_BIN = $(BIN_DIR)/$(TARGET_FULLNAME).bin
334 TARGET_HEX = $(BIN_DIR)/$(TARGET_FULLNAME).hex
335 TARGET_DFU = $(BIN_DIR)/$(TARGET_FULLNAME).dfu
336 TARGET_ZIP = $(BIN_DIR)/$(TARGET_FULLNAME).zip
337 TARGET_OBJ_DIR = $(OBJECT_DIR)/$(TARGET_NAME)
338 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).elf
339 TARGET_EXST_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME)_EXST.elf
340 TARGET_UNPATCHED_BIN = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME)_UNPATCHED.bin
341 TARGET_LST = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).lst
342 TARGET_OBJS = $(addsuffix .o,$(addprefix $(TARGET_OBJ_DIR)/,$(basename $(SRC))))
343 TARGET_DEPS = $(addsuffix .d,$(addprefix $(TARGET_OBJ_DIR)/,$(basename $(SRC))))
344 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).map
346 TARGET_EXST_HASH_SECTION_FILE = $(TARGET_OBJ_DIR)/exst_hash_section.bin
348 ifeq ($(DEBUG_MIXED),yes)
349 TARGET_EF_HASH := $(shell echo -n -- "$(EXTRA_FLAGS)" "$(OPTIONS)" "$(DEVICE_FLAGS)" "$(TARGET_FLAGS)" | openssl dgst -md5 -r | awk '{print $$1;}')
350 else
351 TARGET_EF_HASH := $(shell echo -n -- "$(EXTRA_FLAGS)" "$(OPTIONS)" "$(DEBUG_FLAGS)" "$(DEVICE_FLAGS)" "$(TARGET_FLAGS)" | openssl dgst -md5 -r | awk '{print $$1;}')
352 endif
354 TARGET_EF_HASH_FILE := $(TARGET_OBJ_DIR)/.efhash_$(TARGET_EF_HASH)
356 CLEAN_ARTIFACTS := $(TARGET_BIN)
357 CLEAN_ARTIFACTS += $(TARGET_HEX_REV) $(TARGET_HEX)
358 CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP)
359 CLEAN_ARTIFACTS += $(TARGET_LST)
360 CLEAN_ARTIFACTS += $(TARGET_DFU)
362 # Make sure build date and revision is updated on every incremental build
363 $(TARGET_OBJ_DIR)/build/version.o : $(SRC)
365 # List of buildable ELF files and their object dependencies.
366 # It would be nice to compute these lists, but that seems to be just beyond make.
368 $(TARGET_LST): $(TARGET_ELF)
369 $(V0) $(OBJDUMP) -S --disassemble $< > $@
371 ifeq ($(EXST),no)
372 $(TARGET_BIN): $(TARGET_ELF)
373 @echo "Creating BIN $(TARGET_BIN)" "$(STDOUT)"
374 $(V1) $(OBJCOPY) -O binary $< $@
376 $(TARGET_HEX): $(TARGET_ELF)
377 @echo "Creating HEX $(TARGET_HEX)" "$(STDOUT)"
378 $(V1) $(OBJCOPY) -O ihex --set-start 0x8000000 $< $@
380 $(TARGET_DFU): $(TARGET_HEX)
381 @echo "Creating DFU $(TARGET_DFU)" "$(STDOUT)"
382 $(V1) $(PYTHON) $(DFUSE-PACK) -i $< $@
384 else
385 CLEAN_ARTIFACTS += $(TARGET_UNPATCHED_BIN) $(TARGET_EXST_HASH_SECTION_FILE) $(TARGET_EXST_ELF)
387 $(TARGET_UNPATCHED_BIN): $(TARGET_ELF)
388 @echo "Creating BIN (without checksum) $(TARGET_UNPATCHED_BIN)" "$(STDOUT)"
389 $(V1) $(OBJCOPY) -O binary $< $@
391 $(TARGET_BIN): $(TARGET_UNPATCHED_BIN)
392 @echo "Creating EXST $(TARGET_BIN)" "$(STDOUT)"
393 # Linker script should allow .bin generation from a .elf which results in a file that is the same length as the FIRMWARE_SIZE.
394 # These 'dd' commands will pad a short binary to length FIRMWARE_SIZE.
395 $(V1) dd if=/dev/zero ibs=1k count=$(FIRMWARE_SIZE) of=$(TARGET_BIN)
396 $(V1) dd if=$(TARGET_UNPATCHED_BIN) of=$(TARGET_BIN) conv=notrunc
398 @echo "Generating MD5 hash of binary" "$(STDOUT)"
399 $(V1) openssl dgst -md5 $(TARGET_BIN) > $(TARGET_UNPATCHED_BIN).md5
401 @echo "Patching MD5 hash into binary" "$(STDOUT)"
402 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",(1024*$(FIRMWARE_SIZE))-16,$$2);}' | xxd -r - $(TARGET_BIN)
403 $(V1) echo $(FIRMWARE_SIZE) | awk '{printf("-s 0x%08x -l 16 -c 16 %s",(1024*$$1)-16,"$(TARGET_BIN)");}' | xargs xxd
405 # 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"
406 # Due to this a temporary file must be created and removed, even though we're only extracting data from the input file.
407 # If this temporary file is NOT used the $(TARGET_ELF) is modified, and running make a second time will result in
408 # a) regeneration of $(TARGET_BIN), and
409 # b) the results of $(TARGET_BIN) will not be as expected.
410 @echo "Extracting HASH section from unpatched EXST elf $(TARGET_ELF)" "$(STDOUT)"
411 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF).tmp --dump-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE) -j .exst_hash
412 rm $(TARGET_EXST_ELF).tmp
414 @echo "Patching MD5 hash into HASH section" "$(STDOUT)"
415 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",64-16,$$2);}' | xxd -r - $(TARGET_EXST_HASH_SECTION_FILE)
417 $(V1) @echo "Patching updated HASH section into $(TARGET_EXST_ELF)" "$(STDOUT)"
418 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF) --update-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE)
420 $(V1) $(READELF) -S $(TARGET_EXST_ELF)
421 $(V1) $(READELF) -l $(TARGET_EXST_ELF)
423 $(TARGET_HEX): $(TARGET_BIN)
424 $(if $(EXST_ADJUST_VMA),,$(error "EXST_ADJUST_VMA not specified"))
426 @echo "Creating EXST HEX from patched EXST BIN $(TARGET_BIN), VMA Adjust $(EXST_ADJUST_VMA)" "$(STDOUT)"
427 $(V1) $(OBJCOPY) -I binary -O ihex --adjust-vma=$(EXST_ADJUST_VMA) $(TARGET_BIN) $@
429 endif
431 $(TARGET_ELF): $(TARGET_OBJS) $(LD_SCRIPT) $(LD_SCRIPTS)
432 @echo "Linking $(TARGET_NAME)" "$(STDOUT)"
433 $(V1) $(CROSS_CC) -o $@ $(filter-out %.ld,$^) $(LD_FLAGS)
434 $(V1) $(SIZE) $(TARGET_ELF)
436 # Compile
438 ## compile_file takes two arguments: (1) optimisation description string and (2) optimisation compiler flag
439 define compile_file
440 echo "%% ($(1)) $<" "$(STDOUT)" && \
441 $(CROSS_CC) -c -o $@ $(CFLAGS) $(2) $<
442 endef
444 ifeq ($(DEBUG),GDB)
445 $(TARGET_OBJ_DIR)/%.o: %.c
446 $(V1) mkdir -p $(dir $@)
447 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
448 $(call compile_file,not optimised, $(CC_NO_OPTIMISATION)) \
450 $(call compile_file,debug,$(CC_DEBUG_OPTIMISATION)) \
452 else
453 $(TARGET_OBJ_DIR)/%.o: %.c
454 $(V1) mkdir -p $(dir $@)
455 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
456 $(call compile_file,not optimised,$(CC_NO_OPTIMISATION)) \
458 $(if $(findstring $(subst ./src/main/,,$<),$(SPEED_OPTIMISED_SRC)), \
459 $(call compile_file,speed optimised,$(CC_SPEED_OPTIMISATION)) \
461 $(if $(findstring $(subst ./src/main/,,$<),$(SIZE_OPTIMISED_SRC)), \
462 $(call compile_file,size optimised,$(CC_SIZE_OPTIMISATION)) \
464 $(call compile_file,optimised,$(CC_DEFAULT_OPTIMISATION)) \
468 endif
470 # Assemble
471 $(TARGET_OBJ_DIR)/%.o: %.s
472 $(V1) mkdir -p $(dir $@)
473 @echo "%% $(notdir $<)" "$(STDOUT)"
474 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
476 $(TARGET_OBJ_DIR)/%.o: %.S
477 $(V1) mkdir -p $(dir $@)
478 @echo "%% $(notdir $<)" "$(STDOUT)"
479 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
482 ## all : Build all currently built targets
483 all: $(CI_TARGETS)
485 $(BASE_TARGETS):
486 $(V0) @echo "Building target $@" && \
487 $(MAKE) hex TARGET=$@ && \
488 echo "Building $@ succeeded."
490 TARGETS_CLEAN = $(addsuffix _clean,$(BASE_TARGETS))
492 CONFIGS_CLEAN = $(addsuffix _clean,$(BASE_CONFIGS))
494 ## clean : clean up temporary / machine-generated files
495 clean:
496 @echo "Cleaning $(TARGET_NAME)"
497 $(V0) rm -f $(CLEAN_ARTIFACTS)
498 $(V0) rm -rf $(TARGET_OBJ_DIR)
499 @echo "Cleaning $(TARGET_NAME) succeeded."
501 ## test_clean : clean up temporary / machine-generated files (tests)
502 test-%_clean:
503 $(MAKE) test_clean
505 test_clean:
506 $(V0) cd src/test && $(MAKE) clean || true
508 ## <TARGET>_clean : clean up one specific target (alias for above)
509 $(TARGETS_CLEAN):
510 $(V0) $(MAKE) -j TARGET=$(subst _clean,,$@) clean
512 ## <CONFIG>_clean : clean up one specific target (alias for above)
513 $(CONFIGS_CLEAN):
514 $(V0) $(MAKE) -j CONFIG=$(subst _clean,,$@) clean
516 ## clean_all : clean all targets
517 clean_all: $(TARGETS_CLEAN) test_clean
519 ## configs : Hydrate configuration
520 configs: configs
522 ## all_configs : Build all configs
523 all_configs: $(BASE_CONFIGS)
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 all_rev: $(addsuffix _rev,$(CI_TARGETS))
586 unbrick_$(TARGET): $(TARGET_HEX)
587 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
588 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
590 ## unbrick : unbrick flight controller
591 unbrick: unbrick_$(TARGET)
593 ## cppcheck : run static analysis on C source code
594 cppcheck: $(CSOURCES)
595 $(V0) $(CPPCHECK)
597 cppcheck-result.xml: $(CSOURCES)
598 $(V0) $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
600 # mkdirs
601 $(DIRECTORIES):
602 mkdir -p $@
604 ## version : print firmware version
605 version:
606 @echo $(FC_VER)
608 ## help : print this help message and exit
609 help: Makefile mk/tools.mk
610 @echo ""
611 @echo "Makefile for the $(FORKNAME) firmware"
612 @echo ""
613 @echo "Usage:"
614 @echo " make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"] [EXTRA_FLAGS=\"<extra_flags>\"]"
615 @echo "Or:"
616 @echo " make <target> [V=<verbosity>] [OPTIONS=\"<options>\"] [EXTRA_FLAGS=\"<extra_flags>\"]"
617 @echo "Or:"
618 @echo " make <config-target> [V=<verbosity>] [OPTIONS=\"<options>\"] [EXTRA_FLAGS=\"<extra_flags>\"]"
619 @echo ""
620 @echo "To pupulate configuration targets:"
621 @echo " make configs"
622 @echo ""
623 @echo "Valid TARGET values are: $(BASE_TARGETS)"
624 @echo ""
625 @sed -n 's/^## //p' $?
627 ## targets : print a list of all valid target platforms (for consumption by scripts)
628 targets:
629 @echo "Valid targets: $(BASE_TARGETS)"
630 @echo "Built targets: $(CI_TARGETS)"
631 @echo "Default target: $(TARGET)"
633 targets-ci-print:
634 @echo $(CI_TARGETS)
636 ## target-mcu : print the MCU type of the target
637 target-mcu:
638 @echo "$(TARGET_MCU_FAMILY) : $(TARGET_MCU)"
640 ## targets-by-mcu : make all targets that have a MCU_TYPE mcu
641 targets-by-mcu:
642 $(V1) for target in $${TARGETS}; do \
643 TARGET_MCU_TYPE=$$($(MAKE) -s TARGET=$${target} target-mcu); \
644 if [ "$${TARGET_MCU_TYPE}" = "$${MCU_TYPE}" ]; then \
645 if [ "$${DO_BUILD}" = 1 ]; then \
646 echo "Building target $${target}..."; \
647 $(MAKE) TARGET=$${target}; \
648 if [ $$? -ne 0 ]; then \
649 echo "Building target $${target} failed, aborting."; \
650 exit 1; \
651 fi; \
652 else \
653 echo -n "$${target} "; \
654 fi; \
655 fi; \
656 done
657 @echo
659 ## test : run the Betaflight test suite
660 ## junittest : run the Betaflight test suite, producing Junit XML result files.
661 ## 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)
662 ## test-all: run the Betaflight test suite including all per-target expanded tests
663 test junittest test-all test-representative:
664 $(V0) cd src/test && $(MAKE) $@
666 ## test_help : print the help message for the test suite (including a list of the available tests)
667 test_help:
668 $(V0) cd src/test && $(MAKE) help
670 ## test_versions : print the compiler versions used for the test suite
671 test_versions:
672 $(V0) cd src/test && $(MAKE) versions
674 ## test_% : run test 'test_%' from the test suite
675 test_%:
676 $(V0) cd src/test && $(MAKE) $@
678 $(TARGET_EF_HASH_FILE):
679 $(V1) mkdir -p $(dir $@)
680 $(V1) rm -f $(TARGET_OBJ_DIR)/.efhash_*
681 @echo "EF HASH -> $(TARGET_EF_HASH_FILE)"
682 $(V1) touch $(TARGET_EF_HASH_FILE)
684 # rebuild everything when makefile changes or the extra flags have changed
685 $(TARGET_OBJS): $(TARGET_EF_HASH_FILE) Makefile $(TARGET_DIR)/target.mk $(wildcard make/*) $(CONFIG_FILE)
687 # include auto-generated dependencies
688 -include $(TARGET_DEPS)