diff --git a/Makefile b/Makefile index 52477a1..aacc8c7 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ BINARY = firmware # which development board is used # supported are: SYSTEM_BOARD, MAPLE_MINI, BLUE_PILL -BOARD ?= SYSTEM_BOARD +BOARD = SYSTEM_BOARD # source files CSRC = $(wildcard *.c) @@ -45,32 +45,24 @@ LIB_OBJ = $(patsubst %.c,%.o,$(LIB_CSRC)) # populates LIB_CSRC based on the library files used -include $(DEPENDENCIES) -# cross-compiler tools +# executables for linking, compiling, debugging, ... PREFIX ?= arm-none-eabi -# compile using gcc (requires gcc-arm-none-eabi) -#CC := $(PREFIX)-gcc -# compile using clang (requires clang gcc-multilib) -CC := clang -target $(PREFIX) -# link using gcc (requires gcc-arm-none-eabi) -#LD := $(PREFIX)-gcc -# link (requires binutils-arm-none-eabi) +CC := $(PREFIX)-gcc LD := $(PREFIX)-ld -# create archive (requires binutils-arm-none-eabi) AR := $(PREFIX)-ar -# compile assembly (requires binutils-arm-none-eabi) AS := $(PREFIX)-as OBJCOPY := $(PREFIX)-objcopy OBJDUMP := $(PREFIX)-objdump -# debugger (requires) GDB := $(PREFIX)-gdb # opencm3 libraries OPENCM3_DIR := libopencm3 -INCLUDE_DIR = $(OPENCM3_DIR)/include -LIB_DIR = $(OPENCM3_DIR)/lib -SCRIPT_DIR = $(OPENCM3_DIR)/scripts +OPENCM3_INC = $(OPENCM3_DIR)/include +OPENCM3_LIB = $(OPENCM3_DIR)/lib +# library for the STM32F1 (provided by opencm3) +STM32F1_LIB = opencm3_stm32f1 -# linker script +# linker script for STN32F1 boards ifeq ($(BOARD),SYSTEM_BOARD) LDSCRIPT = $(OPENCM3_DIR)/lib/stm32/f1/stm32f103x8.ld else ifeq ($(BOARD),BLUE_PILL) @@ -79,44 +71,54 @@ else ifeq ($(BOARD),MAPLE_MINI) LDSCRIPT = $(OPENCM3_DIR)/lib/stm32/f1/stm32f103xb.ld endif -# verify if libopencm3 has been downloaded +# verify if opencm3 has been downloaded OPENCM3_DIR_EXISTS = $(shell [ -f $(LDSCRIPT) ] && echo 1 || echo 0 ) ifeq ($(OPENCM3_DIR_EXISTS), 0) $(info run "git submodule init" and "git submodule update" before runnig make) $(error libopencm3 repository is not initialized) endif -# device flags +# device micro-controller and board DEFS += -DSTM32F1 -D$(BOARD) # C flags -CFLAGS += -Os -g -std=c99 -nostdlib -nostdinc -CFLAGS += -fno-common -ffunction-sections -fdata-sections -ffreestanding -CFLAGS += -Wpedantic -Wall -Werror -Wextra -Wshadow -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes -Wstrict-overflow=5 -CFLAGS += -I/usr/include/newlib/ -I/usr/lib/gcc/arm-none-eabi/*/include/ -I. -I$(INCLUDE_DIR) $(patsubst %,-I%,$(LIB)) +# optimize for size +CFLAGS += -Os +# add debug symbols (remove for smaller release) +CFLAGS += -g +# 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 +CFLAGS += -fno-common -ffunction-sections -fdata-sections +# don't use system main definition (the starting point) +CFLAGS += -ffreestanding +# include own libraries +CFLAGS += -I . $(patsubst %,-I%,$(LIB)) +# include opencm3 libraries +CFLAGS += -I $(OPENCM3_INC) +# add defines for micro-controller and board CFLAGS += $(DEFS) # linker flags -LDFLAGS += -static -nostartfiles -nostdlib -# opencm3 libraries -LDFLAGS += --library-path $(LIB_DIR) -LIBNAME = opencm3_stm32f1 -LDLIBS += --library $(LIBNAME) -# use newlib (providing libc, libm, libnosys, libg) -# TODO retrieve path automatically -LDFLAGS += --library-path /usr/lib/arm-none-eabi/newlib/armv7-m/ -# use libgcc (provides __aeabi_*) -# TODO retrieve path automatically (arm-none-eabi-gcc does it on its own) -LDFLAGS += --library-path /usr/lib/gcc/arm-none-eabi/*/armv7-m/ -# own libraries -LDFLAGS += --dynamic-linker . $(patsubst %,--dynamic-linker %,$(LIB)) -# libc and other system libraries -LDLIBS += --library c --library gcc --library nosys --library g --library m -# linker script for micro-controller -LDFLAGS += -T $(LDSCRIPT) +LDFLAGS += -static -nostartfiles -nostdlib -nostdinc +LDFLAGS += -L$(OPENCM3_LIB) +LDFLAGS += -L. $(patsubst %,-L%,$(LIB)) +# add c,m,nosys system libraries +LDFLAGS += -L/usr/arm-none-eabi/lib/armv7-m/ +# add gcc system library +LDFLAGS += -L/usr/lib/gcc/arm-none-eabi/6.1.1/armv7-m/ +# linker definitions for micro-controller +LDFLAGS += -T$(LDSCRIPT) +# only keep used sections +LDFLAGS += --gc-sections -# device specific flags -ARCH_FLAGS = -mthumb -mcpu=cortex-m3 +# used libraries (must be provided after objects) +LDLIBS += -l$(STM32F1_LIB) -lc -lgcc -lnosys -lm + +# 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 # SWD adapter used # supported are : st-link v2 (STLINKV2), black magic probe (BMP) @@ -154,12 +156,13 @@ list: $(BINARY).list %.map %.list: %.elf $(Q)$(OBJDUMP) -S $(<) > $(@) -%.elf: $(LDSCRIPT) $(LIB_DIR)/lib$(LIBNAME).a $(OBJ) $(LIB_OBJ) - $(info compiling $(@)) +%.elf: $(LDSCRIPT) $(OPENCM3_LIB)/lib$(STM32F1_LIB).a $(OBJ) $(LIB_OBJ) + $(info linking $(@)) $(Q)$(LD) $(LDFLAGS) $(OBJ) $(LIB_OBJ) $(LDLIBS) -o $(@) $(Q)size $(@) %.o: %.c $(CHDR) $(LIB_CHDR) + $(info compiling $(@)) $(Q)$(CC) $(CFLAGS) $(ARCH_FLAGS) -o $(@) -c $(<) # generate dependencies @@ -178,8 +181,8 @@ doc: Doxyfile README.md $(CSRC) $(CHDR) $(LIB_CSRC) $(LIB_CHDR) clean: $(Q)$(RM) $(BINARY).elf $(BINARY).bin $(BINARY).hex $(BINARY).map $(OBJ) $(LIB_OBJ) $(LIB)/*.o $(DEPENDENCIES) -# make libopencm3 if not done -$(LIB_DIR)/lib$(LIBNAME).a: +# make libopencm3 if library for STM32F1 is not yet existing +$(OPENCM3_LIB)/lib$(STM32F1_LIB).a: $(info compiling libopencm3 library) $(Q)$(MAKE) -C $(OPENCM3_DIR)