]> sigrok.org Git - libsigrok.git/blobdiff - hardware/rigol-ds/protocol.c
rigol-ds: Support VS5000 series devices.
[libsigrok.git] / hardware / rigol-ds / protocol.c
index e8f611bcfc3ac3822ee7767b89536fc7a9819593..af369e9cf5881234d316c15faa1237b9e9999b3b 100644 (file)
@@ -216,11 +216,11 @@ static int rigol_ds_check_stop(const struct sr_dev_inst *sdi)
        if (!(devc = sdi->priv))
                return SR_ERR;
 
-       if (rigol_ds_send(sdi, ":WAV:SOUR CHAN%d",
+       if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d",
                          devc->channel_frame->index + 1) != SR_OK)
                return SR_ERR;
        /* Check that the number of samples will be accepted */
-       if (rigol_ds_send(sdi, ":WAV:POIN %d;*OPC", devc->analog_frame_size) != SR_OK)
+       if (sr_scpi_send(sdi->conn, ":WAV:POIN %d;*OPC", devc->analog_frame_size) != SR_OK)
                return SR_ERR;
        if (get_cfg_int(sdi, "*ESR?", &tmp) != SR_OK)
                return SR_ERR;
@@ -240,7 +240,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);
-               rigol_ds_send(sdi, ":SING");
+               sr_scpi_send(sdi->conn, ":SING");
                rigol_ds_set_wait_event(devc, WAIT_STOP);
                return SR_ERR;
        }
@@ -299,16 +299,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 (rigol_ds_send(sdi, ":WAV:FORM BYTE") != SR_OK)
+       if (sr_scpi_send(sdi->conn, ":WAV:FORM BYTE") != SR_OK)
                return SR_ERR;
        if (devc->data_source == DATA_SOURCE_LIVE) {
-               if (rigol_ds_send(sdi, ":WAV:MODE NORM") != SR_OK)
+               if (sr_scpi_send(sdi->conn, ":WAV:MODE NORM") != SR_OK)
                        return SR_ERR;
                rigol_ds_set_wait_event(devc, WAIT_TRIGGER);
        } else {
-               if (rigol_ds_send(sdi, ":WAV:MODE RAW") != SR_OK)
+               if (sr_scpi_send(sdi->conn, ":WAV:MODE RAW") != SR_OK)
                        return SR_ERR;
-               if (rigol_ds_send(sdi, ":SING", devc->analog_frame_size) != SR_OK)
+               if (sr_scpi_send(sdi->conn, ":SING", devc->analog_frame_size) != SR_OK)
                        return SR_ERR;          
                rigol_ds_set_wait_event(devc, WAIT_STOP);
        }
@@ -327,13 +327,13 @@ SR_PRIV int rigol_ds_channel_start(const struct sr_dev_inst *sdi)
        sr_dbg("Starting reading data from channel %d",
               devc->channel_frame->index + 1);
 
-       if (rigol_ds_send(sdi, ":WAV:SOUR CHAN%d",
+       if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d",
                          devc->channel_frame->index + 1) != SR_OK)
                return SR_ERR;
        if (devc->data_source != DATA_SOURCE_LIVE) {
-               if (rigol_ds_send(sdi, ":WAV:RES") != SR_OK)
+               if (sr_scpi_send(sdi->conn, ":WAV:RES") != SR_OK)
                        return SR_ERR;
-               if (rigol_ds_send(sdi, ":WAV:BEG") != SR_OK)
+               if (sr_scpi_send(sdi->conn, ":WAV:BEG") != SR_OK)
                        return SR_ERR;
                rigol_ds_set_wait_event(devc, WAIT_BLOCK);
        } else
@@ -346,13 +346,13 @@ SR_PRIV int rigol_ds_channel_start(const struct sr_dev_inst *sdi)
 }
 
 /* Read the header of a data block */
-static int rigol_ds_read_header(struct sr_usbtmc_dev_inst *usbtmc)
+static int rigol_ds_read_header(struct sr_scpi_dev_inst *scpi)
 {
        char start[3], length[10];
        int len, tmp;
 
        /* Read the hashsign and length digit. */
-       tmp = read(usbtmc->fd, start, 2);
+       tmp = sr_scpi_read(scpi, start, 2);
        start[2] = '\0';
        if (tmp != 2)
        {
@@ -367,7 +367,7 @@ static int rigol_ds_read_header(struct sr_usbtmc_dev_inst *usbtmc)
        len = atoi(start + 1);
 
        /* Read the data length. */
-       tmp = read(usbtmc->fd, length, len);
+       tmp = sr_scpi_read(scpi, length, len);
        length[len] = '\0';
        if (tmp != len)
        {
@@ -388,7 +388,7 @@ static int rigol_ds_read_header(struct sr_usbtmc_dev_inst *usbtmc)
 SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
 {
        struct sr_dev_inst *sdi;
-       struct sr_usbtmc_dev_inst *usbtmc;
+       struct sr_scpi_dev_inst *scpi;
        struct dev_context *devc;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_analog analog;
@@ -405,7 +405,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
        if (!(devc = sdi->priv))
                return TRUE;
 
-       usbtmc = sdi->conn;
+       scpi = sdi->conn;
 
        if (revents == G_IO_IN) {
                if (devc->model->protocol == PROTOCOL_IEEE488_2) {
@@ -440,9 +440,9 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                
                        if (devc->num_block_bytes == 0) {
                                sr_dbg("New block header expected");
-                               if (rigol_ds_send(sdi, ":WAV:DATA?") != SR_OK)
+                               if (sr_scpi_send(sdi->conn, ":WAV:DATA?") != SR_OK)
                                        return TRUE;
-                               len = rigol_ds_read_header(usbtmc);
+                               len = rigol_ds_read_header(scpi);
                                if (len == -1)
                                        return TRUE;
                                /* At slow timebases in live capture the DS2072
@@ -454,7 +454,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_bytes) {
                                        sr_dbg("Discarding short data block");
-                                       read(usbtmc->fd, devc->buffer, len + 1);
+                                       sr_scpi_read(scpi, (char *)devc->buffer, len + 1);
                                        return TRUE;
                                }
                                devc->num_block_bytes = len;
@@ -465,12 +465,16 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                probe = devc->channel_frame;
                if (devc->model->protocol == PROTOCOL_IEEE488_2) {
                        len = devc->num_block_bytes - devc->num_block_read;
-                       len = read(usbtmc->fd, devc->buffer,
+                       len = sr_scpi_read(scpi, (char *)devc->buffer,
                                        len < ACQ_BUFFER_SIZE ? len : ACQ_BUFFER_SIZE);
                } else {
                        waveform_size = probe->type == SR_PROBE_ANALOG ?
-                                       DS1000_ANALOG_LIVE_WAVEFORM_SIZE : DIGITAL_WAVEFORM_SIZE;
-                       len = read(usbtmc->fd, devc->buffer, waveform_size - devc->num_frame_bytes);
+                                       (devc->model->series == RIGOL_VS5000 ?
+                                               VS5000_ANALOG_LIVE_WAVEFORM_SIZE :
+                                               DS1000_ANALOG_LIVE_WAVEFORM_SIZE) :
+                                       DIGITAL_WAVEFORM_SIZE;
+                       len = sr_scpi_read(scpi, (char *)devc->buffer,
+                                       waveform_size - devc->num_frame_bytes);
                }
                sr_dbg("Received %d bytes.", len);
                if (len == -1)
@@ -510,25 +514,21 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                                        sr_dbg("Block has been completed");
                                        /* Discard the terminating linefeed and prepare for
                                           possible next block */
-                                       read(usbtmc->fd, devc->buffer, 1);
+                                       sr_scpi_read(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);
                                } else
                                        sr_dbg("%d of %d block bytes read", devc->num_block_read, devc->num_block_bytes);
+                       }
 
-                               devc->num_frame_bytes += len;
+                       devc->num_frame_bytes += len;
 
-                               if (devc->num_frame_bytes < devc->analog_frame_size)
-                                       /* Don't have the whole frame yet. */
-                                       return TRUE;
+                       if (devc->num_frame_bytes < devc->analog_frame_size)
+                               /* Don't have the whole frame yet. */
+                               return TRUE;
 
-                               sr_dbg("Frame completed, %d samples", devc->num_frame_bytes);
-                       } else {
-                               if (len != DS1000_ANALOG_LIVE_WAVEFORM_SIZE)
-                                       /* Don't have the whole frame yet. */
-                                       return TRUE;
-                       }
+                       sr_dbg("Frame completed, %d samples", devc->num_frame_bytes);
                } else {
                        logic.length = len - 10;
                        logic.unitsize = 2;
@@ -555,7 +555,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                                 * to the next channel causes an error. Fun with
                                 * firmware...
                                 */
-                               rigol_ds_send(sdi, ":WAV:END");
+                               sr_scpi_send(sdi->conn, ":WAV:END");
                }
 
                if (devc->enabled_analog_probes
@@ -567,7 +567,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                        if (devc->model->protocol == PROTOCOL_IEEE488_2) {
                                rigol_ds_channel_start(sdi);
                        } else {
-                               rigol_ds_send(sdi, ":WAV:DATA? CHAN%c",
+                               sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%c",
                                                devc->channel_frame->name[2]);
                        }
                } else {
@@ -576,7 +576,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                                        && 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_ds_send(sdi, ":WAV:DATA? DIG");
+                               sr_scpi_send(sdi->conn, ":WAV:DATA? DIG");
                        } else if (++devc->num_frames == devc->limit_frames) {
                                /* End of last frame. */
                                packet.type = SR_DF_END;
@@ -592,11 +592,11 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                                } else {
                                        if (devc->enabled_analog_probes) {
                                                devc->channel_frame = devc->enabled_analog_probes->data;
-                                               rigol_ds_send(sdi, ":WAV:DATA? CHAN%c",
+                                               sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%c",
                                                                devc->channel_frame->name[2]);
                                        } else {
                                                devc->channel_frame = devc->enabled_digital_probes->data;
-                                               rigol_ds_send(sdi, ":WAV:DATA? DIG");
+                                               sr_scpi_send(sdi->conn, ":WAV:DATA? DIG");
                                        }
                                }
                        }
@@ -606,43 +606,22 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
        return TRUE;
 }
 
-SR_PRIV int rigol_ds_send(const struct sr_dev_inst *sdi, const char *format, ...)
-{
-       va_list args;
-       char buf[256];
-       int len, out, ret;
-       struct sr_usbtmc_dev_inst *usbtmc = sdi->conn;
-
-       va_start(args, format);
-       len = vsnprintf(buf, 255, format, args);
-       va_end(args);
-       strcat(buf, "\n");
-       len++;
-       out = write(usbtmc->fd, buf, len);
-       buf[len - 1] = '\0';
-       if (out != len) {
-               sr_dbg("Only sent %d/%d bytes of '%s'.", out, len, buf);
-               ret = SR_ERR;
-       } else {
-               sr_spew("Sent '%s'.", buf);
-               ret = SR_OK;
-       }
-
-       return ret;
-}
-
 static int get_cfg(const struct sr_dev_inst *sdi, char *cmd, char *reply, size_t maxlen)
 {
        int len;
        struct dev_context *devc = sdi->priv;
-       struct sr_usbtmc_dev_inst *usbtmc = sdi->conn;
+       struct sr_scpi_dev_inst *scpi = sdi->conn;
+       char *response;
 
-       if (rigol_ds_send(sdi, cmd) != SR_OK)
+       if (sr_scpi_send(scpi, cmd) != SR_OK)
                return SR_ERR;
 
-       if ((len = read(usbtmc->fd, reply, maxlen - 1)) < 0)
+       if (sr_scpi_receive(scpi, &response) != SR_OK)
                return SR_ERR;
-       reply[len] = '\0';
+
+       g_strlcpy(reply, response, maxlen);
+       g_free(response);
+       len = strlen(reply);
 
        if (devc->model->protocol == PROTOCOL_IEEE488_2) {
                /* get rid of trailing linefeed */
@@ -744,11 +723,11 @@ SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi)
 
        if (devc->model->protocol == PROTOCOL_IEEE488_2) {
                /* Vertical reference - not certain if this is the place to read it. */
-               if (rigol_ds_send(sdi, ":WAV:SOUR CHAN1") != SR_OK)
+               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 (rigol_ds_send(sdi, ":WAV:SOUR CHAN2") != SR_OK)
+               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;