]>
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 | */ | |
c2b394d5 | 185 | if (devc->timebase < 0.0499) { |
babab622 ML |
186 | if (devc->timebase > 0.99e-6) { |
187 | /* | |
188 | * Timebase * num hor. divs * 85(%) * 1e6(usecs) / 100 | |
189 | * -> 85 percent of sweep time | |
190 | */ | |
191 | s = (devc->timebase * devc->model->num_horizontal_divs | |
192 | * 85e6) / 100L; | |
193 | sr_spew("Sleeping for %ld usecs instead of trigger-wait", s); | |
194 | g_usleep(s); | |
195 | } | |
196 | rigol_ds_set_wait_event(devc, WAIT_NONE); | |
197 | return SR_OK; | |
198 | } else { | |
199 | return rigol_ds_event_wait(sdi, 'T', 'A'); | |
200 | } | |
201 | } | |
bafd4890 | 202 | |
babab622 ML |
203 | /* Wait for scope to got to "Stop" in single shot mode */ |
204 | static int rigol_ds_stop_wait(const struct sr_dev_inst *sdi) | |
205 | { | |
206 | return rigol_ds_event_wait(sdi, 'S', 'S'); | |
207 | } | |
208 | ||
209 | /* Check that a single shot acquisition actually succeeded on the DS2000 */ | |
210 | static int rigol_ds_check_stop(const struct sr_dev_inst *sdi) | |
211 | { | |
212 | struct dev_context *devc; | |
821fbcad | 213 | struct sr_probe *probe; |
babab622 ML |
214 | int tmp; |
215 | ||
216 | if (!(devc = sdi->priv)) | |
bafd4890 | 217 | return SR_ERR; |
babab622 | 218 | |
821fbcad ML |
219 | probe = devc->channel_entry->data; |
220 | ||
17b5b202 | 221 | if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d", |
821fbcad | 222 | probe->index + 1) != SR_OK) |
babab622 ML |
223 | return SR_ERR; |
224 | /* Check that the number of samples will be accepted */ | |
17b5b202 | 225 | if (sr_scpi_send(sdi->conn, ":WAV:POIN %d;*OPC", devc->analog_frame_size) != SR_OK) |
babab622 ML |
226 | return SR_ERR; |
227 | if (get_cfg_int(sdi, "*ESR?", &tmp) != SR_OK) | |
bafd4890 | 228 | return SR_ERR; |
babab622 ML |
229 | /* |
230 | * If we get an "Execution error" the scope went from "Single" to | |
231 | * "Stop" without actually triggering. There is no waveform | |
232 | * displayed and trying to download one will fail - the scope thinks | |
233 | * it has 1400 samples (like display memory) and the driver thinks | |
234 | * it has a different number of samples. | |
235 | * | |
236 | * In that case just try to capture something again. Might still | |
237 | * fail in interesting ways. | |
238 | * | |
239 | * Ain't firmware fun? | |
240 | */ | |
241 | if (tmp & 0x10) { | |
242 | sr_warn("Single shot acquisition failed, retrying..."); | |
243 | /* Sleep a bit, otherwise the single shot will often fail */ | |
244 | g_usleep(500000); | |
17b5b202 | 245 | sr_scpi_send(sdi->conn, ":SING"); |
babab622 | 246 | rigol_ds_set_wait_event(devc, WAIT_STOP); |
bafd4890 | 247 | return SR_ERR; |
babab622 | 248 | } |
bafd4890 | 249 | |
babab622 ML |
250 | return SR_OK; |
251 | } | |
bafd4890 | 252 | |
babab622 ML |
253 | /* Wait for enough data becoming available in scope output buffer */ |
254 | static int rigol_ds_block_wait(const struct sr_dev_inst *sdi) | |
255 | { | |
256 | char buf[30]; | |
257 | struct dev_context *devc; | |
258 | time_t start; | |
259 | int len; | |
260 | ||
261 | if (!(devc = sdi->priv)) | |
262 | return SR_ERR; | |
263 | ||
264 | start = time(NULL); | |
265 | ||
266 | do { | |
267 | if (time(NULL) - start >= 3) { | |
268 | sr_dbg("Timeout waiting for data block"); | |
269 | return SR_ERR_TIMEOUT; | |
270 | } | |
271 | ||
272 | /* | |
273 | * The scope copies data really slowly from sample | |
274 | * memory to its output buffer, so try not to bother | |
275 | * it too much with SCPI requests but don't wait too | |
276 | * long for short sample frame sizes. | |
277 | */ | |
278 | g_usleep(devc->analog_frame_size < 15000 ? 100000 : 1000000); | |
279 | ||
280 | /* "READ,nnnn" (still working) or "IDLE,nnnn" (finished) */ | |
281 | if (get_cfg(sdi, ":WAV:STAT?", buf, sizeof(buf)) != SR_OK) | |
282 | return SR_ERR; | |
283 | ||
284 | if (parse_int(buf + 5, &len) != SR_OK) | |
285 | return SR_ERR; | |
286 | } while (buf[0] == 'R' && len < 1000000); | |
287 | ||
288 | rigol_ds_set_wait_event(devc, WAIT_NONE); | |
289 | ||
290 | return SR_OK; | |
291 | } | |
292 | ||
293 | /* Start capturing a new frameset */ | |
294 | SR_PRIV int rigol_ds_capture_start(const struct sr_dev_inst *sdi) | |
295 | { | |
296 | struct dev_context *devc; | |
297 | ||
298 | if (!(devc = sdi->priv)) | |
299 | return SR_ERR; | |
300 | ||
301 | sr_dbg("Starting data capture for frameset %lu of %lu", | |
302 | devc->num_frames + 1, devc->limit_frames); | |
303 | ||
17b5b202 | 304 | if (sr_scpi_send(sdi->conn, ":WAV:FORM BYTE") != SR_OK) |
babab622 ML |
305 | return SR_ERR; |
306 | if (devc->data_source == DATA_SOURCE_LIVE) { | |
17b5b202 | 307 | if (sr_scpi_send(sdi->conn, ":WAV:MODE NORM") != SR_OK) |
babab622 ML |
308 | return SR_ERR; |
309 | rigol_ds_set_wait_event(devc, WAIT_TRIGGER); | |
bafd4890 | 310 | } else { |
17b5b202 | 311 | if (sr_scpi_send(sdi->conn, ":WAV:MODE RAW") != SR_OK) |
babab622 | 312 | return SR_ERR; |
17b5b202 | 313 | if (sr_scpi_send(sdi->conn, ":SING", devc->analog_frame_size) != SR_OK) |
babab622 ML |
314 | return SR_ERR; |
315 | rigol_ds_set_wait_event(devc, WAIT_STOP); | |
bafd4890 ML |
316 | } |
317 | ||
318 | return SR_OK; | |
319 | } | |
320 | ||
babab622 ML |
321 | /* Start reading data from the current channel */ |
322 | SR_PRIV int rigol_ds_channel_start(const struct sr_dev_inst *sdi) | |
323 | { | |
324 | struct dev_context *devc; | |
821fbcad | 325 | struct sr_probe *probe; |
babab622 ML |
326 | |
327 | if (!(devc = sdi->priv)) | |
328 | return SR_ERR; | |
329 | ||
821fbcad ML |
330 | probe = devc->channel_entry->data; |
331 | ||
332 | sr_dbg("Starting reading data from channel %d", probe->index + 1); | |
babab622 | 333 | |
470140fc | 334 | if (devc->model->series < RIGOL_DS1000Z) { |
821fbcad | 335 | if (probe->type == SR_PROBE_LOGIC) { |
677f85d0 ML |
336 | if (sr_scpi_send(sdi->conn, ":WAV:DATA? DIG") != SR_OK) |
337 | return SR_ERR; | |
338 | } else { | |
821fbcad ML |
339 | if (sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%d", |
340 | probe->index + 1) != SR_OK) | |
677f85d0 ML |
341 | return SR_ERR; |
342 | } | |
343 | } else { | |
344 | if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d", | |
821fbcad | 345 | probe->index + 1) != SR_OK) |
babab622 | 346 | return SR_ERR; |
677f85d0 ML |
347 | if (devc->data_source != DATA_SOURCE_LIVE) { |
348 | if (sr_scpi_send(sdi->conn, ":WAV:RES") != SR_OK) | |
349 | return SR_ERR; | |
350 | if (sr_scpi_send(sdi->conn, ":WAV:BEG") != SR_OK) | |
351 | return SR_ERR; | |
352 | rigol_ds_set_wait_event(devc, WAIT_BLOCK); | |
353 | } else | |
354 | rigol_ds_set_wait_event(devc, WAIT_NONE); | |
355 | } | |
babab622 | 356 | |
0d87bd93 | 357 | devc->num_frame_samples = 0; |
babab622 ML |
358 | devc->num_block_bytes = 0; |
359 | ||
360 | return SR_OK; | |
361 | } | |
362 | ||
363 | /* Read the header of a data block */ | |
ae1bc1cc | 364 | static int rigol_ds_read_header(struct sr_scpi_dev_inst *scpi) |
bafd4890 ML |
365 | { |
366 | char start[3], length[10]; | |
367 | int len, tmp; | |
368 | ||
369 | /* Read the hashsign and length digit. */ | |
05c644ea | 370 | tmp = sr_scpi_read_data(scpi, start, 2); |
bafd4890 | 371 | start[2] = '\0'; |
c2b394d5 | 372 | if (tmp != 2) { |
bafd4890 ML |
373 | sr_err("Failed to read first two bytes of data block header."); |
374 | return -1; | |
375 | } | |
c2b394d5 | 376 | if (start[0] != '#' || !isdigit(start[1]) || start[1] == '0') { |
bafd4890 ML |
377 | sr_err("Received invalid data block header start '%s'.", start); |
378 | return -1; | |
379 | } | |
380 | len = atoi(start + 1); | |
381 | ||
382 | /* Read the data length. */ | |
05c644ea | 383 | tmp = sr_scpi_read_data(scpi, length, len); |
bafd4890 | 384 | length[len] = '\0'; |
c2b394d5 | 385 | if (tmp != len) { |
bafd4890 ML |
386 | sr_err("Failed to read %d bytes of data block length.", len); |
387 | return -1; | |
388 | } | |
c2b394d5 | 389 | if (parse_int(length, &len) != SR_OK) { |
bafd4890 ML |
390 | sr_err("Received invalid data block length '%s'.", length); |
391 | return -1; | |
392 | } | |
393 | ||
394 | sr_dbg("Received data block header: %s%s -> block length %d", start, length, len); | |
395 | ||
396 | return len; | |
397 | } | |
398 | ||
3086efdd | 399 | SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) |
f4816ac6 | 400 | { |
e0b7d23c | 401 | struct sr_dev_inst *sdi; |
ae1bc1cc | 402 | struct sr_scpi_dev_inst *scpi; |
f4816ac6 | 403 | struct dev_context *devc; |
e0b7d23c ML |
404 | struct sr_datafeed_packet packet; |
405 | struct sr_datafeed_analog analog; | |
6bb192bc | 406 | struct sr_datafeed_logic logic; |
254dd102 | 407 | double vdiv, offset; |
f80a0bf2 | 408 | int len, i, vref; |
6bb192bc | 409 | struct sr_probe *probe; |
f4816ac6 | 410 | |
decfe89d | 411 | (void)fd; |
9bd4c956 | 412 | |
f4816ac6 ML |
413 | if (!(sdi = cb_data)) |
414 | return TRUE; | |
415 | ||
416 | if (!(devc = sdi->priv)) | |
417 | return TRUE; | |
418 | ||
ae1bc1cc | 419 | scpi = sdi->conn; |
9bd4c956 | 420 | |
d5876cfb | 421 | if (revents == G_IO_IN || revents == 0) { |
470140fc ML |
422 | if (devc->model->series >= RIGOL_DS1000Z) { |
423 | switch(devc->wait_event) { | |
babab622 ML |
424 | case WAIT_NONE: |
425 | break; | |
babab622 ML |
426 | case WAIT_TRIGGER: |
427 | if (rigol_ds_trigger_wait(sdi) != SR_OK) | |
428 | return TRUE; | |
429 | if (rigol_ds_channel_start(sdi) != SR_OK) | |
430 | return TRUE; | |
431 | break; | |
babab622 ML |
432 | case WAIT_BLOCK: |
433 | if (rigol_ds_block_wait(sdi) != SR_OK) | |
434 | return TRUE; | |
435 | break; | |
babab622 ML |
436 | case WAIT_STOP: |
437 | if (rigol_ds_stop_wait(sdi) != SR_OK) | |
438 | return TRUE; | |
439 | if (rigol_ds_check_stop(sdi) != SR_OK) | |
440 | return TRUE; | |
441 | if (rigol_ds_channel_start(sdi) != SR_OK) | |
442 | return TRUE; | |
bafd4890 | 443 | return TRUE; |
babab622 ML |
444 | default: |
445 | sr_err("BUG: Unknown event target encountered"); | |
446 | } | |
f80a0bf2 ML |
447 | } |
448 | ||
821fbcad | 449 | probe = devc->channel_entry->data; |
babab622 | 450 | |
a53278de | 451 | if (devc->num_block_bytes == 0 && |
470140fc | 452 | devc->model->series >= RIGOL_DS1000Z) { |
a53278de AJ |
453 | if (sr_scpi_send(sdi->conn, ":WAV:DATA?") != SR_OK) |
454 | return TRUE; | |
455 | } | |
456 | ||
f80a0bf2 | 457 | if (devc->num_block_bytes == 0) { |
05c644ea ML |
458 | if (sr_scpi_read_begin(scpi) != SR_OK) |
459 | return TRUE; | |
f80a0bf2 | 460 | if (devc->model->protocol == PROTOCOL_IEEE488_2) { |
babab622 | 461 | sr_dbg("New block header expected"); |
ae1bc1cc | 462 | len = rigol_ds_read_header(scpi); |
babab622 ML |
463 | if (len == -1) |
464 | return TRUE; | |
465 | /* At slow timebases in live capture the DS2072 | |
466 | * sometimes returns "short" data blocks, with | |
467 | * apparently no way to get the rest of the data. | |
468 | * Discard these, the complete data block will | |
469 | * appear eventually. | |
470 | */ | |
471 | if (devc->data_source == DATA_SOURCE_LIVE | |
f80a0bf2 | 472 | && (unsigned)len < devc->num_frame_samples) { |
babab622 | 473 | sr_dbg("Discarding short data block"); |
05c644ea | 474 | sr_scpi_read_data(scpi, (char *)devc->buffer, len + 1); |
babab622 ML |
475 | return TRUE; |
476 | } | |
477 | devc->num_block_bytes = len; | |
f80a0bf2 ML |
478 | } else { |
479 | devc->num_block_bytes = probe->type == SR_PROBE_ANALOG ? | |
d22250a9 | 480 | devc->analog_frame_size : devc->digital_frame_size; |
f80a0bf2 ML |
481 | } |
482 | devc->num_block_read = 0; | |
bafd4890 | 483 | } |
f80a0bf2 ML |
484 | |
485 | len = devc->num_block_bytes - devc->num_block_read; | |
05c644ea | 486 | len = sr_scpi_read_data(scpi, (char *)devc->buffer, |
f80a0bf2 ML |
487 | len < ACQ_BUFFER_SIZE ? len : ACQ_BUFFER_SIZE); |
488 | ||
29d957ce | 489 | sr_dbg("Received %d bytes.", len); |
e0b7d23c ML |
490 | if (len == -1) |
491 | return TRUE; | |
75d8a4e5 | 492 | |
48460c6f ML |
493 | devc->num_block_read += len; |
494 | ||
0d87bd93 | 495 | if (devc->num_frame_samples == 0) { |
75d8a4e5 BV |
496 | /* Start of a new frame. */ |
497 | packet.type = SR_DF_FRAME_BEGIN; | |
498 | sr_session_send(sdi, &packet); | |
499 | } | |
500 | ||
6bb192bc | 501 | if (probe->type == SR_PROBE_ANALOG) { |
bafd4890 ML |
502 | vref = devc->vert_reference[probe->index]; |
503 | vdiv = devc->vdiv[probe->index] / 25.6; | |
504 | offset = devc->vert_offset[probe->index]; | |
470140fc | 505 | if (devc->model->series >= RIGOL_DS1000Z) |
bafd4890 | 506 | for (i = 0; i < len; i++) |
babab622 | 507 | devc->data[i] = ((int)devc->buffer[i] - vref) * vdiv - offset; |
bafd4890 ML |
508 | else |
509 | for (i = 0; i < len; i++) | |
babab622 | 510 | devc->data[i] = (128 - devc->buffer[i]) * vdiv - offset; |
6bb192bc ML |
511 | analog.probes = g_slist_append(NULL, probe); |
512 | analog.num_samples = len; | |
babab622 | 513 | analog.data = devc->data; |
6bb192bc ML |
514 | analog.mq = SR_MQ_VOLTAGE; |
515 | analog.unit = SR_UNIT_VOLT; | |
516 | analog.mqflags = 0; | |
517 | packet.type = SR_DF_ANALOG; | |
518 | packet.payload = &analog; | |
519 | sr_session_send(cb_data, &packet); | |
520 | g_slist_free(analog.probes); | |
6bb192bc | 521 | } else { |
470140fc | 522 | logic.length = len; |
6bb192bc | 523 | logic.unitsize = 2; |
470140fc | 524 | logic.data = devc->buffer; |
6bb192bc ML |
525 | packet.type = SR_DF_LOGIC; |
526 | packet.payload = &logic; | |
527 | sr_session_send(cb_data, &packet); | |
48460c6f | 528 | } |
6bb192bc | 529 | |
48460c6f ML |
530 | if (devc->num_block_read == devc->num_block_bytes) { |
531 | sr_dbg("Block has been completed"); | |
470140fc ML |
532 | if (devc->model->series >= RIGOL_DS1000Z) { |
533 | /* Discard the terminating linefeed */ | |
05c644ea | 534 | sr_scpi_read_data(scpi, (char *)devc->buffer, 1); |
470140fc ML |
535 | } |
536 | if (devc->model->protocol == PROTOCOL_IEEE488_2) { | |
537 | /* Prepare for possible next block */ | |
48460c6f ML |
538 | devc->num_block_bytes = 0; |
539 | if (devc->data_source != DATA_SOURCE_LIVE) | |
540 | rigol_ds_set_wait_event(devc, WAIT_BLOCK); | |
541 | } | |
3ed7a40c ML |
542 | if (!sr_scpi_read_complete(scpi)) { |
543 | sr_err("Read should have been completed"); | |
544 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
545 | return TRUE; | |
546 | } | |
48460c6f ML |
547 | devc->num_block_read = 0; |
548 | } else { | |
549 | sr_dbg("%d of %d block bytes read", devc->num_block_read, devc->num_block_bytes); | |
ee7e9bee | 550 | } |
75d8a4e5 | 551 | |
48460c6f ML |
552 | devc->num_frame_samples += len; |
553 | ||
554 | if (devc->num_frame_samples < (probe->type == SR_PROBE_ANALOG ? | |
d22250a9 | 555 | devc->analog_frame_size : devc->digital_frame_size)) |
48460c6f ML |
556 | /* Don't have the whole frame yet. */ |
557 | return TRUE; | |
558 | ||
254dd102 | 559 | /* End of the frame. */ |
48460c6f | 560 | sr_dbg("Frame completed, %d samples", devc->num_frame_samples); |
254dd102 BV |
561 | packet.type = SR_DF_FRAME_END; |
562 | sr_session_send(sdi, &packet); | |
470140fc | 563 | if (devc->model->series >= RIGOL_DS1000Z) { |
babab622 ML |
564 | /* Signal end of data download to scope */ |
565 | if (devc->data_source != DATA_SOURCE_LIVE) | |
566 | /* | |
567 | * This causes a query error, without it switching | |
568 | * to the next channel causes an error. Fun with | |
569 | * firmware... | |
570 | */ | |
17b5b202 | 571 | sr_scpi_send(sdi->conn, ":WAV:END"); |
babab622 | 572 | } |
254dd102 | 573 | |
821fbcad ML |
574 | if (probe->type == SR_PROBE_ANALOG |
575 | && devc->channel_entry->next != NULL) { | |
576 | /* We got the frame for this analog channel, but | |
577 | * there's another analog channel. */ | |
578 | devc->channel_entry = devc->channel_entry->next; | |
677f85d0 | 579 | rigol_ds_channel_start(sdi); |
254dd102 | 580 | } else { |
821fbcad | 581 | /* Done with all analog channels in this frame. */ |
6bb192bc | 582 | if (devc->enabled_digital_probes |
821fbcad | 583 | && devc->channel_entry != devc->enabled_digital_probes) { |
6bb192bc | 584 | /* Now we need to get the digital data. */ |
821fbcad | 585 | devc->channel_entry = devc->enabled_digital_probes; |
677f85d0 | 586 | rigol_ds_channel_start(sdi); |
6bb192bc | 587 | } else if (++devc->num_frames == devc->limit_frames) { |
75d8a4e5 | 588 | sdi->driver->dev_acquisition_stop(sdi, cb_data); |
254dd102 | 589 | } else { |
6bb192bc | 590 | /* Get the next frame, starting with the first analog channel. */ |
677f85d0 | 591 | if (devc->enabled_analog_probes) |
821fbcad | 592 | devc->channel_entry = devc->enabled_analog_probes; |
677f85d0 | 593 | else |
821fbcad | 594 | devc->channel_entry = devc->enabled_digital_probes; |
677f85d0 | 595 | |
470140fc | 596 | if (devc->model->series < RIGOL_DS1000Z) |
677f85d0 ML |
597 | rigol_ds_channel_start(sdi); |
598 | else | |
599 | rigol_ds_capture_start(sdi); | |
254dd102 | 600 | } |
75d8a4e5 | 601 | } |
f4816ac6 ML |
602 | } |
603 | ||
604 | return TRUE; | |
605 | } | |
e0b7d23c | 606 | |
bafd4890 | 607 | static int get_cfg(const struct sr_dev_inst *sdi, char *cmd, char *reply, size_t maxlen) |
254dd102 | 608 | { |
254dd102 | 609 | int len; |
bafd4890 | 610 | struct dev_context *devc = sdi->priv; |
ae1bc1cc ML |
611 | struct sr_scpi_dev_inst *scpi = sdi->conn; |
612 | char *response; | |
254dd102 | 613 | |
05c644ea | 614 | if (sr_scpi_get_string(scpi, cmd, &response) != SR_OK) |
254dd102 | 615 | return SR_ERR; |
ae1bc1cc ML |
616 | |
617 | g_strlcpy(reply, response, maxlen); | |
618 | g_free(response); | |
619 | len = strlen(reply); | |
bafd4890 | 620 | |
470140fc | 621 | if (devc->model->series >= RIGOL_DS1000Z) { |
bafd4890 ML |
622 | /* get rid of trailing linefeed */ |
623 | if (len >= 1 && reply[len-1] == '\n') | |
624 | reply[len-1] = '\0'; | |
625 | } | |
626 | ||
254dd102 BV |
627 | sr_spew("Received '%s'.", reply); |
628 | ||
629 | return SR_OK; | |
630 | } | |
631 | ||
bafd4890 ML |
632 | static int get_cfg_int(const struct sr_dev_inst *sdi, char *cmd, int *i) |
633 | { | |
634 | char buf[32]; | |
635 | ||
636 | if (get_cfg(sdi, cmd, buf, sizeof(buf)) != SR_OK) | |
637 | return SR_ERR; | |
638 | ||
639 | if (parse_int(buf, i) != SR_OK) | |
640 | return SR_ERR; | |
641 | ||
642 | return SR_OK; | |
643 | } | |
644 | ||
254dd102 BV |
645 | static int get_cfg_float(const struct sr_dev_inst *sdi, char *cmd, float *f) |
646 | { | |
bafd4890 | 647 | char buf[32], *e; |
254dd102 | 648 | |
bafd4890 | 649 | if (get_cfg(sdi, cmd, buf, sizeof(buf)) != SR_OK) |
254dd102 BV |
650 | return SR_ERR; |
651 | *f = strtof(buf, &e); | |
169dbe85 | 652 | if (e == buf || (fpclassify(*f) & (FP_ZERO | FP_NORMAL)) == 0) { |
254dd102 BV |
653 | sr_dbg("failed to parse response to '%s': '%s'", cmd, buf); |
654 | return SR_ERR; | |
655 | } | |
656 | ||
657 | return SR_OK; | |
658 | } | |
659 | ||
660 | static int get_cfg_string(const struct sr_dev_inst *sdi, char *cmd, char **buf) | |
661 | { | |
254dd102 | 662 | if (!(*buf = g_try_malloc0(256))) |
169dbe85 | 663 | return SR_ERR_MALLOC; |
254dd102 | 664 | |
bafd4890 | 665 | if (get_cfg(sdi, cmd, *buf, 256) != SR_OK) |
254dd102 BV |
666 | return SR_ERR; |
667 | ||
668 | return SR_OK; | |
669 | } | |
670 | ||
3086efdd | 671 | SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi) |
254dd102 BV |
672 | { |
673 | struct dev_context *devc; | |
6bb192bc | 674 | char *t_s, *cmd; |
821fbcad ML |
675 | unsigned int i; |
676 | int res; | |
254dd102 BV |
677 | |
678 | devc = sdi->priv; | |
679 | ||
6bb192bc | 680 | /* Analog channel state. */ |
821fbcad ML |
681 | for (i = 0; i < devc->model->analog_channels; i++) { |
682 | cmd = g_strdup_printf(":CHAN%d:DISP?", i + 1); | |
683 | res = get_cfg_string(sdi, cmd, &t_s); | |
684 | g_free(cmd); | |
685 | if (res != SR_OK) | |
686 | return SR_ERR; | |
687 | devc->analog_channels[i] = !strcmp(t_s, "ON") || !strcmp(t_s, "1"); | |
688 | } | |
689 | sr_dbg("Current analog channel state:"); | |
690 | for (i = 0; i < devc->model->analog_channels; i++) | |
691 | sr_dbg("CH%d %s", i + 1, devc->analog_channels[i] ? "on" : "off"); | |
6bb192bc ML |
692 | |
693 | /* Digital channel state. */ | |
bafd4890 | 694 | if (devc->model->has_digital) { |
04e8e01e ML |
695 | if (get_cfg_string(sdi, ":LA:DISP?", &t_s) != SR_OK) |
696 | return SR_ERR; | |
697 | devc->la_enabled = !strcmp(t_s, "ON") ? TRUE : FALSE; | |
698 | sr_dbg("Logic analyzer %s, current digital channel state:", | |
699 | devc->la_enabled ? "enabled" : "disabled"); | |
6bb192bc | 700 | for (i = 0; i < 16; i++) { |
bfaf112b | 701 | cmd = g_strdup_printf(":DIG%d:TURN?", i); |
6bb192bc ML |
702 | res = get_cfg_string(sdi, cmd, &t_s); |
703 | g_free(cmd); | |
704 | if (res != SR_OK) | |
705 | return SR_ERR; | |
706 | devc->digital_channels[i] = !strcmp(t_s, "ON") ? TRUE : FALSE; | |
707 | g_free(t_s); | |
bfaf112b | 708 | sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "on" : "off"); |
6bb192bc ML |
709 | } |
710 | } | |
254dd102 BV |
711 | |
712 | /* Timebase. */ | |
713 | if (get_cfg_float(sdi, ":TIM:SCAL?", &devc->timebase) != SR_OK) | |
714 | return SR_ERR; | |
bafd4890 | 715 | sr_dbg("Current timebase %g", devc->timebase); |
254dd102 BV |
716 | |
717 | /* Vertical gain. */ | |
821fbcad ML |
718 | for (i = 0; i < devc->model->analog_channels; i++) { |
719 | cmd = g_strdup_printf(":CHAN%d:SCAL?", i + 1); | |
720 | res = get_cfg_float(sdi, cmd, &devc->vdiv[i]); | |
721 | g_free(cmd); | |
722 | if (res != SR_OK) | |
723 | return SR_ERR; | |
724 | } | |
725 | sr_dbg("Current vertical gain:"); | |
726 | for (i = 0; i < devc->model->analog_channels; i++) | |
727 | sr_dbg("CH%d %g", i + 1, devc->vdiv[i]); | |
bafd4890 | 728 | |
821fbcad | 729 | sr_dbg("Current vertical reference:"); |
470140fc | 730 | if (devc->model->series >= RIGOL_DS1000Z) { |
bafd4890 | 731 | /* Vertical reference - not certain if this is the place to read it. */ |
821fbcad ML |
732 | for (i = 0; i < devc->model->analog_channels; i++) { |
733 | if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d", i + 1) != SR_OK) | |
734 | return SR_ERR; | |
735 | if (get_cfg_int(sdi, ":WAV:YREF?", &devc->vert_reference[i]) != SR_OK) | |
736 | return SR_ERR; | |
737 | sr_dbg("CH%d %d", i + 1, devc->vert_reference[i]); | |
738 | } | |
bafd4890 | 739 | } |
254dd102 BV |
740 | |
741 | /* Vertical offset. */ | |
821fbcad ML |
742 | for (i = 0; i < devc->model->analog_channels; i++) { |
743 | cmd = g_strdup_printf(":CHAN%d:OFFS?", i + 1); | |
744 | res = get_cfg_float(sdi, cmd, &devc->vert_offset[i]); | |
745 | g_free(cmd); | |
746 | if (res != SR_OK) | |
747 | return SR_ERR; | |
748 | } | |
749 | sr_dbg("Current vertical offset:"); | |
750 | for (i = 0; i < devc->model->analog_channels; i++) | |
751 | sr_dbg("CH%d %g", i + 1, devc->vert_offset[i]); | |
254dd102 BV |
752 | |
753 | /* Coupling. */ | |
821fbcad ML |
754 | for (i = 0; i < devc->model->analog_channels; i++) { |
755 | cmd = g_strdup_printf(":CHAN%d:COUP?", i + 1); | |
756 | res = get_cfg_string(sdi, cmd, &devc->coupling[i]); | |
757 | g_free(cmd); | |
758 | if (res != SR_OK) | |
759 | return SR_ERR; | |
760 | } | |
761 | sr_dbg("Current coupling:"); | |
762 | for (i = 0; i < devc->model->analog_channels; i++) | |
763 | sr_dbg("CH%d %s", i + 1, devc->coupling[i]); | |
254dd102 BV |
764 | |
765 | /* Trigger source. */ | |
766 | if (get_cfg_string(sdi, ":TRIG:EDGE:SOUR?", &devc->trigger_source) != SR_OK) | |
767 | return SR_ERR; | |
768 | sr_dbg("Current trigger source %s", devc->trigger_source); | |
769 | ||
770 | /* Horizontal trigger position. */ | |
771 | if (get_cfg_float(sdi, ":TIM:OFFS?", &devc->horiz_triggerpos) != SR_OK) | |
772 | return SR_ERR; | |
bafd4890 | 773 | sr_dbg("Current horizontal trigger position %g", devc->horiz_triggerpos); |
254dd102 BV |
774 | |
775 | /* Trigger slope. */ | |
776 | if (get_cfg_string(sdi, ":TRIG:EDGE:SLOP?", &devc->trigger_slope) != SR_OK) | |
777 | return SR_ERR; | |
778 | sr_dbg("Current trigger slope %s", devc->trigger_slope); | |
779 | ||
780 | return SR_OK; | |
781 | } |