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 get_cfg(const struct sr_dev_inst *sdi, char *cmd, char *reply, size_t maxlen);
68 static int get_cfg_int(const struct sr_dev_inst *sdi, char *cmd, int *i);
70 static int parse_int(const char *str, int *ret)
76 tmp = strtol(str, &e, 10);
77 if (e == str || *e != '\0') {
78 sr_dbg("Failed to parse integer: '%s'", str);
82 sr_dbg("Failed to parse integer: '%s', numerical overflow", str);
85 if (tmp > INT_MAX || tmp < INT_MIN) {
86 sr_dbg("Failed to parse integer: '%s', value to large/small", str);
94 /* Set the next event to wait for in rigol_ds_receive */
95 static void rigol_ds_set_wait_event(struct dev_context *devc, enum wait_events event)
97 if (event == WAIT_STOP)
98 devc->wait_status = 2;
100 devc->wait_status = 1;
101 devc->wait_event = event;
105 * Waiting for a event will return a timeout after 2 to 3 seconds in order
106 * to not block the application.
108 static int rigol_ds_event_wait(const struct sr_dev_inst *sdi, char status1, char status2)
111 struct dev_context *devc;
114 if (!(devc = sdi->priv))
120 * Trigger status may return:
121 * "TD" or "T'D" - triggered
122 * "AUTO" - autotriggered
124 * "WAIT" - waiting for trigger
128 if (devc->wait_status == 1) {
130 if (time(NULL) - start >= 3) {
131 sr_dbg("Timeout waiting for trigger");
132 return SR_ERR_TIMEOUT;
135 if (get_cfg(sdi, ":TRIG:STAT?", buf, sizeof(buf)) != SR_OK)
137 } while (buf[0] == status1 || buf[0] == status2);
139 devc->wait_status = 2;
141 if (devc->wait_status == 2) {
143 if (time(NULL) - start >= 3) {
144 sr_dbg("Timeout waiting for trigger");
145 return SR_ERR_TIMEOUT;
148 if (get_cfg(sdi, ":TRIG:STAT?", buf, sizeof(buf)) != SR_OK)
150 } while (buf[0] != status1 && buf[0] != status2);
152 rigol_ds_set_wait_event(devc, WAIT_NONE);
159 * For live capture we need to wait for a new trigger event to ensure that
160 * sample data is not returned twice.
162 * Unfortunately this will never really work because for sufficiently fast
163 * timebases and trigger rates it just can't catch the status changes.
165 * What would be needed is a trigger event register with autoreset like the
166 * Agilents have. The Rigols don't seem to have anything like this.
168 * The workaround is to only wait for the trigger when the timebase is slow
169 * enough. Of course this means that for faster timebases sample data can be
170 * returned multiple times, this effect is mitigated somewhat by sleeping
171 * for about one sweep time in that case.
173 static int rigol_ds_trigger_wait(const struct sr_dev_inst *sdi)
175 struct dev_context *devc;
178 if (!(devc = sdi->priv))
182 * If timebase < 50 msecs/DIV just sleep about one sweep time except
183 * for really fast sweeps.
185 if (devc->timebase < 0.0499)
187 if (devc->timebase > 0.99e-6) {
189 * Timebase * num hor. divs * 85(%) * 1e6(usecs) / 100
190 * -> 85 percent of sweep time
192 s = (devc->timebase * devc->model->num_horizontal_divs
194 sr_spew("Sleeping for %ld usecs instead of trigger-wait", s);
197 rigol_ds_set_wait_event(devc, WAIT_NONE);
200 return rigol_ds_event_wait(sdi, 'T', 'A');
204 /* Wait for scope to got to "Stop" in single shot mode */
205 static int rigol_ds_stop_wait(const struct sr_dev_inst *sdi)
207 return rigol_ds_event_wait(sdi, 'S', 'S');
210 /* Check that a single shot acquisition actually succeeded on the DS2000 */
211 static int rigol_ds_check_stop(const struct sr_dev_inst *sdi)
213 struct dev_context *devc;
216 if (!(devc = sdi->priv))
219 if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d",
220 devc->channel->index + 1) != SR_OK)
222 /* Check that the number of samples will be accepted */
223 if (sr_scpi_send(sdi->conn, ":WAV:POIN %d;*OPC", devc->analog_frame_size) != SR_OK)
225 if (get_cfg_int(sdi, "*ESR?", &tmp) != SR_OK)
228 * If we get an "Execution error" the scope went from "Single" to
229 * "Stop" without actually triggering. There is no waveform
230 * displayed and trying to download one will fail - the scope thinks
231 * it has 1400 samples (like display memory) and the driver thinks
232 * it has a different number of samples.
234 * In that case just try to capture something again. Might still
235 * fail in interesting ways.
237 * Ain't firmware fun?
240 sr_warn("Single shot acquisition failed, retrying...");
241 /* Sleep a bit, otherwise the single shot will often fail */
243 sr_scpi_send(sdi->conn, ":SING");
244 rigol_ds_set_wait_event(devc, WAIT_STOP);
251 /* Wait for enough data becoming available in scope output buffer */
252 static int rigol_ds_block_wait(const struct sr_dev_inst *sdi)
255 struct dev_context *devc;
259 if (!(devc = sdi->priv))
265 if (time(NULL) - start >= 3) {
266 sr_dbg("Timeout waiting for data block");
267 return SR_ERR_TIMEOUT;
271 * The scope copies data really slowly from sample
272 * memory to its output buffer, so try not to bother
273 * it too much with SCPI requests but don't wait too
274 * long for short sample frame sizes.
276 g_usleep(devc->analog_frame_size < 15000 ? 100000 : 1000000);
278 /* "READ,nnnn" (still working) or "IDLE,nnnn" (finished) */
279 if (get_cfg(sdi, ":WAV:STAT?", buf, sizeof(buf)) != SR_OK)
282 if (parse_int(buf + 5, &len) != SR_OK)
284 } while (buf[0] == 'R' && len < 1000000);
286 rigol_ds_set_wait_event(devc, WAIT_NONE);
291 /* Start capturing a new frameset */
292 SR_PRIV int rigol_ds_capture_start(const struct sr_dev_inst *sdi)
294 struct dev_context *devc;
296 if (!(devc = sdi->priv))
299 sr_dbg("Starting data capture for frameset %lu of %lu",
300 devc->num_frames + 1, devc->limit_frames);
302 if (sr_scpi_send(sdi->conn, ":WAV:FORM BYTE") != SR_OK)
304 if (devc->data_source == DATA_SOURCE_LIVE) {
305 if (sr_scpi_send(sdi->conn, ":WAV:MODE NORM") != SR_OK)
307 rigol_ds_set_wait_event(devc, WAIT_TRIGGER);
309 if (sr_scpi_send(sdi->conn, ":WAV:MODE RAW") != SR_OK)
311 if (sr_scpi_send(sdi->conn, ":SING", devc->analog_frame_size) != SR_OK)
313 rigol_ds_set_wait_event(devc, WAIT_STOP);
319 /* Start reading data from the current channel */
320 SR_PRIV int rigol_ds_channel_start(const struct sr_dev_inst *sdi)
322 struct dev_context *devc;
324 if (!(devc = sdi->priv))
327 sr_dbg("Starting reading data from channel %d",
328 devc->channel->index + 1);
330 if (devc->model->protocol == PROTOCOL_LEGACY) {
331 if (devc->channel->type == SR_DF_LOGIC) {
332 if (sr_scpi_send(sdi->conn, ":WAV:DATA? DIG") != SR_OK)
335 if (sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%c",
336 devc->channel->name[2]) != SR_OK)
340 if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d",
341 devc->channel->index + 1) != SR_OK)
343 if (devc->data_source != DATA_SOURCE_LIVE) {
344 if (sr_scpi_send(sdi->conn, ":WAV:RES") != SR_OK)
346 if (sr_scpi_send(sdi->conn, ":WAV:BEG") != SR_OK)
348 rigol_ds_set_wait_event(devc, WAIT_BLOCK);
350 rigol_ds_set_wait_event(devc, WAIT_NONE);
353 devc->num_frame_samples = 0;
354 devc->num_block_bytes = 0;
359 /* Read the header of a data block */
360 static int rigol_ds_read_header(struct sr_scpi_dev_inst *scpi)
362 char start[3], length[10];
365 /* Read the hashsign and length digit. */
366 tmp = sr_scpi_read(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')
375 sr_err("Received invalid data block header start '%s'.", start);
378 len = atoi(start + 1);
380 /* Read the data length. */
381 tmp = sr_scpi_read(scpi, length, len);
385 sr_err("Failed to read %d bytes of data block length.", len);
388 if (parse_int(length, &len) != SR_OK)
390 sr_err("Received invalid data block length '%s'.", length);
394 sr_dbg("Received data block header: %s%s -> block length %d", start, length, len);
399 SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
401 struct sr_dev_inst *sdi;
402 struct sr_scpi_dev_inst *scpi;
403 struct dev_context *devc;
404 struct sr_datafeed_packet packet;
405 struct sr_datafeed_analog analog;
406 struct sr_datafeed_logic logic;
408 int len, i, waveform_size, vref;
409 struct sr_probe *probe;
413 if (!(sdi = cb_data))
416 if (!(devc = sdi->priv))
421 if (revents == G_IO_IN) {
422 if (devc->model->protocol == PROTOCOL_IEEE488_2) {
423 switch(devc->wait_event) {
428 if (rigol_ds_trigger_wait(sdi) != SR_OK)
430 if (rigol_ds_channel_start(sdi) != SR_OK)
435 if (rigol_ds_block_wait(sdi) != SR_OK)
440 if (rigol_ds_stop_wait(sdi) != SR_OK)
442 if (rigol_ds_check_stop(sdi) != SR_OK)
444 if (rigol_ds_channel_start(sdi) != SR_OK)
449 sr_err("BUG: Unknown event target encountered");
452 if (devc->num_block_bytes == 0) {
453 sr_dbg("New block header expected");
454 if (sr_scpi_send(sdi->conn, ":WAV:DATA?") != SR_OK)
456 len = rigol_ds_read_header(scpi);
459 /* At slow timebases in live capture the DS2072
460 * sometimes returns "short" data blocks, with
461 * apparently no way to get the rest of the data.
462 * Discard these, the complete data block will
465 if (devc->data_source == DATA_SOURCE_LIVE
466 && (unsigned)len < devc->num_frame_samples) {
467 sr_dbg("Discarding short data block");
468 sr_scpi_read(scpi, (char *)devc->buffer, len + 1);
471 devc->num_block_bytes = len;
472 devc->num_block_read = 0;
476 probe = devc->channel;
477 if (devc->model->protocol == PROTOCOL_IEEE488_2) {
478 len = devc->num_block_bytes - devc->num_block_read;
479 len = sr_scpi_read(scpi, (char *)devc->buffer,
480 len < ACQ_BUFFER_SIZE ? len : ACQ_BUFFER_SIZE);
482 waveform_size = probe->type == SR_PROBE_ANALOG ?
483 (devc->model->series == RIGOL_VS5000 ?
484 VS5000_ANALOG_LIVE_WAVEFORM_SIZE :
485 DS1000_ANALOG_LIVE_WAVEFORM_SIZE) :
486 DIGITAL_WAVEFORM_SIZE;
487 len = sr_scpi_read(scpi, (char *)devc->buffer,
488 waveform_size - devc->num_frame_samples);
490 sr_dbg("Received %d bytes.", len);
494 if (devc->num_frame_samples == 0) {
495 /* Start of a new frame. */
496 packet.type = SR_DF_FRAME_BEGIN;
497 sr_session_send(sdi, &packet);
500 if (probe->type == SR_PROBE_ANALOG) {
501 if (devc->model->protocol == PROTOCOL_IEEE488_2)
502 devc->num_block_read += len;
503 vref = devc->vert_reference[probe->index];
504 vdiv = devc->vdiv[probe->index] / 25.6;
505 offset = devc->vert_offset[probe->index];
506 if (devc->model->protocol == PROTOCOL_IEEE488_2)
507 for (i = 0; i < len; i++)
508 devc->data[i] = ((int)devc->buffer[i] - vref) * vdiv - offset;
510 for (i = 0; i < len; i++)
511 devc->data[i] = (128 - devc->buffer[i]) * vdiv - offset;
512 analog.probes = g_slist_append(NULL, probe);
513 analog.num_samples = len;
514 analog.data = devc->data;
515 analog.mq = SR_MQ_VOLTAGE;
516 analog.unit = SR_UNIT_VOLT;
518 packet.type = SR_DF_ANALOG;
519 packet.payload = &analog;
520 sr_session_send(cb_data, &packet);
521 g_slist_free(analog.probes);
523 if (devc->model->protocol == PROTOCOL_IEEE488_2) {
524 if (devc->num_block_read == devc->num_block_bytes) {
525 sr_dbg("Block has been completed");
526 /* Discard the terminating linefeed and prepare for
527 possible next block */
528 sr_scpi_read(scpi, (char *)devc->buffer, 1);
529 devc->num_block_bytes = 0;
530 if (devc->data_source != DATA_SOURCE_LIVE)
531 rigol_ds_set_wait_event(devc, WAIT_BLOCK);
533 sr_dbg("%d of %d block bytes read", devc->num_block_read, devc->num_block_bytes);
536 devc->num_frame_samples += len;
538 if (devc->num_frame_samples < devc->analog_frame_size)
539 /* Don't have the whole frame yet. */
542 sr_dbg("Frame completed, %d samples", devc->num_frame_samples);
544 logic.length = len - 10;
546 logic.data = devc->buffer + 10;
547 packet.type = SR_DF_LOGIC;
548 packet.payload = &logic;
549 sr_session_send(cb_data, &packet);
551 if (len != DIGITAL_WAVEFORM_SIZE)
552 /* Don't have the whole frame yet. */
556 /* End of the frame. */
557 packet.type = SR_DF_FRAME_END;
558 sr_session_send(sdi, &packet);
559 if (devc->model->protocol == PROTOCOL_LEGACY)
560 devc->num_frame_samples = 0;
562 /* Signal end of data download to scope */
563 if (devc->data_source != DATA_SOURCE_LIVE)
565 * This causes a query error, without it switching
566 * to the next channel causes an error. Fun with
569 sr_scpi_send(sdi->conn, ":WAV:END");
572 if (devc->enabled_analog_probes
573 && devc->channel == devc->enabled_analog_probes->data
574 && devc->enabled_analog_probes->next != NULL) {
575 /* We got the frame for the first analog channel, but
576 * there's a second analog channel. */
577 devc->channel = devc->enabled_analog_probes->next->data;
578 rigol_ds_channel_start(sdi);
580 /* Done with both analog channels in this frame. */
581 if (devc->enabled_digital_probes
582 && devc->channel != devc->enabled_digital_probes->data) {
583 /* Now we need to get the digital data. */
584 devc->channel = devc->enabled_digital_probes->data;
585 rigol_ds_channel_start(sdi);
586 } else if (++devc->num_frames == devc->limit_frames) {
587 /* End of last frame. */
588 packet.type = SR_DF_END;
589 sr_session_send(sdi, &packet);
590 sdi->driver->dev_acquisition_stop(sdi, cb_data);
592 /* Get the next frame, starting with the first analog channel. */
593 if (devc->enabled_analog_probes)
594 devc->channel = devc->enabled_analog_probes->data;
596 devc->channel = devc->enabled_digital_probes->data;
598 if (devc->model->protocol == PROTOCOL_LEGACY)
599 rigol_ds_channel_start(sdi);
601 rigol_ds_capture_start(sdi);
609 static int get_cfg(const struct sr_dev_inst *sdi, char *cmd, char *reply, size_t maxlen)
612 struct dev_context *devc = sdi->priv;
613 struct sr_scpi_dev_inst *scpi = sdi->conn;
616 if (sr_scpi_send(scpi, cmd) != SR_OK)
619 if (sr_scpi_receive(scpi, &response) != SR_OK)
622 g_strlcpy(reply, response, maxlen);
626 if (devc->model->protocol == PROTOCOL_IEEE488_2) {
627 /* get rid of trailing linefeed */
628 if (len >= 1 && reply[len-1] == '\n')
632 sr_spew("Received '%s'.", reply);
637 static int get_cfg_int(const struct sr_dev_inst *sdi, char *cmd, int *i)
641 if (get_cfg(sdi, cmd, buf, sizeof(buf)) != SR_OK)
644 if (parse_int(buf, i) != SR_OK)
650 static int get_cfg_float(const struct sr_dev_inst *sdi, char *cmd, float *f)
654 if (get_cfg(sdi, cmd, buf, sizeof(buf)) != SR_OK)
656 *f = strtof(buf, &e);
657 if (e == buf || (fpclassify(*f) & (FP_ZERO | FP_NORMAL)) == 0) {
658 sr_dbg("failed to parse response to '%s': '%s'", cmd, buf);
665 static int get_cfg_string(const struct sr_dev_inst *sdi, char *cmd, char **buf)
667 if (!(*buf = g_try_malloc0(256)))
668 return SR_ERR_MALLOC;
670 if (get_cfg(sdi, cmd, *buf, 256) != SR_OK)
676 SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi)
678 struct dev_context *devc;
684 /* Analog channel state. */
685 if (get_cfg_string(sdi, ":CHAN1:DISP?", &t_s) != SR_OK)
687 devc->analog_channels[0] = !strcmp(t_s, "ON") || !strcmp(t_s, "1");
689 if (get_cfg_string(sdi, ":CHAN2:DISP?", &t_s) != SR_OK)
691 devc->analog_channels[1] = !strcmp(t_s, "ON") || !strcmp(t_s, "1");
693 sr_dbg("Current analog channel state CH1 %s CH2 %s",
694 devc->analog_channels[0] ? "on" : "off",
695 devc->analog_channels[1] ? "on" : "off");
697 /* Digital channel state. */
698 if (devc->model->has_digital) {
699 sr_dbg("Current digital channel state:");
700 for (i = 0; i < 16; i++) {
701 cmd = g_strdup_printf(":DIG%d:TURN?", i);
702 res = get_cfg_string(sdi, cmd, &t_s);
706 devc->digital_channels[i] = !strcmp(t_s, "ON") ? TRUE : FALSE;
708 sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "on" : "off");
713 if (get_cfg_float(sdi, ":TIM:SCAL?", &devc->timebase) != SR_OK)
715 sr_dbg("Current timebase %g", devc->timebase);
718 if (get_cfg_float(sdi, ":CHAN1:SCAL?", &devc->vdiv[0]) != SR_OK)
720 if (get_cfg_float(sdi, ":CHAN2:SCAL?", &devc->vdiv[1]) != SR_OK)
722 sr_dbg("Current vertical gain CH1 %g CH2 %g", devc->vdiv[0], devc->vdiv[1]);
724 if (devc->model->protocol == PROTOCOL_IEEE488_2) {
725 /* Vertical reference - not certain if this is the place to read it. */
726 if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN1") != SR_OK)
728 if (get_cfg_int(sdi, ":WAV:YREF?", &devc->vert_reference[0]) != SR_OK)
730 if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN2") != SR_OK)
732 if (get_cfg_int(sdi, ":WAV:YREF?", &devc->vert_reference[1]) != SR_OK)
734 sr_dbg("Current vertical reference CH1 %d CH2 %d",
735 devc->vert_reference[0], devc->vert_reference[1]);
738 /* Vertical offset. */
739 if (get_cfg_float(sdi, ":CHAN1:OFFS?", &devc->vert_offset[0]) != SR_OK)
741 if (get_cfg_float(sdi, ":CHAN2:OFFS?", &devc->vert_offset[1]) != SR_OK)
743 sr_dbg("Current vertical offset CH1 %g CH2 %g", devc->vert_offset[0],
744 devc->vert_offset[1]);
747 if (get_cfg_string(sdi, ":CHAN1:COUP?", &devc->coupling[0]) != SR_OK)
749 if (get_cfg_string(sdi, ":CHAN2:COUP?", &devc->coupling[1]) != SR_OK)
751 sr_dbg("Current coupling CH1 %s CH2 %s", devc->coupling[0],
754 /* Trigger source. */
755 if (get_cfg_string(sdi, ":TRIG:EDGE:SOUR?", &devc->trigger_source) != SR_OK)
757 sr_dbg("Current trigger source %s", devc->trigger_source);
759 /* Horizontal trigger position. */
760 if (get_cfg_float(sdi, ":TIM:OFFS?", &devc->horiz_triggerpos) != SR_OK)
762 sr_dbg("Current horizontal trigger position %g", devc->horiz_triggerpos);
765 if (get_cfg_string(sdi, ":TRIG:EDGE:SLOP?", &devc->trigger_slope) != SR_OK)
767 sr_dbg("Current trigger slope %s", devc->trigger_slope);