]> sigrok.org Git - libsigrok.git/blob - src/hardware/siglent-sds/api.c
a1b1ed71d4c490aed37dfa145275d92f59830b22
[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         return SR_OK;
375 }
376
377 static int dev_close(struct sr_dev_inst *sdi)
378 {
379         return sr_scpi_close(sdi->conn);
380 }
381
382 static int config_get(uint32_t key, GVariant **data,
383         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
384 {
385         struct dev_context *devc;
386         struct sr_channel *ch;
387         const char *tmp_str;
388         int analog_channel = -1;
389         float smallest_diff = INFINITY;
390         int idx = -1;
391         unsigned i;
392
393         if (!sdi)
394                 return SR_ERR_ARG;
395
396         devc = sdi->priv;
397
398         /* If a channel group is specified, it must be a valid one. */
399         if (cg && !g_slist_find(sdi->channel_groups, cg)) {
400                 sr_err("Invalid channel group specified.");
401                 return SR_ERR;
402         }
403
404         if (cg) {
405                 ch = g_slist_nth_data(cg->channels, 0);
406                 if (!ch)
407                         return SR_ERR;
408                 if (ch->type == SR_CHANNEL_ANALOG) {
409                         if (ch->name[2] < '1' || ch->name[2] > '4')
410                                 return SR_ERR;
411                         analog_channel = ch->name[2] - '1';
412                 }
413         }
414
415         switch (key) {
416         case SR_CONF_NUM_HDIV:
417                 *data = g_variant_new_int32(devc->model->series->num_horizontal_divs);
418                 break;
419         case SR_CONF_NUM_VDIV:
420                 *data = g_variant_new_int32(devc->num_vdivs);
421                 break;
422         case SR_CONF_LIMIT_FRAMES:
423                 *data = g_variant_new_uint64(devc->limit_frames);
424                 break;
425         case SR_CONF_DATA_SOURCE:
426                 if (devc->data_source == DATA_SOURCE_SCREEN)
427                         *data = g_variant_new_string("Screen");
428                 else if (devc->data_source == DATA_SOURCE_HISTORY)
429                         *data = g_variant_new_string("History");
430                 break;
431         case SR_CONF_SAMPLERATE:
432                 siglent_sds_get_dev_cfg_horizontal(sdi);
433                 *data = g_variant_new_uint64(devc->samplerate);
434                 break;
435         case SR_CONF_TRIGGER_SOURCE:
436                 if (!strcmp(devc->trigger_source, "ACL"))
437                         tmp_str = "AC Line";
438                 else if (!strcmp(devc->trigger_source, "CHAN1"))
439                         tmp_str = "CH1";
440                 else if (!strcmp(devc->trigger_source, "CHAN2"))
441                         tmp_str = "CH2";
442                 else
443                         tmp_str = devc->trigger_source;
444                 *data = g_variant_new_string(tmp_str);
445                 break;
446         case SR_CONF_TRIGGER_SLOPE:
447                 if (!strncmp(devc->trigger_slope, "POS", 3)) {
448                         tmp_str = "r";
449                 } else if (!strncmp(devc->trigger_slope, "NEG", 3)) {
450                         tmp_str = "f";
451                 } else {
452                         sr_dbg("Unknown trigger slope: '%s'.", devc->trigger_slope);
453                         return SR_ERR_NA;
454                 }
455                 *data = g_variant_new_string(tmp_str);
456                 break;
457         case SR_CONF_TRIGGER_LEVEL:
458                 *data = g_variant_new_double(devc->trigger_level);
459                 break;
460         case SR_CONF_HORIZ_TRIGGERPOS:
461                 *data = g_variant_new_double(devc->horiz_triggerpos);
462                 break;
463         case SR_CONF_TIMEBASE:
464                 for (i = 0; i < devc->num_timebases; i++) {
465                         float tb, diff;
466
467                         tb = (float)devc->timebases[i][0] / devc->timebases[i][1];
468                         diff = fabs(devc->timebase - tb);
469                         if (diff < smallest_diff) {
470                                 smallest_diff = diff;
471                                 idx = i;
472                         }
473                 }
474                 if (idx < 0) {
475                         sr_dbg("Negative timebase index: %d.", idx);
476                         return SR_ERR_NA;
477                 }
478                 *data = g_variant_new("(tt)", devc->timebases[idx][0],
479                         devc->timebases[idx][1]);
480                 break;
481         case SR_CONF_VDIV:
482                 if (analog_channel < 0) {
483                         sr_dbg("Negative analog channel: %d.", analog_channel);
484                         return SR_ERR_NA;
485                 }
486                 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
487                         float vdiv, diff;
488
489                         vdiv = (float)vdivs[i][0] / vdivs[i][1];
490                         diff = fabsf(devc->vdiv[analog_channel] - vdiv);
491                         if (diff < smallest_diff) {
492                                 smallest_diff = diff;
493                                 idx = i;
494                         }
495                 }
496                 if (idx < 0) {
497                         sr_dbg("Negative vdiv index: %d.", idx);
498                         return SR_ERR_NA;
499                 }
500                 *data = g_variant_new("(tt)", vdivs[idx][0], vdivs[idx][1]);
501                 break;
502         case SR_CONF_COUPLING:
503                 if (analog_channel < 0) {
504                         sr_dbg("Negative analog channel: %d.", analog_channel);
505                         return SR_ERR_NA;
506                 }
507                 *data = g_variant_new_string(devc->coupling[analog_channel]);
508                 break;
509         case SR_CONF_PROBE_FACTOR:
510                 if (analog_channel < 0) {
511                         sr_dbg("Negative analog channel: %d.", analog_channel);
512                         return SR_ERR_NA;
513                 }
514                 *data = g_variant_new_uint64(devc->attenuation[analog_channel]);
515                 break;
516         default:
517                 return SR_ERR_NA;
518         }
519
520         return SR_OK;
521 }
522
523 static int config_set(uint32_t key, GVariant *data,
524         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
525 {
526         struct dev_context *devc;
527         uint64_t p, q;
528         double t_dbl;
529         unsigned int i, j;
530         int ret, idx;
531         const char *tmp_str;
532         char buffer[16];
533
534         devc = sdi->priv;
535
536         /* If a channel group is specified, it must be a valid one. */
537         if (cg && !g_slist_find(sdi->channel_groups, cg)) {
538                 sr_err("Invalid channel group specified.");
539                 return SR_ERR;
540         }
541
542         ret = SR_OK;
543         switch (key) {
544         case SR_CONF_LIMIT_FRAMES:
545                 devc->limit_frames = g_variant_get_uint64(data);
546                 break;
547         case SR_CONF_TRIGGER_SLOPE:
548                 if ((idx = std_str_idx(data, ARRAY_AND_SIZE(trigger_slopes))) < 0)
549                         return SR_ERR_ARG;
550                 g_free(devc->trigger_slope);
551                 devc->trigger_slope = g_strdup((trigger_slopes[idx][0] == 'r') ? "POS" : "NEG");
552                 ret = siglent_sds_config_set(sdi, "%s:TRSL %s",
553                         devc->trigger_source, devc->trigger_slope);
554                 break;
555         case SR_CONF_HORIZ_TRIGGERPOS:
556                 t_dbl = g_variant_get_double(data);
557                 if (t_dbl < 0.0 || t_dbl > 1.0) {
558                         sr_err("Invalid horiz. trigger position: %g.", t_dbl);
559                         return SR_ERR;
560                 }
561                 devc->horiz_triggerpos = t_dbl;
562                 /* We have the trigger offset as a percentage of the frame, but
563                  * need to express this in seconds. */
564                 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
565                 g_ascii_formatd(buffer, sizeof(buffer), "%.6f", t_dbl);
566                 ret = siglent_sds_config_set(sdi, ":TIM:OFFS %s", buffer);
567                 break;
568         case SR_CONF_TRIGGER_LEVEL:
569                 t_dbl = g_variant_get_double(data);
570                 g_ascii_formatd(buffer, sizeof(buffer), "%.3f", t_dbl);
571                 ret = siglent_sds_config_set(sdi, ":TRIG:EDGE:LEV %s", buffer);
572                 if (ret == SR_OK)
573                         devc->trigger_level = t_dbl;
574                 break;
575         case SR_CONF_TIMEBASE:
576                 g_variant_get(data, "(tt)", &p, &q);
577                 for (i = 0; i < devc->num_timebases; i++) {
578                         char *cmd;
579                         if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
580                                 cmd = "";
581                                 devc->timebase = (float) p / q;
582                                 switch (q) {
583                                 case 1:
584                                         cmd = g_strdup_printf("%" PRIu64 "S", p);
585                                         break;
586                                 case 1000:
587                                         cmd = g_strdup_printf("%" PRIu64 "MS", p);
588                                         break;
589                                 case 1000000:
590                                         cmd = g_strdup_printf("%" PRIu64 "US", p);
591                                         break;
592                                 case 100000000:
593                                         cmd = g_strdup_printf("%" PRIu64 "NS", p);
594                                         break;
595                                 }
596                                 sr_dbg("Setting device timebase: TDIV %s.", cmd);
597                                 ret = siglent_sds_config_set(sdi, "TDIV %s", cmd);
598                                 break;
599                         }
600                 }
601                 if (i == devc->num_timebases) {
602                         sr_err("Invalid timebase index: %d.", i);
603                         ret = SR_ERR_ARG;
604                 }
605                 break;
606         case SR_CONF_TRIGGER_SOURCE:
607                 tmp_str = g_variant_get_string(data, NULL);
608                 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
609                         if (!strcmp(trigger_sources[i], tmp_str)) {
610                                 g_free(devc->trigger_source);
611                                 devc->trigger_source = g_strdup(trigger_sources[i]);
612                                 if (!strcmp(devc->trigger_source, "AC Line"))
613                                         tmp_str = "LINE";
614                                 else if (!strcmp(devc->trigger_source, "CH1"))
615                                         tmp_str = "C1";
616                                 else if (!strcmp(devc->trigger_source, "CH2"))
617                                         tmp_str = "C2";
618                                 else if (!strcmp(devc->trigger_source, "CH3"))
619                                         tmp_str = "C3";
620                                 else if (!strcmp(devc->trigger_source, "CH4"))
621                                         tmp_str = "C4";
622                                 else if (!strcmp(devc->trigger_source, "Ext"))
623                                         tmp_str = "EX";
624                                 else if (!strcmp(devc->trigger_source, "Ext /5"))
625                                         tmp_str = "EX5";
626                                 else
627                                         tmp_str = (char *) devc->trigger_source;
628                                 ret = siglent_sds_config_set(sdi, "TRSE EDGE,SR,%s,OFF", tmp_str);
629                                 break;
630                         }
631                 }
632                 if (i == ARRAY_SIZE(trigger_sources)) {
633                         sr_err("Invalid trigger source index: %d.", i);
634                         ret = SR_ERR_ARG;
635                 }
636                 break;
637         case SR_CONF_VDIV:
638                 if (!cg)
639                         return SR_ERR_CHANNEL_GROUP;
640                 g_variant_get(data, "(tt)", &p, &q);
641                 for (i = 0; i < devc->model->analog_channels; i++) {
642                         char *cmd;
643                         if (cg == devc->analog_groups[i]) {
644                                 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
645                                         if (vdivs[j][0] != p || vdivs[j][1] != q)
646                                                 continue;
647                                         cmd = "";
648                                         switch (q) {
649                                         case 1:
650                                                 cmd = g_strdup_printf("%" PRIu64 "V", p);
651                                                 break;
652                                         case 1000:
653                                                 cmd = g_strdup_printf("%" PRIu64 "MV", p);
654                                                 break;
655                                         case 100000:
656                                                 cmd = g_strdup_printf("%" PRIu64 "UV", p);
657                                                 break;
658                                         }
659                                         return siglent_sds_config_set(sdi, "C%d:VDIV %s", i + 1, cmd);
660                                 }
661                                 sr_err("Invalid vdiv index: %d.", j);
662                                 return SR_ERR_ARG;
663                         }
664                 }
665                 sr_dbg("Didn't set vdiv, unknown channel(group).");
666                 return SR_ERR_NA;
667         case SR_CONF_COUPLING:
668                 if (!cg)
669                         return SR_ERR_CHANNEL_GROUP;
670                 tmp_str = g_variant_get_string(data, NULL);
671                 for (i = 0; i < devc->model->analog_channels; i++) {
672                         char cmd[4];
673                         if (cg == devc->analog_groups[i]) {
674                                 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
675                                         if (!strcmp(tmp_str, coupling[j])) {
676                                                 g_free(devc->coupling[i]);
677                                                 devc->coupling[i] = g_strdup(coupling[j]);
678                                                 strncpy(cmd, devc->coupling[i], 3);
679                                                 cmd[3] = 0;
680                                                 return siglent_sds_config_set(sdi, "C%d:CPL %s", i + 1, cmd);
681                                         }
682                                 }
683                                 sr_err("Invalid coupling index: %d.", j);
684                                 return SR_ERR_ARG;
685                         }
686                 }
687                 sr_dbg("Didn't set coupling, unknown channel(group).");
688                 return SR_ERR_NA;
689         case SR_CONF_PROBE_FACTOR:
690                 if (!cg)
691                         return SR_ERR_CHANNEL_GROUP;
692                 p = g_variant_get_uint64(data);
693                 for (i = 0; i < devc->model->analog_channels; i++) {
694                         if (cg == devc->analog_groups[i]) {
695                                 for (j = 0; j < ARRAY_SIZE(probe_factor); j++) {
696                                         if (p == probe_factor[j]) {
697                                                 devc->attenuation[i] = p;
698                                                 ret = siglent_sds_config_set(sdi, "C%d:ATTN %" PRIu64, i + 1, p);
699                                                 if (ret == SR_OK)
700                                                         siglent_sds_get_dev_cfg_vertical(sdi);
701                                                 return ret;
702                                         }
703                                 }
704                                 sr_err("Invalid probe factor: %" PRIu64 ".", p);
705                                 return SR_ERR_ARG;
706                         }
707                 }
708                 sr_dbg("Didn't set probe factor, unknown channel(group).");
709                 return SR_ERR_NA;
710         case SR_CONF_DATA_SOURCE:
711                 tmp_str = g_variant_get_string(data, NULL);
712                 if (!strcmp(tmp_str, "Display"))
713                         devc->data_source = DATA_SOURCE_SCREEN;
714                 else if (devc->model->series->protocol >= SPO_MODEL
715                         && !strcmp(tmp_str, "History"))
716                         devc->data_source = DATA_SOURCE_HISTORY;
717                 else {
718                         sr_err("Unknown data source: '%s'.", tmp_str);
719                         return SR_ERR;
720                 }
721                 break;
722         case SR_CONF_SAMPLERATE:
723                 siglent_sds_get_dev_cfg_horizontal(sdi);
724                 data = g_variant_new_uint64(devc->samplerate);
725                 break;
726         default:
727                 return SR_ERR_NA;
728         }
729
730         return ret;
731 }
732
733 static int config_list(uint32_t key, GVariant **data,
734         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
735 {
736         GVariant *tuple, *rational[2];
737         GVariantBuilder gvb;
738         unsigned int i;
739         struct dev_context *devc = NULL;
740
741         if (key == SR_CONF_SCAN_OPTIONS) {
742                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
743                         scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
744                 return SR_OK;
745         } else if (key == SR_CONF_DEVICE_OPTIONS && !cg) {
746                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
747                         devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
748                 return SR_OK;
749         }
750
751         /* Every other option requires a valid device instance. */
752         if (!sdi)
753                 return SR_ERR_ARG;
754         devc = sdi->priv;
755
756         /* If a channel group is specified, it must be a valid one. */
757         if (cg && !g_slist_find(sdi->channel_groups, cg)) {
758                 sr_err("Invalid channel group specified.");
759                 return SR_ERR;
760         }
761
762         switch (key) {
763         case SR_CONF_DEVICE_OPTIONS:
764                 if (!cg)
765                         return SR_ERR_CHANNEL_GROUP;
766                 if (cg == devc->digital_group) {
767                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
768                                 NULL, 0, sizeof(uint32_t));
769                         return SR_OK;
770                 } else {
771                         for (i = 0; i < devc->model->analog_channels; i++) {
772                                 if (cg == devc->analog_groups[i]) {
773                                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
774                                                 analog_devopts, ARRAY_SIZE(analog_devopts), sizeof(uint32_t));
775                                         return SR_OK;
776                                 }
777                         }
778                         return SR_ERR_NA;
779                 }
780                 break;
781         case SR_CONF_COUPLING:
782                 if (!cg)
783                         return SR_ERR_CHANNEL_GROUP;
784                 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
785                 break;
786         case SR_CONF_PROBE_FACTOR:
787                 if (!cg)
788                         return SR_ERR_CHANNEL_GROUP;
789                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
790                         probe_factor, ARRAY_SIZE(probe_factor), sizeof(uint64_t));
791                 break;
792         case SR_CONF_VDIV:
793                 if (!devc)
794                         /* Can't know this until we have the exact model. */
795                         return SR_ERR_ARG;
796                 if (!cg)
797                         return SR_ERR_CHANNEL_GROUP;
798                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
799                 for (i = 0; i < devc->num_vdivs; i++) {
800                         rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
801                         rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
802                         tuple = g_variant_new_tuple(rational, 2);
803                         g_variant_builder_add_value(&gvb, tuple);
804                 }
805                 *data = g_variant_builder_end(&gvb);
806                 break;
807         case SR_CONF_TIMEBASE:
808                 if (!devc)
809                         /* Can't know this until we have the exact model. */
810                         return SR_ERR_ARG;
811                 if (devc->num_timebases <= 0)
812                         return SR_ERR_NA;
813                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
814                 for (i = 0; i < devc->num_timebases; i++) {
815                         rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
816                         rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
817                         tuple = g_variant_new_tuple(rational, 2);
818                         g_variant_builder_add_value(&gvb, tuple);
819                 }
820                 *data = g_variant_builder_end(&gvb);
821                 break;
822         case SR_CONF_TRIGGER_SOURCE:
823                 if (!devc)
824                         /* Can't know this until we have the exact model. */
825                         return SR_ERR_ARG;
826                 *data = g_variant_new_strv(trigger_sources,
827                         devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 5);
828                 break;
829         case SR_CONF_TRIGGER_SLOPE:
830                 *data = g_variant_new_strv(trigger_slopes, ARRAY_SIZE(trigger_slopes));
831                 break;
832         case SR_CONF_DATA_SOURCE:
833                 if (!devc)
834                         /* Can't know this until we have the exact model. */
835                         return SR_ERR_ARG;
836                 switch (devc->model->series->protocol) {
837                 /* TODO: Check what must be done here for the data source buffer sizes. */
838                 case NON_SPO_MODEL:
839                         *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
840                         break;
841                 case SPO_MODEL:
842                         *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
843                         break;
844                 }
845                 break;
846         case SR_CONF_NUM_HDIV:
847                 *data = g_variant_new_int32(devc->model->series->num_horizontal_divs);
848                 break;
849         case SR_CONF_AVERAGING:
850                 /* TODO: Implement averaging. */
851                 break;
852         default:
853                 return SR_ERR_NA;
854         }
855
856         return SR_OK;
857 }
858
859 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
860 {
861         struct sr_scpi_dev_inst *scpi;
862         struct dev_context *devc;
863         struct sr_channel *ch;
864         struct sr_datafeed_packet packet;
865         gboolean some_digital;
866         GSList *l, *d;
867
868         scpi = sdi->conn;
869         devc = sdi->priv;
870
871         devc->num_frames = 0;
872         some_digital = FALSE;
873
874         /*
875          * Check if there are any logic channels enabled, if so then enable
876          * the MSO, otherwise skip the digital channel setup. Enable and
877          * disable channels on the device is very slow and it is faster when
878          * checked in a small loop without the actual actions.
879          */
880         for (d = sdi->channels; d; d = d->next) {
881                 ch = d->data;
882                 if (ch->type == SR_CHANNEL_LOGIC && ch->enabled)
883                         some_digital = TRUE;
884         }
885
886         for (l = sdi->channels; l; l = l->next) {
887                 ch = l->data;
888                 if (ch->type == SR_CHANNEL_ANALOG) {
889                         if (ch->enabled)
890                                 devc->enabled_channels = g_slist_append(
891                                         devc->enabled_channels, ch);
892                         if (ch->enabled != devc->analog_channels[ch->index]) {
893                                 /* Enabled channel is currently disabled, or vice versa. */
894                                 if (siglent_sds_config_set(sdi, "C%d:TRA %s", ch->index + 1,
895                                         ch->enabled ? "ON" : "OFF") != SR_OK)
896                                         return SR_ERR;
897                                 devc->analog_channels[ch->index] = ch->enabled;
898                         }
899                 } else if (ch->type == SR_CHANNEL_LOGIC && some_digital) {
900                         if (ch->enabled) {
901                                 /* Turn on LA module if currently off and digital channels are enabled. */
902                                 if (!devc->la_enabled) {
903                                         if (siglent_sds_config_set(sdi, "DGST ON") != SR_OK)
904                                                 return SR_ERR;
905                                         g_usleep(630000);
906                                         devc->la_enabled = TRUE;
907                                 }
908                                 devc->enabled_channels = g_slist_append(
909                                         devc->enabled_channels, ch);
910                         }
911                         /* Enabled channel is currently disabled, or vice versa. */
912                         if (siglent_sds_config_set(sdi, "D%d:DGCH %s", ch->index,
913                                 ch->enabled ? "ON" : "OFF") != SR_OK)
914                                 return SR_ERR;
915                         /* Slowing the command sequence down to let the device handle it. */
916                         g_usleep(630000);
917                         devc->digital_channels[ch->index] = ch->enabled;
918                 }
919         }
920         if (!devc->enabled_channels)
921                 return SR_ERR;
922         /* Turn off LA module if on and no digital channels selected. */
923         if (devc->la_enabled && !some_digital)
924                 if (siglent_sds_config_set(sdi, "DGST OFF") != SR_OK) {
925                         devc->la_enabled = FALSE;
926                         g_usleep(500000);
927                         return SR_ERR;
928                 }
929
930         // devc->analog_frame_size = devc->model->series->buffer_samples;
931         // devc->digital_frame_size = devc->model->series->buffer_samples;
932
933         switch (devc->model->series->protocol) {
934         case SPO_MODEL:
935                 if (siglent_sds_config_set(sdi, "WFSU SP,0,TYPE,1") != SR_OK)
936                         return SR_ERR;
937                 if (siglent_sds_config_set(sdi, "ACQW SAMPLING") != SR_OK)
938                         return SR_ERR;
939                 break;
940         case NON_SPO_MODEL:
941                 /* TODO: Implement CML/CNL/DL models. */
942                 if (siglent_sds_config_set(sdi, "WFSU SP,0,TYPE,1") != SR_OK)
943                         return SR_ERR;
944                 if (siglent_sds_config_set(sdi, "ACQW SAMPLING") != SR_OK)
945                         return SR_ERR;
946                 break;
947         default:
948                 break;
949         }
950
951         sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
952                 siglent_sds_receive, (void *) sdi);
953
954         std_session_send_df_header(sdi);
955
956         devc->channel_entry = devc->enabled_channels;
957
958         if (siglent_sds_capture_start(sdi) != SR_OK)
959                 return SR_ERR;
960
961         /* Start of first frame. */
962         packet.type = SR_DF_FRAME_BEGIN;
963         sr_session_send(sdi, &packet);
964
965         return SR_OK;
966 }
967
968 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
969 {
970         struct dev_context *devc;
971         struct sr_scpi_dev_inst *scpi;
972
973         devc = sdi->priv;
974
975         std_session_send_df_end(sdi);
976
977         g_slist_free(devc->enabled_channels);
978         devc->enabled_channels = NULL;
979         scpi = sdi->conn;
980         sr_scpi_source_remove(sdi->session, scpi);
981
982         return SR_OK;
983 }
984
985 SR_PRIV struct sr_dev_driver siglent_sds_driver_info = {
986         .name = "siglent-sds",
987         .longname = "Siglent SDS1000/SDS2000",
988         .api_version = 1,
989         .init = std_init,
990         .cleanup = std_cleanup,
991         .scan = scan,
992         .dev_list = std_dev_list,
993         .dev_clear = dev_clear,
994         .config_get = config_get,
995         .config_set = config_set,
996         .config_list = config_list,
997         .dev_open = dev_open,
998         .dev_close = dev_close,
999         .dev_acquisition_start = dev_acquisition_start,
1000         .dev_acquisition_stop = dev_acquisition_stop,
1001         .context = NULL,
1002 };
1003
1004 SR_REGISTER_DEV_DRIVER(siglent_sds_driver_info);