-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
219 lines (172 loc) · 5.75 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Makefile for STM32F103C8
#
################################################################################
#####
##### PROJECT
#####
##### The name of the project used as the build target name
#####
##### - Explicitly configure project name;
##### - Create project name from name of parent directory;
PROJECT = ln
#PROJECT = $(lastword $(subst /, ,$(shell cd ..;pwd)))
################################################################################
#####
##### Project target
#####
ARMCPU = cortex-m3
STM32MCU = STM32F103xB
################################################################################
#####
##### include master makefile
#####
## kludge to handle directory names with spaces
SPACECHAR =
SPACECHAR +=
BASEPATH = $(subst $(SPACECHAR),\ ,$(shell cd ..;pwd))
################################################################################
#####
##### Toolchain paths
#####
##### These paths need to point to the location of your tools installation.
#####
##### BUILDTOOLPATH : The location where the ARM-GCC binaries are located
##### FLASHTOOLPATH : The location where the tools used for flashing are located,
##### st-flash, stm32flash, stm32loader, openocd, etc.
#####
BUILDTOOLPATH = /opt/homebrew/bin/
FLASHTOOLPATH = /opt/homebrew/bin
################################################################################
#####
##### CMSIS / Device Directory Layout
#####
##### Paths and locations of the CMSIS drivers from ST.
#####
##### See https://github.com/getoffmyhack/STM32-CMSIS
CMSISBASE = ./CMSIS
CMSISINC = $(CMSISBASE)/include
# Device specific, change to either IF-THEN or makefile includes
DEVICEINC = $(CMSISBASE)/Device/STM32F1xx/include
DEVICESRC = $(CMSISBASE)/Device/STM32F1xx/src
DEVICESTARTUP = startup_stm32f103xb.s
DEVICELINKER = $(CMSISBASE)/Device/STM32F1xx/linker/STM32F103XB_FLASH.ld
################################################################################
#####
##### Local Project Directory Layout
#####
SRCDIR = src
BINDIR = bin
OBJDIR = obj
INCDIR = include
################################################################################
#####
##### C and ASM source files
#####
SRC = $(wildcard $(SRCDIR)/*.c) $(wildcard $(DEVICESRC)/*.c)
ASM = $(wildcard $(SRCDIR)/*.s) $(DEVICESRC)/$(DEVICESTARTUP)
################################################################################
#####
##### include files
#####
INCLUDE = -I$(INCDIR)
INCLUDE += -I$(CMSISINC)
INCLUDE += -I$(DEVICEINC)
################################################################################
#####
##### compiler flags
#####
CFLAGS = -std=gnu99
CFLAGS += -Wall
CFLAGS += -fno-common
CFLAGS += -mthumb
CFLAGS += -mcpu=$(ARMCPU)
CFLAGS += -D$(STM32MCU)
CFLAGS += -g
CFLAGS += -u _printf_float
CFLAGS += -Wa,-ahlms=$(addprefix $(OBJDIR)/,$(notdir $(<:.c=.lst)))
CFLAGS += $(INCLUDE)
################################################################################
#####
##### linker flags
#####
LDFLAGS = -T$(DEVICELINKER)
LDFLAGS += -mthumb
LDFLAGS += -mcpu=$(ARMCPU)
#LDFLAGS += -specs=rdimon.specs
LDFLAGS += --specs=nosys.specs
LDFLAGS += --specs=nano.specs
LDFLAGS += -lc
LDFLAGS += -u _printf_float
LDFLAGS += -Wl,-Map=$(OBJDIR)/$(PROJECT).map
#LDFLAGS += -lrdimon
################################################################################
#####
##### assembler flags
#####
ASFLAGS += -mcpu=$(ARMCPU)
################################################################################
#####
##### tools
#####
CC = $(BUILDTOOLPATH)/arm-none-eabi-gcc
AS = $(BUILDTOOLPATH)/arm-none-eabi-as
AR = $(BUILDTOOLPATH)/arm-none-eabi-ar
LD = $(BUILDTOOLPATH)/arm-none-eabi-ld
GDB = $(BUILDTOOLPATH)/arm-none-eabi-gdb
GDBPY = $(BUILDTOOLPATH)/arm-none-eabi-gdb-py
OBJCOPY = $(BUILDTOOLPATH)/arm-none-eabi-objcopy
SIZE = $(BUILDTOOLPATH)/arm-none-eabi-size
OBJDUMP = $(BUILDTOOLPATH)/arm-none-eabi-objdump
SWDFLASH = $(FLASHTOOLPATH)/st-flash
SERFLASH = $(FLASHTOOLPATH)/stm32flash
RM = rm -rf
## Build process
OBJ := $(addprefix $(OBJDIR)/,$(notdir $(SRC:.c=.o)))
OBJ += $(addprefix $(OBJDIR)/,$(notdir $(ASM:.s=.o)))
all: $(BINDIR)/$(PROJECT).bin
print-% : ; @echo $* = $($*)
swdflash: $(BINDIR)/$(PROJECT).bin
$(SWDFLASH) write $(BINDIR)/$(PROJECT).bin 0x8000000
serflash: $(BINDIR)/$(PROJECT).bin
@read -p "Press --RESET-- with Boot0 = 1 and press <CR>" dummy;
$(SERFLASH) -b 230400 -w $(BINDIR)/$(PROJECT).bin -g 0 /dev/tty.usbserial-*
debug: $(BINDIR)/$(PROJECT).bin
$(GDBPY) --command $(BASEPATH)/gdb/debug.gdb $(BINDIR)/$(PROJECT).elf
macros:
$(CC) $(GCFLAGS) -dM -E - < /dev/null
clean:
$(RM) $(BINDIR)
$(RM) $(OBJDIR)
################################################################################
#####
##### Compile / assemble project files
#####
$(OBJDIR)/%.o: $(SRCDIR)/%.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/%.o: $(SRCDIR)/%.s
@mkdir -p $(dir $@)
$(AS) $(ASFLAGS) -o $@ $<
################################################################################
#####
##### Compile / assemble CMSIS and Device files
#####
$(OBJDIR)/%.o: $(DEVICESRC)/%.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/%.o: $(DEVICESRC)/%.s
@mkdir -p $(dir $@)
$(AS) $(ASFLAGS) -o $@ $<
################################################################################
#####
##### Link binaries
#####
$(BINDIR)/$(PROJECT).hex: $(BINDIR)/$(PROJECT).elf
$(OBJCOPY) -R .stack -O ihex $(BINDIR)/$(PROJECT).elf $(BINDIR)/$(PROJECT).hex
$(BINDIR)/$(PROJECT).bin: $(BINDIR)/$(PROJECT).elf
$(OBJCOPY) -R .stack -O binary $(BINDIR)/$(PROJECT).elf $(BINDIR)/$(PROJECT).bin
$(BINDIR)/$(PROJECT).elf: $(OBJ)
@mkdir -p $(dir $@)
$(CC) $(OBJ) $(LDFLAGS) -o $(BINDIR)/$(PROJECT).elf
$(OBJDUMP) -D $(BINDIR)/$(PROJECT).elf > $(BINDIR)/$(PROJECT).lst
$(SIZE) $(BINDIR)/$(PROJECT).elf