stlink: Add workaround for intermittent FW info retrieval failure 61/1561/4
authorAndrey Smirnov <andrew.smirnov@gmail.com>
Tue, 20 Aug 2013 19:26:19 +0000 (12:26 -0700)
committerSpencer Oliver <spen@spen-soft.co.uk>
Thu, 29 Aug 2013 07:59:19 +0000 (07:59 +0000)
It appears that on some host USB configurations(2012 MacBook Air)
multiple restarts of openocd tool cause the FW on STLINKv2 dongle to
go into a weird state in which it will no longer respond to
STLINK_GET_VERSION command. This patch adds code that, if said request
fails for the first time, attempts to reset the device and retry to
initialize it and obtain FW information one more time.

Change-Id: I7227fc972adb49d52ae700ad48ab9f66b2aaa72c
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
Reviewed-on: http://openocd.zylin.com/1561
Tested-by: jenkins
Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
Reviewed-by: Andreas Fritiofson <andreas.fritiofson@gmail.com>
src/jtag/drivers/libusb0_common.h
src/jtag/drivers/libusb1_common.h
src/jtag/drivers/stlink_usb.c

index 362235f697611f259bd12d6d42d63523083d3788..bea743dcf5c5d9e59fcbb5300b49e513552a62e0 100644 (file)
@@ -47,6 +47,12 @@ static inline int jtag_libusb_claim_interface(jtag_libusb_device_handle *devh,
        return usb_claim_interface(devh, iface);
 };
 
        return usb_claim_interface(devh, iface);
 };
 
+static inline int jtag_libusb_release_interface(jtag_libusb_device_handle *devh,
+                                      int iface)
+{
+       return usb_release_interface(devh, iface);
+}
+
 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
                struct jtag_libusb_device_handle **out);
 void jtag_libusb_close(jtag_libusb_device_handle *dev);
 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
                struct jtag_libusb_device_handle **out);
 void jtag_libusb_close(jtag_libusb_device_handle *dev);
index 8b2792cdcb34fa4f03da26aea3ff918508e181d5..cd1f7f10f51b521a234242ccdb571d8fd4ba4c6f 100644 (file)
@@ -41,6 +41,12 @@ static inline int jtag_libusb_claim_interface(jtag_libusb_device_handle *devh,
        return libusb_claim_interface(devh, iface);
 };
 
        return libusb_claim_interface(devh, iface);
 };
 
+static inline int jtag_libusb_release_interface(jtag_libusb_device_handle *devh,
+               int iface)
+{
+       return libusb_release_interface(devh, iface);
+}
+
 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
                struct jtag_libusb_device_handle **out);
 void jtag_libusb_close(jtag_libusb_device_handle *dev);
 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
                struct jtag_libusb_device_handle **out);
 void jtag_libusb_close(jtag_libusb_device_handle *dev);
index 1f3fbe2cdc7d7112b7e729084979505e95910902..cccb9ffa0b2fbccd3d83d7aef9240171084aeaf5 100644 (file)
@@ -1447,7 +1447,7 @@ static int stlink_usb_close(void *fd)
 /** */
 static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
 {
 /** */
 static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
 {
-       int err;
+       int err, retry_count = 1;
        struct stlink_usb_handle_s *h;
        enum stlink_jtag_api_version api;
 
        struct stlink_usb_handle_s *h;
        enum stlink_jtag_api_version api;
 
@@ -1471,35 +1471,69 @@ static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
        LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x", param->transport,
                param->vid, param->pid);
 
        LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x", param->transport,
                param->vid, param->pid);
 
-       if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
-               LOG_ERROR("open failed");
-               goto error_open;
-       }
+       /*
+         On certain host USB configurations(e.g. MacBook Air)
+         STLINKv2 dongle seems to have its FW in a funky state if,
+         after plugging it in, you try to use openocd with it more
+         then once (by launching and closing openocd). In cases like
+         that initial attempt to read the FW info via
+         stlink_usb_version will fail and the device has to be reset
+         in order to become operational.
+        */
+       do {
+               if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
+                       LOG_ERROR("open failed");
+                       goto error_open;
+               }
 
 
-       jtag_libusb_set_configuration(h->fd, 0);
+               jtag_libusb_set_configuration(h->fd, 0);
 
 
-       if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
-               LOG_DEBUG("claim interface failed");
-               goto error_open;
-       }
+               if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
+                       LOG_DEBUG("claim interface failed");
+                       goto error_open;
+               }
 
 
-       /* wrap version for first read */
-       switch (param->pid) {
+               /* wrap version for first read */
+               switch (param->pid) {
                case 0x3744:
                        h->version.stlink = 1;
                        break;
                default:
                        h->version.stlink = 2;
                        break;
                case 0x3744:
                        h->version.stlink = 1;
                        break;
                default:
                        h->version.stlink = 2;
                        break;
-       }
+               }
 
 
-       /* get the device version */
-       err = stlink_usb_version(h);
+               /* get the device version */
+               err = stlink_usb_version(h);
 
 
-       if (err != ERROR_OK) {
-               LOG_ERROR("read version failed");
-               goto error_open;
-       }
+               if (err == ERROR_OK) {
+                       break;
+               } else if (h->version.stlink == 1 ||
+                          retry_count == 0) {
+                       LOG_ERROR("read version failed");
+                       goto error_open;
+               } else {
+                       err = jtag_libusb_release_interface(h->fd, 0);
+                       if (err != ERROR_OK) {
+                               LOG_ERROR("release interface failed");
+                               goto error_open;
+                       }
+
+                       err = jtag_libusb_reset_device(h->fd);
+                       if (err != ERROR_OK) {
+                               LOG_ERROR("reset device failed");
+                               goto error_open;
+                       }
+
+                       jtag_libusb_close(h->fd);
+                       /*
+                         Give the device one second to settle down and
+                         reenumerate.
+                        */
+                       usleep(1 * 1000 * 1000);
+                       retry_count--;
+               }
+       } while (1);
 
        /* compare usb vid/pid */
        if ((param->vid != h->vid) || (param->pid != h->pid))
 
        /* compare usb vid/pid */
        if ((param->vid != h->vid) || (param->pid != h->pid))

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)