]> sigrok.org Git - libsigrok.git/blobdiff - hardware/rigol-ds/protocol.c
rigol-ds: Use common rigol_ds_config_set() function throughout.
[libsigrok.git] / hardware / rigol-ds / protocol.c
index 4a3f80eb6b2d1adc594c72287f4e8d034af1a7ca..1d0e35a93b1315efac8ef83a624d0e418fef3c9a 100644 (file)
@@ -215,11 +215,11 @@ static int rigol_ds_check_stop(const struct sr_dev_inst *sdi)
 
        probe = devc->channel_entry->data;
 
-       if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d",
+       if (rigol_ds_config_set(sdi, ":WAV:SOUR CHAN%d",
                          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)
+       if (rigol_ds_config_set(sdi, ":WAV:POIN %d", devc->analog_frame_size) != SR_OK)
                return SR_ERR;
        if (sr_scpi_get_int(sdi->conn, "*ESR?", &tmp) != SR_OK)
                return SR_ERR;
@@ -239,7 +239,7 @@ static int rigol_ds_check_stop(const struct sr_dev_inst *sdi)
                sr_warn("Single shot acquisition failed, retrying...");
                /* Sleep a bit, otherwise the single shot will often fail */
                g_usleep(500000);
-               sr_scpi_send(sdi->conn, ":SING");
+               rigol_ds_config_set(sdi, ":SING");
                rigol_ds_set_wait_event(devc, WAIT_STOP);
                return SR_ERR;
        }
@@ -287,6 +287,30 @@ static int rigol_ds_block_wait(const struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
+/* Send a configuration setting. */
+SR_PRIV int rigol_ds_config_set(const struct sr_dev_inst *sdi, const char *format, ...)
+{
+       struct dev_context *devc = sdi->priv;
+       va_list args;
+       int ret;
+
+       va_start(args, format);
+       ret = sr_scpi_send_variadic(sdi->conn, format, args);
+       va_end(args);
+
+       if (ret != SR_OK)
+               return SR_ERR;
+
+       if (devc->model->series == RIGOL_DS1000) {
+               /* The DS1000 series needs this stupid delay, *OPC? doesn't work. */
+               sr_spew("delay %dms", 100);
+               g_usleep(100000);
+               return SR_OK;
+       } else {
+               return sr_scpi_get_opc(sdi->conn);
+       }
+}
+
 /* Start capturing a new frameset */
 SR_PRIV int rigol_ds_capture_start(const struct sr_dev_inst *sdi)
 {
@@ -298,16 +322,16 @@ SR_PRIV int rigol_ds_capture_start(const struct sr_dev_inst *sdi)
        sr_dbg("Starting data capture for frameset %lu of %lu",
               devc->num_frames + 1, devc->limit_frames);
 
-       if (sr_scpi_send(sdi->conn, ":WAV:FORM BYTE") != SR_OK)
+       if (rigol_ds_config_set(sdi, ":WAV:FORM BYTE") != SR_OK)
                return SR_ERR;
        if (devc->data_source == DATA_SOURCE_LIVE) {
-               if (sr_scpi_send(sdi->conn, ":WAV:MODE NORM") != SR_OK)
+               if (rigol_ds_config_set(sdi, ":WAV:MODE NORM") != SR_OK)
                        return SR_ERR;
                rigol_ds_set_wait_event(devc, WAIT_TRIGGER);
        } else {
-               if (sr_scpi_send(sdi->conn, ":WAV:MODE RAW") != SR_OK)
+               if (rigol_ds_config_set(sdi, ":WAV:MODE RAW") != SR_OK)
                        return SR_ERR;
-               if (sr_scpi_send(sdi->conn, ":SING", devc->analog_frame_size) != SR_OK)
+               if (rigol_ds_config_set(sdi, ":SING", devc->analog_frame_size) != SR_OK)
                        return SR_ERR;          
                rigol_ds_set_wait_event(devc, WAIT_STOP);
        }
@@ -338,20 +362,20 @@ SR_PRIV int rigol_ds_channel_start(const struct sr_dev_inst *sdi)
                                return SR_ERR;
                }
        } else {
-               if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d",
+               if (rigol_ds_config_set(sdi, ":WAV:SOUR CHAN%d",
                                  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)
+                       if (rigol_ds_config_set(sdi, ":WAV:RES") != SR_OK)
                                return SR_ERR;
-                       if (sr_scpi_send(sdi->conn, ":WAV:BEG") != SR_OK)
+                       if (rigol_ds_config_set(sdi, ":WAV:BEG") != SR_OK)
                                return SR_ERR;
                        rigol_ds_set_wait_event(devc, WAIT_BLOCK);
                } else
                        rigol_ds_set_wait_event(devc, WAIT_NONE);
        }
 
-       devc->num_frame_samples = 0;
+       devc->num_channel_bytes = 0;
        devc->num_block_bytes = 0;
 
        return SR_OK;
@@ -448,7 +472,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
 
                expected_data_bytes = probe->type == SR_PROBE_ANALOG ?
                                devc->analog_frame_size : devc->digital_frame_size;
-               
+
                if (devc->num_block_bytes == 0 &&
                    devc->model->series >= RIGOL_DS1000Z) {
                                if (sr_scpi_send(sdi->conn, ":WAV:DATA?") != SR_OK)
@@ -460,7 +484,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                        if (sr_scpi_read_begin(scpi) != SR_OK)
                                return TRUE;
 
-                       if (devc->model->protocol == PROTOCOL_IEEE488_2) {
+                       if (devc->protocol == PROTOCOL_IEEE488_2) {
                                sr_dbg("New block header expected");
                                len = rigol_ds_read_header(scpi);
                                if (len == -1)
@@ -494,12 +518,6 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
 
                devc->num_block_read += len;
 
-               if (devc->num_frame_samples == 0) {
-                       /* Start of a new frame. */
-                       packet.type = SR_DF_FRAME_BEGIN;
-                       sr_session_send(sdi, &packet);
-               }
-
                if (probe->type == SR_PROBE_ANALOG) {
                        vref = devc->vert_reference[probe->index];
                        vdiv = devc->vdiv[probe->index] / 25.6;
@@ -535,7 +553,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                                /* Discard the terminating linefeed */
                                sr_scpi_read_data(scpi, (char *)devc->buffer, 1);
                        }
-                       if (devc->model->protocol == PROTOCOL_IEEE488_2) {
+                       if (devc->protocol == PROTOCOL_IEEE488_2) {
                                /* Prepare for possible next block */
                                devc->num_block_bytes = 0;
                                if (devc->data_source != DATA_SOURCE_LIVE)
@@ -551,16 +569,13 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                        sr_dbg("%d of %d block bytes read", devc->num_block_read, devc->num_block_bytes);
                }
 
-               devc->num_frame_samples += len;
+               devc->num_channel_bytes += len;
 
-               if (devc->num_frame_samples < expected_data_bytes)
-                       /* Don't have the whole frame yet. */
+               if (devc->num_channel_bytes < expected_data_bytes)
+                       /* Don't have the full data for this channel yet, re-run. */
                        return TRUE;
 
-               /* End of the frame. */
-               sr_dbg("Frame completed, %d samples", devc->num_frame_samples);
-               packet.type = SR_DF_FRAME_END;
-               sr_session_send(sdi, &packet);
+               /* End of data for this channel. */
                if (devc->model->series >= RIGOL_DS1000Z) {
                        /* Signal end of data download to scope */
                        if (devc->data_source != DATA_SOURCE_LIVE)
@@ -569,7 +584,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                                 * to the next channel causes an error. Fun with
                                 * firmware...
                                 */
-                               sr_scpi_send(sdi->conn, ":WAV:END");
+                               rigol_ds_config_set(sdi, ":WAV:END");
                }
 
                if (probe->type == SR_PROBE_ANALOG
@@ -585,19 +600,30 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                                /* Now we need to get the digital data. */
                                devc->channel_entry = devc->enabled_digital_probes;
                                rigol_ds_channel_start(sdi);
-                       } else if (++devc->num_frames == devc->limit_frames) {
-                               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_entry = devc->enabled_analog_probes;
-                               else
-                                       devc->channel_entry = devc->enabled_digital_probes;
-
-                               if (devc->model->series < RIGOL_DS1000Z)
-                                       rigol_ds_channel_start(sdi);
-                               else
-                                       rigol_ds_capture_start(sdi);
+                               /* Done with this frame. */
+                               packet.type = SR_DF_FRAME_END;
+                               sr_session_send(cb_data, &packet);
+
+                               if (++devc->num_frames == devc->limit_frames) {
+                                       /* Last frame, stop capture. */
+                                       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_entry = devc->enabled_analog_probes;
+                                       else
+                                               devc->channel_entry = devc->enabled_digital_probes;
+
+                                       if (devc->model->series < RIGOL_DS1000Z)
+                                               rigol_ds_channel_start(sdi);
+                                       else
+                                               rigol_ds_capture_start(sdi);
+
+                                       /* Start of next frame. */
+                                       packet.type = SR_DF_FRAME_BEGIN;
+                                       sr_session_send(cb_data, &packet);
+                               }
                        }
                }
        }
@@ -667,7 +693,7 @@ SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi)
        if (devc->model->series >= RIGOL_DS1000Z) {
                /* Vertical reference - not certain if this is the place to read it. */
                for (i = 0; i < devc->model->analog_channels; i++) {
-                       if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d", i + 1) != SR_OK)
+                       if (rigol_ds_config_set(sdi, ":WAV:SOUR CHAN%d", i + 1) != SR_OK)
                                return SR_ERR;
                        if (sr_scpi_get_int(sdi->conn, ":WAV:YREF?", &devc->vert_reference[i]) != SR_OK)
                                return SR_ERR;