Added parameter to specify one stlink v2 of many
authorGeorg von Zengen <vonzengen@ibr.cs.tu-bs.de>
Mon, 7 Dec 2015 16:23:15 +0000 (17:23 +0100)
committerGeorg von Zengen <vonzengen@ibr.cs.tu-bs.de>
Mon, 7 Dec 2015 16:44:52 +0000 (17:44 +0100)
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.

flash/main.c
gdbserver/gdb-server.c
gui/stlink-gui.c
src/st-info.c
src/st-term.c
src/stlink-usb.c
src/stlink-usb.h
src/test_usb.c

index 2679bbbce930d0f508fa5d664b4cd48289da233a..d0d303a0dc8c0e545c1b11571f0b5a15a7420c75 100644 (file)
@@ -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 <size>");
+    puts("stlinkv1 command line: ./st-flash [--debug] [--reset] [--serial <iSerial>] {read|write} /dev/sgX path addr <size>");
     puts("stlinkv1 command line: ./st-flash [--debug] /dev/sgX erase");
-    puts("stlinkv2 command line: ./st-flash [--debug] [--reset] {read|write} path addr <size>");
-    puts("stlinkv2 command line: ./st-flash [--debug] erase");
-    puts("                       use hex format for addr and <size>");
+    puts("stlinkv2 command line: ./st-flash [--debug] [--reset] [--serial <iSerial>] {read|write} path addr <size>");
+    puts("stlinkv2 command line: ./st-flash [--debug] [--serial <iSerial>] erase");
+    puts("                       use hex format for addr, <iSerial> and <size>");
 }
 
 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;
     }
index 27b8010293ae368968cf8ecff6c8cd91bf876885..b0245320edfeab207884e3438eb5be61917f4999 100644 (file)
@@ -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:
index f60e92db068950edbc843e53ff7cc09c304ecad6..93f92c4629531f2aba904afb8da4baf2369d78e2 100644 (file)
@@ -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;
index 6fe2f8d7a5ab70ab56597b77dd0291a3da548d79..66d9fb391776556a0d2f30bf291b1d2709404dfc 100644 (file)
@@ -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;
 }
 
index af7b85ee1df143481647d7832b15427eecb1349a..245ed5949d893188f77e52fc22d7633067951241 100644 (file)
@@ -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);
index d8b6c4673318a889147e6c8d76fcdac5de701f98..546f722ea45c89d079086be140567dd78543e622 100644 (file)
@@ -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;
index 58b60e430694e146ae7827c70e01bfe31aeb38a3..d7e745f514bc0af57e325a46244c8ac337b10f85 100644 (file)
@@ -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
index eb8e54cdd82031082d8ad39361c2d95db6443891..787ac12ce05e0544cbecc0867f25138ebe47f04f 100644 (file)
@@ -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);