-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathjlink.sh
executable file
·301 lines (281 loc) · 8.24 KB
/
jlink.sh
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
#!/bin/sh
#
# Unified Segger JLink script for RIOT
#
# This script is supposed to be called from RIOTs make system,
# as it depends on certain environment variables. An
#
# Global environment variables used:
# JLINK: JLink command name, default: "JLinkExe"
# JLINK_SERVER: JLink GCB server command name, default: "JLinkGDBDerver"
# JLINK_DEVICE: Device identifier used by JLink
# JLINK_SERIAL: Device serial used by JLink
# JLINK_IF: Interface used by JLink, default: "SWD"
# JLINK_SPEED: Interface clock speed to use (in kHz), default "2000"
# FLASH_ADDR: Starting address of the target's flash memory, default: "0"
# IMAGE_OFFSET: Offset from the targets flash memory, for flashing the image
# JLINK_PRE_FLASH: Additional JLink commands to execute before flashing
# JLINK_POST_FLASH: Additional JLink commands to execute after flashing
#
# The script supports the following actions:
#
# flash: flash <binfile>
#
# flash given binary format file to the target.
#
# options:
# <binfile>: path to the binary file that is flashed
#
# debug: debug <elffile>
#
# starts JLink as GDB server in the background and
# connects to the server with the GDB client specified by
# the board (DBG environment variable)
#
# options:
# <elffile>: path to the ELF file to debug
# GDB_PORT: port opened for GDB connections
# TELNET_PORT: port opened for telnet connections
# DBG: debugger client command, default: 'gdb -q'
# TUI: if TUI!=null, the -tui option will be used
#
# debug-server: starts JLink as GDB server, but does not connect to
# to it with any frontend. This might be useful when using
# IDEs.
#
# options:
# GDB_PORT: port opened for GDB connections
# TELNET_PORT: port opened for telnet connections
#
# reset: triggers a hardware reset of the target board
#
#
# term-rtt: opens a serial terminal using jlink RTT(reak time transfer)
#
#
# @author Hauke Peteresen <hauke.petersen@fu-berlin.de>
# Set IMAGE_OFFSET to zero by default.
: ${IMAGE_OFFSET:=0}
# Allow overwriting the reset commands.
: ${JLINK_RESET_FILE:=${RIOTTOOLS}/jlink/reset.seg}
# default GDB port
_GDB_PORT=3333
# default telnet port
_TELNET_PORT=4444
# default JLink command, interface and speed
_JLINK=JLinkExe
_JLINK_SERVER=JLinkGDBServer
_JLINK_IF=SWD
_JLINK_SPEED=2000
# default terminal frontend
_JLINK_TERMPROG=${RIOTTOOLS}/pyterm/pyterm
_JLINK_TERMFLAGS="-ts 19021 ${PYTERMFLAGS}"
#
# a couple of tests for certain configuration options
#
test_config() {
if [ -z "${JLINK}" ]; then
JLINK=${_JLINK}
fi
if [ -z "${JLINK_SERVER}" ]; then
JLINK_SERVER=${_JLINK_SERVER}
fi
if [ -z "${JLINK_IF}" ]; then
JLINK_IF=${_JLINK_IF}
fi
if [ -z "${JLINK_SPEED}" ]; then
JLINK_SPEED=${_JLINK_SPEED}
fi
if [ -z "${JLINK_DEVICE}" ]; then
echo "Error: No target device defined in JLINK_DEVICE env var"
exit 1
fi
if [ -z "${FLASH_ADDR}" ]; then
echo "Error: No flash address defined in FLASH_ADDR env var"
exit 1
fi
}
test_binfile() {
if [ ! -f "${BINFILE}" ]; then
echo "Error: Unable to locate BINFILE"
echo " (${BINFILE})"
exit 1
fi
}
test_ports() {
if [ -z "${GDB_PORT}" ]; then
GDB_PORT=${_GDB_PORT}
fi
if [ -z "${TELNET_PORT}" ]; then
TELNET_PORT=${_TELNET_PORT}
fi
}
test_elffile() {
if [ ! -f "${ELFFILE}" ]; then
echo "Error: Unable to locate ELFFILE"
echo " (${ELFFILE})"
exit 1
fi
}
test_tui() {
if [ -n "${TUI}" ]; then
TUI=-tui
fi
}
test_serial() {
if [ -n "${JLINK_SERIAL}" ]; then
JLINK_SERIAL_SERVER="-select usb='${JLINK_SERIAL}'"
JLINK_SERIAL="-SelectEmuBySN '${JLINK_SERIAL}'"
fi
}
test_dbg() {
if [ -z "${DBG}" ]; then
DBG="${GDB}"
fi
}
test_term() {
if [ -z "${JLINK_TERMPROG}" ]; then
JLINK_TERMPROG="${_JLINK_TERMPROG}"
fi
if [ -z "${JLINK_TERMFLAGS}" ]; then
JLINK_TERMFLAGS="${_JLINK_TERMFLAGS}"
fi
}
#
# now comes the actual actions
#
do_flash() {
BINFILE=$1
test_config
test_serial
test_binfile
# clear any existing contents in burn file
/bin/echo -n "" > ${BINDIR}/burn.seg
# create temporary burn file
if [ ! -z "${JLINK_PRE_FLASH}" ]; then
printf "${JLINK_PRE_FLASH}\n" >> ${BINDIR}/burn.seg
fi
# address to flash is hex formatted, as required by JLink
ADDR_TO_FLASH=$(printf "0x%08x\n" "$((${FLASH_ADDR} + ${IMAGE_OFFSET}))")
echo "loadbin ${BINFILE} ${ADDR_TO_FLASH}" >> ${BINDIR}/burn.seg
if [ ! -z "${JLINK_POST_FLASH}" ]; then
printf "${JLINK_POST_FLASH}\n" >> ${BINDIR}/burn.seg
fi
cat ${JLINK_RESET_FILE} >> ${BINDIR}/burn.seg
# flash device
sh -c "${JLINK} ${JLINK_SERIAL} \
-ExitOnError 1 \
-device '${JLINK_DEVICE}' \
-speed '${JLINK_SPEED}' \
-if '${JLINK_IF}' \
-jtagconf -1,-1 \
-commandfile '${BINDIR}/burn.seg'"
}
do_debug() {
ELFFILE=$1
test_config
test_serial
test_elffile
test_ports
test_tui
test_dbg
# start the JLink GDB server
sh -c "${JLINK_SERVER} ${JLINK_SERIAL_SERVER} \
-device '${JLINK_DEVICE}' \
-speed '${JLINK_SPEED}' \
-if '${JLINK_IF}' \
-port '${GDB_PORT}' \
-telnetport '${TELNET_PORT}'" &
# save PID for terminating the server afterwards
DBG_PID=$?
# connect to the GDB server
${DBG} -q ${TUI} -ex "tar ext :${GDB_PORT}" ${ELFFILE}
# clean up
kill ${DBG_PID}
}
do_debugserver() {
test_ports
test_config
test_serial
# start the JLink GDB server
sh -c "${JLINK_SERVER} ${JLINK_SERIAL_SERVER} \
-device '${JLINK_DEVICE}' \
-speed '${JLINK_SPEED}' \
-if '${JLINK_IF}' \
-port '${GDB_PORT}' \
-telnetport '${TELNET_PORT}'"
}
do_reset() {
test_config
test_serial
# reset the board
sh -c "${JLINK} ${JLINK_SERIAL} \
-ExitOnError 1 \
-device '${JLINK_DEVICE}' \
-speed '${JLINK_SPEED}' \
-if '${JLINK_IF}' \
-jtagconf -1,-1 \
-commandfile '${JLINK_RESET_FILE}'"
}
do_term() {
test_config
test_serial
test_term
# temporary file that save the JLink pid
JLINK_PIDFILE=$(mktemp -t "jilnk_pid.XXXXXXXXXX")
# will be called by trap
cleanup() {
if [ -f $JLINK_PIDFILE ]; then
JLINK_PID="$(cat ${JLINK_PIDFILE})"
kill ${JLINK_PID}
rm -r "${JLINK_PIDFILE}"
fi
exit 0
}
# cleanup after script terminates
trap "cleanup ${JLINK_PIDFILE}" EXIT INT
# start Jlink as RTT server
sh -c "${JLINK} ${JLINK_SERIAL} \
-ExitOnError 1 \
-device '${JLINK_DEVICE}' \
-speed '${JLINK_SPEED}' \
-if '${JLINK_IF}' \
-jtagconf -1,-1 \
-commandfile '${RIOTTOOLS}/jlink/term.seg' >/dev/null & \
echo \$! > $JLINK_PIDFILE" &
sleep 1
sh -c "${JLINK_TERMPROG} ${JLINK_TERMFLAGS}"
}
#
# parameter dispatching
#
ACTION="$1"
shift # pop $1 from $@
case "${ACTION}" in
flash)
echo "### Flashing Target ###"
echo "### Flashing at base address ${FLASH_ADDR} with offset ${IMAGE_OFFSET} ###"
do_flash "$@"
;;
debug)
echo "### Starting Debugging ###"
do_debug "$@"
;;
debug-server)
echo "### Starting GDB Server ###"
do_debugserver "$@"
;;
reset)
echo "### Resetting Target ###"
do_reset "$@"
;;
term-rtt)
echo "### Starting RTT terminal ###"
do_term
;;
*)
echo "Usage: $0 {flash|debug|debug-server|reset|term-rtt}"
echo " flash <binfile>"
echo " debug <elffile>"
;;
esac