]> sigrok.org Git - libsigrok.git/blobdiff - hardware/rigol-ds/protocol.c
rigol-ds: Enable/disable LA pod when (de)selecting digital channels.
[libsigrok.git] / hardware / rigol-ds / protocol.c
index b11fb2c75d82bb8833fa97e1104f67fbe0d99cb8..55d437b468806c509487f73b7d620417f5fa8ab6 100644 (file)
@@ -211,13 +211,16 @@ static int rigol_ds_stop_wait(const struct sr_dev_inst *sdi)
 static int rigol_ds_check_stop(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
+       struct sr_probe *probe;
        int tmp;
 
        if (!(devc = sdi->priv))
                return SR_ERR;
 
+       probe = devc->channel_entry->data;
+
        if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d",
-                         devc->channel->index + 1) != SR_OK)
+                         probe->index + 1) != SR_OK)
                return SR_ERR;
        /* Check that the number of samples will be accepted */
        if (sr_scpi_send(sdi->conn, ":WAV:POIN %d;*OPC", devc->analog_frame_size) != SR_OK)
@@ -320,25 +323,27 @@ SR_PRIV int rigol_ds_capture_start(const struct sr_dev_inst *sdi)
 SR_PRIV int rigol_ds_channel_start(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
+       struct sr_probe *probe;
 
        if (!(devc = sdi->priv))
                return SR_ERR;
 
-       sr_dbg("Starting reading data from channel %d",
-              devc->channel->index + 1);
+       probe = devc->channel_entry->data;
+
+       sr_dbg("Starting reading data from channel %d", probe->index + 1);
 
        if (devc->model->protocol == PROTOCOL_LEGACY) {
-               if (devc->channel->type == SR_DF_LOGIC) {
+               if (probe->type == SR_PROBE_LOGIC) {
                        if (sr_scpi_send(sdi->conn, ":WAV:DATA? DIG") != SR_OK)
                                return SR_ERR;
                } else {
-                       if (sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%c",
-                                       devc->channel->name[2]) != SR_OK)
+                       if (sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%d",
+                                       probe->index + 1) != SR_OK)
                                return SR_ERR;
                }
        } else {
                if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d",
-                                 devc->channel->index + 1) != SR_OK)
+                                 probe->index + 1) != SR_OK)
                        return SR_ERR;
                if (devc->data_source != DATA_SOURCE_LIVE) {
                        if (sr_scpi_send(sdi->conn, ":WAV:RES") != SR_OK)
@@ -363,7 +368,7 @@ static int rigol_ds_read_header(struct sr_scpi_dev_inst *scpi)
        int len, tmp;
 
        /* Read the hashsign and length digit. */
-       tmp = sr_scpi_read(scpi, start, 2);
+       tmp = sr_scpi_read_data(scpi, start, 2);
        start[2] = '\0';
        if (tmp != 2)
        {
@@ -378,7 +383,7 @@ static int rigol_ds_read_header(struct sr_scpi_dev_inst *scpi)
        len = atoi(start + 1);
 
        /* Read the data length. */
-       tmp = sr_scpi_read(scpi, length, len);
+       tmp = sr_scpi_read_data(scpi, length, len);
        length[len] = '\0';
        if (tmp != len)
        {
@@ -418,7 +423,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
 
        scpi = sdi->conn;
 
-       if (revents == G_IO_IN) {
+       if (revents == G_IO_IN || revents == 0) {
                if (devc->model->protocol == PROTOCOL_IEEE488_2) {
                        switch(devc->wait_event) {
                        case WAIT_NONE:
@@ -450,13 +455,19 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                        }
                }
 
-               probe = devc->channel;
+               probe = devc->channel_entry->data;
                
+               if (devc->num_block_bytes == 0 &&
+                   devc->model->protocol == PROTOCOL_IEEE488_2) {
+                               if (sr_scpi_send(sdi->conn, ":WAV:DATA?") != SR_OK)
+                                       return TRUE;
+               }
+
                if (devc->num_block_bytes == 0) {
+                       if (sr_scpi_read_begin(scpi) != SR_OK)
+                               return TRUE;
                        if (devc->model->protocol == PROTOCOL_IEEE488_2) {
                                sr_dbg("New block header expected");
-                               if (sr_scpi_send(sdi->conn, ":WAV:DATA?") != SR_OK)
-                                       return TRUE;
                                len = rigol_ds_read_header(scpi);
                                if (len == -1)
                                        return TRUE;
@@ -469,7 +480,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                                if (devc->data_source == DATA_SOURCE_LIVE
                                                && (unsigned)len < devc->num_frame_samples) {
                                        sr_dbg("Discarding short data block");
-                                       sr_scpi_read(scpi, (char *)devc->buffer, len + 1);
+                                       sr_scpi_read_data(scpi, (char *)devc->buffer, len + 1);
                                        return TRUE;
                                }
                                devc->num_block_bytes = len;
@@ -484,7 +495,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                }
 
                len = devc->num_block_bytes - devc->num_block_read;
-               len = sr_scpi_read(scpi, (char *)devc->buffer,
+               len = sr_scpi_read_data(scpi, (char *)devc->buffer,
                                len < ACQ_BUFFER_SIZE ? len : ACQ_BUFFER_SIZE);
 
                sr_dbg("Received %d bytes.", len);
@@ -533,7 +544,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                        if (devc->model->protocol == PROTOCOL_IEEE488_2) {
                                /* Discard the terminating linefeed and prepare for
                                   possible next block */
-                               sr_scpi_read(scpi, (char *)devc->buffer, 1);
+                               sr_scpi_read_data(scpi, (char *)devc->buffer, 1);
                                devc->num_block_bytes = 0;
                                if (devc->data_source != DATA_SOURCE_LIVE)
                                        rigol_ds_set_wait_event(devc, WAIT_BLOCK);
@@ -565,31 +576,27 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                                sr_scpi_send(sdi->conn, ":WAV:END");
                }
 
-               if (devc->enabled_analog_probes
-                               && devc->channel == 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 = devc->enabled_analog_probes->next->data;
+               if (probe->type == SR_PROBE_ANALOG
+                               && devc->channel_entry->next != NULL) {
+                       /* We got the frame for this analog channel, but
+                        * there's another analog channel. */
+                       devc->channel_entry = devc->channel_entry->next;
                        rigol_ds_channel_start(sdi);
                } else {
-                       /* Done with both analog channels in this frame. */
+                       /* Done with all analog channels in this frame. */
                        if (devc->enabled_digital_probes
-                                       && devc->channel != devc->enabled_digital_probes->data) {
+                                       && devc->channel_entry != devc->enabled_digital_probes) {
                                /* Now we need to get the digital data. */
-                               devc->channel = devc->enabled_digital_probes->data;
+                               devc->channel_entry = devc->enabled_digital_probes;
                                rigol_ds_channel_start(sdi);
                        } else if (++devc->num_frames == devc->limit_frames) {
-                               /* End of last frame. */
-                               packet.type = SR_DF_END;
-                               sr_session_send(sdi, &packet);
                                sdi->driver->dev_acquisition_stop(sdi, cb_data);
                        } else {
                                /* Get the next frame, starting with the first analog channel. */
                                if (devc->enabled_analog_probes)
-                                       devc->channel = devc->enabled_analog_probes->data;
+                                       devc->channel_entry = devc->enabled_analog_probes;
                                else
-                                       devc->channel = devc->enabled_digital_probes->data;
+                                       devc->channel_entry = devc->enabled_digital_probes;
 
                                if (devc->model->protocol == PROTOCOL_LEGACY)
                                        rigol_ds_channel_start(sdi);
@@ -609,10 +616,7 @@ static int get_cfg(const struct sr_dev_inst *sdi, char *cmd, char *reply, size_t
        struct sr_scpi_dev_inst *scpi = sdi->conn;
        char *response;
 
-       if (sr_scpi_send(scpi, cmd) != SR_OK)
-               return SR_ERR;
-
-       if (sr_scpi_receive(scpi, &response) != SR_OK)
+       if (sr_scpi_get_string(scpi, cmd, &response) != SR_OK)
                return SR_ERR;
 
        g_strlcpy(reply, response, maxlen);
@@ -673,26 +677,31 @@ SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
        char *t_s, *cmd;
-       int i, res;
+       unsigned int i;
+       int res;
 
        devc = sdi->priv;
 
        /* Analog channel state. */
-       if (get_cfg_string(sdi, ":CHAN1:DISP?", &t_s) != SR_OK)
-               return SR_ERR;
-       devc->analog_channels[0] = !strcmp(t_s, "ON") || !strcmp(t_s, "1");
-       g_free(t_s);
-       if (get_cfg_string(sdi, ":CHAN2:DISP?", &t_s) != SR_OK)
-               return SR_ERR;
-       devc->analog_channels[1] = !strcmp(t_s, "ON") || !strcmp(t_s, "1");
-       g_free(t_s);
-       sr_dbg("Current analog channel state CH1 %s CH2 %s",
-                       devc->analog_channels[0] ? "on" : "off",
-                       devc->analog_channels[1] ? "on" : "off");
+       for (i = 0; i < devc->model->analog_channels; i++) {
+               cmd = g_strdup_printf(":CHAN%d:DISP?", i + 1);
+               res = get_cfg_string(sdi, cmd, &t_s);
+               g_free(cmd);
+               if (res != SR_OK)
+                       return SR_ERR;
+               devc->analog_channels[i] = !strcmp(t_s, "ON") || !strcmp(t_s, "1");
+       }
+       sr_dbg("Current analog channel state:");
+       for (i = 0; i < devc->model->analog_channels; i++)
+               sr_dbg("CH%d %s", i + 1, devc->analog_channels[i] ? "on" : "off");
 
        /* Digital channel state. */
        if (devc->model->has_digital) {
-               sr_dbg("Current digital channel state:");
+               if (get_cfg_string(sdi, ":LA:DISP?", &t_s) != SR_OK)
+                       return SR_ERR;
+               devc->la_enabled = !strcmp(t_s, "ON") ? TRUE : FALSE;
+               sr_dbg("Logic analyzer %s, current digital channel state:",
+                               devc->la_enabled ? "enabled" : "disabled");
                for (i = 0; i < 16; i++) {
                        cmd = g_strdup_printf(":DIG%d:TURN?", i);
                        res = get_cfg_string(sdi, cmd, &t_s);
@@ -711,41 +720,52 @@ SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi)
        sr_dbg("Current timebase %g", devc->timebase);
 
        /* Vertical gain. */
-       if (get_cfg_float(sdi, ":CHAN1:SCAL?", &devc->vdiv[0]) != SR_OK)
-               return SR_ERR;
-       if (get_cfg_float(sdi, ":CHAN2:SCAL?", &devc->vdiv[1]) != SR_OK)
-               return SR_ERR;
-       sr_dbg("Current vertical gain CH1 %g CH2 %g", devc->vdiv[0], devc->vdiv[1]);
+       for (i = 0; i < devc->model->analog_channels; i++) {
+               cmd = g_strdup_printf(":CHAN%d:SCAL?", i + 1);
+               res = get_cfg_float(sdi, cmd, &devc->vdiv[i]);
+               g_free(cmd);
+               if (res != SR_OK)
+                       return SR_ERR;
+       }
+       sr_dbg("Current vertical gain:");
+       for (i = 0; i < devc->model->analog_channels; i++)
+               sr_dbg("CH%d %g", i + 1, devc->vdiv[i]);
 
+       sr_dbg("Current vertical reference:");
        if (devc->model->protocol == PROTOCOL_IEEE488_2) {
                /* Vertical reference - not certain if this is the place to read it. */
-               if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN1") != SR_OK)
-                       return SR_ERR;
-               if (get_cfg_int(sdi, ":WAV:YREF?", &devc->vert_reference[0]) != SR_OK)
-                       return SR_ERR;
-               if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN2") != SR_OK)
-                       return SR_ERR;
-               if (get_cfg_int(sdi, ":WAV:YREF?", &devc->vert_reference[1]) != SR_OK)
-                       return SR_ERR;
-               sr_dbg("Current vertical reference CH1 %d CH2 %d",
-                               devc->vert_reference[0], devc->vert_reference[1]);
+               for (i = 0; i < devc->model->analog_channels; i++) {
+                       if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d", i + 1) != SR_OK)
+                               return SR_ERR;
+                       if (get_cfg_int(sdi, ":WAV:YREF?", &devc->vert_reference[i]) != SR_OK)
+                               return SR_ERR;
+                       sr_dbg("CH%d %d", i + 1, devc->vert_reference[i]);
+               }
        }
 
        /* Vertical offset. */
-       if (get_cfg_float(sdi, ":CHAN1:OFFS?", &devc->vert_offset[0]) != SR_OK)
-               return SR_ERR;
-       if (get_cfg_float(sdi, ":CHAN2:OFFS?", &devc->vert_offset[1]) != SR_OK)
-               return SR_ERR;
-       sr_dbg("Current vertical offset CH1 %g CH2 %g", devc->vert_offset[0],
-                       devc->vert_offset[1]);
+       for (i = 0; i < devc->model->analog_channels; i++) {
+               cmd = g_strdup_printf(":CHAN%d:OFFS?", i + 1);
+               res = get_cfg_float(sdi, cmd, &devc->vert_offset[i]);
+               g_free(cmd);
+               if (res != SR_OK)
+                       return SR_ERR;
+       }
+       sr_dbg("Current vertical offset:");
+       for (i = 0; i < devc->model->analog_channels; i++)
+               sr_dbg("CH%d %g", i + 1, devc->vert_offset[i]);
 
        /* Coupling. */
-       if (get_cfg_string(sdi, ":CHAN1:COUP?", &devc->coupling[0]) != SR_OK)
-               return SR_ERR;
-       if (get_cfg_string(sdi, ":CHAN2:COUP?", &devc->coupling[1]) != SR_OK)
-               return SR_ERR;
-       sr_dbg("Current coupling CH1 %s CH2 %s", devc->coupling[0],
-                       devc->coupling[1]);
+       for (i = 0; i < devc->model->analog_channels; i++) {
+               cmd = g_strdup_printf(":CHAN%d:COUP?", i + 1);
+               res = get_cfg_string(sdi, cmd, &devc->coupling[i]);
+               g_free(cmd);
+               if (res != SR_OK)
+                       return SR_ERR;
+       }
+       sr_dbg("Current coupling:");
+       for (i = 0; i < devc->model->analog_channels; i++)
+               sr_dbg("CH%d %s", i + 1, devc->coupling[i]);
 
        /* Trigger source. */
        if (get_cfg_string(sdi, ":TRIG:EDGE:SOUR?", &devc->trigger_source) != SR_OK)