Fix all compilation warnings
[fw/stlink] / src / stlink-usb.c
index 277431a872979c6ef18d512882321701678d6b3f..ec4053ac7bd050bd3d4f0f91b750084902e7a94a 100644 (file)
@@ -203,6 +203,34 @@ void _stlink_usb_version(stlink_t *sl) {
     }
 }
 
+int32_t _stlink_usb_target_voltage(stlink_t *sl) {
+    struct stlink_libusb * const slu = sl->backend_data;
+    unsigned char* const rdata = sl->q_buf;
+    unsigned char* const cmd  = sl->c_buf;
+    ssize_t size;
+    uint32_t rep_len = 8;
+    int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
+    uint32_t factor, reading;
+    int voltage;
+
+    cmd[i++] = STLINK_GET_TARGET_VOLTAGE;
+
+    size = send_recv(slu, 1, cmd, slu->cmd_len, rdata, rep_len);
+    if (size == -1) {
+        printf("[!] send_recv\n");
+        return -1;
+    } else if (size != 8) {
+        printf("[!] wrong length\n");
+        return -1;
+    }
+
+    factor = (rdata[3] << 24) | (rdata[2] << 16) | (rdata[1] << 8) | (rdata[0] << 0);
+    reading = (rdata[7] << 24) | (rdata[6] << 16) | (rdata[5] << 8) | (rdata[4] << 0);
+    voltage = 2400 * reading / factor;
+
+    return voltage;
+}
+
 uint32_t _stlink_usb_read_debug32(stlink_t *sl, uint32_t addr) {
     struct stlink_libusb * const slu = sl->backend_data;
     unsigned char* const rdata = sl->q_buf;
@@ -700,11 +728,12 @@ stlink_backend_t _stlink_usb_backend = {
     _stlink_usb_write_reg,
     _stlink_usb_step,
     _stlink_usb_current_mode,
-    _stlink_usb_force_debug
+    _stlink_usb_force_debug,
+    _stlink_usb_target_voltage
 };
 
 
-stlink_t* stlink_open_usb(const int verbose) {
+stlink_t* stlink_open_usb(const int verbose, int reset) {
     stlink_t* sl = NULL;
     struct stlink_libusb* slu = NULL;
     int error = -1;
@@ -729,15 +758,49 @@ stlink_t* stlink_open_usb(const int verbose) {
         goto on_error;
     }
     
-    slu->usb_handle = libusb_open_device_with_vid_pid(slu->libusb_ctx, USB_ST_VID, USB_STLINK_32L_PID);
-    if (slu->usb_handle == NULL) {
-       slu->usb_handle = libusb_open_device_with_vid_pid(slu->libusb_ctx, USB_ST_VID, USB_STLINK_PID);
-       if (slu->usb_handle == NULL) {
-           WLOG("Couldn't find any ST-Link/V2 devices\n");
+    libusb_device **list;
+    int cnt = libusb_get_device_list(slu->libusb_ctx, &list);
+    struct libusb_device_descriptor desc;
+    int devBus =0;
+    int devAddr=0;
+    
+    char *device = getenv("STLINK_DEVICE");
+    if (device) {
+       char *c = strchr(device,':');
+       if (c==NULL) {
+           WLOG("STLINK_DEVICE must be <USB_BUS>:<USB_ADDR> format\n");
            goto on_error;
        }
-       slu->protocoll = 1;
+       devBus=atoi(device);
+       *c++=0;
+       devAddr=atoi(c);
+       ILOG("bus %03d dev %03d\n",devBus, devAddr);
+    }
+    while (cnt){
+       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) break;
+        if (desc.idProduct == USB_STLINK_PID) {
+            slu->protocoll = 1;
+            break;
+        }
     }
+    
+    if (cnt < 0) {
+           WLOG ("Couldn't find %s ST-Link/V2 devices\n",(devBus && devAddr)?"matched":"any");
+           goto on_error;
+    } else {
+       if( libusb_open(list[cnt], &slu->usb_handle) !=0){
+           WLOG("Couldn't open ST-Link/V2 device %03d:%03d\n",libusb_get_bus_number(list[cnt]), libusb_get_device_address(list[cnt]));
+           goto on_error;
+       }
+    }
+    
+    libusb_free_device_list(list, 1);
+    
 
     if (libusb_kernel_driver_active(slu->usb_handle, 0) == 1) {
         int r;
@@ -799,7 +862,9 @@ stlink_t* stlink_open_usb(const int verbose) {
       stlink_enter_swd_mode(sl);
     }
 
-    stlink_reset(sl);
+    if (reset) {
+        stlink_reset(sl);
+    }
     stlink_load_device_params(sl);
     stlink_version(sl);