Anas Nashif | 51be9a5 | 2016-01-15 12:18:53 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Anas Nashif | a49762c | 2016-02-01 22:24:21 -0500 | [diff] [blame] | 3 | # This script is loosly based on a script with same purpose provided |
| 4 | # by RIOT-OS (https://github.com/RIOT-OS/RIOT) |
| 5 | |
Anas Nashif | 51be9a5 | 2016-01-15 12:18:53 -0500 | [diff] [blame] | 6 | OPENOCD_CMD="${OPENOCD:-openocd} -s ${OPENOCD_DEFAULT_PATH}" |
Kumar Gala | 8d35760 | 2016-10-19 15:13:41 -0500 | [diff] [blame] | 7 | OPENOCD_CONFIG=${ZEPHYR_BASE}/boards/${ARCH}/${BOARD_NAME}/support/openocd.cfg |
Anas Nashif | 51be9a5 | 2016-01-15 12:18:53 -0500 | [diff] [blame] | 8 | BIN_NAME=${O}/${KERNEL_BIN_NAME} |
Anas Nashif | a49762c | 2016-02-01 22:24:21 -0500 | [diff] [blame] | 9 | ELF_NAME=${O}/${KERNEL_ELF_NAME} |
Anas Nashif | 51be9a5 | 2016-01-15 12:18:53 -0500 | [diff] [blame] | 10 | |
| 11 | test_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 | |
| 22 | test_bin() { |
| 23 | if [ ! -f "${BIN_NAME}" ]; then |
| 24 | echo "Error: Unable to locate image binary: ${BIN_NAME}" |
| 25 | exit 1 |
| 26 | fi |
| 27 | } |
| 28 | |
| 29 | do_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 Nashif | a49762c | 2016-02-01 22:24:21 -0500 | [diff] [blame] | 48 | |
| 49 | do_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 Nashif | 54634d6 | 2016-02-01 19:42:54 -0500 | [diff] [blame] | 82 | do_debugserver() { |
| 83 | test_config |
Anas Nashif | 54634d6 | 2016-02-01 19:42:54 -0500 | [diff] [blame] | 84 | sh -c "${OPENOCD_CMD} -f '${OPENOCD_CONFIG}' \ |
| 85 | -c 'init' \ |
| 86 | -c 'targets' \ |
| 87 | -c 'reset halt'" |
| 88 | } |
Anas Nashif | 51be9a5 | 2016-01-15 12:18:53 -0500 | [diff] [blame] | 89 | |
| 90 | CMD="$1" |
| 91 | shift |
| 92 | |
| 93 | case "${CMD}" in |
| 94 | flash) |
| 95 | echo "Flashing Target Device" |
| 96 | do_flash "$@" |
| 97 | ;; |
Anas Nashif | 54634d6 | 2016-02-01 19:42:54 -0500 | [diff] [blame] | 98 | debugserver) |
| 99 | do_debugserver "$@" |
| 100 | ;; |
Anas Nashif | a49762c | 2016-02-01 22:24:21 -0500 | [diff] [blame] | 101 | debug) |
| 102 | do_debug "$@" |
| 103 | ;; |
Anas Nashif | 51be9a5 | 2016-01-15 12:18:53 -0500 | [diff] [blame] | 104 | esac |