-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
183 lines (143 loc) · 4 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
# Makefile for compiling executables onto the stm32f0
#
#Verbosity flags (Q=quiet, E=echo)
E = @echo
ifdef VERBOSE
Q =
else
Q = @
endif
include mcu.mk
# Define result paths
BUILD_PATH := bin
OBJ_PATH := $(BUILD_PATH)/obj
# Define dependencies and targets
# Define native test dependencies and variables
include ntv.mk
# Note using .native.o to separate from mcu compiled object files
_NTV_OBJ := $(NTV_SRC:.c=.native.o)
NTV_OBJ := $(patsubst %,$(OBJ_PATH)/%,$(_NTV_OBJ))
NTV_TEST_TARGET := $(BUILD_PATH)/test_natively
# Define cross-compiled dependencies and variables
include hw.mk
_HW_OBJ := $(HW_SRC:.c=.o)
_HW_OBJ += $(MCU_STARTUP:.s=.o)
HW_OBJ := $(patsubst %,$(OBJ_PATH)/%,$(_HW_OBJ))
# Define cross-compiled targets
CROSS_TARGET := $(BUILD_PATH)/hw_binary.bin
CROSS_HEX := $(CROSS_TARGET:.bin=.hex)
CROSS_ELF := $(CROSS_TARGET:.bin=.elf)
MAP_FILE := $(BUILD_PATH)/mapfile.map
# Define commands necessary for creating targets
# Native commands
NTV_CC:=gcc
NTV_LD:=$(NTV_CC)
# Cross compile commands
CC_TYPE:=arm-none-eabi
# CC_PATH must be defined in environment!
CC_PREFIX:=$(CC_PATH)/$(CC_TYPE)
HW_CC:=$(CC_PREFIX)-gcc
GDBTUI = $(CC_PREFIX)-gdb
OBJCOPY:=$(CC_PREFIX)-objcopy
HEX:=$(OBJCOPY) -O ihex
BIN:=$(OBJCOPY) -O binary -S
HW_LD:=$(HW_CC)
HW_AS:=$(HW_CC) -x assembler-with-cpp
# Define compiler options
OPT = # -Os
DEBUG_FLAGS := -g \
-gdwarf-2
BASE_CFLAGS := -std=c99 \
-Wall \
$(DDEFS) $(OPT)
ARFLAGS := r
# Define native options
NTV_CFLAGS := $(BASE_CFLAGS) \
$(NTV_CPPFLAGS) \
$(DEBUG_FLAGS)
NTV_LDFLAGS := $(DEBUG_FLAGS)
# Define cross-compiler options
HW_ASFLAGS := $(DEBUG_FLAGS) $(MCU_ASFLAGS)
HW_CFLAGS := $(BASE_CFLAGS) \
$(HW_CPPFLAGS) \
$(MCU_CFLAGS) \
$(DEBUG_FLAGS)
HW_LDFLAGS := $(DEBUG_FLAGS) \
$(MCU_LDFLAGS) \
-Wl,-Map=$(MAP_FILE),--cref,--no-warn-mismatch
# Optionally turn on listings
# -Wa passes comma separated list of arguments onto assembler
# -a (turns on listings)
# m: include macro expansions
# h: include high-level source
# l: include assembly
# s: include symbols
# =: list to file
ifdef VERBOSE
HW_CFLAGS += -Wa,-amhls=$(<:.c=.lst)
HW_ASFLAGS += -Wa,-amhls=$(<:.s=.lst)
NTV_CFLAGS += -Wa,-amhls=$(<:.c=.lst)
NTV_ASFLAGS += -Wa,-amhls=$(<:.s=.lst)
endif
# Make commands
.PHONY: all
all: $(CROSS_TARGET)
.PHONY: test
test: $(NTV_TEST_TARGET)
./$(NTV_TEST_TARGET)
.PHONY: help
help:
$(E)
$(E)"all: Create the hw binary $(CROSS_TARGET)"
$(E)"test: Create a native test binary and run it"
$(E)"flash: Flash the hw binary to the chip"
$(E)"erase: Erase the flash data on the chip (useful for getting out of error state)"
$(E)"debug: Run gdb remotely on the chip"
$(E)"serial: Open up a serial communication (must setup hardware appropriately)"
$(E)"clean: Remove any files created by this Makefile"
$(E)
.PHONY: flash
flash: $(CROSS_TARGET)
st-flash write $(CROSS_TARGET) 0x8000000
.PHONY: erase
erase:
st-flash erase
.PHONY: debug
debug: $(CROSS_ELF)
xterm -e st-util &
$(GDBTUI) --eval-command="target remote localhost:4242" $(CROSS_ELF) -ex 'load'
# Open up a serial connection
.PHONY: serial
serial:
minicom -c on
.PHONY: clean
clean:
rm -rf $(BUILD_PATH)
# Create all of the objects
# $^ is shorthand for all of the dependencies
# $< is shorthand for the first dependency
# $@ is shorthand for the target
$(OBJ_PATH)/%.native.o: %.c
$(E)$(notdir $(NTV_CC)) compiling $(notdir $<) to $@
$(Q)mkdir -p `dirname $@`
$(Q)$(NTV_CC) -o $@ $< $(NTV_CFLAGS) -c
$(OBJ_PATH)/%.o: %.c
$(E)$(notdir $(HW_CC)) compiling $(notdir $<) to $@
$(Q)mkdir -p `dirname $@`
$(Q)$(HW_CC) -o $@ $< $(HW_CFLAGS) -c
$(OBJ_PATH)/%.o: %.s
$(E)$(notdir $(AS)) assembling $(notdir $<) to $@
$(Q)mkdir -p `dirname $@`
$(Q)$(HW_AS) -c $(HW_ASFLAGS) $< -o $@
$(CROSS_TARGET): $(CROSS_ELF)
$(E)Building $@
$(Q)$(BIN) $< $@
$(CROSS_HEX): $(CROSS_ELF)
$(E)Building $@
$(Q)$(HEX) $< $@
$(CROSS_ELF): $(HW_OBJ)
$(E)$(notdir $(HW_LD)) linking $@
$(Q)$(HW_LD) $(HW_LDFLAGS) -o $@ $^
$(NTV_TEST_TARGET): $(NTV_OBJ)
$(E)$(notdir $(NTV_LD)) linking $@
$(Q)$(NTV_LD) $(NTV_LDFLAGS) -o $@ $^