]> mj.ucw.cz Git - home-hw.git/blob - mk/bluepill.mk
Air tests: logger
[home-hw.git] / mk / bluepill.mk
1 # Generic Makefile fragment for Blue Pill devices and LibOpenCM3
2 #
3 # Parameters:
4 #
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 #
12
13 vpath %.c $(ROOT)/lib
14 OBJS += $(LIB_OBJS)
15
16 OPENCM3_DIR=/home/mj/stm/libopencm3
17 DEVICE=stm32f103x8
18
19 .PHONY: all
20 all: $(BINARY).elf $(BINARY).bin
21
22 .PHONY: flash
23 flash: $(BINARY).flash
24
25 ifneq ($(V),1)
26 Q               := @
27 NULL            := 2>/dev/null
28 endif
29
30 include $(OPENCM3_DIR)/mk/genlink-config.mk
31
32 ifdef WITH_BOOT_LOADER
33
34 # We want to generate a linked script for a different ROM start address
35 UCW_LDSCRIPT=bootloader-$(DEVICE).ld
36
37 bootloader-$(DEVICE).ld: generated.$(DEVICE).ld
38         @printf "  GENLNK2 $@\n"
39         $(Q)sed '/^ rom /s/ORIGIN = 0x08000000/ORIGIN = 0x08002000/' <$< >$@
40
41 else
42
43 UCW_LDSCRIPT=$(LDSCRIPT)
44
45 endif
46
47 PREFIX          ?= arm-none-eabi
48
49 CC              := $(PREFIX)-gcc
50 CXX             := $(PREFIX)-g++
51 LD              := $(PREFIX)-gcc
52 AR              := $(PREFIX)-ar
53 AS              := $(PREFIX)-as
54 OBJCOPY         := $(PREFIX)-objcopy
55 OBJDUMP         := $(PREFIX)-objdump
56 GDB             := $(PREFIX)-gdb
57 OPT             := -Os
58 DEBUG           := -ggdb3
59 CSTD            ?= -std=c99
60
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
67
68 TGT_CPPFLAGS    += -MD
69
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
75 ifeq ($(V),99)
76 TGT_LDFLAGS     += -Wl,--print-gc-sections
77 endif
78
79 LDLIBS          += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group
80
81 include $(OPENCM3_DIR)/mk/genlink-rules.mk
82
83 %.bin: %.elf
84         @printf "  OBJCOPY $(*).bin\n"
85         $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin
86
87 %.elf: $(OBJS) $(UCW_LDSCRIPT)
88         @printf "  LD      $(*).elf\n"
89         $(Q)$(LD) $(TGT_LDFLAGS) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $*.elf
90
91 %.o: %.c
92         @printf "  CC      $(*).c\n"
93         $(Q)$(CC) $(TGT_CFLAGS) $(CFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $@ -c $<
94
95 .PHONY: clean
96 clean:
97         @printf "  CLEAN\n"
98         $(Q)rm -f *.elf *.bin *.o *.d *.map $(LDSCRIPT) $(UCW_LDSCRIPT)
99
100 ifdef WITH_DFU_FLASH
101
102 %.flash: %.bin
103         @printf "  FLASH  $<\n"
104         $(Q)dfu-util -a2 -D $(*).bin
105
106 else
107
108 %.flash: %.bin
109         @printf "  FLASH  $<\n"
110         $(Q)$(ROOT)/bin/st-flash write $(*).bin 0x8000000
111
112 .PHONY: reset
113 reset: all
114         $(ROOT)/bin/st-flash reset
115
116 endif
117
118 .SECONDEXPANSION:
119 .SECONDARY:
120
121 -include $(OBJS:.o=.d)