2 * This file is part of the libsigrok project.
4 * Copyright (C) 2016 mhooijboer <marchelh@gmail.com>
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>
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.
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.
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/>.
34 #include <libsigrok/libsigrok.h>
35 #include "libsigrok-internal.h"
39 /* Set the next event to wait for in siglent_sds_receive(). */
40 static void siglent_sds_set_wait_event(struct dev_context *devc, enum wait_events event)
42 if (event == WAIT_STOP) {
43 devc->wait_status = 2;
45 devc->wait_status = 1;
46 devc->wait_event = event;
51 * Waiting for a event will return a timeout after 2 to 3 seconds in order
52 * to not block the application.
54 static int siglent_sds_event_wait(const struct sr_dev_inst *sdi)
59 struct dev_context *devc;
62 if (!(devc = sdi->priv))
67 s = 10000; /* Sleep time for status refresh. */
68 if (devc->wait_status == 1) {
70 if (time(NULL) - start >= 3) {
71 sr_dbg("Timeout waiting for trigger.");
72 return SR_ERR_TIMEOUT;
75 if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
81 sr_dbg("Device triggered.");
83 if ((devc->timebase < 0.51) && (devc->timebase > 0.99e-6)) {
85 * Timebase * num hor. divs * 85(%) * 1e6(usecs) / 100
86 * -> 85 percent of sweep time
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);
94 if (devc->wait_status == 2) {
96 if (time(NULL) - start >= 3) {
97 sr_dbg("Timeout waiting for trigger.");
98 return SR_ERR_TIMEOUT;
100 if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
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.
112 } while (out != DEVICE_STATE_TRIG_RDY || out != DEVICE_STATE_DATA_TRIG_RDY || out != DEVICE_STATE_STOPPED);
114 sr_dbg("Device triggered.");
116 siglent_sds_set_wait_event(devc, WAIT_NONE);
122 static int siglent_sds_trigger_wait(const struct sr_dev_inst *sdi)
124 struct dev_context *devc;
126 if (!(devc = sdi->priv))
128 return siglent_sds_event_wait(sdi);
131 /* Wait for scope to got to "Stop" in single shot mode. */
132 static int siglent_sds_stop_wait(const struct sr_dev_inst *sdi)
134 return siglent_sds_event_wait(sdi);
137 /* Send a configuration setting. */
138 SR_PRIV int siglent_sds_config_set(const struct sr_dev_inst *sdi, const char *format, ...)
143 va_start(args, format);
144 ret = sr_scpi_send_variadic(sdi->conn, format, args);
150 /* Start capturing a new frameset. */
151 SR_PRIV int siglent_sds_capture_start(const struct sr_dev_inst *sdi)
153 struct dev_context *devc;
155 if (!(devc = sdi->priv))
158 switch (devc->model->series->protocol) {
160 if (devc->data_source == DATA_SOURCE_SCREEN) {
164 sr_dbg("Starting data capture for active frameset %" PRIu64 " of %" PRIu64,
165 devc->num_frames + 1, devc->limit_frames);
166 if (siglent_sds_config_set(sdi, "ARM") != SR_OK)
168 if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
171 if (out == DEVICE_STATE_TRIG_RDY) {
172 siglent_sds_set_wait_event(devc, WAIT_TRIGGER);
173 } else if (out == DEVICE_STATE_DATA_TRIG_RDY) {
174 sr_spew("Device triggered.");
175 siglent_sds_set_wait_event(devc, WAIT_BLOCK);
178 sr_spew("Device did not enter ARM mode.");
181 } else { /* TODO: Implement history retrieval. */
182 unsigned int framecount;
186 sr_dbg("Starting data capture for history frameset.");
187 if (siglent_sds_config_set(sdi, "FPAR?") != SR_OK)
189 ret = sr_scpi_read_data(sdi->conn, buf, 200);
191 sr_err("Read error while reading data header.");
194 memcpy(&framecount, buf + 40, 4);
195 if (devc->limit_frames > framecount)
196 sr_err("Frame limit higher than frames in buffer of device!");
197 else if (devc->limit_frames == 0)
198 devc->limit_frames = framecount;
199 sr_dbg("Starting data capture for history frameset %" PRIu64 " of %" PRIu64,
200 devc->num_frames + 1, devc->limit_frames);
201 if (siglent_sds_config_set(sdi, "FRAM %i", devc->num_frames + 1) != SR_OK)
203 if (siglent_sds_channel_start(sdi) != SR_OK)
205 siglent_sds_set_wait_event(devc, WAIT_STOP);
209 if (devc->data_source == DATA_SOURCE_SCREEN) {
213 sr_dbg("Starting data capture for active frameset %" PRIu64 " of %" PRIu64,
214 devc->num_frames + 1, devc->limit_frames);
215 if (siglent_sds_config_set(sdi, "ARM") != SR_OK)
217 if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
220 if (out == DEVICE_STATE_TRIG_RDY) {
221 siglent_sds_set_wait_event(devc, WAIT_TRIGGER);
222 } else if (out == DEVICE_STATE_DATA_TRIG_RDY) {
223 sr_spew("Device triggered.");
224 siglent_sds_set_wait_event(devc, WAIT_BLOCK);
227 sr_spew("Device did not enter ARM mode.");
230 } else { /* TODO: Implement history retrieval. */
231 unsigned int framecount;
235 sr_dbg("Starting data capture for history frameset.");
236 if (siglent_sds_config_set(sdi, "FPAR?") != SR_OK)
238 ret = sr_scpi_read_data(sdi->conn, buf, 200);
240 sr_err("Read error while reading data header.");
243 memcpy(&framecount, buf + 40, 4);
244 if (devc->limit_frames > framecount)
245 sr_err("Frame limit higher than frames in buffer of device!");
246 else if (devc->limit_frames == 0)
247 devc->limit_frames = framecount;
248 sr_dbg("Starting data capture for history frameset %" PRIu64 " of %" PRIu64,
249 devc->num_frames + 1, devc->limit_frames);
250 if (siglent_sds_config_set(sdi, "FRAM %i", devc->num_frames + 1) != SR_OK)
252 if (siglent_sds_channel_start(sdi) != SR_OK)
254 siglent_sds_set_wait_event(devc, WAIT_STOP);
258 siglent_sds_set_wait_event(devc, WAIT_TRIGGER);
265 /* Start reading data from the current channel. */
266 SR_PRIV int siglent_sds_channel_start(const struct sr_dev_inst *sdi)
268 struct dev_context *devc;
269 struct sr_channel *ch;
272 if (!(devc = sdi->priv))
275 ch = devc->channel_entry->data;
277 sr_dbg("Start reading data from channel %s.", ch->name);
279 switch (devc->model->series->protocol) {
282 s = (ch->type == SR_CHANNEL_LOGIC) ? "D%d:WF?" : "C%d:WF? ALL";
283 if (sr_scpi_send(sdi->conn, s, ch->index + 1) != SR_OK)
285 siglent_sds_set_wait_event(devc, WAIT_NONE);
288 if (ch->type == SR_CHANNEL_ANALOG) {
289 if (sr_scpi_send(sdi->conn, "C%d:WF? ALL",
290 ch->index + 1) != SR_OK)
293 siglent_sds_set_wait_event(devc, WAIT_NONE);
294 if (sr_scpi_read_begin(sdi->conn) != SR_OK)
296 siglent_sds_set_wait_event(devc, WAIT_BLOCK);
300 devc->num_channel_bytes = 0;
301 devc->num_header_bytes = 0;
302 devc->num_block_bytes = 0;
307 /* Read the header of a data block. */
308 static int siglent_sds_read_header(struct sr_dev_inst *sdi)
310 struct sr_scpi_dev_inst *scpi = sdi->conn;
311 struct dev_context *devc = sdi->priv;
312 char *buf = (char *)devc->buffer;
313 int ret, desc_length;
314 int block_offset = 15; /* Offset for descriptor block. */
315 long data_length = 0;
317 /* Read header from device. */
318 ret = sr_scpi_read_data(scpi, buf, SIGLENT_HEADER_SIZE);
319 if (ret < SIGLENT_HEADER_SIZE) {
320 sr_err("Read error while reading data header.");
323 sr_dbg("Device returned %i bytes.", ret);
324 devc->num_header_bytes += ret;
325 buf += block_offset; /* Skip to start descriptor block. */
327 /* Parse WaveDescriptor header. */
328 memcpy(&desc_length, buf + 36, 4); /* Descriptor block length */
329 memcpy(&data_length, buf + 60, 4); /* Data block length */
331 devc->block_header_size = desc_length + 15;
332 devc->num_samples = data_length;
334 sr_dbg("Received data block header: '%s' -> block length %d.", buf, ret);
339 static int siglent_sds_get_digital(const struct sr_dev_inst *sdi, struct sr_channel *ch)
341 struct sr_scpi_dev_inst *scpi = sdi->conn;
342 struct dev_context *devc = sdi->priv;
343 GArray *tmp_samplebuf; /* Temp buffer while iterating over the scope samples */
344 char *buf = (char *)devc->buffer; /* Buffer from scope */
345 uint8_t tmp_value; /* Holding temp value from data */
346 GArray *data_low_channels, *data_high_channels, *buffdata;
348 gboolean low_channels; /* Lower channels enabled */
349 gboolean high_channels; /* Higher channels enabled */
350 int len, channel_index;
355 low_channels = FALSE;
356 high_channels = FALSE;
357 data_low_channels = g_array_new(FALSE, TRUE, sizeof(uint8_t));
358 data_high_channels = g_array_new(FALSE, TRUE, sizeof(uint8_t));
360 for (l = sdi->channels; l; l = l->next) {
363 if (ch->type == SR_CHANNEL_LOGIC) {
365 if (sr_scpi_send(sdi->conn, "D%d:WF? DAT2", ch->index) != SR_OK)
367 if (sr_scpi_read_begin(scpi) != SR_OK)
369 len = sr_scpi_read_data(scpi, buf, -1);
373 buffdata = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len);
374 buf += 15; /* Skipping the data header. */
375 g_array_append_vals(buffdata, buf, len);
376 tmp_samplebuf = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len); /* New temp buffer. */
377 for (uint64_t cur_sample_index = 0; cur_sample_index < devc->memory_depth_digital; cur_sample_index++) {
378 char sample = (char)g_array_index(buffdata, uint8_t, cur_sample_index);
379 for (int ii = 0; ii < 8; ii++, sample >>= 1) {
381 channel_index = ch->index;
382 if (data_low_channels->len <= samples_index) {
383 tmp_value = 0; /* New sample. */
384 low_channels = TRUE; /* We have at least one enabled low channel. */
386 /* Get previous stored sample from low channel buffer. */
387 tmp_value = g_array_index(data_low_channels, uint8_t, samples_index);
390 channel_index = ch->index - 8;
391 if (data_high_channels->len <= samples_index) {
392 tmp_value = 0; /* New sample. */
393 high_channels = TRUE; /* We have at least one enabled high channel. */
395 /* Get previous stored sample from high channel buffer. */
396 tmp_value = g_array_index(data_high_channels, uint8_t, samples_index);
399 /* Check if the current scope sample bit is set. */
401 tmp_value |= 1UL << channel_index; /* Set current scope sample bit based on channel index. */
402 g_array_append_val(tmp_samplebuf, tmp_value);
407 /* Clear the buffers to prepare for the new samples */
409 g_array_free(data_low_channels, FALSE);
410 data_low_channels = g_array_new(FALSE, FALSE, sizeof(uint8_t));
412 g_array_free(data_high_channels, FALSE);
413 data_high_channels = g_array_new(FALSE, FALSE, sizeof(uint8_t));
416 /* Storing the converted temp values from the the scope into the buffers. */
417 for (long index = 0; index < tmp_samplebuf->len; index++) {
418 uint8_t value = g_array_index(tmp_samplebuf, uint8_t, index);
420 g_array_append_val(data_low_channels, value);
422 g_array_append_val(data_high_channels, value);
424 g_array_free(tmp_samplebuf, TRUE);
425 g_array_free(buffdata, TRUE);
430 /* Combining the lower and higher channel buffers into one buffer for sigrok. */
431 devc->dig_buffer = g_array_new(FALSE, FALSE, sizeof(uint8_t));
432 for (uint64_t index = 0; index < devc->memory_depth_digital; index++) {
435 value = g_array_index(data_low_channels, uint8_t, index);
436 g_array_append_val(devc->dig_buffer, value);
439 g_array_append_val(devc->dig_buffer, value);
442 value = g_array_index(data_high_channels, uint8_t, index);
443 g_array_append_val(devc->dig_buffer, value);
446 g_array_append_val(devc->dig_buffer, value);
450 g_array_free(data_low_channels, TRUE);
451 g_array_free(data_high_channels, TRUE);
456 SR_PRIV int siglent_sds_receive(int fd, int revents, void *cb_data)
458 struct sr_dev_inst *sdi;
459 struct sr_scpi_dev_inst *scpi;
460 struct dev_context *devc;
461 struct sr_datafeed_packet packet;
462 struct sr_datafeed_analog analog;
463 struct sr_analog_encoding encoding;
464 struct sr_analog_meaning meaning;
465 struct sr_analog_spec spec;
466 struct sr_datafeed_logic logic;
467 struct sr_channel *ch;
470 gboolean read_complete = FALSE;
474 if (!(sdi = cb_data))
477 if (!(devc = sdi->priv))
482 if (!(revents == G_IO_IN || revents == 0))
485 switch (devc->wait_event) {
489 if (siglent_sds_trigger_wait(sdi) != SR_OK)
491 if (siglent_sds_channel_start(sdi) != SR_OK)
495 if (siglent_sds_channel_start(sdi) != SR_OK)
499 if (siglent_sds_stop_wait(sdi) != SR_OK)
501 if (siglent_sds_channel_start(sdi) != SR_OK)
505 sr_err("BUG: Unknown event target encountered.");
509 ch = devc->channel_entry->data;
512 if (ch->type == SR_CHANNEL_ANALOG) {
513 if (devc->num_block_bytes == 0) {
514 /* Wait for the device to fill its output buffers. */
515 switch (devc->model->series->protocol) {
518 /* The older models need more time to prepare the the output buffers due to CPU speed. */
519 wait = (devc->memory_depth_analog * 2.5);
520 sr_dbg("Waiting %.f0 ms for device to prepare the output buffers", wait / 1000);
522 if (sr_scpi_read_begin(scpi) != SR_OK)
526 /* The newer models (ending with the E) have faster CPUs but still need time when a slow timebase is selected. */
527 if (sr_scpi_read_begin(scpi) != SR_OK)
529 wait = ((devc->timebase * devc->model->series->num_horizontal_divs) * 100000);
530 sr_dbg("Waiting %.f0 ms for device to prepare the output buffers", wait / 1000);
535 sr_dbg("New block with header expected.");
536 len = siglent_sds_read_header(sdi);
538 /* Still reading the header. */
541 sr_err("Read error, aborting capture.");
542 packet.type = SR_DF_FRAME_END;
543 sr_session_send(sdi, &packet);
544 sdi->driver->dev_acquisition_stop(sdi);
547 devc->num_block_bytes = len;
548 devc->num_block_read = 0;
551 sr_err("Read error, aborting capture.");
552 packet.type = SR_DF_FRAME_END;
553 sr_session_send(sdi, &packet);
554 sdi->driver->dev_acquisition_stop(sdi);
559 read_complete = FALSE;
560 if (devc->num_block_bytes > devc->num_samples) {
561 /* We received all data as one block. */
562 /* Offset the data block buffer past the IEEE header and description header. */
563 devc->buffer += devc->block_header_size;
564 len = devc->num_samples;
566 sr_dbg("Requesting: %li bytes.", devc->num_samples - devc->num_block_bytes);
567 len = sr_scpi_read_data(scpi, (char *)devc->buffer, devc->num_samples-devc->num_block_bytes);
569 sr_err("Read error, aborting capture.");
570 packet.type = SR_DF_FRAME_END;
571 sr_session_send(sdi, &packet);
572 sdi->driver->dev_acquisition_stop(sdi);
575 devc->num_block_read++;
576 devc->num_block_bytes += len;
578 sr_dbg("Received block: %i, %d bytes.", devc->num_block_read, len);
579 if (ch->type == SR_CHANNEL_ANALOG) {
580 float vdiv = devc->vdiv[ch->index];
581 float offset = devc->vert_offset[ch->index];
584 float voltage, vdivlog;
587 data = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len);
588 g_array_append_vals(data, devc->buffer, len);
589 float_data = g_array_new(FALSE, FALSE, sizeof(float));
590 for (i = 0; i < len; i++) {
591 voltage = (float)g_array_index(data, int8_t, i) / 25;
592 voltage = ((vdiv * voltage) - offset);
593 g_array_append_val(float_data, voltage);
595 vdivlog = log10f(vdiv);
596 digits = -(int) vdivlog + (vdivlog < 0.0);
597 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
598 analog.meaning->channels = g_slist_append(NULL, ch);
599 analog.num_samples = float_data->len;
600 analog.data = (float *)float_data->data;
601 analog.meaning->mq = SR_MQ_VOLTAGE;
602 analog.meaning->unit = SR_UNIT_VOLT;
603 analog.meaning->mqflags = 0;
604 packet.type = SR_DF_ANALOG;
605 packet.payload = &analog;
606 sr_session_send(sdi, &packet);
607 g_slist_free(analog.meaning->channels);
608 g_array_free(data, TRUE);
611 if (devc->num_samples == (devc->num_block_bytes - SIGLENT_HEADER_SIZE)) {
612 sr_dbg("Transfer has been completed.");
613 devc->num_header_bytes = 0;
614 devc->num_block_bytes = 0;
615 read_complete = TRUE;
616 if (!sr_scpi_read_complete(scpi)) {
617 sr_err("Read should have been completed.");
618 packet.type = SR_DF_FRAME_END;
619 sr_session_send(sdi, &packet);
620 sdi->driver->dev_acquisition_stop(sdi);
623 devc->num_block_read = 0;
625 sr_dbg("%" PRIu64 " of %" PRIu64 " block bytes read.",
626 devc->num_block_bytes, devc->num_samples);
628 } while (!read_complete);
630 if (devc->channel_entry->next) {
631 /* We got the frame for this channel, now get the next channel. */
632 devc->channel_entry = devc->channel_entry->next;
633 siglent_sds_channel_start(sdi);
635 /* Done with this frame. */
636 packet.type = SR_DF_FRAME_END;
637 sr_session_send(sdi, &packet);
638 if (++devc->num_frames == devc->limit_frames) {
639 /* Last frame, stop capture. */
640 sdi->driver->dev_acquisition_stop(sdi);
642 /* Get the next frame, starting with the first channel. */
643 devc->channel_entry = devc->enabled_channels;
644 siglent_sds_capture_start(sdi);
646 /* Start of next frame. */
647 packet.type = SR_DF_FRAME_BEGIN;
648 sr_session_send(sdi, &packet);
653 if (!siglent_sds_get_digital(sdi, ch))
655 logic.length = devc->dig_buffer->len;
657 logic.data = devc->dig_buffer->data;
658 packet.type = SR_DF_LOGIC;
659 packet.payload = &logic;
660 sr_session_send(sdi, &packet);
661 packet.type = SR_DF_FRAME_END;
662 sr_session_send(sdi, &packet);
663 sdi->driver->dev_acquisition_stop(sdi);
665 if (++devc->num_frames == devc->limit_frames) {
666 /* Last frame, stop capture. */
667 sdi->driver->dev_acquisition_stop(sdi);
669 /* Get the next frame, starting with the first channel. */
670 devc->channel_entry = devc->enabled_channels;
671 siglent_sds_capture_start(sdi);
673 /* Start of next frame. */
674 packet.type = SR_DF_FRAME_BEGIN;
675 sr_session_send(sdi, &packet);
679 // sr_session_send(sdi, &packet);
680 // packet.type = SR_DF_FRAME_END;
681 // sr_session_send(sdi, &packet);
682 // sdi->driver->dev_acquisition_stop(sdi);
687 SR_PRIV int siglent_sds_get_dev_cfg(const struct sr_dev_inst *sdi)
689 struct dev_context *devc;
690 struct sr_channel *ch;
691 char *cmd, *response;
700 /* Analog channel state. */
701 for (i = 0; i < devc->model->analog_channels; i++) {
702 cmd = g_strdup_printf("C%i:TRA?", i + 1);
703 res = sr_scpi_get_bool(sdi->conn, cmd, &devc->analog_channels[i]);
707 ch = g_slist_nth_data(sdi->channels, i);
708 ch->enabled = devc->analog_channels[i];
710 sr_dbg("Current analog channel state:");
711 for (i = 0; i < devc->model->analog_channels; i++)
712 sr_dbg("CH%d %s", i + 1, devc->analog_channels[i] ? "On" : "Off");
714 /* Digital channel state. */
715 if (devc->model->has_digital) {
718 sr_dbg("Check logic analyzer channel state.");
719 devc->la_enabled = FALSE;
720 cmd = g_strdup_printf("DI:SW?");
721 res = sr_scpi_get_bool(sdi->conn, cmd, &status);
725 sr_dbg("Logic analyzer status: %s", status ? "On" : "Off");
727 devc->la_enabled = TRUE;
728 for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) {
729 cmd = g_strdup_printf("D%i:TRA?", i);
730 res = sr_scpi_get_bool(sdi->conn, cmd, &devc->digital_channels[i]);
734 ch = g_slist_nth_data(sdi->channels, i + devc->model->analog_channels);
735 ch->enabled = devc->digital_channels[i];
736 sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "On" : "Off");
739 for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) {
740 ch = g_slist_nth_data(sdi->channels, i + devc->model->analog_channels);
741 devc->digital_channels[i] = FALSE;
742 ch->enabled = devc->digital_channels[i];
743 sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "On" : "Off");
749 if (sr_scpi_get_float(sdi->conn, ":TDIV?", &devc->timebase) != SR_OK)
751 sr_dbg("Current timebase: %g.", devc->timebase);
753 /* Probe attenuation. */
754 for (i = 0; i < devc->model->analog_channels; i++) {
755 cmd = g_strdup_printf("C%d:ATTN?", i + 1);
756 res = sr_scpi_get_float(sdi->conn, cmd, &devc->attenuation[i]);
761 sr_dbg("Current probe attenuation:");
762 for (i = 0; i < devc->model->analog_channels; i++)
763 sr_dbg("CH%d %g", i + 1, devc->attenuation[i]);
765 /* Vertical gain and offset. */
766 if (siglent_sds_get_dev_cfg_vertical(sdi) != SR_OK)
770 for (i = 0; i < devc->model->analog_channels; i++) {
771 cmd = g_strdup_printf("C%d:CPL?", i + 1);
772 res = sr_scpi_get_string(sdi->conn, cmd, &devc->coupling[i]);
778 sr_dbg("Current coupling:");
779 for (i = 0; i < devc->model->analog_channels; i++)
780 sr_dbg("CH%d %s", i + 1, devc->coupling[i]);
782 /* Trigger source. */
785 if (sr_scpi_get_string(sdi->conn, "TRSE?", &response) != SR_OK)
787 tokens = g_strsplit(response, ",", 0);
788 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
789 if (num_tokens < 4) {
790 sr_dbg("IDN response not according to spec: %80.s.", response);
796 devc->trigger_source = g_strstrip(g_strdup(tokens[2]));
797 sr_dbg("Current trigger source: %s.", devc->trigger_source);
799 /* TODO: Horizontal trigger position. */
802 // if (sr_scpi_get_string(sdi->conn, g_strdup_printf("%s:TRDL?", devc->trigger_source), &response) != SR_OK)
804 // len = strlen(response);
805 len = strlen(tokens[4]);
806 if (!g_ascii_strcasecmp(tokens[4] + (len - 2), "us")) {
807 trigger_pos = atof(tokens[4]) / SR_GHZ(1);
808 sr_dbg("Current trigger position us %s.", tokens[4] );
809 } else if (!g_ascii_strcasecmp(tokens[4] + (len - 2), "ns")) {
810 trigger_pos = atof(tokens[4]) / SR_MHZ(1);
811 sr_dbg("Current trigger position ms %s.", tokens[4] );
812 } else if (!g_ascii_strcasecmp(tokens[4] + (len - 2), "ms")) {
813 trigger_pos = atof(tokens[4]) / SR_KHZ(1);
814 sr_dbg("Current trigger position ns %s.", tokens[4] );
815 } else if (!g_ascii_strcasecmp(tokens[4] + (len - 2), "s")) {
816 trigger_pos = atof(tokens[4]);
817 sr_dbg("Current trigger position s %s.", tokens[4] );
819 devc->horiz_triggerpos = trigger_pos;
821 sr_dbg("Current horizontal trigger position %.10f.", devc->horiz_triggerpos);
824 cmd = g_strdup_printf("%s:TRSL?", devc->trigger_source);
825 res = sr_scpi_get_string(sdi->conn, cmd, &devc->trigger_slope);
829 sr_dbg("Current trigger slope: %s.", devc->trigger_slope);
831 /* Trigger level, only when analog channel. */
832 if (g_str_has_prefix(tokens[2], "C")) {
833 cmd = g_strdup_printf("%s:TRLV?", devc->trigger_source);
834 res = sr_scpi_get_float(sdi->conn, cmd, &devc->trigger_level);
838 sr_dbg("Current trigger level: %g.", devc->trigger_level);
844 SR_PRIV int siglent_sds_get_dev_cfg_vertical(const struct sr_dev_inst *sdi)
846 struct dev_context *devc;
854 for (i = 0; i < devc->model->analog_channels; i++) {
855 cmd = g_strdup_printf("C%d:VDIV?", i + 1);
856 res = sr_scpi_get_float(sdi->conn, cmd, &devc->vdiv[i]);
861 sr_dbg("Current vertical gain:");
862 for (i = 0; i < devc->model->analog_channels; i++)
863 sr_dbg("CH%d %g", i + 1, devc->vdiv[i]);
865 /* Vertical offset. */
866 for (i = 0; i < devc->model->analog_channels; i++) {
867 cmd = g_strdup_printf("C%d:OFST?", i + 1);
868 res = sr_scpi_get_float(sdi->conn, cmd, &devc->vert_offset[i]);
873 sr_dbg("Current vertical offset:");
874 for (i = 0; i < devc->model->analog_channels; i++)
875 sr_dbg("CH%d %g", i + 1, devc->vert_offset[i]);
880 SR_PRIV int siglent_sds_get_dev_cfg_horizontal(const struct sr_dev_inst *sdi)
882 struct dev_context *devc;
885 char *sample_points_string;
886 float samplerate_scope, fvalue;
890 switch (devc->model->series->protocol) {
893 cmd = g_strdup_printf("SANU? C1");
894 res = sr_scpi_get_string(sdi->conn, cmd, &sample_points_string);
896 samplerate_scope = 0;
900 if (g_strstr_len(sample_points_string, -1, "Mpts") != NULL) {
901 sample_points_string[strlen(sample_points_string) - 4] = '\0';
902 if (sr_atof_ascii(sample_points_string, &fvalue) != SR_OK) {
903 sr_dbg("Invalid float converted from scope response.");
906 samplerate_scope = fvalue * 1000000;
907 } else if (g_strstr_len(sample_points_string, -1, "Kpts") != NULL) {
908 sample_points_string[strlen(sample_points_string) - 4] = '\0';
909 if (sr_atof_ascii(sample_points_string, &fvalue) != SR_OK) {
910 sr_dbg("Invalid float converted from scope response.");
913 samplerate_scope = fvalue * 10000;
915 samplerate_scope = fvalue;
917 g_free(sample_points_string);
918 devc->memory_depth_analog = samplerate_scope;
921 cmd = g_strdup_printf("SANU? C1");
922 if (sr_scpi_get_float(sdi->conn, cmd, &fvalue) != SR_OK)
924 devc->memory_depth_analog = (long)fvalue;
925 if (devc->la_enabled) {
926 cmd = g_strdup_printf("SANU? D0");
927 if (sr_scpi_get_float(sdi->conn, cmd, &fvalue) != SR_OK)
929 devc->memory_depth_digital = (long)fvalue;
935 /* Get the timebase. */
936 if (sr_scpi_get_float(sdi->conn, ":TDIV?", &devc->timebase) != SR_OK)
939 sr_dbg("Current timebase: %g.", devc->timebase);
940 devc->samplerate = devc->memory_depth_analog / (devc->timebase * devc->model->series->num_horizontal_divs);
941 sr_dbg("Current samplerate: %0f.", devc->samplerate);
942 sr_dbg("Current memory depth: %lu.", devc->memory_depth_analog);