Added SPARKY2 target (#544)
[betaflight.git] / Makefile
blob2ae9aa1c22c80506f688a67f8efb5e3bc6dd4d69
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 iNav 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 ?= NAZE
21 # Compile-time options
22 OPTIONS ?=
24 # compile for OpenPilot BootLoader support
25 OPBL ?= no
27 # Debugger optons, must be empty or GDB
28 DEBUG ?=
30 # Insert the debugging hardfault debugger
31 # releases should not be built with this flag as it does not disable pwm output
32 DEBUG_HARDFAULTS ?=
34 # Serial port/Device for flashing
35 SERIAL_DEVICE ?= $(firstword $(wildcard /dev/ttyUSB*) no-port-found)
37 # Flash size (KB). Some low-end chips actually have more flash than advertised, use this to override.
38 FLASH_SIZE ?=
40 ###############################################################################
41 # Things that need to be maintained as the source changes
44 FORKNAME = inav
46 # Working directories
47 ROOT := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
48 SRC_DIR = $(ROOT)/src/main
49 OBJECT_DIR = $(ROOT)/obj/main
50 BIN_DIR = $(ROOT)/obj
51 CMSIS_DIR = $(ROOT)/lib/main/CMSIS
52 INCLUDE_DIRS = $(SRC_DIR) \
53 $(ROOT)/src/main/target
54 LINKER_DIR = $(ROOT)/src/main/target
56 # default xtal value for F4 targets
57 HSE_VALUE = 8000000
59 # used for turning on features like VCP and SDCARD
60 FEATURES =
62 ALT_TARGETS = $(sort $(filter-out target, $(basename $(notdir $(wildcard $(ROOT)/src/main/target/*/*.mk)))))
63 OPBL_TARGETS = $(filter %_OPBL, $(ALT_TARGETS))
65 #VALID_TARGETS = $(F1_TARGETS) $(F3_TARGETS) $(F4_TARGETS)
66 VALID_TARGETS = $(dir $(wildcard $(ROOT)/src/main/target/*/target.mk))
67 VALID_TARGETS := $(subst /,, $(subst ./src/main/target/,, $(VALID_TARGETS)))
68 VALID_TARGETS := $(VALID_TARGETS) $(ALT_TARGETS)
69 VALID_TARGETS := $(sort $(VALID_TARGETS))
71 ifeq ($(filter $(TARGET),$(ALT_TARGETS)), $(TARGET))
72 BASE_TARGET := $(firstword $(subst /,, $(subst ./src/main/target/,, $(dir $(wildcard $(ROOT)/src/main/target/*/$(TARGET).mk)))))
73 -include $(ROOT)/src/main/target/$(BASE_TARGET)/$(TARGET).mk
74 else
75 BASE_TARGET := $(TARGET)
76 endif
78 ifeq ($(filter $(TARGET),$(OPBL_TARGETS)), $(TARGET))
79 OPBL = yes
80 endif
82 # silently ignore if the file is not present. Allows for target specific.
83 -include $(ROOT)/src/main/target/$(BASE_TARGET)/target.mk
85 F4_TARGETS = $(F405_TARGETS) $(F411_TARGETS)
87 ifeq ($(filter $(TARGET),$(VALID_TARGETS)),)
88 $(error Target '$(TARGET)' is not valid, must be one of $(VALID_TARGETS). Have you prepared a valid target.mk?)
89 endif
91 ifeq ($(filter $(TARGET),$(F1_TARGETS) $(F3_TARGETS) $(F4_TARGETS)),)
92 $(error Target '$(TARGET)' has not specified a valid STM group, must be one of F1, F3, F405, or F411. Have you prepared a valid target.mk?)
93 endif
95 128K_TARGETS = $(F1_TARGETS)
96 256K_TARGETS = $(F3_TARGETS)
97 512K_TARGETS = $(F411_TARGETS)
98 1024K_TARGETS = $(F405_TARGETS)
100 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
101 ifeq ($(FLASH_SIZE),)
102 ifeq ($(TARGET),$(filter $(TARGET),$(1024K_TARGETS)))
103 FLASH_SIZE = 1024
104 else ifeq ($(TARGET),$(filter $(TARGET),$(512K_TARGETS)))
105 FLASH_SIZE = 512
106 else ifeq ($(TARGET),$(filter $(TARGET),$(256K_TARGETS)))
107 FLASH_SIZE = 256
108 else ifeq ($(TARGET),$(filter $(TARGET),$(128K_TARGETS)))
109 FLASH_SIZE = 128
110 else
111 $(error FLASH_SIZE not configured for target $(TARGET))
112 endif
113 endif
115 # note that there is no hardfault debugging startup file assembly handler for other platforms
116 ifeq ($(DEBUG_HARDFAULTS),F3)
117 CFLAGS += -DDEBUG_HARDFAULTS
118 STM32F30x_COMMON_SRC = startup_stm32f3_debug_hardfault_handler.S
119 else
120 STM32F30x_COMMON_SRC = startup_stm32f30x_md_gcc.S
121 endif
123 REVISION = $(shell git log -1 --format="%h")
125 FC_VER_MAJOR := $(shell grep " FC_VERSION_MAJOR" src/main/build/version.h | awk '{print $$3}' )
126 FC_VER_MINOR := $(shell grep " FC_VERSION_MINOR" src/main/build/version.h | awk '{print $$3}' )
127 FC_VER_PATCH := $(shell grep " FC_VERSION_PATCH" src/main/build/version.h | awk '{print $$3}' )
129 FC_VER := $(FC_VER_MAJOR).$(FC_VER_MINOR).$(FC_VER_PATCH)
131 # Search path for sources
132 VPATH := $(SRC_DIR):$(SRC_DIR)/startup
133 USBFS_DIR = $(ROOT)/lib/main/STM32_USB-FS-Device_Driver
134 USBPERIPH_SRC = $(notdir $(wildcard $(USBFS_DIR)/src/*.c))
135 FATFS_DIR = $(ROOT)/lib/main/FatFS
136 FATFS_SRC = $(notdir $(wildcard $(FATFS_DIR)/*.c))
138 CSOURCES := $(shell find $(SRC_DIR) -name '*.c')
140 ifeq ($(TARGET),$(filter $(TARGET),$(F3_TARGETS)))
141 # F3 TARGETS
143 STDPERIPH_DIR = $(ROOT)/lib/main/STM32F30x_StdPeriph_Driver
144 STDPERIPH_SRC = $(notdir $(wildcard $(STDPERIPH_DIR)/src/*.c))
145 EXCLUDES = stm32f30x_crc.c \
146 stm32f30x_can.c
148 STDPERIPH_SRC := $(filter-out ${EXCLUDES}, $(STDPERIPH_SRC))
149 DEVICE_STDPERIPH_SRC = $(STDPERIPH_SRC)
151 VPATH := $(VPATH):$(CMSIS_DIR)/CM1/CoreSupport:$(CMSIS_DIR)/CM1/DeviceSupport/ST/STM32F30x
152 CMSIS_SRC = $(notdir $(wildcard $(CMSIS_DIR)/CM1/CoreSupport/*.c \
153 $(CMSIS_DIR)/CM1/DeviceSupport/ST/STM32F30x/*.c))
155 INCLUDE_DIRS := $(INCLUDE_DIRS) \
156 $(STDPERIPH_DIR)/inc \
157 $(CMSIS_DIR)/CM1/CoreSupport \
158 $(CMSIS_DIR)/CM1/DeviceSupport/ST/STM32F30x
160 ifneq ($(filter VCP, $(FEATURES)),)
161 INCLUDE_DIRS := $(INCLUDE_DIRS) \
162 $(USBFS_DIR)/inc \
163 $(ROOT)/src/main/vcp
165 VPATH := $(VPATH):$(USBFS_DIR)/src
167 DEVICE_STDPERIPH_SRC := $(DEVICE_STDPERIPH_SRC)\
168 $(USBPERIPH_SRC)
169 endif
171 ifneq ($(filter SDCARD, $(FEATURES)),)
172 INCLUDE_DIRS := $(INCLUDE_DIRS) \
173 $(FATFS_DIR) \
175 VPATH := $(VPATH):$(FATFS_DIR)
176 endif
178 LD_SCRIPT = $(LINKER_DIR)/stm32_flash_f303_$(FLASH_SIZE)k.ld
180 ARCH_FLAGS = -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fsingle-precision-constant -Wdouble-promotion
181 DEVICE_FLAGS = -DSTM32F303xC -DSTM32F303
182 TARGET_FLAGS = -D$(TARGET)
183 # End F3 targets
185 # Start F4 targets
186 else ifeq ($(TARGET),$(filter $(TARGET), $(F4_TARGETS)))
188 #STDPERIPH
189 STDPERIPH_DIR = $(ROOT)/lib/main/STM32F4xx_StdPeriph_Driver
190 STDPERIPH_SRC = $(notdir $(wildcard $(STDPERIPH_DIR)/src/*.c))
191 EXCLUDES = stm32f4xx_crc.c \
192 stm32f4xx_can.c \
193 stm32f4xx_fmc.c \
194 stm32f4xx_sai.c \
195 stm32f4xx_cec.c \
196 stm32f4xx_dsi.c \
197 stm32f4xx_flash_ramfunc.c \
198 stm32f4xx_fmpi2c.c \
199 stm32f4xx_lptim.c \
200 stm32f4xx_qspi.c \
201 stm32f4xx_spdifrx.c \
202 stm32f4xx_cryp.c \
203 stm32f4xx_cryp_aes.c \
204 stm32f4xx_hash_md5.c \
205 stm32f4xx_cryp_des.c \
206 stm32f4xx_rtc.c \
207 stm32f4xx_hash.c \
208 stm32f4xx_dbgmcu.c \
209 stm32f4xx_cryp_tdes.c \
210 stm32f4xx_hash_sha1.c
213 ifeq ($(TARGET),$(filter $(TARGET), $(F411_TARGETS)))
214 EXCLUDES += stm32f4xx_fsmc.c
215 endif
217 STDPERIPH_SRC := $(filter-out ${EXCLUDES}, $(STDPERIPH_SRC))
219 #USB
220 USBCORE_DIR = $(ROOT)/lib/main/STM32_USB_Device_Library/Core
221 USBCORE_SRC = $(notdir $(wildcard $(USBCORE_DIR)/src/*.c))
222 USBOTG_DIR = $(ROOT)/lib/main/STM32_USB_OTG_Driver
223 USBOTG_SRC = $(notdir $(wildcard $(USBOTG_DIR)/src/*.c))
224 EXCLUDES = usb_bsp_template.c \
225 usb_conf_template.c \
226 usb_hcd_int.c \
227 usb_hcd.c \
228 usb_otg.c
230 USBOTG_SRC := $(filter-out ${EXCLUDES}, $(USBOTG_SRC))
231 USBCDC_DIR = $(ROOT)/lib/main/STM32_USB_Device_Library/Class/cdc
232 USBCDC_SRC = $(notdir $(wildcard $(USBCDC_DIR)/src/*.c))
233 EXCLUDES = usbd_cdc_if_template.c
234 USBCDC_SRC := $(filter-out ${EXCLUDES}, $(USBCDC_SRC))
235 VPATH := $(VPATH):$(USBOTG_DIR)/src:$(USBCORE_DIR)/src:$(USBCDC_DIR)/src
237 DEVICE_STDPERIPH_SRC := $(STDPERIPH_SRC) \
238 $(USBOTG_SRC) \
239 $(USBCORE_SRC) \
240 $(USBCDC_SRC)
242 #CMSIS
243 VPATH := $(VPATH):$(CMSIS_DIR)/CM4/CoreSupport:$(CMSIS_DIR)/CM4/DeviceSupport/ST/STM32F4xx
244 CMSIS_SRC = $(notdir $(wildcard $(CMSIS_DIR)/CM4/CoreSupport/*.c \
245 $(CMSIS_DIR)/CM4/DeviceSupport/ST/STM32F4xx/*.c))
246 INCLUDE_DIRS := $(INCLUDE_DIRS) \
247 $(STDPERIPH_DIR)/inc \
248 $(USBOTG_DIR)/inc \
249 $(USBCORE_DIR)/inc \
250 $(USBCDC_DIR)/inc \
251 $(USBFS_DIR)/inc \
252 $(CMSIS_DIR)/CM4/CoreSupport \
253 $(CMSIS_DIR)/CM4/DeviceSupport/ST/STM32F4xx \
254 $(ROOT)/src/main/vcpf4
256 ifneq ($(filter SDCARD,$(FEATURES)),)
257 INCLUDE_DIRS := $(INCLUDE_DIRS) \
258 $(FATFS_DIR)
259 VPATH := $(VPATH):$(FATFS_DIR)
260 endif
262 #Flags
263 ARCH_FLAGS = -mthumb -mcpu=cortex-m4 -march=armv7e-m -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fsingle-precision-constant -Wdouble-promotion
265 ifeq ($(TARGET),$(filter $(TARGET),$(F411_TARGETS)))
266 DEVICE_FLAGS = -DSTM32F411xE
267 LD_SCRIPT = $(LINKER_DIR)/stm32_flash_f411.ld
268 else ifeq ($(TARGET),$(filter $(TARGET),$(F405_TARGETS)))
269 DEVICE_FLAGS = -DSTM32F40_41xxx
270 LD_SCRIPT = $(LINKER_DIR)/stm32_flash_f405.ld
271 else
272 $(error Unknown MCU for F4 target)
273 endif
274 DEVICE_FLAGS += -DHSE_VALUE=$(HSE_VALUE)
276 TARGET_FLAGS = -D$(TARGET)
277 # End F4 targets
279 # Start F1 targets
280 else
282 STDPERIPH_DIR = $(ROOT)/lib/main/STM32F10x_StdPeriph_Driver
283 STDPERIPH_SRC = $(notdir $(wildcard $(STDPERIPH_DIR)/src/*.c))
284 EXCLUDES = stm32f10x_crc.c \
285 stm32f10x_cec.c \
286 stm32f10x_can.c
288 STDPERIPH_SRC := $(filter-out ${EXCLUDES}, $(STDPERIPH_SRC))
290 # Search path and source files for the CMSIS sources
291 VPATH := $(VPATH):$(CMSIS_DIR)/CM3/CoreSupport:$(CMSIS_DIR)/CM3/DeviceSupport/ST/STM32F10x
292 CMSIS_SRC = $(notdir $(wildcard $(CMSIS_DIR)/CM3/CoreSupport/*.c \
293 $(CMSIS_DIR)/CM3/DeviceSupport/ST/STM32F10x/*.c))
295 INCLUDE_DIRS := $(INCLUDE_DIRS) \
296 $(STDPERIPH_DIR)/inc \
297 $(CMSIS_DIR)/CM3/CoreSupport \
298 $(CMSIS_DIR)/CM3/DeviceSupport/ST/STM32F10x \
300 DEVICE_STDPERIPH_SRC = $(STDPERIPH_SRC)
302 ifneq ($(filter VCP, $(FEATURES)),)
303 INCLUDE_DIRS := $(INCLUDE_DIRS) \
304 $(USBFS_DIR)/inc \
305 $(ROOT)/src/main/vcp
307 VPATH := $(VPATH):$(USBFS_DIR)/src
309 DEVICE_STDPERIPH_SRC := $(DEVICE_STDPERIPH_SRC) \
310 $(USBPERIPH_SRC)
312 endif
314 LD_SCRIPT = $(LINKER_DIR)/stm32_flash_f103_$(FLASH_SIZE)k.ld
315 ARCH_FLAGS = -mthumb -mcpu=cortex-m3
316 TARGET_FLAGS := -D$(TARGET) -pedantic $(TARGET_FLAGS)
318 ifeq ($(DEVICE_FLAGS),)
319 DEVICE_FLAGS = -DSTM32F10X_MD
320 endif
321 DEVICE_FLAGS += -DSTM32F10X
323 endif
325 # End F1 targets
327 ifneq ($(BASE_TARGET), $(TARGET))
328 TARGET_FLAGS := $(TARGET_FLAGS) -D$(BASE_TARGET)
329 endif
331 ifneq ($(FLASH_SIZE),)
332 DEVICE_FLAGS := $(DEVICE_FLAGS) -DFLASH_SIZE=$(FLASH_SIZE)
333 endif
335 TARGET_DIR = $(ROOT)/src/main/target/$(BASE_TARGET)
336 TARGET_DIR_SRC = $(notdir $(wildcard $(TARGET_DIR)/*.c))
338 ifeq ($(OPBL),yes)
339 TARGET_FLAGS := -DOPBL $(TARGET_FLAGS)
340 ifeq ($(TARGET), $(filter $(TARGET),$(F405_TARGETS)))
341 LD_SCRIPT = $(LINKER_DIR)/stm32_flash_f405_opbl.ld
342 else ifeq ($(TARGET), $(filter $(TARGET),$(F411_TARGETS)))
343 LD_SCRIPT = $(LINKER_DIR)/stm32_flash_f411_opbl.ld
344 else ifeq ($(TARGET), $(filter $(TARGET),$(F3_TARGETS)))
345 LD_SCRIPT = $(LINKER_DIR)/stm32_flash_f303_$(FLASH_SIZE)k_opbl.ld
346 else ifeq ($(TARGET), $(filter $(TARGET),$(F1_TARGETS)))
347 LD_SCRIPT = $(LINKER_DIR)/stm32_flash_f103_$(FLASH_SIZE)k_opbl.ld
348 endif
349 .DEFAULT_GOAL := binary
350 else
351 .DEFAULT_GOAL := hex
352 endif
354 INCLUDE_DIRS := $(INCLUDE_DIRS) \
355 $(TARGET_DIR)
357 VPATH := $(VPATH):$(TARGET_DIR)
359 COMMON_SRC = \
360 build/build_config.c \
361 build/debug.c \
362 build/version.c \
363 build/assert.c \
364 $(TARGET_DIR_SRC) \
365 main.c \
366 fc/mw.c \
367 common/encoding.c \
368 common/filter.c \
369 common/maths.c \
370 common/printf.c \
371 common/typeconversion.c \
372 common/streambuf.c \
373 config/config.c \
374 fc/runtime_config.c \
375 drivers/logging.c \
376 drivers/adc.c \
377 drivers/buf_writer.c \
378 drivers/bus_i2c_soft.c \
379 drivers/bus_spi.c \
380 drivers/bus_spi_soft.c \
381 drivers/exti.c \
382 drivers/gps_i2cnav.c \
383 drivers/gyro_sync.c \
384 drivers/io.c \
385 drivers/light_led.c \
386 drivers/rx_nrf24l01.c \
387 drivers/rx_xn297.c \
388 drivers/pwm_mapping.c \
389 drivers/pwm_output.c \
390 drivers/pwm_rx.c \
391 drivers/rcc.c \
392 drivers/serial.c \
393 drivers/serial_uart.c \
394 drivers/sound_beeper.c \
395 drivers/system.c \
396 drivers/timer.c \
397 flight/failsafe.c \
398 flight/imu.c \
399 flight/hil.c \
400 flight/mixer.c \
401 flight/pid.c \
402 io/beeper.c \
403 fc/rc_controls.c \
404 fc/rc_curves.c \
405 io/serial.c \
406 io/serial_4way.c \
407 io/serial_4way_avrootloader.c \
408 io/serial_4way_stk500v2.c \
409 io/serial_cli.c \
410 io/serial_msp.c \
411 io/statusindicator.c \
412 rx/ibus.c \
413 rx/jetiexbus.c \
414 rx/msp.c \
415 rx/nrf24.c \
416 rx/nrf24_cx10.c \
417 rx/nrf24_inav.c \
418 rx/nrf24_h8_3d.c \
419 rx/nrf24_syma.c \
420 rx/nrf24_v202.c \
421 rx/pwm.c \
422 rx/rx.c \
423 rx/sbus.c \
424 rx/spektrum.c \
425 rx/sumd.c \
426 rx/sumh.c \
427 rx/xbus.c \
428 scheduler/scheduler.c \
429 scheduler/scheduler_tasks.c \
430 sensors/acceleration.c \
431 sensors/battery.c \
432 sensors/boardalignment.c \
433 sensors/compass.c \
434 sensors/gyro.c \
435 sensors/initialisation.c \
436 $(CMSIS_SRC) \
437 $(DEVICE_STDPERIPH_SRC)
439 HIGHEND_SRC = \
440 blackbox/blackbox.c \
441 blackbox/blackbox_io.c \
442 common/colorconversion.c \
443 drivers/display_ug2864hsweg01.c \
444 drivers/sonar_hcsr04.c \
445 drivers/sonar_srf10.c \
446 flight/navigation_rewrite.c \
447 flight/navigation_rewrite_multicopter.c \
448 flight/navigation_rewrite_fixedwing.c \
449 flight/navigation_rewrite_pos_estimator.c \
450 flight/navigation_rewrite_geo.c \
451 flight/gps_conversion.c \
452 io/gps.c \
453 io/gps_ublox.c \
454 io/gps_nmea.c \
455 io/gps_naza.c \
456 io/gps_i2cnav.c \
457 io/ledstrip.c \
458 io/display.c \
459 sensors/rangefinder.c \
460 sensors/barometer.c \
461 telemetry/telemetry.c \
462 telemetry/frsky.c \
463 telemetry/hott.c \
464 telemetry/smartport.c \
465 telemetry/mavlink.c \
466 telemetry/ltm.c
468 ifeq ($(TARGET),$(filter $(TARGET),$(F4_TARGETS)))
469 VCP_SRC = \
470 vcpf4/stm32f4xx_it.c \
471 vcpf4/usb_bsp.c \
472 vcpf4/usbd_desc.c \
473 vcpf4/usbd_usr.c \
474 vcpf4/usbd_cdc_vcp.c \
475 drivers/serial_usb_vcp.c
476 else
477 VCP_SRC = \
478 vcp/hw_config.c \
479 vcp/stm32_it.c \
480 vcp/usb_desc.c \
481 vcp/usb_endp.c \
482 vcp/usb_istr.c \
483 vcp/usb_prop.c \
484 vcp/usb_pwr.c \
485 drivers/serial_usb_vcp.c \
486 drivers/usb_io.c
487 endif
489 STM32F10x_COMMON_SRC = \
490 startup_stm32f10x_md_gcc.S \
491 drivers/adc_stm32f10x.c \
492 drivers/bus_i2c_stm32f10x.c \
493 drivers/dma.c \
494 drivers/gpio_stm32f10x.c \
495 drivers/inverter.c \
496 drivers/serial_softserial.c \
497 drivers/serial_uart_stm32f10x.c \
498 drivers/system_stm32f10x.c \
499 drivers/timer_stm32f10x.c
501 STM32F30x_COMMON_SRC = \
502 startup_stm32f30x_md_gcc.S \
503 target/system_stm32f30x.c \
504 drivers/adc_stm32f30x.c \
505 drivers/bus_i2c_stm32f30x.c \
506 drivers/dma.c \
507 drivers/gpio_stm32f30x.c \
508 drivers/light_ws2811strip_stm32f30x.c \
509 drivers/serial_uart_stm32f30x.c \
510 drivers/system_stm32f30x.c \
511 drivers/timer_stm32f30x.c
513 STM32F4xx_COMMON_SRC = \
514 startup_stm32f40xx.s \
515 target/system_stm32f4xx.c \
516 drivers/accgyro_mpu.c \
517 drivers/adc_stm32f4xx.c \
518 drivers/adc_stm32f4xx.c \
519 drivers/bus_i2c_stm32f10x.c \
520 drivers/gpio_stm32f4xx.c \
521 drivers/inverter.c \
522 drivers/serial_softserial.c \
523 drivers/serial_uart_stm32f4xx.c \
524 drivers/system_stm32f4xx.c \
525 drivers/timer_stm32f4xx.c \
526 drivers/dma_stm32f4xx.c
528 # check if target.mk supplied
529 ifeq ($(TARGET),$(filter $(TARGET),$(F4_TARGETS)))
530 TARGET_SRC := $(STM32F4xx_COMMON_SRC) $(TARGET_SRC)
531 else ifeq ($(TARGET),$(filter $(TARGET),$(F3_TARGETS)))
532 TARGET_SRC := $(STM32F30x_COMMON_SRC) $(TARGET_SRC)
533 else ifeq ($(TARGET),$(filter $(TARGET),$(F1_TARGETS)))
534 TARGET_SRC := $(STM32F10x_COMMON_SRC) $(TARGET_SRC)
535 endif
537 ifneq ($(filter ONBOARDFLASH,$(FEATURES)),)
538 TARGET_SRC += \
539 drivers/flash_m25p16.c \
540 io/flashfs.c
541 endif
543 ifeq ($(TARGET),$(filter $(TARGET),$(F4_TARGETS) $(F3_TARGETS)))
544 TARGET_SRC += $(HIGHEND_SRC)
545 else ifneq ($(filter HIGHEND,$(FEATURES)),)
546 TARGET_SRC += $(HIGHEND_SRC)
547 endif
549 TARGET_SRC += $(COMMON_SRC)
551 ifneq ($(filter SDCARD,$(FEATURES)),)
552 TARGET_SRC += \
553 drivers/sdcard.c \
554 drivers/sdcard_standard.c \
555 io/asyncfatfs/asyncfatfs.c \
556 io/asyncfatfs/fat_standard.c
557 endif
559 ifneq ($(filter VCP,$(FEATURES)),)
560 TARGET_SRC += $(VCP_SRC)
561 endif
562 # end target specific make file checks
565 # Search path and source files for the ST stdperiph library
566 VPATH := $(VPATH):$(STDPERIPH_DIR)/src
568 ###############################################################################
569 # Things that might need changing to use different tools
572 # Tool names
573 CC = arm-none-eabi-gcc
574 OBJCOPY = arm-none-eabi-objcopy
575 SIZE = arm-none-eabi-size
578 # Tool options.
581 ifeq ($(DEBUG),GDB)
582 OPTIMIZE = -O0
583 LTO_FLAGS = $(OPTIMIZE)
584 else
585 OPTIMIZE = -Os
586 LTO_FLAGS = -flto -fuse-linker-plugin $(OPTIMIZE)
587 endif
589 DEBUG_FLAGS = -ggdb3 -DDEBUG
591 CFLAGS += $(ARCH_FLAGS) \
592 $(LTO_FLAGS) \
593 $(addprefix -D,$(OPTIONS)) \
594 $(addprefix -I,$(INCLUDE_DIRS)) \
595 $(DEBUG_FLAGS) \
596 -std=gnu99 \
597 -Wall -Wextra -Wunsafe-loop-optimizations -Wdouble-promotion \
598 -ffunction-sections \
599 -fdata-sections \
600 $(DEVICE_FLAGS) \
601 -DUSE_STDPERIPH_DRIVER \
602 $(TARGET_FLAGS) \
603 -D'__FORKNAME__="$(FORKNAME)"' \
604 -D'__TARGET__="$(TARGET)"' \
605 -D'__REVISION__="$(REVISION)"' \
606 -save-temps=obj \
607 -MMD -MP
609 ASFLAGS = $(ARCH_FLAGS) \
610 -x assembler-with-cpp \
611 $(addprefix -I,$(INCLUDE_DIRS)) \
612 -MMD -MP
614 LDFLAGS = -lm \
615 -nostartfiles \
616 --specs=nano.specs \
617 -lc \
618 -lnosys \
619 $(ARCH_FLAGS) \
620 $(LTO_FLAGS) \
621 $(DEBUG_FLAGS) \
622 -static \
623 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
624 -Wl,-L$(LINKER_DIR) \
625 -Wl,--cref \
626 -Wl,--no-wchar-size-warning \
627 -T$(LD_SCRIPT)
629 ###############################################################################
630 # No user-serviceable parts below
631 ###############################################################################
633 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
634 --std=c99 --inline-suppr --quiet --force \
635 $(addprefix -I,$(INCLUDE_DIRS)) \
636 -I/usr/include -I/usr/include/linux
639 # Things we will build
641 TARGET_BIN = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET).bin
642 TARGET_HEX = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET).hex
643 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).elf
644 TARGET_OBJS = $(addsuffix .o,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(TARGET_SRC))))
645 TARGET_DEPS = $(addsuffix .d,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(TARGET_SRC))))
646 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).map
649 CLEAN_ARTIFACTS := $(TARGET_BIN)
650 CLEAN_ARTIFACTS += $(TARGET_HEX)
651 CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP)
653 # List of buildable ELF files and their object dependencies.
654 # It would be nice to compute these lists, but that seems to be just beyond make.
656 $(TARGET_HEX): $(TARGET_ELF)
657 $(OBJCOPY) -O ihex --set-start 0x8000000 $< $@
659 $(TARGET_BIN): $(TARGET_ELF)
660 $(OBJCOPY) -O binary $< $@
662 $(TARGET_ELF): $(TARGET_OBJS)
663 @echo LD $(notdir $@)
664 @$(CC) -o $@ $^ $(LDFLAGS)
665 $(SIZE) $(TARGET_ELF)
667 # Compile
668 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
669 @mkdir -p $(dir $@)
670 @echo %% $(notdir $<)
671 @$(CC) -c -o $@ $(CFLAGS) $<
673 # Assemble
674 $(OBJECT_DIR)/$(TARGET)/%.o: %.s
675 @mkdir -p $(dir $@)
676 @echo %% $(notdir $<)
677 @$(CC) -c -o $@ $(ASFLAGS) $<
679 $(OBJECT_DIR)/$(TARGET)/%.o: %.S
680 @mkdir -p $(dir $@)
681 @echo %% $(notdir $<)
682 @$(CC) -c -o $@ $(ASFLAGS) $<
685 ## all : Build all valid targets
686 all: $(VALID_TARGETS)
688 $(VALID_TARGETS):
689 echo "" && \
690 echo "Building $@" && \
691 $(MAKE) -j binary hex TARGET=$@ && \
692 echo "Building $@ succeeded."
694 ## clean : clean up all temporary / machine-generated files
695 clean:
696 rm -f $(CLEAN_ARTIFACTS)
697 rm -rf $(OBJECT_DIR)/$(TARGET)
699 ## clean_test : clean up all temporary / machine-generated files (tests)
700 clean_test:
701 cd src/test && $(MAKE) clean || true
703 ## clean_all_targets : clean all valid target platforms
704 clean_all:
705 for clean_target in $(VALID_TARGETS); do \
706 echo "" && \
707 echo "Cleaning $$clean_target" && \
708 $(MAKE) -j TARGET=$$clean_target clean || \
709 break; \
710 echo "Cleaning $$clean_target succeeded."; \
711 done
713 flash_$(TARGET): $(TARGET_HEX)
714 stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
715 echo -n 'R' >$(SERIAL_DEVICE)
716 stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
718 ## flash : flash firmware (.hex) onto flight controller
719 flash: flash_$(TARGET)
721 st-flash_$(TARGET): $(TARGET_BIN)
722 st-flash --reset write $< 0x08000000
724 ## st-flash : flash firmware (.bin) onto flight controller
725 st-flash: st-flash_$(TARGET)
727 binary: $(TARGET_BIN)
728 hex: $(TARGET_HEX)
730 unbrick_$(TARGET): $(TARGET_HEX)
731 stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
732 stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
734 ## unbrick : unbrick flight controller
735 unbrick: unbrick_$(TARGET)
737 ## cppcheck : run static analysis on C source code
738 cppcheck: $(CSOURCES)
739 $(CPPCHECK)
741 cppcheck-result.xml: $(CSOURCES)
742 $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
744 ## help : print this help message and exit
745 help: Makefile
746 @echo ""
747 @echo "Makefile for the $(FORKNAME) firmware"
748 @echo ""
749 @echo "Usage:"
750 @echo " make [TARGET=<target>] [OPTIONS=\"<options>\"]"
751 @echo "Or:"
752 @echo " make <target> [OPTIONS=\"<options>\"]"
753 @echo ""
754 @echo "Valid TARGET values are: $(VALID_TARGETS)"
755 @echo ""
756 @sed -n 's/^## //p' $<
758 ## targets : print a list of all valid target platforms (for consumption by scripts)
759 targets:
760 @echo "Valid targets: $(VALID_TARGETS)"
761 @echo "Target: $(TARGET)"
762 @echo "Base target: $(BASE_TARGET)"
764 ## test : run the cleanflight test suite
765 test:
766 cd src/test && $(MAKE) test || true
768 # rebuild everything when makefile changes
769 $(TARGET_OBJS) : Makefile
771 # include auto-generated dependencies
772 -include $(TARGET_DEPS)