]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/rigol-ds/api.c
config_list: Don't check for sdi->priv != NULL.
[libsigrok.git] / src / hardware / rigol-ds / api.c
index 257bd11ee082df2d7eccfcc473562178380e4783..e743608381952bc46825564cca42650bac46b028 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
+#include <strings.h>
 #include <math.h>
 #include <glib.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
+#include "scpi.h"
 #include "protocol.h"
 
 static const uint32_t scanopts[] = {
@@ -41,15 +44,15 @@ static const uint32_t devopts[] = {
        SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
        SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
        SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_SET,
-       SR_CONF_NUM_TIMEBASE | SR_CONF_GET,
+       SR_CONF_NUM_HDIV | SR_CONF_GET,
        SR_CONF_SAMPLERATE | SR_CONF_GET,
+       SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
 };
 
 static const uint32_t analog_devopts[] = {
        SR_CONF_NUM_VDIV | SR_CONF_GET,
        SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
        SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
-       SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
 };
 
 static const uint64_t timebases[][2] = {
@@ -175,6 +178,7 @@ enum series {
        DS2000,
        DS2000A,
        DSO1000,
+       DS1000Z,
 };
 
 /* short name, full name */
@@ -192,11 +196,13 @@ static const struct rigol_ds_series supported_series[] = {
        [DS1000] = {VENDOR(RIGOL), "DS1000", PROTOCOL_V2, FORMAT_IEEE488_2,
                {50, 1}, {2, 1000}, 12, 600, 1048576},
        [DS2000] = {VENDOR(RIGOL), "DS2000", PROTOCOL_V3, FORMAT_IEEE488_2,
-               {500, 1}, {2, 1000}, 14, 1400, 14000},
+               {500, 1}, {500, 1000000}, 14, 1400, 14000},
        [DS2000A] = {VENDOR(RIGOL), "DS2000A", PROTOCOL_V3, FORMAT_IEEE488_2,
                {1000, 1}, {500, 1000000}, 14, 1400, 14000},
        [DSO1000] = {VENDOR(AGILENT), "DSO1000", PROTOCOL_V3, FORMAT_IEEE488_2,
                {50, 1}, {2, 1000}, 12, 600, 20480},
+       [DS1000Z] = {VENDOR(RIGOL), "DS1000Z", PROTOCOL_V4, FORMAT_IEEE488_2,
+               {50, 1}, {1, 1000}, 12, 1200, 12000000},
 };
 
 #define SERIES(x) &supported_series[x]
@@ -232,10 +238,18 @@ static const struct rigol_ds_model supported_models[] = {
        {SERIES(DSO1000), "DSO1014A", {2, 1000000000}, 4, false},
        {SERIES(DSO1000), "DSO1022A", {2, 1000000000}, 2, false},
        {SERIES(DSO1000), "DSO1024A", {2, 1000000000}, 4, false},
+       {SERIES(DS1000Z), "DS1054Z", {5, 1000000000}, 4, false},
+       {SERIES(DS1000Z), "DS1074Z", {5, 1000000000}, 4, false},
+       {SERIES(DS1000Z), "DS1104Z", {5, 1000000000}, 4, false},
+       {SERIES(DS1000Z), "DS1074Z-S", {5, 1000000000}, 4, false},
+       {SERIES(DS1000Z), "DS1104Z-S", {5, 1000000000}, 4, false},
+       {SERIES(DS1000Z), "MSO1074Z", {5, 1000000000}, 4, true},
+       {SERIES(DS1000Z), "MSO1104Z", {5, 1000000000}, 4, true},
+       {SERIES(DS1000Z), "MSO1074Z-S", {5, 1000000000}, 4, true},
+       {SERIES(DS1000Z), "MSO1104Z-S", {5, 1000000000}, 4, true},
 };
 
 SR_PRIV struct sr_dev_driver rigol_ds_driver_info;
-static struct sr_dev_driver *di = &rigol_ds_driver_info;
 
 static void clear_helper(void *priv)
 {
@@ -250,20 +264,14 @@ static void clear_helper(void *priv)
        g_free(devc->trigger_source);
        g_free(devc->trigger_slope);
        g_free(devc->analog_groups);
-       g_free(devc->digital_group);
        g_free(devc);
 }
 
-static int dev_clear(void)
+static int dev_clear(const struct sr_dev_driver *di)
 {
        return std_dev_clear(di, clear_helper);
 }
 
-static int init(struct sr_context *sr_ctx)
-{
-       return std_init(sr_ctx, di, LOG_PREFIX);
-}
-
 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
 {
        struct dev_context *devc;
@@ -286,7 +294,7 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
        }
 
        for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
-               if (!strcasecmp(hw_info->manufacturer,
+               if (!g_ascii_strcasecmp(hw_info->manufacturer,
                                        supported_models[i].series->vendor->full_name) &&
                                !strcmp(hw_info->model, supported_models[i].name)) {
                        model = &supported_models[i];
@@ -300,12 +308,11 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
        }
 
        sdi = g_malloc0(sizeof(struct sr_dev_inst));
-       sdi->status = SR_ST_ACTIVE;
        sdi->vendor = g_strdup(model->series->vendor->name);
        sdi->model = g_strdup(model->name);
        sdi->version = g_strdup(hw_info->firmware_version);
        sdi->conn = scpi;
-       sdi->driver = di;
+       sdi->driver = &rigol_ds_driver_info;
        sdi->inst_type = SR_INST_SCPI;
        sdi->serial_num = g_strdup(hw_info->serial_number);
        devc = g_malloc0(sizeof(struct dev_context));
@@ -327,13 +334,13 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
                        }
                        if (i != 3)
                                break;
-                       if (n[0] != 0 || n[1] > 2)
-                               break;
-                       if (n[1] == 2 && n[2] > 3)
-                               break;
-                       sr_dbg("Found DS1000 firmware < 0.2.4, using raw data format.");
-                       devc->format = FORMAT_RAW;
-               } while(0);
+                       scpi->firmware_version = n[0] * 100 + n[1] * 10 + n[2];
+                       if (scpi->firmware_version < 24) {
+                               sr_dbg("Found DS1000 firmware < 0.2.4, using raw data format.");
+                               devc->format = FORMAT_RAW;
+                       }
+                       break;
+               } while (0);
                g_strfreev(version);
        }
 
@@ -343,10 +350,8 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
                                        model->analog_channels);
 
        for (i = 0; i < model->analog_channels; i++) {
-               if (!(channel_name = g_strdup_printf("CH%d", i + 1)))
-                       return NULL;
-               ch = sr_channel_new(i, SR_CHANNEL_ANALOG, TRUE, channel_name);
-               sdi->channels = g_slist_append(sdi->channels, ch);
+               channel_name = g_strdup_printf("CH%d", i + 1);
+               ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_name);
 
                devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
 
@@ -357,14 +362,12 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
        }
 
        if (devc->model->has_digital) {
-               devc->digital_group = g_malloc0(sizeof(struct sr_channel_group*));
+               devc->digital_group = g_malloc0(sizeof(struct sr_channel_group));
 
                for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) {
-                       if (!(channel_name = g_strdup_printf("D%d", i)))
-                               return NULL;
-                       ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE, channel_name);
+                       channel_name = g_strdup_printf("D%d", i);
+                       ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
                        g_free(channel_name);
-                       sdi->channels = g_slist_append(sdi->channels, ch);
                        devc->digital_group->channels = g_slist_append(
                                        devc->digital_group->channels, ch);
                }
@@ -380,14 +383,16 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
                        devc->num_timebases = &timebases[i] - devc->timebases + 1;
        }
 
-       for (i = 0; i < NUM_VDIV; i++)
-               if (!memcmp(&devc->model->series->min_vdiv, &vdivs[i], sizeof(uint64_t[2])))
+       for (i = 0; i < NUM_VDIV; i++) {
+               if (!memcmp(&devc->model->series->min_vdiv,
+                                       &vdivs[i], sizeof(uint64_t[2]))) {
                        devc->vdivs = &vdivs[i];
+                       devc->num_vdivs = NUM_VDIV - i;
+               }
+       }
 
-       if (!(devc->buffer = g_try_malloc(ACQ_BUFFER_SIZE)))
-               return NULL;
-       if (!(devc->data = g_try_malloc(ACQ_BUFFER_SIZE * sizeof(float))))
-               return NULL;
+       devc->buffer = g_malloc(ACQ_BUFFER_SIZE);
+       devc->data = g_malloc(ACQ_BUFFER_SIZE * sizeof(float));
 
        devc->data_source = DATA_SOURCE_LIVE;
 
@@ -396,14 +401,9 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
        return sdi;
 }
 
-static GSList *scan(GSList *options)
+static GSList *scan(struct sr_dev_driver *di, GSList *options)
 {
-       return sr_scpi_scan(di->priv, options, probe_device);
-}
-
-static GSList *dev_list(void)
-{
-       return ((struct drv_context *)(di->priv))->instances;
+       return sr_scpi_scan(di->context, options, probe_device);
 }
 
 static int dev_open(struct sr_dev_inst *sdi)
@@ -449,11 +449,6 @@ static int dev_close(struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int cleanup(void)
-{
-       return dev_clear();
-}
-
 static int analog_frame_size(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc = sdi->priv;
@@ -502,13 +497,15 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
        const char *tmp_str;
        uint64_t samplerate;
        int analog_channel = -1;
-       float smallest_diff = 0.0000000001;
+       float smallest_diff = INFINITY;
        int idx = -1;
        unsigned i;
 
-       if (!sdi || !(devc = sdi->priv))
+       if (!sdi)
                return SR_ERR_ARG;
 
+       devc = sdi->priv;
+
        /* If a channel group is specified, it must be a valid one. */
        if (cg && !g_slist_find(sdi->channel_groups, cg)) {
                sr_err("Invalid channel group specified.");
@@ -527,11 +524,11 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
        }
 
        switch (key) {
-       case SR_CONF_NUM_TIMEBASE:
+       case SR_CONF_NUM_HDIV:
                *data = g_variant_new_int32(devc->model->series->num_horizontal_divs);
                break;
        case SR_CONF_NUM_VDIV:
-               *data = g_variant_new_int32(NUM_VDIV);
+               *data = g_variant_new_int32(devc->num_vdivs);
                break;
        case SR_CONF_DATA_SOURCE:
                if (devc->data_source == DATA_SOURCE_LIVE)
@@ -620,7 +617,6 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
                *data = g_variant_new_string(devc->coupling[analog_channel]);
                break;
        default:
-               sr_dbg("Tried to get unknown config key: %d.", key);
                return SR_ERR_NA;
        }
 
@@ -638,8 +634,7 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
        const char *tmp_str;
        char buffer[16];
 
-       if (!(devc = sdi->priv))
-               return SR_ERR_ARG;
+       devc = sdi->priv;
 
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
@@ -785,9 +780,7 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
                }
                break;
        default:
-               sr_dbg("Tried to set unknown config key: %d.", key);
-               ret = SR_ERR_NA;
-               break;
+               return SR_ERR_NA;
        }
 
        return ret;
@@ -801,32 +794,25 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
        unsigned int i;
        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_UINT32,
                                scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
                return SR_OK;
-       } else if (key == SR_CONF_DEVICE_OPTIONS && cg == NULL) {
+       } else if (key == SR_CONF_DEVICE_OPTIONS && !cg) {
                *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
                                devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
                return SR_OK;
        }
 
        /* Every other option requires a valid device instance. */
-       if (!sdi || !(devc = sdi->priv))
+       if (!sdi)
                return SR_ERR_ARG;
+       devc = sdi->priv;
 
        /* If a channel group is specified, it must be a valid one. */
-       if (cg) {
-               for (i = 0; i < devc->model->analog_channels; i++)
-                       if (cg == devc->analog_groups[i])
-                               break;
-               if (i >= devc->model->analog_channels) {
-                       sr_err("Invalid channel group specified.");
-                       return SR_ERR;
-               }
+       if (cg && !g_slist_find(sdi->channel_groups, cg)) {
+               sr_err("Invalid channel group specified.");
+               return SR_ERR;
        }
 
        switch (key) {
@@ -866,7 +852,7 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
                        return SR_ERR_CHANNEL_GROUP;
                }
                g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
-               for (i = 0; i < NUM_VDIV; i++) {
+               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);
@@ -916,19 +902,19 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
                }
                break;
        default:
-               sr_dbg("Tried to list unknown config key: %d.", key);
                return SR_ERR_NA;
        }
 
        return SR_OK;
 }
 
-static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_start(const struct sr_dev_inst *sdi)
 {
        struct sr_scpi_dev_inst *scpi;
        struct dev_context *devc;
        struct sr_channel *ch;
        struct sr_datafeed_packet packet;
+       gboolean some_digital;
        GSList *l;
 
        if (sdi->status != SR_ST_ACTIVE)
@@ -939,13 +925,14 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
 
        devc->num_frames = 0;
 
+       some_digital = FALSE;
        for (l = sdi->channels; l; l = l->next) {
                ch = l->data;
                sr_dbg("handling channel %s", ch->name);
                if (ch->type == SR_CHANNEL_ANALOG) {
                        if (ch->enabled)
-                               devc->enabled_analog_channels = g_slist_append(
-                                               devc->enabled_analog_channels, ch);
+                               devc->enabled_channels = g_slist_append(
+                                               devc->enabled_channels, ch);
                        if (ch->enabled != devc->analog_channels[ch->index]) {
                                /* Enabled channel is currently disabled, or vice versa. */
                                if (rigol_ds_config_set(sdi, ":CHAN%d:DISP %s", ch->index + 1,
@@ -954,19 +941,29 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
                                devc->analog_channels[ch->index] = ch->enabled;
                        }
                } else if (ch->type == SR_CHANNEL_LOGIC) {
+                       /* Only one list entry for DS1000D series. All channels are retrieved
+                        * together when this entry is processed. */
+                       if (ch->enabled && (
+                                               devc->model->series->protocol > PROTOCOL_V2 ||
+                                               !some_digital))
+                               devc->enabled_channels = g_slist_append(
+                                               devc->enabled_channels, ch);
                        if (ch->enabled) {
-                               devc->enabled_digital_channels = g_slist_append(
-                                               devc->enabled_digital_channels, ch);
+                               some_digital = TRUE;
                                /* Turn on LA module if currently off. */
                                if (!devc->la_enabled) {
-                                       if (rigol_ds_config_set(sdi, ":LA:DISP ON") != SR_OK)
+                                       if (rigol_ds_config_set(sdi,
+                                                       devc->model->series->protocol >= PROTOCOL_V4 ?
+                                                               ":LA:STAT ON" : ":LA:DISP ON") != SR_OK)
                                                return SR_ERR;
                                        devc->la_enabled = TRUE;
                                }
                        }
                        if (ch->enabled != devc->digital_channels[ch->index]) {
                                /* Enabled channel is currently disabled, or vice versa. */
-                               if (rigol_ds_config_set(sdi, ":DIG%d:TURN %s", ch->index,
+                               if (rigol_ds_config_set(sdi,
+                                               devc->model->series->protocol >= PROTOCOL_V4 ?
+                                                       ":LA:DIG%d:DISP %s" : ":DIG%d:TURN %s", ch->index,
                                                ch->enabled ? "ON" : "OFF") != SR_OK)
                                        return SR_ERR;
                                devc->digital_channels[ch->index] = ch->enabled;
@@ -974,12 +971,14 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
                }
        }
 
-       if (!devc->enabled_analog_channels && !devc->enabled_digital_channels)
+       if (!devc->enabled_channels)
                return SR_ERR;
 
        /* Turn off LA module if on and no digital channels selected. */
-       if (devc->la_enabled && !devc->enabled_digital_channels)
-               if (rigol_ds_config_set(sdi, ":LA:DISP OFF") != SR_OK)
+       if (devc->la_enabled && !some_digital)
+               if (rigol_ds_config_set(sdi,
+                               devc->model->series->protocol >= PROTOCOL_V4 ?
+                                       ":LA:STAT OFF" : ":LA:DISP OFF") != SR_OK)
                        return SR_ERR;
 
        /* Set memory mode. */
@@ -1019,31 +1018,24 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        sr_scpi_source_add(sdi->session, 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);
+       std_session_send_df_header(sdi, LOG_PREFIX);
 
-       if (devc->enabled_analog_channels)
-               devc->channel_entry = devc->enabled_analog_channels;
-       else
-               devc->channel_entry = devc->enabled_digital_channels;
+       devc->channel_entry = devc->enabled_channels;
 
        if (rigol_ds_capture_start(sdi) != SR_OK)
                return SR_ERR;
 
        /* Start of first frame. */
        packet.type = SR_DF_FRAME_BEGIN;
-       sr_session_send(cb_data, &packet);
+       sr_session_send(sdi, &packet);
 
        return SR_OK;
 }
 
-static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_stop(struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
        struct sr_scpi_dev_inst *scpi;
-       struct sr_datafeed_packet packet;
-
-       (void)cb_data;
 
        devc = sdi->priv;
 
@@ -1052,14 +1044,10 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
                return SR_ERR;
        }
 
-       /* End of last frame. */
-       packet.type = SR_DF_END;
-       sr_session_send(sdi, &packet);
+       std_session_send_df_end(sdi, LOG_PREFIX);
 
-       g_slist_free(devc->enabled_analog_channels);
-       g_slist_free(devc->enabled_digital_channels);
-       devc->enabled_analog_channels = NULL;
-       devc->enabled_digital_channels = NULL;
+       g_slist_free(devc->enabled_channels);
+       devc->enabled_channels = NULL;
        scpi = sdi->conn;
        sr_scpi_source_remove(sdi->session, scpi);
 
@@ -1070,10 +1058,10 @@ SR_PRIV struct sr_dev_driver rigol_ds_driver_info = {
        .name = "rigol-ds",
        .longname = "Rigol DS",
        .api_version = 1,
-       .init = init,
-       .cleanup = cleanup,
+       .init = std_init,
+       .cleanup = std_cleanup,
        .scan = scan,
-       .dev_list = dev_list,
+       .dev_list = std_dev_list,
        .dev_clear = dev_clear,
        .config_get = config_get,
        .config_set = config_set,
@@ -1082,5 +1070,5 @@ SR_PRIV struct sr_dev_driver rigol_ds_driver_info = {
        .dev_close = dev_close,
        .dev_acquisition_start = dev_acquisition_start,
        .dev_acquisition_stop = dev_acquisition_stop,
-       .priv = NULL,
+       .context = NULL,
 };