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