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 devc->channel_frame = devc->enabled_analog_probes->data;
174 sr_dbg("Starting acquisition on channel %d",
175 devc->channel_frame->index + 1);
177 if (rigol_ds_send(sdi, ":WAV:FORM BYTE") != SR_OK)
179 if (rigol_ds_send(sdi, ":WAV:SOUR CHAN%d",
180 devc->channel_frame->index + 1) != SR_OK)
182 if (rigol_ds_send(sdi, ":WAV:MODE NORM") != SR_OK)
185 devc->num_frame_bytes = 0;
186 devc->num_block_bytes = 0;
188 /* only wait for trigger if timbase 50 msecs/DIV or slower */
189 if (wait_for_trigger && devc->timebase > 0.0499)
191 devc->trigger_wait_status = 1;
193 devc->trigger_wait_status = 0;
199 static int rigol_ds2xx2_read_header(struct sr_serial_dev_inst *serial)
201 char start[3], length[10];
204 /* Read the hashsign and length digit. */
205 tmp = serial_read(serial, start, 2);
209 sr_err("Failed to read first two bytes of data block header.");
212 if (start[0] != '#' || !isdigit(start[1]) || start[1] == '0')
214 sr_err("Received invalid data block header start '%s'.", start);
217 len = atoi(start + 1);
219 /* Read the data length. */
220 tmp = serial_read(serial, length, len);
224 sr_err("Failed to read %d bytes of data block length.", len);
227 if (parse_int(length, &len) != SR_OK)
229 sr_err("Received invalid data block length '%s'.", length);
233 sr_dbg("Received data block header: %s%s -> block length %d", start, length, len);
238 SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
240 struct sr_dev_inst *sdi;
241 struct sr_serial_dev_inst *serial;
242 struct dev_context *devc;
243 struct sr_datafeed_packet packet;
244 struct sr_datafeed_analog analog;
245 struct sr_datafeed_logic logic;
246 unsigned char buf[DS2000_ANALOG_WAVEFORM_SIZE];
248 float data[DS2000_ANALOG_WAVEFORM_SIZE];
249 int len, i, waveform_size, vref;
250 struct sr_probe *probe;
254 if (!(sdi = cb_data))
257 if (!(devc = sdi->priv))
262 if (revents == G_IO_IN) {
263 if (devc->trigger_wait_status > 0) {
264 if (rigol_ds2xx2_trigger_wait(sdi) != SR_OK)
268 if (devc->model->series == 2 && devc->num_block_bytes == 0) {
269 sr_dbg("New block header expected");
270 if (rigol_ds_send(sdi, ":WAV:DATA?") != SR_OK)
272 len = rigol_ds2xx2_read_header(serial);
275 /* At slow timebases the scope sometimes returns
276 * "short" data blocks, with apparently no way to
277 * get the rest of the data. Discard these, the
278 * complete data block will appear eventually.
280 if (len < DS2000_ANALOG_WAVEFORM_SIZE) {
281 sr_dbg("Discarding short data block");
282 serial_read(serial, buf, len + 1);
285 devc->num_block_bytes = len;
286 devc->num_block_read = 0;
289 probe = devc->channel_frame;
290 if (devc->model->series == 2) {
291 len = devc->num_block_bytes - devc->num_block_read;
292 len = serial_read(serial, buf,
293 len < DS2000_ANALOG_WAVEFORM_SIZE ? len : DS2000_ANALOG_WAVEFORM_SIZE);
295 waveform_size = probe->type == SR_PROBE_ANALOG ?
296 DS1000_ANALOG_WAVEFORM_SIZE : DIGITAL_WAVEFORM_SIZE;
297 len = serial_read(serial, buf, waveform_size - devc->num_frame_bytes);
299 sr_dbg("Received %d bytes.", len);
303 if (devc->num_frame_bytes == 0) {
304 /* Start of a new frame. */
305 packet.type = SR_DF_FRAME_BEGIN;
306 sr_session_send(sdi, &packet);
309 if (probe->type == SR_PROBE_ANALOG) {
310 if (devc->model->series == 2)
311 devc->num_block_read += len;
312 vref = devc->vert_reference[probe->index];
313 vdiv = devc->vdiv[probe->index] / 25.6;
314 offset = devc->vert_offset[probe->index];
315 if (devc->model->series == 2)
316 for (i = 0; i < len; i++)
317 data[i] = ((int)buf[i] - vref) * vdiv - offset;
319 for (i = 0; i < len; i++)
320 data[i] = (128 - buf[i]) * vdiv - offset;
321 analog.probes = g_slist_append(NULL, probe);
322 analog.num_samples = len;
324 analog.mq = SR_MQ_VOLTAGE;
325 analog.unit = SR_UNIT_VOLT;
327 packet.type = SR_DF_ANALOG;
328 packet.payload = &analog;
329 sr_session_send(cb_data, &packet);
330 g_slist_free(analog.probes);
332 if (devc->model->series == 2) {
333 devc->num_frame_bytes += len;
335 if (devc->num_frame_bytes < DS2000_ANALOG_WAVEFORM_SIZE)
336 /* Don't have the whole frame yet. */
339 sr_dbg("Frame completed, %d samples", devc->num_frame_bytes);
341 if (len != DS1000_ANALOG_WAVEFORM_SIZE)
342 /* Don't have the whole frame yet. */
346 logic.length = len - 10;
348 logic.data = buf + 10;
349 packet.type = SR_DF_LOGIC;
350 packet.payload = &logic;
351 sr_session_send(cb_data, &packet);
353 if (len != DIGITAL_WAVEFORM_SIZE)
354 /* Don't have the whole frame yet. */
358 /* End of the frame. */
359 packet.type = SR_DF_FRAME_END;
360 sr_session_send(sdi, &packet);
361 if (devc->model->series == 1)
362 devc->num_frame_bytes = 0;
364 if (devc->enabled_analog_probes
365 && devc->channel_frame == devc->enabled_analog_probes->data
366 && devc->enabled_analog_probes->next != NULL) {
367 /* We got the frame for the first analog channel, but
368 * there's a second analog channel. */
369 if (devc->model->series == 2) {
370 /* Do not wait for trigger to try and keep channel data related. */
371 rigol_ds2xx2_acquisition_start(sdi, FALSE);
373 devc->channel_frame = devc->enabled_analog_probes->next->data;
374 rigol_ds_send(sdi, ":WAV:DATA? CHAN%c",
375 devc->channel_frame->name[2]);
378 /* Done with both analog channels in this frame. */
379 if (devc->enabled_digital_probes
380 && devc->channel_frame != devc->enabled_digital_probes->data) {
381 /* Now we need to get the digital data. */
382 devc->channel_frame = devc->enabled_digital_probes->data;
383 rigol_ds_send(sdi, ":WAV:DATA? DIG");
384 } else if (++devc->num_frames == devc->limit_frames) {
385 /* End of last frame. */
386 packet.type = SR_DF_END;
387 sr_session_send(sdi, &packet);
388 sdi->driver->dev_acquisition_stop(sdi, cb_data);
390 /* Get the next frame, starting with the first analog channel. */
391 if (devc->model->series == 2) {
392 if (devc->enabled_analog_probes) {
393 /* Must wait for trigger because at
394 * slow timebases the scope will
395 * return old data otherwise. */
396 rigol_ds2xx2_acquisition_start(sdi, TRUE);
399 if (devc->enabled_analog_probes) {
400 devc->channel_frame = devc->enabled_analog_probes->data;
401 rigol_ds_send(sdi, ":WAV:DATA? CHAN%c",
402 devc->channel_frame->name[2]);
404 devc->channel_frame = devc->enabled_digital_probes->data;
405 rigol_ds_send(sdi, ":WAV:DATA? DIG");
415 SR_PRIV int rigol_ds_send(const struct sr_dev_inst *sdi, const char *format, ...)
421 va_start(args, format);
422 len = vsnprintf(buf, 255, format, args);
426 out = serial_write(sdi->conn, buf, len);
429 sr_dbg("Only sent %d/%d bytes of '%s'.", out, len, buf);
432 sr_spew("Sent '%s'.", buf);
439 static int get_cfg(const struct sr_dev_inst *sdi, char *cmd, char *reply, size_t maxlen)
442 struct dev_context *devc = sdi->priv;
444 if (rigol_ds_send(sdi, cmd) != SR_OK)
447 if ((len = serial_read(sdi->conn, reply, maxlen - 1)) < 0)
451 if (devc->model->series == 2) {
452 /* get rid of trailing linefeed */
453 if (len >= 1 && reply[len-1] == '\n')
457 sr_spew("Received '%s'.", reply);
462 static int get_cfg_int(const struct sr_dev_inst *sdi, char *cmd, int *i)
466 if (get_cfg(sdi, cmd, buf, sizeof(buf)) != SR_OK)
469 if (parse_int(buf, i) != SR_OK)
475 static int get_cfg_float(const struct sr_dev_inst *sdi, char *cmd, float *f)
479 if (get_cfg(sdi, cmd, buf, sizeof(buf)) != SR_OK)
481 *f = strtof(buf, &e);
482 if (e == buf || (fpclassify(*f) & (FP_ZERO | FP_NORMAL)) == 0) {
483 sr_dbg("failed to parse response to '%s': '%s'", cmd, buf);
490 static int get_cfg_string(const struct sr_dev_inst *sdi, char *cmd, char **buf)
492 if (!(*buf = g_try_malloc0(256)))
493 return SR_ERR_MALLOC;
495 if (get_cfg(sdi, cmd, *buf, 256) != SR_OK)
501 SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi)
503 struct dev_context *devc;
509 /* Analog channel state. */
510 if (get_cfg_string(sdi, ":CHAN1:DISP?", &t_s) != SR_OK)
512 devc->analog_channels[0] = !strcmp(t_s, "ON") || !strcmp(t_s, "1");
514 if (get_cfg_string(sdi, ":CHAN2:DISP?", &t_s) != SR_OK)
516 devc->analog_channels[1] = !strcmp(t_s, "ON") || !strcmp(t_s, "1");
518 sr_dbg("Current analog channel state CH1 %s CH2 %s",
519 devc->analog_channels[0] ? "on" : "off",
520 devc->analog_channels[1] ? "on" : "off");
522 /* Digital channel state. */
523 if (devc->model->has_digital) {
524 sr_dbg("Current digital channel state:");
525 for (i = 0; i < 16; i++) {
526 cmd = g_strdup_printf(":DIG%d:TURN?", i);
527 res = get_cfg_string(sdi, cmd, &t_s);
531 devc->digital_channels[i] = !strcmp(t_s, "ON") ? TRUE : FALSE;
533 sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "on" : "off");
538 if (get_cfg_float(sdi, ":TIM:SCAL?", &devc->timebase) != SR_OK)
540 sr_dbg("Current timebase %g", devc->timebase);
543 if (get_cfg_float(sdi, ":CHAN1:SCAL?", &devc->vdiv[0]) != SR_OK)
545 if (get_cfg_float(sdi, ":CHAN2:SCAL?", &devc->vdiv[1]) != SR_OK)
547 sr_dbg("Current vertical gain CH1 %g CH2 %g", devc->vdiv[0], devc->vdiv[1]);
549 if (devc->model->series == 2) {
550 /* Vertical reference - not certain if this is the place to read it. */
551 if (rigol_ds_send(sdi, ":WAV:SOUR CHAN1") != SR_OK)
553 if (get_cfg_int(sdi, ":WAV:YREF?", &devc->vert_reference[0]) != SR_OK)
555 if (rigol_ds_send(sdi, ":WAV:SOUR CHAN2") != SR_OK)
557 if (get_cfg_int(sdi, ":WAV:YREF?", &devc->vert_reference[1]) != SR_OK)
559 sr_dbg("Current vertical reference CH1 %d CH2 %d",
560 devc->vert_reference[0], devc->vert_reference[1]);
563 /* Vertical offset. */
564 if (get_cfg_float(sdi, ":CHAN1:OFFS?", &devc->vert_offset[0]) != SR_OK)
566 if (get_cfg_float(sdi, ":CHAN2:OFFS?", &devc->vert_offset[1]) != SR_OK)
568 sr_dbg("Current vertical offset CH1 %g CH2 %g", devc->vert_offset[0],
569 devc->vert_offset[1]);
572 if (get_cfg_string(sdi, ":CHAN1:COUP?", &devc->coupling[0]) != SR_OK)
574 if (get_cfg_string(sdi, ":CHAN2:COUP?", &devc->coupling[1]) != SR_OK)
576 sr_dbg("Current coupling CH1 %s CH2 %s", devc->coupling[0],
579 /* Trigger source. */
580 if (get_cfg_string(sdi, ":TRIG:EDGE:SOUR?", &devc->trigger_source) != SR_OK)
582 sr_dbg("Current trigger source %s", devc->trigger_source);
584 /* Horizontal trigger position. */
585 if (get_cfg_float(sdi, ":TIM:OFFS?", &devc->horiz_triggerpos) != SR_OK)
587 sr_dbg("Current horizontal trigger position %g", devc->horiz_triggerpos);
590 if (get_cfg_string(sdi, ":TRIG:EDGE:SLOP?", &devc->trigger_slope) != SR_OK)
592 sr_dbg("Current trigger slope %s", devc->trigger_slope);