Fix all compilation warnings
[fw/stlink] / src / stlink-usb.c
index 825ee0aeaf3897caae788b7aab16a42aae88b228..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;
@@ -754,12 +783,15 @@ stlink_t* stlink_open_usb(const int verbose) {
         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 (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;
+    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]));
@@ -830,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);