]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/hantek-dso/protocol.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / hantek-dso / protocol.c
index 831e357f0857e93ff0453a95165585022192a5d8..34a3935b9fd4fa6e36f3ceab8197652f2629e625 100644 (file)
@@ -130,7 +130,9 @@ SR_PRIV int dso_open(struct sr_dev_inst *sdi)
                        /*
                         * Check device by its physical USB bus/port address.
                         */
-                       usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
+                       if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
+                               continue;
+
                        if (strcmp(sdi->connection_id, connection_id))
                                /* This is not the one. */
                                continue;
@@ -184,7 +186,6 @@ SR_PRIV void dso_close(struct sr_dev_inst *sdi)
        libusb_close(usb->devhdl);
        usb->devhdl = NULL;
        sdi->status = SR_ST_INACTIVE;
-
 }
 
 static int get_channel_offsets(const struct sr_dev_inst *sdi)
@@ -241,15 +242,32 @@ static int get_channel_offsets(const struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
+static void dso2250_set_triggerpos(int value, int long_buffer, uint8_t dest[], int offset)
+{
+       uint32_t min, max;
+       uint32_t tmp, diff;
+
+       min = long_buffer ? 0 : 0x7d7ff;
+       max = 0x7ffff;
+
+       diff = max - min;
+       tmp = min + diff * value / 100;
+       sr_dbg("2250 trigger pos: %3d%% * [0x%x,0x%x] == 0x%x", value, min, max, tmp);
+
+       dest[offset + 0] = tmp & 0xff;
+       dest[offset + 1] = (tmp >> 8) & 0xff;
+       dest[offset + 2] = (tmp >> 16) & 0x7;
+}
+
 /* See http://openhantek.sourceforge.net/doc/namespaceHantek.html#ac1cd181814cf3da74771c29800b39028 */
 static int dso2250_set_trigger_samplerate(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
        struct sr_usb_dev_inst *usb;
        int ret, tmp;
-       int base;
+       uint64_t base;
        uint8_t cmdstring[12];
-
+       int trig;
 
        devc = sdi->priv;
        usb = sdi->conn;
@@ -257,8 +275,10 @@ static int dso2250_set_trigger_samplerate(const struct sr_dev_inst *sdi)
        memset(cmdstring, 0, sizeof(cmdstring));
        /* Command */
        cmdstring[0] = CMD_2250_SET_TRIGGERSOURCE;
-       sr_dbg("Trigger source %s.", devc->triggersource);
-       if (!strcmp("CH2", devc->triggersource))
+       sr_dbg("Trigger source %s.", devc->triggersource ? : "<none>");
+       if (!devc->triggersource)
+               tmp = 0;
+       else if (!strcmp("CH2", devc->triggersource))
                tmp = 3;
        else if (!strcmp("CH1", devc->triggersource))
                tmp = 2;
@@ -270,7 +290,6 @@ static int dso2250_set_trigger_samplerate(const struct sr_dev_inst *sdi)
        }
        cmdstring[2] = tmp;
 
-
        sr_dbg("Trigger slope: %d.", devc->triggerslope);
        cmdstring[2] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
 
@@ -284,11 +303,10 @@ static int dso2250_set_trigger_samplerate(const struct sr_dev_inst *sdi)
                return SR_ERR;
        }
 
-
        /* Frame size */
        sr_dbg("Frame size: %d.", devc->framesize);
        cmdstring[0] = CMD_2250_SET_RECORD_LENGTH;
-       cmdstring[2] = devc->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02;
+       cmdstring[2] = (devc->framesize == FRAMESIZE_SMALL) ? 0x01 : 0x02;
 
        if (send_begin(sdi) != SR_OK)
                return SR_ERR;
@@ -300,44 +318,33 @@ static int dso2250_set_trigger_samplerate(const struct sr_dev_inst *sdi)
                return SR_ERR;
        }
 
-
        memset(cmdstring, 0, sizeof(cmdstring));
        cmdstring[0] = CMD_2250_SET_SAMPLERATE;
-       /* Timebase fast */
-       sr_dbg("Time base index: %d.", devc->timebase);
        base = 100e6;
-       if (devc->timebase < TIME_40us) {
-               if (devc->framesize != FRAMESIZE_SMALL) {
-                       sr_err("Timebase < 40us only supported with 10K buffer.");
-                       return SR_ERR_ARG;
-               }
-
-               /* Fast mode on */
-               base = 200e6;
-               cmdstring[2] |= 1;
+       if (devc->samplerate > base) {
+               /* Timebase fast */
+               sr_err("Sample rate > 100MHz not yet supported.");
+               return SR_ERR_ARG;
        }
 
-       /* Downsampling on */
-       cmdstring[2] |= 2;
-       /* Downsampler = 1comp((Base / Samplerate) - 2)
-        *  Base == 100Msa resp. 200MSa
-        *
-        * Example for 500kSa/s:
-        *  100e6 / 500e3 => 200
-        *  200 - 2 => 198
-        *  1comp(198) => ff39 */
-
-       tmp = base * timebase_to_time(devc->timebase);
-       tmp = 200;
-       if (tmp < 0)
-               return SR_ERR_ARG;
-       tmp -= 2;
-       if (tmp < 0)
-               return SR_ERR_ARG;
-       tmp = ~tmp;
-       sr_dbg("sample rate value: 0x%x.", tmp & 0xffff);
-       cmdstring[4] = (tmp >> 0) & 0xff;
-       cmdstring[5] = (tmp >> 8) & 0xff;
+       tmp = base / devc->samplerate;
+       /* Downsample only if really necessary */
+       if (tmp > 1) {
+               /* Downsampling on */
+               cmdstring[2] |= 2;
+               /* Downsampler = 1comp((Base / Samplerate) - 2)
+                *  Base == 100Msa resp. 200MSa
+                *
+                * Example for 500kSa/s:
+                *  100e6 / 500e3 => 200
+                *  200 - 2 => 198
+                *  1comp(198) => ff39 */
+               tmp -= 2;
+               tmp = ~tmp;
+               sr_dbg("Down sampler value: 0x%x.", tmp & 0xffff);
+               cmdstring[4] = (tmp >> 0) & 0xff;
+               cmdstring[5] = (tmp >> 8) & 0xff;
+       }
 
        if (send_begin(sdi) != SR_OK)
                return SR_ERR;
@@ -350,11 +357,10 @@ static int dso2250_set_trigger_samplerate(const struct sr_dev_inst *sdi)
        }
        sr_dbg("Sent CMD_2250_SET_SAMPLERATE.");
 
-
-       /* Enabled channels: 00=CH1 01=CH2 10=both */
+       /* Enabled channels: 00=CH1, 01=CH2, 10=both. */
        memset(cmdstring, 0, sizeof(cmdstring));
        cmdstring[0] = CMD_2250_SET_CHANNELS;
-       sr_dbg("Channels CH1=%d CH2=%d", devc->ch_enabled[0], devc->ch_enabled[1]);
+       sr_dbg("Channels: CH1=%d, CH2=%d.", devc->ch_enabled[0], devc->ch_enabled[1]);
        cmdstring[2] = (devc->ch_enabled[0] ? 0 : 1) + (devc->ch_enabled[1] ? 2 : 0);
 
        if (send_begin(sdi) != SR_OK)
@@ -368,27 +374,16 @@ static int dso2250_set_trigger_samplerate(const struct sr_dev_inst *sdi)
        }
        sr_dbg("Sent CMD_2250_SET_CHANNELS.");
 
-
-
-       /* Trigger slope: 0=positive 1=negative */
+       /* Trigger slope: 0=positive, 1=negative. */
        memset(cmdstring, 0, sizeof(cmdstring));
        cmdstring[0] = CMD_2250_SET_TRIGGERPOS_AND_BUFFER;
 
-       /* TODO for big buffer */
-       /* TODO */
-       sr_dbg("Trigger position: %3.2f.", devc->triggerposition);
-//     tmp = 0x77fff + 0x8000 * devc->triggerposition;
-//     cmdstring[6] = tmp & 0xff;
-//     cmdstring[7] = (tmp >> 8) & 0xff;
-//     cmdstring[10] = (tmp >> 16) & 0xff;
-
-       cmdstring[2]=0xff;
-       cmdstring[3]=0xff;
-       cmdstring[4]=0x07;
-
-       cmdstring[6]=0xff;
-       cmdstring[7]=0xd7;
-       cmdstring[8]=0x07;
+       sr_dbg("Capture ratio: %" PRIu64 ".", devc->capture_ratio);
+       trig = devc->capture_ratio;
+       dso2250_set_triggerpos(trig,
+                       devc->framesize != FRAMESIZE_SMALL, cmdstring, 2);
+       dso2250_set_triggerpos(100 - trig,
+                       devc->framesize != FRAMESIZE_SMALL, cmdstring, 6);
 
        if (send_begin(sdi) != SR_OK)
                return SR_ERR;
@@ -404,7 +399,7 @@ static int dso2250_set_trigger_samplerate(const struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int dso_set_trigger_samplerate(const struct sr_dev_inst *sdi)
+SR_PRIV int dso_set_trigger_samplerate(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
        struct sr_usb_dev_inst *usb;
@@ -428,8 +423,10 @@ static int dso_set_trigger_samplerate(const struct sr_dev_inst *sdi)
        cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE;
 
        /* Trigger source */
-       sr_dbg("Trigger source %s.", devc->triggersource);
-       if (!strcmp("CH2", devc->triggersource))
+       sr_dbg("Trigger source %s.", devc->triggersource ? : "<none>");
+       if (!devc->triggersource)
+               tmp = 2;
+       else if (!strcmp("CH2", devc->triggersource))
                tmp = 0;
        else if (!strcmp("CH1", devc->triggersource))
                tmp = 1;
@@ -504,8 +501,8 @@ static int dso_set_trigger_samplerate(const struct sr_dev_inst *sdi)
        cmdstring[5] = (tmp >> 8) & 0xff;
 
        /* Horizontal trigger position */
-       sr_dbg("Trigger position: %3.2f.", devc->triggerposition);
-       tmp = 0x77fff + 0x8000 * devc->triggerposition;
+       sr_dbg("Capture ratio: %" PRIu64 ".", devc->capture_ratio);
+       tmp = 0x77fff + 0x8000 * devc->capture_ratio / 100;
        cmdstring[6] = tmp & 0xff;
        cmdstring[7] = (tmp >> 8) & 0xff;
        cmdstring[10] = (tmp >> 16) & 0xff;
@@ -524,7 +521,6 @@ static int dso_set_trigger_samplerate(const struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-
 static int dso_set_filters(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
@@ -582,13 +578,14 @@ static int dso_set_voltage(const struct sr_dev_inst *sdi)
        cmdstring[0] = CMD_SET_VOLTAGE;
 
        if (devc->profile->fw_pid == 0x2250) {
+               cmdstring[1] = 0x00;
                cmdstring[2] = 0x08;
        } else {
                cmdstring[1] = 0x0f;
                cmdstring[2] = 0x30;
        }
 
-       /* CH1 volts/div is encoded in bits 0-1 */
+       /* CH1 volts/div is encoded in bits 0-1. */
        sr_dbg("CH1 vdiv index: %d.", devc->voltage[0]);
        switch (devc->voltage[0]) {
        case VDIV_1V:
@@ -608,7 +605,7 @@ static int dso_set_voltage(const struct sr_dev_inst *sdi)
                break;
        }
 
-       /* CH2 volts/div is encoded in bits 2-3 */
+       /* CH2 volts/div is encoded in bits 2-3. */
        sr_dbg("CH2 vdiv index: %d.", devc->voltage[1]);
        switch (devc->voltage[1]) {
        case VDIV_1V:
@@ -675,7 +672,7 @@ static int dso_set_relays(const struct sr_dev_inst *sdi)
        if (devc->coupling[1] != COUPLING_AC)
                relays[6] = ~relays[6];
 
-       if (!strcmp(devc->triggersource, "EXT"))
+       if (devc->triggersource && strcmp(devc->triggersource, "EXT") == 0)
                relays[7] = ~relays[7];
 
        if (sr_log_loglevel_get() >= SR_LOG_DBG) {
@@ -698,7 +695,7 @@ static int dso_set_relays(const struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int dso_set_voffsets(const struct sr_dev_inst *sdi)
+SR_PRIV int dso_set_voffsets(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
        struct sr_usb_dev_inst *usb;
@@ -800,7 +797,6 @@ SR_PRIV int dso_force_trigger(const struct sr_dev_inst *sdi)
 
 SR_PRIV int dso_init(const struct sr_dev_inst *sdi)
 {
-
        sr_dbg("Initializing DSO.");
 
        if (get_channel_offsets(sdi) != SR_OK)