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);
95 * Waiting for a trigger event will return a timeout after 2, 3 seconds in
96 * order to not block the application.
99 static int rigol_ds2xx2_trigger_wait(const struct sr_dev_inst *sdi)
102 struct dev_context *devc;
105 if (!(devc = sdi->priv))
111 * Trigger status may return:
113 * "AUTO" - autotriggered
115 * "WAIT" - waiting for trigger
119 if (devc->trigger_wait_status == 1) {
121 if (time(NULL) - start >= 3) {
122 sr_dbg("Timeout waiting for trigger");
123 return SR_ERR_TIMEOUT;
126 if (get_cfg(sdi, ":TRIG:STAT?", buf, sizeof(buf)) != SR_OK)
128 } while (buf[0] == 'T' || buf[0] == 'A');
130 devc->trigger_wait_status = 2;
132 if (devc->trigger_wait_status == 2) {
134 if (time(NULL) - start >= 3) {
135 sr_dbg("Timeout waiting for trigger");
136 return SR_ERR_TIMEOUT;
139 if (get_cfg(sdi, ":TRIG:STAT?", buf, sizeof(buf)) != SR_OK)
141 } while (buf[0] != 'T' && buf[0] != 'A');
143 devc->trigger_wait_status = 0;
150 * This needs to wait for a new trigger event to ensure that sample data is
151 * not returned twice.
153 * Unfortunately this will never really work because for sufficiently fast
154 * timebases it just can't catch the status changes.
156 * What would be needed is a trigger event register with autoreset like the
157 * Agilents have. The Rigols don't seem to have anything like this.
159 * The workaround is to only wait for the trigger when the timebase is slow
160 * enough. Of course this means that for faster timebases sample data can be
161 * returned multiple times.
164 SR_PRIV int rigol_ds2xx2_acquisition_start(const struct sr_dev_inst *sdi,
165 gboolean wait_for_trigger)
167 struct dev_context *devc;
169 if (!(devc = sdi->priv))
172 sr_dbg("Starting acquisition on channel %d",
173 devc->channel_frame->index + 1);
175 if (rigol_ds_send(sdi, ":WAV:FORM BYTE") != SR_OK)
177 if (rigol_ds_send(sdi, ":WAV:SOUR CHAN%d",
178 devc->channel_frame->index + 1) != SR_OK)
180 if (rigol_ds_send(sdi, ":WAV:MODE NORM") != SR_OK)
183 devc->num_frame_bytes = 0;
184 devc->num_block_bytes = 0;
186 /* only wait for trigger if timbase 50 msecs/DIV or slower */
187 if (wait_for_trigger && devc->timebase > 0.0499)
189 devc->trigger_wait_status = 1;
191 devc->trigger_wait_status = 0;
197 static int rigol_ds2xx2_read_header(struct sr_serial_dev_inst *serial)
199 char start[3], length[10];
202 /* Read the hashsign and length digit. */
203 tmp = serial_read(serial, start, 2);
207 sr_err("Failed to read first two bytes of data block header.");
210 if (start[0] != '#' || !isdigit(start[1]) || start[1] == '0')
212 sr_err("Received invalid data block header start '%s'.", start);
215 len = atoi(start + 1);
217 /* Read the data length. */
218 tmp = serial_read(serial, length, len);
222 sr_err("Failed to read %d bytes of data block length.", len);
225 if (parse_int(length, &len) != SR_OK)
227 sr_err("Received invalid data block length '%s'.", length);
231 sr_dbg("Received data block header: %s%s -> block length %d", start, length, len);
236 SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
238 struct sr_dev_inst *sdi;
239 struct sr_serial_dev_inst *serial;
240 struct dev_context *devc;
241 struct sr_datafeed_packet packet;
242 struct sr_datafeed_analog analog;
243 struct sr_datafeed_logic logic;
244 unsigned char buf[DS2000_ANALOG_WAVEFORM_SIZE];
246 float data[DS2000_ANALOG_WAVEFORM_SIZE];
247 int len, i, waveform_size, vref;
248 struct sr_probe *probe;
252 if (!(sdi = cb_data))
255 if (!(devc = sdi->priv))
260 if (revents == G_IO_IN) {
261 if (devc->trigger_wait_status > 0) {
262 if (rigol_ds2xx2_trigger_wait(sdi) != SR_OK)
266 if (devc->model->series == 2 && devc->num_block_bytes == 0) {
267 sr_dbg("New block header expected");
268 if (rigol_ds_send(sdi, ":WAV:DATA?") != SR_OK)
270 len = rigol_ds2xx2_read_header(serial);
273 /* At slow timebases the scope sometimes returns
274 * "short" data blocks, with apparently no way to
275 * get the rest of the data. Discard these, the
276 * complete data block will appear eventually.
278 if (len < DS2000_ANALOG_WAVEFORM_SIZE) {
279 sr_dbg("Discarding short data block");
280 serial_read(serial, buf, len + 1);
283 devc->num_block_bytes = len;
284 devc->num_block_read = 0;
287 probe = devc->channel_frame;
288 if (devc->model->series == 2) {
289 len = devc->num_block_bytes - devc->num_block_read;
290 len = serial_read(serial, buf,
291 len < DS2000_ANALOG_WAVEFORM_SIZE ? len : DS2000_ANALOG_WAVEFORM_SIZE);
293 waveform_size = probe->type == SR_PROBE_ANALOG ?
294 DS1000_ANALOG_WAVEFORM_SIZE : DIGITAL_WAVEFORM_SIZE;
295 len = serial_read(serial, buf, waveform_size - devc->num_frame_bytes);
297 sr_dbg("Received %d bytes.", len);
301 if (devc->num_frame_bytes == 0) {
302 /* Start of a new frame. */
303 packet.type = SR_DF_FRAME_BEGIN;
304 sr_session_send(sdi, &packet);
307 if (probe->type == SR_PROBE_ANALOG) {
308 if (devc->model->series == 2)
309 devc->num_block_read += len;
310 vref = devc->vert_reference[probe->index];
311 vdiv = devc->vdiv[probe->index] / 25.6;
312 offset = devc->vert_offset[probe->index];
313 if (devc->model->series == 2)
314 for (i = 0; i < len; i++)
315 data[i] = ((int)buf[i] - vref) * vdiv - offset;
317 for (i = 0; i < len; i++)
318 data[i] = (128 - buf[i]) * vdiv - offset;
319 analog.probes = g_slist_append(NULL, probe);
320 analog.num_samples = len;
322 analog.mq = SR_MQ_VOLTAGE;
323 analog.unit = SR_UNIT_VOLT;
325 packet.type = SR_DF_ANALOG;
326 packet.payload = &analog;
327 sr_session_send(cb_data, &packet);
328 g_slist_free(analog.probes);
330 if (devc->model->series == 2) {
331 devc->num_frame_bytes += len;
333 if (devc->num_frame_bytes < DS2000_ANALOG_WAVEFORM_SIZE)
334 /* Don't have the whole frame yet. */
337 sr_dbg("Frame completed, %d samples", devc->num_frame_bytes);
339 if (len != DS1000_ANALOG_WAVEFORM_SIZE)
340 /* Don't have the whole frame yet. */
344 logic.length = len - 10;
346 logic.data = buf + 10;
347 packet.type = SR_DF_LOGIC;
348 packet.payload = &logic;
349 sr_session_send(cb_data, &packet);
351 if (len != DIGITAL_WAVEFORM_SIZE)
352 /* Don't have the whole frame yet. */
356 /* End of the frame. */
357 packet.type = SR_DF_FRAME_END;
358 sr_session_send(sdi, &packet);
359 if (devc->model->series == 1)
360 devc->num_frame_bytes = 0;
362 if (devc->enabled_analog_probes
363 && devc->channel_frame == devc->enabled_analog_probes->data
364 && devc->enabled_analog_probes->next != NULL) {
365 /* We got the frame for the first analog channel, but
366 * there's a second analog channel. */
367 devc->channel_frame = devc->enabled_analog_probes->next->data;
368 if (devc->model->series == 2) {
369 /* Do not wait for trigger to try and keep channel data related. */
370 rigol_ds2xx2_acquisition_start(sdi, FALSE);
372 rigol_ds_send(sdi, ":WAV:DATA? CHAN%c",
373 devc->channel_frame->name[2]);
376 /* Done with both analog channels in this frame. */
377 if (devc->enabled_digital_probes
378 && devc->channel_frame != devc->enabled_digital_probes->data) {
379 /* Now we need to get the digital data. */
380 devc->channel_frame = devc->enabled_digital_probes->data;
381 rigol_ds_send(sdi, ":WAV:DATA? DIG");
382 } else if (++devc->num_frames == devc->limit_frames) {
383 /* End of last frame. */
384 packet.type = SR_DF_END;
385 sr_session_send(sdi, &packet);
386 sdi->driver->dev_acquisition_stop(sdi, cb_data);
388 /* Get the next frame, starting with the first analog channel. */
389 if (devc->model->series == 2) {
390 if (devc->enabled_analog_probes) {
391 devc->channel_frame = devc->enabled_analog_probes->data;
392 /* Must wait for trigger because at
393 * slow timebases the scope will
394 * return old data otherwise. */
395 rigol_ds2xx2_acquisition_start(sdi, TRUE);
398 if (devc->enabled_analog_probes) {
399 devc->channel_frame = devc->enabled_analog_probes->data;
400 rigol_ds_send(sdi, ":WAV:DATA? CHAN%c",
401 devc->channel_frame->name[2]);
403 devc->channel_frame = devc->enabled_digital_probes->data;
404 rigol_ds_send(sdi, ":WAV:DATA? DIG");
414 SR_PRIV int rigol_ds_send(const struct sr_dev_inst *sdi, const char *format, ...)
420 va_start(args, format);
421 len = vsnprintf(buf, 255, format, args);
425 out = serial_write(sdi->conn, buf, len);
428 sr_dbg("Only sent %d/%d bytes of '%s'.", out, len, buf);
431 sr_spew("Sent '%s'.", buf);
438 static int get_cfg(const struct sr_dev_inst *sdi, char *cmd, char *reply, size_t maxlen)
441 struct dev_context *devc = sdi->priv;
443 if (rigol_ds_send(sdi, cmd) != SR_OK)
446 if ((len = serial_read(sdi->conn, reply, maxlen - 1)) < 0)
450 if (devc->model->series == 2) {
451 /* get rid of trailing linefeed */
452 if (len >= 1 && reply[len-1] == '\n')
456 sr_spew("Received '%s'.", reply);
461 static int get_cfg_int(const struct sr_dev_inst *sdi, char *cmd, int *i)
465 if (get_cfg(sdi, cmd, buf, sizeof(buf)) != SR_OK)
468 if (parse_int(buf, i) != SR_OK)
474 static int get_cfg_float(const struct sr_dev_inst *sdi, char *cmd, float *f)
478 if (get_cfg(sdi, cmd, buf, sizeof(buf)) != SR_OK)
480 *f = strtof(buf, &e);
481 if (e == buf || (fpclassify(*f) & (FP_ZERO | FP_NORMAL)) == 0) {
482 sr_dbg("failed to parse response to '%s': '%s'", cmd, buf);
489 static int get_cfg_string(const struct sr_dev_inst *sdi, char *cmd, char **buf)
491 if (!(*buf = g_try_malloc0(256)))
492 return SR_ERR_MALLOC;
494 if (get_cfg(sdi, cmd, *buf, 256) != SR_OK)
500 SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi)
502 struct dev_context *devc;
508 /* Analog channel state. */
509 if (get_cfg_string(sdi, ":CHAN1:DISP?", &t_s) != SR_OK)
511 devc->analog_channels[0] = !strcmp(t_s, "ON") || !strcmp(t_s, "1");
513 if (get_cfg_string(sdi, ":CHAN2:DISP?", &t_s) != SR_OK)
515 devc->analog_channels[1] = !strcmp(t_s, "ON") || !strcmp(t_s, "1");
517 sr_dbg("Current analog channel state CH1 %s CH2 %s",
518 devc->analog_channels[0] ? "on" : "off",
519 devc->analog_channels[1] ? "on" : "off");
521 /* Digital channel state. */
522 if (devc->model->has_digital) {
523 sr_dbg("Current digital channel state:");
524 for (i = 0; i < 16; i++) {
525 cmd = g_strdup_printf(":DIG%d:TURN?", i);
526 res = get_cfg_string(sdi, cmd, &t_s);
530 devc->digital_channels[i] = !strcmp(t_s, "ON") ? TRUE : FALSE;
532 sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "on" : "off");
537 if (get_cfg_float(sdi, ":TIM:SCAL?", &devc->timebase) != SR_OK)
539 sr_dbg("Current timebase %g", devc->timebase);
542 if (get_cfg_float(sdi, ":CHAN1:SCAL?", &devc->vdiv[0]) != SR_OK)
544 if (get_cfg_float(sdi, ":CHAN2:SCAL?", &devc->vdiv[1]) != SR_OK)
546 sr_dbg("Current vertical gain CH1 %g CH2 %g", devc->vdiv[0], devc->vdiv[1]);
548 if (devc->model->series == 2) {
549 /* Vertical reference - not certain if this is the place to read it. */
550 if (rigol_ds_send(sdi, ":WAV:SOUR CHAN1") != SR_OK)
552 if (get_cfg_int(sdi, ":WAV:YREF?", &devc->vert_reference[0]) != SR_OK)
554 if (rigol_ds_send(sdi, ":WAV:SOUR CHAN2") != SR_OK)
556 if (get_cfg_int(sdi, ":WAV:YREF?", &devc->vert_reference[1]) != SR_OK)
558 sr_dbg("Current vertical reference CH1 %d CH2 %d",
559 devc->vert_reference[0], devc->vert_reference[1]);
562 /* Vertical offset. */
563 if (get_cfg_float(sdi, ":CHAN1:OFFS?", &devc->vert_offset[0]) != SR_OK)
565 if (get_cfg_float(sdi, ":CHAN2:OFFS?", &devc->vert_offset[1]) != SR_OK)
567 sr_dbg("Current vertical offset CH1 %g CH2 %g", devc->vert_offset[0],
568 devc->vert_offset[1]);
571 if (get_cfg_string(sdi, ":CHAN1:COUP?", &devc->coupling[0]) != SR_OK)
573 if (get_cfg_string(sdi, ":CHAN2:COUP?", &devc->coupling[1]) != SR_OK)
575 sr_dbg("Current coupling CH1 %s CH2 %s", devc->coupling[0],
578 /* Trigger source. */
579 if (get_cfg_string(sdi, ":TRIG:EDGE:SOUR?", &devc->trigger_source) != SR_OK)
581 sr_dbg("Current trigger source %s", devc->trigger_source);
583 /* Horizontal trigger position. */
584 if (get_cfg_float(sdi, ":TIM:OFFS?", &devc->horiz_triggerpos) != SR_OK)
586 sr_dbg("Current horizontal trigger position %g", devc->horiz_triggerpos);
589 if (get_cfg_string(sdi, ":TRIG:EDGE:SLOP?", &devc->trigger_slope) != SR_OK)
591 sr_dbg("Current trigger slope %s", devc->trigger_slope);