Merge pull request #368 from meisteg/l4_memory_map
[fw/stlink] / src / stlink-usb.c
index 4a62d9dd5ff12d4f27e0b63df13c4f685d2ef0b4..0911ff7b939b05bf47a9340763c0b377c31a32e0 100644 (file)
@@ -5,6 +5,8 @@
 #include <sys/time.h>
 #include <sys/types.h>
 #include <libusb.h>
+#include <errno.h>
+#include <unistd.h>
 
 #include "stlink-common.h"
 #include "stlink-usb.h"
@@ -432,7 +434,7 @@ void _stlink_usb_jtag_reset(stlink_t * sl, int value) {
 
     cmd[i++] = STLINK_DEBUG_COMMAND;
     cmd[i++] = STLINK_JTAG_DRIVE_NRST;
-    cmd[i++] = (value)?0:1;
+    cmd[i++] = value;
 
     size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
     if (size == -1) {
@@ -727,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;
@@ -746,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;
@@ -770,13 +771,27 @@ stlink_t* stlink_open_usb(const int verbose, int reset) {
         devAddr=atoi(c);
         ILOG("bus %03d dev %03d\n",devBus, devAddr);
     }
-    while (cnt){
-        cnt--;
+    while (cnt--){
         libusb_get_device_descriptor( list[cnt], &desc );
         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;
@@ -789,7 +804,8 @@ stlink_t* stlink_open_usb(const int verbose, int reset) {
     } else {
         int error = libusb_open(list[cnt], &slu->usb_handle);
         if( error !=0 ) {
-            WLOG("Error %d opening ST-Link/V2 device %03d:%03d\n", error, libusb_get_bus_number(list[cnt]), libusb_get_device_address(list[cnt]));
+            WLOG("Error %d (%s) opening ST-Link/V2 device %03d:%03d\n", 
+               error, strerror (errno), libusb_get_bus_number(list[cnt]), libusb_get_device_address(list[cnt]));
             goto on_error;
         }
     }
@@ -864,11 +880,10 @@ stlink_t* stlink_open_usb(const int verbose, int reset) {
 
     if (reset) {
         stlink_reset(sl);
+        usleep(10000);
     }
-    stlink_load_device_params(sl);
     stlink_version(sl);
-
-    error = 0;
+    error = stlink_load_device_params(sl);
 
 on_libusb_error:
     if (devs != NULL) {
@@ -888,6 +903,6 @@ on_error:
         libusb_exit(slu->libusb_ctx);
     if (sl != NULL) free(sl);
     if (slu != NULL) free(slu);
-    return 0;
+    return NULL;
 }