]> sigrok.org Git - libsigrok.git/blobdiff - hardware/rigol-ds1xx2/protocol.c
rigol-ds1xx2: support digital channels.
[libsigrok.git] / hardware / rigol-ds1xx2 / protocol.c
index 95ff3263dedde5ba48e96c04469f0ce41f66136e..d6dc70ad9a44b21b29186c05e3d15bf8038b5af4 100644 (file)
@@ -35,10 +35,12 @@ SR_PRIV int rigol_ds1xx2_receive(int fd, int revents, void *cb_data)
        struct dev_context *devc;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_analog analog;
-       unsigned char buf[WAVEFORM_SIZE];
+       struct sr_datafeed_logic logic;
+       unsigned char buf[DIGITAL_WAVEFORM_SIZE];
        double vdiv, offset;
-       float data[WAVEFORM_SIZE];
-       int probenum, len, i;
+       float data[ANALOG_WAVEFORM_SIZE];
+       int len, i, waveform_size;
+       struct sr_probe *probe;
 
        if (!(sdi = cb_data))
                return TRUE;
@@ -47,7 +49,10 @@ SR_PRIV int rigol_ds1xx2_receive(int fd, int revents, void *cb_data)
                return TRUE;
 
        if (revents == G_IO_IN) {
-               len = read(fd, buf, WAVEFORM_SIZE - devc->num_frame_bytes);
+               probe = devc->channel_frame;
+               waveform_size = probe->type == SR_PROBE_ANALOG ?
+                               ANALOG_WAVEFORM_SIZE : DIGITAL_WAVEFORM_SIZE;
+               len = read(fd, buf, waveform_size - devc->num_frame_bytes);
                sr_dbg("Received %d bytes.", len);
                if (len == -1)
                        return TRUE;
@@ -58,28 +63,37 @@ SR_PRIV int rigol_ds1xx2_receive(int fd, int revents, void *cb_data)
                        sr_session_send(sdi, &packet);
                }
 
-               probenum = devc->channel_frame->name[2] == '1' ? 0 : 1;
-               for (i = 0; i < len; i++) {
-                       vdiv = devc->vdiv[probenum];
-                       offset = devc->vert_offset[probenum];
-                       data[i] = vdiv / 25.6 * (128 - buf[i]) - offset;
-               }
-               analog.probes = g_slist_append(NULL, devc->channel_frame);
-               analog.num_samples = len;
-               analog.data = data;
-               analog.mq = SR_MQ_VOLTAGE;
-               analog.unit = SR_UNIT_VOLT;
-               analog.mqflags = 0;
-               packet.type = SR_DF_ANALOG;
-               packet.payload = &analog;
-               sr_session_send(cb_data, &packet);
-               g_slist_free(analog.probes);
-
-               devc->num_frame_bytes += len;
-
-               if (devc->num_frame_bytes != WAVEFORM_SIZE) {
-                       /* Don't have the whole frame yet. */
-                       return TRUE;
+               if (probe->type == SR_PROBE_ANALOG) {
+                       for (i = 0; i < len; i++) {
+                               vdiv = devc->vdiv[probe->index];
+                               offset = devc->vert_offset[probe->index];
+                               data[i] = vdiv / 25.6 * (128 - buf[i]) - offset;
+                       }
+                       analog.probes = g_slist_append(NULL, probe);
+                       analog.num_samples = len;
+                       analog.data = data;
+                       analog.mq = SR_MQ_VOLTAGE;
+                       analog.unit = SR_UNIT_VOLT;
+                       analog.mqflags = 0;
+                       packet.type = SR_DF_ANALOG;
+                       packet.payload = &analog;
+                       sr_session_send(cb_data, &packet);
+                       g_slist_free(analog.probes);
+
+                       if (len != ANALOG_WAVEFORM_SIZE)
+                               /* Don't have the whole frame yet. */
+                               return TRUE;
+               } else {
+                       logic.length = len - 10;
+                       logic.unitsize = 2;
+                       logic.data = buf + 10;
+                       packet.type = SR_DF_LOGIC;
+                       packet.payload = &logic;
+                       sr_session_send(cb_data, &packet);
+
+                       if (len != DIGITAL_WAVEFORM_SIZE)
+                               /* Don't have the whole frame yet. */
+                               return TRUE;
                }
 
                /* End of the frame. */
@@ -87,22 +101,34 @@ SR_PRIV int rigol_ds1xx2_receive(int fd, int revents, void *cb_data)
                sr_session_send(sdi, &packet);
                devc->num_frame_bytes = 0;
 
-               if (devc->channel_frame == devc->enabled_probes->data
-                               && devc->enabled_probes->next != NULL) {
-                       /* We got the frame for the first channel, but
-                        * there's a second channel. */
-                       devc->channel_frame = devc->enabled_probes->next->data;
+               if (devc->enabled_analog_probes
+                               && devc->channel_frame == devc->enabled_analog_probes->data
+                               && devc->enabled_analog_probes->next != NULL) {
+                       /* We got the frame for the first analog channel, but
+                        * there's a second analog channel. */
+                       devc->channel_frame = devc->enabled_analog_probes->next->data;
                        rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%c",
                                        devc->channel_frame->name[2]);
                } else {
-                       /* Done with both channels in this frame. */
-                       if (++devc->num_frames == devc->limit_frames) {
+                       /* Done with both analog channels in this frame. */
+                       if (devc->enabled_digital_probes
+                                       && devc->channel_frame != devc->enabled_digital_probes->data) {
+                               /* Now we need to get the digital data. */
+                               devc->channel_frame = devc->enabled_digital_probes->data;
+                               rigol_ds1xx2_send(devc, ":WAV:DATA? DIG");
+                       } else if (++devc->num_frames == devc->limit_frames) {
+                               /* End of last frame. */
                                sdi->driver->dev_acquisition_stop(sdi, cb_data);
                        } else {
-                               /* Get the next frame, starting with the first channel. */
-                               devc->channel_frame = devc->enabled_probes->data;
-                               rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%c",
-                                               devc->channel_frame->name[2]);
+                               /* Get the next frame, starting with the first analog channel. */
+                               if (devc->enabled_analog_probes) {
+                                       devc->channel_frame = devc->enabled_analog_probes->data;
+                                       rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%c",
+                                                       devc->channel_frame->name[2]);
+                               } else {
+                                       devc->channel_frame = devc->enabled_digital_probes->data;
+                                       rigol_ds1xx2_send(devc, ":WAV:DATA? DIG");
+                               }
                        }
                }
        }
@@ -182,22 +208,38 @@ static int get_cfg_string(const struct sr_dev_inst *sdi, char *cmd, char **buf)
 SR_PRIV int rigol_ds1xx2_get_dev_cfg(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
-       char *t_s;
+       char *t_s, *cmd;
+       int i, res;
 
        devc = sdi->priv;
 
-       /* Channel state. */
+       /* Analog channel state. */
        if (get_cfg_string(sdi, ":CHAN1:DISP?", &t_s) != SR_OK)
                return SR_ERR;
-       devc->channels[0] = !strcmp(t_s, "ON") ? TRUE : FALSE;
+       devc->analog_channels[0] = !strcmp(t_s, "ON") ? TRUE : FALSE;
        g_free(t_s);
        if (get_cfg_string(sdi, ":CHAN2:DISP?", &t_s) != SR_OK)
                return SR_ERR;
-       devc->channels[1] = !strcmp(t_s, "ON") ? TRUE : FALSE;
+       devc->analog_channels[1] = !strcmp(t_s, "ON") ? TRUE : FALSE;
        g_free(t_s);
-       sr_dbg("Current channel state CH1 %s CH2 %s",
-                       devc->channels[0] ? "on" : "off",
-                       devc->channels[1] ? "on" : "off");
+       sr_dbg("Current analog channel state CH1 %s CH2 %s",
+                       devc->analog_channels[0] ? "on" : "off",
+                       devc->analog_channels[1] ? "on" : "off");
+
+       /* Digital channel state. */
+       if (devc->has_digital) {
+               sr_dbg("Current digital channel state:");
+               for (i = 0; i < 16; i++) {
+                       cmd = g_strdup_printf(":DIG%d:TURN?", i);
+                       res = get_cfg_string(sdi, cmd, &t_s);
+                       g_free(cmd);
+                       if (res != SR_OK)
+                               return SR_ERR;
+                       devc->digital_channels[i] = !strcmp(t_s, "ON") ? TRUE : FALSE;
+                       g_free(t_s);
+                       sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "on" : "off");
+               }
+       }
 
        /* Timebase. */
        if (get_cfg_float(sdi, ":TIM:SCAL?", &devc->timebase) != SR_OK)