2015-02-08 14:05:28 +01:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# Unified OpenOCD script for RIOT
|
|
|
|
#
|
|
|
|
# This script is supposed to be called from RIOTs make system,
|
|
|
|
# as it depends on certain environment variables. An OpenOCD
|
|
|
|
# configuration file must be present in a the boards dist folder.
|
|
|
|
#
|
2015-02-23 10:31:07 +01:00
|
|
|
# Any extra command line arguments after the command name are passed on the
|
|
|
|
# openocd command line after the configuration file name but before any other
|
|
|
|
# initialization commands.
|
|
|
|
#
|
2015-02-18 10:20:26 +01:00
|
|
|
# Global environment variables used:
|
|
|
|
# OPENOCD: OpenOCD command name, default: "openocd"
|
|
|
|
# OPENOCD_CONFIG: OpenOCD configuration file name,
|
|
|
|
# default: "${RIOTBOARD}/${BOARD}/dist/openocd.cfg"
|
|
|
|
#
|
2015-02-08 14:05:28 +01:00
|
|
|
# The script supports the following actions:
|
|
|
|
#
|
|
|
|
# flash: flash a given hexfile to the target.
|
|
|
|
# hexfile is expected in ihex format and is pointed to
|
|
|
|
# by HEXFILE environment variable
|
|
|
|
#
|
|
|
|
# options:
|
|
|
|
# HEXFILE: path to the hexfile that is flashed
|
|
|
|
#
|
|
|
|
# debug: starts OpenOCD as GDB server in the background and
|
|
|
|
# connects to the server with the GDB client specified by
|
|
|
|
# the board (DBG environment variable)
|
|
|
|
#
|
|
|
|
# options:
|
|
|
|
# GDB_PORT: port opened for GDB connections
|
|
|
|
# TCL_PORT: port opened for TCL connections
|
|
|
|
# TELNET_PORT: port opened for telnet connections
|
|
|
|
# TUI: if TUI!=null, the -tui option will be used
|
|
|
|
#
|
|
|
|
# debug-server: starts OpenOCD 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
|
|
|
|
# TCL_PORT: port opened for TCL connections
|
|
|
|
# TELNET_PORT: port opened for telnet connections
|
|
|
|
#
|
|
|
|
# reset: triggers a hardware reset of the target board
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# @author Hauke Peteresen <hauke.petersen@fu-berlin.de>
|
2015-02-17 21:42:52 +01:00
|
|
|
# @author Joakim Gebart <joakim.gebart@eistec.se>
|
2015-02-08 14:05:28 +01:00
|
|
|
|
|
|
|
# default GDB port
|
|
|
|
_GDB_PORT=3333
|
|
|
|
# default telnet port
|
|
|
|
_TELNET_PORT=4444
|
|
|
|
# default TCL port
|
|
|
|
_TCL_PORT=6333
|
2015-02-17 21:42:52 +01:00
|
|
|
# default path to OpenOCD configuration file
|
|
|
|
_OPENOCD_CONFIG=${RIOTBOARD}/${BOARD}/dist/openocd.cfg
|
2015-02-17 21:52:01 +01:00
|
|
|
# default OpenOCD command
|
|
|
|
_OPENOCD=openocd
|
2015-02-08 14:05:28 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# a couple of tests for certain configuration options
|
|
|
|
#
|
|
|
|
test_config() {
|
2015-02-18 10:07:35 +01:00
|
|
|
if [ -z "${OPENOCD}" ]; then
|
2015-02-17 21:52:01 +01:00
|
|
|
OPENOCD=${_OPENOCD}
|
|
|
|
fi
|
2015-02-18 10:07:35 +01:00
|
|
|
if [ -z "${OPENOCD_CONFIG}" ]; then
|
2015-02-17 21:42:52 +01:00
|
|
|
OPENOCD_CONFIG=${_OPENOCD_CONFIG}
|
|
|
|
fi
|
2015-02-18 10:07:35 +01:00
|
|
|
if [ ! -f "${OPENOCD_CONFIG}" ]; then
|
2015-02-08 14:05:28 +01:00
|
|
|
echo "Error: Unable to locate OpenOCD configuration file"
|
2015-02-17 21:42:52 +01:00
|
|
|
echo " (${OPENOCD_CONFIG})"
|
2015-02-08 14:05:28 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
test_hexfile() {
|
2015-02-18 10:07:35 +01:00
|
|
|
if [ ! -f "${HEXFILE}" ]; then
|
2015-02-08 14:05:28 +01:00
|
|
|
echo "Error: Unable to locate HEXFILE"
|
|
|
|
echo " (${HEXFILE})"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
test_elffile() {
|
2015-02-18 10:07:35 +01:00
|
|
|
if [ ! -f "${ELFFILE}" ]; then
|
2015-02-08 14:05:28 +01:00
|
|
|
echo "Error: Unable to locate ELFFILE"
|
|
|
|
echo " (${ELFFILE})"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
test_ports() {
|
2015-02-18 10:07:35 +01:00
|
|
|
if [ -z "${GDB_PORT}" ]; then
|
2015-02-08 14:05:28 +01:00
|
|
|
GDB_PORT=${_GDB_PORT}
|
|
|
|
fi
|
2015-02-18 10:07:35 +01:00
|
|
|
if [ -z "${TELNET_PORT}" ]; then
|
2015-02-08 14:05:28 +01:00
|
|
|
TELNET_PORT=${_TELNET_PORT}
|
|
|
|
fi
|
2015-02-18 10:07:35 +01:00
|
|
|
if [ -z "${TCL_PORT}" ]; then
|
2015-02-08 14:05:28 +01:00
|
|
|
TCL_PORT=${_TCL_PORT}
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
test_tui() {
|
|
|
|
if [ -n "${TUI}" ]; then
|
|
|
|
TUI=-tui
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# now comes the actual actions
|
|
|
|
#
|
|
|
|
do_flash() {
|
|
|
|
test_config
|
|
|
|
test_hexfile
|
|
|
|
# flash device
|
2015-05-23 11:06:55 +02:00
|
|
|
sh -c "${OPENOCD} -f '${OPENOCD_CONFIG}' \
|
|
|
|
${OPENOCD_EXTRA_INIT} \
|
|
|
|
-c 'tcl_port 0' \
|
|
|
|
-c 'telnet_port 0' \
|
|
|
|
-c 'gdb_port 0' \
|
|
|
|
-c 'init' \
|
|
|
|
-c 'targets' \
|
|
|
|
-c 'reset halt' \
|
|
|
|
${OPENOCD_PRE_FLASH_CMDS} \
|
|
|
|
-c 'flash write_image erase \"${HEXFILE}\"' \
|
|
|
|
-c 'reset halt' \
|
|
|
|
${OPENOCD_PRE_VERIFY_CMDS} \
|
|
|
|
-c 'verify_image \"${HEXFILE}\"' \
|
|
|
|
-c 'reset run' \
|
|
|
|
-c 'shutdown'"
|
|
|
|
echo 'Done flashing'
|
2015-02-08 14:05:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
do_debug() {
|
|
|
|
test_config
|
|
|
|
test_elffile
|
|
|
|
test_ports
|
|
|
|
test_tui
|
|
|
|
# start OpenOCD as GDB server
|
2015-05-23 11:06:55 +02:00
|
|
|
sh -c "${OPENOCD} -f '${OPENOCD_CONFIG}' \
|
|
|
|
${OPENOCD_EXTRA_INIT} \
|
|
|
|
-c 'tcl_port ${TCL_PORT}' \
|
|
|
|
-c 'telnet_port ${TELNET_PORT}' \
|
|
|
|
-c 'gdb_port ${GDB_PORT}' \
|
|
|
|
-c 'init' \
|
|
|
|
-c 'targets' \
|
|
|
|
-c 'reset halt' \
|
|
|
|
-l /dev/null" &
|
2015-02-08 14:05:28 +01:00
|
|
|
# save PID for terminating the server afterwards
|
|
|
|
OCD_PID=$?
|
|
|
|
# connect to the GDB server
|
|
|
|
${DBG} ${TUI} -ex "tar ext :${GDB_PORT}" ${ELFFILE}
|
|
|
|
# clean up
|
|
|
|
kill ${OCD_PID}
|
|
|
|
}
|
|
|
|
|
|
|
|
do_debugserver() {
|
|
|
|
test_config
|
|
|
|
test_ports
|
|
|
|
# start OpenOCD as GDB server
|
2015-05-23 11:06:55 +02:00
|
|
|
sh -c "${OPENOCD} -f '${OPENOCD_CONFIG}' \
|
|
|
|
${OPENOCD_EXTRA_INIT} \
|
|
|
|
-c 'tcl_port ${TCL_PORT}' \
|
|
|
|
-c 'telnet_port ${TELNET_PORT}' \
|
|
|
|
-c 'gdb_port ${GDB_PORT}' \
|
|
|
|
-c 'init' \
|
|
|
|
-c 'targets' \
|
|
|
|
-c 'reset halt'"
|
2015-02-08 14:05:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
do_reset() {
|
|
|
|
test_config
|
|
|
|
# start OpenOCD and invoke board reset
|
2015-05-23 11:06:55 +02:00
|
|
|
sh -c "${OPENOCD} -f '${OPENOCD_CONFIG}' \
|
|
|
|
${OPENOCD_EXTRA_INIT} \
|
|
|
|
-c 'tcl_port 0' \
|
|
|
|
-c 'telnet_port 0' \
|
|
|
|
-c 'gdb_port 0' \
|
|
|
|
-c 'init' \
|
|
|
|
-c 'reset run' \
|
|
|
|
-c 'shutdown'"
|
2015-02-08 14:05:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# parameter dispatching
|
|
|
|
#
|
2015-02-23 10:31:07 +01:00
|
|
|
ACTION="$1"
|
|
|
|
shift # pop $1 from $@
|
|
|
|
|
|
|
|
case "${ACTION}" in
|
2015-02-08 14:05:28 +01:00
|
|
|
flash)
|
|
|
|
echo "### Flashing Target ###"
|
2015-02-23 10:31:07 +01:00
|
|
|
do_flash "$@"
|
2015-02-08 14:05:28 +01:00
|
|
|
;;
|
|
|
|
debug)
|
|
|
|
echo "### Starting Debugging ###"
|
2015-02-23 10:31:07 +01:00
|
|
|
do_debug "$@"
|
2015-02-08 14:05:28 +01:00
|
|
|
;;
|
|
|
|
debug-server)
|
|
|
|
echo "### Starting GDB Server ###"
|
2015-02-23 10:31:07 +01:00
|
|
|
do_debugserver "$@"
|
2015-02-08 14:05:28 +01:00
|
|
|
;;
|
|
|
|
reset)
|
|
|
|
echo "### Resetting Target ###"
|
2015-02-23 10:31:07 +01:00
|
|
|
do_reset "$@"
|
2015-02-08 14:05:28 +01:00
|
|
|
;;
|
|
|
|
*)
|
2015-02-23 10:31:07 +01:00
|
|
|
echo "Usage: $0 {flash|debug|debug-server|reset} [extra OpenOCD initialization arguments]"
|
2015-02-08 14:05:28 +01:00
|
|
|
;;
|
|
|
|
esac
|