1 # Generic Makefile fragment for Blue Pill devices and LibOpenCM3
5 # ROOT path to root of the repository
6 # BINARY binary to build (without extension)
7 # OBJS list of object files
8 # LIB_OBJS list of library object files
9 # WITH_BOOT_LOADER define if code origin should be shifted by 8K for boot loader
10 # WITH_DFU_FLASH flashing uses dfu-util
16 OPENCM3_DIR=/home/mj/stm/libopencm3
20 all: $(BINARY).elf $(BINARY).bin
23 flash: $(BINARY).flash
30 include $(OPENCM3_DIR)/mk/genlink-config.mk
32 ifdef WITH_BOOT_LOADER
34 # We want to generate a linked script for a different ROM start address
35 UCW_LDSCRIPT=bootloader-$(DEVICE).ld
37 bootloader-$(DEVICE).ld: generated.$(DEVICE).ld
38 @printf " GENLNK2 $@\n"
39 $(Q)sed '/^ rom /s/ORIGIN = 0x08000000/ORIGIN = 0x08002000/' <$< >$@
43 UCW_LDSCRIPT=$(LDSCRIPT)
47 PREFIX ?= arm-none-eabi
54 OBJCOPY := $(PREFIX)-objcopy
55 OBJDUMP := $(PREFIX)-objdump
61 TGT_CFLAGS += $(OPT) $(CSTD) $(DEBUG)
62 TGT_CFLAGS += $(ARCH_FLAGS)
63 TGT_CFLAGS += -Wall -Wextra -Wshadow -Wimplicit-function-declaration
64 TGT_CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes
65 TGT_CFLAGS += -fno-common -ffunction-sections -fdata-sections
66 TGT_CFLAGS += -I. -I../lib
70 TGT_LDFLAGS += --static -nostartfiles
71 TGT_LDFLAGS += -T$(UCW_LDSCRIPT)
72 TGT_LDFLAGS += $(ARCH_FLAGS) $(DEBUG)
73 TGT_LDFLAGS += -Wl,-Map=$(*).map -Wl,--cref
74 TGT_LDFLAGS += -Wl,--gc-sections
76 TGT_LDFLAGS += -Wl,--print-gc-sections
79 LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group
81 include $(OPENCM3_DIR)/mk/genlink-rules.mk
84 @printf " OBJCOPY $(*).bin\n"
85 $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin
87 %.elf: $(OBJS) $(UCW_LDSCRIPT)
88 @printf " LD $(*).elf\n"
89 $(Q)$(LD) $(TGT_LDFLAGS) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $*.elf
92 @printf " CC $(*).c\n"
93 $(Q)$(CC) $(TGT_CFLAGS) $(CFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $@ -c $<
98 $(Q)rm -f *.elf *.bin *.o *.d *.map $(LDSCRIPT) $(UCW_LDSCRIPT)
103 @printf " FLASH $<\n"
104 $(Q)dfu-util -a2 -D $(*).bin
109 @printf " FLASH $<\n"
110 $(Q)$(ROOT)/bin/st-flash write $(*).bin 0x8000000
114 $(ROOT)/bin/st-flash reset
121 -include $(OBJS:.o=.d)