Update MinGW instructions for USB 3.0 hub support.
[fw/stlink] / src / stlink-usb.c
index 9895da85573c166209767513ae97ae8d300cfb7a..8e910bcf289b20432512116df2731e50e85a8d0e 100644 (file)
@@ -150,7 +150,7 @@ ssize_t send_recv(struct stlink_libusb* handle, int terminate,
             (handle->rep_trans, handle->usb_handle,
              handle->ep_rep, sg_buf, 13, NULL, NULL, 0);
         res = submit_wait(handle, handle->rep_trans);
-       /* The STLink doesn't seem to evaluate the sequence number */
+        /* The STLink doesn't seem to evaluate the sequence number */
         handle->sg_transfer_idx++;
         if (res ) return -1;
     }
@@ -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;
@@ -737,23 +766,23 @@ stlink_t* stlink_open_usb(const int verbose) {
     
     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;
-       }
-       devBus=atoi(device);
-       *c++=0;
-       devAddr=atoi(c);
-       ILOG("bus %03d dev %03d\n",devBus, devAddr);
+        char *c = strchr(device,':');
+        if (c==NULL) {
+            WLOG("STLINK_DEVICE must be <USB_BUS>:<USB_ADDR> format\n");
+            goto on_error;
+        }
+        devBus=atoi(device);
+        *c++=0;
+        devAddr=atoi(c);
+        ILOG("bus %03d dev %03d\n",devBus, devAddr);
     }
     while (cnt){
-       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_32L_PID) || (desc.idProduct == USB_STLINK_NUCLEO_PID) ) break;
         if (desc.idProduct == USB_STLINK_PID) {
             slu->protocoll = 1;
             break;
@@ -761,13 +790,14 @@ stlink_t* stlink_open_usb(const int verbose) {
     }
     
     if (cnt < 0) {
-           WLOG ("Couldn't find %s ST-Link/V2 devices\n",(devBus && devAddr)?"matched":"any");
-           goto on_error;
+        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;
-       }
+        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]));
+            goto on_error;
+        }
     }
     
     libusb_free_device_list(list, 1);
@@ -814,9 +844,14 @@ stlink_t* stlink_open_usb(const int verbose) {
         WLOG("libusb_alloc_transfer failed\n");
         goto on_libusb_error;
     }
+
     // TODO - could use the scanning techniq from stm8 code here...
     slu->ep_rep = 1 /* ep rep */ | LIBUSB_ENDPOINT_IN;
-    slu->ep_req = 2 /* ep req */ | LIBUSB_ENDPOINT_OUT;
+    if (desc.idProduct == USB_STLINK_NUCLEO_PID) {
+       slu->ep_req = 1 /* ep req */ | LIBUSB_ENDPOINT_OUT;
+    } else {
+       slu->ep_req = 2 /* ep req */ | LIBUSB_ENDPOINT_OUT;
+    }
 
     slu->sg_transfer_idx = 0;
     // TODO - never used at the moment, always CMD_SIZE
@@ -833,7 +868,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);