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)
82 sr_dbg("Device triggered.");
84 if ((devc->timebase < 0.51) && (devc->timebase > 0.99e-6)) {
86 * Timebase * num hor. divs * 85(%) * 1e6(usecs) / 100
87 * -> 85 percent of sweep time
89 s = (devc->timebase * devc->model->series->num_horizontal_divs * 1000);
90 sr_spew("Sleeping for %ld usecs after trigger, "
91 "to let the acq buffer in the device fill", s);
95 if (devc->wait_status == 2) {
97 if (time(NULL) - start >= 3) {
98 sr_dbg("Timeout waiting for trigger.");
99 return SR_ERR_TIMEOUT;
101 if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
107 * Now this loop condition looks suspicious! A bitwise
108 * OR of a variable and a non-zero literal should be
109 * non-zero. Logical AND of several non-zero values
110 * should be non-zero. Are many parts of the condition
111 * not taking effect? Was some different condition meant
112 * to get encoded? This needs review, and adjustment.
114 } while (out != DEVICE_STATE_TRIG_RDY || out != DEVICE_STATE_DATA_TRIG_RDY || out != DEVICE_STATE_STOPPED);
116 sr_dbg("Device triggered.");
118 siglent_sds_set_wait_event(devc, WAIT_NONE);
124 static int siglent_sds_trigger_wait(const struct sr_dev_inst *sdi)
126 struct dev_context *devc;
128 if (!(devc = sdi->priv))
130 return siglent_sds_event_wait(sdi);
133 /* Wait for scope to got to "Stop" in single shot mode. */
134 static int siglent_sds_stop_wait(const struct sr_dev_inst *sdi)
136 return siglent_sds_event_wait(sdi);
139 /* Send a configuration setting. */
140 SR_PRIV int siglent_sds_config_set(const struct sr_dev_inst *sdi, const char *format, ...)
145 va_start(args, format);
146 ret = sr_scpi_send_variadic(sdi->conn, format, args);
152 /* Start capturing a new frameset. */
153 SR_PRIV int siglent_sds_capture_start(const struct sr_dev_inst *sdi)
155 struct dev_context *devc;
157 if (!(devc = sdi->priv))
160 switch (devc->model->series->protocol) {
162 if (devc->data_source == DATA_SOURCE_SCREEN) {
166 sr_dbg("Starting data capture for active frameset %" PRIu64 " of %" PRIu64,
167 devc->num_frames + 1, devc->limit_frames);
168 if (siglent_sds_config_set(sdi, "ARM") != SR_OK)
170 if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
174 if (out == DEVICE_STATE_TRIG_RDY) {
175 siglent_sds_set_wait_event(devc, WAIT_TRIGGER);
176 } else if (out == DEVICE_STATE_DATA_TRIG_RDY) {
177 sr_spew("Device triggered.");
178 siglent_sds_set_wait_event(devc, WAIT_BLOCK);
181 sr_spew("Device did not enter ARM mode.");
184 } else { /* TODO: Implement history retrieval. */
185 unsigned int framecount;
189 sr_dbg("Starting data capture for history frameset.");
190 if (siglent_sds_config_set(sdi, "FPAR?") != SR_OK)
192 ret = sr_scpi_read_data(sdi->conn, buf, 200);
194 sr_err("Read error while reading data header.");
197 memcpy(&framecount, buf + 40, 4);
198 if (devc->limit_frames > framecount)
199 sr_err("Frame limit higher than frames in buffer of device!");
200 else if (devc->limit_frames == 0)
201 devc->limit_frames = framecount;
202 sr_dbg("Starting data capture for history frameset %" PRIu64 " of %" PRIu64,
203 devc->num_frames + 1, devc->limit_frames);
204 if (siglent_sds_config_set(sdi, "FRAM %i", devc->num_frames + 1) != SR_OK)
206 if (siglent_sds_channel_start(sdi) != SR_OK)
208 siglent_sds_set_wait_event(devc, WAIT_STOP);
212 if (devc->data_source == DATA_SOURCE_SCREEN) {
216 sr_dbg("Starting data capture for active frameset %" PRIu64 " of %" PRIu64,
217 devc->num_frames + 1, devc->limit_frames);
218 if (siglent_sds_config_set(sdi, "ARM") != SR_OK)
220 if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
224 if (out == DEVICE_STATE_TRIG_RDY) {
225 siglent_sds_set_wait_event(devc, WAIT_TRIGGER);
226 } else if (out == DEVICE_STATE_DATA_TRIG_RDY) {
227 sr_spew("Device triggered.");
228 siglent_sds_set_wait_event(devc, WAIT_BLOCK);
231 sr_spew("Device did not enter ARM mode.");
234 } else { /* TODO: Implement history retrieval. */
235 unsigned int framecount;
239 sr_dbg("Starting data capture for history frameset.");
240 if (siglent_sds_config_set(sdi, "FPAR?") != SR_OK)
242 ret = sr_scpi_read_data(sdi->conn, buf, 200);
244 sr_err("Read error while reading data header.");
247 memcpy(&framecount, buf + 40, 4);
248 if (devc->limit_frames > framecount)
249 sr_err("Frame limit higher than frames in buffer of device!");
250 else if (devc->limit_frames == 0)
251 devc->limit_frames = framecount;
252 sr_dbg("Starting data capture for history frameset %" PRIu64 " of %" PRIu64,
253 devc->num_frames + 1, devc->limit_frames);
254 if (siglent_sds_config_set(sdi, "FRAM %i", devc->num_frames + 1) != SR_OK)
256 if (siglent_sds_channel_start(sdi) != SR_OK)
258 siglent_sds_set_wait_event(devc, WAIT_STOP);
262 siglent_sds_set_wait_event(devc, WAIT_TRIGGER);
269 /* Start reading data from the current channel. */
270 SR_PRIV int siglent_sds_channel_start(const struct sr_dev_inst *sdi)
272 struct dev_context *devc;
273 struct sr_channel *ch;
276 if (!(devc = sdi->priv))
279 ch = devc->channel_entry->data;
281 sr_dbg("Start reading data from channel %s.", ch->name);
283 switch (devc->model->series->protocol) {
286 s = (ch->type == SR_CHANNEL_LOGIC) ? "D%d:WF?" : "C%d:WF? ALL";
287 if (sr_scpi_send(sdi->conn, s, ch->index + 1) != SR_OK)
289 siglent_sds_set_wait_event(devc, WAIT_NONE);
292 if (ch->type == SR_CHANNEL_ANALOG) {
293 if (sr_scpi_send(sdi->conn, "C%d:WF? ALL",
294 ch->index + 1) != SR_OK)
297 siglent_sds_set_wait_event(devc, WAIT_NONE);
298 if (sr_scpi_read_begin(sdi->conn) != SR_OK)
300 siglent_sds_set_wait_event(devc, WAIT_BLOCK);
304 devc->num_channel_bytes = 0;
305 devc->num_header_bytes = 0;
306 devc->num_block_bytes = 0;
311 /* Read the header of a data block. */
312 static int siglent_sds_read_header(struct sr_dev_inst *sdi)
314 struct sr_scpi_dev_inst *scpi = sdi->conn;
315 struct dev_context *devc = sdi->priv;
316 char *buf = (char *)devc->buffer;
317 int ret, desc_length;
318 int block_offset = 15; /* Offset for descriptor block. */
319 long data_length = 0;
321 /* Read header from device. */
322 ret = sr_scpi_read_data(scpi, buf, SIGLENT_HEADER_SIZE);
323 if (ret < SIGLENT_HEADER_SIZE) {
324 sr_err("Read error while reading data header.");
327 sr_dbg("Device returned %i bytes.", ret);
328 devc->num_header_bytes += ret;
329 buf += block_offset; /* Skip to start descriptor block. */
331 /* Parse WaveDescriptor header. */
332 memcpy(&desc_length, buf + 36, 4); /* Descriptor block length */
333 memcpy(&data_length, buf + 60, 4); /* Data block length */
335 devc->block_header_size = desc_length + 15;
336 devc->num_samples = data_length;
338 sr_dbg("Received data block header: '%s' -> block length %d.", buf, ret);
343 static int siglent_sds_get_digital(const struct sr_dev_inst *sdi, struct sr_channel *ch)
345 struct sr_scpi_dev_inst *scpi = sdi->conn;
346 struct dev_context *devc = sdi->priv;
347 GArray *tmp_samplebuf; /* Temp buffer while iterating over the scope samples */
348 char *buf = (char *)devc->buffer; /* Buffer from scope */
349 uint8_t tmp_value; /* Holding temp value from data */
350 GArray *data_low_channels, *data_high_channels, *buffdata;
352 gboolean low_channels; /* Lower channels enabled */
353 gboolean high_channels; /* Higher channels enabled */
354 int len, channel_index;
355 uint64_t samples_index;
359 low_channels = FALSE;
360 high_channels = FALSE;
361 data_low_channels = g_array_new(FALSE, TRUE, sizeof(uint8_t));
362 data_high_channels = g_array_new(FALSE, TRUE, sizeof(uint8_t));
364 for (l = sdi->channels; l; l = l->next) {
367 if (ch->type == SR_CHANNEL_LOGIC) {
369 if (sr_scpi_send(sdi->conn, "D%d:WF? DAT2", ch->index) != SR_OK)
371 if (sr_scpi_read_begin(scpi) != SR_OK)
373 len = sr_scpi_read_data(scpi, buf, -1);
377 buffdata = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len);
378 buf += 15; /* Skipping the data header. */
379 g_array_append_vals(buffdata, buf, len);
380 tmp_samplebuf = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len); /* New temp buffer. */
381 for (uint64_t cur_sample_index = 0; cur_sample_index < devc->memory_depth_digital; cur_sample_index++) {
382 char sample = (char)g_array_index(buffdata, uint8_t, cur_sample_index);
383 for (int ii = 0; ii < 8; ii++, sample >>= 1) {
385 channel_index = ch->index;
386 if (data_low_channels->len <= samples_index) {
387 tmp_value = 0; /* New sample. */
388 low_channels = TRUE; /* We have at least one enabled low channel. */
390 /* Get previous stored sample from low channel buffer. */
391 tmp_value = g_array_index(data_low_channels, uint8_t, samples_index);
394 channel_index = ch->index - 8;
395 if (data_high_channels->len <= samples_index) {
396 tmp_value = 0; /* New sample. */
397 high_channels = TRUE; /* We have at least one enabled high channel. */
399 /* Get previous stored sample from high channel buffer. */
400 tmp_value = g_array_index(data_high_channels, uint8_t, samples_index);
403 /* Check if the current scope sample bit is set. */
405 tmp_value |= 1UL << channel_index; /* Set current scope sample bit based on channel index. */
406 g_array_append_val(tmp_samplebuf, tmp_value);
411 /* Clear the buffers to prepare for the new samples */
413 g_array_free(data_low_channels, FALSE);
414 data_low_channels = g_array_new(FALSE, FALSE, sizeof(uint8_t));
416 g_array_free(data_high_channels, FALSE);
417 data_high_channels = g_array_new(FALSE, FALSE, sizeof(uint8_t));
420 /* Storing the converted temp values from the the scope into the buffers. */
421 for (uint64_t index = 0; index < tmp_samplebuf->len; index++) {
422 uint8_t value = g_array_index(tmp_samplebuf, uint8_t, index);
424 g_array_append_val(data_low_channels, value);
426 g_array_append_val(data_high_channels, value);
428 g_array_free(tmp_samplebuf, TRUE);
429 g_array_free(buffdata, TRUE);
434 /* Combining the lower and higher channel buffers into one buffer for sigrok. */
435 devc->dig_buffer = g_array_new(FALSE, FALSE, sizeof(uint8_t));
436 for (uint64_t index = 0; index < devc->memory_depth_digital; index++) {
439 value = g_array_index(data_low_channels, uint8_t, index);
440 g_array_append_val(devc->dig_buffer, value);
443 g_array_append_val(devc->dig_buffer, value);
446 value = g_array_index(data_high_channels, uint8_t, index);
447 g_array_append_val(devc->dig_buffer, value);
450 g_array_append_val(devc->dig_buffer, value);
454 g_array_free(data_low_channels, TRUE);
455 g_array_free(data_high_channels, TRUE);
460 SR_PRIV int siglent_sds_receive(int fd, int revents, void *cb_data)
462 struct sr_dev_inst *sdi;
463 struct sr_scpi_dev_inst *scpi;
464 struct dev_context *devc;
465 struct sr_datafeed_packet packet;
466 struct sr_datafeed_analog analog;
467 struct sr_analog_encoding encoding;
468 struct sr_analog_meaning meaning;
469 struct sr_analog_spec spec;
470 struct sr_datafeed_logic logic;
471 struct sr_channel *ch;
474 gboolean read_complete = FALSE;
478 if (!(sdi = cb_data))
481 if (!(devc = sdi->priv))
486 if (!(revents == G_IO_IN || revents == 0))
489 switch (devc->wait_event) {
493 if (siglent_sds_trigger_wait(sdi) != SR_OK)
495 if (siglent_sds_channel_start(sdi) != SR_OK)
499 if (siglent_sds_channel_start(sdi) != SR_OK)
503 if (siglent_sds_stop_wait(sdi) != SR_OK)
505 if (siglent_sds_channel_start(sdi) != SR_OK)
509 sr_err("BUG: Unknown event target encountered.");
513 ch = devc->channel_entry->data;
516 if (ch->type == SR_CHANNEL_ANALOG) {
517 if (devc->num_block_bytes == 0) {
518 /* Wait for the device to fill its output buffers. */
519 switch (devc->model->series->protocol) {
522 /* The older models need more time to prepare the the output buffers due to CPU speed. */
523 wait = (devc->memory_depth_analog * 2.5);
524 sr_dbg("Waiting %.f0 ms for device to prepare the output buffers", wait / 1000);
526 if (sr_scpi_read_begin(scpi) != SR_OK)
530 /* The newer models (ending with the E) have faster CPUs but still need time when a slow timebase is selected. */
531 if (sr_scpi_read_begin(scpi) != SR_OK)
533 wait = ((devc->timebase * devc->model->series->num_horizontal_divs) * 100000);
534 sr_dbg("Waiting %.f0 ms for device to prepare the output buffers", wait / 1000);
539 sr_dbg("New block with header expected.");
540 len = siglent_sds_read_header(sdi);
542 /* Still reading the header. */
545 sr_err("Read error, aborting capture.");
546 std_session_send_df_frame_end(sdi);
547 sdi->driver->dev_acquisition_stop(sdi);
550 devc->num_block_bytes = len;
551 devc->num_block_read = 0;
554 sr_err("Read error, aborting capture.");
555 std_session_send_df_frame_end(sdi);
556 sdi->driver->dev_acquisition_stop(sdi);
561 read_complete = FALSE;
562 if (devc->num_block_bytes > devc->num_samples) {
563 /* We received all data as one block. */
564 /* Offset the data block buffer past the IEEE header and description header. */
565 devc->buffer += devc->block_header_size;
566 len = devc->num_samples;
568 sr_dbg("Requesting: %" PRIu64 " bytes.", devc->num_samples - devc->num_block_bytes);
569 len = sr_scpi_read_data(scpi, (char *)devc->buffer, devc->num_samples-devc->num_block_bytes);
571 sr_err("Read error, aborting capture.");
572 std_session_send_df_frame_end(sdi);
573 sdi->driver->dev_acquisition_stop(sdi);
576 devc->num_block_read++;
577 devc->num_block_bytes += len;
579 sr_dbg("Received block: %i, %d bytes.", devc->num_block_read, len);
580 if (ch->type == SR_CHANNEL_ANALOG) {
581 float vdiv = devc->vdiv[ch->index];
582 float offset = devc->vert_offset[ch->index];
585 float voltage, vdivlog;
588 data = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len);
589 g_array_append_vals(data, devc->buffer, len);
590 float_data = g_array_new(FALSE, FALSE, sizeof(float));
591 for (i = 0; i < len; i++) {
592 voltage = (float)g_array_index(data, int8_t, i) / 25;
593 voltage = ((vdiv * voltage) - offset);
594 g_array_append_val(float_data, voltage);
596 vdivlog = log10f(vdiv);
597 digits = -(int) vdivlog + (vdivlog < 0.0);
598 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
599 analog.meaning->channels = g_slist_append(NULL, ch);
600 analog.num_samples = float_data->len;
601 analog.data = (float *)float_data->data;
602 analog.meaning->mq = SR_MQ_VOLTAGE;
603 analog.meaning->unit = SR_UNIT_VOLT;
604 analog.meaning->mqflags = 0;
605 packet.type = SR_DF_ANALOG;
606 packet.payload = &analog;
607 sr_session_send(sdi, &packet);
608 g_slist_free(analog.meaning->channels);
609 g_array_free(data, TRUE);
612 if (devc->num_samples == (devc->num_block_bytes - SIGLENT_HEADER_SIZE)) {
613 sr_dbg("Transfer has been completed.");
614 devc->num_header_bytes = 0;
615 devc->num_block_bytes = 0;
616 read_complete = TRUE;
617 if (!sr_scpi_read_complete(scpi)) {
618 sr_err("Read should have been completed.");
619 std_session_send_df_frame_end(sdi);
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 std_session_send_df_frame_end(sdi);
637 if (++devc->num_frames == devc->limit_frames) {
638 /* Last frame, stop capture. */
639 sdi->driver->dev_acquisition_stop(sdi);
641 /* Get the next frame, starting with the first channel. */
642 devc->channel_entry = devc->enabled_channels;
643 siglent_sds_capture_start(sdi);
645 /* Start of next frame. */
646 std_session_send_df_frame_begin(sdi);
651 if (!siglent_sds_get_digital(sdi, ch))
653 logic.length = devc->dig_buffer->len;
655 logic.data = devc->dig_buffer->data;
656 packet.type = SR_DF_LOGIC;
657 packet.payload = &logic;
658 sr_session_send(sdi, &packet);
659 std_session_send_df_frame_end(sdi);
660 sdi->driver->dev_acquisition_stop(sdi);
662 if (++devc->num_frames == devc->limit_frames) {
663 /* Last frame, stop capture. */
664 sdi->driver->dev_acquisition_stop(sdi);
666 /* Get the next frame, starting with the first channel. */
667 devc->channel_entry = devc->enabled_channels;
668 siglent_sds_capture_start(sdi);
670 /* Start of next frame. */
671 std_session_send_df_frame_begin(sdi);
675 // sr_session_send(sdi, &packet);
676 // std_session_send_df_frame_end(sdi);
677 // sdi->driver->dev_acquisition_stop(sdi);
682 SR_PRIV int siglent_sds_get_dev_cfg(const struct sr_dev_inst *sdi)
684 struct dev_context *devc;
685 struct sr_channel *ch;
686 char *cmd, *response;
695 /* Analog channel state. */
696 for (i = 0; i < devc->model->analog_channels; i++) {
697 cmd = g_strdup_printf("C%i:TRA?", i + 1);
698 res = sr_scpi_get_bool(sdi->conn, cmd, &devc->analog_channels[i]);
702 ch = g_slist_nth_data(sdi->channels, i);
703 ch->enabled = devc->analog_channels[i];
705 sr_dbg("Current analog channel state:");
706 for (i = 0; i < devc->model->analog_channels; i++)
707 sr_dbg("CH%d %s", i + 1, devc->analog_channels[i] ? "On" : "Off");
709 /* Digital channel state. */
710 if (devc->model->has_digital) {
713 sr_dbg("Check logic analyzer channel state.");
714 devc->la_enabled = FALSE;
715 cmd = g_strdup_printf("DI:SW?");
716 res = sr_scpi_get_bool(sdi->conn, cmd, &status);
720 sr_dbg("Logic analyzer status: %s", status ? "On" : "Off");
722 devc->la_enabled = TRUE;
723 for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) {
724 cmd = g_strdup_printf("D%i:TRA?", i);
725 res = sr_scpi_get_bool(sdi->conn, cmd, &devc->digital_channels[i]);
729 ch = g_slist_nth_data(sdi->channels, i + devc->model->analog_channels);
730 ch->enabled = devc->digital_channels[i];
731 sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "On" : "Off");
734 for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) {
735 ch = g_slist_nth_data(sdi->channels, i + devc->model->analog_channels);
736 devc->digital_channels[i] = FALSE;
737 ch->enabled = devc->digital_channels[i];
738 sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "On" : "Off");
744 if (sr_scpi_get_float(sdi->conn, ":TDIV?", &devc->timebase) != SR_OK)
746 sr_dbg("Current timebase: %g.", devc->timebase);
748 /* Probe attenuation. */
749 for (i = 0; i < devc->model->analog_channels; i++) {
750 cmd = g_strdup_printf("C%d:ATTN?", i + 1);
751 res = sr_scpi_get_float(sdi->conn, cmd, &devc->attenuation[i]);
756 sr_dbg("Current probe attenuation:");
757 for (i = 0; i < devc->model->analog_channels; i++)
758 sr_dbg("CH%d %g", i + 1, devc->attenuation[i]);
760 /* Vertical gain and offset. */
761 if (siglent_sds_get_dev_cfg_vertical(sdi) != SR_OK)
765 for (i = 0; i < devc->model->analog_channels; i++) {
766 cmd = g_strdup_printf("C%d:CPL?", i + 1);
767 g_free(devc->coupling[i]);
768 devc->coupling[i] = NULL;
769 res = sr_scpi_get_string(sdi->conn, cmd, &devc->coupling[i]);
775 sr_dbg("Current coupling:");
776 for (i = 0; i < devc->model->analog_channels; i++)
777 sr_dbg("CH%d %s", i + 1, devc->coupling[i]);
779 /* Trigger source. */
782 if (sr_scpi_get_string(sdi->conn, "TRSE?", &response) != SR_OK)
784 tokens = g_strsplit(response, ",", 0);
785 num_tokens = g_strv_length(tokens);
786 if (num_tokens < 4) {
787 sr_dbg("IDN response not according to spec: %80.s.", response);
793 devc->trigger_source = g_strstrip(g_strdup(tokens[2]));
794 sr_dbg("Current trigger source: %s.", devc->trigger_source);
796 /* TODO: Horizontal trigger position. */
799 // if (sr_scpi_get_string(sdi->conn, g_strdup_printf("%s:TRDL?", devc->trigger_source), &response) != SR_OK)
801 // len = strlen(response);
802 len = strlen(tokens[4]);
803 if (!g_ascii_strcasecmp(tokens[4] + (len - 2), "us")) {
804 trigger_pos = atof(tokens[4]) / SR_GHZ(1);
805 sr_dbg("Current trigger position us %s.", tokens[4] );
806 } else if (!g_ascii_strcasecmp(tokens[4] + (len - 2), "ns")) {
807 trigger_pos = atof(tokens[4]) / SR_MHZ(1);
808 sr_dbg("Current trigger position ms %s.", tokens[4] );
809 } else if (!g_ascii_strcasecmp(tokens[4] + (len - 2), "ms")) {
810 trigger_pos = atof(tokens[4]) / SR_KHZ(1);
811 sr_dbg("Current trigger position ns %s.", tokens[4] );
812 } else if (!g_ascii_strcasecmp(tokens[4] + (len - 2), "s")) {
813 trigger_pos = atof(tokens[4]);
814 sr_dbg("Current trigger position s %s.", tokens[4] );
816 devc->horiz_triggerpos = trigger_pos;
818 sr_dbg("Current horizontal trigger position %.10f.", devc->horiz_triggerpos);
821 cmd = g_strdup_printf("%s:TRSL?", devc->trigger_source);
822 g_free(devc->trigger_slope);
823 devc->trigger_slope = NULL;
824 res = sr_scpi_get_string(sdi->conn, cmd, &devc->trigger_slope);
828 sr_dbg("Current trigger slope: %s.", devc->trigger_slope);
830 /* Trigger level, only when analog channel. */
831 if (g_str_has_prefix(tokens[2], "C")) {
832 cmd = g_strdup_printf("%s:TRLV?", devc->trigger_source);
833 res = sr_scpi_get_float(sdi->conn, cmd, &devc->trigger_level);
837 sr_dbg("Current trigger level: %g.", devc->trigger_level);
843 SR_PRIV int siglent_sds_get_dev_cfg_vertical(const struct sr_dev_inst *sdi)
845 struct dev_context *devc;
853 for (i = 0; i < devc->model->analog_channels; i++) {
854 cmd = g_strdup_printf("C%d:VDIV?", i + 1);
855 res = sr_scpi_get_float(sdi->conn, cmd, &devc->vdiv[i]);
860 sr_dbg("Current vertical gain:");
861 for (i = 0; i < devc->model->analog_channels; i++)
862 sr_dbg("CH%d %g", i + 1, devc->vdiv[i]);
864 /* Vertical offset. */
865 for (i = 0; i < devc->model->analog_channels; i++) {
866 cmd = g_strdup_printf("C%d:OFST?", i + 1);
867 res = sr_scpi_get_float(sdi->conn, cmd, &devc->vert_offset[i]);
872 sr_dbg("Current vertical offset:");
873 for (i = 0; i < devc->model->analog_channels; i++)
874 sr_dbg("CH%d %g", i + 1, devc->vert_offset[i]);
879 SR_PRIV int siglent_sds_get_dev_cfg_horizontal(const struct sr_dev_inst *sdi)
881 struct dev_context *devc;
884 char *sample_points_string;
885 float samplerate_scope, fvalue;
889 switch (devc->model->series->protocol) {
892 cmd = g_strdup_printf("SANU? C1");
893 res = sr_scpi_get_string(sdi->conn, cmd, &sample_points_string);
895 samplerate_scope = 0;
898 g_free(sample_points_string);
901 if (g_strstr_len(sample_points_string, -1, "Mpts") != NULL) {
902 sample_points_string[strlen(sample_points_string) - 4] = '\0';
903 if (sr_atof_ascii(sample_points_string, &fvalue) != SR_OK) {
904 sr_dbg("Invalid float converted from scope response.");
905 g_free(sample_points_string);
908 samplerate_scope = fvalue * 1000000;
909 } else if (g_strstr_len(sample_points_string, -1, "Kpts") != NULL) {
910 sample_points_string[strlen(sample_points_string) - 4] = '\0';
911 if (sr_atof_ascii(sample_points_string, &fvalue) != SR_OK) {
912 sr_dbg("Invalid float converted from scope response.");
913 g_free(sample_points_string);
916 samplerate_scope = fvalue * 10000;
918 samplerate_scope = fvalue;
920 g_free(sample_points_string);
921 devc->memory_depth_analog = samplerate_scope;
924 cmd = g_strdup_printf("SANU? C1");
925 if (sr_scpi_get_float(sdi->conn, cmd, &fvalue) != SR_OK)
927 devc->memory_depth_analog = (long)fvalue;
928 if (devc->la_enabled) {
929 cmd = g_strdup_printf("SANU? D0");
930 if (sr_scpi_get_float(sdi->conn, cmd, &fvalue) != SR_OK)
932 devc->memory_depth_digital = (long)fvalue;
938 /* Get the timebase. */
939 if (sr_scpi_get_float(sdi->conn, ":TDIV?", &devc->timebase) != SR_OK)
942 sr_dbg("Current timebase: %g.", devc->timebase);
943 devc->samplerate = devc->memory_depth_analog / (devc->timebase * devc->model->series->num_horizontal_divs);
944 sr_dbg("Current samplerate: %0f.", devc->samplerate);
945 sr_dbg("Current memory depth: %" PRIu64 ".", devc->memory_depth_analog);