]> sigrok.org Git - libsigrok.git/blob - src/hardware/siglent-sds/api.c
siglent-sds: Random cosmetics, drop unused stuff.
[libsigrok.git] / src / hardware / siglent-sds / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2018 mhooijboer <marchelh@gmail.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <fcntl.h>
22 #include <glib.h>
23 #include <math.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <strings.h>
27 #include <unistd.h>
28 #include <libsigrok/libsigrok.h>
29 #include "libsigrok-internal.h"
30 #include "protocol.h"
31 #include "scpi.h"
32
33 static const uint32_t scanopts[] = {
34         SR_CONF_CONN,
35         SR_CONF_SERIALCOMM,
36 };
37
38 static const uint32_t devopts[] = {
39         SR_CONF_OSCILLOSCOPE,
40         SR_CONF_LOGIC_ANALYZER,
41         SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
42         SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43         SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
44         SR_CONF_TRIGGER_LEVEL | SR_CONF_GET | SR_CONF_SET,
45         SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
46         SR_CONF_NUM_HDIV | SR_CONF_GET | SR_CONF_LIST,
47         SR_CONF_SAMPLERATE | SR_CONF_GET,
48         SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
49         SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
50         SR_CONF_AVERAGING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
51         SR_CONF_AVG_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
52 };
53
54 static const uint32_t analog_devopts[] = {
55         SR_CONF_NUM_VDIV | SR_CONF_GET,
56         SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
57         SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
58         SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
59         SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
60         SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
61 };
62
63 static const uint64_t timebases[][2] = {
64         /* nanoseconds */
65         { 1, 1000000000 },
66         { 2, 1000000000 },
67         { 5, 1000000000 },
68         { 10, 1000000000 },
69         { 20, 1000000000 },
70         { 50, 1000000000 },
71         { 100, 1000000000 },
72         { 200, 1000000000 },
73         { 500, 1000000000 },
74         /* microseconds */
75         { 1, 1000000 },
76         { 2, 1000000 },
77         { 5, 1000000 },
78         { 10, 1000000 },
79         { 20, 1000000 },
80         { 50, 1000000 },
81         { 100, 1000000 },
82         { 200, 1000000 },
83         { 500, 1000000 },
84         /* milliseconds */
85         { 1, 1000 },
86         { 2, 1000 },
87         { 5, 1000 },
88         { 10, 1000 },
89         { 20, 1000 },
90         { 50, 1000 },
91         { 100, 1000 },
92         { 200, 1000 },
93         { 500, 1000 },
94         /* seconds */
95         { 1, 1 },
96         { 2, 1 },
97         { 5, 1 },
98         { 10, 1 },
99         { 20, 1 },
100         { 50, 1 },
101         { 100, 1 },
102 };
103
104 static const uint64_t vdivs[][2] = {
105         /* microvolts */
106         { 500, 100000 },
107         /* millivolts */
108         { 1, 1000 },
109         { 2, 1000 },
110         { 5, 1000 },
111         { 10, 1000 },
112         { 20, 1000 },
113         { 50, 1000 },
114         { 100, 1000 },
115         { 200, 1000 },
116         { 500, 1000 },
117         /* volts */
118         { 1, 1 },
119         { 2, 1 },
120         { 5, 1 },
121         { 10, 1 },
122         { 20, 1 },
123         { 50, 1 },
124         { 100, 1 },
125 };
126
127 static const char *trigger_sources[] = {
128         "CH1", "CH2", "Ext", "Ext /5", "AC Line",
129         "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
130         "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
131 };
132
133 static const char *trigger_slopes[] = {
134         "r", "f",
135 };
136
137 static const char *coupling[] = {
138         "A1M AC 1 Meg",
139         "A50 AC 50 Ohm",
140         "D1M DC 1 Meg",
141         "D50 DC 50 Ohm",
142         "GND",
143 };
144
145 static const uint64_t probe_factor[] = {
146         1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000,
147 };
148
149 /* Do not change the order of entries. */
150 static const char *data_sources[] = {
151         "Display",
152         "History",
153 };
154
155 enum vendor {
156         SIGLENT,
157 };
158
159 enum series {
160         SDS1000CML,
161         SDS1000CNL,
162         SDS1000DL,
163         SDS1000X,
164         SDS1000XP,
165         SDS1000XE,
166         SDS2000X,
167 };
168
169 /* short name, full name */
170 static const struct siglent_sds_vendor supported_vendors[] = {
171         [SIGLENT] = {"Siglent", "Siglent Technologies"},
172 };
173
174 #define VENDOR(x) &supported_vendors[x]
175 /* vendor, series, protocol, max timebase, min vdiv, number of horizontal divs,
176  * number of vertical divs, live waveform samples, memory buffer samples */
177 static const struct siglent_sds_series supported_series[] = {
178         [SDS1000CML] = {VENDOR(SIGLENT), "SDS1000CML", NON_SPO_MODEL,
179                 { 50, 1 }, { 2, 1000 }, 18, 8, 1400363},
180         [SDS1000CNL] = {VENDOR(SIGLENT), "SDS1000CNL", NON_SPO_MODEL,
181                 { 50, 1 }, { 2, 1000 }, 18, 8, 1400363},
182         [SDS1000DL] = {VENDOR(SIGLENT), "SDS1000DL", NON_SPO_MODEL,
183                 { 50, 1 }, { 2, 1000 }, 18, 8, 1400363},
184         [SDS1000X] = {VENDOR(SIGLENT), "SDS1000X", SPO_MODEL,
185                 { 50, 1 }, { 500, 100000 }, 14, 8, 14000363},
186         [SDS1000XP] = {VENDOR(SIGLENT), "SDS1000X+", SPO_MODEL,
187                 { 50, 1 }, { 500, 100000 }, 14, 8, 14000363},
188         [SDS1000XE] = {VENDOR(SIGLENT), "SDS1000XE", SPO_MODEL,
189                 { 50, 1 }, { 500, 100000 }, 14, 8, 14000363},
190         [SDS2000X] = {VENDOR(SIGLENT), "SDS2000X", SPO_MODEL,
191                 { 50, 1 }, { 500, 100000 }, 14, 8, 14000363},
192 };
193
194 #define SERIES(x) &supported_series[x]
195 /* series, model, min timebase, analog channels, digital */
196 static const struct siglent_sds_model supported_models[] = {
197         { SERIES(SDS1000CML), "SDS1152CML", { 20, 1000000000 }, 2, false, 0 },
198         { SERIES(SDS1000CML), "SDS1102CML", { 10, 1000000000 }, 2, false, 0 },
199         { SERIES(SDS1000CML), "SDS1072CML", { 5, 1000000000 }, 2, false, 0 },
200         { SERIES(SDS1000CNL), "SDS1202CNL", { 20, 1000000000 }, 2, false, 0 },
201         { SERIES(SDS1000CNL), "SDS1102CNL", { 10, 1000000000 }, 2, false, 0 },
202         { SERIES(SDS1000CNL), "SDS1072CNL", { 5, 1000000000 }, 2, false, 0 },
203         { SERIES(SDS1000DL), "SDS1202DL", { 20, 1000000000 }, 2, false, 0 },
204         { SERIES(SDS1000DL), "SDS1102DL", { 10, 1000000000 }, 2, false, 0 },
205         { SERIES(SDS1000DL), "SDS1022DL", { 5, 1000000000 }, 2, false, 0 },
206         { SERIES(SDS1000DL), "SDS1052DL", { 5, 1000000000 }, 2, false, 0 },
207         { SERIES(SDS1000DL), "SDS1052DL+", { 5, 1000000000 }, 2, false, 0 },
208         { SERIES(SDS1000X), "SDS1102X", { 2, 1000000000 }, 2, false, 0 },
209         { SERIES(SDS1000XP), "SDS1102X+", { 2, 1000000000 }, 2, false, 0 },
210         { SERIES(SDS1000X), "SDS1202X", { 2, 1000000000 }, 2, false, 0 },
211         { SERIES(SDS1000XP), "SDS1202X+", { 2, 1000000000 }, 2, false, 0 },
212         { SERIES(SDS1000XE), "SDS1202X-E", { 1, 1000000000 }, 2, false, 0 },
213         { SERIES(SDS1000XE), "SDS1104X-E", { 1, 1000000000 }, 4, true, 16 },
214         { SERIES(SDS1000XE), "SDS1204X-E", { 1, 1000000000 }, 4, true, 16 },
215         { SERIES(SDS2000X), "SDS2072X", { 2, 1000000000 }, 2, false, 0 },
216         { SERIES(SDS2000X), "SDS2074X", { 2, 1000000000 }, 4, false, 0 },
217         { SERIES(SDS2000X), "SDS2102X", { 2, 1000000000 }, 2, false, 0 },
218         { SERIES(SDS2000X), "SDS2104X", { 2, 1000000000 }, 4, false, 0 },
219         { SERIES(SDS2000X), "SDS2202X", { 2, 1000000000 }, 2, false, 0 },
220         { SERIES(SDS2000X), "SDS2204X", { 2, 1000000000 }, 4, false, 0 },
221         { SERIES(SDS2000X), "SDS2302X", { 2, 1000000000 }, 2, false, 0 },
222         { SERIES(SDS2000X), "SDS2304X", { 2, 1000000000 }, 4, false, 0 },
223 };
224
225 SR_PRIV struct sr_dev_driver siglent_sds_driver_info;
226
227 static void clear_helper(void *priv)
228 {
229         struct dev_context *devc;
230
231         devc = priv;
232         if (!devc)
233                 return;
234         g_free(devc->analog_groups);
235         g_free(devc->enabled_channels);
236 }
237
238 static int dev_clear(const struct sr_dev_driver *di)
239 {
240         return std_dev_clear_with_callback(di, clear_helper);
241 }
242
243 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
244 {
245         struct dev_context *devc;
246         struct sr_dev_inst *sdi;
247         struct sr_scpi_hw_info *hw_info;
248         struct sr_channel *ch;
249         unsigned int i;
250         const struct siglent_sds_model *model;
251         gchar *channel_name;
252
253         sr_dbg("Setting Communication Headers to off.");
254         if (sr_scpi_send(scpi, "CHDR OFF") != SR_OK)
255                 return NULL;
256
257         if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
258                 sr_info("Couldn't get IDN response, retrying.");
259                 sr_scpi_close(scpi);
260                 sr_scpi_open(scpi);
261                 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
262                         sr_info("Couldn't get IDN response.");
263                         return NULL;
264                 }
265         }
266
267         model = NULL;
268         for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
269                 if (!strcmp(hw_info->model, supported_models[i].name)) {
270                         model = &supported_models[i];
271                         break;
272                 }
273         }
274
275         if (!model) {
276                 sr_scpi_hw_info_free(hw_info);
277                 return NULL;
278         }
279
280         sdi = g_malloc0(sizeof(struct sr_dev_inst));
281         sdi->vendor = g_strdup(model->series->vendor->name);
282         sdi->model = g_strdup(model->name);
283         sdi->version = g_strdup(hw_info->firmware_version);
284         sdi->conn = scpi;
285         sdi->driver = &siglent_sds_driver_info;
286         sdi->inst_type = SR_INST_SCPI;
287         sdi->serial_num = g_strdup(hw_info->serial_number);
288         devc = g_malloc0(sizeof(struct dev_context));
289         devc->limit_frames = 1;
290         devc->model = model;
291
292         sr_scpi_hw_info_free(hw_info);
293
294         devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group *) *
295                 model->analog_channels);
296
297         for (i = 0; i < model->analog_channels; i++) {
298                 channel_name = g_strdup_printf("CH%d", i + 1);
299                 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_name);
300
301                 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
302
303                 devc->analog_groups[i]->name = channel_name;
304                 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
305                 sdi->channel_groups = g_slist_append(sdi->channel_groups,
306                         devc->analog_groups[i]);
307         }
308
309         if (devc->model->has_digital) {
310                 devc->digital_group = g_malloc0(sizeof(struct sr_channel_group));
311
312                 for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) {
313                         channel_name = g_strdup_printf("D%d", i);
314                         ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
315                         g_free(channel_name);
316                         devc->digital_group->channels = g_slist_append(
317                                 devc->digital_group->channels, ch);
318                 }
319                 devc->digital_group->name = g_strdup("LA");
320                 sdi->channel_groups = g_slist_append(sdi->channel_groups,
321                         devc->digital_group);
322         }
323
324         for (i = 0; i < ARRAY_SIZE(timebases); i++) {
325                 if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
326                         devc->timebases = &timebases[i];
327
328                 if (!memcmp(&devc->model->series->max_timebase, &timebases[i], sizeof(uint64_t[2])))
329                         devc->num_timebases = &timebases[i] - devc->timebases + 1;
330         }
331
332         for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
333                 devc->vdivs = &vdivs[i];
334                 if (!memcmp(&devc->model->series->min_vdiv,
335                         &vdivs[i], sizeof(uint64_t[2]))) {
336                         devc->vdivs = &vdivs[i];
337                         devc->num_vdivs = ARRAY_SIZE(vdivs) - i;
338                         break;
339                 }
340         }
341
342         devc->buffer = g_malloc(devc->model->series->buffer_samples);
343         sr_dbg("Setting device context buffer size: %i.", devc->model->series->buffer_samples);
344         devc->data = g_malloc(devc->model->series->buffer_samples * sizeof(float));
345
346         devc->data_source = DATA_SOURCE_SCREEN;
347
348         sdi->priv = devc;
349
350         return sdi;
351 }
352
353 static GSList *scan(struct sr_dev_driver *di, GSList *options)
354 {
355         /* TODO: Implement RPC call for LXI device discovery. */
356         return sr_scpi_scan(di->context, options, probe_device);
357 }
358
359 static int dev_open(struct sr_dev_inst *sdi)
360 {
361         int ret;
362         struct sr_scpi_dev_inst *scpi = sdi->conn;
363
364         if ((ret = sr_scpi_open(scpi)) < 0) {
365                 sr_err("Failed to open SCPI device: %s.", sr_strerror(ret));
366                 return SR_ERR;
367         }
368
369         if ((ret = siglent_sds_get_dev_cfg(sdi)) < 0) {
370                 sr_err("Failed to get device config: %s.", sr_strerror(ret));
371                 return SR_ERR;
372         }
373
374         sdi->status = SR_ST_ACTIVE;
375
376         return SR_OK;
377 }
378
379 static int dev_close(struct sr_dev_inst *sdi)
380 {
381         return sr_scpi_close(sdi->conn);
382 }
383
384 static int config_get(uint32_t key, GVariant **data,
385         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
386 {
387         struct dev_context *devc;
388         struct sr_channel *ch;
389         const char *tmp_str;
390         int analog_channel = -1;
391         float smallest_diff = INFINITY;
392         int idx = -1;
393         unsigned i;
394
395         if (!sdi)
396                 return SR_ERR_ARG;
397
398         devc = sdi->priv;
399
400         /* If a channel group is specified, it must be a valid one. */
401         if (cg && !g_slist_find(sdi->channel_groups, cg)) {
402                 sr_err("Invalid channel group specified.");
403                 return SR_ERR;
404         }
405
406         if (cg) {
407                 ch = g_slist_nth_data(cg->channels, 0);
408                 if (!ch)
409                         return SR_ERR;
410                 if (ch->type == SR_CHANNEL_ANALOG) {
411                         if (ch->name[2] < '1' || ch->name[2] > '4')
412                                 return SR_ERR;
413                         analog_channel = ch->name[2] - '1';
414                 }
415         }
416
417         switch (key) {
418         case SR_CONF_NUM_HDIV:
419                 *data = g_variant_new_int32(devc->model->series->num_horizontal_divs);
420                 break;
421         case SR_CONF_NUM_VDIV:
422                 *data = g_variant_new_int32(devc->num_vdivs);
423                 break;
424         case SR_CONF_LIMIT_FRAMES:
425                 *data = g_variant_new_uint64(devc->limit_frames);
426                 break;
427         case SR_CONF_DATA_SOURCE:
428                 if (devc->data_source == DATA_SOURCE_SCREEN)
429                         *data = g_variant_new_string("Screen");
430                 else if (devc->data_source == DATA_SOURCE_HISTORY)
431                         *data = g_variant_new_string("History");
432                 break;
433         case SR_CONF_SAMPLERATE:
434                 siglent_sds_get_dev_cfg_horizontal(sdi);
435                 *data = g_variant_new_uint64(devc->samplerate);
436                 break;
437         case SR_CONF_TRIGGER_SOURCE:
438                 if (!strcmp(devc->trigger_source, "ACL"))
439                         tmp_str = "AC Line";
440                 else if (!strcmp(devc->trigger_source, "CHAN1"))
441                         tmp_str = "CH1";
442                 else if (!strcmp(devc->trigger_source, "CHAN2"))
443                         tmp_str = "CH2";
444                 else
445                         tmp_str = devc->trigger_source;
446                 *data = g_variant_new_string(tmp_str);
447                 break;
448         case SR_CONF_TRIGGER_SLOPE:
449                 if (!strncmp(devc->trigger_slope, "POS", 3)) {
450                         tmp_str = "r";
451                 } else if (!strncmp(devc->trigger_slope, "NEG", 3)) {
452                         tmp_str = "f";
453                 } else {
454                         sr_dbg("Unknown trigger slope: '%s'.", devc->trigger_slope);
455                         return SR_ERR_NA;
456                 }
457                 *data = g_variant_new_string(tmp_str);
458                 break;
459         case SR_CONF_TRIGGER_LEVEL:
460                 *data = g_variant_new_double(devc->trigger_level);
461                 break;
462         case SR_CONF_HORIZ_TRIGGERPOS:
463                 *data = g_variant_new_double(devc->horiz_triggerpos);
464                 break;
465         case SR_CONF_TIMEBASE:
466                 for (i = 0; i < devc->num_timebases; i++) {
467                         float tb, diff;
468
469                         tb = (float)devc->timebases[i][0] / devc->timebases[i][1];
470                         diff = fabs(devc->timebase - tb);
471                         if (diff < smallest_diff) {
472                                 smallest_diff = diff;
473                                 idx = i;
474                         }
475                 }
476                 if (idx < 0) {
477                         sr_dbg("Negative timebase index: %d.", idx);
478                         return SR_ERR_NA;
479                 }
480                 *data = g_variant_new("(tt)", devc->timebases[idx][0],
481                         devc->timebases[idx][1]);
482                 break;
483         case SR_CONF_VDIV:
484                 if (analog_channel < 0) {
485                         sr_dbg("Negative analog channel: %d.", analog_channel);
486                         return SR_ERR_NA;
487                 }
488                 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
489                         float vdiv, diff;
490
491                         vdiv = (float)vdivs[i][0] / vdivs[i][1];
492                         diff = fabsf(devc->vdiv[analog_channel] - vdiv);
493                         if (diff < smallest_diff) {
494                                 smallest_diff = diff;
495                                 idx = i;
496                         }
497                 }
498                 if (idx < 0) {
499                         sr_dbg("Negative vdiv index: %d.", idx);
500                         return SR_ERR_NA;
501                 }
502                 *data = g_variant_new("(tt)", vdivs[idx][0], vdivs[idx][1]);
503                 break;
504         case SR_CONF_COUPLING:
505                 if (analog_channel < 0) {
506                         sr_dbg("Negative analog channel: %d.", analog_channel);
507                         return SR_ERR_NA;
508                 }
509                 *data = g_variant_new_string(devc->coupling[analog_channel]);
510                 break;
511         case SR_CONF_PROBE_FACTOR:
512                 if (analog_channel < 0) {
513                         sr_dbg("Negative analog channel: %d.", analog_channel);
514                         return SR_ERR_NA;
515                 }
516                 *data = g_variant_new_uint64(devc->attenuation[analog_channel]);
517                 break;
518         default:
519                 return SR_ERR_NA;
520         }
521
522         return SR_OK;
523 }
524
525 static int config_set(uint32_t key, GVariant *data,
526         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
527 {
528         struct dev_context *devc;
529         uint64_t p, q;
530         double t_dbl;
531         unsigned int i, j;
532         int ret, idx;
533         const char *tmp_str;
534         char buffer[16];
535
536         devc = sdi->priv;
537
538         if (sdi->status != SR_ST_ACTIVE)
539                 return SR_ERR_DEV_CLOSED;
540
541         /* If a channel group is specified, it must be a valid one. */
542         if (cg && !g_slist_find(sdi->channel_groups, cg)) {
543                 sr_err("Invalid channel group specified.");
544                 return SR_ERR;
545         }
546
547         ret = SR_OK;
548         switch (key) {
549         case SR_CONF_LIMIT_FRAMES:
550                 devc->limit_frames = g_variant_get_uint64(data);
551                 break;
552         case SR_CONF_TRIGGER_SLOPE:
553                 if ((idx = std_str_idx(data, ARRAY_AND_SIZE(trigger_slopes))) < 0)
554                         return SR_ERR_ARG;
555                 g_free(devc->trigger_slope);
556                 devc->trigger_slope = g_strdup((trigger_slopes[idx][0] == 'r') ? "POS" : "NEG");
557                 ret = siglent_sds_config_set(sdi, "%s:TRSL %s",
558                         devc->trigger_source, devc->trigger_slope);
559                 break;
560         case SR_CONF_HORIZ_TRIGGERPOS:
561                 t_dbl = g_variant_get_double(data);
562                 if (t_dbl < 0.0 || t_dbl > 1.0) {
563                         sr_err("Invalid horiz. trigger position: %g.", t_dbl);
564                         return SR_ERR;
565                 }
566                 devc->horiz_triggerpos = t_dbl;
567                 /* We have the trigger offset as a percentage of the frame, but
568                  * need to express this in seconds. */
569                 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
570                 g_ascii_formatd(buffer, sizeof(buffer), "%.6f", t_dbl);
571                 ret = siglent_sds_config_set(sdi, ":TIM:OFFS %s", buffer);
572                 break;
573         case SR_CONF_TRIGGER_LEVEL:
574                 t_dbl = g_variant_get_double(data);
575                 g_ascii_formatd(buffer, sizeof(buffer), "%.3f", t_dbl);
576                 ret = siglent_sds_config_set(sdi, ":TRIG:EDGE:LEV %s", buffer);
577                 if (ret == SR_OK)
578                         devc->trigger_level = t_dbl;
579                 break;
580         case SR_CONF_TIMEBASE:
581                 g_variant_get(data, "(tt)", &p, &q);
582                 for (i = 0; i < devc->num_timebases; i++) {
583                         char *cmd;
584                         if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
585                                 cmd = "";
586                                 devc->timebase = (float) p / q;
587                                 switch (q) {
588                                 case 1:
589                                         cmd = g_strdup_printf("%" PRIu64 "S", p);
590                                         break;
591                                 case 1000:
592                                         cmd = g_strdup_printf("%" PRIu64 "MS", p);
593                                         break;
594                                 case 1000000:
595                                         cmd = g_strdup_printf("%" PRIu64 "US", p);
596                                         break;
597                                 case 100000000:
598                                         cmd = g_strdup_printf("%" PRIu64 "NS", p);
599                                         break;
600                                 }
601                                 sr_dbg("Setting device timebase: TDIV %s.", cmd);
602                                 ret = siglent_sds_config_set(sdi, "TDIV %s", cmd);
603                                 break;
604                         }
605                 }
606                 if (i == devc->num_timebases) {
607                         sr_err("Invalid timebase index: %d.", i);
608                         ret = SR_ERR_ARG;
609                 }
610                 break;
611         case SR_CONF_TRIGGER_SOURCE:
612                 tmp_str = g_variant_get_string(data, NULL);
613                 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
614                         if (!strcmp(trigger_sources[i], tmp_str)) {
615                                 g_free(devc->trigger_source);
616                                 devc->trigger_source = g_strdup(trigger_sources[i]);
617                                 if (!strcmp(devc->trigger_source, "AC Line"))
618                                         tmp_str = "LINE";
619                                 else if (!strcmp(devc->trigger_source, "CH1"))
620                                         tmp_str = "C1";
621                                 else if (!strcmp(devc->trigger_source, "CH2"))
622                                         tmp_str = "C2";
623                                 else if (!strcmp(devc->trigger_source, "CH3"))
624                                         tmp_str = "C3";
625                                 else if (!strcmp(devc->trigger_source, "CH4"))
626                                         tmp_str = "C4";
627                                 else if (!strcmp(devc->trigger_source, "Ext"))
628                                         tmp_str = "EX";
629                                 else if (!strcmp(devc->trigger_source, "Ext /5"))
630                                         tmp_str = "EX5";
631                                 else
632                                         tmp_str = (char *) devc->trigger_source;
633                                 ret = siglent_sds_config_set(sdi, "TRSE EDGE,SR,%s,OFF", tmp_str);
634                                 break;
635                         }
636                 }
637                 if (i == ARRAY_SIZE(trigger_sources)) {
638                         sr_err("Invalid trigger source index: %d.", i);
639                         ret = SR_ERR_ARG;
640                 }
641                 break;
642         case SR_CONF_VDIV:
643                 if (!cg)
644                         return SR_ERR_CHANNEL_GROUP;
645                 g_variant_get(data, "(tt)", &p, &q);
646                 for (i = 0; i < devc->model->analog_channels; i++) {
647                         char *cmd;
648                         if (cg == devc->analog_groups[i]) {
649                                 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
650                                         if (vdivs[j][0] != p || vdivs[j][1] != q)
651                                                 continue;
652                                         cmd = "";
653                                         switch (q) {
654                                         case 1:
655                                                 cmd = g_strdup_printf("%" PRIu64 "V", p);
656                                                 break;
657                                         case 1000:
658                                                 cmd = g_strdup_printf("%" PRIu64 "MV", p);
659                                                 break;
660                                         case 100000:
661                                                 cmd = g_strdup_printf("%" PRIu64 "UV", p);
662                                                 break;
663                                         }
664                                         return siglent_sds_config_set(sdi, "C%d:VDIV %s", i + 1, cmd);
665                                 }
666                                 sr_err("Invalid vdiv index: %d.", j);
667                                 return SR_ERR_ARG;
668                         }
669                 }
670                 sr_dbg("Didn't set vdiv, unknown channel(group).");
671                 return SR_ERR_NA;
672         case SR_CONF_COUPLING:
673                 if (!cg)
674                         return SR_ERR_CHANNEL_GROUP;
675                 tmp_str = g_variant_get_string(data, NULL);
676                 for (i = 0; i < devc->model->analog_channels; i++) {
677                         char cmd[4];
678                         if (cg == devc->analog_groups[i]) {
679                                 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
680                                         if (!strcmp(tmp_str, coupling[j])) {
681                                                 g_free(devc->coupling[i]);
682                                                 devc->coupling[i] = g_strdup(coupling[j]);
683                                                 strncpy(cmd, devc->coupling[i], 3);
684                                                 cmd[3] = 0;
685                                                 return siglent_sds_config_set(sdi, "C%d:CPL %s", i + 1, cmd);
686                                         }
687                                 }
688                                 sr_err("Invalid coupling index: %d.", j);
689                                 return SR_ERR_ARG;
690                         }
691                 }
692                 sr_dbg("Didn't set coupling, unknown channel(group).");
693                 return SR_ERR_NA;
694         case SR_CONF_PROBE_FACTOR:
695                 if (!cg)
696                         return SR_ERR_CHANNEL_GROUP;
697                 p = g_variant_get_uint64(data);
698                 for (i = 0; i < devc->model->analog_channels; i++) {
699                         if (cg == devc->analog_groups[i]) {
700                                 for (j = 0; j < ARRAY_SIZE(probe_factor); j++) {
701                                         if (p == probe_factor[j]) {
702                                                 devc->attenuation[i] = p;
703                                                 ret = siglent_sds_config_set(sdi, "C%d:ATTN %" PRIu64, i + 1, p);
704                                                 if (ret == SR_OK)
705                                                         siglent_sds_get_dev_cfg_vertical(sdi);
706                                                 return ret;
707                                         }
708                                 }
709                                 sr_err("Invalid probe factor: %" PRIu64 ".", p);
710                                 return SR_ERR_ARG;
711                         }
712                 }
713                 sr_dbg("Didn't set probe factor, unknown channel(group).");
714                 return SR_ERR_NA;
715         case SR_CONF_DATA_SOURCE:
716                 tmp_str = g_variant_get_string(data, NULL);
717                 if (!strcmp(tmp_str, "Display"))
718                         devc->data_source = DATA_SOURCE_SCREEN;
719                 else if (devc->model->series->protocol >= SPO_MODEL
720                         && !strcmp(tmp_str, "History"))
721                         devc->data_source = DATA_SOURCE_HISTORY;
722                 else {
723                         sr_err("Unknown data source: '%s'.", tmp_str);
724                         return SR_ERR;
725                 }
726                 break;
727         case SR_CONF_SAMPLERATE:
728                 siglent_sds_get_dev_cfg_horizontal(sdi);
729                 data = g_variant_new_uint64(devc->samplerate);
730                 break;
731         default:
732                 return SR_ERR_NA;
733         }
734
735         return ret;
736 }
737
738 static int config_list(uint32_t key, GVariant **data,
739         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
740 {
741         GVariant *tuple, *rational[2];
742         GVariantBuilder gvb;
743         unsigned int i;
744         struct dev_context *devc = NULL;
745
746         if (key == SR_CONF_SCAN_OPTIONS) {
747                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
748                         scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
749                 return SR_OK;
750         } else if (key == SR_CONF_DEVICE_OPTIONS && !cg) {
751                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
752                         devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
753                 return SR_OK;
754         }
755
756         /* Every other option requires a valid device instance. */
757         if (!sdi)
758                 return SR_ERR_ARG;
759         devc = sdi->priv;
760
761         /* If a channel group is specified, it must be a valid one. */
762         if (cg && !g_slist_find(sdi->channel_groups, cg)) {
763                 sr_err("Invalid channel group specified.");
764                 return SR_ERR;
765         }
766
767         switch (key) {
768         case SR_CONF_DEVICE_OPTIONS:
769                 if (!cg)
770                         return SR_ERR_CHANNEL_GROUP;
771                 if (cg == devc->digital_group) {
772                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
773                                 NULL, 0, sizeof(uint32_t));
774                         return SR_OK;
775                 } else {
776                         for (i = 0; i < devc->model->analog_channels; i++) {
777                                 if (cg == devc->analog_groups[i]) {
778                                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
779                                                 analog_devopts, ARRAY_SIZE(analog_devopts), sizeof(uint32_t));
780                                         return SR_OK;
781                                 }
782                         }
783                         return SR_ERR_NA;
784                 }
785                 break;
786         case SR_CONF_COUPLING:
787                 if (!cg)
788                         return SR_ERR_CHANNEL_GROUP;
789                 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
790                 break;
791         case SR_CONF_PROBE_FACTOR:
792                 if (!cg)
793                         return SR_ERR_CHANNEL_GROUP;
794                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
795                         probe_factor, ARRAY_SIZE(probe_factor), sizeof(uint64_t));
796                 break;
797         case SR_CONF_VDIV:
798                 if (!devc)
799                         /* Can't know this until we have the exact model. */
800                         return SR_ERR_ARG;
801                 if (!cg)
802                         return SR_ERR_CHANNEL_GROUP;
803                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
804                 for (i = 0; i < devc->num_vdivs; i++) {
805                         rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
806                         rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
807                         tuple = g_variant_new_tuple(rational, 2);
808                         g_variant_builder_add_value(&gvb, tuple);
809                 }
810                 *data = g_variant_builder_end(&gvb);
811                 break;
812         case SR_CONF_TIMEBASE:
813                 if (!devc)
814                         /* Can't know this until we have the exact model. */
815                         return SR_ERR_ARG;
816                 if (devc->num_timebases <= 0)
817                         return SR_ERR_NA;
818                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
819                 for (i = 0; i < devc->num_timebases; i++) {
820                         rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
821                         rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
822                         tuple = g_variant_new_tuple(rational, 2);
823                         g_variant_builder_add_value(&gvb, tuple);
824                 }
825                 *data = g_variant_builder_end(&gvb);
826                 break;
827         case SR_CONF_TRIGGER_SOURCE:
828                 if (!devc)
829                         /* Can't know this until we have the exact model. */
830                         return SR_ERR_ARG;
831                 *data = g_variant_new_strv(trigger_sources,
832                         devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 5);
833                 break;
834         case SR_CONF_TRIGGER_SLOPE:
835                 *data = g_variant_new_strv(trigger_slopes, ARRAY_SIZE(trigger_slopes));
836                 break;
837         case SR_CONF_DATA_SOURCE:
838                 if (!devc)
839                         /* Can't know this until we have the exact model. */
840                         return SR_ERR_ARG;
841                 switch (devc->model->series->protocol) {
842                 /* TODO: Check what must be done here for the data source buffer sizes. */
843                 case NON_SPO_MODEL:
844                         *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
845                         break;
846                 case SPO_MODEL:
847                         *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
848                         break;
849                 }
850                 break;
851         case SR_CONF_NUM_HDIV:
852                 *data = g_variant_new_int32(devc->model->series->num_horizontal_divs);
853                 break;
854         case SR_CONF_AVERAGING:
855                 /* TODO: Implement averaging. */
856                 break;
857         default:
858                 return SR_ERR_NA;
859         }
860
861         return SR_OK;
862 }
863
864 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
865 {
866         struct sr_scpi_dev_inst *scpi;
867         struct dev_context *devc;
868         struct sr_channel *ch;
869         struct sr_datafeed_packet packet;
870         gboolean some_digital;
871         GSList *l, *d;
872
873         if (sdi->status != SR_ST_ACTIVE)
874                 return SR_ERR_DEV_CLOSED;
875
876         scpi = sdi->conn;
877         devc = sdi->priv;
878
879         devc->num_frames = 0;
880         some_digital = FALSE;
881
882         /*
883          * Check if there are any logic channels enabled, if so then enable
884          * the MSO, otherwise skip the digital channel setup. Enable and
885          * disable channels on the device is very slow and it is faster when
886          * checked in a small loop without the actual actions.
887          */
888         for (d = sdi->channels; d; d = d->next) {
889                 ch = d->data;
890                 if (ch->type == SR_CHANNEL_LOGIC && ch->enabled)
891                         some_digital = TRUE;
892         }
893
894         for (l = sdi->channels; l; l = l->next) {
895                 ch = l->data;
896                 if (ch->type == SR_CHANNEL_ANALOG) {
897                         if (ch->enabled)
898                                 devc->enabled_channels = g_slist_append(
899                                         devc->enabled_channels, ch);
900                         if (ch->enabled != devc->analog_channels[ch->index]) {
901                                 /* Enabled channel is currently disabled, or vice versa. */
902                                 if (siglent_sds_config_set(sdi, "C%d:TRA %s", ch->index + 1,
903                                         ch->enabled ? "ON" : "OFF") != SR_OK)
904                                         return SR_ERR;
905                                 devc->analog_channels[ch->index] = ch->enabled;
906                         }
907                 } else if (ch->type == SR_CHANNEL_LOGIC && some_digital) {
908                         if (ch->enabled) {
909                                 /* Turn on LA module if currently off and digital channels are enabled. */
910                                 if (!devc->la_enabled) {
911                                         if (siglent_sds_config_set(sdi, "DGST ON") != SR_OK)
912                                                 return SR_ERR;
913                                         g_usleep(630000);
914                                         devc->la_enabled = TRUE;
915                                 }
916                                 devc->enabled_channels = g_slist_append(
917                                         devc->enabled_channels, ch);
918                         }
919                         /* Enabled channel is currently disabled, or vice versa. */
920                         if (siglent_sds_config_set(sdi, "D%d:DGCH %s", ch->index,
921                                 ch->enabled ? "ON" : "OFF") != SR_OK)
922                                 return SR_ERR;
923                         /* Slowing the command sequence down to let the device handle it. */
924                         g_usleep(630000);
925                         devc->digital_channels[ch->index] = ch->enabled;
926                 }
927         }
928         if (!devc->enabled_channels)
929                 return SR_ERR;
930         /* Turn off LA module if on and no digital channels selected. */
931         if (devc->la_enabled && !some_digital)
932                 if (siglent_sds_config_set(sdi, "DGST OFF") != SR_OK) {
933                         devc->la_enabled = FALSE;
934                         g_usleep(500000);
935                         return SR_ERR;
936                 }
937
938         // devc->analog_frame_size = devc->model->series->buffer_samples;
939         // devc->digital_frame_size = devc->model->series->buffer_samples;
940
941         switch (devc->model->series->protocol) {
942         case SPO_MODEL:
943                 if (siglent_sds_config_set(sdi, "WFSU SP,0,TYPE,1") != SR_OK)
944                         return SR_ERR;
945                 if (siglent_sds_config_set(sdi, "ACQW SAMPLING") != SR_OK)
946                         return SR_ERR;
947                 break;
948         case NON_SPO_MODEL:
949                 /* TODO: Implement CML/CNL/DL models. */
950                 if (siglent_sds_config_set(sdi, "WFSU SP,0,TYPE,1") != SR_OK)
951                         return SR_ERR;
952                 if (siglent_sds_config_set(sdi, "ACQW SAMPLING") != SR_OK)
953                         return SR_ERR;
954                 break;
955         default:
956                 break;
957         }
958
959         sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
960                 siglent_sds_receive, (void *) sdi);
961
962         std_session_send_df_header(sdi);
963
964         devc->channel_entry = devc->enabled_channels;
965
966         if (siglent_sds_capture_start(sdi) != SR_OK)
967                 return SR_ERR;
968
969         /* Start of first frame. */
970         packet.type = SR_DF_FRAME_BEGIN;
971         sr_session_send(sdi, &packet);
972
973         return SR_OK;
974 }
975
976 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
977 {
978         struct dev_context *devc;
979         struct sr_scpi_dev_inst *scpi;
980
981         devc = sdi->priv;
982
983         if (sdi->status != SR_ST_ACTIVE) {
984                 sr_err("Device inactive, can't stop acquisition.");
985                 return SR_ERR;
986         }
987
988         std_session_send_df_end(sdi);
989
990         g_slist_free(devc->enabled_channels);
991         devc->enabled_channels = NULL;
992         scpi = sdi->conn;
993         sr_scpi_source_remove(sdi->session, scpi);
994
995         return SR_OK;
996 }
997
998 SR_PRIV struct sr_dev_driver siglent_sds_driver_info = {
999         .name = "siglent-sds",
1000         .longname = "Siglent SDS1000/SDS2000",
1001         .api_version = 1,
1002         .init = std_init,
1003         .cleanup = std_cleanup,
1004         .scan = scan,
1005         .dev_list = std_dev_list,
1006         .dev_clear = dev_clear,
1007         .config_get = config_get,
1008         .config_set = config_set,
1009         .config_list = config_list,
1010         .dev_open = dev_open,
1011         .dev_close = dev_close,
1012         .dev_acquisition_start = dev_acquisition_start,
1013         .dev_acquisition_stop = dev_acquisition_stop,
1014         .context = NULL,
1015 };
1016
1017 SR_REGISTER_DEV_DRIVER(siglent_sds_driver_info);