2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Martin Ling <martin-git@earth.li>
5 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
6 * Copyright (C) 2013 Mathias Grimmberger <mgri@zaphod.sax.de>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "libsigrok.h"
32 #include "libsigrok-internal.h"
36 * This is a unified protocol driver for the DS1000 and DS2000 series.
38 * DS1000 support tested with a Rigol DS1102D.
40 * DS2000 support tested with a Rigol DS2072 using firmware version 01.01.00.02.
42 * The Rigol DS2000 series scopes try to adhere to the IEEE 488.2 (I think)
43 * standard. If you want to read it - it costs real money...
45 * Every response from the scope has a linefeed appended because the
46 * standard says so. In principle this could be ignored because sending the
47 * next command clears the output queue of the scope. This driver tries to
48 * avoid doing that because it may cause an error being generated inside the
49 * scope and who knows what bugs the firmware has WRT this.
51 * Waveform data is transferred in a format called "arbitrary block program
52 * data" specified in IEEE 488.2. See Agilents programming manuals for their
53 * 2000/3000 series scopes for a nice description.
55 * Each data block from the scope has a header, e.g. "#900000001400".
56 * The '#' marks the start of a block.
57 * Next is one ASCII decimal digit between 1 and 9, this gives the number of
58 * ASCII decimal digits following.
59 * Last are the ASCII decimal digits giving the number of bytes (not
60 * samples!) in the block.
62 * After this header as many data bytes as indicated follow.
64 * Each data block has a trailing linefeed too.
67 static int parse_int(const char *str, int *ret)
73 tmp = strtol(str, &e, 10);
74 if (e == str || *e != '\0') {
75 sr_dbg("Failed to parse integer: '%s'", str);
79 sr_dbg("Failed to parse integer: '%s', numerical overflow", str);
82 if (tmp > INT_MAX || tmp < INT_MIN) {
83 sr_dbg("Failed to parse integer: '%s', value to large/small", str);
91 /* Set the next event to wait for in rigol_ds_receive */
92 static void rigol_ds_set_wait_event(struct dev_context *devc, enum wait_events event)
94 if (event == WAIT_STOP)
95 devc->wait_status = 2;
97 devc->wait_status = 1;
98 devc->wait_event = event;
102 * Waiting for a event will return a timeout after 2 to 3 seconds in order
103 * to not block the application.
105 static int rigol_ds_event_wait(const struct sr_dev_inst *sdi, char status1, char status2)
108 struct dev_context *devc;
111 if (!(devc = sdi->priv))
117 * Trigger status may return:
118 * "TD" or "T'D" - triggered
119 * "AUTO" - autotriggered
121 * "WAIT" - waiting for trigger
125 if (devc->wait_status == 1) {
127 if (time(NULL) - start >= 3) {
128 sr_dbg("Timeout waiting for trigger");
129 return SR_ERR_TIMEOUT;
132 if (sr_scpi_get_string(sdi->conn, ":TRIG:STAT?", &buf) != SR_OK)
134 } while (buf[0] == status1 || buf[0] == status2);
136 devc->wait_status = 2;
138 if (devc->wait_status == 2) {
140 if (time(NULL) - start >= 3) {
141 sr_dbg("Timeout waiting for trigger");
142 return SR_ERR_TIMEOUT;
145 if (sr_scpi_get_string(sdi->conn, ":TRIG:STAT?", &buf) != SR_OK)
147 } while (buf[0] != status1 && buf[0] != status2);
149 rigol_ds_set_wait_event(devc, WAIT_NONE);
156 * For live capture we need to wait for a new trigger event to ensure that
157 * sample data is not returned twice.
159 * Unfortunately this will never really work because for sufficiently fast
160 * timebases and trigger rates it just can't catch the status changes.
162 * What would be needed is a trigger event register with autoreset like the
163 * Agilents have. The Rigols don't seem to have anything like this.
165 * The workaround is to only wait for the trigger when the timebase is slow
166 * enough. Of course this means that for faster timebases sample data can be
167 * returned multiple times, this effect is mitigated somewhat by sleeping
168 * for about one sweep time in that case.
170 static int rigol_ds_trigger_wait(const struct sr_dev_inst *sdi)
172 struct dev_context *devc;
175 if (!(devc = sdi->priv))
179 * If timebase < 50 msecs/DIV just sleep about one sweep time except
180 * for really fast sweeps.
182 if (devc->timebase < 0.0499) {
183 if (devc->timebase > 0.99e-6) {
185 * Timebase * num hor. divs * 85(%) * 1e6(usecs) / 100
186 * -> 85 percent of sweep time
188 s = (devc->timebase * devc->model->num_horizontal_divs
190 sr_spew("Sleeping for %ld usecs instead of trigger-wait", s);
193 rigol_ds_set_wait_event(devc, WAIT_NONE);
196 return rigol_ds_event_wait(sdi, 'T', 'A');
200 /* Wait for scope to got to "Stop" in single shot mode */
201 static int rigol_ds_stop_wait(const struct sr_dev_inst *sdi)
203 return rigol_ds_event_wait(sdi, 'S', 'S');
206 /* Check that a single shot acquisition actually succeeded on the DS2000 */
207 static int rigol_ds_check_stop(const struct sr_dev_inst *sdi)
209 struct dev_context *devc;
210 struct sr_probe *probe;
213 if (!(devc = sdi->priv))
216 probe = devc->channel_entry->data;
218 if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d",
219 probe->index + 1) != SR_OK)
221 /* Check that the number of samples will be accepted */
222 if (sr_scpi_send(sdi->conn, ":WAV:POIN %d;*OPC", devc->analog_frame_size) != SR_OK)
224 if (sr_scpi_get_int(sdi->conn, "*ESR?", &tmp) != SR_OK)
227 * If we get an "Execution error" the scope went from "Single" to
228 * "Stop" without actually triggering. There is no waveform
229 * displayed and trying to download one will fail - the scope thinks
230 * it has 1400 samples (like display memory) and the driver thinks
231 * it has a different number of samples.
233 * In that case just try to capture something again. Might still
234 * fail in interesting ways.
236 * Ain't firmware fun?
239 sr_warn("Single shot acquisition failed, retrying...");
240 /* Sleep a bit, otherwise the single shot will often fail */
242 sr_scpi_send(sdi->conn, ":SING");
243 rigol_ds_set_wait_event(devc, WAIT_STOP);
250 /* Wait for enough data becoming available in scope output buffer */
251 static int rigol_ds_block_wait(const struct sr_dev_inst *sdi)
254 struct dev_context *devc;
258 if (!(devc = sdi->priv))
264 if (time(NULL) - start >= 3) {
265 sr_dbg("Timeout waiting for data block");
266 return SR_ERR_TIMEOUT;
270 * The scope copies data really slowly from sample
271 * memory to its output buffer, so try not to bother
272 * it too much with SCPI requests but don't wait too
273 * long for short sample frame sizes.
275 g_usleep(devc->analog_frame_size < 15000 ? 100000 : 1000000);
277 /* "READ,nnnn" (still working) or "IDLE,nnnn" (finished) */
278 if (sr_scpi_get_string(sdi->conn, ":WAV:STAT?", &buf) != SR_OK)
281 if (parse_int(buf + 5, &len) != SR_OK)
283 } while (buf[0] == 'R' && len < 1000000);
285 rigol_ds_set_wait_event(devc, WAIT_NONE);
290 /* Start capturing a new frameset */
291 SR_PRIV int rigol_ds_capture_start(const struct sr_dev_inst *sdi)
293 struct dev_context *devc;
295 if (!(devc = sdi->priv))
298 sr_dbg("Starting data capture for frameset %lu of %lu",
299 devc->num_frames + 1, devc->limit_frames);
301 if (sr_scpi_send(sdi->conn, ":WAV:FORM BYTE") != SR_OK)
303 if (devc->data_source == DATA_SOURCE_LIVE) {
304 if (sr_scpi_send(sdi->conn, ":WAV:MODE NORM") != SR_OK)
306 rigol_ds_set_wait_event(devc, WAIT_TRIGGER);
308 if (sr_scpi_send(sdi->conn, ":WAV:MODE RAW") != SR_OK)
310 if (sr_scpi_send(sdi->conn, ":SING", devc->analog_frame_size) != SR_OK)
312 rigol_ds_set_wait_event(devc, WAIT_STOP);
318 /* Start reading data from the current channel */
319 SR_PRIV int rigol_ds_channel_start(const struct sr_dev_inst *sdi)
321 struct dev_context *devc;
322 struct sr_probe *probe;
324 if (!(devc = sdi->priv))
327 probe = devc->channel_entry->data;
329 sr_dbg("Starting reading data from channel %d", probe->index + 1);
331 if (devc->model->series < RIGOL_DS1000Z) {
332 if (probe->type == SR_PROBE_LOGIC) {
333 if (sr_scpi_send(sdi->conn, ":WAV:DATA? DIG") != SR_OK)
336 if (sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%d",
337 probe->index + 1) != SR_OK)
341 if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d",
342 probe->index + 1) != SR_OK)
344 if (devc->data_source != DATA_SOURCE_LIVE) {
345 if (sr_scpi_send(sdi->conn, ":WAV:RES") != SR_OK)
347 if (sr_scpi_send(sdi->conn, ":WAV:BEG") != SR_OK)
349 rigol_ds_set_wait_event(devc, WAIT_BLOCK);
351 rigol_ds_set_wait_event(devc, WAIT_NONE);
354 devc->num_channel_bytes = 0;
355 devc->num_block_bytes = 0;
360 /* Read the header of a data block */
361 static int rigol_ds_read_header(struct sr_scpi_dev_inst *scpi)
363 char start[3], length[10];
366 /* Read the hashsign and length digit. */
367 tmp = sr_scpi_read_data(scpi, start, 2);
370 sr_err("Failed to read first two bytes of data block header.");
373 if (start[0] != '#' || !isdigit(start[1]) || start[1] == '0') {
374 sr_err("Received invalid data block header start '%s'.", start);
377 len = atoi(start + 1);
379 /* Read the data length. */
380 tmp = sr_scpi_read_data(scpi, length, len);
383 sr_err("Failed to read %d bytes of data block length.", len);
386 if (parse_int(length, &len) != SR_OK) {
387 sr_err("Received invalid data block length '%s'.", length);
391 sr_dbg("Received data block header: %s%s -> block length %d", start, length, len);
396 SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
398 struct sr_dev_inst *sdi;
399 struct sr_scpi_dev_inst *scpi;
400 struct dev_context *devc;
401 struct sr_datafeed_packet packet;
402 struct sr_datafeed_analog analog;
403 struct sr_datafeed_logic logic;
406 struct sr_probe *probe;
407 gsize expected_data_bytes;
411 if (!(sdi = cb_data))
414 if (!(devc = sdi->priv))
419 if (revents == G_IO_IN || revents == 0) {
420 if (devc->model->series >= RIGOL_DS1000Z) {
421 switch(devc->wait_event) {
425 if (rigol_ds_trigger_wait(sdi) != SR_OK)
427 if (rigol_ds_channel_start(sdi) != SR_OK)
431 if (rigol_ds_block_wait(sdi) != SR_OK)
435 if (rigol_ds_stop_wait(sdi) != SR_OK)
437 if (rigol_ds_check_stop(sdi) != SR_OK)
439 if (rigol_ds_channel_start(sdi) != SR_OK)
443 sr_err("BUG: Unknown event target encountered");
447 probe = devc->channel_entry->data;
449 expected_data_bytes = probe->type == SR_PROBE_ANALOG ?
450 devc->analog_frame_size : devc->digital_frame_size;
452 if (devc->num_block_bytes == 0 &&
453 devc->model->series >= RIGOL_DS1000Z) {
454 if (sr_scpi_send(sdi->conn, ":WAV:DATA?") != SR_OK)
458 if (devc->num_block_bytes == 0) {
460 if (sr_scpi_read_begin(scpi) != SR_OK)
463 if (devc->model->protocol == PROTOCOL_IEEE488_2) {
464 sr_dbg("New block header expected");
465 len = rigol_ds_read_header(scpi);
468 /* At slow timebases in live capture the DS2072
469 * sometimes returns "short" data blocks, with
470 * apparently no way to get the rest of the data.
471 * Discard these, the complete data block will
474 if (devc->data_source == DATA_SOURCE_LIVE
475 && (unsigned)len < expected_data_bytes) {
476 sr_dbg("Discarding short data block");
477 sr_scpi_read_data(scpi, (char *)devc->buffer, len + 1);
480 devc->num_block_bytes = len;
482 devc->num_block_bytes = expected_data_bytes;
484 devc->num_block_read = 0;
487 len = devc->num_block_bytes - devc->num_block_read;
488 len = sr_scpi_read_data(scpi, (char *)devc->buffer,
489 len < ACQ_BUFFER_SIZE ? len : ACQ_BUFFER_SIZE);
491 sr_dbg("Received %d bytes.", len);
495 devc->num_block_read += len;
497 if (probe->type == SR_PROBE_ANALOG) {
498 vref = devc->vert_reference[probe->index];
499 vdiv = devc->vdiv[probe->index] / 25.6;
500 offset = devc->vert_offset[probe->index];
501 if (devc->model->series >= RIGOL_DS1000Z)
502 for (i = 0; i < len; i++)
503 devc->data[i] = ((int)devc->buffer[i] - vref) * vdiv - offset;
505 for (i = 0; i < len; i++)
506 devc->data[i] = (128 - devc->buffer[i]) * vdiv - offset;
507 analog.probes = g_slist_append(NULL, probe);
508 analog.num_samples = len;
509 analog.data = devc->data;
510 analog.mq = SR_MQ_VOLTAGE;
511 analog.unit = SR_UNIT_VOLT;
513 packet.type = SR_DF_ANALOG;
514 packet.payload = &analog;
515 sr_session_send(cb_data, &packet);
516 g_slist_free(analog.probes);
520 logic.data = devc->buffer;
521 packet.type = SR_DF_LOGIC;
522 packet.payload = &logic;
523 sr_session_send(cb_data, &packet);
526 if (devc->num_block_read == devc->num_block_bytes) {
527 sr_dbg("Block has been completed");
528 if (devc->model->series >= RIGOL_DS1000Z) {
529 /* Discard the terminating linefeed */
530 sr_scpi_read_data(scpi, (char *)devc->buffer, 1);
532 if (devc->model->protocol == PROTOCOL_IEEE488_2) {
533 /* Prepare for possible next block */
534 devc->num_block_bytes = 0;
535 if (devc->data_source != DATA_SOURCE_LIVE)
536 rigol_ds_set_wait_event(devc, WAIT_BLOCK);
538 if (!sr_scpi_read_complete(scpi)) {
539 sr_err("Read should have been completed");
540 sdi->driver->dev_acquisition_stop(sdi, cb_data);
543 devc->num_block_read = 0;
545 sr_dbg("%d of %d block bytes read", devc->num_block_read, devc->num_block_bytes);
548 devc->num_channel_bytes += len;
550 if (devc->num_channel_bytes < expected_data_bytes)
551 /* Don't have the full data for this channel yet, re-run. */
554 /* End of data for this channel. */
555 if (devc->model->series >= RIGOL_DS1000Z) {
556 /* Signal end of data download to scope */
557 if (devc->data_source != DATA_SOURCE_LIVE)
559 * This causes a query error, without it switching
560 * to the next channel causes an error. Fun with
563 sr_scpi_send(sdi->conn, ":WAV:END");
566 if (probe->type == SR_PROBE_ANALOG
567 && devc->channel_entry->next != NULL) {
568 /* We got the frame for this analog channel, but
569 * there's another analog channel. */
570 devc->channel_entry = devc->channel_entry->next;
571 rigol_ds_channel_start(sdi);
573 /* Done with all analog channels in this frame. */
574 if (devc->enabled_digital_probes
575 && devc->channel_entry != devc->enabled_digital_probes) {
576 /* Now we need to get the digital data. */
577 devc->channel_entry = devc->enabled_digital_probes;
578 rigol_ds_channel_start(sdi);
580 /* Done with this frame. */
581 packet.type = SR_DF_FRAME_END;
582 sr_session_send(cb_data, &packet);
584 if (++devc->num_frames == devc->limit_frames) {
585 /* Last frame, stop capture. */
586 sdi->driver->dev_acquisition_stop(sdi, cb_data);
588 /* Get the next frame, starting with the first analog channel. */
589 if (devc->enabled_analog_probes)
590 devc->channel_entry = devc->enabled_analog_probes;
592 devc->channel_entry = devc->enabled_digital_probes;
594 if (devc->model->series < RIGOL_DS1000Z)
595 rigol_ds_channel_start(sdi);
597 rigol_ds_capture_start(sdi);
599 /* Start of next frame. */
600 packet.type = SR_DF_FRAME_BEGIN;
601 sr_session_send(cb_data, &packet);
610 SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi)
612 struct dev_context *devc;
619 /* Analog channel state. */
620 for (i = 0; i < devc->model->analog_channels; i++) {
621 cmd = g_strdup_printf(":CHAN%d:DISP?", i + 1);
622 res = sr_scpi_get_string(sdi->conn, cmd, &t_s);
626 devc->analog_channels[i] = !strcmp(t_s, "ON") || !strcmp(t_s, "1");
628 sr_dbg("Current analog channel state:");
629 for (i = 0; i < devc->model->analog_channels; i++)
630 sr_dbg("CH%d %s", i + 1, devc->analog_channels[i] ? "on" : "off");
632 /* Digital channel state. */
633 if (devc->model->has_digital) {
634 if (sr_scpi_get_string(sdi->conn, ":LA:DISP?", &t_s) != SR_OK)
636 devc->la_enabled = !strcmp(t_s, "ON") ? TRUE : FALSE;
637 sr_dbg("Logic analyzer %s, current digital channel state:",
638 devc->la_enabled ? "enabled" : "disabled");
639 for (i = 0; i < 16; i++) {
640 cmd = g_strdup_printf(":DIG%d:TURN?", i);
641 res = sr_scpi_get_string(sdi->conn, cmd, &t_s);
645 devc->digital_channels[i] = !strcmp(t_s, "ON") ? TRUE : FALSE;
647 sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "on" : "off");
652 if (sr_scpi_get_float(sdi->conn, ":TIM:SCAL?", &devc->timebase) != SR_OK)
654 sr_dbg("Current timebase %g", devc->timebase);
657 for (i = 0; i < devc->model->analog_channels; i++) {
658 cmd = g_strdup_printf(":CHAN%d:SCAL?", i + 1);
659 res = sr_scpi_get_float(sdi->conn, cmd, &devc->vdiv[i]);
664 sr_dbg("Current vertical gain:");
665 for (i = 0; i < devc->model->analog_channels; i++)
666 sr_dbg("CH%d %g", i + 1, devc->vdiv[i]);
668 sr_dbg("Current vertical reference:");
669 if (devc->model->series >= RIGOL_DS1000Z) {
670 /* Vertical reference - not certain if this is the place to read it. */
671 for (i = 0; i < devc->model->analog_channels; i++) {
672 if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d", i + 1) != SR_OK)
674 if (sr_scpi_get_int(sdi->conn, ":WAV:YREF?", &devc->vert_reference[i]) != SR_OK)
676 sr_dbg("CH%d %d", i + 1, devc->vert_reference[i]);
680 /* Vertical offset. */
681 for (i = 0; i < devc->model->analog_channels; i++) {
682 cmd = g_strdup_printf(":CHAN%d:OFFS?", i + 1);
683 res = sr_scpi_get_float(sdi->conn, cmd, &devc->vert_offset[i]);
688 sr_dbg("Current vertical offset:");
689 for (i = 0; i < devc->model->analog_channels; i++)
690 sr_dbg("CH%d %g", i + 1, devc->vert_offset[i]);
693 for (i = 0; i < devc->model->analog_channels; i++) {
694 cmd = g_strdup_printf(":CHAN%d:COUP?", i + 1);
695 res = sr_scpi_get_string(sdi->conn, cmd, &devc->coupling[i]);
700 sr_dbg("Current coupling:");
701 for (i = 0; i < devc->model->analog_channels; i++)
702 sr_dbg("CH%d %s", i + 1, devc->coupling[i]);
704 /* Trigger source. */
705 if (sr_scpi_get_string(sdi->conn, ":TRIG:EDGE:SOUR?", &devc->trigger_source) != SR_OK)
707 sr_dbg("Current trigger source %s", devc->trigger_source);
709 /* Horizontal trigger position. */
710 if (sr_scpi_get_float(sdi->conn, ":TIM:OFFS?", &devc->horiz_triggerpos) != SR_OK)
712 sr_dbg("Current horizontal trigger position %g", devc->horiz_triggerpos);
715 if (sr_scpi_get_string(sdi->conn, ":TRIG:EDGE:SLOP?", &devc->trigger_slope) != SR_OK)
717 sr_dbg("Current trigger slope %s", devc->trigger_slope);