From f767b82e71d69c0fa43c83545ec8b591298a0f6b Mon Sep 17 00:00:00 2001 From: nightwalker-87 <15526941+Nightwalker-87@users.noreply.github.com> Date: Wed, 17 Jun 2020 13:29:41 +0200 Subject: [PATCH] Added 'connect under reset' option for USB. --- doc/dev/app-example/main.c | 2 +- src/st-flash/flash.c | 2 +- src/st-info/info.c | 6 +++--- src/st-util/gdb-server.c | 4 ++-- src/stlink-gui/gui.c | 4 ++-- src/stlink-lib/usb.c | 10 +++++++--- src/stlink-lib/usb.h | 2 +- tests/usb.c | 2 +- 8 files changed, 18 insertions(+), 14 deletions(-) diff --git a/doc/dev/app-example/main.c b/doc/dev/app-example/main.c index b8e64b259..b56a73e17 100644 --- a/doc/dev/app-example/main.c +++ b/doc/dev/app-example/main.c @@ -6,7 +6,7 @@ static stlink_t *stlink_open_first(void) { stlink_t* sl = NULL; sl = stlink_v1_open(0, 1); if (sl == NULL) - sl = stlink_open_usb(0, 1, NULL); + sl = stlink_open_usb(0, 0, 1, NULL); return sl; } diff --git a/src/st-flash/flash.c b/src/st-flash/flash.c index 43378965c..8bd0e289f 100644 --- a/src/st-flash/flash.c +++ b/src/st-flash/flash.c @@ -53,7 +53,7 @@ int main(int ac, char** av) { printf("st-flash %s\n", STLINK_VERSION); - sl = stlink_open_usb(o.log_level, 1, (char *)o.serial, o.freq); + sl = stlink_open_usb(o.log_level, 0, 1, (char *)o.serial, o.freq); if (sl == NULL) { return(-1); } diff --git a/src/st-info/info.c b/src/st-info/info.c index 1bc48a05e..286c6f61b 100644 --- a/src/st-info/info.c +++ b/src/st-info/info.c @@ -76,13 +76,13 @@ static void stlink_probe(void) { static stlink_t *stlink_open_first(bool under_reset) { stlink_t* sl = NULL; - sl = stlink_v1_open(0, 1); + sl = stlink_v1_open(0, 1); // TODO: deprecated? if (sl == NULL) { if (under_reset) { - sl = stlink_open_usb(0, 2, NULL, 0); + sl = stlink_open_usb(0, 1, 2, NULL, 0); } else { - sl = stlink_open_usb(0, 1, NULL, 0); + sl = stlink_open_usb(0, 0, 1, NULL, 0); } } diff --git a/src/st-util/gdb-server.c b/src/st-util/gdb-server.c index 94cd6fda7..c5bca3bde 100644 --- a/src/st-util/gdb-server.c +++ b/src/st-util/gdb-server.c @@ -89,9 +89,9 @@ static stlink_t* do_connect(st_state_t *st) { stlink_t *sl = NULL; if (serial_specified) { - sl = stlink_open_usb(st->logging_level, st->reset, serialnumber, 0); + sl = stlink_open_usb(st->logging_level, 0, st->reset, serialnumber, 0); } else { - sl = stlink_open_usb(st->logging_level, st->reset, NULL, 0); + sl = stlink_open_usb(st->logging_level, 0, st->reset, NULL, 0); } return(sl); diff --git a/src/stlink-gui/gui.c b/src/stlink-gui/gui.c index ed38fcc7c..1515750d1 100644 --- a/src/stlink-gui/gui.c +++ b/src/stlink-gui/gui.c @@ -496,9 +496,9 @@ static void connect_button_cb(GtkWidget *widget, gpointer data) { if (gui->sl != NULL) { return; } - gui->sl = stlink_v1_open(0, 1); // try version 1 then version 2 + gui->sl = stlink_v1_open(0, 1); // try version 1 then version 2 // TODO: deprecated? - if (gui->sl == NULL) { gui->sl = stlink_open_usb(0, 1, NULL, 0); } + if (gui->sl == NULL) { gui->sl = stlink_open_usb(0, 0, 1, NULL, 0); } if (gui->sl == NULL) { stlink_gui_set_info_error_message(gui, "Failed to connect to STLink."); diff --git a/src/stlink-lib/usb.c b/src/stlink-lib/usb.c index f9b4775f3..089bb906f 100644 --- a/src/stlink-lib/usb.c +++ b/src/stlink-lib/usb.c @@ -1002,7 +1002,7 @@ static stlink_backend_t _stlink_usb_backend = { _stlink_usb_set_swdclk }; -stlink_t *stlink_open_usb(enum ugly_loglevel verbose, int reset, char serial[STLINK_SERIAL_MAX_SIZE], int freq) { +stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool connect_under_reset, int reset, char serial[STLINK_SERIAL_MAX_SIZE], int freq) { stlink_t* sl = NULL; struct stlink_libusb* slu = NULL; int ret = -1; @@ -1226,7 +1226,11 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, int reset, char serial[STL if (reset == 2) { stlink_jtag_reset(sl, 0); - if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) { stlink_enter_swd_mode(sl); } + if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) { + if (connect_under_reset) { stlink_jtag_reset(sl, 0); } + stlink_enter_swd_mode(sl); + if (connect_under_reset) { stlink_jtag_reset(sl, 1); } + } stlink_force_debug(sl); stlink_jtag_reset(sl, 1); @@ -1332,7 +1336,7 @@ static size_t stlink_probe_usb_devs(libusb_device **devs, stlink_t **sldevs[]) { if (ret < 0) { continue; } - stlink_t *sl = stlink_open_usb(0, 1, serial, 0); + stlink_t *sl = stlink_open_usb(0, 0, 1, serial, 0); if (!sl) { ELOG("Failed to open USB device %#06x:%#06x\n", desc.idVendor, desc.idProduct); diff --git a/src/stlink-lib/usb.h b/src/stlink-lib/usb.h index d11f17381..8a678fa91 100644 --- a/src/stlink-lib/usb.h +++ b/src/stlink-lib/usb.h @@ -66,7 +66,7 @@ struct stlink_libusb { * @retval NULL Error while opening the stlink * @retval !NULL Stlink found and ready to use */ -stlink_t *stlink_open_usb(enum ugly_loglevel verbose, int reset, char serial[STLINK_SERIAL_MAX_SIZE], int freq); +stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool connect_under_reset, int reset, char serial[STLINK_SERIAL_MAX_SIZE], int freq); size_t stlink_probe_usb(stlink_t **stdevs[]); void stlink_probe_usb_free(stlink_t **stdevs[], size_t size); diff --git a/tests/usb.c b/tests/usb.c index 03c135f00..9e6e0d0c4 100644 --- a/tests/usb.c +++ b/tests/usb.c @@ -26,7 +26,7 @@ int main(int ac, char** av) { return(0); } - sl = stlink_open_usb(10, reset, NULL, 0); + sl = stlink_open_usb(10, 0, reset, NULL, 0); if (sl != NULL) { printf("-- version\n");