]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/hantek-dso/dso.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / hardware / hantek-dso / dso.c
index da5eb121b3a04ff1fab70c6bfa5358dc9ddb9959..27ef63c1e864b35385973c9750237bbaa3f62ef7 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "libsigrok.h"
-#include "libsigrok-internal.h"
-#include "dso.h"
 #include <string.h>
 #include <glib.h>
 #include <libusb.h>
+#include <libsigrok/libsigrok.h>
+#include "libsigrok-internal.h"
+#include "dso.h"
+
+#define NUM_CHANNELS 2
 
 extern struct sr_dev_driver hantek_dso_driver_info;
 
@@ -109,11 +111,12 @@ err:
 SR_PRIV int dso_open(struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
-       struct drv_context *drvc = hantek_dso_driver_info.priv;
+       struct drv_context *drvc = hantek_dso_driver_info.context;
        struct sr_usb_dev_inst *usb;
        struct libusb_device_descriptor des;
        libusb_device **devlist;
-       int err, skip, i;
+       int err, i;
+       char connection_id[64];
 
        devc = sdi->priv;
        usb = sdi->conn;
@@ -122,7 +125,6 @@ SR_PRIV int dso_open(struct sr_dev_inst *sdi)
                /* already in use */
                return SR_ERR;
 
-       skip = 0;
        libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
        for (i = 0; devlist[i]; i++) {
                if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
@@ -135,20 +137,14 @@ SR_PRIV int dso_open(struct sr_dev_inst *sdi)
                    || des.idProduct != devc->profile->fw_pid)
                        continue;
 
-               if (sdi->status == SR_ST_INITIALIZING) {
-                       if (skip != sdi->index) {
-                               /* Skip devices of this type that aren't the one we want. */
-                               skip += 1;
-                               continue;
-                       }
-               } else if (sdi->status == SR_ST_INACTIVE) {
+               if ((sdi->status == SR_ST_INITIALIZING) ||
+                               (sdi->status == SR_ST_INACTIVE)) {
                        /*
-                        * This device is fully enumerated, so we need to find
-                        * this device by vendor, product, bus and address.
+                        * Check device by its physical USB bus/port address.
                         */
-                       if (libusb_get_bus_number(devlist[i]) != usb->bus
-                               || libusb_get_device_address(devlist[i]) != usb->address)
-                               /* this is not the one */
+                       usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
+                       if (strcmp(sdi->connection_id, connection_id))
+                               /* This is not the one. */
                                continue;
                }
 
@@ -164,9 +160,10 @@ SR_PRIV int dso_open(struct sr_dev_inst *sdi)
                                sr_err("Wrong endpoint profile.");
                        else {
                                sdi->status = SR_ST_ACTIVE;
-                               sr_info("Opened device %d on %d.%d interface %d.",
-                                       sdi->index, usb->bus,
-                                       usb->address, USB_INTERFACE);
+                               sr_info("Opened device on %d.%d (logical) / "
+                                               "%s (physical) interface %d.",
+                                       usb->bus, usb->address,
+                                       sdi->connection_id, USB_INTERFACE);
                        }
                } else {
                        sr_err("Failed to open device: %s.",
@@ -190,11 +187,11 @@ SR_PRIV void dso_close(struct sr_dev_inst *sdi)
 
        usb = sdi->conn;
 
-       if (usb->devhdl == NULL)
+       if (!usb->devhdl)
                return;
 
-       sr_info("Closing device %d on %d.%d interface %d.", sdi->index,
-               usb->bus, usb->address, USB_INTERFACE);
+       sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
+                       usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
        libusb_release_interface(usb->devhdl, USB_INTERFACE);
        libusb_close(usb->devhdl);
        usb->devhdl = NULL;
@@ -230,7 +227,7 @@ static int get_channel_offsets(const struct sr_dev_inst *sdi)
         * since that's how voltage offsets are submitted back to the DSO.
         * Convert to host order now, so we can use them natively.
         */
-       for (chan = 0; chan < 2; chan++) {
+       for (chan = 0; chan < NUM_CHANNELS; chan++) {
                for (v = 0; v < 9; v++) {
                        devc->channel_levels[chan][v][0] =
                                g_ntohs(devc->channel_levels[chan][v][0]);
@@ -241,7 +238,7 @@ static int get_channel_offsets(const struct sr_dev_inst *sdi)
 
        if (sr_log_loglevel_get() >= SR_LOG_DBG) {
                gs = g_string_sized_new(128);
-               for (chan = 0; chan < 2; chan++) {
+               for (chan = 0; chan < NUM_CHANNELS; chan++) {
                        g_string_printf(gs, "CH%d:", chan + 1);
                        for (v = 0; v < 9; v++) {
                                g_string_append_printf(gs, " %.4x-%.4x",
@@ -388,19 +385,18 @@ static int dso_set_filters(const struct sr_dev_inst *sdi)
        memset(cmdstring, 0, sizeof(cmdstring));
        cmdstring[0] = CMD_SET_FILTERS;
        cmdstring[1] = 0x0f;
-       if (devc->filter_ch1) {
+       if (devc->filter[0]) {
                sr_dbg("Turning on CH1 filter.");
                cmdstring[2] |= 0x80;
        }
-       if (devc->filter_ch2) {
+       if (devc->filter[1]) {
                sr_dbg("Turning on CH2 filter.");
                cmdstring[2] |= 0x40;
        }
-       if (devc->filter_trigger) {
-               /* TODO: supported on the DSO-2090? */
-               sr_dbg("Turning on trigger filter.");
-               cmdstring[2] |= 0x20;
-       }
+       /*
+        * Not supported: filtering on the trigger
+        * cmdstring[2] |= 0x20;
+        */
 
        if (send_begin(sdi) != SR_OK)
                return SR_ERR;
@@ -433,8 +429,8 @@ static int dso_set_voltage(const struct sr_dev_inst *sdi)
        cmdstring[2] = 0x30;
 
        /* CH1 volts/div is encoded in bits 0-1 */
-       sr_dbg("CH1 vdiv index: %d.", devc->voltage_ch1);
-       switch (devc->voltage_ch1) {
+       sr_dbg("CH1 vdiv index: %d.", devc->voltage[0]);
+       switch (devc->voltage[0]) {
        case VDIV_1V:
        case VDIV_100MV:
        case VDIV_10MV:
@@ -453,8 +449,8 @@ static int dso_set_voltage(const struct sr_dev_inst *sdi)
        }
 
        /* CH2 volts/div is encoded in bits 2-3 */
-       sr_dbg("CH2 vdiv index: %d.", devc->voltage_ch2);
-       switch (devc->voltage_ch2) {
+       sr_dbg("CH2 vdiv index: %d.", devc->voltage[1]);
+       switch (devc->voltage[1]) {
        case VDIV_1V:
        case VDIV_100MV:
        case VDIV_10MV:
@@ -499,24 +495,24 @@ static int dso_set_relays(const struct sr_dev_inst *sdi)
        devc = sdi->priv;
        usb = sdi->conn;
 
-       if (devc->voltage_ch1 < VDIV_1V)
+       if (devc->voltage[0] < VDIV_1V)
                relays[1] = ~relays[1];
 
-       if (devc->voltage_ch1 < VDIV_100MV)
+       if (devc->voltage[0] < VDIV_100MV)
                relays[2] = ~relays[2];
 
-       sr_dbg("CH1 coupling: %d.", devc->coupling_ch1);
-       if (devc->coupling_ch1 != COUPLING_AC)
+       sr_dbg("CH1 coupling: %d.", devc->coupling[0]);
+       if (devc->coupling[0] != COUPLING_AC)
                relays[3] = ~relays[3];
 
-       if (devc->voltage_ch2 < VDIV_1V)
+       if (devc->voltage[1] < VDIV_1V)
                relays[4] = ~relays[4];
 
-       if (devc->voltage_ch2 < VDIV_100MV)
+       if (devc->voltage[1] < VDIV_100MV)
                relays[5] = ~relays[5];
 
-       sr_dbg("CH2 coupling: %d.", devc->coupling_ch1);
-       if (devc->coupling_ch2 != COUPLING_AC)
+       sr_dbg("CH2 coupling: %d.", devc->coupling[1]);
+       if (devc->coupling[1] != COUPLING_AC)
                relays[6] = ~relays[6];
 
        if (!strcmp(devc->triggersource, "EXT"))
@@ -557,7 +553,7 @@ static int dso_set_voffsets(const struct sr_dev_inst *sdi)
 
        memset(offsets, 0, sizeof(offsets));
        /* Channel 1 */
-       ch_levels = devc->channel_levels[0][devc->voltage_ch1];
+       ch_levels = devc->channel_levels[0][devc->voltage[0]];
        offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch1 + ch_levels[0];
        offsets[0] = (offset >> 8) | 0x20;
        offsets[1] = offset & 0xff;
@@ -565,7 +561,7 @@ static int dso_set_voffsets(const struct sr_dev_inst *sdi)
               offsets[0], offsets[1]);
 
        /* Channel 2 */
-       ch_levels = devc->channel_levels[1][devc->voltage_ch2];
+       ch_levels = devc->channel_levels[1][devc->voltage[1]];
        offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch2 + ch_levels[0];
        offsets[2] = (offset >> 8) | 0x20;
        offsets[3] = offset & 0xff;
@@ -709,7 +705,7 @@ SR_PRIV int dso_get_capturestate(const struct sr_dev_inst *sdi,
        bitvalue = 1;
        for (i = 0; i < 24; i++) {
                /* Each set bit inverts all bits with a lower value. */
-               if(toff & bitvalue)
+               if (toff & bitvalue)
                        toff ^= bitvalue - 1;
                bitvalue <<= 1;
        }