]> sigrok.org Git - libsigrok.git/blobdiff - hardware/rigol-ds/api.c
rigol-ds: Support VS5000 series devices.
[libsigrok.git] / hardware / rigol-ds / api.c
index 31342c23321280add9dd87be37ff033009ae713b..201a6f5d79e9ea414b93f050d9e78338f115c6e1 100644 (file)
@@ -30,6 +30,7 @@
 
 static const int32_t hwopts[] = {
        SR_CONF_CONN,
+       SR_CONF_SERIALCOMM
 };
 
 static const int32_t hwcaps[] = {
@@ -163,6 +164,16 @@ static const struct rigol_ds_model supported_models[] = {
        {"DS2072", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {500, 1}, {500, 1000000}, false, 14},
        {"DS2102", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {500, 1}, {500, 1000000}, false, 14},
        {"DS2202", RIGOL_DS2000, PROTOCOL_IEEE488_2, {2, 1000000000}, {500, 1}, {500, 1000000}, false, 14},
+       {"VS5022", RIGOL_VS5000, PROTOCOL_LEGACY, {20, 1000000000}, {50, 1}, {2, 1000}, false, 14},
+       {"VS5022D", RIGOL_VS5000, PROTOCOL_LEGACY, {20, 1000000000}, {50, 1}, {2, 1000}, true, 14},
+       {"VS5042", RIGOL_VS5000, PROTOCOL_LEGACY, {10, 1000000000}, {50, 1}, {2, 1000}, false, 14},
+       {"VS5042D", RIGOL_VS5000, PROTOCOL_LEGACY, {10, 1000000000}, {50, 1}, {2, 1000}, true, 14},
+       {"VS5062", RIGOL_VS5000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, false, 14},
+       {"VS5062D", RIGOL_VS5000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, true, 14},
+       {"VS5102", RIGOL_VS5000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, false, 14},
+       {"VS5102D", RIGOL_VS5000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, true, 14},
+       {"VS5202", RIGOL_VS5000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, false, 14},
+       {"VS5202D", RIGOL_VS5000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, true, 14},
 };
 
 SR_PRIV struct sr_dev_driver rigol_ds_driver_info;
@@ -192,12 +203,13 @@ static int dev_clear(void)
 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
 {
        va_list args;
-       char buf[256];
+       int ret;
 
        va_start(args, format);
-       vsnprintf(buf, 255, format, args);
+       ret = sr_scpi_send_variadic(sdi->conn, format, args);
        va_end(args);
-       if (rigol_ds_send(sdi, buf) != SR_OK)
+
+       if (ret != SR_OK)
                return SR_ERR;
 
        /* When setting a bunch of parameters in a row, the DS1052E scrambles
@@ -213,77 +225,89 @@ static int init(struct sr_context *sr_ctx)
        return std_init(sr_ctx, di, LOG_PREFIX);
 }
 
-static int probe_port(const char *port, GSList **devices)
+static int probe_port(const char *resource, const char *serialcomm, GSList **devices)
 {
        struct dev_context *devc;
        struct sr_dev_inst *sdi;
-       struct sr_usbtmc_dev_inst *usbtmc;
+       const char *usbtmc_prefix = "/dev/usbtmc";
+       const char *tcp_prefix = "tcp/";
+       gchar **tokens, *address, *port;
+       struct sr_scpi_dev_inst *scpi;
+       struct sr_scpi_hw_info *hw_info;
        struct sr_probe *probe;
        unsigned int i;
-       int len, num_tokens;
-       const char *manufacturer, *model_name, *version;
        const struct rigol_ds_model *model = NULL;
-       char buf[256];
-       gchar **tokens, *channel_name;
+       gchar *channel_name;
 
        *devices = NULL;
-       if (!(usbtmc = sr_usbtmc_dev_inst_new(port)))
-               return SR_ERR_MALLOC;
-
-       if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
-               return SR_ERR;
-       len = write(usbtmc->fd, "*IDN?", 5);
-       len = read(usbtmc->fd, buf, sizeof(buf));
-       if (close(usbtmc->fd) < 0)
-               return SR_ERR;
-
-       sr_usbtmc_dev_inst_free(usbtmc);
-
-       if (len == 0)
-               return SR_ERR_NA;
 
-       buf[len] = 0;
-       tokens = g_strsplit(buf, ",", 0);
-       sr_dbg("response: %s [%s]", port, buf);
-
-       for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
-
-       if (num_tokens < 4) {
+       if (strncmp(resource, usbtmc_prefix, strlen(usbtmc_prefix)) == 0) {
+               sr_dbg("Opening USBTMC device %s", resource);
+               if (!(scpi = scpi_usbtmc_dev_inst_new(resource)))
+                       return SR_ERR_MALLOC;
+       } else if (strncmp(resource, tcp_prefix, strlen(tcp_prefix)) == 0) {
+               sr_dbg("Opening TCP connection %s", resource);
+               tokens = g_strsplit(resource + strlen(tcp_prefix), "/", 0);
+               address = tokens[0];
+               port = tokens[1];
+               if (!address || !port || tokens[2]) {
+                       sr_dbg("Invalid parameters");
+                       g_strfreev(tokens);
+                       return SR_ERR_ARG;
+               }
+               scpi = scpi_tcp_dev_inst_new(address, port);
                g_strfreev(tokens);
-               return SR_ERR_NA;
+               if (!scpi)
+                       return SR_ERR_MALLOC;
+       } else {
+               sr_dbg("Opening serial device %s", resource);
+               if (!(scpi = scpi_serial_dev_inst_new(resource, serialcomm)))
+                       return SR_ERR_MALLOC;
        }
 
-       manufacturer = tokens[0];
-       model_name = tokens[1];
-       version = tokens[3];
+       if (sr_scpi_open(scpi) != SR_OK) {
+               sr_scpi_free(scpi);
+               return SR_ERR;
+       };
 
-       if (strcasecmp(manufacturer, "Rigol Technologies")) {
-               g_strfreev(tokens);
+       if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
+               sr_info("Couldn't get IDN response.");
+               sr_scpi_close(scpi);
+               sr_scpi_free(scpi);
+               return SR_ERR;
+       }
+
+       if (strcasecmp(hw_info->manufacturer, "Rigol Technologies")) {
+               sr_scpi_hw_info_free(hw_info);
+               sr_scpi_free(scpi);
                return SR_ERR_NA;
        }
 
        for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
-               if (!strcmp(model_name, supported_models[i].name)) {
+               if (!strcmp(hw_info->model, supported_models[i].name)) {
                        model = &supported_models[i];
                        break;
                }
        }
 
        if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
-                                             manufacturer, model_name, version))) {
-               g_strfreev(tokens);
+                                             hw_info->manufacturer, hw_info->model,
+                                                 hw_info->firmware_version))) {
+               sr_scpi_hw_info_free(hw_info);
+               sr_scpi_free(scpi);
                return SR_ERR_NA;
        }
 
-       g_strfreev(tokens);
+       sr_scpi_hw_info_free(hw_info);
+
+       sdi->conn = scpi;
 
-       if (!(sdi->conn = sr_usbtmc_dev_inst_new(port)))
-               return SR_ERR_MALLOC;
        sdi->driver = di;
-       sdi->inst_type = SR_INST_USBTMC;
+       sdi->inst_type = SR_INST_SCPI;
 
        if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
                return SR_ERR_MALLOC;
+
        devc->limit_frames = 0;
        devc->model = model;
 
@@ -309,10 +333,10 @@ static int probe_port(const char *port, GSList **devices)
                        sdi->probes = g_slist_append(sdi->probes, probe);
                        devc->digital_group.probes = g_slist_append(
                                        devc->digital_group.probes, probe);
-                       devc->digital_group.name = "LA";
-                       sdi->probe_groups = g_slist_append(sdi->probe_groups,
-                                       &devc->digital_group);
                }
+               devc->digital_group.name = "LA";
+               sdi->probe_groups = g_slist_append(sdi->probe_groups,
+                               &devc->digital_group);
        }
 
        for (i = 0; i < NUM_TIMEBASE; i++) {
@@ -352,21 +376,30 @@ static GSList *scan(GSList *options)
        int ret;
        const gchar *dev_name;
        gchar *port = NULL;
+       gchar *serialcomm = NULL;
 
        drvc = di->priv;
 
        for (l = options; l; l = l->next) {
                src = l->data;
-               if (src->key == SR_CONF_CONN) {
+               switch (src->key) {
+               case SR_CONF_CONN:
                        port = (char *)g_variant_get_string(src->data, NULL);
                        break;
+               case SR_CONF_SERIALCOMM:
+                       serialcomm = (char *)g_variant_get_string(src->data, NULL);
+                       break;
                }
        }
 
        devices = NULL;
        if (port) {
-               if (probe_port(port, &devices) == SR_ERR_MALLOC)
+               if (probe_port(port, serialcomm, &devices) == SR_ERR_MALLOC) {
+                       g_free(port);
+                       if (serialcomm)
+                               g_free(serialcomm);
                        return NULL;
+               }
        } else {
                if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
                        if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
@@ -375,8 +408,10 @@ static GSList *scan(GSList *options)
                        if (strncmp(dev_name, "usbtmc", 6))
                                continue;
                        port = g_strconcat("/dev/", dev_name, NULL);
-                       ret = probe_port(port, &devices);
+                       ret = probe_port(port, serialcomm, &devices);
                        g_free(port);
+                       if (serialcomm)
+                               g_free(serialcomm);
                        if (ret == SR_ERR_MALLOC) {
                                g_dir_close(dir);
                                return NULL;
@@ -399,9 +434,9 @@ static GSList *dev_list(void)
 
 static int dev_open(struct sr_dev_inst *sdi)
 {
-       struct sr_usbtmc_dev_inst *usbtmc = sdi->conn;
+       struct sr_scpi_dev_inst *scpi = sdi->conn;
 
-       if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
+       if (sr_scpi_open(scpi) < 0)
                return SR_ERR;
 
        if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
@@ -414,12 +449,13 @@ static int dev_open(struct sr_dev_inst *sdi)
 
 static int dev_close(struct sr_dev_inst *sdi)
 {
-       struct sr_usbtmc_dev_inst *usbtmc;
+       struct sr_scpi_dev_inst *scpi;
 
-       usbtmc = sdi->conn;
-       if (usbtmc && usbtmc->fd != -1) {
-               close(usbtmc->fd);
-               usbtmc->fd = -1;
+       scpi = sdi->conn;
+
+       if (scpi) {
+               if (sr_scpi_close(scpi) < 0)
+                       return SR_ERR;
                sdi->status = SR_ST_INACTIVE;
        }
 
@@ -739,7 +775,7 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
 
 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
 {
-       struct sr_usbtmc_dev_inst *usbtmc;
+       struct sr_scpi_dev_inst *scpi;
        struct dev_context *devc;
        struct sr_probe *probe;
        GSList *l;
@@ -748,11 +784,11 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
-       usbtmc = sdi->conn;
+       scpi = sdi->conn;
        devc = sdi->priv;
 
        if (devc->data_source == DATA_SOURCE_LIVE) {
-               if (rigol_ds_send(sdi, ":RUN") != SR_OK)
+               if (sr_scpi_send(sdi->conn, ":RUN") != SR_OK)
                        return SR_ERR;
        } else if (devc->data_source == DATA_SOURCE_MEMORY) {
                if (devc->model->series != RIGOL_DS2000) {
@@ -775,7 +811,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
                                /* Enabled channel is currently disabled, or vice versa. */
                                sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
                                                probe->enabled ? "ON" : "OFF");
-                               if (rigol_ds_send(sdi, cmd) != SR_OK)
+                               if (sr_scpi_send(sdi->conn, cmd) != SR_OK)
                                        return SR_ERR;
                        }
                } else if (probe->type == SR_PROBE_LOGIC) {
@@ -786,7 +822,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
                                /* Enabled channel is currently disabled, or vice versa. */
                                sprintf(cmd, ":DIG%d:TURN %s", probe->index,
                                                probe->enabled ? "ON" : "OFF");
-                               if (rigol_ds_send(sdi, cmd) != SR_OK)
+                               if (sr_scpi_send(sdi->conn, cmd) != SR_OK)
                                        return SR_ERR;
                        }
                }
@@ -794,7 +830,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
                return SR_ERR;
 
-       sr_source_add(usbtmc->fd, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
+       sr_scpi_source_add(scpi, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
 
        /* Send header packet to the session bus. */
        std_session_send_df_header(cb_data, LOG_PREFIX);
@@ -804,12 +840,12 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
                if (devc->enabled_analog_probes) {
                        devc->analog_frame_size = DS1000_ANALOG_LIVE_WAVEFORM_SIZE;
                        devc->channel_frame = devc->enabled_analog_probes->data;
-                       if (rigol_ds_send(sdi, ":WAV:DATA? CHAN%d",
+                       if (sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%d",
                                        devc->channel_frame->index + 1) != SR_OK)
                                return SR_ERR;
                } else {
                        devc->channel_frame = devc->enabled_digital_probes->data;
-                       if (rigol_ds_send(sdi, ":WAV:DATA? DIG") != SR_OK)
+                       if (sr_scpi_send(sdi->conn, ":WAV:DATA? DIG") != SR_OK)
                                return SR_ERR;
                }
 
@@ -825,11 +861,11 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
                                /* Apparently for the DS2000 the memory
                                 * depth can only be set in Running state -
                                 * this matches the behaviour of the UI. */
-                               if (rigol_ds_send(sdi, ":RUN") != SR_OK)
+                               if (sr_scpi_send(sdi->conn, ":RUN") != SR_OK)
                                        return SR_ERR;
-                               if (rigol_ds_send(sdi, "ACQ:MDEP %d", devc->analog_frame_size) != SR_OK)
+                               if (sr_scpi_send(sdi->conn, "ACQ:MDEP %d", devc->analog_frame_size) != SR_OK)
                                        return SR_ERR;
-                               if (rigol_ds_send(sdi, ":STOP") != SR_OK)
+                               if (sr_scpi_send(sdi->conn, ":STOP") != SR_OK)
                                        return SR_ERR;
                        } else
                                devc->analog_frame_size = DS2000_ANALOG_LIVE_WAVEFORM_SIZE;
@@ -845,7 +881,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
        struct dev_context *devc;
-       struct sr_usbtmc_dev_inst *usbtmc;
+       struct sr_scpi_dev_inst *scpi;
 
        (void)cb_data;
 
@@ -860,8 +896,8 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
        g_slist_free(devc->enabled_digital_probes);
        devc->enabled_analog_probes = NULL;
        devc->enabled_digital_probes = NULL;
-       usbtmc = sdi->conn;
-       sr_source_remove(usbtmc->fd);
+       scpi = sdi->conn;
+       sr_scpi_source_remove(scpi);
 
        return SR_OK;
 }