]>
Commit | Line | Data |
---|---|---|
f4816ac6 ML |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Martin Ling <martin-git@earth.li> | |
88e429c9 | 5 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> |
bafd4890 | 6 | * Copyright (C) 2013 Mathias Grimmberger <mgri@zaphod.sax.de> |
f4816ac6 ML |
7 | * |
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. | |
12 | * | |
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. | |
17 | * | |
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/>. | |
20 | */ | |
21 | ||
22 | #include <stdlib.h> | |
e0b7d23c ML |
23 | #include <stdarg.h> |
24 | #include <unistd.h> | |
25 | #include <errno.h> | |
a3df166f | 26 | #include <string.h> |
254dd102 | 27 | #include <math.h> |
bafd4890 ML |
28 | #include <ctype.h> |
29 | #include <time.h> | |
f4816ac6 ML |
30 | #include <glib.h> |
31 | #include "libsigrok.h" | |
32 | #include "libsigrok-internal.h" | |
33 | #include "protocol.h" | |
34 | ||
bafd4890 ML |
35 | /* |
36 | * This is a unified protocol driver for the DS1000 and DS2000 series. | |
37 | * | |
38 | * DS1000 support tested with a Rigol DS1102D. | |
39 | * | |
40 | * DS2000 support tested with a Rigol DS2072 using firmware version 01.01.00.02. | |
41 | * | |
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... | |
44 | * | |
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. | |
50 | * | |
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. | |
54 | * | |
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. | |
61 | * | |
62 | * After this header as many data bytes as indicated follow. | |
63 | * | |
64 | * Each data block has a trailing linefeed too. | |
65 | */ | |
66 | ||
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); | |
69 | ||
70 | static int parse_int(const char *str, int *ret) | |
71 | { | |
72 | char *e; | |
73 | long tmp; | |
74 | ||
75 | errno = 0; | |
76 | tmp = strtol(str, &e, 10); | |
77 | if (e == str || *e != '\0') { | |
78 | sr_dbg("Failed to parse integer: '%s'", str); | |
79 | return SR_ERR; | |
80 | } | |
81 | if (errno) { | |
82 | sr_dbg("Failed to parse integer: '%s', numerical overflow", str); | |
83 | return SR_ERR; | |
84 | } | |
85 | if (tmp > INT_MAX || tmp < INT_MIN) { | |
86 | sr_dbg("Failed to parse integer: '%s', value to large/small", str); | |
87 | return SR_ERR; | |
88 | } | |
89 | ||
90 | *ret = (int)tmp; | |
91 | return SR_OK; | |
92 | } | |
93 | ||
babab622 ML |
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) | |
96 | { | |
97 | if (event == WAIT_STOP) | |
98 | devc->wait_status = 2; | |
99 | else | |
100 | devc->wait_status = 1; | |
101 | devc->wait_event = event; | |
102 | } | |
103 | ||
bafd4890 | 104 | /* |
babab622 ML |
105 | * Waiting for a event will return a timeout after 2 to 3 seconds in order |
106 | * to not block the application. | |
bafd4890 | 107 | */ |
babab622 | 108 | static int rigol_ds_event_wait(const struct sr_dev_inst *sdi, char status1, char status2) |
bafd4890 ML |
109 | { |
110 | char buf[20]; | |
111 | struct dev_context *devc; | |
112 | time_t start; | |
113 | ||
114 | if (!(devc = sdi->priv)) | |
115 | return SR_ERR; | |
116 | ||
117 | start = time(NULL); | |
118 | ||
119 | /* | |
120 | * Trigger status may return: | |
babab622 ML |
121 | * "TD" or "T'D" - triggered |
122 | * "AUTO" - autotriggered | |
123 | * "RUN" - running | |
124 | * "WAIT" - waiting for trigger | |
125 | * "STOP" - stopped | |
bafd4890 ML |
126 | */ |
127 | ||
babab622 | 128 | if (devc->wait_status == 1) { |
bafd4890 ML |
129 | do { |
130 | if (time(NULL) - start >= 3) { | |
131 | sr_dbg("Timeout waiting for trigger"); | |
132 | return SR_ERR_TIMEOUT; | |
133 | } | |
134 | ||
135 | if (get_cfg(sdi, ":TRIG:STAT?", buf, sizeof(buf)) != SR_OK) | |
136 | return SR_ERR; | |
babab622 | 137 | } while (buf[0] == status1 || buf[0] == status2); |
bafd4890 | 138 | |
babab622 | 139 | devc->wait_status = 2; |
bafd4890 | 140 | } |
babab622 | 141 | if (devc->wait_status == 2) { |
bafd4890 ML |
142 | do { |
143 | if (time(NULL) - start >= 3) { | |
144 | sr_dbg("Timeout waiting for trigger"); | |
145 | return SR_ERR_TIMEOUT; | |
146 | } | |
147 | ||
148 | if (get_cfg(sdi, ":TRIG:STAT?", buf, sizeof(buf)) != SR_OK) | |
149 | return SR_ERR; | |
babab622 | 150 | } while (buf[0] != status1 && buf[0] != status2); |
bafd4890 | 151 | |
babab622 | 152 | rigol_ds_set_wait_event(devc, WAIT_NONE); |
bafd4890 ML |
153 | } |
154 | ||
155 | return SR_OK; | |
156 | } | |
157 | ||
158 | /* | |
babab622 ML |
159 | * For live capture we need to wait for a new trigger event to ensure that |
160 | * sample data is not returned twice. | |
bafd4890 ML |
161 | * |
162 | * Unfortunately this will never really work because for sufficiently fast | |
babab622 | 163 | * timebases and trigger rates it just can't catch the status changes. |
bafd4890 ML |
164 | * |
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. | |
167 | * | |
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 | |
babab622 ML |
170 | * returned multiple times, this effect is mitigated somewhat by sleeping |
171 | * for about one sweep time in that case. | |
bafd4890 | 172 | */ |
babab622 | 173 | static int rigol_ds_trigger_wait(const struct sr_dev_inst *sdi) |
bafd4890 ML |
174 | { |
175 | struct dev_context *devc; | |
babab622 | 176 | long s; |
bafd4890 ML |
177 | |
178 | if (!(devc = sdi->priv)) | |
179 | return SR_ERR; | |
180 | ||
babab622 ML |
181 | /* |
182 | * If timebase < 50 msecs/DIV just sleep about one sweep time except | |
183 | * for really fast sweeps. | |
184 | */ | |
185 | if (devc->timebase < 0.0499) | |
186 | { | |
187 | if (devc->timebase > 0.99e-6) { | |
188 | /* | |
189 | * Timebase * num hor. divs * 85(%) * 1e6(usecs) / 100 | |
190 | * -> 85 percent of sweep time | |
191 | */ | |
192 | s = (devc->timebase * devc->model->num_horizontal_divs | |
193 | * 85e6) / 100L; | |
194 | sr_spew("Sleeping for %ld usecs instead of trigger-wait", s); | |
195 | g_usleep(s); | |
196 | } | |
197 | rigol_ds_set_wait_event(devc, WAIT_NONE); | |
198 | return SR_OK; | |
199 | } else { | |
200 | return rigol_ds_event_wait(sdi, 'T', 'A'); | |
201 | } | |
202 | } | |
bafd4890 | 203 | |
babab622 ML |
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) | |
206 | { | |
207 | return rigol_ds_event_wait(sdi, 'S', 'S'); | |
208 | } | |
209 | ||
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) | |
212 | { | |
213 | struct dev_context *devc; | |
214 | int tmp; | |
215 | ||
216 | if (!(devc = sdi->priv)) | |
bafd4890 | 217 | return SR_ERR; |
babab622 | 218 | |
17b5b202 | 219 | if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d", |
babab622 ML |
220 | devc->channel_frame->index + 1) != SR_OK) |
221 | return SR_ERR; | |
222 | /* Check that the number of samples will be accepted */ | |
17b5b202 | 223 | if (sr_scpi_send(sdi->conn, ":WAV:POIN %d;*OPC", devc->analog_frame_size) != SR_OK) |
babab622 ML |
224 | return SR_ERR; |
225 | if (get_cfg_int(sdi, "*ESR?", &tmp) != SR_OK) | |
bafd4890 | 226 | return SR_ERR; |
babab622 ML |
227 | /* |
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. | |
233 | * | |
234 | * In that case just try to capture something again. Might still | |
235 | * fail in interesting ways. | |
236 | * | |
237 | * Ain't firmware fun? | |
238 | */ | |
239 | if (tmp & 0x10) { | |
240 | sr_warn("Single shot acquisition failed, retrying..."); | |
241 | /* Sleep a bit, otherwise the single shot will often fail */ | |
242 | g_usleep(500000); | |
17b5b202 | 243 | sr_scpi_send(sdi->conn, ":SING"); |
babab622 | 244 | rigol_ds_set_wait_event(devc, WAIT_STOP); |
bafd4890 | 245 | return SR_ERR; |
babab622 | 246 | } |
bafd4890 | 247 | |
babab622 ML |
248 | return SR_OK; |
249 | } | |
bafd4890 | 250 | |
babab622 ML |
251 | /* Wait for enough data becoming available in scope output buffer */ |
252 | static int rigol_ds_block_wait(const struct sr_dev_inst *sdi) | |
253 | { | |
254 | char buf[30]; | |
255 | struct dev_context *devc; | |
256 | time_t start; | |
257 | int len; | |
258 | ||
259 | if (!(devc = sdi->priv)) | |
260 | return SR_ERR; | |
261 | ||
262 | start = time(NULL); | |
263 | ||
264 | do { | |
265 | if (time(NULL) - start >= 3) { | |
266 | sr_dbg("Timeout waiting for data block"); | |
267 | return SR_ERR_TIMEOUT; | |
268 | } | |
269 | ||
270 | /* | |
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. | |
275 | */ | |
276 | g_usleep(devc->analog_frame_size < 15000 ? 100000 : 1000000); | |
277 | ||
278 | /* "READ,nnnn" (still working) or "IDLE,nnnn" (finished) */ | |
279 | if (get_cfg(sdi, ":WAV:STAT?", buf, sizeof(buf)) != SR_OK) | |
280 | return SR_ERR; | |
281 | ||
282 | if (parse_int(buf + 5, &len) != SR_OK) | |
283 | return SR_ERR; | |
284 | } while (buf[0] == 'R' && len < 1000000); | |
285 | ||
286 | rigol_ds_set_wait_event(devc, WAIT_NONE); | |
287 | ||
288 | return SR_OK; | |
289 | } | |
290 | ||
291 | /* Start capturing a new frameset */ | |
292 | SR_PRIV int rigol_ds_capture_start(const struct sr_dev_inst *sdi) | |
293 | { | |
294 | struct dev_context *devc; | |
295 | ||
296 | if (!(devc = sdi->priv)) | |
297 | return SR_ERR; | |
298 | ||
299 | sr_dbg("Starting data capture for frameset %lu of %lu", | |
300 | devc->num_frames + 1, devc->limit_frames); | |
301 | ||
17b5b202 | 302 | if (sr_scpi_send(sdi->conn, ":WAV:FORM BYTE") != SR_OK) |
babab622 ML |
303 | return SR_ERR; |
304 | if (devc->data_source == DATA_SOURCE_LIVE) { | |
17b5b202 | 305 | if (sr_scpi_send(sdi->conn, ":WAV:MODE NORM") != SR_OK) |
babab622 ML |
306 | return SR_ERR; |
307 | rigol_ds_set_wait_event(devc, WAIT_TRIGGER); | |
bafd4890 | 308 | } else { |
17b5b202 | 309 | if (sr_scpi_send(sdi->conn, ":WAV:MODE RAW") != SR_OK) |
babab622 | 310 | return SR_ERR; |
17b5b202 | 311 | if (sr_scpi_send(sdi->conn, ":SING", devc->analog_frame_size) != SR_OK) |
babab622 ML |
312 | return SR_ERR; |
313 | rigol_ds_set_wait_event(devc, WAIT_STOP); | |
bafd4890 ML |
314 | } |
315 | ||
316 | return SR_OK; | |
317 | } | |
318 | ||
babab622 ML |
319 | /* Start reading data from the current channel */ |
320 | SR_PRIV int rigol_ds_channel_start(const struct sr_dev_inst *sdi) | |
321 | { | |
322 | struct dev_context *devc; | |
323 | ||
324 | if (!(devc = sdi->priv)) | |
325 | return SR_ERR; | |
326 | ||
327 | sr_dbg("Starting reading data from channel %d", | |
328 | devc->channel_frame->index + 1); | |
329 | ||
17b5b202 | 330 | if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d", |
babab622 ML |
331 | devc->channel_frame->index + 1) != SR_OK) |
332 | return SR_ERR; | |
333 | if (devc->data_source != DATA_SOURCE_LIVE) { | |
17b5b202 | 334 | if (sr_scpi_send(sdi->conn, ":WAV:RES") != SR_OK) |
babab622 | 335 | return SR_ERR; |
17b5b202 | 336 | if (sr_scpi_send(sdi->conn, ":WAV:BEG") != SR_OK) |
babab622 ML |
337 | return SR_ERR; |
338 | rigol_ds_set_wait_event(devc, WAIT_BLOCK); | |
339 | } else | |
340 | rigol_ds_set_wait_event(devc, WAIT_NONE); | |
341 | ||
342 | devc->num_frame_bytes = 0; | |
343 | devc->num_block_bytes = 0; | |
344 | ||
345 | return SR_OK; | |
346 | } | |
347 | ||
348 | /* Read the header of a data block */ | |
ae1bc1cc | 349 | static int rigol_ds_read_header(struct sr_scpi_dev_inst *scpi) |
bafd4890 ML |
350 | { |
351 | char start[3], length[10]; | |
352 | int len, tmp; | |
353 | ||
354 | /* Read the hashsign and length digit. */ | |
ae1bc1cc | 355 | tmp = sr_scpi_read(scpi, start, 2); |
bafd4890 ML |
356 | start[2] = '\0'; |
357 | if (tmp != 2) | |
358 | { | |
359 | sr_err("Failed to read first two bytes of data block header."); | |
360 | return -1; | |
361 | } | |
362 | if (start[0] != '#' || !isdigit(start[1]) || start[1] == '0') | |
363 | { | |
364 | sr_err("Received invalid data block header start '%s'.", start); | |
365 | return -1; | |
366 | } | |
367 | len = atoi(start + 1); | |
368 | ||
369 | /* Read the data length. */ | |
ae1bc1cc | 370 | tmp = sr_scpi_read(scpi, length, len); |
bafd4890 ML |
371 | length[len] = '\0'; |
372 | if (tmp != len) | |
373 | { | |
374 | sr_err("Failed to read %d bytes of data block length.", len); | |
375 | return -1; | |
376 | } | |
377 | if (parse_int(length, &len) != SR_OK) | |
378 | { | |
379 | sr_err("Received invalid data block length '%s'.", length); | |
380 | return -1; | |
381 | } | |
382 | ||
383 | sr_dbg("Received data block header: %s%s -> block length %d", start, length, len); | |
384 | ||
385 | return len; | |
386 | } | |
387 | ||
3086efdd | 388 | SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) |
f4816ac6 | 389 | { |
e0b7d23c | 390 | struct sr_dev_inst *sdi; |
ae1bc1cc | 391 | struct sr_scpi_dev_inst *scpi; |
f4816ac6 | 392 | struct dev_context *devc; |
e0b7d23c ML |
393 | struct sr_datafeed_packet packet; |
394 | struct sr_datafeed_analog analog; | |
6bb192bc | 395 | struct sr_datafeed_logic logic; |
254dd102 | 396 | double vdiv, offset; |
bafd4890 | 397 | int len, i, waveform_size, vref; |
6bb192bc | 398 | struct sr_probe *probe; |
f4816ac6 | 399 | |
decfe89d | 400 | (void)fd; |
9bd4c956 | 401 | |
f4816ac6 ML |
402 | if (!(sdi = cb_data)) |
403 | return TRUE; | |
404 | ||
405 | if (!(devc = sdi->priv)) | |
406 | return TRUE; | |
407 | ||
ae1bc1cc | 408 | scpi = sdi->conn; |
9bd4c956 | 409 | |
f4816ac6 | 410 | if (revents == G_IO_IN) { |
babab622 ML |
411 | if (devc->model->protocol == PROTOCOL_IEEE488_2) { |
412 | switch(devc->wait_event) { | |
413 | case WAIT_NONE: | |
414 | break; | |
bafd4890 | 415 | |
babab622 ML |
416 | case WAIT_TRIGGER: |
417 | if (rigol_ds_trigger_wait(sdi) != SR_OK) | |
418 | return TRUE; | |
419 | if (rigol_ds_channel_start(sdi) != SR_OK) | |
420 | return TRUE; | |
421 | break; | |
422 | ||
423 | case WAIT_BLOCK: | |
424 | if (rigol_ds_block_wait(sdi) != SR_OK) | |
425 | return TRUE; | |
426 | break; | |
427 | ||
428 | case WAIT_STOP: | |
429 | if (rigol_ds_stop_wait(sdi) != SR_OK) | |
430 | return TRUE; | |
431 | if (rigol_ds_check_stop(sdi) != SR_OK) | |
432 | return TRUE; | |
433 | if (rigol_ds_channel_start(sdi) != SR_OK) | |
434 | return TRUE; | |
bafd4890 | 435 | return TRUE; |
babab622 ML |
436 | |
437 | default: | |
438 | sr_err("BUG: Unknown event target encountered"); | |
439 | } | |
440 | ||
441 | if (devc->num_block_bytes == 0) { | |
442 | sr_dbg("New block header expected"); | |
17b5b202 | 443 | if (sr_scpi_send(sdi->conn, ":WAV:DATA?") != SR_OK) |
babab622 | 444 | return TRUE; |
ae1bc1cc | 445 | len = rigol_ds_read_header(scpi); |
babab622 ML |
446 | if (len == -1) |
447 | return TRUE; | |
448 | /* At slow timebases in live capture the DS2072 | |
449 | * sometimes returns "short" data blocks, with | |
450 | * apparently no way to get the rest of the data. | |
451 | * Discard these, the complete data block will | |
452 | * appear eventually. | |
453 | */ | |
454 | if (devc->data_source == DATA_SOURCE_LIVE | |
455 | && (unsigned)len < devc->num_frame_bytes) { | |
456 | sr_dbg("Discarding short data block"); | |
ae1bc1cc | 457 | sr_scpi_read(scpi, (char *)devc->buffer, len + 1); |
babab622 ML |
458 | return TRUE; |
459 | } | |
460 | devc->num_block_bytes = len; | |
461 | devc->num_block_read = 0; | |
bafd4890 | 462 | } |
bafd4890 ML |
463 | } |
464 | ||
6bb192bc | 465 | probe = devc->channel_frame; |
babab622 | 466 | if (devc->model->protocol == PROTOCOL_IEEE488_2) { |
bafd4890 | 467 | len = devc->num_block_bytes - devc->num_block_read; |
ae1bc1cc | 468 | len = sr_scpi_read(scpi, (char *)devc->buffer, |
babab622 | 469 | len < ACQ_BUFFER_SIZE ? len : ACQ_BUFFER_SIZE); |
bafd4890 ML |
470 | } else { |
471 | waveform_size = probe->type == SR_PROBE_ANALOG ? | |
babab622 | 472 | DS1000_ANALOG_LIVE_WAVEFORM_SIZE : DIGITAL_WAVEFORM_SIZE; |
ae1bc1cc ML |
473 | len = sr_scpi_read(scpi, (char *)devc->buffer, |
474 | waveform_size - devc->num_frame_bytes); | |
bafd4890 | 475 | } |
29d957ce | 476 | sr_dbg("Received %d bytes.", len); |
e0b7d23c ML |
477 | if (len == -1) |
478 | return TRUE; | |
75d8a4e5 | 479 | |
ee7e9bee | 480 | if (devc->num_frame_bytes == 0) { |
75d8a4e5 BV |
481 | /* Start of a new frame. */ |
482 | packet.type = SR_DF_FRAME_BEGIN; | |
483 | sr_session_send(sdi, &packet); | |
484 | } | |
485 | ||
6bb192bc | 486 | if (probe->type == SR_PROBE_ANALOG) { |
babab622 | 487 | if (devc->model->protocol == PROTOCOL_IEEE488_2) |
bafd4890 ML |
488 | devc->num_block_read += len; |
489 | vref = devc->vert_reference[probe->index]; | |
490 | vdiv = devc->vdiv[probe->index] / 25.6; | |
491 | offset = devc->vert_offset[probe->index]; | |
babab622 | 492 | if (devc->model->protocol == PROTOCOL_IEEE488_2) |
bafd4890 | 493 | for (i = 0; i < len; i++) |
babab622 | 494 | devc->data[i] = ((int)devc->buffer[i] - vref) * vdiv - offset; |
bafd4890 ML |
495 | else |
496 | for (i = 0; i < len; i++) | |
babab622 | 497 | devc->data[i] = (128 - devc->buffer[i]) * vdiv - offset; |
6bb192bc ML |
498 | analog.probes = g_slist_append(NULL, probe); |
499 | analog.num_samples = len; | |
babab622 | 500 | analog.data = devc->data; |
6bb192bc ML |
501 | analog.mq = SR_MQ_VOLTAGE; |
502 | analog.unit = SR_UNIT_VOLT; | |
503 | analog.mqflags = 0; | |
504 | packet.type = SR_DF_ANALOG; | |
505 | packet.payload = &analog; | |
506 | sr_session_send(cb_data, &packet); | |
507 | g_slist_free(analog.probes); | |
508 | ||
babab622 ML |
509 | if (devc->model->protocol == PROTOCOL_IEEE488_2) { |
510 | if (devc->num_block_read == devc->num_block_bytes) { | |
511 | sr_dbg("Block has been completed"); | |
512 | /* Discard the terminating linefeed and prepare for | |
513 | possible next block */ | |
ae1bc1cc | 514 | sr_scpi_read(scpi, (char *)devc->buffer, 1); |
babab622 ML |
515 | devc->num_block_bytes = 0; |
516 | if (devc->data_source != DATA_SOURCE_LIVE) | |
517 | rigol_ds_set_wait_event(devc, WAIT_BLOCK); | |
518 | } else | |
519 | sr_dbg("%d of %d block bytes read", devc->num_block_read, devc->num_block_bytes); | |
56868b5d | 520 | } |
babab622 | 521 | |
56868b5d | 522 | devc->num_frame_bytes += len; |
bafd4890 | 523 | |
56868b5d ML |
524 | if (devc->num_frame_bytes < devc->analog_frame_size) |
525 | /* Don't have the whole frame yet. */ | |
526 | return TRUE; | |
bafd4890 | 527 | |
56868b5d | 528 | sr_dbg("Frame completed, %d samples", devc->num_frame_bytes); |
6bb192bc ML |
529 | } else { |
530 | logic.length = len - 10; | |
531 | logic.unitsize = 2; | |
babab622 | 532 | logic.data = devc->buffer + 10; |
6bb192bc ML |
533 | packet.type = SR_DF_LOGIC; |
534 | packet.payload = &logic; | |
535 | sr_session_send(cb_data, &packet); | |
536 | ||
537 | if (len != DIGITAL_WAVEFORM_SIZE) | |
538 | /* Don't have the whole frame yet. */ | |
539 | return TRUE; | |
ee7e9bee | 540 | } |
75d8a4e5 | 541 | |
254dd102 BV |
542 | /* End of the frame. */ |
543 | packet.type = SR_DF_FRAME_END; | |
544 | sr_session_send(sdi, &packet); | |
babab622 | 545 | if (devc->model->protocol == PROTOCOL_LEGACY) |
bafd4890 | 546 | devc->num_frame_bytes = 0; |
babab622 ML |
547 | else { |
548 | /* Signal end of data download to scope */ | |
549 | if (devc->data_source != DATA_SOURCE_LIVE) | |
550 | /* | |
551 | * This causes a query error, without it switching | |
552 | * to the next channel causes an error. Fun with | |
553 | * firmware... | |
554 | */ | |
17b5b202 | 555 | sr_scpi_send(sdi->conn, ":WAV:END"); |
babab622 | 556 | } |
254dd102 | 557 | |
6bb192bc ML |
558 | if (devc->enabled_analog_probes |
559 | && devc->channel_frame == devc->enabled_analog_probes->data | |
560 | && devc->enabled_analog_probes->next != NULL) { | |
561 | /* We got the frame for the first analog channel, but | |
562 | * there's a second analog channel. */ | |
355de5a1 | 563 | devc->channel_frame = devc->enabled_analog_probes->next->data; |
babab622 ML |
564 | if (devc->model->protocol == PROTOCOL_IEEE488_2) { |
565 | rigol_ds_channel_start(sdi); | |
bafd4890 | 566 | } else { |
17b5b202 | 567 | sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%c", |
bafd4890 ML |
568 | devc->channel_frame->name[2]); |
569 | } | |
254dd102 | 570 | } else { |
6bb192bc ML |
571 | /* Done with both analog channels in this frame. */ |
572 | if (devc->enabled_digital_probes | |
573 | && devc->channel_frame != devc->enabled_digital_probes->data) { | |
574 | /* Now we need to get the digital data. */ | |
575 | devc->channel_frame = devc->enabled_digital_probes->data; | |
17b5b202 | 576 | sr_scpi_send(sdi->conn, ":WAV:DATA? DIG"); |
6bb192bc ML |
577 | } else if (++devc->num_frames == devc->limit_frames) { |
578 | /* End of last frame. */ | |
1130e422 DJ |
579 | packet.type = SR_DF_END; |
580 | sr_session_send(sdi, &packet); | |
75d8a4e5 | 581 | sdi->driver->dev_acquisition_stop(sdi, cb_data); |
254dd102 | 582 | } else { |
6bb192bc | 583 | /* Get the next frame, starting with the first analog channel. */ |
babab622 | 584 | if (devc->model->protocol == PROTOCOL_IEEE488_2) { |
bafd4890 | 585 | if (devc->enabled_analog_probes) { |
355de5a1 | 586 | devc->channel_frame = devc->enabled_analog_probes->data; |
babab622 | 587 | rigol_ds_capture_start(sdi); |
bafd4890 | 588 | } |
6bb192bc | 589 | } else { |
bafd4890 ML |
590 | if (devc->enabled_analog_probes) { |
591 | devc->channel_frame = devc->enabled_analog_probes->data; | |
17b5b202 | 592 | sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%c", |
bafd4890 ML |
593 | devc->channel_frame->name[2]); |
594 | } else { | |
595 | devc->channel_frame = devc->enabled_digital_probes->data; | |
17b5b202 | 596 | sr_scpi_send(sdi->conn, ":WAV:DATA? DIG"); |
bafd4890 | 597 | } |
6bb192bc | 598 | } |
254dd102 | 599 | } |
75d8a4e5 | 600 | } |
f4816ac6 ML |
601 | } |
602 | ||
603 | return TRUE; | |
604 | } | |
e0b7d23c | 605 | |
bafd4890 | 606 | static int get_cfg(const struct sr_dev_inst *sdi, char *cmd, char *reply, size_t maxlen) |
254dd102 | 607 | { |
254dd102 | 608 | int len; |
bafd4890 | 609 | struct dev_context *devc = sdi->priv; |
ae1bc1cc ML |
610 | struct sr_scpi_dev_inst *scpi = sdi->conn; |
611 | char *response; | |
254dd102 | 612 | |
ae1bc1cc | 613 | if (sr_scpi_send(scpi, cmd) != SR_OK) |
254dd102 BV |
614 | return SR_ERR; |
615 | ||
ae1bc1cc | 616 | if (sr_scpi_receive(scpi, &response) != SR_OK) |
254dd102 | 617 | return SR_ERR; |
ae1bc1cc ML |
618 | |
619 | g_strlcpy(reply, response, maxlen); | |
620 | g_free(response); | |
621 | len = strlen(reply); | |
bafd4890 | 622 | |
babab622 | 623 | if (devc->model->protocol == PROTOCOL_IEEE488_2) { |
bafd4890 ML |
624 | /* get rid of trailing linefeed */ |
625 | if (len >= 1 && reply[len-1] == '\n') | |
626 | reply[len-1] = '\0'; | |
627 | } | |
628 | ||
254dd102 BV |
629 | sr_spew("Received '%s'.", reply); |
630 | ||
631 | return SR_OK; | |
632 | } | |
633 | ||
bafd4890 ML |
634 | static int get_cfg_int(const struct sr_dev_inst *sdi, char *cmd, int *i) |
635 | { | |
636 | char buf[32]; | |
637 | ||
638 | if (get_cfg(sdi, cmd, buf, sizeof(buf)) != SR_OK) | |
639 | return SR_ERR; | |
640 | ||
641 | if (parse_int(buf, i) != SR_OK) | |
642 | return SR_ERR; | |
643 | ||
644 | return SR_OK; | |
645 | } | |
646 | ||
254dd102 BV |
647 | static int get_cfg_float(const struct sr_dev_inst *sdi, char *cmd, float *f) |
648 | { | |
bafd4890 | 649 | char buf[32], *e; |
254dd102 | 650 | |
bafd4890 | 651 | if (get_cfg(sdi, cmd, buf, sizeof(buf)) != SR_OK) |
254dd102 BV |
652 | return SR_ERR; |
653 | *f = strtof(buf, &e); | |
169dbe85 | 654 | if (e == buf || (fpclassify(*f) & (FP_ZERO | FP_NORMAL)) == 0) { |
254dd102 BV |
655 | sr_dbg("failed to parse response to '%s': '%s'", cmd, buf); |
656 | return SR_ERR; | |
657 | } | |
658 | ||
659 | return SR_OK; | |
660 | } | |
661 | ||
662 | static int get_cfg_string(const struct sr_dev_inst *sdi, char *cmd, char **buf) | |
663 | { | |
254dd102 | 664 | if (!(*buf = g_try_malloc0(256))) |
169dbe85 | 665 | return SR_ERR_MALLOC; |
254dd102 | 666 | |
bafd4890 | 667 | if (get_cfg(sdi, cmd, *buf, 256) != SR_OK) |
254dd102 BV |
668 | return SR_ERR; |
669 | ||
670 | return SR_OK; | |
671 | } | |
672 | ||
3086efdd | 673 | SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi) |
254dd102 BV |
674 | { |
675 | struct dev_context *devc; | |
6bb192bc ML |
676 | char *t_s, *cmd; |
677 | int i, res; | |
254dd102 BV |
678 | |
679 | devc = sdi->priv; | |
680 | ||
6bb192bc | 681 | /* Analog channel state. */ |
254dd102 BV |
682 | if (get_cfg_string(sdi, ":CHAN1:DISP?", &t_s) != SR_OK) |
683 | return SR_ERR; | |
bafd4890 | 684 | devc->analog_channels[0] = !strcmp(t_s, "ON") || !strcmp(t_s, "1"); |
254dd102 BV |
685 | g_free(t_s); |
686 | if (get_cfg_string(sdi, ":CHAN2:DISP?", &t_s) != SR_OK) | |
687 | return SR_ERR; | |
bafd4890 | 688 | devc->analog_channels[1] = !strcmp(t_s, "ON") || !strcmp(t_s, "1"); |
254dd102 | 689 | g_free(t_s); |
6bb192bc ML |
690 | sr_dbg("Current analog channel state CH1 %s CH2 %s", |
691 | devc->analog_channels[0] ? "on" : "off", | |
692 | devc->analog_channels[1] ? "on" : "off"); | |
693 | ||
694 | /* Digital channel state. */ | |
bafd4890 | 695 | if (devc->model->has_digital) { |
6bb192bc ML |
696 | sr_dbg("Current digital channel state:"); |
697 | for (i = 0; i < 16; i++) { | |
698 | cmd = g_strdup_printf(":DIG%d:TURN?", i); | |
699 | res = get_cfg_string(sdi, cmd, &t_s); | |
700 | g_free(cmd); | |
701 | if (res != SR_OK) | |
702 | return SR_ERR; | |
703 | devc->digital_channels[i] = !strcmp(t_s, "ON") ? TRUE : FALSE; | |
704 | g_free(t_s); | |
705 | sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "on" : "off"); | |
706 | } | |
707 | } | |
254dd102 BV |
708 | |
709 | /* Timebase. */ | |
710 | if (get_cfg_float(sdi, ":TIM:SCAL?", &devc->timebase) != SR_OK) | |
711 | return SR_ERR; | |
bafd4890 | 712 | sr_dbg("Current timebase %g", devc->timebase); |
254dd102 BV |
713 | |
714 | /* Vertical gain. */ | |
715 | if (get_cfg_float(sdi, ":CHAN1:SCAL?", &devc->vdiv[0]) != SR_OK) | |
716 | return SR_ERR; | |
717 | if (get_cfg_float(sdi, ":CHAN2:SCAL?", &devc->vdiv[1]) != SR_OK) | |
718 | return SR_ERR; | |
bafd4890 ML |
719 | sr_dbg("Current vertical gain CH1 %g CH2 %g", devc->vdiv[0], devc->vdiv[1]); |
720 | ||
babab622 | 721 | if (devc->model->protocol == PROTOCOL_IEEE488_2) { |
bafd4890 | 722 | /* Vertical reference - not certain if this is the place to read it. */ |
17b5b202 | 723 | if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN1") != SR_OK) |
bafd4890 ML |
724 | return SR_ERR; |
725 | if (get_cfg_int(sdi, ":WAV:YREF?", &devc->vert_reference[0]) != SR_OK) | |
726 | return SR_ERR; | |
17b5b202 | 727 | if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN2") != SR_OK) |
bafd4890 ML |
728 | return SR_ERR; |
729 | if (get_cfg_int(sdi, ":WAV:YREF?", &devc->vert_reference[1]) != SR_OK) | |
730 | return SR_ERR; | |
731 | sr_dbg("Current vertical reference CH1 %d CH2 %d", | |
732 | devc->vert_reference[0], devc->vert_reference[1]); | |
733 | } | |
254dd102 BV |
734 | |
735 | /* Vertical offset. */ | |
736 | if (get_cfg_float(sdi, ":CHAN1:OFFS?", &devc->vert_offset[0]) != SR_OK) | |
737 | return SR_ERR; | |
738 | if (get_cfg_float(sdi, ":CHAN2:OFFS?", &devc->vert_offset[1]) != SR_OK) | |
739 | return SR_ERR; | |
bafd4890 | 740 | sr_dbg("Current vertical offset CH1 %g CH2 %g", devc->vert_offset[0], |
254dd102 BV |
741 | devc->vert_offset[1]); |
742 | ||
743 | /* Coupling. */ | |
744 | if (get_cfg_string(sdi, ":CHAN1:COUP?", &devc->coupling[0]) != SR_OK) | |
745 | return SR_ERR; | |
746 | if (get_cfg_string(sdi, ":CHAN2:COUP?", &devc->coupling[1]) != SR_OK) | |
747 | return SR_ERR; | |
748 | sr_dbg("Current coupling CH1 %s CH2 %s", devc->coupling[0], | |
749 | devc->coupling[1]); | |
750 | ||
751 | /* Trigger source. */ | |
752 | if (get_cfg_string(sdi, ":TRIG:EDGE:SOUR?", &devc->trigger_source) != SR_OK) | |
753 | return SR_ERR; | |
754 | sr_dbg("Current trigger source %s", devc->trigger_source); | |
755 | ||
756 | /* Horizontal trigger position. */ | |
757 | if (get_cfg_float(sdi, ":TIM:OFFS?", &devc->horiz_triggerpos) != SR_OK) | |
758 | return SR_ERR; | |
bafd4890 | 759 | sr_dbg("Current horizontal trigger position %g", devc->horiz_triggerpos); |
254dd102 BV |
760 | |
761 | /* Trigger slope. */ | |
762 | if (get_cfg_string(sdi, ":TRIG:EDGE:SLOP?", &devc->trigger_slope) != SR_OK) | |
763 | return SR_ERR; | |
764 | sr_dbg("Current trigger slope %s", devc->trigger_slope); | |
765 | ||
766 | return SR_OK; | |
767 | } |