From: Georg von Zengen Date: Mon, 7 Dec 2015 16:23:15 +0000 (+0100) Subject: Added parameter to specify one stlink v2 of many X-Git-Url: https://git.gag.com/?p=fw%2Fstlink;a=commitdiff_plain;h=06fd9c4b700e681e04ec78f5664fd55674f94b6f Added parameter to specify one stlink v2 of many This adds a parameter to the function stlink_open_usb and to the binary st-flash to specify one of multiple connected stlinks. As the identifier the iSerial of the stlink is used. If no serial is given the function and binary behave as before. --- diff --git a/flash/main.c b/flash/main.c index 2679bbb..d0d303a 100644 --- a/flash/main.c +++ b/flash/main.c @@ -17,6 +17,7 @@ struct opts { enum st_cmds cmd; const char* devname; + char *serial; const char* filename; stm32_addr_t addr; size_t size; @@ -26,11 +27,11 @@ struct opts static void usage(void) { - puts("stlinkv1 command line: ./st-flash [--debug] [--reset] {read|write} /dev/sgX path addr "); + puts("stlinkv1 command line: ./st-flash [--debug] [--reset] [--serial ] {read|write} /dev/sgX path addr "); puts("stlinkv1 command line: ./st-flash [--debug] /dev/sgX erase"); - puts("stlinkv2 command line: ./st-flash [--debug] [--reset] {read|write} path addr "); - puts("stlinkv2 command line: ./st-flash [--debug] erase"); - puts(" use hex format for addr and "); + puts("stlinkv2 command line: ./st-flash [--debug] [--reset] [--serial ] {read|write} path addr "); + puts("stlinkv2 command line: ./st-flash [--debug] [--serial ] erase"); + puts(" use hex format for addr, and "); } static int get_opts(struct opts* o, int ac, char** av) @@ -64,6 +65,31 @@ static int get_opts(struct opts* o, int ac, char** av) o->reset = 0; } + if (strcmp(av[0], "--serial") == 0) + { + ac--; + av++; + int i=strlen(av[0]); + if(i%2 != 0){ + puts("no valid hex value, length must be multiple of 2\n"); + return -1; + } + int j=0; + while(i>=0 && j<=13){ + char buffer[3]={0}; + memcpy(buffer,&av[0][i],2); + o->serial[12-j] = (char)strtol((const char*)buffer,NULL, 16); + j++; + i-=2; + } + ac--; + av++; + } + else + { + o->serial = NULL; + } + if (ac < 1) return -1; /* stlinkv2 */ @@ -123,6 +149,8 @@ int main(int ac, char** av) { stlink_t* sl = NULL; struct opts o; + char serial_buffer[13] = {0}; + o.serial = serial_buffer; int err = -1; o.size = 0; @@ -141,7 +169,7 @@ int main(int ac, char** av) } else /* stlinkv2 */ { - sl = stlink_open_usb(o.log_level, 1); + sl = stlink_open_usb(o.log_level, 1, o.serial); if (sl == NULL) goto on_error; sl->verbose = o.log_level; } diff --git a/gdbserver/gdb-server.c b/gdbserver/gdb-server.c index 27b8010..b024532 100644 --- a/gdbserver/gdb-server.c +++ b/gdbserver/gdb-server.c @@ -173,7 +173,7 @@ int main(int argc, char** argv) { parse_options(argc, argv, &state); switch (state.stlink_version) { case 2: - sl = stlink_open_usb(state.logging_level, 0); + sl = stlink_open_usb(state.logging_level, 0, NULL); if(sl == NULL) return 1; break; case 1: diff --git a/gui/stlink-gui.c b/gui/stlink-gui.c index f60e92d..93f92c4 100644 --- a/gui/stlink-gui.c +++ b/gui/stlink-gui.c @@ -530,7 +530,7 @@ connect_button_cb (GtkWidget *widget, gpointer data) /* try version 1 then version 2 */ gui->sl = stlink_v1_open(0, 1); if (gui->sl == NULL) { - gui->sl = stlink_open_usb(0, 1); + gui->sl = stlink_open_usb(0, 1, NULL); } if (gui->sl == NULL) { stlink_gui_set_info_error_message (gui, "Failed to connect to STLink."); return; diff --git a/src/st-info.c b/src/st-info.c index 6fe2f8d..66d9fb3 100644 --- a/src/st-info.c +++ b/src/st-info.c @@ -51,7 +51,7 @@ stlink_t* open_sl(void) stlink_t* sl; sl = stlink_v1_open(0, 1); if (sl == NULL) - sl = stlink_open_usb(0, 1); + sl = stlink_open_usb(0, 1, NULL); return sl; } diff --git a/src/st-term.c b/src/st-term.c index af7b85e..245ed59 100644 --- a/src/st-term.c +++ b/src/st-term.c @@ -251,7 +251,7 @@ int main(int ac, char** av) { sig_init(); - sl = stlink_open_usb(10, 1); + sl = stlink_open_usb(10, 1, NULL); if (sl != NULL) { printf("ST-Linky proof-of-concept terminal :: Created by Necromant for lulz\n"); stlink_version(sl); diff --git a/src/stlink-usb.c b/src/stlink-usb.c index d8b6c46..546f722 100644 --- a/src/stlink-usb.c +++ b/src/stlink-usb.c @@ -728,7 +728,7 @@ stlink_backend_t _stlink_usb_backend = { }; -stlink_t* stlink_open_usb(const int verbose, int reset) { +stlink_t* stlink_open_usb(const int verbose, int reset, char *p_usb_iserial) { stlink_t* sl = NULL; struct stlink_libusb* slu = NULL; int error = -1; @@ -747,7 +747,6 @@ stlink_t* stlink_open_usb(const int verbose, int reset) { sl->backend_data = slu; sl->core_stat = STLINK_CORE_STAT_UNKNOWN; - if (libusb_init(&(slu->libusb_ctx))) { WLOG("failed to init libusb context, wrong version of libraries?\n"); goto on_error; @@ -776,7 +775,22 @@ stlink_t* stlink_open_usb(const int verbose, int reset) { if (desc.idVendor!=USB_ST_VID) continue; if (devBus && devAddr) if ((libusb_get_bus_number(list[cnt])!=devBus) || (libusb_get_device_address(list[cnt])!=devAddr)) continue; - if ( (desc.idProduct == USB_STLINK_32L_PID) || (desc.idProduct == USB_STLINK_NUCLEO_PID) ) break; + if ( (desc.idProduct == USB_STLINK_32L_PID) || (desc.idProduct == USB_STLINK_NUCLEO_PID) ){ + if ((p_usb_iserial != NULL)){ + unsigned char buffer[13]; + struct libusb_device_handle* handle; + libusb_open(list[cnt], &handle); + libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, buffer, 13); + libusb_close(handle); + if (memcmp(p_usb_iserial,&buffer,12) == 0){ + break; + }else{ + continue; + } + }else{ + break; + } + } if (desc.idProduct == USB_STLINK_PID) { slu->protocoll = 1; break; diff --git a/src/stlink-usb.h b/src/stlink-usb.h index 58b60e4..d7e745f 100644 --- a/src/stlink-usb.h +++ b/src/stlink-usb.h @@ -30,7 +30,7 @@ extern "C" { unsigned int cmd_len; }; - stlink_t* stlink_open_usb(const int verbose, int reset); + stlink_t* stlink_open_usb(const int verbose, int reset, char *p_usb_iserial); #ifdef __cplusplus diff --git a/src/test_usb.c b/src/test_usb.c index eb8e54c..787ac12 100644 --- a/src/test_usb.c +++ b/src/test_usb.c @@ -10,7 +10,7 @@ int main(int ac, char** av) { ac = ac; av = av; - sl = stlink_open_usb(10, 1); + sl = stlink_open_usb(10, 1, NULL); if (sl != NULL) { printf("-- version\n"); stlink_version(sl);