]> sigrok.org Git - libsigrok.git/blobdiff - hardware/rigol-ds/api.c
Create & use new sr_usbtmc_dev_inst for Rigol DS driver.
[libsigrok.git] / hardware / rigol-ds / api.c
index e0f4eaea2a388f1a7eb2deaad60abbf3125ab223..31342c23321280add9dd87be37ff033009ae713b 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright (C) 2012 Martin Ling <martin-git@earth.li>
  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
+ * Copyright (C) 2013 Mathias Grimmberger <mgri@zaphod.sax.de>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -27,9 +28,6 @@
 #include "libsigrok-internal.h"
 #include "protocol.h"
 
-#define NUM_TIMEBASE  12
-#define NUM_VDIV      8
-
 static const int32_t hwopts[] = {
        SR_CONF_CONN,
 };
@@ -47,6 +45,7 @@ static const int32_t analog_hwcaps[] = {
        SR_CONF_NUM_VDIV,
        SR_CONF_VDIV,
        SR_CONF_COUPLING,
+       SR_CONF_DATA_SOURCE,
 };
 
 static const uint64_t timebases[][2] = {
@@ -85,10 +84,17 @@ static const uint64_t timebases[][2] = {
        { 10, 1 },
        { 20, 1 },
        { 50, 1 },
+       { 100, 1 },
+       { 200, 1 },
+       { 500, 1 },
+       /* { 1000, 1 }, Confuses other code? */
 };
 
 static const uint64_t vdivs[][2] = {
+       /* microvolts */
+       { 500, 1000000 },
        /* millivolts */
+       { 1, 1000 },
        { 2, 1000 },
        { 5, 1000 },
        { 10, 1000 },
@@ -104,6 +110,9 @@ static const uint64_t vdivs[][2] = {
        { 10, 1 },
 };
 
+#define NUM_TIMEBASE  ARRAY_SIZE(timebases)
+#define NUM_VDIV      ARRAY_SIZE(vdivs)
+
 static const char *trigger_sources[] = {
        "CH1",
        "CH2",
@@ -133,13 +142,27 @@ static const char *coupling[] = {
        "GND",
 };
 
-static const char *supported_models[] = {
-       "DS1052E",
-       "DS1102E",
-       "DS1152E",
-       "DS1052D",
-       "DS1102D",
-       "DS1152D",
+/* Do not change the order of entries */
+static const char *data_sources[] = {
+       "Live",
+       "Memory",
+       "Segmented",
+};
+
+/* 
+ * name, series, protocol flavor, min timebase, max timebase, min vdiv,
+ * digital channels, number of horizontal divs
+ */
+static const struct rigol_ds_model supported_models[] = {
+       {"DS1052E", RIGOL_DS1000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, false, 12},
+       {"DS1102E", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, false, 12},
+       {"DS1152E", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, false, 12},
+       {"DS1052D", RIGOL_DS1000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, true, 12},
+       {"DS1102D", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, true, 12},
+       {"DS1152D", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, true, 12},
+       {"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},
 };
 
 SR_PRIV struct sr_dev_driver rigol_ds_driver_info;
@@ -150,6 +173,8 @@ static void clear_helper(void *priv)
        struct dev_context *devc;
 
        devc = priv;
+       g_free(devc->data);
+       g_free(devc->buffer);
        g_free(devc->coupling[0]);
        g_free(devc->coupling[1]);
        g_free(devc->trigger_source);
@@ -192,27 +217,27 @@ static int probe_port(const char *port, GSList **devices)
 {
        struct dev_context *devc;
        struct sr_dev_inst *sdi;
-       struct sr_serial_dev_inst *serial;
+       struct sr_usbtmc_dev_inst *usbtmc;
        struct sr_probe *probe;
        unsigned int i;
        int len, num_tokens;
-       gboolean matched, has_digital;
-       const char *manufacturer, *model, *version;
+       const char *manufacturer, *model_name, *version;
+       const struct rigol_ds_model *model = NULL;
        char buf[256];
        gchar **tokens, *channel_name;
 
        *devices = NULL;
-       if (!(serial = sr_serial_dev_inst_new(port, NULL)))
+       if (!(usbtmc = sr_usbtmc_dev_inst_new(port)))
                return SR_ERR_MALLOC;
 
-       if (serial_open(serial, SERIAL_RDWR) != SR_OK)
+       if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
                return SR_ERR;
-       len = serial_write(serial, "*IDN?", 5);
-       len = serial_read(serial, buf, sizeof(buf));
-       if (serial_close(serial) != SR_OK)
+       len = write(usbtmc->fd, "*IDN?", 5);
+       len = read(usbtmc->fd, buf, sizeof(buf));
+       if (close(usbtmc->fd) < 0)
                return SR_ERR;
 
-       sr_serial_dev_inst_free(serial);
+       sr_usbtmc_dev_inst_free(usbtmc);
 
        if (len == 0)
                return SR_ERR_NA;
@@ -229,40 +254,38 @@ static int probe_port(const char *port, GSList **devices)
        }
 
        manufacturer = tokens[0];
-       model = tokens[1];
+       model_name = tokens[1];
        version = tokens[3];
 
-       if (strcmp(manufacturer, "Rigol Technologies")) {
+       if (strcasecmp(manufacturer, "Rigol Technologies")) {
                g_strfreev(tokens);
                return SR_ERR_NA;
        }
 
-       matched = has_digital = FALSE;
        for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
-               if (!strcmp(model, supported_models[i])) {
-                       matched = TRUE;
-                       has_digital = g_str_has_suffix(model, "D");
+               if (!strcmp(model_name, supported_models[i].name)) {
+                       model = &supported_models[i];
                        break;
                }
        }
 
-       if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
-               manufacturer, model, version))) {
+       if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
+                                             manufacturer, model_name, version))) {
                g_strfreev(tokens);
                return SR_ERR_NA;
        }
 
        g_strfreev(tokens);
 
-       if (!(sdi->conn = sr_serial_dev_inst_new(port, NULL)))
+       if (!(sdi->conn = sr_usbtmc_dev_inst_new(port)))
                return SR_ERR_MALLOC;
        sdi->driver = di;
-       sdi->inst_type = SR_INST_SERIAL;
+       sdi->inst_type = SR_INST_USBTMC;
 
        if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
                return SR_ERR_MALLOC;
        devc->limit_frames = 0;
-       devc->has_digital = has_digital;
+       devc->model = model;
 
        for (i = 0; i < 2; i++) {
                channel_name = (i == 0 ? "CH1" : "CH2");
@@ -275,7 +298,7 @@ static int probe_port(const char *port, GSList **devices)
                                &devc->analog_groups[i]);
        }
 
-       if (devc->has_digital) {
+       if (devc->model->has_digital) {
                for (i = 0; i < 16; i++) {
                        if (!(channel_name = g_strdup_printf("D%d", i)))
                                return SR_ERR_MALLOC;
@@ -291,6 +314,28 @@ static int probe_port(const char *port, GSList **devices)
                                        &devc->digital_group);
                }
        }
+
+       for (i = 0; i < NUM_TIMEBASE; i++) {
+               if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
+                       devc->timebases = &timebases[i];
+               if (!memcmp(&devc->model->max_timebase, &timebases[i], sizeof(uint64_t[2])))
+                       devc->num_timebases = &timebases[i] - devc->timebases + 1;
+       }
+
+       for (i = 0; i < NUM_VDIV; i++) {
+               if (!memcmp(&devc->model->min_vdiv, &vdivs[i], sizeof(uint64_t[2]))) {
+                       devc->vdivs = &vdivs[i];
+                       devc->num_vdivs = NUM_VDIV - (&vdivs[i] - &vdivs[0]);
+               }
+       }
+
+       if (!(devc->buffer = g_try_malloc(ACQ_BUFFER_SIZE)))
+               return SR_ERR_MALLOC;
+       if (!(devc->data = g_try_malloc(ACQ_BUFFER_SIZE * sizeof(float))))
+               return SR_ERR_MALLOC;
+
+       devc->data_source = DATA_SOURCE_LIVE;
+
        sdi->priv = devc;
 
        *devices = g_slist_append(NULL, sdi);
@@ -354,8 +399,9 @@ static GSList *dev_list(void)
 
 static int dev_open(struct sr_dev_inst *sdi)
 {
+       struct sr_usbtmc_dev_inst *usbtmc = sdi->conn;
 
-       if (serial_open(sdi->conn, SERIAL_RDWR) != SR_OK)
+       if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
                return SR_ERR;
 
        if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
@@ -368,11 +414,12 @@ static int dev_open(struct sr_dev_inst *sdi)
 
 static int dev_close(struct sr_dev_inst *sdi)
 {
-       struct sr_serial_dev_inst *serial;
+       struct sr_usbtmc_dev_inst *usbtmc;
 
-       serial = sdi->conn;
-       if (serial && serial->fd != -1) {
-               serial_close(serial);
+       usbtmc = sdi->conn;
+       if (usbtmc && usbtmc->fd != -1) {
+               close(usbtmc->fd);
+               usbtmc->fd = -1;
                sdi->status = SR_ST_INACTIVE;
        }
 
@@ -404,7 +451,7 @@ static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
 
        switch (id) {
        case SR_CONF_NUM_TIMEBASE:
-               *data = g_variant_new_int32(NUM_TIMEBASE);
+               *data = g_variant_new_int32(devc->num_timebases);
                break;
        case SR_CONF_NUM_VDIV:
                if (!probe_group) {
@@ -413,11 +460,19 @@ static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
                }
                for (i = 0; i < 2; i++) {
                        if (probe_group == &devc->analog_groups[i]) {
-                               *data = g_variant_new_int32(NUM_VDIV);
+                               *data = g_variant_new_int32(devc->num_vdivs);
                                return SR_OK;
                        }
                }
                return SR_ERR_NA;
+       case SR_CONF_DATA_SOURCE:
+               if (devc->data_source == DATA_SOURCE_LIVE)
+                       *data = g_variant_new_string("Live");
+               else if (devc->data_source == DATA_SOURCE_MEMORY)
+                       *data = g_variant_new_string("Memory");
+               else
+                       *data = g_variant_new_string("Segmented");
+               break;
        default:
                return SR_ERR_NA;
        }
@@ -470,19 +525,19 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
                devc->horiz_triggerpos = t_dbl;
                /* We have the trigger offset as a percentage of the frame, but
                 * need to express this in seconds. */
-               t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * NUM_TIMEBASE;
+               t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
                ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
                break;
        case SR_CONF_TIMEBASE:
                g_variant_get(data, "(tt)", &p, &q);
-               for (i = 0; i < ARRAY_SIZE(timebases); i++) {
-                       if (timebases[i][0] == p && timebases[i][1] == q) {
+               for (i = 0; i < devc->num_timebases; i++) {
+                       if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
                                devc->timebase = (float)p / q;
                                ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
                                break;
                        }
                }
-               if (i == ARRAY_SIZE(timebases))
+               if (i == devc->num_timebases)
                        ret = SR_ERR_ARG;
                break;
        case SR_CONF_TRIGGER_SOURCE:
@@ -545,6 +600,18 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
                        }
                }
                return SR_ERR_NA;
+       case SR_CONF_DATA_SOURCE:
+               tmp_str = g_variant_get_string(data, NULL);
+               if (!strcmp(tmp_str, "Live"))
+                       devc->data_source = DATA_SOURCE_LIVE;
+               else if (!strcmp(tmp_str, "Memory"))
+                       devc->data_source = DATA_SOURCE_MEMORY;
+               else if (devc->model->protocol == PROTOCOL_IEEE488_2
+                        && !strcmp(tmp_str, "Segmented"))
+                       devc->data_source = DATA_SOURCE_SEGMENTED;
+               else
+                       return SR_ERR;
+               break;
        default:
                ret = SR_ERR_NA;
                break;
@@ -559,7 +626,10 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
        GVariant *tuple, *rational[2];
        GVariantBuilder gvb;
        unsigned int i;
-       struct dev_context *devc;
+       struct dev_context *devc = NULL;
+
+       if (sdi)
+               devc = sdi->priv;
 
        if (key == SR_CONF_SCAN_OPTIONS) {
                *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
@@ -614,32 +684,51 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
                *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
                break;
        case SR_CONF_VDIV:
+               if (!devc)
+                       /* Can't know this until we have the exact model. */
+                       return SR_ERR_ARG;
                if (!probe_group) {
                        sr_err("No probe group specified.");
                        return SR_ERR_PROBE_GROUP;
                }
                g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
-               for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
-                       rational[0] = g_variant_new_uint64(vdivs[i][0]);
-                       rational[1] = g_variant_new_uint64(vdivs[i][1]);
+               for (i = 0; i < devc->num_vdivs; i++) {
+                       rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
+                       rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
                        tuple = g_variant_new_tuple(rational, 2);
                        g_variant_builder_add_value(&gvb, tuple);
                }
                *data = g_variant_builder_end(&gvb);
                break;
        case SR_CONF_TIMEBASE:
+               if (!devc)
+                       /* Can't know this until we have the exact model. */
+                       return SR_ERR_ARG;
                g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
-               for (i = 0; i < ARRAY_SIZE(timebases); i++) {
-                       rational[0] = g_variant_new_uint64(timebases[i][0]);
-                       rational[1] = g_variant_new_uint64(timebases[i][1]);
+               for (i = 0; i < devc->num_timebases; i++) {
+                       rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
+                       rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
                        tuple = g_variant_new_tuple(rational, 2);
                        g_variant_builder_add_value(&gvb, tuple);
                }
                *data = g_variant_builder_end(&gvb);
                break;
        case SR_CONF_TRIGGER_SOURCE:
+               if (!devc)
+                       /* Can't know this until we have the exact model. */
+                       return SR_ERR_ARG;
                *data = g_variant_new_strv(trigger_sources,
-                               devc->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
+                               devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
+               break;
+       case SR_CONF_DATA_SOURCE:
+               if (!devc)
+                       /* Can't know this until we have the exact model. */
+                       return SR_ERR_ARG;
+               /* This needs tweaking by series/model! */
+               if (devc->model->series == RIGOL_DS2000)
+                       *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
+               else
+                       *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
                break;
        default:
                return SR_ERR_NA;
@@ -650,7 +739,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_serial_dev_inst *serial;
+       struct sr_usbtmc_dev_inst *usbtmc;
        struct dev_context *devc;
        struct sr_probe *probe;
        GSList *l;
@@ -659,9 +748,22 @@ 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;
 
-       serial = sdi->conn;
+       usbtmc = sdi->conn;
        devc = sdi->priv;
 
+       if (devc->data_source == DATA_SOURCE_LIVE) {
+               if (rigol_ds_send(sdi, ":RUN") != SR_OK)
+                       return SR_ERR;
+       } else if (devc->data_source == DATA_SOURCE_MEMORY) {
+               if (devc->model->series != RIGOL_DS2000) {
+                       sr_err("Data source 'Memory' not supported for this device");
+                       return SR_ERR;
+               }
+       } else if (devc->data_source == DATA_SOURCE_SEGMENTED) {
+               sr_err("Data source 'Segmented' not yet supported");
+               return SR_ERR;
+       }
+
        for (l = sdi->probes; l; l = l->next) {
                probe = l->data;
                sr_dbg("handling probe %s", probe->name);
@@ -692,32 +794,58 @@ 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(serial->fd, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
+       sr_source_add(usbtmc->fd, 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);
 
-       /* Fetch the first frame. */
-       if (devc->enabled_analog_probes) {
-               devc->channel_frame = devc->enabled_analog_probes->data;
-               if (rigol_ds_send(sdi, ":WAV:DATA? CHAN%d",
-                               devc->channel_frame->index + 1) != SR_OK)
-                       return SR_ERR;
+       if (devc->model->protocol == PROTOCOL_LEGACY) {
+               /* Fetch the first frame. */
+               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",
+                                       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)
+                               return SR_ERR;
+               }
+
+               devc->num_frame_bytes = 0;
        } else {
-               devc->channel_frame = devc->enabled_digital_probes->data;
-               if (rigol_ds_send(sdi, ":WAV:DATA? DIG") != SR_OK)
-                       return SR_ERR;
+               if (devc->enabled_analog_probes) {
+                       if (devc->data_source == DATA_SOURCE_MEMORY)
+                       {
+                               if (g_slist_length(devc->enabled_analog_probes) == 1)
+                                       devc->analog_frame_size = DS2000_ANALOG_MEM_WAVEFORM_SIZE_1C;
+                               else
+                                       devc->analog_frame_size = DS2000_ANALOG_MEM_WAVEFORM_SIZE_2C;
+                               /* 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)
+                                       return SR_ERR;
+                               if (rigol_ds_send(sdi, "ACQ:MDEP %d", devc->analog_frame_size) != SR_OK)
+                                       return SR_ERR;
+                               if (rigol_ds_send(sdi, ":STOP") != SR_OK)
+                                       return SR_ERR;
+                       } else
+                               devc->analog_frame_size = DS2000_ANALOG_LIVE_WAVEFORM_SIZE;
+                       devc->channel_frame = devc->enabled_analog_probes->data;
+                       if (rigol_ds_capture_start(sdi) != SR_OK)
+                               return SR_ERR;
+               }
        }
 
-       devc->num_frame_bytes = 0;
-
        return SR_OK;
 }
 
 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
        struct dev_context *devc;
-       struct sr_serial_dev_inst *serial;
+       struct sr_usbtmc_dev_inst *usbtmc;
 
        (void)cb_data;
 
@@ -732,8 +860,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;
-       serial = sdi->conn;
-       sr_source_remove(serial->fd);
+       usbtmc = sdi->conn;
+       sr_source_remove(usbtmc->fd);
 
        return SR_OK;
 }