From 1986173163cdff574abc3cadd30f59744731b6e3 Mon Sep 17 00:00:00 2001 From: Guillaume Revaillot Date: Sun, 12 Apr 2020 14:45:41 +0200 Subject: [PATCH 1/4] stlink_open_usb: load device param returning -1 is not an usb issue. do not abort in case of load device param "error" : this is supposed to return -1 if device is "unknown" or not detected, which is not reaaally an error. we want probe to tell us what is the connected device even if we can not operate it we want probe to tell us that it can not operate it, not to hide the connected stlink --- src/usb.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/usb.c b/src/usb.c index 206600f14..68c484ddd 100644 --- a/src/usb.c +++ b/src/usb.c @@ -1054,11 +1054,8 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[ST usleep(10000); } - ret = stlink_load_device_params(sl); - if (ret == -1) { - // This one didn't have any message. - goto on_libusb_error; - } + stlink_load_device_params(sl); + return sl; on_libusb_error: From 784ea36bdc3f0a71d67590a64424f2249e8e14cc Mon Sep 17 00:00:00 2001 From: Guillaume Revaillot Date: Sun, 12 Apr 2020 16:06:15 +0200 Subject: [PATCH 2/4] usb.c: indent, make error use ELOG, and add trace --- src/usb.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/usb.c b/src/usb.c index 68c484ddd..c9ea7039a 100644 --- a/src/usb.c +++ b/src/usb.c @@ -1045,7 +1045,7 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[ST stlink_set_swdclk(sl, STLINK_SWDCLK_1P8MHZ_DIVISOR); if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) { - stlink_enter_swd_mode(sl); + stlink_enter_swd_mode(sl); } if (reset) { @@ -1130,9 +1130,9 @@ static size_t stlink_probe_usb_devs(libusb_device **devs, stlink_t **sldevs[]) { ret = libusb_open(dev, &handle); if (ret < 0) { if (ret == LIBUSB_ERROR_ACCESS) { - WLOG("failed to open USB device (LIBUSB_ERROR_ACCESS), try running as root?\n"); - } else { - WLOG("failed to open USB device (libusb error: %d)\n", ret); + ELOG("Could not open USB device %#06x:%#06x, access error.\n", desc.idVendor, desc.idProduct, ret); + } else { + ELOG("Failed to open USB device %#06x:%#06x, libusb error: %d)\n", desc.idVendor, desc.idProduct, ret); } break; } @@ -1146,8 +1146,10 @@ static size_t stlink_probe_usb_devs(libusb_device **devs, stlink_t **sldevs[]) { } stlink_t *sl = stlink_open_usb(0, 1, serial); - if (!sl) + if (!sl) { + ELOG("Failed to open USB device %#06x:%#06x\n", desc.idVendor, desc.idProduct); continue; + } _sldevs[slcur++] = sl; } From 5aa001cfab6eb70e697691915c8754553bc83b92 Mon Sep 17 00:00:00 2001 From: Guillaume Revaillot Date: Sun, 12 Apr 2020 16:31:49 +0200 Subject: [PATCH 3/4] flash.c: stlink_open_usb can now return a sl with no, or unknown target. check flash type before attempting to continue flash operations --- src/tools/flash.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/tools/flash.c b/src/tools/flash.c index f39b6c6e4..a08581094 100644 --- a/src/tools/flash.c +++ b/src/tools/flash.c @@ -59,8 +59,14 @@ int main(int ac, char** av) sl = stlink_open_usb(o.log_level, 1, (char *)o.serial); - if (sl == NULL) + if (sl == NULL) { return -1; + } + + if (sl->flash_type == STLINK_FLASH_TYPE_UNKNOWN) { + printf("Failed to connect to target\n"); + return -1; + } if ( o.flash_size != 0u && o.flash_size != sl->flash_size ) { sl->flash_size = o.flash_size; From 8f778b2e4c33d8e5916ea3f19bf4b06f50566537 Mon Sep 17 00:00:00 2001 From: Guillaume Revaillot Date: Thu, 16 Apr 2020 16:46:27 +0200 Subject: [PATCH 4/4] st-util: open usb can return sl pointer with no target connected. Check if connected target chip id is known. Some more info regarding connected chip should be set in chipid to know if debugging / attach is possible, but not yet. some more checks should be done to generate flash map / handle flashing if flash type is not supported.. --- src/gdbserver/gdb-server.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/gdbserver/gdb-server.c b/src/gdbserver/gdb-server.c index 1c45c2538..2b5b1d3cb 100644 --- a/src/gdbserver/gdb-server.c +++ b/src/gdbserver/gdb-server.c @@ -234,7 +234,14 @@ int main(int argc, char** argv) { printf("st-util %s\n", STLINK_VERSION); sl = do_connect(&state); - if (sl == NULL) return 1; + if (sl == NULL) { + return 1; + } + + if (sl->chip_id == STLINK_CHIPID_UNKNOWN) { + ELOG("Unsupported Target (Chip ID is %#010x, Core ID is %#010x).\n", sl->chip_id, sl->core_id); + return 1; + } connected_stlink = sl; signal(SIGINT, &cleanup); @@ -245,10 +252,7 @@ int main(int argc, char** argv) { stlink_reset(sl); } - - // This is low-level information for debugging, not useful for normal use. - // So: Demoted to a debug meesage. -- REW - DLOG("Chip ID is %08x, Core ID is %08x.\n", sl->chip_id, sl->core_id); + DLOG("Chip ID is %#010x, Core ID is %#08x.\n", sl->chip_id, sl->core_id); sl->verbose=0; current_memory_map = make_memory_map(sl); @@ -1852,7 +1856,8 @@ int serve(stlink_t *sl, st_state_t *st) { stlink_close(sl); sl = do_connect(st); - if (sl == NULL) cleanup(0); + if (sl == NULL || sl->chip_id == STLINK_CHIPID_UNKNOWN) + cleanup(0); connected_stlink = sl; if (st->reset) {