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
11 # WITH_SERIAL_FLASH flashing uses the built-in serial boot-loader
12 # MAX_SIZE complain if the built firmware exceeds this size
18 OPENCM3_DIR=/home/mj/stm/libopencm3
25 flash: $(BINARY).flash
32 include $(OPENCM3_DIR)/mk/genlink-config.mk
34 ifdef WITH_BOOT_LOADER
36 # We want to generate a linked script for a different ROM start address
37 UCW_LDSCRIPT=bootloader-$(DEVICE).ld
39 bootloader-$(DEVICE).ld: generated.$(DEVICE).ld
40 @printf " GENLNK2 $@\n"
41 $(Q)sed '/^ rom /s/ORIGIN = 0x08000000/ORIGIN = 0x08002000/' <$< >$@
45 UCW_LDSCRIPT=$(LDSCRIPT)
49 PREFIX ?= arm-none-eabi
56 OBJCOPY := $(PREFIX)-objcopy
57 OBJDUMP := $(PREFIX)-objdump
63 TGT_CFLAGS += $(OPT) $(CSTD) $(DEBUG)
64 TGT_CFLAGS += $(ARCH_FLAGS)
65 TGT_CFLAGS += -Wall -Wextra -Wshadow -Wimplicit-function-declaration
66 TGT_CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes -Wno-parentheses
67 TGT_CFLAGS += -fno-common -ffunction-sections -fdata-sections
68 TGT_CFLAGS += -I. -I$(ROOT)/lib
72 TGT_LDFLAGS += --static -nostartfiles
73 TGT_LDFLAGS += -T$(UCW_LDSCRIPT)
74 TGT_LDFLAGS += $(ARCH_FLAGS) $(DEBUG)
75 TGT_LDFLAGS += -Wl,-Map=$(*).map -Wl,--cref
76 TGT_LDFLAGS += -Wl,--gc-sections
78 TGT_LDFLAGS += -Wl,--print-gc-sections
81 LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group
83 include $(OPENCM3_DIR)/mk/genlink-rules.mk
86 @printf " OBJCOPY $< -> $@\n"
87 $(Q)$(OBJCOPY) -Obinary $< $@
89 $(Q)if [ $$(stat -c '%s' $@) -gt $(MAX_SIZE) ] ; then echo >&2 "Output too exceeds $(MAX_SIZE) bytes!" ; false ; fi
92 %.elf: $(OBJS) $(UCW_LDSCRIPT)
93 @printf " LD $(*).elf\n"
94 $(Q)$(LD) $(TGT_LDFLAGS) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $*.elf
97 @printf " CC $(*).c\n"
98 $(Q)$(CC) $(TGT_CFLAGS) $(CFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $@ -c $<
103 $(Q)rm -f *.elf *.bin *.dfu *.o *.d *.map $(LDSCRIPT) $(UCW_LDSCRIPT)
110 @printf " FLASH $<\n"
113 %.dfu: %.bin $(ROOT)/tools/dfu-sign
114 @printf " SIGN $< -> $@\n"
115 $(Q)$(ROOT)/tools/dfu-sign $< $@
117 $(ROOT)/tools/dfu-sign:
118 make -C $(ROOT)/tools
120 # For the STM32duino-bootloader, we used:
122 # @printf " FLASH $<\n"
123 # $(Q)dfu-util -a2 -D $(*).bin
127 ifdef WITH_SERIAL_FLASH
131 BOOT_SERIAL ?= /dev/ttyUSB0
134 @printf " FLASH $<\n"
135 $(Q)$(ROOT)/bin/stm32flash $(BOOT_SERIAL) -i 'dtr,-dtr' -w $< -g 0
139 $(Q)$(ROOT)/bin/stm32flash $(BOOT_SERIAL) -i 'dtr,-dtr' -g 0
146 @printf " FLASH $<\n"
147 $(Q)$(ROOT)/bin/st-flash write $(*).bin 0x8000000
151 $(ROOT)/bin/st-flash reset
159 -include $(OBJS:.o=.d)