]> sigrok.org Git - libsigrok.git/blame - src/hardware/siglent-sds/protocol.c
siglent-sds: Added averaging function.
[libsigrok.git] / src / hardware / siglent-sds / protocol.c
CommitLineData
89f5fab9 1/*
2 * This file is part of the libsigrok project.
3 *
b3360671 4 * Copyright (C) 2016 mhooijboer <marchelh@gmail.com>
b3360671 5 * Copyright (C) 2012 Martin Ling <martin-git@earth.li>
6 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
7 * Copyright (C) 2013 Mathias Grimmberger <mgri@zaphod.sax.de>
89f5fab9 8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
b3360671 23#define _GNU_SOURCE
24
89f5fab9 25#include <config.h>
b3360671 26#include <errno.h>
27#include <glib.h>
b3360671 28#include <math.h>
29#include <stdlib.h>
30#include <stdarg.h>
31#include <string.h>
32#include <time.h>
33#include <unistd.h>
b3360671 34#include <libsigrok/libsigrok.h>
35#include "libsigrok-internal.h"
36#include "scpi.h"
89f5fab9 37#include "protocol.h"
38
3e7fb88f 39/* Set the next event to wait for in siglent_sds_receive(). */
b3360671 40static void siglent_sds_set_wait_event(struct dev_context *devc, enum wait_events event)
41{
b3360671 42 if (event == WAIT_STOP) {
43 devc->wait_status = 2;
44 } else {
45 devc->wait_status = 1;
46 devc->wait_event = event;
47 }
48}
49
50/*
51 * Waiting for a event will return a timeout after 2 to 3 seconds in order
52 * to not block the application.
53 */
54static int siglent_sds_event_wait(const struct sr_dev_inst *sdi)
55{
56 char *buf;
57 long s;
58 int out;
59 struct dev_context *devc;
60 time_t start;
61
62 if (!(devc = sdi->priv))
63 return SR_ERR;
64
65 start = time(NULL);
66
3e7fb88f 67 s = 10000; /* Sleep time for status refresh. */
b3360671 68 if (devc->wait_status == 1) {
69 do {
70 if (time(NULL) - start >= 3) {
3e7fb88f 71 sr_dbg("Timeout waiting for trigger.");
b3360671 72 return SR_ERR_TIMEOUT;
73 }
74
75 if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
76 return SR_ERR;
77 sr_atoi(buf, &out);
78 g_usleep(s);
79 } while (out == 0);
b3360671 80
3e7fb88f
UH
81 sr_dbg("Device triggered.");
82
83 if ((devc->timebase < 0.51) && (devc->timebase > 0.99e-6)) {
84 /*
85 * Timebase * num hor. divs * 85(%) * 1e6(usecs) / 100
86 * -> 85 percent of sweep time
87 */
88 s = (devc->timebase * devc->model->series->num_horizontal_divs * 1000);
89 sr_spew("Sleeping for %ld usecs after trigger, "
90 "to let the acq buffer in the device fill", s);
91 g_usleep(s);
b3360671 92 }
93 }
94 if (devc->wait_status == 2) {
95 do {
96 if (time(NULL) - start >= 3) {
3e7fb88f 97 sr_dbg("Timeout waiting for trigger.");
b3360671 98 return SR_ERR_TIMEOUT;
99 }
100 if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
101 return SR_ERR;
102 sr_atoi(buf, &out);
103 g_usleep(s);
104 /* XXX
105 * Now this loop condition looks suspicious! A bitwise
106 * OR of a variable and a non-zero literal should be
107 * non-zero. Logical AND of several non-zero values
108 * should be non-zero. Are many parts of the condition
109 * not taking effect? Was some different condition meant
110 * to get encoded? This needs review, and adjustment.
111 */
112 } while ((out | DEVICE_STATE_TRIG_RDY && out | DEVICE_STATE_DATA_ACQ) && out | DEVICE_STATE_STOPPED);
3e7fb88f 113 sr_dbg("Device triggerd 2.");
b3360671 114 siglent_sds_set_wait_event(devc, WAIT_NONE);
115 }
116
117 return SR_OK;
118}
119
120static int siglent_sds_trigger_wait(const struct sr_dev_inst *sdi)
121{
122 struct dev_context *devc;
123
124 if (!(devc = sdi->priv))
125 return SR_ERR;
126 return siglent_sds_event_wait(sdi);
127}
128
3e7fb88f 129/* Wait for scope to got to "Stop" in single shot mode. */
b3360671 130static int siglent_sds_stop_wait(const struct sr_dev_inst *sdi)
131{
b3360671 132 return siglent_sds_event_wait(sdi);
133}
134
135/* Send a configuration setting. */
136SR_PRIV int siglent_sds_config_set(const struct sr_dev_inst *sdi, const char *format, ...)
137{
138 va_list args;
139 int ret;
140
141 va_start(args, format);
142 ret = sr_scpi_send_variadic(sdi->conn, format, args);
143 va_end(args);
144
3e7fb88f 145 return ret;
b3360671 146}
147
3e7fb88f 148/* Start capturing a new frameset. */
b3360671 149SR_PRIV int siglent_sds_capture_start(const struct sr_dev_inst *sdi)
150{
151 struct dev_context *devc;
152
153 if (!(devc = sdi->priv))
154 return SR_ERR;
155
156 switch (devc->model->series->protocol) {
157 case SPO_MODEL:
158 if (devc->data_source == DATA_SOURCE_SCREEN) {
159 char *buf;
160 int out;
161
162 sr_dbg("Starting data capture for active frameset %" PRIu64 " of %" PRIu64,
163 devc->num_frames + 1, devc->limit_frames);
164 if (siglent_sds_config_set(sdi, "ARM") != SR_OK)
165 return SR_ERR;
166 if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
167 return SR_ERR;
168 sr_atoi(buf, &out);
169 if (out == DEVICE_STATE_TRIG_RDY) {
170 siglent_sds_set_wait_event(devc, WAIT_TRIGGER);
171 } else if (out == DEVICE_STATE_TRIG_RDY + 1) {
3e7fb88f 172 sr_spew("Device triggered.");
b3360671 173 siglent_sds_set_wait_event(devc, WAIT_BLOCK);
174 return SR_OK;
175 } else {
3e7fb88f 176 sr_spew("Device did not enter ARM mode.");
b3360671 177 return SR_ERR;
178 }
3e7fb88f 179 } else { /* TODO: Implement history retrieval. */
b3360671 180 unsigned int framecount;
181 char buf[200];
182 int ret;
183
3e7fb88f 184 sr_dbg("Starting data capture for history frameset.");
b3360671 185 if (siglent_sds_config_set(sdi, "FPAR?") != SR_OK)
186 return SR_ERR;
187 ret = sr_scpi_read_data(sdi->conn, buf, 200);
188 if (ret < 0) {
189 sr_err("Read error while reading data header.");
190 return SR_ERR;
191 }
192 memcpy(&framecount, buf + 40, 4);
3e7fb88f 193 if (devc->limit_frames > framecount)
e5896840 194 sr_err("Frame limit higher than frames in buffer of device!");
3e7fb88f 195 else if (devc->limit_frames == 0)
b3360671 196 devc->limit_frames = framecount;
b3360671 197 sr_dbg("Starting data capture for history frameset %" PRIu64 " of %" PRIu64,
198 devc->num_frames + 1, devc->limit_frames);
199 if (siglent_sds_config_set(sdi, "FRAM %i", devc->num_frames + 1) != SR_OK)
200 return SR_ERR;
201 if (siglent_sds_channel_start(sdi) != SR_OK)
202 return SR_ERR;
203 siglent_sds_set_wait_event(devc, WAIT_STOP);
204 }
205 break;
206 case NON_SPO_MODEL:
207 siglent_sds_set_wait_event(devc, WAIT_TRIGGER);
208 break;
209 }
210
211 return SR_OK;
212}
213
3e7fb88f 214/* Start reading data from the current channel. */
b3360671 215SR_PRIV int siglent_sds_channel_start(const struct sr_dev_inst *sdi)
89f5fab9 216{
89f5fab9 217 struct dev_context *devc;
b3360671 218 struct sr_channel *ch;
219
220 if (!(devc = sdi->priv))
221 return SR_ERR;
222
223 ch = devc->channel_entry->data;
224
3e7fb88f 225 sr_dbg("Starting reading data from channel %d.", ch->index + 1);
b3360671 226
227 switch (devc->model->series->protocol) {
228 case NON_SPO_MODEL:
229 case SPO_MODEL:
230 if (ch->type == SR_CHANNEL_LOGIC) {
231 if (sr_scpi_send(sdi->conn, "D%d:WF?",
232 ch->index + 1) != SR_OK)
233 return SR_ERR;
234 } else {
235 if (sr_scpi_send(sdi->conn, "C%d:WF? ALL",
236 ch->index + 1) != SR_OK)
237 return SR_ERR;
238 }
239 siglent_sds_set_wait_event(devc, WAIT_NONE);
240 break;
241 }
242
243 siglent_sds_set_wait_event(devc, WAIT_BLOCK);
244
245 devc->num_channel_bytes = 0;
246 devc->num_header_bytes = 0;
247 devc->num_block_bytes = 0;
248
249 return SR_OK;
250}
251
3e7fb88f 252/* Read the header of a data block. */
b3360671 253static int siglent_sds_read_header(struct sr_dev_inst *sdi, int channelIndex)
254{
255 struct sr_scpi_dev_inst *scpi = sdi->conn;
256 struct dev_context *devc = sdi->priv;
257 char *buf = (char *)devc->buffer;
e5896840
UH
258 int ret, desc_length;
259 int block_offset = 15; /* Offset for descriptor block. */
b3360671 260 long dataLength = 0;
261
3e7fb88f
UH
262 /* Read header from device. */
263 ret = sr_scpi_read_data(scpi, buf + devc->num_header_bytes,
264 devc->model->series->buffer_samples);
b3360671 265 if (ret < 346) {
266 sr_err("Read error while reading data header.");
267 return SR_ERR;
268 }
3e7fb88f 269 sr_dbg("Device returned %i bytes.", ret);
b3360671 270 devc->num_header_bytes += ret;
e5896840 271 buf += block_offset; /* Skip to start descriptor block. */
b3360671 272
3e7fb88f 273 /* Parse WaveDescriptor header. */
e5896840 274 memcpy(&desc_length, buf + 36, 4); /* Descriptor block length */
3e7fb88f 275 memcpy(&dataLength, buf + 60, 4); /* Data block length */
b3360671 276
277 devc->vdiv[channelIndex] = 2;
278 devc->vert_offset[channelIndex] = 0;
e5896840 279 devc->block_header_size = desc_length + 15;
b3360671 280 ret = dataLength;
3e7fb88f 281
e5896840 282 sr_dbg("Received data block header: '%s' -> block length %d.", buf, ret);
3e7fb88f 283
b3360671 284 return ret;
285}
286
287SR_PRIV int siglent_sds_receive(int fd, int revents, void *cb_data)
288{
289 struct sr_dev_inst *sdi;
290 struct sr_scpi_dev_inst *scpi;
291 struct dev_context *devc;
292 struct sr_datafeed_packet packet;
293 struct sr_datafeed_analog analog;
294 struct sr_analog_encoding encoding;
295 struct sr_analog_meaning meaning;
296 struct sr_analog_spec spec;
297 struct sr_datafeed_logic logic;
298 int len, i;
299 struct sr_channel *ch;
300 gsize expected_data_bytes = 0;
301 char *memsize;
89f5fab9 302
303 (void)fd;
304
305 if (!(sdi = cb_data))
306 return TRUE;
307
308 if (!(devc = sdi->priv))
309 return TRUE;
310
b3360671 311 scpi = sdi->conn;
312
313 if (!(revents == G_IO_IN || revents == 0))
314 return TRUE;
315
316 memsize = NULL;
317 if (sr_scpi_get_string(sdi->conn, "MSIZ?", &memsize) != SR_OK)
318 return SR_ERR;
319
320 switch (devc->wait_event) {
321 case WAIT_NONE:
322 break;
323 case WAIT_TRIGGER:
324 if (siglent_sds_trigger_wait(sdi) != SR_OK)
325 return TRUE;
326 if (siglent_sds_channel_start(sdi) != SR_OK)
327 return TRUE;
328 return TRUE;
329 case WAIT_BLOCK:
330 if (siglent_sds_channel_start(sdi) != SR_OK)
331 return TRUE;
332 break;
333 case WAIT_STOP:
3e7fb88f 334 if (siglent_sds_stop_wait(sdi) != SR_OK)
b3360671 335 return TRUE;
3e7fb88f 336 if (siglent_sds_channel_start(sdi) != SR_OK)
b3360671 337 return TRUE;
b3360671 338 return TRUE;
339 default:
3e7fb88f 340 sr_err("BUG: Unknown event target encountered.");
b3360671 341 break;
342 }
343
344 ch = devc->channel_entry->data;
345
346 if (devc->num_block_bytes == 0) {
347
3e7fb88f
UH
348 if (g_ascii_strcasecmp(memsize, "14M") == 0) {
349 sr_err("Device memory depth is set to 14Mpts, so please be patient.");
350 g_usleep(4900000); /* Sleep for large memory set. */
b3360671 351 }
3e7fb88f 352 sr_dbg("New block header expected.");
b3360671 353 len = siglent_sds_read_header(sdi, ch->index);
354 expected_data_bytes = len;
355 if (len == 0)
356 /* Still reading the header. */
357 return TRUE;
358 if (len == -1) {
359 sr_err("Read error, aborting capture.");
360 packet.type = SR_DF_FRAME_END;
361 sr_session_send(sdi, &packet);
362 sdi->driver->dev_acquisition_stop(sdi);
363 return TRUE;
364 }
365
366 if (devc->data_source == DATA_SOURCE_SCREEN
367 && (unsigned)len < expected_data_bytes) {
3e7fb88f 368 sr_dbg("Discarding short data block.");
b3360671 369 sr_scpi_read_data(scpi, (char *)devc->buffer, len + 1);
370 return TRUE;
371 }
372 devc->num_block_bytes = len;
373 devc->num_block_read = 0;
374 }
375
376 len = devc->num_block_bytes - devc->num_block_read;
377 if (len > ACQ_BUFFER_SIZE)
378 len = ACQ_BUFFER_SIZE;
379
3e7fb88f 380 /* Offset the data block buffer past the IEEE header and description header. */
e5896840 381 devc->buffer += devc->block_header_size;
b3360671 382
383 if (len == -1) {
384 sr_err("Read error, aborting capture.");
385 packet.type = SR_DF_FRAME_END;
386 sr_session_send(sdi, &packet);
387 sdi->driver->dev_acquisition_stop(sdi);
388 return TRUE;
89f5fab9 389 }
390
b3360671 391 sr_dbg("Received %d bytes.", len);
392
393 devc->num_block_read += len;
394
395 if (ch->type == SR_CHANNEL_ANALOG) {
396 float vdiv = devc->vdiv[ch->index];
397 float offset = devc->vert_offset[ch->index];
398 GArray *float_data;
399 static GArray *data;
e5896840 400 float voltage, vdivlog;
b3360671 401 int digits;
402
403 data = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len);
404 g_array_append_vals(data, devc->buffer, len);
405 float_data = g_array_new(FALSE, FALSE, sizeof(float));
406 for (i = 0; i < len; i++) {
407 voltage = (float)g_array_index(data, int8_t, i) / 25;
408 voltage = ((vdiv * voltage) - offset);
409 g_array_append_val(float_data, voltage);
410 }
411 vdivlog = log10f(vdiv);
412 digits = -(int) vdivlog + (vdivlog < 0.0);
413 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
414 analog.meaning->channels = g_slist_append(NULL, ch);
415 analog.num_samples = float_data->len;
416 analog.data = (float *)float_data->data;
417 analog.meaning->mq = SR_MQ_VOLTAGE;
418 analog.meaning->unit = SR_UNIT_VOLT;
419 analog.meaning->mqflags = 0;
420 packet.type = SR_DF_ANALOG;
421 packet.payload = &analog;
422 sr_session_send(sdi, &packet);
423 g_slist_free(analog.meaning->channels);
424 g_array_free(data, TRUE);
425 } else {
426 logic.length = len;
427 logic.unitsize = 1;
428 logic.data = devc->buffer;
429 packet.type = SR_DF_LOGIC;
430 packet.payload = &logic;
431 sr_session_send(sdi, &packet);
432 }
433
434 if (devc->num_block_read == devc->num_block_bytes) {
3e7fb88f
UH
435 sr_dbg("Block has been completed.");
436 sr_dbg("Preparing for possible next block.");
b3360671 437 devc->num_header_bytes = 0;
438 devc->num_block_bytes = 0;
3e7fb88f 439 if (devc->data_source != DATA_SOURCE_SCREEN)
b3360671 440 siglent_sds_set_wait_event(devc, WAIT_BLOCK);
b3360671 441 if (!sr_scpi_read_complete(scpi)) {
3e7fb88f 442 sr_err("Read should have been completed.");
b3360671 443 packet.type = SR_DF_FRAME_END;
444 sr_session_send(sdi, &packet);
445 sdi->driver->dev_acquisition_stop(sdi);
446 return TRUE;
447 }
448 devc->num_block_read = 0;
449 } else {
3e7fb88f 450 sr_dbg("%" PRIu64 " of %" PRIu64 " block bytes read.",
b3360671 451 devc->num_block_read, devc->num_block_bytes);
452 }
453 devc->num_channel_bytes += len;
454 if (devc->num_channel_bytes < expected_data_bytes) {
455 /* Don't have the full data for this channel yet, re-run. */
456 return TRUE;
457 }
458 if (devc->channel_entry->next) {
459 /* We got the frame for this channel, now get the next channel. */
460 devc->channel_entry = devc->channel_entry->next;
461 siglent_sds_channel_start(sdi);
462 } else {
463 /* Done with this frame. */
464 packet.type = SR_DF_FRAME_END;
465 sr_session_send(sdi, &packet);
466
467 if (++devc->num_frames == devc->limit_frames) {
468 /* Last frame, stop capture. */
469 sdi->driver->dev_acquisition_stop(sdi);
470 } else {
471 /* Get the next frame, starting with the first channel. */
472 devc->channel_entry = devc->enabled_channels;
473 siglent_sds_capture_start(sdi);
474
475 /* Start of next frame. */
476 packet.type = SR_DF_FRAME_BEGIN;
477 sr_session_send(sdi, &packet);
478 }
479 }
3e7fb88f 480
89f5fab9 481 return TRUE;
482}
b3360671 483
484SR_PRIV int siglent_sds_get_dev_cfg(const struct sr_dev_inst *sdi)
485{
486 struct dev_context *devc;
487 struct sr_channel *ch;
e5896840 488 char *cmd, *response;
b3360671 489 unsigned int i;
e5896840 490 int res, num_tokens;
b3360671 491 gchar **tokens;
b3360671 492
493 devc = sdi->priv;
494
495 /* Analog channel state. */
496 for (i = 0; i < devc->model->analog_channels; i++) {
497 cmd = g_strdup_printf("C%i:TRA?", i + 1);
498 res = sr_scpi_get_bool(sdi->conn, cmd, &devc->analog_channels[i]);
499 g_free(cmd);
500 if (res != SR_OK)
501 return SR_ERR;
502 ch = g_slist_nth_data(sdi->channels, i);
503 ch->enabled = devc->analog_channels[i];
504 }
505 sr_dbg("Current analog channel state:");
506 for (i = 0; i < devc->model->analog_channels; i++)
507 sr_dbg("CH%d %s", i + 1, devc->analog_channels[i] ? "On" : "Off");
508
509 /* Digital channel state. */
510 if (devc->model->has_digital) {
511 gboolean status;
512
3e7fb88f 513 sr_dbg("Check logic analyzer channel state.");
b3360671 514 devc->la_enabled = FALSE;
515 cmd = g_strdup_printf("DGST?");
516 res = sr_scpi_get_bool(sdi->conn, cmd, &status);
517 g_free(cmd);
518 if (res != SR_OK)
519 return SR_ERR;
3e7fb88f 520 sr_dbg("Logic analyzer status: %s", status ? "On" : "Off");
b3360671 521 if (status) {
522 devc->la_enabled = TRUE;
523 for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) {
524 cmd = g_strdup_printf("D%i:DGCH?", i);
525 res = sr_scpi_get_bool(sdi->conn, cmd, &devc->digital_channels[i]);
526 g_free(cmd);
527 if (res != SR_OK)
528 return SR_ERR;
529 ch = g_slist_nth_data(sdi->channels, i + devc->model->analog_channels);
530 ch->enabled = devc->digital_channels[i];
531 sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "On" : "Off");
532 }
533 } else {
534 for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) {
535 ch = g_slist_nth_data(sdi->channels, i + devc->model->analog_channels);
536 devc->digital_channels[i] = FALSE;
537 ch->enabled = devc->digital_channels[i];
538 sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "On" : "Off");
539 }
540 }
541 }
542
543 /* Timebase. */
544 if (sr_scpi_get_float(sdi->conn, ":TDIV?", &devc->timebase) != SR_OK)
545 return SR_ERR;
3e7fb88f 546 sr_dbg("Current timebase: %g.", devc->timebase);
b3360671 547
548 /* Probe attenuation. */
549 for (i = 0; i < devc->model->analog_channels; i++) {
550 cmd = g_strdup_printf("C%d:ATTN?", i + 1);
551 res = sr_scpi_get_float(sdi->conn, cmd, &devc->attenuation[i]);
552 g_free(cmd);
553 if (res != SR_OK)
554 return SR_ERR;
555 }
556 sr_dbg("Current probe attenuation:");
557 for (i = 0; i < devc->model->analog_channels; i++)
558 sr_dbg("CH%d %g", i + 1, devc->attenuation[i]);
559
560 /* Vertical gain and offset. */
561 if (siglent_sds_get_dev_cfg_vertical(sdi) != SR_OK)
562 return SR_ERR;
563
564 /* Coupling. */
565 for (i = 0; i < devc->model->analog_channels; i++) {
566 cmd = g_strdup_printf("C%d:CPL?", i + 1);
567 res = sr_scpi_get_string(sdi->conn, cmd, &devc->coupling[i]);
568 g_free(cmd);
569 if (res != SR_OK)
570 return SR_ERR;
571 }
572
573 sr_dbg("Current coupling:");
574 for (i = 0; i < devc->model->analog_channels; i++)
575 sr_dbg("CH%d %s", i + 1, devc->coupling[i]);
576
577 /* Trigger source. */
578 response = NULL;
579 tokens = NULL;
580 if (sr_scpi_get_string(sdi->conn, "TRSE?", &response) != SR_OK)
581 return SR_ERR;
582 tokens = g_strsplit(response, ",", 0);
583 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
584 if (num_tokens < 4) {
585 sr_dbg("IDN response not according to spec: %80.s.", response);
586 g_strfreev(tokens);
587 g_free(response);
588 return SR_ERR_DATA;
589 }
590 g_free(response);
591 devc->trigger_source = g_strstrip(g_strdup(tokens[2]));
3e7fb88f 592 sr_dbg("Current trigger source: %s.", devc->trigger_source);
b3360671 593
3e7fb88f 594 /* TODO: Horizontal trigger position. */
b3360671 595
596 devc->horiz_triggerpos = 0;
3e7fb88f 597 sr_dbg("Current horizontal trigger position: %g.", devc->horiz_triggerpos);
b3360671 598
599 /* Trigger slope. */
600 cmd = g_strdup_printf("%s:TRSL?", devc->trigger_source);
601 res = sr_scpi_get_string(sdi->conn, cmd, &devc->trigger_slope);
602 g_free(cmd);
603 if (res != SR_OK)
604 return SR_ERR;
3e7fb88f 605 sr_dbg("Current trigger slope: %s.", devc->trigger_slope);
b3360671 606
607 /* Trigger level. */
608 cmd = g_strdup_printf("%s:TRLV?", devc->trigger_source);
609 res = sr_scpi_get_float(sdi->conn, cmd, &devc->trigger_level);
610 g_free(cmd);
611 if (res != SR_OK)
612 return SR_ERR;
3e7fb88f 613 sr_dbg("Current trigger level: %g.", devc->trigger_level);
b3360671 614 return SR_OK;
615}
616
617SR_PRIV int siglent_sds_get_dev_cfg_vertical(const struct sr_dev_inst *sdi)
618{
619 struct dev_context *devc;
620 char *cmd;
621 unsigned int i;
622 int res;
623
624 devc = sdi->priv;
625
626 /* Vertical gain. */
627 for (i = 0; i < devc->model->analog_channels; i++) {
628 cmd = g_strdup_printf("C%d:VDIV?", i + 1);
629 res = sr_scpi_get_float(sdi->conn, cmd, &devc->vdiv[i]);
630 g_free(cmd);
631 if (res != SR_OK)
632 return SR_ERR;
633 }
634 sr_dbg("Current vertical gain:");
635 for (i = 0; i < devc->model->analog_channels; i++)
636 sr_dbg("CH%d %g", i + 1, devc->vdiv[i]);
637
638 /* Vertical offset. */
639 for (i = 0; i < devc->model->analog_channels; i++) {
640 cmd = g_strdup_printf("C%d:OFST?", i + 1);
641 res = sr_scpi_get_float(sdi->conn, cmd, &devc->vert_offset[i]);
642 g_free(cmd);
643 if (res != SR_OK)
644 return SR_ERR;
645 }
646 sr_dbg("Current vertical offset:");
647 for (i = 0; i < devc->model->analog_channels; i++)
648 sr_dbg("CH%d %g", i + 1, devc->vert_offset[i]);
649
650 return SR_OK;
651}
652
653SR_PRIV int siglent_sds_get_dev_cfg_horizontal(const struct sr_dev_inst *sdi)
654{
655 struct dev_context *devc;
656 char *cmd;
657 int res;
e5896840
UH
658 char *sample_points_string;
659 float samplerate_scope, fvalue;
b3360671 660 char *first, *concat;
661
662 devc = sdi->priv;
663 cmd = g_strdup_printf("SANU? C1");
e5896840 664 res = sr_scpi_get_string(sdi->conn, cmd, &sample_points_string);
b3360671 665 g_free(cmd);
666 if (res != SR_OK)
667 return SR_ERR;
e5896840
UH
668 if (g_strstr_len(sample_points_string, -1, "Mpts") != NULL) {
669 sample_points_string[strlen(sample_points_string) - 4] = '\0';
b3360671 670
e5896840
UH
671 if (g_strstr_len(sample_points_string, -1, ".") != NULL) {
672 first = strtok(sample_points_string, ".");
b3360671 673 concat = strcat(first, strtok(NULL, "."));
674 if (sr_atof_ascii(concat, &fvalue) != SR_OK || fvalue == 0.0) {
675 sr_dbg("Invalid float converted from scope response.");
676 return SR_ERR;
677 }
678 } else {
e5896840 679 if (sr_atof_ascii(sample_points_string, &fvalue) != SR_OK || fvalue == 0.0) {
b3360671 680 sr_dbg("Invalid float converted from scope response.");
681 return SR_ERR;
682 }
683 }
e5896840 684 samplerate_scope = fvalue * 100000;
b3360671 685 } else {
e5896840
UH
686 sample_points_string[strlen(sample_points_string) - 4] = '\0';
687 if (sr_atof_ascii(sample_points_string, &fvalue) != SR_OK || fvalue == 0.0) {
b3360671 688 sr_dbg("Invalid float converted from scope response.");
689 return SR_ERR;
690 }
e5896840 691 samplerate_scope = fvalue * 1000;
b3360671 692 }
3e7fb88f 693 /* Get the timebase. */
b3360671 694 if (sr_scpi_get_float(sdi->conn, ":TDIV?", &devc->timebase) != SR_OK)
695 return SR_ERR;
3e7fb88f 696 sr_dbg("Current timebase: %g.", devc->timebase);
e5896840 697 devc->samplerate = samplerate_scope / (devc->timebase * devc->model->series->num_horizontal_divs);
3e7fb88f 698
b3360671 699 return SR_OK;
700}