From 5d59338de5f396300e46f52c264d6a050b8ae247 Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Wed, 21 Aug 2019 12:50:29 +0200 Subject: [PATCH] pc-stlinkv2: Try first to reset device if we read a strange version number. --- src/platforms/pc-stlinkv2/stlinkv2.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/platforms/pc-stlinkv2/stlinkv2.c b/src/platforms/pc-stlinkv2/stlinkv2.c index 7cc6ddd..f3de39e 100644 --- a/src/platforms/pc-stlinkv2/stlinkv2.c +++ b/src/platforms/pc-stlinkv2/stlinkv2.c @@ -800,10 +800,23 @@ void stlink_init(int argc, char **argv) Stlink.req_trans = libusb_alloc_transfer(0); Stlink.rep_trans = libusb_alloc_transfer(0); stlink_version(); - if (Stlink.ver_stlink < 3 && Stlink.ver_jtag < 32) { - DEBUG("Please update Firmware\n"); - goto error_1; - } else if (Stlink.ver_stlink == 3 && Stlink.ver_jtag < 3) { + if ((Stlink.ver_stlink < 3 && Stlink.ver_jtag < 32) || + (Stlink.ver_stlink == 3 && Stlink.ver_jtag < 3)) { + /* Maybe the adapter is in some strange state. Try to reset */ + int result = libusb_reset_device(Stlink.handle); + DEBUG("Trying reset\n"); + if (result == LIBUSB_ERROR_BUSY) { /* Try again */ + platform_delay(50); + result = libusb_reset_device(Stlink.handle); + } + if (result != LIBUSB_SUCCESS) { + DEBUG("libusb_reset_device failed\n"); + goto error_1; + } + stlink_version(); + } + if ((Stlink.ver_stlink < 3 && Stlink.ver_jtag < 32) || + (Stlink.ver_stlink == 3 && Stlink.ver_jtag < 3)) { DEBUG("Please update Firmware\n"); goto error_1; }