X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fhantek-dso%2Fdso.c;h=98a326c0cb4fe363a62e51afdfed5652a41477aa;hb=a5b35a167a32ffbaee1ce9c0de8501f781f733d1;hp=1d59017970e7efc90f115a73c3c26740b8803ce8;hpb=2715c0b86bafed70dc1d3737b59750570951f507;p=libsigrok.git
diff --git a/hardware/hantek-dso/dso.c b/hardware/hantek-dso/dso.c
index 1d590179..98a326c0 100644
--- a/hardware/hantek-dso/dso.c
+++ b/hardware/hantek-dso/dso.c
@@ -19,17 +19,16 @@
* along with this program. If not, see .
*/
-#include "sigrok.h"
-#include "sigrok-internal.h"
+#include "libsigrok.h"
+#include "libsigrok-internal.h"
#include "config.h"
#include "dso.h"
#include
#include
#include
-#include
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)
@@ -105,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)
@@ -134,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;
@@ -223,8 +219,8 @@ static int get_channel_offsets(struct context *ctx)
*/
for (chan = 0; chan < 2; chan++) {
for (v = 0; v < 9; v++) {
- ctx->channel_levels[chan][v][0] = ntohs(ctx->channel_levels[chan][v][0]);
- ctx->channel_levels[chan][v][1] = ntohs(ctx->channel_levels[chan][v][1]);
+ ctx->channel_levels[chan][v][0] = g_ntohs(ctx->channel_levels[chan][v][0]);
+ ctx->channel_levels[chan][v][1] = g_ntohs(ctx->channel_levels[chan][v][1]);
}
}
@@ -443,12 +439,12 @@ SR_PRIV int dso_set_voltage(struct context *ctx)
case VDIV_2V:
case VDIV_200MV:
case VDIV_20MV:
- cmdstring[2] |= 0x08;
+ cmdstring[2] |= 0x04;
break;
case VDIV_5V:
case VDIV_500MV:
case VDIV_50MV:
- cmdstring[2] |= 0x04;
+ cmdstring[2] |= 0x08;
break;
}
@@ -469,29 +465,27 @@ SR_PRIV int dso_set_voltage(struct context *ctx)
SR_PRIV int dso_set_relays(struct context *ctx)
{
- int ret, cv1, cv2;
- uint8_t relays[] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01,
+ GString *gs;
+ int ret, i;
+ uint8_t relays[17] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
sr_dbg("hantek-dso: preparing CTRL_SETRELAYS");
- cv1 = ctx->voltage_ch1 / 3;
- cv2 = ctx->voltage_ch2 / 3;
-
- if (cv1 > 0)
+ if (ctx->voltage_ch1 < VDIV_1V)
relays[1] = ~relays[1];
- if (cv1 > 1)
+ if (ctx->voltage_ch1 < VDIV_100MV)
relays[2] = ~relays[2];
sr_dbg("hantek-dso: CH1 coupling %d", ctx->coupling_ch1);
if (ctx->coupling_ch1 != COUPLING_AC)
relays[3] = ~relays[3];
- if (cv2 > 0)
+ if (ctx->voltage_ch2 < VDIV_1V)
relays[4] = ~relays[4];
- if (cv2 > 1)
+ if (ctx->voltage_ch2 < VDIV_100MV)
relays[5] = ~relays[5];
sr_dbg("hantek-dso: CH2 coupling %d", ctx->coupling_ch1);
@@ -501,9 +495,18 @@ SR_PRIV int dso_set_relays(struct context *ctx)
if (!strcmp(ctx->triggersource, "EXT"))
relays[7] = ~relays[7];
+ if (sr_log_loglevel_get() >= SR_LOG_DBG) {
+ gs = g_string_sized_new(128);
+ g_string_printf(gs, "hantek-dso: relays:");
+ for (i = 0; i < 17; i++)
+ g_string_append_printf(gs, " %.2x", relays[i]);
+ sr_dbg(gs->str);
+ g_string_free(gs, TRUE);
+ }
+
if ((ret = libusb_control_transfer(ctx->usb->devhdl,
LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS,
- 0, 0, relays, sizeof(relays), 100)) != sizeof(relays)) {
+ 0, 0, relays, 17, 100)) != sizeof(relays)) {
sr_err("failed to set relays: %d", ret);
return SR_ERR;
}
@@ -634,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");
@@ -646,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];