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