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