remote_bitbang: Add SWD support 44/6044/18
authorManuel Wick <manuel@matronix.de>
Sat, 30 Jan 2021 21:46:50 +0000 (22:46 +0100)
committerTomas Vanek <vanekt@fbl.cz>
Sun, 3 Dec 2023 06:22:54 +0000 (06:22 +0000)
This adds new command characters to make SWD work with the new split
jtag and swd operations of bitbang.

The command characters are as follows:
    O - SWDIO drive 1
    o - SWDIO drive 0
    c - SWDIO read request
    d - SWD write 0 0
    e - SWD write 0 1
    f - SWD write 1 0
    g - SWD write 1 1

Documentation has been updated accordingly. The new commands will be
used by an adapted version of the jtag-openocd applet of the "Glasgow
Debug Tool" (https://github.com/glasgowEmbedded/Glasgow). It has been
tested against an stm32f103 and an at91samd21 target.

contrib/remote/bitbang/remote_bitbang_sysfsgpio.c has also been adapted
to support SWD via the new command set. Some limited testing has been
done using a Raspberry Pi 2 with an stm32f103 and an at91samd21 target
attached.

Change-Id: I8e998a2cb36905142cb16e534483094cd99e8fa7
Signed-off-by: Manuel Wick <manuel@matronix.de>
Signed-off-by: David Ryskalczyk <david.rysk@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/6044
Tested-by: jenkins
Reviewed-by: Tomas Vanek <vanekt@fbl.cz>
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
configure.ac
contrib/remote_bitbang/remote_bitbang_sysfsgpio.c
doc/manual/jtag/drivers/remote_bitbang.txt
doc/openocd.texi
src/jtag/drivers/remote_bitbang.c

index a2442d40b5bd1ae74bb99d70717508a41041234e..c9cf0214e3163a639b6c92108cc4d7a61521a8c8 100644 (file)
@@ -382,7 +382,7 @@ AC_ARG_ENABLE([internal-libjaylink],
   [use_internal_libjaylink=$enableval], [use_internal_libjaylink=no])
 
 AC_ARG_ENABLE([remote-bitbang],
-  AS_HELP_STRING([--enable-remote-bitbang], [Enable building support for the Remote Bitbang jtag driver]),
+  AS_HELP_STRING([--enable-remote-bitbang], [Enable building support for the Remote Bitbang driver]),
   [build_remote_bitbang=$enableval], [build_remote_bitbang=no])
 
 AS_CASE(["${host_cpu}"],
@@ -598,9 +598,9 @@ AS_IF([test "x$use_internal_jimtcl" = "xyes"], [
 
 AS_IF([test "x$build_remote_bitbang" = "xyes"], [
   build_bitbang=yes
-  AC_DEFINE([BUILD_REMOTE_BITBANG], [1], [1 if you want the Remote Bitbang JTAG driver.])
+  AC_DEFINE([BUILD_REMOTE_BITBANG], [1], [1 if you want the Remote Bitbang driver.])
 ], [
-  AC_DEFINE([BUILD_REMOTE_BITBANG], [0], [0 if you don't want the Remote Bitbang JTAG driver.])
+  AC_DEFINE([BUILD_REMOTE_BITBANG], [0], [0 if you don't want the Remote Bitbang driver.])
 ])
 
 AS_IF([test "x$build_sysfsgpio" = "xyes"], [
index 9294837e3c77f5c5ab0916d1b80927caa7f2731c..1588eb9a3ca2f1c744699477164290692c59729b 100644 (file)
@@ -1,30 +1,31 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 
 /***************************************************************************
+ *   Copyright (C) 2021 by Manuel Wick <manuel@matronix.de>                *
  *   Copyright (C) 2013 Paul Fertser <fercerpav@gmail.com>                 *
  *   Copyright (C) 2012 by Creative Product Design, marc @ cpdesign.com.au *
  ***************************************************************************/
 
 /*
-  This is a test application to be used as a remote bitbang server for
-  the OpenOCD remote_bitbang interface driver.
-
-  To compile run:
-  gcc -Wall -ansi -pedantic -std=c99 -o remote_bitbang_sysfsgpio remote_bitbang_sysfsgpio.c
-
-
-  Usage example:
-
-  On Raspberry Pi run:
-  socat TCP6-LISTEN:7777,fork EXEC:"sudo ./remote_bitbang_sysfsgpio tck 11 tms 25 tdo 9 tdi 10"
-
-  On host run:
 openocd -c "interface remote_bitbang; remote_bitbang host raspberrypi; remote_bitbang port 7777" \
        -f target/stm32f1x.cfg
-
-  Or if you want to test UNIX sockets, run both on Raspberry Pi:
-  socat UNIX-LISTEN:/tmp/remotebitbang-socket,fork EXEC:"sudo ./remote_bitbang_sysfsgpio tck 11 tms 25 tdo 9 tdi 10"
 openocd -c "interface remote_bitbang; remote_bitbang host /tmp/remotebitbang-socket" -f target/stm32f1x.cfg
* This is a test application to be used as a remote bitbang server for
* the OpenOCD remote_bitbang interface driver.
+ *
* To compile run:
* gcc -Wall -ansi -pedantic -std=c99 -o remote_bitbang_sysfsgpio remote_bitbang_sysfsgpio.c
+ *
+ *
* Usage example:
+ *
* On Raspberry Pi run:
* socat TCP6-LISTEN:7777,fork EXEC:"sudo ./remote_bitbang_sysfsgpio tck 11 tms 25 tdo 9 tdi 10"
+ *
* On host run:
* openocd -c "adapter driver remote_bitbang; remote_bitbang host raspberrypi; remote_bitbang port 7777" \
*  -f target/stm32f1x.cfg
+ *
* Or if you want to test UNIX sockets, run both on Raspberry Pi:
* socat UNIX-LISTEN:/tmp/remotebitbang-socket,fork EXEC:"sudo ./remote_bitbang_sysfsgpio tck 11 tms 25 tdo 9 tdi 10"
* openocd -c "adapter driver remote_bitbang; remote_bitbang host /tmp/remotebitbang-socket" -f target/stm32f1x.cfg
 */
 
 #include <sys/types.h>
@@ -97,11 +98,14 @@ static void unexport_sysfs_gpio(int gpio)
  * If the gpio is an output, it is initialized according to init_high,
  * otherwise it is ignored.
  *
+ * When open_rw is set, the file descriptor will be open as read and write,
+ * e.g. for SWDIO (TMS) that is used as input and output.
+ *
  * If the gpio is already exported we just show a warning and continue; if
  * openocd happened to crash (or was killed by user) then the gpios will not
  * have been cleaned up.
  */
-static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
+static int setup_sysfs_gpio(int gpio, int is_output, int init_high, int open_rw)
 {
        char buf[40];
        char gpiostr[4];
@@ -132,7 +136,9 @@ static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
        }
 
        snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
-       if (is_output)
+       if (open_rw)
+               ret = open(buf, O_RDWR | O_NONBLOCK | O_SYNC);
+       else if (is_output)
                ret = open(buf, O_WRONLY | O_NONBLOCK | O_SYNC);
        else
                ret = open(buf, O_RDONLY | O_NONBLOCK | O_SYNC);
@@ -143,6 +149,37 @@ static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
        return ret;
 }
 
+/*
+ * Change direction for gpio.
+ */
+static int change_dir_sysfs_gpio(int gpio, int is_output, int init_high)
+{
+       char buf[40];
+       int ret;
+
+       if (!is_gpio_valid(gpio))
+               return ERROR_OK;
+
+       snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", gpio);
+       ret = open_write_close(buf, is_output ? (init_high ? "high" : "low") : "in");
+       if (ret < 0) {
+               LOG_ERROR("Couldn't set direction for gpio %d", gpio);
+               perror("sysfsgpio: ");
+               unexport_sysfs_gpio(gpio);
+               return ERROR_FAIL;
+       }
+
+       return ERROR_OK;
+}
+
+/* gpio numbers for each gpio. Negative values are invalid */
+static int tck_gpio = -1;
+static int tms_gpio = -1;
+static int tdi_gpio = -1;
+static int tdo_gpio = -1;
+static int trst_gpio = -1;
+static int srst_gpio = -1;
+
 /*
  * file descriptors for /sys/class/gpio/gpioXX/value
  * Set up during init.
@@ -154,6 +191,15 @@ static int tdo_fd = -1;
 static int trst_fd = -1;
 static int srst_fd = -1;
 
+/*
+ * GPIO state of /sys/class/gpio/gpioXX/value
+ */
+static int last_tck = -1;
+static int last_tms = -1;
+static int last_tms_drive = -1;
+static int last_tdi = -1;
+static int last_initialized = -1;
+
 /*
  * Bitbang interface read of TDO
  *
@@ -179,26 +225,22 @@ static int sysfsgpio_read(void)
 /*
  * Bitbang interface write of TCK, TMS, TDI
  *
- * Seeing as this is the only function where the outputs are changed,
- * we can cache the old value to avoid needlessly writing it.
+ * Output states are changed here and in sysfsgpio_write_swd,
+ * which are not used simultaneously, so we can cache the old
+ * value to avoid needlessly writing it.
  */
 static void sysfsgpio_write(int tck, int tms, int tdi)
 {
        const char one[] = "1";
        const char zero[] = "0";
 
-       static int last_tck;
-       static int last_tms;
-       static int last_tdi;
-
-       static int first_time;
        size_t bytes_written;
 
-       if (!first_time) {
+       if (!last_initialized) {
                last_tck = !tck;
                last_tms = !tms;
                last_tdi = !tdi;
-               first_time = 1;
+               last_initialized = 1;
        }
 
        if (tdi != last_tdi) {
@@ -251,13 +293,81 @@ static void sysfsgpio_reset(int trst, int srst)
        }
 }
 
-/* gpio numbers for each gpio. Negative values are invalid */
-static int tck_gpio = -1;
-static int tms_gpio = -1;
-static int tdi_gpio = -1;
-static int tdo_gpio = -1;
-static int trst_gpio = -1;
-static int srst_gpio = -1;
+/*
+ * Bitbang interface set direction of SWDIO (TMS)
+ */
+static void sysfsgpio_swdio_drive(int is_output)
+{
+       int ret;
+
+       if (is_output != 0 && last_tms == -1)
+               last_tms = 0;
+
+       ret = change_dir_sysfs_gpio(tms_gpio, (is_output != 0) ? 1 : 0, last_tms);
+       if (ret != ERROR_OK)
+               LOG_WARNING("Failed to change SWDIO (TMS) direction to output");
+       else
+               last_tms_drive = (is_output != 0) ? 1 : 0;
+}
+
+/*
+ * Bitbang interface read of SWDIO (TMS)
+ *
+ * The sysfs value will read back either '0' or '1'. The trick here is to call
+ * lseek to bypass buffering in the sysfs kernel driver.
+ */
+static int sysfsgpio_swdio_read(void)
+{
+       char buf[1];
+
+       /* important to seek to signal sysfs of new read */
+       lseek(tms_fd, 0, SEEK_SET);
+       int ret = read(tms_fd, &buf, sizeof(buf));
+
+       if (ret < 0) {
+               LOG_WARNING("reading swdio (tms) failed");
+               return 0;
+       }
+
+       return buf[0];
+}
+
+/*
+ * Bitbang interface write of SWCLK (TCK) and SWDIO (TMS)
+ *
+ * Output states are changed here and in sysfsgpio_write, which
+ * are not used simultaneously, so we can cache the old value
+ * to avoid needlessly writing it.
+ */
+static void sysfsgpio_swd_write(int swclk, int swdio)
+{
+       static const char one[] = "1";
+       static const char zero[] = "0";
+
+       size_t bytes_written;
+
+       if (!last_initialized) {
+               last_tck = !swclk;
+               last_tms = !swdio;
+               last_initialized = 1;
+       }
+
+       if (last_tms_drive == 1 && swdio != last_tms) {
+               bytes_written = write(tms_fd, swdio ? &one : &zero, 1);
+               if (bytes_written != 1)
+                       LOG_WARNING("writing swdio (tms) failed");
+       }
+
+       /* write clk last */
+       if (swclk != last_tck) {
+               bytes_written = write(tck_fd, swclk ? &one : &zero, 1);
+               if (bytes_written != 1)
+                       LOG_WARNING("writing swclk (tck) failed");
+       }
+
+       last_tms = swdio;
+       last_tck = swclk;
+}
 
 /* helper func to close and cleanup files only if they were valid/ used */
 static void cleanup_fd(int fd, int gpio)
@@ -293,13 +403,21 @@ static void process_remote_protocol(void)
                        char d = c - 'r';
                        sysfsgpio_reset(!!(d & 2),
                                        (d & 1));
-               } else if (c >= '0' && c <= '0' + 7) {/* Write */
+               } else if (c >= '0' && c <= '0' + 7) { /* Write */
                        char d = c - '0';
                        sysfsgpio_write(!!(d & 4),
                                        !!(d & 2),
                                        (d & 1));
                } else if (c == 'R')
                        putchar(sysfsgpio_read());
+               else if (c == 'c') /* SWDIO read */
+                       putchar(sysfsgpio_swdio_read());
+               else if (c == 'o' || c == 'O') /* SWDIO drive */
+                       sysfsgpio_swdio_drive(c == 'o' ? 0 : 1);
+               else if (c >= 'd' && c <= 'g') { /* SWD write */
+                       char d = c - 'd';
+                       sysfsgpio_swd_write((d & 2), (d & 1));
+               }
                else
                        LOG_ERROR("Unknown command '%c' received", c);
        }
@@ -307,7 +425,7 @@ static void process_remote_protocol(void)
 
 int main(int argc, char *argv[])
 {
-       LOG_WARNING("SysfsGPIO remote_bitbang JTAG driver\n");
+       LOG_WARNING("SysfsGPIO remote_bitbang JTAG+SWD driver\n");
 
        for (int i = 1; i < argc; i++) {
                if (!strcmp(argv[i], "tck"))
@@ -349,36 +467,39 @@ int main(int argc, char *argv[])
         * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
         * as outputs.  Drive TDI and TCK low, and TMS/TRST/SRST high.
         */
-       tck_fd = setup_sysfs_gpio(tck_gpio, 1, 0);
+       tck_fd = setup_sysfs_gpio(tck_gpio, 1, 0, 0);
        if (tck_fd < 0)
                goto out_error;
 
-       tms_fd = setup_sysfs_gpio(tms_gpio, 1, 1);
+       tms_fd = setup_sysfs_gpio(tms_gpio, 1, 1, 1);
        if (tms_fd < 0)
                goto out_error;
+       last_tms_drive = 0;
 
-       tdi_fd = setup_sysfs_gpio(tdi_gpio, 1, 0);
+       tdi_fd = setup_sysfs_gpio(tdi_gpio, 1, 0, 0);
        if (tdi_fd < 0)
                goto out_error;
 
-       tdo_fd = setup_sysfs_gpio(tdo_gpio, 0, 0);
+       tdo_fd = setup_sysfs_gpio(tdo_gpio, 0, 0, 0);
        if (tdo_fd < 0)
                goto out_error;
 
        /* assume active low */
        if (trst_gpio > 0) {
-               trst_fd = setup_sysfs_gpio(trst_gpio, 1, 1);
+               trst_fd = setup_sysfs_gpio(trst_gpio, 1, 1, 0);
                if (trst_fd < 0)
                        goto out_error;
        }
 
        /* assume active low */
        if (srst_gpio > 0) {
-               srst_fd = setup_sysfs_gpio(srst_gpio, 1, 1);
+               srst_fd = setup_sysfs_gpio(srst_gpio, 1, 1, 0);
                if (srst_fd < 0)
                        goto out_error;
        }
 
+       last_initialized = 0;
+
        LOG_WARNING("SysfsGPIO nums: tck = %d, tms = %d, tdi = %d, tdo = %d",
                 tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
        LOG_WARNING("SysfsGPIO num: srst = %d", srst_gpio);
index f394d736af384611bde6e77ae89cc01234832d2d..7c8eee289e2a02cfd3328999ac598d966282e915 100644 (file)
@@ -1,15 +1,19 @@
 /** @remote_bitbangpage OpenOCD Developer's Guide
 
-The remote_bitbang JTAG driver is used to drive JTAG from a remote process. The
-remote_bitbang driver communicates via TCP or UNIX sockets with some remote
-process using an ASCII encoding of the bitbang interface. The remote process
-presumably then drives the JTAG however it pleases. The remote process should
-act as a server, listening for connections from the openocd remote_bitbang
-driver.
+The remote_bitbang JTAG+SWD driver is used to drive JTAG and/or SWD from a
+remote process. The remote_bitbang driver communicates via TCP or UNIX
+sockets with some remote process using an ASCII encoding of the bitbang
+interface. The remote process presumably then drives the JTAG/SWD however
+it pleases. The remote process should act as a server, listening for
+connections from the openocd remote_bitbang driver.
 
 The remote bitbang driver is useful for debugging software running on
 processors which are being simulated.
 
+There also is an implementation of the server-side protocol for the
+Glasgow Debug Tool (https://github.com/glasgowEmbedded/Glasgow) through
+the jtag-openocd applet.
+
 The bitbang interface consists of the following functions.
 
 blink on
@@ -24,11 +28,20 @@ write tck tms tdi
 reset trst srst
        Set the value of trst, srst.
 
+swdio_drive
+       Set the output enable of the bidirectional swdio (tms) pin
+
+swdio_read
+       Sample the value of swdio (tms).
+
+swd_write
+       Set the value of swclk (tck) and swdio (tms).
+
 An additional function, quit, is added to the remote_bitbang interface to
 indicate there will be no more requests and the connection with the remote
 driver should be closed.
 
-These five functions are encoded in ASCII by assigning a single character to
+These eight functions are encoded in ASCII by assigning a single character to
 each possible request. The assignments are:
 
        B - Blink on
@@ -47,7 +60,14 @@ each possible request. The assignments are:
        s - Reset 0 1
        t - Reset 1 0
        u - Reset 1 1
+       O - SWDIO drive 1
+       o - SWDIO drive 0
+       c - SWDIO read request
+       d - SWD write 0 0
+       e - SWD write 0 1
+       f - SWD write 1 0
+       g - SWD write 1 1
 
-The read response is encoded in ASCII as either digit 0 or 1.
+The read responses are encoded in ASCII as either digit 0 or 1.
 
  */
index c14ee9cb6a2eed55d0ab74a5570d6a05c7f2b26a..de990c53811123d704a893eeb22f83a173eb7bb0 100644 (file)
@@ -2810,9 +2810,9 @@ If not specified, default 0xFFFF is used.
 @end deffn
 
 @deffn {Interface Driver} {remote_bitbang}
-Drive JTAG from a remote process. This sets up a UNIX or TCP socket connection
-with a remote process and sends ASCII encoded bitbang requests to that process
-instead of directly driving JTAG.
+Drive JTAG and SWD from a remote process. This sets up a UNIX or TCP socket
+connection with a remote process and sends ASCII encoded bitbang requests to
+that process instead of directly driving JTAG and SWD.
 
 The remote_bitbang driver is useful for debugging software running on
 processors which are being simulated.
index 36bcbe19cac2c3d0c4c7c1f906a5db695a091478..261c30df2dba5f3aff04b8dd911c63a39bd3fd90 100644 (file)
@@ -3,6 +3,8 @@
 /***************************************************************************
  *   Copyright (C) 2011 by Richard Uhler                                   *
  *   ruhler@mit.edu                                                        *
+ *                                                                         *
+ *   Copyright (C) 2021 by Manuel Wick <manuel@matronix.de>                *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -220,11 +222,35 @@ static int remote_bitbang_blink(int on)
        return remote_bitbang_queue(c, FLUSH_SEND_BUF);
 }
 
+static void remote_bitbang_swdio_drive(bool is_output)
+{
+       char c = is_output ? 'O' : 'o';
+       if (remote_bitbang_queue(c, FLUSH_SEND_BUF) == ERROR_FAIL)
+               LOG_ERROR("Error setting direction for swdio");
+}
+
+static int remote_bitbang_swdio_read(void)
+{
+       if (remote_bitbang_queue('c', FLUSH_SEND_BUF) != ERROR_FAIL)
+               return remote_bitbang_read_sample();
+       else
+               return BB_ERROR;
+}
+
+static int remote_bitbang_swd_write(int swclk, int swdio)
+{
+       char c = 'd' + ((swclk ? 0x2 : 0x0) | (swdio ? 0x1 : 0x0));
+       return remote_bitbang_queue(c, NO_FLUSH);
+}
+
 static struct bitbang_interface remote_bitbang_bitbang = {
        .buf_size = sizeof(remote_bitbang_recv_buf) - 1,
        .sample = &remote_bitbang_sample,
        .read_sample = &remote_bitbang_read_sample,
        .write = &remote_bitbang_write,
+       .swdio_read = &remote_bitbang_swdio_read,
+       .swdio_drive = &remote_bitbang_swdio_drive,
+       .swd_write = &remote_bitbang_swd_write,
        .blink = &remote_bitbang_blink,
 };
 
@@ -349,6 +375,8 @@ COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_host_command)
        return ERROR_COMMAND_SYNTAX_ERROR;
 }
 
+static const char * const remote_bitbang_transports[] = { "jtag", "swd", NULL };
+
 static const struct command_registration remote_bitbang_subcommand_handlers[] = {
        {
                .name = "port",
@@ -401,7 +429,7 @@ static struct jtag_interface remote_bitbang_interface = {
 
 struct adapter_driver remote_bitbang_adapter_driver = {
        .name = "remote_bitbang",
-       .transports = jtag_only,
+       .transports = remote_bitbang_transports,
        .commands = remote_bitbang_command_handlers,
 
        .init = &remote_bitbang_init,
@@ -409,4 +437,5 @@ struct adapter_driver remote_bitbang_adapter_driver = {
        .reset = &remote_bitbang_reset,
 
        .jtag_ops = &remote_bitbang_interface,
+       .swd_ops = &bitbang_swd,
 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)