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