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