Merge pull request #367 from meisteg/fix_every_other
[fw/stlink] / src / stlink-usb.c
index 47924c7bef04fa0bef1a9caacd6d463feadbdd0f..0911ff7b939b05bf47a9340763c0b377c31a32e0 100644 (file)
@@ -6,6 +6,7 @@
 #include <sys/types.h>
 #include <libusb.h>
 #include <errno.h>
+#include <unistd.h>
 
 #include "stlink-common.h"
 #include "stlink-usb.h"
@@ -413,8 +414,7 @@ void _stlink_usb_reset(stlink_t * sl) {
     int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
 
     cmd[i++] = STLINK_DEBUG_COMMAND;
-    cmd[i++] = STLINK_DEBUG_HARDRESET;
-    cmd[i++] = 0x2;
+    cmd[i++] = STLINK_DEBUG_RESETSYS;
 
     size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
     if (size == -1) {
@@ -729,7 +729,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;
@@ -748,7 +748,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;
@@ -777,7 +776,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;
@@ -866,6 +880,7 @@ stlink_t* stlink_open_usb(const int verbose, int reset) {
 
     if (reset) {
         stlink_reset(sl);
+        usleep(10000);
     }
     stlink_version(sl);
     error = stlink_load_device_params(sl);