2016-01-17 14:06:39 +01:00
|
|
|
## This library is free software: you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
## the Free Software Foundation, either version 3 of the License, or
|
|
|
|
## (at your option) any later version.
|
|
|
|
##
|
|
|
|
## This library is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU Lesser General Public License for more details.
|
|
|
|
##
|
|
|
|
## You should have received a copy of the GNU Lesser General Public License
|
|
|
|
## along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
##
|
|
|
|
|
2016-01-28 22:59:56 +01:00
|
|
|
## the make file provide rule to compile and flash firmware for STM32F1 micro-controllers
|
2016-08-14 18:36:27 +02:00
|
|
|
## it uses libopencm3
|
2016-01-17 14:06:39 +01:00
|
|
|
|
|
|
|
# be silent per default, but 'make V=1' will show all compiler calls.
|
|
|
|
ifneq ($(V),1)
|
|
|
|
Q := @
|
2016-01-28 21:21:50 +01:00
|
|
|
NULL := 1> /dev/null 2> /dev/null
|
2016-01-17 14:06:39 +01:00
|
|
|
endif
|
|
|
|
|
2017-04-15 13:51:24 +02:00
|
|
|
# main names (without extension, for input source file and output binary)
|
|
|
|
APPLICATION = application
|
|
|
|
BOOTLOADER = bootloader
|
|
|
|
FIRMWARE = $(APPLICATION) $(BOOTLOADER)
|
2016-01-17 14:06:39 +01:00
|
|
|
|
2016-01-28 22:59:56 +01:00
|
|
|
# which development board is used
|
2017-04-03 13:06:28 +02:00
|
|
|
# supported are: SYSTEM_BOARD, MAPLE_MINI, BLUE_PILL, CORE_BOARD
|
2017-06-27 11:54:47 +02:00
|
|
|
BOARD = SYSTEM_BOARD
|
2016-01-28 22:59:56 +01:00
|
|
|
|
2017-06-27 15:33:31 +02:00
|
|
|
# opencm3 libraries
|
|
|
|
OPENCM3_DIR := libopencm3
|
|
|
|
OPENCM3_INC = $(OPENCM3_DIR)/include
|
|
|
|
OPENCM3_LIB = $(OPENCM3_DIR)/lib
|
|
|
|
|
|
|
|
# library for the STM32F1 (provided by opencm3)
|
|
|
|
STM32F1_LIB = opencm3_stm32f1
|
|
|
|
|
2017-04-15 13:51:24 +02:00
|
|
|
# source files (this will be populated using includes based DEPENDENCIES)
|
|
|
|
CSRC = global.c
|
|
|
|
# headers corresponding to source files
|
2017-06-27 15:46:50 +02:00
|
|
|
CHDR = $(sort $(patsubst %.c,%.h,$(CSRC)))
|
2017-04-15 13:51:24 +02:00
|
|
|
# objects compiled from source files
|
2017-06-27 15:46:50 +02:00
|
|
|
OBJ = $(sort $(patsubst %.c,%.o,$(CSRC)))
|
2017-04-15 13:51:24 +02:00
|
|
|
# figure out based on the main sources files which library files are used
|
|
|
|
DEPENDENCIES = $(patsubst %,%.inc,$(FIRMWARE))
|
|
|
|
# populates CSRC based on the library files used
|
2016-02-18 10:39:08 +01:00
|
|
|
-include $(DEPENDENCIES)
|
2016-01-17 14:06:39 +01:00
|
|
|
|
2016-10-23 17:42:27 +02:00
|
|
|
# executables for linking, compiling, debugging, ...
|
2017-04-03 13:06:28 +02:00
|
|
|
# use ELLCC cross-compiling chain (based on clang/llvm + musl)
|
|
|
|
ELLCC := /opt/ellcc/
|
|
|
|
# use ELLCC as compile
|
|
|
|
CC := $(ELLCC)bin/ecc -target arm-none-eabi
|
2016-10-23 17:42:27 +02:00
|
|
|
LD := $(PREFIX)-ld
|
2017-04-03 13:06:28 +02:00
|
|
|
LD := $(ELLCC)bin/ecc-ld -m armelf
|
|
|
|
AR := $(ELLCC)bin/ecc-ar
|
|
|
|
AS := $(ELLCC)bin/ecc-as
|
|
|
|
OBJCOPY := $(ELLCC)bin/ecc-objcopy
|
|
|
|
OBJDUMP := $(ELLCC)bin/ecc-objdump
|
2017-04-15 13:51:24 +02:00
|
|
|
GDB := $(ELLCC)bin/ecc-gdb
|
|
|
|
# ecc-gdb (0.13.3) is buggy (crash on kill, can't load elf)
|
|
|
|
GDB := arm-none-eabi-gdb
|
2016-01-17 14:06:39 +01:00
|
|
|
|
2016-10-23 17:42:27 +02:00
|
|
|
# device micro-controller and board
|
2016-01-29 00:23:48 +01:00
|
|
|
DEFS += -DSTM32F1 -D$(BOARD)
|
2016-01-17 14:06:39 +01:00
|
|
|
|
|
|
|
# C flags
|
2016-10-23 17:42:27 +02:00
|
|
|
# optimize for size
|
|
|
|
CFLAGS += -Os
|
|
|
|
# add debug symbols (remove for smaller release)
|
2017-04-03 13:06:28 +02:00
|
|
|
CFLAGS += -ggdb
|
2016-10-23 17:42:27 +02:00
|
|
|
# use C99 (supported by most an sufficient)
|
|
|
|
CFLAGS += -std=c99
|
|
|
|
# have strict warning (for better code)
|
|
|
|
CFLAGS += -Wpedantic -Wall -Werror -Wundef -Wextra -Wshadow -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes -Wstrict-overflow=5
|
|
|
|
# add options for better code optimization
|
2016-01-17 14:06:39 +01:00
|
|
|
CFLAGS += -fno-common -ffunction-sections -fdata-sections
|
2016-10-23 17:42:27 +02:00
|
|
|
# use variable size enum (opencm3, gcc, and compiler-rt do)
|
2017-04-03 13:32:50 +02:00
|
|
|
#CFLAGS += -fshort-enums
|
2017-04-03 13:06:28 +02:00
|
|
|
# use no variable size enum (ELLCC/musl does not)
|
|
|
|
CFLAGS += -fno-short-enums
|
2016-10-23 17:42:27 +02:00
|
|
|
# don't use system main definition (the starting point)
|
|
|
|
CFLAGS += -ffreestanding
|
|
|
|
# don't use the standard library
|
2017-04-03 13:06:28 +02:00
|
|
|
CFLAGS += -nostdlib -nostdinc
|
|
|
|
# include ELLCC libraries
|
2017-04-15 13:51:24 +02:00
|
|
|
CFLAGS += -I $(ELLCC)libecc/include/ -I $(ELLCC)libecc/include/arm/
|
2016-10-23 17:42:27 +02:00
|
|
|
# include own libraries
|
2017-04-15 13:51:24 +02:00
|
|
|
CFLAGS += -I . -I lib
|
2016-10-23 17:42:27 +02:00
|
|
|
# include opencm3 libraries
|
|
|
|
CFLAGS += -I $(OPENCM3_INC)
|
|
|
|
# add defines for micro-controller and board
|
2016-01-17 14:54:15 +01:00
|
|
|
CFLAGS += $(DEFS)
|
2016-01-17 14:06:39 +01:00
|
|
|
|
|
|
|
# linker flags
|
2016-10-23 17:42:27 +02:00
|
|
|
# build static binary (no shared libraries on the micro-controller)
|
|
|
|
LDFLAGS += -static
|
|
|
|
# don's include the system start files
|
|
|
|
LDFLAGS += -nostartfiles
|
|
|
|
# only keep used sections
|
|
|
|
LDFLAGS += --gc-sections
|
|
|
|
# don't use system libraries
|
2017-04-03 13:06:28 +02:00
|
|
|
LDFLAGS += -nostdlib -nostdinc
|
|
|
|
# add ELLCC standard libraries (for libc, libm, libgcc)
|
|
|
|
LDFLAGS += --library-path $(ELLCC)libecc/lib/cortex-m3-linux/
|
2016-10-23 17:42:27 +02:00
|
|
|
# opencm3 libraries
|
|
|
|
LDFLAGS += --library-path $(OPENCM3_LIB)
|
2017-04-03 13:06:28 +02:00
|
|
|
# used libraries (gcc provides the ARM ABI, not sure how to replace with compiler-rt)
|
|
|
|
LDLIBS += --library $(STM32F1_LIB) --library c --library m --library gcc
|
2016-01-17 14:06:39 +01:00
|
|
|
|
2016-10-23 17:42:27 +02:00
|
|
|
# target micro-controller information (ARM Cortex-M3 supports thumb and thumnb2, but does not include a floating point unit)
|
|
|
|
ARCH_FLAGS = -mthumb -mcpu=cortex-m3 -msoft-float
|
2016-01-17 14:06:39 +01:00
|
|
|
|
2016-08-14 18:36:27 +02:00
|
|
|
# SWD adapter used
|
|
|
|
# supported are : st-link v2 (STLINKV2), black magic probe (BMP)
|
2017-06-27 11:54:47 +02:00
|
|
|
SWD_ADAPTER ?= BMP
|
2016-08-14 18:36:27 +02:00
|
|
|
ifeq ($(SWD_ADAPTER),STLINKV2)
|
2016-01-17 14:06:39 +01:00
|
|
|
# OpenOCD configuration
|
|
|
|
OOCD ?= openocd
|
|
|
|
OOCD_INTERFACE ?= stlink-v2
|
|
|
|
OOCD_TARGET ?= stm32f1x
|
2016-08-14 18:36:27 +02:00
|
|
|
else ifeq ($(SWD_ADAPTER),BMP)
|
|
|
|
# the black magic probe has a SWD controller built in
|
2017-04-15 13:51:24 +02:00
|
|
|
BMP_PORT ?= /dev/ttyACM0
|
2016-01-17 14:06:39 +01:00
|
|
|
endif
|
|
|
|
|
|
|
|
# compile target rules
|
2017-06-27 15:33:31 +02:00
|
|
|
all: elf hex bin
|
2016-01-17 14:06:39 +01:00
|
|
|
|
2017-04-15 13:51:24 +02:00
|
|
|
elf: $(patsubst %,%.elf,$(FIRMWARE))
|
|
|
|
bin: $(patsubst %,%.bin,$(FIRMWARE))
|
|
|
|
hex: $(patsubst %,%.hex,$(FIRMWARE))
|
2016-01-17 14:06:39 +01:00
|
|
|
|
2017-04-15 13:51:24 +02:00
|
|
|
%.hex: %.elf
|
|
|
|
$(Q)$(OBJCOPY) --strip-all --strip-debug --output-target ihex $(<) $(@)
|
|
|
|
|
|
|
|
%.bin: %.elf
|
|
|
|
$(Q)$(OBJCOPY) --strip-all --strip-debug --output-target binary $(<) $(@)
|
2016-01-17 14:06:39 +01:00
|
|
|
|
2016-08-14 18:36:27 +02:00
|
|
|
%.map %.list: %.elf
|
2016-01-17 14:06:39 +01:00
|
|
|
$(Q)$(OBJDUMP) -S $(<) > $(@)
|
|
|
|
|
2017-04-15 13:51:24 +02:00
|
|
|
%.elf: %.o %.ld $(OBJ) $(OPENCM3_LIB)/lib$(STM32F1_LIB).a
|
2016-10-23 17:42:27 +02:00
|
|
|
$(info linking $(@))
|
2017-04-15 13:51:24 +02:00
|
|
|
$(Q)$(LD) $(LDFLAGS) --script $(*).ld $(<) $(OBJ) $(LDLIBS) -o $(@)
|
2016-01-28 21:21:50 +01:00
|
|
|
$(Q)size $(@)
|
2016-01-17 14:06:39 +01:00
|
|
|
|
2017-06-27 15:33:31 +02:00
|
|
|
%.o: %.c $(CHDR) $(OPENCM3_LIB)/lib$(STM32F1_LIB).a
|
2016-10-23 17:42:27 +02:00
|
|
|
$(info compiling $(@))
|
2016-01-17 14:06:39 +01:00
|
|
|
$(Q)$(CC) $(CFLAGS) $(ARCH_FLAGS) -o $(@) -c $(<)
|
|
|
|
|
2017-06-27 15:33:31 +02:00
|
|
|
# generate dependencies (requires libopencm3 to be built to generate all libraries)
|
|
|
|
%.d: %.c $(OPENCM3_LIB)/lib$(STM32F1_LIB).a
|
2016-02-18 10:39:08 +01:00
|
|
|
@# check which libraries are used
|
|
|
|
$(Q)$(CC) $(CFLAGS) $(ARCH_FLAGS) -MM -MF $(@) -o /dev/null -c $(<)
|
|
|
|
|
|
|
|
# figure out which library source files are used for later inclusion
|
|
|
|
%.inc: %.d
|
2017-04-15 13:51:24 +02:00
|
|
|
$(Q)grep -o -e " lib\/[^ ]*\.h" $(<) | sed -e 's|\(.*\)\.h$$|CSRC +=\1.c\n-include\1.inc|g' -e 's|.*${*}.*||g' > $(@)
|
2016-02-18 10:39:08 +01:00
|
|
|
|
2017-06-27 15:33:31 +02:00
|
|
|
# get libopencm3
|
|
|
|
$(OPENCM3_DIR)/Makefile:
|
|
|
|
$(info checking out libopencm3 submodule)
|
|
|
|
$(Q)git submodule init
|
|
|
|
$(Q)git submodule update
|
|
|
|
|
|
|
|
# compile libopencm3
|
|
|
|
$(OPENCM3_LIB)/lib$(STM32F1_LIB).a: $(OPENCM3_DIR)/Makefile
|
|
|
|
$(info compiling libopencm3 submodule)
|
|
|
|
$(Q)CFLAGS=-fno-short-enums $(MAKE) --directory $(OPENCM3_DIR)
|
|
|
|
|
2016-08-14 18:36:27 +02:00
|
|
|
# doxygen documentation
|
2017-04-15 13:51:24 +02:00
|
|
|
doc: Doxyfile README.md $(patsubst %,%.c,$(FIRMWARE)) $(CSRC) $(CHDR)
|
2016-08-14 18:36:27 +02:00
|
|
|
$(Q)doxygen $(<)
|
|
|
|
|
2016-01-17 14:06:39 +01:00
|
|
|
clean:
|
2017-04-15 17:58:52 +02:00
|
|
|
$(Q)$(RM) $(BOOTLOADER).elf $(BOOTLOADER).bin $(BOOTLOADER).hex $(APPLICATION).elf $(APPLICATION).bin $(APPLICATION).hex $(OBJ) $(DEPENDENCIES) *.inc lib/*.inc
|
2016-01-17 14:06:39 +01:00
|
|
|
|
2017-04-15 13:51:24 +02:00
|
|
|
# flash application using DFU
|
|
|
|
flash: $(APPLICATION).bin
|
|
|
|
$(Q)dfu-util -d c440:0d00 -D $(<)
|
|
|
|
|
|
|
|
# flash bootloader using SWD
|
|
|
|
flash_bootloader: $(BOOTLOADER).hex
|
2016-01-17 14:06:39 +01:00
|
|
|
$(info flashing $(<) using SWD)
|
2016-08-14 18:36:27 +02:00
|
|
|
ifeq ($(SWD_ADAPTER),STLINKV2)
|
2016-01-18 16:11:45 +01:00
|
|
|
$(Q)$(OOCD) --file interface/$(OOCD_INTERFACE).cfg --file target/$(OOCD_TARGET).cfg --command "init" --command "reset init" --command "flash write_image erase $(<)" --command "reset" --command "shutdown" $(NULL)
|
2016-08-14 18:36:27 +02:00
|
|
|
else ifeq ($(SWD_ADAPTER),BMP)
|
2017-04-15 13:51:24 +02:00
|
|
|
$(Q)$(GDB) --eval-command="target extended-remote $(BMP_PORT)" --eval-command="set confirm off" --eval-command="monitor swdp_scan" --eval-command="attach 1" --eval-command="load" --eval-command="detach" --eval-command="quit" $(<)
|
|
|
|
endif
|
|
|
|
|
|
|
|
# flash application using SWD
|
|
|
|
flash_application: $(APPLICATION).hex
|
|
|
|
$(info flashing $(<) using SWD)
|
|
|
|
ifeq ($(SWD_ADAPTER),STLINKV2)
|
|
|
|
$(Q)$(OOCD) --file interface/$(OOCD_INTERFACE).cfg --file target/$(OOCD_TARGET).cfg --command "init" --command "reset init" --command "flash write_image erase $(<)" --command "reset" --command "shutdown" $(NULL)
|
|
|
|
else ifeq ($(SWD_ADAPTER),BMP)
|
2017-06-27 11:55:56 +02:00
|
|
|
$(Q)$(GDB) --eval-command="target extended-remote $(BMP_PORT)" --eval-command="set confirm off" --eval-command="monitor swdp_scan" --eval-command="attach 1" --eval-command="load" --eval-command="kill" --eval-command="quit" $(<)
|
2016-08-14 18:36:27 +02:00
|
|
|
endif
|
2016-01-17 14:06:39 +01:00
|
|
|
|
2016-01-28 21:21:50 +01:00
|
|
|
# reset device by setting the data width to 5 bis on the USB CDC ACM port
|
2017-04-15 13:51:24 +02:00
|
|
|
ifeq ($(SWD_ADAPTER)$(BMP_PORT),BMP/dev/ttyACM0)
|
|
|
|
ACM_PORT := /dev/ttyACM2
|
|
|
|
else
|
|
|
|
ACM_PORT := /dev/ttyACM0
|
|
|
|
endif
|
2016-01-28 21:21:50 +01:00
|
|
|
reset:
|
2017-04-15 13:51:24 +02:00
|
|
|
$(Q)stty --file $(ACM_PORT) raw cs5
|
2016-01-28 21:21:50 +01:00
|
|
|
$(Q)sleep 0.5
|
2016-01-17 14:06:39 +01:00
|
|
|
|
2017-04-15 13:51:24 +02:00
|
|
|
# debug application using GDB
|
|
|
|
debug: $(APPLICATION).elf
|
2016-08-14 18:36:27 +02:00
|
|
|
ifeq ($(SWD_ADAPTER),STLINKV2)
|
|
|
|
# for GDB to work with openOCD the firmware needs to be reloaded
|
|
|
|
$(Q)$(GDB) --eval-command="target remote | $(OOCD) --file interface/$(OOCD_INTERFACE).cfg --file target/$(OOCD_TARGET).cfg --command \"gdb_port pipe; log_output /dev/null; init\"" --eval-command="monitor reset halt" --eval-command="load" --eval-command="monitor reset init" $(<)
|
|
|
|
else ifeq ($(SWD_ADAPTER),BMP)
|
2017-04-15 13:51:24 +02:00
|
|
|
$(Q)$(GDB) --eval-command="target extended-remote $(BMP_PORT)" --eval-command="monitor version" --eval-command="monitor swdp_scan" --eval-command="attach 1" $(<)
|
2016-08-14 18:36:27 +02:00
|
|
|
endif
|
2016-02-18 10:39:08 +01:00
|
|
|
|
2016-10-23 17:42:27 +02:00
|
|
|
.PHONY: clean elf bin hex srec list flash reset
|