From 45e0468ea43c66f8dbe8b6561e686f36b647a8d5 Mon Sep 17 00:00:00 2001
From: Hauke Petersen <hauke.petersen@fu-berlin.de>
Date: Mon, 15 Jan 2018 23:00:16 +0100
Subject: [PATCH] tools/jlink: add RTT terminal support (term_rtt)

---
 dist/tools/jlink/jlink.sh | 46 +++++++++++++++++++++++++++++++++++++++
 dist/tools/jlink/term.seg |  1 +
 2 files changed, 47 insertions(+)
 create mode 100644 dist/tools/jlink/term.seg

diff --git a/dist/tools/jlink/jlink.sh b/dist/tools/jlink/jlink.sh
index 8778118d54..b525207f15 100755
--- a/dist/tools/jlink/jlink.sh
+++ b/dist/tools/jlink/jlink.sh
@@ -57,6 +57,9 @@ _JLINK=JLinkExe
 _JLINK_SERVER=JLinkGDBServer
 _JLINK_IF=SWD
 _JLINK_SPEED=2000
+# default terminal frontend
+_JLINK_TERMPROG=${RIOTBASE}/dist/tools/pyterm/pyterm
+_JLINK_TERMFLAGS="-ts 19021"
 
 #
 # a couple of tests for certain configuration options
@@ -135,6 +138,15 @@ test_dbg() {
     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
 #
@@ -209,6 +221,36 @@ do_reset() {
                     -commandfile '${RIOTBASE}/dist/tools/jlink/reset.seg'"
 }
 
+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() {
+        JLINK_PID="$(cat ${JLINK_PIDFILE})"
+        kill ${JLINK_PID}
+        rm -r "${JLINK_PIDFILE}"
+        exit 0
+    }
+    # cleanup after script terminates
+    trap "cleanup ${JLINK_PIDFILE}" EXIT
+    # don't trapon Ctrl+C, because JLink keeps running
+    trap '' INT
+    # start Jlink as RTT server
+    setsid sh -c "${JLINK} ${JLINK_SERIAL} \
+            -device '${JLINK_DEVICE}' \
+            -speed '${JLINK_SPEED}' \
+            -if '${JLINK_IF}' \
+            -jtagconf -1,-1 \
+            -commandfile '${RIOTBASE}/dist/tools/jlink/term.seg' & \
+            echo  \$! > $JLINK_PIDFILE" &
+
+    sh -c "${JLINK_TERMPROG} ${JLINK_TERMFLAGS}"
+}
+
 #
 # parameter dispatching
 #
@@ -233,6 +275,10 @@ case "${ACTION}" in
     echo "### Resetting Target ###"
     do_reset "$@"
     ;;
+  term_rtt)
+    echo "### Starting RTT terminal ###"
+    do_term
+    ;;
   *)
     echo "Usage: $0 {flash|debug|debug-server|reset}"
     ;;
diff --git a/dist/tools/jlink/term.seg b/dist/tools/jlink/term.seg
new file mode 100644
index 0000000000..1bd5775c42
--- /dev/null
+++ b/dist/tools/jlink/term.seg
@@ -0,0 +1 @@
+connect
-- 
GitLab