]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/siglent-sds/api.c
siglent-sds: move model dependent request emission to after identification
[libsigrok.git] / src / hardware / siglent-sds / api.c
index 01430234face1501f86b8edfc099afaf9492748a..846399b678c4a8a3af56864e99ffbf27173b9d87 100644 (file)
@@ -18,7 +18,6 @@
  */
 
 #include <config.h>
-
 #include <fcntl.h>
 #include <glib.h>
 #include <math.h>
@@ -26,7 +25,6 @@
 #include <string.h>
 #include <strings.h>
 #include <unistd.h>
-
 #include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
 #include "protocol.h"
 
 static const uint32_t scanopts[] = {
        SR_CONF_CONN,
-       SR_CONF_SERIALCOMM
+       SR_CONF_SERIALCOMM,
 };
 
-static const uint32_t devopts[] = {
+static const uint32_t drvopts[] = {
        SR_CONF_OSCILLOSCOPE,
        SR_CONF_LOGIC_ANALYZER,
+};
+
+static const uint32_t devopts[] = {
        SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
        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,
@@ -49,11 +50,11 @@ static const uint32_t devopts[] = {
        SR_CONF_SAMPLERATE | SR_CONF_GET,
        SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
        SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
-       SR_CONF_AVERAGING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
+       SR_CONF_AVERAGING | SR_CONF_GET | SR_CONF_SET,
        SR_CONF_AVG_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
 };
 
-static const uint32_t analog_devopts[] = {
+static const uint32_t devopts_cg_analog[] = {
        SR_CONF_NUM_VDIV | SR_CONF_GET,
        SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
        SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
@@ -100,6 +101,7 @@ static const uint64_t timebases[][2] = {
        { 10, 1 },
        { 20, 1 },
        { 50, 1 },
+       { 100, 1 },
 };
 
 static const uint64_t vdivs[][2] = {
@@ -125,36 +127,14 @@ static const uint64_t vdivs[][2] = {
        { 100, 1 },
 };
 
-#define NUM_TIMEBASE   ARRAY_SIZE(timebases)
-#define NUM_VDIV       ARRAY_SIZE(vdivs)
-
 static const char *trigger_sources[] = {
-       "CH1",
-       "CH2",
-       "Ext",
-       "Ext /5",
-       "AC Line",
-       "D0",
-       "D1",
-       "D2",
-       "D3",
-       "D4",
-       "D5",
-       "D6",
-       "D7",
-       "D8",
-       "D9",
-       "D10",
-       "D11",
-       "D12",
-       "D13",
-       "D14",
-       "D15",
+       "CH1", "CH2", "Ext", "Ext /5", "AC Line",
+       "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
+       "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
 };
 
 static const char *trigger_slopes[] = {
-       "Rising",
-       "Falling",
+       "r", "f",
 };
 
 static const char *coupling[] = {
@@ -166,22 +146,14 @@ static const char *coupling[] = {
 };
 
 static const uint64_t probe_factor[] = {
-       1,
-       2,
-       5,
-       10,
-       20,
-       50,
-       100,
-       200,
-       500,
-       1000,
-       2000,
-       5000,
-       10000,
+       1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000,
 };
 
-/* Do not change the order of entries */
+static const uint64_t averages[] = {
+       4, 16, 32, 64, 128, 256, 512, 1024,
+};
+
+/* Do not change the order of entries. */
 static const char *data_sources[] = {
        "Display",
        "History",
@@ -201,110 +173,63 @@ enum series {
        SDS2000X,
 };
 
-/* short name, full name, USB Name */
+/* short name, full name */
 static const struct siglent_sds_vendor supported_vendors[] = {
-       [SIGLENT] = {
-               "Siglent", "Siglent Technologies", "Siglent Technologies Co,. Ltd.",
-       },
+       [SIGLENT] = {"Siglent", "Siglent Technologies"},
 };
 
 #define VENDOR(x) &supported_vendors[x]
 /* vendor, series, protocol, max timebase, min vdiv, number of horizontal divs,
  * number of vertical divs, live waveform samples, memory buffer samples */
 static const struct siglent_sds_series supported_series[] = {
-       [SDS1000CML] = {
-               VENDOR(SIGLENT), "SDS1000CML", NON_SPO_MODEL,
-               { 50, 1 },
-               { 2, 1000 }, 18, 8, 1400363,
-       },
-       [SDS1000CNL] = {
-               VENDOR(SIGLENT), "SDS1000CNL", NON_SPO_MODEL,
-               { 50, 1 },
-               { 2, 1000 }, 18, 8, 1400363,
-       },
-       [SDS1000DL] = {
-               VENDOR(SIGLENT), "SDS1000DL", NON_SPO_MODEL,
-               { 50, 1 },
-               { 2, 1000 }, 18, 8, 1400363,
-       },
-       [SDS1000X] = {
-               VENDOR(SIGLENT), "SDS1000X", SPO_MODEL,
-               { 50, 1 },
-               { 500, 100000 }, 14, 8, 14000363,
-       },
-       [SDS1000XP] = {
-               VENDOR(SIGLENT), "SDS1000X+", SPO_MODEL,
-               { 50, 1 },
-               { 500, 100000 }, 14, 8, 14000363,
-       },
-       [SDS1000XE] = {
-               VENDOR(SIGLENT), "SDS1000XE", SPO_MODEL,
-               { 50, 1 },
-               { 500, 100000 }, 14, 8, 14000363,
-       },
-       [SDS2000X] = {
-               VENDOR(SIGLENT), "SDS2000X", SPO_MODEL,
-               { 50, 1 },
-               { 500, 100000 }, 14, 8, 14000363,
-       },
+       [SDS1000CML] = {VENDOR(SIGLENT), "SDS1000CML", NON_SPO_MODEL,
+               { 50, 1 }, { 2, 1000 }, 18, 8, 1400363},
+       [SDS1000CNL] = {VENDOR(SIGLENT), "SDS1000CNL", NON_SPO_MODEL,
+               { 50, 1 }, { 2, 1000 }, 18, 8, 1400363},
+       [SDS1000DL] = {VENDOR(SIGLENT), "SDS1000DL", NON_SPO_MODEL,
+               { 50, 1 }, { 2, 1000 }, 18, 8, 1400363},
+       [SDS1000X] = {VENDOR(SIGLENT), "SDS1000X", SPO_MODEL,
+               { 50, 1 }, { 500, 100000 }, 14, 8, 14000363},
+       [SDS1000XP] = {VENDOR(SIGLENT), "SDS1000X+", SPO_MODEL,
+               { 50, 1 }, { 500, 100000 }, 14, 8, 14000363},
+       [SDS1000XE] = {VENDOR(SIGLENT), "SDS1000XE", ESERIES,
+               { 50, 1 }, { 500, 100000 }, 14, 8, 14000363},
+       [SDS2000X] = {VENDOR(SIGLENT), "SDS2000X", SPO_MODEL,
+               { 50, 1 }, { 500, 100000 }, 14, 8, 14000363},
 };
 
 #define SERIES(x) &supported_series[x]
 /* series, model, min timebase, analog channels, digital */
 static const struct siglent_sds_model supported_models[] = {
-       { SERIES(SDS1000CML), "SDS1152CML",
-               { 20, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS1000CML), "SDS1102CML",
-               { 10, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS1000CML), "SDS1072CML",
-               { 5, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS1000CNL), "SDS1202CNL",
-               { 20, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS1000CNL), "SDS1102CNL",
-               { 10, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS1000CNL), "SDS1072CNL",
-               { 5, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS1000DL), "SDS1202DL",
-               { 20, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS1000DL), "SDS1102DL",
-               { 10, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS1000DL), "SDS1022DL",
-               { 5, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS1000DL), "SDS1052DL",
-               { 5, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS1000X), "SDS1102X",
-               { 2, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS1000XP), "SDS1102X+",
-               { 2, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS1000X), "SDS1202X",
-               { 2, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS1000XP), "SDS1202X+",
-               { 2, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS1000XE), "SDS1202X-E",
-               { 1, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS1000XE), "SDS1104X-E",
-               { 1, 1000000000 }, 4, true, 16 },
-       { SERIES(SDS1000XE), "SDS1204X-E",
-               { 1, 1000000000 }, 4, true, 16 },
-       { SERIES(SDS2000X), "SDS2072X",
-               { 2, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS2000X), "SDS2074X",
-               { 2, 1000000000 }, 4, false, 0 },
-       { SERIES(SDS2000X), "SDS2102X",
-               { 2, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS2000X), "SDS2104X",
-               { 2, 1000000000 }, 4, false, 0 },
-       { SERIES(SDS2000X), "SDS2202X",
-               { 2, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS2000X), "SDS2204X",
-               { 2, 1000000000 }, 4, false, 0 },
-       { SERIES(SDS2000X), "SDS2302X",
-               { 2, 1000000000 }, 2, false, 0 },
-       { SERIES(SDS2000X), "SDS2304X",
-               { 2, 1000000000 }, 4, false, 0 },
+       { SERIES(SDS1000CML), "SDS1152CML", { 20, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS1000CML), "SDS1102CML", { 10, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS1000CML), "SDS1072CML", { 5, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS1000CNL), "SDS1202CNL", { 20, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS1000CNL), "SDS1102CNL", { 10, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS1000CNL), "SDS1072CNL", { 5, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS1000DL), "SDS1202DL", { 20, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS1000DL), "SDS1102DL", { 10, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS1000DL), "SDS1022DL", { 5, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS1000DL), "SDS1052DL", { 5, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS1000DL), "SDS1052DL+", { 5, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS1000X), "SDS1102X", { 2, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS1000XP), "SDS1102X+", { 2, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS1000X), "SDS1202X", { 2, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS1000XP), "SDS1202X+", { 2, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS1000XE), "SDS1202X-E", { 1, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS1000XE), "SDS1104X-E", { 1, 1000000000 }, 4, TRUE, 16 },
+       { SERIES(SDS1000XE), "SDS1204X-E", { 1, 1000000000 }, 4, TRUE, 16 },
+       { SERIES(SDS2000X), "SDS2072X", { 2, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS2000X), "SDS2074X", { 2, 1000000000 }, 4, FALSE, 0 },
+       { SERIES(SDS2000X), "SDS2102X", { 2, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS2000X), "SDS2104X", { 2, 1000000000 }, 4, FALSE, 0 },
+       { SERIES(SDS2000X), "SDS2202X", { 2, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS2000X), "SDS2204X", { 2, 1000000000 }, 4, FALSE, 0 },
+       { SERIES(SDS2000X), "SDS2302X", { 2, 1000000000 }, 2, FALSE, 0 },
+       { SERIES(SDS2000X), "SDS2304X", { 2, 1000000000 }, 4, FALSE, 0 },
 };
 
-SR_PRIV struct sr_dev_driver siglent_sds_driver_info;
+static struct sr_dev_driver siglent_sds_driver_info;
 
 static void clear_helper(void *priv)
 {
@@ -319,7 +244,6 @@ static void clear_helper(void *priv)
 
 static int dev_clear(const struct sr_dev_driver *di)
 {
-
        return std_dev_clear_with_callback(di, clear_helper);
 }
 
@@ -333,13 +257,6 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
        const struct siglent_sds_model *model;
        gchar *channel_name;
 
-       sr_info("Device probing decode...");
-
-       /* Setting communication Header Format to OFF*/
-       sr_dbg("Setting Communication Headers to off");
-       if (sr_scpi_send(scpi, "CHDR OFF") != SR_OK)
-               return NULL;
-
        if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
                sr_info("Couldn't get IDN response, retrying.");
                sr_scpi_close(scpi);
@@ -358,30 +275,26 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
                }
        }
 
-       sr_info("Decoded Manufacturer: %s", hw_info->manufacturer);
-       sr_info("Decoded Model: %s", hw_info->model);
-
        if (!model) {
                sr_scpi_hw_info_free(hw_info);
                return NULL;
        }
 
+       sr_dbg("Setting Communication Headers to off.");
+       if (sr_scpi_send(scpi, "CHDR OFF") != SR_OK)
+               return NULL;
+
        sdi = g_malloc0(sizeof(struct sr_dev_inst));
-       sr_dbg("Setting Device Instance Vendor: %s", model->series->vendor->name);
        sdi->vendor = g_strdup(model->series->vendor->name);
-       sr_dbg("Setting Device Instance model: %s", model->name);
        sdi->model = g_strdup(model->name);
-       sr_dbg("Setting Device Instance version: %s", hw_info->firmware_version);
        sdi->version = g_strdup(hw_info->firmware_version);
        sdi->conn = scpi;
        sdi->driver = &siglent_sds_driver_info;
-       sr_dbg("Setting Device Instance inst_type: SCPI");
        sdi->inst_type = SR_INST_SCPI;
        sdi->serial_num = g_strdup(hw_info->serial_number);
        devc = g_malloc0(sizeof(struct dev_context));
        devc->limit_frames = 1;
        devc->model = model;
-       sr_dbg("Setting device Context model: %s", devc->model->name);
 
        sr_scpi_hw_info_free(hw_info);
 
@@ -415,7 +328,7 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
                        devc->digital_group);
        }
 
-       for (i = 0; i < NUM_TIMEBASE; i++) {
+       for (i = 0; i < ARRAY_SIZE(timebases); i++) {
                if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
                        devc->timebases = &timebases[i];
 
@@ -423,18 +336,18 @@ 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++) {
+       for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
                devc->vdivs = &vdivs[i];
                if (!memcmp(&devc->model->series->min_vdiv,
                        &vdivs[i], sizeof(uint64_t[2]))) {
                        devc->vdivs = &vdivs[i];
-                       devc->num_vdivs = NUM_VDIV - i;
+                       devc->num_vdivs = ARRAY_SIZE(vdivs) - i;
                        break;
                }
        }
 
        devc->buffer = g_malloc(devc->model->series->buffer_samples);
-       sr_dbg("Setting device Context buffer Size: %i", devc->model->series->buffer_samples);
+       sr_dbg("Setting device context buffer size: %i.", devc->model->series->buffer_samples);
        devc->data = g_malloc(devc->model->series->buffer_samples * sizeof(float));
 
        devc->data_source = DATA_SOURCE_SCREEN;
@@ -446,8 +359,7 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
 
 static GSList *scan(struct sr_dev_driver *di, GSList *options)
 {
-
-       // TODO implement RPC call for LXI device discovery.
+       /* TODO: Implement RPC call for LXI device discovery. */
        return sr_scpi_scan(di->context, options, probe_device);
 }
 
@@ -466,18 +378,14 @@ static int dev_open(struct sr_dev_inst *sdi)
                return SR_ERR;
        }
 
-       sdi->status = SR_ST_ACTIVE;
-
        return SR_OK;
 }
 
 static int dev_close(struct sr_dev_inst *sdi)
 {
-
        return sr_scpi_close(sdi->conn);
 }
 
-
 static int config_get(uint32_t key, GVariant **data,
        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
@@ -529,8 +437,7 @@ static int config_get(uint32_t key, GVariant **data,
                break;
        case SR_CONF_SAMPLERATE:
                siglent_sds_get_dev_cfg_horizontal(sdi);
-               *data = g_variant_new_uint64(devc->sampleRate);
-               sr_dbg("Sample rate set to %f", devc->sampleRate);
+               *data = g_variant_new_uint64(devc->samplerate);
                break;
        case SR_CONF_TRIGGER_SOURCE:
                if (!strcmp(devc->trigger_source, "ACL"))
@@ -584,10 +491,8 @@ static int config_get(uint32_t key, GVariant **data,
                        return SR_ERR_NA;
                }
                for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
-                       float vdiv, diff;
-
-                       vdiv = (float)vdivs[i][0] / vdivs[i][1];
-                       diff = fabsf(devc->vdiv[analog_channel] - vdiv);
+                       float vdiv = (float)vdivs[i][0] / vdivs[i][1];
+                       float diff = fabsf(devc->vdiv[analog_channel] - vdiv);
                        if (diff < smallest_diff) {
                                smallest_diff = diff;
                                idx = i;
@@ -613,6 +518,12 @@ static int config_get(uint32_t key, GVariant **data,
                }
                *data = g_variant_new_uint64(devc->attenuation[analog_channel]);
                break;
+       case SR_CONF_AVERAGING:
+               *data = g_variant_new_boolean(devc->average_enabled);
+               break;
+       case SR_CONF_AVG_SAMPLES:
+               *data = g_variant_new_uint64(devc->average_samples);
+               break;
        default:
                return SR_ERR_NA;
        }
@@ -624,18 +535,17 @@ static int config_set(uint32_t key, GVariant *data,
        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
-       uint64_t p, q;
+       uint64_t p;
        double t_dbl;
-       unsigned int i, j;
-       int ret;
+       int i;
+       int ret, idx;
        const char *tmp_str;
        char buffer[16];
+       char *cmd = "";
+       char cmd4[4];
 
        devc = sdi->priv;
 
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
-
        /* 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.");
@@ -648,10 +558,12 @@ static int config_set(uint32_t key, GVariant *data,
                devc->limit_frames = g_variant_get_uint64(data);
                break;
        case SR_CONF_TRIGGER_SLOPE:
-               tmp_str = g_variant_get_string(data, NULL);
+               if ((idx = std_str_idx(data, ARRAY_AND_SIZE(trigger_slopes))) < 0)
+                       return SR_ERR_ARG;
                g_free(devc->trigger_slope);
-               ret = siglent_sds_config_set(sdi, "%s:TRSL %s", devc->trigger_source, devc->trigger_slope);
-               break;
+               devc->trigger_slope = g_strdup((trigger_slopes[idx][0] == 'r') ? "POS" : "NEG");
+               return siglent_sds_config_set(sdi, "%s:TRSL %s",
+                       devc->trigger_source, devc->trigger_slope);
        case SR_CONF_HORIZ_TRIGGERPOS:
                t_dbl = g_variant_get_double(data);
                if (t_dbl < 0.0 || t_dbl > 1.0) {
@@ -663,8 +575,7 @@ static int config_set(uint32_t key, GVariant *data,
                 * need to express this in seconds. */
                t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
                g_ascii_formatd(buffer, sizeof(buffer), "%.6f", t_dbl);
-               ret = siglent_sds_config_set(sdi, ":TIM:OFFS %s", buffer);
-               break;
+               return siglent_sds_config_set(sdi, ":TIM:OFFS %s", buffer);
        case SR_CONF_TRIGGER_LEVEL:
                t_dbl = g_variant_get_double(data);
                g_ascii_formatd(buffer, sizeof(buffer), "%.3f", t_dbl);
@@ -673,150 +584,97 @@ static int config_set(uint32_t key, GVariant *data,
                        devc->trigger_level = t_dbl;
                break;
        case SR_CONF_TIMEBASE:
-               sr_dbg("Setting device Timebase");
-               g_variant_get(data, "(tt)", &p, &q);
-               for (i = 0; i < devc->num_timebases; i++) {
-                       char *cmd;
-                       if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
-                               cmd = "";
-                               devc->timebase = (float) p / q;
-                               switch (q) {
-                               case 1:
-                                       cmd = g_strdup_printf("%luS", p);
-                                       break;
-                               case 1000:
-                                       cmd = g_strdup_printf("%luMS", p);
-                                       break;
-                               case 1000000:
-                                       cmd = g_strdup_printf("%luUS", p);
-                                       break;
-                               case 100000000:
-                                       cmd = g_strdup_printf("%luNS", p);
-                                       break;
-                               }
-                               sr_dbg("Setting device Timebase: TDIV %s", cmd);
-                               ret = siglent_sds_config_set(sdi, "TDIV %s", cmd);
-                               break;
-                       }
-               }
-               if (i == devc->num_timebases) {
-                       sr_err("Invalid timebase index: %d.", i);
-                       ret = SR_ERR_ARG;
+               if ((idx = std_u64_tuple_idx(data, devc->timebases, devc->num_timebases)) < 0)
+                       return SR_ERR_ARG;
+               devc->timebase = (float)devc->timebases[idx][0] / devc->timebases[idx][1];
+               p = devc->timebases[idx][0];
+               switch (devc->timebases[idx][1]) {
+               case 1:
+                       cmd = g_strdup_printf("%" PRIu64 "S", p);
+                       break;
+               case 1000:
+                       cmd = g_strdup_printf("%" PRIu64 "MS", p);
+                       break;
+               case 1000000:
+                       cmd = g_strdup_printf("%" PRIu64 "US", p);
+                       break;
+               case 1000000000:
+                       cmd = g_strdup_printf("%" PRIu64 "NS", p);
+                       break;
                }
-               break;
+               ret = siglent_sds_config_set(sdi, "TDIV %s", cmd);
+               g_free(cmd);
+               return ret;
        case SR_CONF_TRIGGER_SOURCE:
-               tmp_str = g_variant_get_string(data, NULL);
-               for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
-                       if (!strcmp(trigger_sources[i], tmp_str)) {
-                               g_free(devc->trigger_source);
-                               devc->trigger_source = g_strdup(trigger_sources[i]);
-                               if (!strcmp(devc->trigger_source, "AC Line"))
-                                       tmp_str = "LINE";
-                               else if (!strcmp(devc->trigger_source, "CH1"))
-                                       tmp_str = "C1";
-                               else if (!strcmp(devc->trigger_source, "CH2"))
-                                       tmp_str = "C2";
-                               else if (!strcmp(devc->trigger_source, "CH3"))
-                                       tmp_str = "C3";
-                               else if (!strcmp(devc->trigger_source, "CH4"))
-                                       tmp_str = "C4";
-                               else if (!strcmp(devc->trigger_source, "Ext"))
-                                       tmp_str = "EX";
-                               else if (!strcmp(devc->trigger_source, "Ext /5"))
-                                       tmp_str = "EX5";
-                               else
-                                       tmp_str = (char *) devc->trigger_source;
-                               ret = siglent_sds_config_set(sdi, "TRSE EDGE,SR,%s,OFF", tmp_str);
-                               break;
-                       }
-               }
-               if (i == ARRAY_SIZE(trigger_sources)) {
-                       sr_err("Invalid trigger source index: %d.", i);
-                       ret = SR_ERR_ARG;
-               }
-               break;
+               if ((idx = std_str_idx(data, ARRAY_AND_SIZE(trigger_sources))) < 0)
+                       return SR_ERR_ARG;
+               g_free(devc->trigger_source);
+               devc->trigger_source = g_strdup(trigger_sources[idx]);
+               if (!strcmp(devc->trigger_source, "AC Line"))
+                       tmp_str = "LINE";
+               else if (!strcmp(devc->trigger_source, "CH1"))
+                       tmp_str = "C1";
+               else if (!strcmp(devc->trigger_source, "CH2"))
+                       tmp_str = "C2";
+               else if (!strcmp(devc->trigger_source, "CH3"))
+                       tmp_str = "C3";
+               else if (!strcmp(devc->trigger_source, "CH4"))
+                       tmp_str = "C4";
+               else if (!strcmp(devc->trigger_source, "Ext"))
+                       tmp_str = "EX";
+               else if (!strcmp(devc->trigger_source, "Ext /5"))
+                       tmp_str = "EX5";
+               else
+                       tmp_str = (char *)devc->trigger_source;
+               return siglent_sds_config_set(sdi, "TRSE EDGE,SR,%s,OFF", tmp_str);
        case SR_CONF_VDIV:
-               if (!cg) {
-                       sr_err("No channel group specified.");
+               if (!cg)
                        return SR_ERR_CHANNEL_GROUP;
+               if ((i = std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels)) < 0)
+                       return SR_ERR_ARG;
+               if ((idx = std_u64_tuple_idx(data, ARRAY_AND_SIZE(vdivs))) < 0)
+                       return SR_ERR_ARG;
+               devc->vdiv[i] = (float)vdivs[idx][0] / vdivs[idx][1];
+               p = vdivs[idx][0];
+               switch (vdivs[idx][1]) {
+               case 1:
+                       cmd = g_strdup_printf("%" PRIu64 "V", p);
+                       break;
+               case 1000:
+                       cmd = g_strdup_printf("%" PRIu64 "MV", p);
+                       break;
+               case 100000:
+                       cmd = g_strdup_printf("%" PRIu64 "UV", p);
+                       break;
                }
-               g_variant_get(data, "(tt)", &p, &q);
-               for (i = 0; i < devc->model->analog_channels; i++) {
-                       char *cmd;
-                       if (cg == devc->analog_groups[i]) {
-                               for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
-                                       if (vdivs[j][0] != p || vdivs[j][1] != q)
-                                               continue;
-                                       cmd = "";
-                                       switch (q) {
-                                       case 1:
-                                               cmd = g_strdup_printf("%luV", p);
-                                               break;
-                                       case 1000:
-                                               cmd = g_strdup_printf("%luMV", p);
-                                               break;
-                                       case 100000:
-                                               cmd = g_strdup_printf("%luUV", p);
-                                               break;
-                                       }
-                                       return siglent_sds_config_set(sdi, "C%d:VDIV %s", i + 1,
-                                               cmd);
-                               }
-                               sr_err("Invalid vdiv index: %d.", j);
-                               return SR_ERR_ARG;
-                       }
-               }
-               sr_dbg("Didn't set vdiv, unknown channel(group).");
-               return SR_ERR_NA;
+               ret = siglent_sds_config_set(sdi, "C%d:VDIV %s", i + 1, cmd);
+               g_free(cmd);
+               return ret;
        case SR_CONF_COUPLING:
-               if (!cg) {
-                       sr_err("No channel group specified.");
+               if (!cg)
                        return SR_ERR_CHANNEL_GROUP;
-               }
-               tmp_str = g_variant_get_string(data, NULL);
-               for (i = 0; i < devc->model->analog_channels; i++) {
-                       char cmd[4];
-                       if (cg == devc->analog_groups[i]) {
-                               for (j = 0; j < ARRAY_SIZE(coupling); j++) {
-                                       if (!strcmp(tmp_str, coupling[j])) {
-                                               g_free(devc->coupling[i]);
-                                               devc->coupling[i] = g_strdup(coupling[j]);
-                                               strncpy(cmd, devc->coupling[i], 3);
-                                               cmd[3] = 0;
-                                               return siglent_sds_config_set(sdi, "C%d:CPL %s", i + 1,
-                                                       cmd);
-                                       }
-                               }
-                               sr_err("Invalid coupling index: %d.", j);
-                               return SR_ERR_ARG;
-                       }
-               }
-               sr_dbg("Didn't set coupling, unknown channel(group).");
-               return SR_ERR_NA;
+               if ((i = std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels)) < 0)
+                       return SR_ERR_ARG;
+               if ((idx = std_str_idx(data, ARRAY_AND_SIZE(coupling))) < 0)
+                       return SR_ERR_ARG;
+               g_free(devc->coupling[i]);
+               devc->coupling[i] = g_strdup(coupling[idx]);
+               strncpy(cmd4, devc->coupling[i], 3);
+               cmd4[3] = 0;
+               return siglent_sds_config_set(sdi, "C%d:CPL %s", i + 1, cmd4);
        case SR_CONF_PROBE_FACTOR:
-               if (!cg) {
-                       sr_err("No channel group specified.");
+               if (!cg)
                        return SR_ERR_CHANNEL_GROUP;
-               }
+               if ((i = std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels)) < 0)
+                       return SR_ERR_ARG;
+               if ((idx = std_u64_idx(data, ARRAY_AND_SIZE(probe_factor))) < 0)
+                       return SR_ERR_ARG;
                p = g_variant_get_uint64(data);
-               for (i = 0; i < devc->model->analog_channels; i++) {
-                       if (cg == devc->analog_groups[i]) {
-                               for (j = 0; j < ARRAY_SIZE(probe_factor); j++) {
-                                       if (p == probe_factor[j]) {
-                                               devc->attenuation[i] = p;
-                                               ret = siglent_sds_config_set(sdi, "C%d:ATTN %"PRIu64,
-                                                       i + 1, p);
-                                               if (ret == SR_OK)
-                                                       siglent_sds_get_dev_cfg_vertical(sdi);
-                                               return ret;
-                                       }
-                               }
-                               sr_err("Invalid probe factor: %"PRIu64".", p);
-                               return SR_ERR_ARG;
-                       }
-               }
-               sr_dbg("Didn't set probe factor, unknown channel(group).");
-               return SR_ERR_NA;
+               devc->attenuation[i] = probe_factor[idx];
+               ret = siglent_sds_config_set(sdi, "C%d:ATTN %" PRIu64, i + 1, p);
+               if (ret == SR_OK)
+                       siglent_sds_get_dev_cfg_vertical(sdi);
+               return ret;
        case SR_CONF_DATA_SOURCE:
                tmp_str = g_variant_get_string(data, NULL);
                if (!strcmp(tmp_str, "Display"))
@@ -831,8 +689,16 @@ static int config_set(uint32_t key, GVariant *data,
                break;
        case SR_CONF_SAMPLERATE:
                siglent_sds_get_dev_cfg_horizontal(sdi);
-               data = g_variant_new_uint64(devc->sampleRate);
+               data = g_variant_new_uint64(devc->samplerate);
+               break;
+       case SR_CONF_AVERAGING:
+               devc->average_enabled = g_variant_get_boolean(data);
+               sr_dbg("%s averaging", devc->average_enabled ? "Enabling" : "Disabling");
                break;
+       case SR_CONF_AVG_SAMPLES:
+               devc->average_samples = g_variant_get_uint64(data);
+               sr_dbg("Setting averaging rate to %" PRIu64, devc->average_samples);
+               break;  
        default:
                return SR_ERR_NA;
        }
@@ -843,84 +709,44 @@ static int config_set(uint32_t key, GVariant *data,
 static int config_list(uint32_t key, GVariant **data,
        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
-       GVariant *tuple, *rational[2];
-       GVariantBuilder gvb;
-       unsigned int i;
-       struct dev_context *devc = NULL;
-
-       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) {
-               *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)
-               return SR_ERR_ARG;
-       devc = sdi->priv;
+       struct dev_context *devc;
 
-       /* 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.");
-               return SR_ERR;
-       }
+       devc = (sdi) ? sdi->priv : NULL;
 
        switch (key) {
+       case SR_CONF_SCAN_OPTIONS:
        case SR_CONF_DEVICE_OPTIONS:
-               if (!cg) {
-                       sr_err("No channel group specified.");
-                       return SR_ERR_CHANNEL_GROUP;
-               }
+               if (!cg)
+                       return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
+               if (!devc)
+                       return SR_ERR_ARG;
                if (cg == devc->digital_group) {
-                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                               NULL, 0, sizeof(uint32_t));
+                       *data = std_gvar_array_u32(NULL, 0);
                        return SR_OK;
                } else {
-                       for (i = 0; i < devc->model->analog_channels; i++) {
-                               if (cg == devc->analog_groups[i]) {
-                                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                                               analog_devopts, ARRAY_SIZE(analog_devopts), sizeof(uint32_t));
-                                       return SR_OK;
-                               }
-                       }
-                       return SR_ERR_NA;
+                       if (std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels) < 0)
+                               return SR_ERR_ARG;
+                       *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog));
+                       return SR_OK;
                }
                break;
        case SR_CONF_COUPLING:
-               if (!cg) {
-                       sr_err("No channel group specified.");
+               if (!cg)
                        return SR_ERR_CHANNEL_GROUP;
-               }
-               *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
+               *data = g_variant_new_strv(ARRAY_AND_SIZE(coupling));
                break;
        case SR_CONF_PROBE_FACTOR:
-               if (!cg) {
-                       sr_err("No channel group specified.");
+               if (!cg)
                        return SR_ERR_CHANNEL_GROUP;
-               }
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
-                       probe_factor, ARRAY_SIZE(probe_factor), sizeof(uint64_t));
+               *data = std_gvar_array_u64(ARRAY_AND_SIZE(probe_factor));
                break;
        case SR_CONF_VDIV:
                if (!devc)
                        /* Can't know this until we have the exact model. */
                        return SR_ERR_ARG;
-               if (!cg) {
-                       sr_err("No channel group specified.");
+               if (!cg)
                        return SR_ERR_CHANNEL_GROUP;
-               }
-               g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
-               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);
+               *data = std_gvar_tuple_array(devc->vdivs, devc->num_vdivs);
                break;
        case SR_CONF_TIMEBASE:
                if (!devc)
@@ -928,14 +754,7 @@ static int config_list(uint32_t key, GVariant **data,
                        return SR_ERR_ARG;
                if (devc->num_timebases <= 0)
                        return SR_ERR_NA;
-               g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
-               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);
+               *data = std_gvar_tuple_array(devc->timebases, devc->num_timebases);
                break;
        case SR_CONF_TRIGGER_SOURCE:
                if (!devc)
@@ -945,27 +764,28 @@ static int config_list(uint32_t key, GVariant **data,
                        devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 5);
                break;
        case SR_CONF_TRIGGER_SLOPE:
-               *data = g_variant_new_strv(trigger_slopes, ARRAY_SIZE(trigger_slopes));
+               *data = g_variant_new_strv(ARRAY_AND_SIZE(trigger_slopes));
                break;
        case SR_CONF_DATA_SOURCE:
                if (!devc)
                        /* Can't know this until we have the exact model. */
                        return SR_ERR_ARG;
                switch (devc->model->series->protocol) {
-               // TODO check what must be done here for the data source buffer sizes
+               /* TODO: Check what must be done here for the data source buffer sizes. */
                case NON_SPO_MODEL:
                        *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
                        break;
                case SPO_MODEL:
-                       *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
+               case ESERIES:
+                       *data = g_variant_new_strv(ARRAY_AND_SIZE(data_sources));
                        break;
                }
                break;
        case SR_CONF_NUM_HDIV:
                *data = g_variant_new_int32(devc->model->series->num_horizontal_divs);
                break;
-       case SR_CONF_AVERAGING:
-               //TODO implement averaging.
+       case SR_CONF_AVG_SAMPLES:
+               *data = std_gvar_array_u64(ARRAY_AND_SIZE(averages));
                break;
        default:
                return SR_ERR_NA;
@@ -979,13 +799,8 @@ 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;
-       GSList *d;
-
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
+       GSList *l, *d;
 
        scpi = sdi->conn;
        devc = sdi->priv;
@@ -993,18 +808,20 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
        devc->num_frames = 0;
        some_digital = FALSE;
 
-       /* Check if there are any logic channels enabled, if so then enable de MSO, otherwise skip the digital channel setup */
-       /* enable and disable channels on the device is very slow and it is faster when checked in a small loop without the actual actions */
+       /*
+        * Check if there are any logic channels enabled, if so then enable
+        * the MSO, otherwise skip the digital channel setup. Enable and
+        * disable channels on the device is very slow and it is faster when
+        * checked in a small loop without the actual actions.
+        */
        for (d = sdi->channels; d; d = d->next) {
                ch = d->data;
-               if (ch->type == SR_CHANNEL_LOGIC && ch->enabled) {
+               if (ch->type == SR_CHANNEL_LOGIC && ch->enabled)
                        some_digital = TRUE;
-               }
        }
 
        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_channels = g_slist_append(
@@ -1020,20 +837,17 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
                        if (ch->enabled) {
                                /* Turn on LA module if currently off and digital channels are enabled. */
                                if (!devc->la_enabled) {
-                                       if (siglent_sds_config_set(sdi, "DGST ON") != SR_OK)
+                                       if (siglent_sds_config_set(sdi, "DI:SW?") != SR_OK)
                                                return SR_ERR;
-                                       g_usleep(630000);
                                        devc->la_enabled = TRUE;
                                }
                                devc->enabled_channels = g_slist_append(
                                        devc->enabled_channels, ch);
                        }
                        /* Enabled channel is currently disabled, or vice versa. */
-                       if (siglent_sds_config_set(sdi, "D%d:DGCH %s", ch->index,
+                       if (siglent_sds_config_set(sdi, "D%d:TRA %s", ch->index,
                                ch->enabled ? "ON" : "OFF") != SR_OK)
                                return SR_ERR;
-                       /* Slowing the command sequence down to let the device handle it */
-                       g_usleep(630000);
                        devc->digital_channels[ch->index] = ch->enabled;
                }
        }
@@ -1050,15 +864,21 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
        // devc->analog_frame_size = devc->model->series->buffer_samples;
        // devc->digital_frame_size = devc->model->series->buffer_samples;
 
+       siglent_sds_get_dev_cfg_horizontal(sdi);
        switch (devc->model->series->protocol) {
        case SPO_MODEL:
                if (siglent_sds_config_set(sdi, "WFSU SP,0,TYPE,1") != SR_OK)
                        return SR_ERR;
-               if (siglent_sds_config_set(sdi, "ACQW SAMPLING") != SR_OK)
-                       return SR_ERR;
+               if (devc->average_enabled) {
+                       if (siglent_sds_config_set(sdi, "ACQW AVERAGE,%i", devc->average_samples) != SR_OK)
+                               return SR_ERR;
+               } else {
+                       if (siglent_sds_config_set(sdi, "ACQW SAMPLING") != SR_OK)
+                               return SR_ERR;
+               }
                break;
        case NON_SPO_MODEL:
-               //TODO implement CML/CNL/DL models
+               /* TODO: Implement CML/CNL/DL models. */
                if (siglent_sds_config_set(sdi, "WFSU SP,0,TYPE,1") != SR_OK)
                        return SR_ERR;
                if (siglent_sds_config_set(sdi, "ACQW SAMPLING") != SR_OK)
@@ -1068,7 +888,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
                break;
        }
 
-       sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
+       sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 7000,
                siglent_sds_receive, (void *) sdi);
 
        std_session_send_df_header(sdi);
@@ -1079,8 +899,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
                return SR_ERR;
 
        /* Start of first frame. */
-       packet.type = SR_DF_FRAME_BEGIN;
-       sr_session_send(sdi, &packet);
+       std_session_send_df_frame_begin(sdi);
 
        return SR_OK;
 }
@@ -1092,11 +911,6 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi)
 
        devc = sdi->priv;
 
-       if (sdi->status != SR_ST_ACTIVE) {
-               sr_err("Device inactive, can't stop acquisition.");
-               return SR_ERR;
-       }
-
        std_session_send_df_end(sdi);
 
        g_slist_free(devc->enabled_channels);
@@ -1107,9 +921,9 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-SR_PRIV struct sr_dev_driver siglent_sds_driver_info = {
+static struct sr_dev_driver siglent_sds_driver_info = {
        .name = "siglent-sds",
-       .longname = "Siglent SDS1000/SDS2000 Series",
+       .longname = "Siglent SDS1000/SDS2000",
        .api_version = 1,
        .init = std_init,
        .cleanup = std_cleanup,
@@ -1125,5 +939,4 @@ SR_PRIV struct sr_dev_driver siglent_sds_driver_info = {
        .dev_acquisition_stop = dev_acquisition_stop,
        .context = NULL,
 };
-
 SR_REGISTER_DEV_DRIVER(siglent_sds_driver_info);