-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
198 lines (139 loc) · 3.6 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
# Target / project Name
NAME = blink
# Startup files
OPENOCD_CFG = stm32f0.cfg
LDSCRIPT = stm32f0.ld
STARTUP = startup_stm32f0xx.s
BUILDDIR = build
OUTFILE = $(BUILDDIR)/$(NAME)
# Target files
OUTPREFIX = $(BUILDDIR)/$(NAME)
HEXFILE = $(OUTPREFIX).hex
ELFFILE = $(OUTPREFIX).elf
BINFILE = $(OUTPREFIX).bin
MAPFILE = $(OUTPREFIX).map
# Lib locations
PERIPH = lib/StdPeriph
CMSIS = lib/CMSIS
PROLIB = lib/project_lib
# Include locations
INCLUDE = -Iinc
INCLUDE += -I$(PERIPH)/inc
INCLUDE += -I$(CMSIS)/Include
INCLUDE += -I$(CMSIS)/Device/ST/STM32F0xx/Include
INCLUDE += -I$(PROLIB)/include
# GCC Tools
ARM_PREFIX = arm-none-eabi
CC = $(ARM_PREFIX)-gcc
LD = $(ARM_PREFIX)-gcc
OBJCPY = $(ARM_PREFIX)-objcopy
SIZE = $(ARM_PREFIX)-size
GDB = $(ARM_PREFIX)-gdb
GDBTUI = $(ARM_PREFIX)-gdbtui
# Other Tools
OPENOCD = openocd
# MCU Related flags
MCUFLAGS = -mlittle-endian -mcpu=cortex-m0
MCUFLAGS += -mthumb -mfloat-abi=soft
# Optimization flags
OPTIMIZE = -Os
# ASMFlags
AFLAGS += $(INCLUDE) $(MCUFLAGS)
# CFlags
CFLAGS += -std=c99
CFLAGS += -Wall -Wextra -Warray-bounds
CFLAGS += -ffunction-sections -fdata-sections # Remove unused functions
CFLAGS += $(INCLUDE) $(MCUFLAGS)
# LDFlags
LDFLAGS += -T$(LDSCRIPT)
LDFLAGS += -Wl,--gc-sections,-Map=$(MAPFILE) # Remove unused functions
LDFLAGS += -nostartfiles
LDFLAGS += -specs=nosys.specs
LDFLAGS += -lrdimon
LDFLAGS += -static
LDFLAGS += -specs=rdimon.specs
LDFLAGS += -specs=nano.specs
# Libs need to be at the end
LDLIBS += -Llib -lstm32f0_periph
# Terminal Colors
vpath %.c src/
vpath %.s src/
############### Debugging ############
#
# Use `make DEBUG=1` to enable debugging
#
######################################
DEBUG ?= 1
ifeq ($(DEBUG), 1)
CFLAGS += -O0 -ggdb
AFLAGS += -O0 -ggdb
DEFS += -DDEBUG
else
CFLAGS += $(OPTIMIZE)
AFLAGS += $(OPTIMIZE)
endif
############### Colors ##############
#
# Use `make COLORS=0` to disable colors
#
#####################################
COLOR ?= 1
ifeq ($(COLOR), 1)
NO_COLOR = \x1b[0m
GREEN_COLOR = \x1b[32;01m
YELLOW_COLOR = \x1b[33;01m
RED_COLOR = \x1b[31;01m
BLUE_COLOR = \x1b[34;01m
CYAN_COLOR = \x1b[36;01m
endif
#
# Source Files
#
#### Dont touch ########
CSRC = system_stm32f0xx.c stm32f0xx_it.c
ASRC = $(STARTUP)
#######################
#
# Add sources here
# CSRC : C Source files
# ASRC : ASM Source files
#######################
CSRC += main.c
ASRC += delay.s
###### C Defs
DEFS = -D "USE_STDPERIPH_DRIVER"
DEFS +=
#####
AOBJS += $(patsubst %, $(BUILDDIR)/%,$(ASRC:.s=.o))
COBJS = $(patsubst %, $(BUILDDIR)/%,$(CSRC:.c=.o))
all: setup build size
setup:
@mkdir -p $(BUILDDIR)
build: setup $(COBJS) $(AOBJS)
@echo -e 'LINKING: $(COBJS)$(AOBJS)'
$(LD) $(CFLAGS) $(LDFLAGS) -o $(ELFFILE) $(COBJS) $(AOBJS) $(LDLIBS)
@$(OBJCPY) -O ihex $(ELFFILE) $(HEXFILE)
@$(OBJCPY) -O binary $(ELFFILE) $(BINFILE)
clean:
rm -rf $(BUILDDIR)
flash: build
$(OPENOCD) -f $(OPENOCD_CFG) -c "stm_flash $(BINFILE)"
erase:
$(OPENOCD) -f $(OPENOCD_CFG) -c "stm_erase"
debug: _startopenocd _gdb _killopenocd
size: build
@echo -e 'SIZE:\t\t$(BLUE_COLOR)$(ELFFILE)'
@$(SIZE) $(ELFFILE)
_startopenocd: $(BIN)
openocd -f $(OPENOCD_CFG) -c "halt" 1>/dev/null 2>/dev/null &
_killopenocd:
@echo "shutdown" | nc localhost 4444 1>/dev/null 2>/dev/null
_gdb:
$(GDB) --eval-command="target remote localhost:3333" $(ELF)
########### Compile / Assemble
$(COBJS): $(BUILDDIR)/%.o : %.c
@echo -e 'COMPILING:'
$(CC) $(CFLAGS) $(DEFS) -c $< -o $@
$(AOBJS): $(BUILDDIR)/%.o : %.s
@echo -e 'ASSEMBLING:'
$(CC) $(AFLAGS) $(DEFS) -c $< -o $@