]> sigrok.org Git - libsigrok.git/blobdiff - hardware/hantek-dso/dso.c
sr/drivers: change driver dev_open/dev_close calls to use sdi
[libsigrok.git] / hardware / hantek-dso / dso.c
index 88158674ad188b57f8ac5bead3aaaea0cf56d776..98a326c0cb4fe363a62e51afdfed5652a41477aa 100644 (file)
@@ -19,8 +19,8 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "sigrok.h"
-#include "sigrok-internal.h"
+#include "libsigrok.h"
+#include "libsigrok-internal.h"
 #include "config.h"
 #include "dso.h"
 #include <string.h>
@@ -28,7 +28,7 @@
 #include <libusb.h>
 
 extern libusb_context *usb_context;
-extern GSList *dev_insts;
+extern struct sr_dev_driver hantek_dso_driver_info;
 
 
 static int send_begin(struct context *ctx)
@@ -104,16 +104,13 @@ err:
        return mps;
 }
 
-SR_PRIV int dso_open(int dev_index)
+SR_PRIV int dso_open(struct sr_dev_inst *sdi)
 {
        libusb_device **devlist;
        struct libusb_device_descriptor des;
-       struct sr_dev_inst *sdi;
        struct context *ctx;
        int err, skip, i;
 
-       if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
-               return SR_ERR_ARG;
        ctx = sdi->priv;
 
        if (sdi->status == SR_ST_ACTIVE)
@@ -133,7 +130,7 @@ SR_PRIV int dso_open(int dev_index)
                        continue;
 
                if (sdi->status == SR_ST_INITIALIZING) {
-                       if (skip != dev_index) {
+                       if (skip != sdi->index) {
                                /* Skip devices of this type that aren't the one we want. */
                                skip += 1;
                                continue;
@@ -640,9 +637,11 @@ SR_PRIV int dso_init(struct context *ctx)
        return SR_OK;
 }
 
-SR_PRIV uint8_t dso_get_capturestate(struct context *ctx)
+SR_PRIV int dso_get_capturestate(struct context *ctx, uint8_t *capturestate,
+               uint32_t *trigger_offset)
 {
-       int ret, tmp;
+       int ret, tmp, i;
+       unsigned int bitvalue, toff;
        uint8_t cmdstring[2], inbuf[512];
 
        sr_dbg("hantek-dso: sending CMD_GET_CAPTURESTATE");
@@ -652,20 +651,35 @@ SR_PRIV uint8_t dso_get_capturestate(struct context *ctx)
 
        if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) {
                sr_dbg("Failed to send get_capturestate command: %d", ret);
-               return CAPTURE_UNKNOWN;
+               return SR_ERR;
        }
 
        if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
                        DSO_EP_IN | LIBUSB_ENDPOINT_IN,
                        inbuf, 512, &tmp, 100)) != 0) {
                sr_dbg("Failed to get capturestate: %d", ret);
-               return CAPTURE_UNKNOWN;
+               return SR_ERR;
        }
+       *capturestate = inbuf[0];
+       toff = (inbuf[1] << 16) | (inbuf[3] << 8) | inbuf[2];
 
-       return inbuf[0];
+       /* This conversion comes from the openhantek project.
+        * Each set bit in the 24-bit value inverts all bits with a lower
+        * value. No idea why the device reports the trigger point this way.
+        */
+       bitvalue = 1;
+       for (i = 0; i < 24; i++) {
+               /* Each set bit inverts all bits with a lower value. */
+               if(toff & bitvalue)
+                       toff ^= bitvalue - 1;
+               bitvalue <<= 1;
+       }
+       *trigger_offset = toff;
+
+       return SR_OK;
 }
 
-SR_PRIV uint8_t dso_capture_start(struct context *ctx)
+SR_PRIV int dso_capture_start(struct context *ctx)
 {
        int ret;
        uint8_t cmdstring[2];