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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
#############################################################
# Configuration
#############################################################
# Allows users to create Makefile.local or ../Makefile.project with
# configuration variables, so they don't have to be set on the command-line
# every time.
extra_configs := $(wildcard Makefile.local ../Makefile.project)
ifneq ($(extra_configs),)
$(info Obtaining additional make variables from $(extra_configs))
include $(extra_configs)
endif
# Select Legacy BSP or Freedom Metal BSP
# Allowed values are 'legacy' and 'mee'
BSP ?= legacy
ifeq ($(BSP),legacy)
BSP_SUBDIR ?= env
BOARD ?= freedom-e300-hifive1
PROGRAM ?= demo_gpio
LINK_TARGET ?= flash
GDB_PORT ?= 3333
else # MEE
BSP = mee
BSP_SUBDIR ?=
PROGRAM ?= hello
BOARD ?= sifive-hifive1
endif # $(BSP)
BOARD_ROOT ?= $(abspath .)
PROGRAM_ROOT ?= $(abspath .)
SRC_DIR = $(PROGRAM_ROOT)/software/$(PROGRAM)
PROGRAM_ELF = $(SRC_DIR)/$(PROGRAM)
PROGRAM_HEX = $(SRC_DIR)/$(PROGRAM).hex
#############################################################
# BSP Loading
#############################################################
# Finds the directory in which this BSP is located, ensuring that there is
# exactly one.
BSP_DIR := $(wildcard $(BOARD_ROOT)/bsp/$(BSP_SUBDIR)/$(BOARD))
ifeq ($(words $(BSP_DIR)),0)
$(error Unable to find BSP for $(BOARD), expected to find either "bsp/$(BOARD)" or "bsp-addons/$(BOARD)")
endif
ifneq ($(words $(BSP_DIR)),1)
$(error Found multiple BSPs for $(BOARD): "$(BSP_DIR)")
endif
#############################################################
# Standalone Script Include
#############################################################
# The standalone script is included here because it needs $(SRC_DIR) and
# $(BSP_DIR) to be set.
#
# The standalone Makefile handles the following tasks:
# - Including $(BSP_DIR)/settings.mk and validating RISCV_ARCH, RISCV_ABI
# - Setting the toolchain path with CROSS_COMPILE and RISCV_PATH
# - Providing the software and $(PROGRAM_ELF) Make targets for the MEE
include scripts/standalone.mk
#############################################################
# Prints help message
#############################################################
.PHONY: help
help:
@echo " SiFive Freedom E Software Development Kit "
@echo " Makefile targets:"
@echo ""
@echo " software BSP=mee [PROGRAM=$(PROGRAM) BOARD=$(BOARD)]:"
@echo " Build a software program to load with the"
@echo " debugger."
@echo ""
@echo " mee BSP=mee [BOARD=$(BOARD)]"
@echo " Build the MEE library for BOARD"
@echo ""
@echo " clean BSP=mee [PROGRAM=$(PROGRAM) BOARD=$(BOARD)]:"
@echo " Clean compiled objects for a specified "
@echo " software program."
@echo ""
@echo " upload BSP=mee [PROGRAM=$(PROGRAM) BOARD=$(BOARD)]:"
@echo " Launch OpenOCD to flash your program to the"
@echo " on-board Flash."
@echo ""
@echo " debug BSP=mee [PROGRAM=$(PROGRAM) BOARD=$(BOARD)]:"
@echo " Launch OpenOCD and attach GDB to the running program."
@echo ""
@echo " standalone BSP=mee STANDALONE_DEST=/path/to/desired/location"
@echo " [PROGRAM=$(PROGRAM) BOARD=$(BOARD)]:"
@echo " Export a program for a single target into a standalone"
@echo " project directory at STANDALONE_DEST."
@echo ""
@echo " For more information, read the accompanying README.md"
.PHONY: clean
clean:
#############################################################
# Enumerate MEE BSPs and Programs
#
# List all available MEE boards and programs in a form that
# Freedom Studio knows how to parse. Do not change the
# format or fixed text of the output without consulting the
# Freedom Studio dev team.
#############################################################
ifeq ($(BSP),mee)
EXCLUDE_BOARD_DIRS = drivers env include libwrap update-targets.sh
# MEE boards are any folders that aren't the Legacy BSP or update-targets.sh
list-boards:
@echo bsp-list: $(sort $(filter-out $(EXCLUDE_BOARD_DIRS),$(notdir $(wildcard bsp/*))))
# MEE programs are any submodules in the software folder
list-programs:
@echo program-list: $(shell grep -o '= software/.*$$' .gitmodules | sed -r 's/.*\///')
list-options: list-programs list-boards
@echo done
endif
#############################################################
# Compiles an instance of the MEE targeted at $(BOARD)
#############################################################
ifeq ($(BSP),mee)
MEE_SOURCE_PATH ?= freedom-mee
MEE_LDSCRIPT = $(BSP_DIR)/mee.lds
MEE_HEADER = $(BSP_DIR)/mee.h
.PHONY: mee
mee: $(BSP_DIR)/install/stamp
$(BSP_DIR)/build/Makefile:
@rm -rf $(dir $@)
@mkdir -p $(dir $@)
cd $(dir $@) && \
CFLAGS="-march=$(RISCV_ARCH) -mabi=$(RISCV_ABI) -g -mcmodel=medany" \
$(abspath $(MEE_SOURCE_PATH)/configure) \
--host=$(CROSS_COMPILE) \
--prefix=$(abspath $(BSP_DIR)/install) \
--disable-maintainer-mode \
--with-preconfigured \
--with-machine-name=$(BOARD) \
--with-machine-header=$(abspath $(MEE_HEADER)) \
--with-machine-ldscript=$(abspath $(MEE_LDSCRIPT)) \
--with-builtin-libgloss
touch -c $@
$(BSP_DIR)/install/stamp: $(BSP_DIR)/build/Makefile
$(MAKE) -C $(abspath $(BSP_DIR)/build) install
date > $@
$(BSP_DIR)/install/lib/libriscv%.a: $(BSP_DIR)/install/stamp ;@:
$(BSP_DIR)/install/lib/libmee.a: $(BSP_DIR)/install/lib/libriscv__mmachine__$(BOARD).a
cp $< $@
$(BSP_DIR)/install/lib/libmee-gloss.a: $(BSP_DIR)/install/lib/libriscv__menv__mee.a
cp $< $@
.PHONY: clean-mee
clean-mee:
rm -rf $(BSP_DIR)/install
rm -rf $(BSP_DIR)/build
clean: clean-mee
endif
mee_install: mee
$(MAKE) -C $(MEE_SOURCE_PATH) install
#############################################################
# elf2hex
#############################################################
scripts/elf2hex/build/Makefile: scripts/elf2hex/configure
@rm -rf $(dir $@)
@mkdir -p $(dir $@)
cd $(dir $@); \
$(abspath $<) \
--prefix=$(abspath $(dir $<))/install \
--target=$(CROSS_COMPILE)
scripts/elf2hex/install/bin/$(CROSS_COMPILE)-elf2hex: scripts/elf2hex/build/Makefile
$(MAKE) -C $(dir $<) install
touch -c $@
.PHONY: clean-elf2hex
clean-elf2hex:
rm -rf scripts/elf2hex/build scripts/elf2hex/install
clean: clean-elf2hex
#############################################################
# Standalone Project Export
#############################################################
ifeq ($(BSP),mee)
ifeq ($(STANDALONE_DEST),)
standalone:
$(error Please provide STANDALONE_DEST to create a standalone project)
else
$(STANDALONE_DEST):
$(STANDALONE_DEST)/%:
mkdir -p $@
# We have to use $$(shell ls ...) in this recipe instead of $$(wildcard) so that we
# pick up $$(BSP_DIR)/install
standalone: \
$(STANDALONE_DEST) \
$(STANDALONE_DEST)/bsp \
$(STANDALONE_DEST)/src \
$(BSP_DIR)/install/lib/libmee.a \
$(BSP_DIR)/install/lib/libmee-gloss.a \
$(SRC_DIR) \
scripts/standalone.mk
cp -r $(addprefix $(BSP_DIR)/,$(filter-out build,$(shell ls $(BSP_DIR)))) $</bsp/
$(MAKE) -C $(SRC_DIR) clean
cp -r $(SRC_DIR)/* $</src/
echo "PROGRAM = $(PROGRAM)" > $</Makefile
cat scripts/standalone.mk >> $</Makefile
endif
endif
#############################################################
# MEE Software Compilation
#############################################################
# Generation of $(PROGRAM_ELF) is handled by scripts/standalone.mk
# In this top level Makefile, just describe how to turn the elf into
# $(PROGRAM_HEX)
ifeq ($(BSP),mee)
$(PROGRAM_HEX): \
scripts/elf2hex/install/bin/$(CROSS_COMPILE)-elf2hex \
$(PROGRAM_ELF)
$< --output $@ --input $(PROGRAM_ELF) --bit-width $(COREIP_MEM_WIDTH)
endif
#############################################################
# Legacy Software Compilation
#############################################################
ifeq ($(BSP),legacy)
PROGRAM_DIR=$(dir $(PROGRAM_ELF))
.PHONY: software_clean
clean: software_clean
software_clean:
$(MAKE) -C $(PROGRAM_DIR) CC=$(RISCV_GCC) RISCV_ARCH=$(RISCV_ARCH) RISCV_ABI=$(RISCV_ABI) AR=$(RISCV_AR) BSP_BASE=$(abspath bsp) BOARD=$(BOARD) LINK_TARGET=$(LINK_TARGET) clean
.PHONY: software
software: software_clean
$(MAKE) -C $(PROGRAM_DIR) CC=$(RISCV_GCC) RISCV_ARCH=$(RISCV_ARCH) RISCV_ABI=$(RISCV_ABI) AR=$(RISCV_AR) BSP_BASE=$(abspath bsp) BOARD=$(BOARD) LINK_TARGET=$(LINK_TARGET)
dasm: software $(RISCV_OBJDUMP)
$(RISCV_OBJDUMP) -D $(PROGRAM_ELF)
endif
#############################################################
# Upload and Debug
#############################################################
ifeq ($(BSP),mee)
upload: $(PROGRAM_ELF)
scripts/upload --elf $(PROGRAM_ELF) --openocd $(RISCV_OPENOCD) --gdb $(RISCV_GDB) --openocd-config bsp/$(BOARD)/openocd.cfg
debug: $(PROGRAM_ELF)
scripts/debug --elf $(PROGRAM_ELF) --openocd $(RISCV_OPENOCD) --gdb $(RISCV_GDB) --openocd-config bsp/$(BOARD)/openocd.cfg
else # BSP != mee
OPENOCDCFG ?= bsp/env/$(BOARD)/openocd.cfg
OPENOCDARGS += -f $(OPENOCDCFG)
GDB_UPLOAD_ARGS ?= --batch
GDB_UPLOAD_CMDS += -ex "set remotetimeout 240"
GDB_UPLOAD_CMDS += -ex "target extended-remote localhost:$(GDB_PORT)"
GDB_UPLOAD_CMDS += -ex "monitor reset halt"
GDB_UPLOAD_CMDS += -ex "monitor flash protect 0 64 last off"
GDB_UPLOAD_CMDS += -ex "load"
GDB_UPLOAD_CMDS += -ex "monitor resume"
GDB_UPLOAD_CMDS += -ex "monitor shutdown"
GDB_UPLOAD_CMDS += -ex "quit"
upload:
$(RISCV_OPENOCD) $(OPENOCDARGS) & \
$(RISCV_GDB) $(PROGRAM_DIR)/$(PROGRAM) $(GDB_UPLOAD_ARGS) $(GDB_UPLOAD_CMDS) && \
echo "Successfully uploaded '$(PROGRAM)' to $(BOARD)."
#############################################################
# This Section is for launching the debugger
#############################################################
run_openocd:
$(RISCV_OPENOCD) $(OPENOCDARGS)
GDBCMDS += -ex "set remotetimeout 240"
GDBCMDS += -ex "target extended-remote localhost:$(GDB_PORT)"
run_gdb:
$(RISCV_GDB) $(PROGRAM_DIR)/$(PROGRAM) $(GDBARGS) $(GDBCMDS)
endif # BSP == mee
|