Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
140 lines (111 sloc) 3.38 KB
# We only need to build our one source file
OBJS = main.o
# We've got opencm3 in the folder with us
OPENCM3_DIR := ./libopencm3
# Our output name
BINARY = main
# Linker script for our MCU
LDSCRIPT = stm32f0.ld
# Using the stm32f0 series chip
LIBNAME = opencm3_stm32f0
DEFS += -DSTM32F0
# Target-specific flags
FP_FLAGS ?= -msoft-float
ARCH_FLAGS = -mthumb -mcpu=cortex-m0 $(FP_FLAGS)
# Where our Black Magic Probe is attached
BMP_PORT = /dev/ttyACM0
## Boilerplate
# Compiler configuration
PREFIX ?= arm-none-eabi
CC := $(PREFIX)-gcc
CXX := $(PREFIX)-g++
LD := $(PREFIX)-gcc
AR := $(PREFIX)-ar
AS := $(PREFIX)-as
SIZE := $(PREFIX)-size
OBJCOPY := $(PREFIX)-objcopy
OBJDUMP := $(PREFIX)-objdump
GDB := $(PREFIX)-gdb
STFLASH = $(shell which st-flash)
OPT := -Os
DEBUG := -ggdb3
CSTD ?= -std=c99
# C flags
TGT_CFLAGS += $(OPT) $(CSTD) $(DEBUG)
TGT_CFLAGS += $(ARCH_FLAGS)
TGT_CFLAGS += -Wextra -Wshadow -Wimplicit-function-declaration
TGT_CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes
TGT_CFLAGS += -fno-common -ffunction-sections -fdata-sections
# C++ flags
TGT_CXXFLAGS += $(OPT) $(CXXSTD) $(DEBUG)
TGT_CXXFLAGS += $(ARCH_FLAGS)
TGT_CXXFLAGS += -Wextra -Wshadow -Wredundant-decls -Weffc++
TGT_CXXFLAGS += -fno-common -ffunction-sections -fdata-sections
TGT_CXXFLAGS += -std=c++11
# C & C++ preprocessor common flags
TGT_CPPFLAGS += -MD
TGT_CPPFLAGS += -Wall -Wundef
TGT_CPPFLAGS += $(DEFS)
# Linker flags
TGT_LDFLAGS += --static -nostartfiles
TGT_LDFLAGS += -T$(LDSCRIPT)
TGT_LDFLAGS += $(ARCH_FLAGS) $(DEBUG)
TGT_LDFLAGS += -Wl,-Map=$(*).map -Wl,--cref
TGT_LDFLAGS += -Wl,--gc-sections
ifeq ($(V),99)
TGT_LDFLAGS += -Wl,--print-gc-sections
endif
# Used libraries
DEFS += -I$(OPENCM3_DIR)/include
LDFLAGS += -L$(OPENCM3_DIR)/lib
LDLIBS += -l$(LIBNAME)
LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group
.SUFFIXES: .elf .bin .hex .srec .list .map .images
.SECONDEXPANSION:
.SECONDARY:
all: elf size
size: $(BINARY).size
elf: $(BINARY).elf
bin: $(BINARY).bin
hex: $(BINARY).hex
srec: $(BINARY).srec
list: $(BINARY).list
flash: $(BINARY).flash
GENERATED_BINARIES=$(BINARY).elf $(BINARY).bin $(BINARY).hex $(BINARY).srec $(BINARY).list $(BINARY).map
%.bin: %.elf
$(OBJCOPY) -Obinary $(*).elf $(*).bin
%.hex: %.elf
$(OBJCOPY) -Oihex $(*).elf $(*).hex
%.srec: %.elf
$(OBJCOPY) -Osrec $(*).elf $(*).srec
%.list: %.elf
$(OBJDUMP) -S $(*).elf > $(*).list
%.elf %.map: $(OBJS) $(LDSCRIPT)
$(LD) $(TGT_LDFLAGS) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $(*).elf
%.o: %.c
$(CC) $(TGT_CFLAGS) $(CFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).c
%.o: %.cxx
$(CXX) $(TGT_CXXFLAGS) $(CXXFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).cxx
%.o: %.cpp
$(CXX) $(TGT_CXXFLAGS) $(CXXFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).cpp
%.size: %.elf
@echo "Output code size:"
@$(SIZE) -A -d $(*).elf | egrep 'text|data|bss' | awk ' \
function human(x) { \
if (x<1000) {return x} else {x/=1024} \
s="kMGTEPZY"; \
while (x>=1000 && length(s)>1) \
{x/=1024; s=substr(s,2)} \
return int(x+0.5) substr(s,1,1) \
} \
{printf("%10s %8s\n", $$1, human($$2))} \
'
%.flash: %.elf
$(GDB) --batch \
-ex 'target extended-remote $(BMP_PORT)' \
-x bmp_flash.scr \
$(*).elf
clean:
$(RM) $(GENERATED_BINARIES) generated.* $(OBJS) $(OBJS:%.o=%.d)
.PHONY: images clean stylecheck styleclean elf bin hex srec list
-include $(OBJS:.o=.d)