]> sigrok.org Git - libsigrok.git/blobdiff - hardware/hantek-dso/dso.c
sr: moved sigrok.h so libsigrok/libsigrok.h
[libsigrok.git] / hardware / hantek-dso / dso.c
index 278b2ebef021094a8d41bea16dd49c8a45aeb3b2..77cad923e1554ce514423d2eabfe2641ac556544 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>
@@ -200,7 +200,8 @@ SR_PRIV void dso_close(struct sr_dev_inst *sdi)
 
 static int get_channel_offsets(struct context *ctx)
 {
-       int ret;
+       GString *gs;
+       int chan, v, ret;
 
        sr_dbg("hantek-dso: getting channel offsets");
 
@@ -214,6 +215,32 @@ static int get_channel_offsets(struct context *ctx)
                return SR_ERR;
        }
 
+       /* Comes in as 16-bit numbers with the second byte always 0 on
+        * the DSO-2090. Guessing this is supposed to be big-endian,
+        * 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 (v = 0; v < 9; v++) {
+                       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]);
+               }
+       }
+
+       if (sr_log_loglevel_get() >= SR_LOG_DBG) {
+               gs = g_string_sized_new(128);
+               for (chan = 0; chan < 2; chan++) {
+                       g_string_printf(gs, "hantek-dso: CH%d:", chan + 1);
+                       for (v = 0; v < 9; v++) {
+                               g_string_append_printf(gs, " %.4x-%.4x",
+                                               ctx->channel_levels[chan][v][0],
+                                               ctx->channel_levels[chan][v][1]);
+                       }
+                       sr_dbg(gs->str);
+               }
+               g_string_free(gs, TRUE);
+       }
+
        return SR_OK;
 }
 
@@ -221,49 +248,83 @@ SR_PRIV int dso_set_trigger_samplerate(struct context *ctx)
 {
        int ret, tmp;
        uint8_t cmdstring[12];
-       uint8_t timebasefast_small[] = {1, 2, 3, 4};
-       uint8_t timebasefast_large[] = {0, 0, 2, 3};
        uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce,
                        0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 };
        uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8,
                        0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 };
 
-       sr_dbg("hantek-dso: sending CMD_SET_TRIGGER_SAMPLERATE");
+       sr_dbg("hantek-dso: preparing CMD_SET_TRIGGER_SAMPLERATE");
 
        memset(cmdstring, 0, sizeof(cmdstring));
        /* Command */
        cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE;
 
        /* Trigger source */
-       cmdstring[2] = (ctx->triggersource & 0x03) << 6;
-
-       /* Frame size */
-       cmdstring[2] |= (ctx->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 3;
-
-       /* Timebase fast (no idea what this means) */
-       if (ctx->timebase < TIME_20us)
+       sr_dbg("hantek-dso: trigger source %s", ctx->triggersource);
+       if (!strcmp("CH2", ctx->triggersource))
                tmp = 0;
-       else if (ctx->timebase > TIME_200us)
-               tmp = 4;
+       else if (!strcmp("CH1", ctx->triggersource))
+               tmp = 1;
+       else if (!strcmp("EXT", ctx->triggersource))
+               tmp = 2;
        else {
-               if (ctx->framesize == FRAMESIZE_SMALL)
-                       tmp = timebasefast_small[ctx->timebase - 1];
-               else
-                       tmp = timebasefast_large[ctx->timebase - 1];
+               sr_err("hantek-dso: invalid trigger source %s", ctx->triggersource);
+               return SR_ERR_ARG;
        }
-       cmdstring[2] |= tmp & 0x07;
-cmdstring[2] = 0x45;
-       /* Selected channel */
-       cmdstring[3] = (ctx->selected_channel & 0x03) << 6;
-cmdstring[3] = 0x02;
+       cmdstring[2] = tmp;
 
-       /* TODO: Fast rates channel */
-       cmdstring[3] |= 0 << 5;
+       /* Frame size */
+       sr_dbg("hantek-dso: frame size %d", ctx->framesize);
+       cmdstring[2] |= (ctx->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2;
+
+       /* Timebase fast */
+       sr_dbg("hantek-dso: time base index %d", ctx->timebase);
+       switch (ctx->framesize) {
+       case FRAMESIZE_SMALL:
+               if (ctx->timebase < TIME_20us)
+                       tmp = 0;
+               else if (ctx->timebase == TIME_20us)
+                       tmp = 1;
+               else if (ctx->timebase == TIME_40us)
+                       tmp = 2;
+               else if (ctx->timebase == TIME_100us)
+                       tmp = 3;
+               else if (ctx->timebase >= TIME_200us)
+                       tmp = 4;
+               break;
+       case FRAMESIZE_LARGE:
+               if (ctx->timebase < TIME_40us) {
+                       sr_err("hantek-dso: timebase < 40us only supported with 10K buffer");
+                       return SR_ERR_ARG;
+               }
+               else if (ctx->timebase == TIME_40us)
+                       tmp = 0;
+               else if (ctx->timebase == TIME_100us)
+                       tmp = 2;
+               else if (ctx->timebase == TIME_200us)
+                       tmp = 3;
+               else if (ctx->timebase >= TIME_400us)
+                       tmp = 4;
+               break;
+       }
+       cmdstring[2] |= (tmp & 0x07) << 5;
+
+       /* Enabled channels: 00=CH1 01=CH2 10=both */
+       sr_dbg("hantek-dso: channels CH1=%d CH2=%d", ctx->ch1_enabled, ctx->ch2_enabled);
+       tmp = (((ctx->ch2_enabled ? 1 : 0) << 1) + (ctx->ch1_enabled ? 1 : 0)) - 1;
+       cmdstring[3] = tmp;
 
-       /* Trigger slope */
-       cmdstring[3] |= (ctx->triggerslope ? 1 : 0) << 4;
+       /* Fast rates channel */
+       /* TODO: is this right? */
+       tmp = ctx->timebase < TIME_10us ? 1 : 0;
+       cmdstring[3] |= tmp << 2;
 
-       /* Timebase */
+       /* Trigger slope: 0=positive 1=negative */
+       /* TODO: does this work? */
+       sr_dbg("hantek-dso: trigger slope %d", ctx->triggerslope);
+       cmdstring[3] |= (ctx->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
+
+       /* Timebase slow */
        if (ctx->timebase < TIME_100us)
                tmp = 0;
        else if (ctx->timebase > TIME_400ms)
@@ -274,15 +335,14 @@ cmdstring[3] = 0x02;
                else
                        tmp = timebase_large[ctx->timebase - 3];
        }
-tmp = 0xebff;
+       cmdstring[4] = tmp & 0xff;
+       cmdstring[5] = (tmp >> 8) & 0xff;
+
+       /* Horizontal trigger position */
+       sr_dbg("hantek-dso: trigger position %3.2f", ctx->triggerposition);
+       tmp = 0x77fff + 0x8000 * ctx->triggerposition;
        cmdstring[6] = tmp & 0xff;
        cmdstring[7] = (tmp >> 8) & 0xff;
-
-       /* Trigger position (time) */
-       tmp = 0x77660 + ctx->triggerposition;
-tmp = 0x7006f;
-       cmdstring[8] = tmp & 0xff;
-       cmdstring[9] = (tmp >> 8) & 0xff;
        cmdstring[10] = (tmp >> 16) & 0xff;
 
        if (send_begin(ctx) != SR_OK)
@@ -295,6 +355,7 @@ tmp = 0x7006f;
                sr_err("Failed to set trigger/samplerate: %d", ret);
                return SR_ERR;
        }
+       sr_dbg("hantek-dso: sent CMD_SET_TRIGGER_SAMPLERATE");
 
        return SR_OK;
 }
@@ -304,17 +365,24 @@ SR_PRIV int dso_set_filters(struct context *ctx)
        int ret, tmp;
        uint8_t cmdstring[8];
 
-       sr_dbg("hantek-dso: sending CMD_SET_FILTERS");
+       sr_dbg("hantek-dso: preparing CMD_SET_FILTERS");
 
        memset(cmdstring, 0, sizeof(cmdstring));
        cmdstring[0] = CMD_SET_FILTERS;
        cmdstring[1] = 0x0f;
-       if (ctx->filter_ch1)
+       if (ctx->filter_ch1) {
+               sr_dbg("hantek-dso: turning on CH1 filter");
                cmdstring[2] |= 0x80;
-       if (ctx->filter_ch2)
+       }
+       if (ctx->filter_ch2) {
+               sr_dbg("hantek-dso: turning on CH2 filter");
                cmdstring[2] |= 0x40;
-       if (ctx->filter_trigger)
+       }
+       if (ctx->filter_trigger) {
+               /* TODO: supported on the DSO-2090? */
+               sr_dbg("hantek-dso: turning on trigger filter");
                cmdstring[2] |= 0x20;
+       }
 
        if (send_begin(ctx) != SR_OK)
                return SR_ERR;
@@ -326,6 +394,7 @@ SR_PRIV int dso_set_filters(struct context *ctx)
                sr_err("Failed to set filters: %d", ret);
                return SR_ERR;
        }
+       sr_dbg("hantek-dso: sent CMD_SET_FILTERS");
 
        return SR_OK;
 }
@@ -335,15 +404,52 @@ SR_PRIV int dso_set_voltage(struct context *ctx)
        int ret, tmp;
        uint8_t cmdstring[8];
 
-       sr_dbg("hantek-dso: sending CMD_SET_VOLTAGE");
+       sr_dbg("hantek-dso: preparing CMD_SET_VOLTAGE");
 
        memset(cmdstring, 0, sizeof(cmdstring));
        cmdstring[0] = CMD_SET_VOLTAGE;
        cmdstring[1] = 0x0f;
-       cmdstring[2] = 0x03;
-       cmdstring[2] |= ((2 - ctx->voltage_ch1 % 3) << 6);
-       cmdstring[2] |= ((2 - ctx->voltage_ch2 % 3) << 4);
-cmdstring[2] = 0x30;
+       cmdstring[2] = 0x30;
+
+       /* CH1 volts/div is encoded in bits 0-1 */
+       sr_dbg("hantek-dso: CH1 vdiv index %d", ctx->voltage_ch1);
+       switch (ctx->voltage_ch1) {
+       case VDIV_1V:
+       case VDIV_100MV:
+       case VDIV_10MV:
+               cmdstring[2] |= 0x00;
+               break;
+       case VDIV_2V:
+       case VDIV_200MV:
+       case VDIV_20MV:
+               cmdstring[2] |= 0x01;
+               break;
+       case VDIV_5V:
+       case VDIV_500MV:
+       case VDIV_50MV:
+               cmdstring[2] |= 0x02;
+               break;
+       }
+
+       /* CH2 volts/div is encoded in bits 2-3 */
+       sr_dbg("hantek-dso: CH2 vdiv index %d", ctx->voltage_ch2);
+       switch (ctx->voltage_ch2) {
+       case VDIV_1V:
+       case VDIV_100MV:
+       case VDIV_10MV:
+               cmdstring[2] |= 0x00;
+               break;
+       case VDIV_2V:
+       case VDIV_200MV:
+       case VDIV_20MV:
+               cmdstring[2] |= 0x04;
+               break;
+       case VDIV_5V:
+       case VDIV_500MV:
+       case VDIV_50MV:
+               cmdstring[2] |= 0x08;
+               break;
+       }
 
        if (send_begin(ctx) != SR_OK)
                return SR_ERR;
@@ -355,48 +461,59 @@ cmdstring[2] = 0x30;
                sr_err("Failed to set voltage: %d", ret);
                return SR_ERR;
        }
+       sr_dbg("hantek-dso: sent CMD_SET_VOLTAGE");
 
        return SR_OK;
 }
 
 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: sending CTRL_SETRELAYS");
+       sr_dbg("hantek-dso: preparing CTRL_SETRELAYS");
 
-       cv1 = ctx->voltage_ch1 / 3;
-       cv2 = ctx->voltage_ch2 / 3;
-relays[0] = 0x01;
-       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);
        if (ctx->coupling_ch2 != COUPLING_AC)
                relays[6] = ~relays[6];
 
-       if (ctx->triggersource == TRIGGER_EXT || ctx->triggersource == TRIGGER_EXT10)
+       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;
        }
+       sr_dbg("hantek-dso: sent CTRL_SETRELAYS");
 
        return SR_OK;
 }
@@ -405,31 +522,33 @@ SR_PRIV int dso_set_voffsets(struct context *ctx)
 {
        int offset, ret;
        uint16_t *ch_levels;
-//     uint8_t offsets[17];
-uint8_t offsets[] = {0x20,0x75,0x30,0x3f,0x20,0xbd,0x3f,0x02,0x20,0x00,0x71,0x01,0x2e,0x0b,0x3f,0x02,0x50};
-//uint8_t offsets[] = {0xff, 0x75, 0x30, 0x3f, 0x20, 0xbd,
-//             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
-//};
-
-       sr_dbg("hantek-dso: sending CTRL_SETOFFSET");
-
-//     memset(offsets, 0, sizeof(offsets));
-//     /* Channel 1 */
-//     ch_levels = ctx->channel_levels[0][VOLTAGE_10mV - ctx->voltage_ch1];
-//     offset = (ch_levels[1] - ch_levels[0]) * ctx->voffset_ch1 + ch_levels[0];
-//     offsets[0] = (offset >> 8) | 0x20;
-//     offsets[1] = offset & 0xff;
-//
-//     /* Channel 2 */
-//     ch_levels = ctx->channel_levels[1][VOLTAGE_10mV - ctx->voltage_ch2];
-//     offset = (ch_levels[1] - ch_levels[0]) * ctx->voffset_ch2 + ch_levels[0];
-//     offsets[2] = (offset >> 8) | 0x20;
-//     offsets[3] = offset & 0xff;
-//
-//     /* Trigger */
-//     offset = MAX_VERT_TRIGGER * ctx->voffset_trigger;
-//     offsets[4] = (offset >> 8) | 0x20;
-//     offsets[5] = offset & 0xff;
+       uint8_t offsets[17];
+
+       sr_dbg("hantek-dso: preparing CTRL_SETOFFSET");
+
+       memset(offsets, 0, sizeof(offsets));
+       /* Channel 1 */
+       ch_levels = ctx->channel_levels[0][ctx->voltage_ch1];
+       offset = (ch_levels[1] - ch_levels[0]) * ctx->voffset_ch1 + ch_levels[0];
+       offsets[0] = (offset >> 8) | 0x20;
+       offsets[1] = offset & 0xff;
+       sr_dbg("hantek-dso: CH1 offset %3.2f (%.2x%.2x)", ctx->voffset_ch1,
+                       offsets[0], offsets[1]);
+
+       /* Channel 2 */
+       ch_levels = ctx->channel_levels[1][ctx->voltage_ch2];
+       offset = (ch_levels[1] - ch_levels[0]) * ctx->voffset_ch2 + ch_levels[0];
+       offsets[2] = (offset >> 8) | 0x20;
+       offsets[3] = offset & 0xff;
+       sr_dbg("hantek-dso: CH2 offset %3.2f (%.2x%.2x)", ctx->voffset_ch2,
+                       offsets[2], offsets[3]);
+
+       /* Trigger */
+       offset = MAX_VERT_TRIGGER * ctx->voffset_trigger;
+       offsets[4] = (offset >> 8) | 0x20;
+       offsets[5] = offset & 0xff;
+       sr_dbg("hantek-dso: trigger offset %3.2f (%.2x%.2x)", ctx->voffset_trigger,
+                       offsets[4], offsets[5]);
 
        if ((ret = libusb_control_transfer(ctx->usb->devhdl,
                        LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET,
@@ -437,6 +556,7 @@ uint8_t offsets[] = {0x20,0x75,0x30,0x3f,0x20,0xbd,0x3f,0x02,0x20,0x00,0x71,0x01
                sr_err("failed to set offsets: %d", ret);
                return SR_ERR;
        }
+       sr_dbg("hantek-dso: sent CTRL_SETOFFSET");
 
        return SR_OK;
 }
@@ -517,15 +637,14 @@ SR_PRIV int dso_init(struct context *ctx)
        if (dso_enable_trigger(ctx) != SR_OK)
                return SR_ERR;
 
-       if (dso_force_trigger(ctx) != SR_OK)
-               return SR_ERR;
-
        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");
@@ -535,20 +654,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];
+
+       /* 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 inbuf[0];
+       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];