Merge pull request #347 from jauler/master
[fw/stlink] / src / stlink-usb.c
index 4a62d9dd5ff12d4f27e0b63df13c4f685d2ef0b4..d8b6c4673318a889147e6c8d76fcdac5de701f98 100644 (file)
@@ -5,6 +5,7 @@
 #include <sys/time.h>
 #include <sys/types.h>
 #include <libusb.h>
+#include <errno.h>
 
 #include "stlink-common.h"
 #include "stlink-usb.h"
@@ -432,7 +433,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) {
@@ -770,8 +771,7 @@ 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)
@@ -789,7 +789,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;
         }
     }
@@ -865,10 +866,8 @@ stlink_t* stlink_open_usb(const int verbose, int reset) {
     if (reset) {
         stlink_reset(sl);
     }
-    stlink_load_device_params(sl);
     stlink_version(sl);
-
-    error = 0;
+    error = stlink_load_device_params(sl);
 
 on_libusb_error:
     if (devs != NULL) {
@@ -888,6 +887,6 @@ on_error:
         libusb_exit(slu->libusb_ctx);
     if (sl != NULL) free(sl);
     if (slu != NULL) free(slu);
-    return 0;
+    return NULL;
 }