blob: 87534d813ebe0f546cdc0afe33d90cb17a52b0b1 [file] [log] [blame]
Anas Nashif51be9a52016-01-15 12:18:53 -05001#!/bin/sh
2
Anas Nashifa49762c2016-02-01 22:24:21 -05003# This script is loosly based on a script with same purpose provided
4# by RIOT-OS (https://github.com/RIOT-OS/RIOT)
5
Anas Nashif51be9a52016-01-15 12:18:53 -05006OPENOCD_CMD="${OPENOCD:-openocd} -s ${OPENOCD_DEFAULT_PATH}"
Kumar Gala8d357602016-10-19 15:13:41 -05007OPENOCD_CONFIG=${ZEPHYR_BASE}/boards/${ARCH}/${BOARD_NAME}/support/openocd.cfg
Anas Nashif51be9a52016-01-15 12:18:53 -05008BIN_NAME=${O}/${KERNEL_BIN_NAME}
Anas Nashifa49762c2016-02-01 22:24:21 -05009ELF_NAME=${O}/${KERNEL_ELF_NAME}
Anas Nashif51be9a52016-01-15 12:18:53 -050010
11test_config() {
12 if [ ! -f "${OPENOCD_CONFIG}" ]; then
13 echo "Error: Unable to locate OpenOCD configuration file: ${OPENOCD_CONFIG}"
14 exit 1
15 fi
16 if [ ! -f "${OPENOCD}" ]; then
17 echo "Error: Unable to locate OpenOCD executable: ${OPENOCD}"
18 exit 1
19 fi
20}
21
22test_bin() {
23 if [ ! -f "${BIN_NAME}" ]; then
24 echo "Error: Unable to locate image binary: ${BIN_NAME}"
25 exit 1
26 fi
27}
28
29do_flash() {
30 test_config
31 test_bin
32
33 # flash device with specified image
34 sh -c "${OPENOCD_CMD} -f '${OPENOCD_CONFIG}' \
35 -c 'init' \
36 -c 'targets' \
37 ${OPENOCD_PRE_CMD} \
38 -c 'reset halt' \
39 -c ${OPENOCD_LOAD_CMD} \
40 -c 'reset halt' \
41 -c ${OPENOCD_VERIFY_CMD} \
42 ${OPENOCD_POST_CMD} \
43 -c 'reset run' \
44 -c 'shutdown'"
45 echo 'Done flashing'
46}
47
Anas Nashifa49762c2016-02-01 22:24:21 -050048
49do_debug() {
50 test_config
51 test_bin
52 # setsid is needed so that Ctrl+C in GDB doesn't kill OpenOCD
53 [ -z "${SETSID}" ] && SETSID="$(which setsid)"
54 # temporary file that saves OpenOCD pid
55 OCD_PIDFILE=$(mktemp -t "openocd_pid.XXXXXXXXXX")
56 # cleanup after script terminates
57 trap "cleanup ${OCD_PIDFILE}" EXIT
58 # don't trap on Ctrl+C, because GDB keeps running
59 trap '' INT
60 # start OpenOCD as GDB server
61 ${SETSID} sh -c "${OPENOCD_CMD} -f '${OPENOCD_CONFIG}' \
62 ${OPENOCD_EXTRA_INIT} \
63 -c 'tcl_port ${TCL_PORT:-6333}' \
64 -c 'telnet_port ${TELNET_PORT:-4444}' \
65 -c 'gdb_port ${GDB_PORT:-3333}' \
66 -c 'init' \
67 -c 'targets' \
68 -c 'halt' \
69 & \
70 echo \$! > $OCD_PIDFILE" &
71 # connect to the GDB server
72 ${GDB} ${TUI} -ex "target remote :${GDB_PORT:-3333}" ${ELF_NAME}
73 # will be called by trap
74 cleanup() {
75 OCD_PID="$(cat $OCD_PIDFILE)"
76 kill ${OCD_PID} &>/dev/null
77 rm -f "$OCD_PIDFILE"
78 exit 0
79 }
80}
81
Anas Nashif54634d62016-02-01 19:42:54 -050082do_debugserver() {
83 test_config
Anas Nashif54634d62016-02-01 19:42:54 -050084 sh -c "${OPENOCD_CMD} -f '${OPENOCD_CONFIG}' \
85 -c 'init' \
86 -c 'targets' \
87 -c 'reset halt'"
88}
Anas Nashif51be9a52016-01-15 12:18:53 -050089
90CMD="$1"
91shift
92
93case "${CMD}" in
94 flash)
95 echo "Flashing Target Device"
96 do_flash "$@"
97 ;;
Anas Nashif54634d62016-02-01 19:42:54 -050098 debugserver)
99 do_debugserver "$@"
100 ;;
Anas Nashifa49762c2016-02-01 22:24:21 -0500101 debug)
102 do_debug "$@"
103 ;;
Anas Nashif51be9a52016-01-15 12:18:53 -0500104esac