]>
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 | ||
6ec6c43b | 22 | #include <config.h> |
f4816ac6 | 23 | #include <stdlib.h> |
e0b7d23c ML |
24 | #include <stdarg.h> |
25 | #include <unistd.h> | |
26 | #include <errno.h> | |
a3df166f | 27 | #include <string.h> |
254dd102 | 28 | #include <math.h> |
bafd4890 ML |
29 | #include <ctype.h> |
30 | #include <time.h> | |
f4816ac6 | 31 | #include <glib.h> |
c1aae900 | 32 | #include <libsigrok/libsigrok.h> |
f4816ac6 | 33 | #include "libsigrok-internal.h" |
5a1afc09 | 34 | #include "scpi.h" |
f4816ac6 ML |
35 | #include "protocol.h" |
36 | ||
bafd4890 ML |
37 | /* |
38 | * This is a unified protocol driver for the DS1000 and DS2000 series. | |
39 | * | |
40 | * DS1000 support tested with a Rigol DS1102D. | |
41 | * | |
42 | * DS2000 support tested with a Rigol DS2072 using firmware version 01.01.00.02. | |
43 | * | |
44 | * The Rigol DS2000 series scopes try to adhere to the IEEE 488.2 (I think) | |
45 | * standard. If you want to read it - it costs real money... | |
46 | * | |
47 | * Every response from the scope has a linefeed appended because the | |
48 | * standard says so. In principle this could be ignored because sending the | |
49 | * next command clears the output queue of the scope. This driver tries to | |
50 | * avoid doing that because it may cause an error being generated inside the | |
51 | * scope and who knows what bugs the firmware has WRT this. | |
52 | * | |
53 | * Waveform data is transferred in a format called "arbitrary block program | |
54 | * data" specified in IEEE 488.2. See Agilents programming manuals for their | |
55 | * 2000/3000 series scopes for a nice description. | |
56 | * | |
57 | * Each data block from the scope has a header, e.g. "#900000001400". | |
58 | * The '#' marks the start of a block. | |
59 | * Next is one ASCII decimal digit between 1 and 9, this gives the number of | |
60 | * ASCII decimal digits following. | |
61 | * Last are the ASCII decimal digits giving the number of bytes (not | |
62 | * samples!) in the block. | |
63 | * | |
64 | * After this header as many data bytes as indicated follow. | |
65 | * | |
66 | * Each data block has a trailing linefeed too. | |
67 | */ | |
68 | ||
bafd4890 ML |
69 | static int parse_int(const char *str, int *ret) |
70 | { | |
71 | char *e; | |
72 | long tmp; | |
73 | ||
74 | errno = 0; | |
75 | tmp = strtol(str, &e, 10); | |
76 | if (e == str || *e != '\0') { | |
77 | sr_dbg("Failed to parse integer: '%s'", str); | |
78 | return SR_ERR; | |
79 | } | |
80 | if (errno) { | |
81 | sr_dbg("Failed to parse integer: '%s', numerical overflow", str); | |
82 | return SR_ERR; | |
83 | } | |
84 | if (tmp > INT_MAX || tmp < INT_MIN) { | |
85 | sr_dbg("Failed to parse integer: '%s', value to large/small", str); | |
86 | return SR_ERR; | |
87 | } | |
88 | ||
89 | *ret = (int)tmp; | |
90 | return SR_OK; | |
91 | } | |
92 | ||
babab622 ML |
93 | /* Set the next event to wait for in rigol_ds_receive */ |
94 | static void rigol_ds_set_wait_event(struct dev_context *devc, enum wait_events event) | |
95 | { | |
96 | if (event == WAIT_STOP) | |
97 | devc->wait_status = 2; | |
98 | else | |
99 | devc->wait_status = 1; | |
100 | devc->wait_event = event; | |
101 | } | |
102 | ||
bafd4890 | 103 | /* |
babab622 ML |
104 | * Waiting for a event will return a timeout after 2 to 3 seconds in order |
105 | * to not block the application. | |
bafd4890 | 106 | */ |
babab622 | 107 | static int rigol_ds_event_wait(const struct sr_dev_inst *sdi, char status1, char status2) |
bafd4890 | 108 | { |
334fbc2a | 109 | char *buf; |
bafd4890 ML |
110 | struct dev_context *devc; |
111 | time_t start; | |
112 | ||
113 | if (!(devc = sdi->priv)) | |
114 | return SR_ERR; | |
115 | ||
116 | start = time(NULL); | |
117 | ||
118 | /* | |
119 | * Trigger status may return: | |
babab622 ML |
120 | * "TD" or "T'D" - triggered |
121 | * "AUTO" - autotriggered | |
122 | * "RUN" - running | |
123 | * "WAIT" - waiting for trigger | |
124 | * "STOP" - stopped | |
bafd4890 ML |
125 | */ |
126 | ||
babab622 | 127 | if (devc->wait_status == 1) { |
bafd4890 ML |
128 | do { |
129 | if (time(NULL) - start >= 3) { | |
130 | sr_dbg("Timeout waiting for trigger"); | |
131 | return SR_ERR_TIMEOUT; | |
132 | } | |
133 | ||
334fbc2a | 134 | if (sr_scpi_get_string(sdi->conn, ":TRIG:STAT?", &buf) != SR_OK) |
bafd4890 | 135 | return SR_ERR; |
babab622 | 136 | } while (buf[0] == status1 || buf[0] == status2); |
bafd4890 | 137 | |
babab622 | 138 | devc->wait_status = 2; |
bafd4890 | 139 | } |
babab622 | 140 | if (devc->wait_status == 2) { |
bafd4890 ML |
141 | do { |
142 | if (time(NULL) - start >= 3) { | |
143 | sr_dbg("Timeout waiting for trigger"); | |
144 | return SR_ERR_TIMEOUT; | |
145 | } | |
146 | ||
334fbc2a | 147 | if (sr_scpi_get_string(sdi->conn, ":TRIG:STAT?", &buf) != SR_OK) |
bafd4890 | 148 | return SR_ERR; |
babab622 | 149 | } while (buf[0] != status1 && buf[0] != status2); |
bafd4890 | 150 | |
babab622 | 151 | rigol_ds_set_wait_event(devc, WAIT_NONE); |
bafd4890 ML |
152 | } |
153 | ||
154 | return SR_OK; | |
155 | } | |
156 | ||
157 | /* | |
babab622 ML |
158 | * For live capture we need to wait for a new trigger event to ensure that |
159 | * sample data is not returned twice. | |
bafd4890 ML |
160 | * |
161 | * Unfortunately this will never really work because for sufficiently fast | |
babab622 | 162 | * timebases and trigger rates it just can't catch the status changes. |
bafd4890 ML |
163 | * |
164 | * What would be needed is a trigger event register with autoreset like the | |
165 | * Agilents have. The Rigols don't seem to have anything like this. | |
166 | * | |
167 | * The workaround is to only wait for the trigger when the timebase is slow | |
168 | * enough. Of course this means that for faster timebases sample data can be | |
babab622 ML |
169 | * returned multiple times, this effect is mitigated somewhat by sleeping |
170 | * for about one sweep time in that case. | |
bafd4890 | 171 | */ |
babab622 | 172 | static int rigol_ds_trigger_wait(const struct sr_dev_inst *sdi) |
bafd4890 ML |
173 | { |
174 | struct dev_context *devc; | |
babab622 | 175 | long s; |
bafd4890 ML |
176 | |
177 | if (!(devc = sdi->priv)) | |
178 | return SR_ERR; | |
179 | ||
176d785d | 180 | /* |
babab622 ML |
181 | * If timebase < 50 msecs/DIV just sleep about one sweep time except |
182 | * for really fast sweeps. | |
183 | */ | |
c2b394d5 | 184 | if (devc->timebase < 0.0499) { |
babab622 ML |
185 | if (devc->timebase > 0.99e-6) { |
186 | /* | |
187 | * Timebase * num hor. divs * 85(%) * 1e6(usecs) / 100 | |
188 | * -> 85 percent of sweep time | |
189 | */ | |
569d4dbd | 190 | s = (devc->timebase * devc->model->series->num_horizontal_divs |
babab622 ML |
191 | * 85e6) / 100L; |
192 | sr_spew("Sleeping for %ld usecs instead of trigger-wait", s); | |
193 | g_usleep(s); | |
194 | } | |
195 | rigol_ds_set_wait_event(devc, WAIT_NONE); | |
196 | return SR_OK; | |
197 | } else { | |
198 | return rigol_ds_event_wait(sdi, 'T', 'A'); | |
199 | } | |
200 | } | |
bafd4890 | 201 | |
babab622 ML |
202 | /* Wait for scope to got to "Stop" in single shot mode */ |
203 | static int rigol_ds_stop_wait(const struct sr_dev_inst *sdi) | |
204 | { | |
205 | return rigol_ds_event_wait(sdi, 'S', 'S'); | |
206 | } | |
207 | ||
208 | /* Check that a single shot acquisition actually succeeded on the DS2000 */ | |
209 | static int rigol_ds_check_stop(const struct sr_dev_inst *sdi) | |
210 | { | |
211 | struct dev_context *devc; | |
ba7dd8bb | 212 | struct sr_channel *ch; |
babab622 ML |
213 | int tmp; |
214 | ||
215 | if (!(devc = sdi->priv)) | |
bafd4890 | 216 | return SR_ERR; |
babab622 | 217 | |
ba7dd8bb | 218 | ch = devc->channel_entry->data; |
821fbcad | 219 | |
702f42e8 | 220 | if (devc->model->series->protocol != PROTOCOL_V3) |
e086b750 ML |
221 | return SR_OK; |
222 | ||
01dd7a4c ML |
223 | if (ch->type == SR_CHANNEL_LOGIC) { |
224 | if (rigol_ds_config_set(sdi->conn, ":WAV:SOUR LA") != SR_OK) | |
225 | return SR_ERR; | |
226 | } else { | |
227 | if (rigol_ds_config_set(sdi, ":WAV:SOUR CHAN%d", | |
228 | ch->index + 1) != SR_OK) | |
229 | return SR_ERR; | |
230 | } | |
babab622 | 231 | /* Check that the number of samples will be accepted */ |
01dd7a4c ML |
232 | if (rigol_ds_config_set(sdi, ":WAV:POIN %d", |
233 | ch->type == SR_CHANNEL_LOGIC ? | |
234 | devc->digital_frame_size : | |
235 | devc->analog_frame_size) != SR_OK) | |
babab622 | 236 | return SR_ERR; |
334fbc2a | 237 | if (sr_scpi_get_int(sdi->conn, "*ESR?", &tmp) != SR_OK) |
bafd4890 | 238 | return SR_ERR; |
babab622 ML |
239 | /* |
240 | * If we get an "Execution error" the scope went from "Single" to | |
241 | * "Stop" without actually triggering. There is no waveform | |
242 | * displayed and trying to download one will fail - the scope thinks | |
243 | * it has 1400 samples (like display memory) and the driver thinks | |
244 | * it has a different number of samples. | |
245 | * | |
246 | * In that case just try to capture something again. Might still | |
247 | * fail in interesting ways. | |
248 | * | |
249 | * Ain't firmware fun? | |
250 | */ | |
251 | if (tmp & 0x10) { | |
252 | sr_warn("Single shot acquisition failed, retrying..."); | |
253 | /* Sleep a bit, otherwise the single shot will often fail */ | |
1a46cc62 | 254 | g_usleep(500 * 1000); |
38354d9d | 255 | rigol_ds_config_set(sdi, ":SING"); |
babab622 | 256 | rigol_ds_set_wait_event(devc, WAIT_STOP); |
bafd4890 | 257 | return SR_ERR; |
babab622 | 258 | } |
bafd4890 | 259 | |
babab622 ML |
260 | return SR_OK; |
261 | } | |
bafd4890 | 262 | |
babab622 ML |
263 | /* Wait for enough data becoming available in scope output buffer */ |
264 | static int rigol_ds_block_wait(const struct sr_dev_inst *sdi) | |
265 | { | |
334fbc2a | 266 | char *buf; |
babab622 ML |
267 | struct dev_context *devc; |
268 | time_t start; | |
269 | int len; | |
270 | ||
271 | if (!(devc = sdi->priv)) | |
272 | return SR_ERR; | |
273 | ||
702f42e8 | 274 | if (devc->model->series->protocol == PROTOCOL_V3) { |
babab622 | 275 | |
4472867a ML |
276 | start = time(NULL); |
277 | ||
278 | do { | |
279 | if (time(NULL) - start >= 3) { | |
280 | sr_dbg("Timeout waiting for data block"); | |
281 | return SR_ERR_TIMEOUT; | |
282 | } | |
babab622 | 283 | |
4472867a ML |
284 | /* |
285 | * The scope copies data really slowly from sample | |
286 | * memory to its output buffer, so try not to bother | |
287 | * it too much with SCPI requests but don't wait too | |
288 | * long for short sample frame sizes. | |
289 | */ | |
1a46cc62 | 290 | g_usleep(devc->analog_frame_size < (15 * 1000) ? (100 * 1000) : (1000 * 1000)); |
4472867a ML |
291 | |
292 | /* "READ,nnnn" (still working) or "IDLE,nnnn" (finished) */ | |
293 | if (sr_scpi_get_string(sdi->conn, ":WAV:STAT?", &buf) != SR_OK) | |
294 | return SR_ERR; | |
295 | ||
296 | if (parse_int(buf + 5, &len) != SR_OK) | |
297 | return SR_ERR; | |
1a46cc62 | 298 | } while (buf[0] == 'R' && len < (1000 * 1000)); |
4472867a | 299 | } |
babab622 ML |
300 | |
301 | rigol_ds_set_wait_event(devc, WAIT_NONE); | |
302 | ||
303 | return SR_OK; | |
304 | } | |
305 | ||
38354d9d ML |
306 | /* Send a configuration setting. */ |
307 | SR_PRIV int rigol_ds_config_set(const struct sr_dev_inst *sdi, const char *format, ...) | |
308 | { | |
309 | struct dev_context *devc = sdi->priv; | |
310 | va_list args; | |
311 | int ret; | |
312 | ||
313 | va_start(args, format); | |
314 | ret = sr_scpi_send_variadic(sdi->conn, format, args); | |
315 | va_end(args); | |
316 | ||
317 | if (ret != SR_OK) | |
318 | return SR_ERR; | |
319 | ||
569d4dbd | 320 | if (devc->model->series->protocol == PROTOCOL_V2) { |
38354d9d ML |
321 | /* The DS1000 series needs this stupid delay, *OPC? doesn't work. */ |
322 | sr_spew("delay %dms", 100); | |
1a46cc62 | 323 | g_usleep(100 * 1000); |
38354d9d ML |
324 | return SR_OK; |
325 | } else { | |
326 | return sr_scpi_get_opc(sdi->conn); | |
327 | } | |
328 | } | |
329 | ||
babab622 ML |
330 | /* Start capturing a new frameset */ |
331 | SR_PRIV int rigol_ds_capture_start(const struct sr_dev_inst *sdi) | |
332 | { | |
333 | struct dev_context *devc; | |
e086b750 | 334 | gchar *trig_mode; |
702f42e8 | 335 | unsigned int num_channels, i, j; |
babab622 ML |
336 | |
337 | if (!(devc = sdi->priv)) | |
338 | return SR_ERR; | |
339 | ||
0c536bcd | 340 | sr_dbg("Starting data capture for frameset %" PRIu64 " of %" PRIu64, |
babab622 ML |
341 | devc->num_frames + 1, devc->limit_frames); |
342 | ||
569d4dbd ML |
343 | switch (devc->model->series->protocol) { |
344 | case PROTOCOL_V1: | |
345 | rigol_ds_set_wait_event(devc, WAIT_TRIGGER); | |
346 | break; | |
347 | case PROTOCOL_V2: | |
348 | if (devc->data_source == DATA_SOURCE_LIVE) { | |
349 | if (rigol_ds_config_set(sdi, ":WAV:POIN:MODE NORMAL") != SR_OK) | |
e086b750 | 350 | return SR_ERR; |
569d4dbd | 351 | rigol_ds_set_wait_event(devc, WAIT_TRIGGER); |
e086b750 | 352 | } else { |
e086b750 ML |
353 | if (rigol_ds_config_set(sdi, ":STOP") != SR_OK) |
354 | return SR_ERR; | |
355 | if (rigol_ds_config_set(sdi, ":WAV:POIN:MODE RAW") != SR_OK) | |
356 | return SR_ERR; | |
357 | if (sr_scpi_get_string(sdi->conn, ":TRIG:MODE?", &trig_mode) != SR_OK) | |
358 | return SR_ERR; | |
359 | if (rigol_ds_config_set(sdi, ":TRIG:%s:SWE SING", trig_mode) != SR_OK) | |
360 | return SR_ERR; | |
361 | if (rigol_ds_config_set(sdi, ":RUN") != SR_OK) | |
362 | return SR_ERR; | |
569d4dbd ML |
363 | rigol_ds_set_wait_event(devc, WAIT_STOP); |
364 | } | |
365 | break; | |
366 | case PROTOCOL_V3: | |
702f42e8 | 367 | case PROTOCOL_V4: |
569d4dbd ML |
368 | if (rigol_ds_config_set(sdi, ":WAV:FORM BYTE") != SR_OK) |
369 | return SR_ERR; | |
370 | if (devc->data_source == DATA_SOURCE_LIVE) { | |
371 | if (rigol_ds_config_set(sdi, ":WAV:MODE NORM") != SR_OK) | |
372 | return SR_ERR; | |
702f42e8 ML |
373 | devc->analog_frame_size = devc->model->series->live_samples; |
374 | devc->digital_frame_size = devc->model->series->live_samples; | |
569d4dbd | 375 | rigol_ds_set_wait_event(devc, WAIT_TRIGGER); |
e086b750 | 376 | } else { |
702f42e8 ML |
377 | if (devc->model->series->protocol == PROTOCOL_V3) { |
378 | if (rigol_ds_config_set(sdi, ":WAV:MODE RAW") != SR_OK) | |
379 | return SR_ERR; | |
380 | } else if (devc->model->series->protocol == PROTOCOL_V4) { | |
381 | num_channels = 0; | |
382 | ||
383 | /* Channels 3 and 4 are multiplexed with D0-7 and D8-15 */ | |
384 | for (i = 0; i < devc->model->analog_channels; i++) { | |
385 | if (devc->analog_channels[i]) { | |
386 | num_channels++; | |
387 | } else if (i >= 2 && devc->model->has_digital) { | |
388 | for (j = 0; j < 8; j++) { | |
389 | if (devc->digital_channels[8 * (i - 2) + j]) { | |
390 | num_channels++; | |
391 | break; | |
392 | } | |
393 | } | |
394 | } | |
395 | } | |
396 | ||
397 | devc->analog_frame_size = devc->digital_frame_size = | |
398 | num_channels == 1 ? | |
399 | devc->model->series->buffer_samples : | |
400 | num_channels == 2 ? | |
401 | devc->model->series->buffer_samples / 2 : | |
402 | devc->model->series->buffer_samples / 4; | |
403 | } | |
404 | ||
e086b750 ML |
405 | if (rigol_ds_config_set(sdi, ":SING") != SR_OK) |
406 | return SR_ERR; | |
569d4dbd | 407 | rigol_ds_set_wait_event(devc, WAIT_STOP); |
e086b750 | 408 | } |
569d4dbd | 409 | break; |
bafd4890 ML |
410 | } |
411 | ||
412 | return SR_OK; | |
413 | } | |
414 | ||
babab622 ML |
415 | /* Start reading data from the current channel */ |
416 | SR_PRIV int rigol_ds_channel_start(const struct sr_dev_inst *sdi) | |
417 | { | |
418 | struct dev_context *devc; | |
ba7dd8bb | 419 | struct sr_channel *ch; |
babab622 ML |
420 | |
421 | if (!(devc = sdi->priv)) | |
422 | return SR_ERR; | |
423 | ||
ba7dd8bb | 424 | ch = devc->channel_entry->data; |
821fbcad | 425 | |
ba7dd8bb | 426 | sr_dbg("Starting reading data from channel %d", ch->index + 1); |
babab622 | 427 | |
2ea67fc9 | 428 | switch (devc->model->series->protocol) { |
702f42e8 ML |
429 | case PROTOCOL_V1: |
430 | case PROTOCOL_V2: | |
3f239f08 | 431 | if (ch->type == SR_CHANNEL_LOGIC) { |
677f85d0 ML |
432 | if (sr_scpi_send(sdi->conn, ":WAV:DATA? DIG") != SR_OK) |
433 | return SR_ERR; | |
434 | } else { | |
821fbcad | 435 | if (sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%d", |
ba7dd8bb | 436 | ch->index + 1) != SR_OK) |
677f85d0 ML |
437 | return SR_ERR; |
438 | } | |
e086b750 | 439 | rigol_ds_set_wait_event(devc, WAIT_NONE); |
702f42e8 ML |
440 | break; |
441 | case PROTOCOL_V3: | |
01dd7a4c ML |
442 | if (ch->type == SR_CHANNEL_LOGIC) { |
443 | if (rigol_ds_config_set(sdi->conn, ":WAV:SOUR LA") != SR_OK) | |
444 | return SR_ERR; | |
445 | } else { | |
446 | if (rigol_ds_config_set(sdi, ":WAV:SOUR CHAN%d", | |
447 | ch->index + 1) != SR_OK) | |
448 | return SR_ERR; | |
449 | } | |
677f85d0 | 450 | if (devc->data_source != DATA_SOURCE_LIVE) { |
38354d9d | 451 | if (rigol_ds_config_set(sdi, ":WAV:RES") != SR_OK) |
677f85d0 | 452 | return SR_ERR; |
38354d9d | 453 | if (rigol_ds_config_set(sdi, ":WAV:BEG") != SR_OK) |
677f85d0 | 454 | return SR_ERR; |
aff00e40 | 455 | } |
702f42e8 ML |
456 | break; |
457 | case PROTOCOL_V4: | |
458 | if (ch->type == SR_CHANNEL_ANALOG) { | |
459 | if (rigol_ds_config_set(sdi, ":WAV:SOUR CHAN%d", | |
460 | ch->index + 1) != SR_OK) | |
461 | return SR_ERR; | |
462 | } else { | |
463 | if (rigol_ds_config_set(sdi, ":WAV:SOUR D%d", | |
464 | ch->index) != SR_OK) | |
465 | return SR_ERR; | |
466 | } | |
467 | ||
468 | if (rigol_ds_config_set(sdi, | |
469 | devc->data_source == DATA_SOURCE_LIVE ? | |
470 | ":WAV:MODE NORM" :":WAV:MODE RAW") != SR_OK) | |
471 | return SR_ERR; | |
472 | break; | |
473 | } | |
474 | ||
475 | if (devc->model->series->protocol >= PROTOCOL_V3 && | |
476 | ch->type == SR_CHANNEL_ANALOG) { | |
477 | /* Vertical reference. */ | |
478 | if (sr_scpi_get_int(sdi->conn, ":WAV:YREF?", | |
479 | &devc->vert_reference[ch->index]) != SR_OK) | |
480 | return SR_ERR; | |
677f85d0 | 481 | } |
babab622 | 482 | |
aff00e40 ML |
483 | rigol_ds_set_wait_event(devc, WAIT_BLOCK); |
484 | ||
f76c24f6 | 485 | devc->num_channel_bytes = 0; |
aff00e40 | 486 | devc->num_header_bytes = 0; |
babab622 ML |
487 | devc->num_block_bytes = 0; |
488 | ||
489 | return SR_OK; | |
490 | } | |
491 | ||
492 | /* Read the header of a data block */ | |
aff00e40 | 493 | static int rigol_ds_read_header(struct sr_dev_inst *sdi) |
bafd4890 | 494 | { |
aff00e40 ML |
495 | struct sr_scpi_dev_inst *scpi = sdi->conn; |
496 | struct dev_context *devc = sdi->priv; | |
497 | char *buf = (char *) devc->buffer; | |
fe0d9caa ML |
498 | size_t header_length; |
499 | int ret; | |
aff00e40 ML |
500 | |
501 | /* Try to read the hashsign and length digit. */ | |
502 | if (devc->num_header_bytes < 2) { | |
fe0d9caa | 503 | ret = sr_scpi_read_data(scpi, buf + devc->num_header_bytes, |
aff00e40 | 504 | 2 - devc->num_header_bytes); |
fe0d9caa | 505 | if (ret < 0) { |
aff00e40 ML |
506 | sr_err("Read error while reading data header."); |
507 | return SR_ERR; | |
508 | } | |
fe0d9caa | 509 | devc->num_header_bytes += ret; |
bafd4890 | 510 | } |
aff00e40 ML |
511 | |
512 | if (devc->num_header_bytes < 2) | |
513 | return 0; | |
514 | ||
515 | if (buf[0] != '#' || !isdigit(buf[1]) || buf[1] == '0') { | |
516 | sr_err("Received invalid data block header '%c%c'.", buf[0], buf[1]); | |
517 | return SR_ERR; | |
bafd4890 | 518 | } |
bafd4890 | 519 | |
fe0d9caa | 520 | header_length = 2 + buf[1] - '0'; |
aff00e40 ML |
521 | |
522 | /* Try to read the length. */ | |
fe0d9caa ML |
523 | if (devc->num_header_bytes < header_length) { |
524 | ret = sr_scpi_read_data(scpi, buf + devc->num_header_bytes, | |
525 | header_length - devc->num_header_bytes); | |
526 | if (ret < 0) { | |
aff00e40 ML |
527 | sr_err("Read error while reading data header."); |
528 | return SR_ERR; | |
529 | } | |
fe0d9caa | 530 | devc->num_header_bytes += ret; |
bafd4890 | 531 | } |
aff00e40 | 532 | |
fe0d9caa | 533 | if (devc->num_header_bytes < header_length) |
aff00e40 ML |
534 | return 0; |
535 | ||
536 | /* Read the data length. */ | |
fe0d9caa | 537 | buf[header_length] = '\0'; |
aff00e40 | 538 | |
fe0d9caa | 539 | if (parse_int(buf + 2, &ret) != SR_OK) { |
aff00e40 | 540 | sr_err("Received invalid data block length '%s'.", buf + 2); |
bafd4890 ML |
541 | return -1; |
542 | } | |
543 | ||
fe0d9caa | 544 | sr_dbg("Received data block header: '%s' -> block length %d", buf, ret); |
bafd4890 | 545 | |
fe0d9caa | 546 | return ret; |
bafd4890 ML |
547 | } |
548 | ||
3086efdd | 549 | SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) |
f4816ac6 | 550 | { |
e0b7d23c | 551 | struct sr_dev_inst *sdi; |
ae1bc1cc | 552 | struct sr_scpi_dev_inst *scpi; |
f4816ac6 | 553 | struct dev_context *devc; |
e0b7d23c | 554 | struct sr_datafeed_packet packet; |
246399f7 UH |
555 | struct sr_datafeed_analog analog; |
556 | struct sr_analog_encoding encoding; | |
557 | struct sr_analog_meaning meaning; | |
558 | struct sr_analog_spec spec; | |
6bb192bc | 559 | struct sr_datafeed_logic logic; |
254dd102 | 560 | double vdiv, offset; |
f80a0bf2 | 561 | int len, i, vref; |
ba7dd8bb | 562 | struct sr_channel *ch; |
bac11aeb | 563 | gsize expected_data_bytes; |
f4816ac6 | 564 | |
decfe89d | 565 | (void)fd; |
9bd4c956 | 566 | |
f4816ac6 ML |
567 | if (!(sdi = cb_data)) |
568 | return TRUE; | |
569 | ||
570 | if (!(devc = sdi->priv)) | |
571 | return TRUE; | |
572 | ||
ae1bc1cc | 573 | scpi = sdi->conn; |
9bd4c956 | 574 | |
dc89faea UH |
575 | if (!(revents == G_IO_IN || revents == 0)) |
576 | return TRUE; | |
577 | ||
578 | switch (devc->wait_event) { | |
579 | case WAIT_NONE: | |
580 | break; | |
581 | case WAIT_TRIGGER: | |
582 | if (rigol_ds_trigger_wait(sdi) != SR_OK) | |
3918fbb0 | 583 | return TRUE; |
dc89faea | 584 | if (rigol_ds_channel_start(sdi) != SR_OK) |
e086b750 | 585 | return TRUE; |
dc89faea UH |
586 | return TRUE; |
587 | case WAIT_BLOCK: | |
588 | if (rigol_ds_block_wait(sdi) != SR_OK) | |
589 | return TRUE; | |
590 | break; | |
591 | case WAIT_STOP: | |
592 | if (rigol_ds_stop_wait(sdi) != SR_OK) | |
593 | return TRUE; | |
594 | if (rigol_ds_check_stop(sdi) != SR_OK) | |
595 | return TRUE; | |
596 | if (rigol_ds_channel_start(sdi) != SR_OK) | |
597 | return TRUE; | |
598 | return TRUE; | |
599 | default: | |
600 | sr_err("BUG: Unknown event target encountered"); | |
601 | break; | |
602 | } | |
f76c24f6 | 603 | |
dc89faea | 604 | ch = devc->channel_entry->data; |
702f42e8 | 605 | |
dc89faea UH |
606 | expected_data_bytes = ch->type == SR_CHANNEL_ANALOG ? |
607 | devc->analog_frame_size : devc->digital_frame_size; | |
bac11aeb | 608 | |
dc89faea UH |
609 | if (devc->num_block_bytes == 0) { |
610 | if (devc->model->series->protocol >= PROTOCOL_V4) { | |
ef7fb1ab | 611 | if (rigol_ds_config_set(sdi, ":WAV:START %d", |
dc89faea UH |
612 | devc->num_channel_bytes + 1) != SR_OK) |
613 | return TRUE; | |
ef7fb1ab | 614 | if (rigol_ds_config_set(sdi, ":WAV:STOP %d", |
dc89faea UH |
615 | MIN(devc->num_channel_bytes + ACQ_BLOCK_SIZE, |
616 | devc->analog_frame_size)) != SR_OK) | |
05c644ea | 617 | return TRUE; |
bafd4890 | 618 | } |
f80a0bf2 | 619 | |
dc89faea UH |
620 | if (devc->model->series->protocol >= PROTOCOL_V3) |
621 | if (sr_scpi_send(sdi->conn, ":WAV:DATA?") != SR_OK) | |
622 | return TRUE; | |
f80a0bf2 | 623 | |
dc89faea | 624 | if (sr_scpi_read_begin(scpi) != SR_OK) |
7d63347e | 625 | return TRUE; |
6bb192bc | 626 | |
dc89faea UH |
627 | if (devc->format == FORMAT_IEEE488_2) { |
628 | sr_dbg("New block header expected"); | |
629 | len = rigol_ds_read_header(sdi); | |
630 | if (len == 0) | |
631 | /* Still reading the header. */ | |
632 | return TRUE; | |
633 | if (len == -1) { | |
634 | sr_err("Read error, aborting capture."); | |
7d63347e | 635 | packet.type = SR_DF_FRAME_END; |
695dc859 UH |
636 | sr_session_send(sdi, &packet); |
637 | sdi->driver->dev_acquisition_stop(sdi); | |
3ed7a40c ML |
638 | return TRUE; |
639 | } | |
dc89faea UH |
640 | /* At slow timebases in live capture the DS2072 |
641 | * sometimes returns "short" data blocks, with | |
642 | * apparently no way to get the rest of the data. | |
643 | * Discard these, the complete data block will | |
644 | * appear eventually. | |
645 | */ | |
646 | if (devc->data_source == DATA_SOURCE_LIVE | |
647 | && (unsigned)len < expected_data_bytes) { | |
648 | sr_dbg("Discarding short data block"); | |
649 | sr_scpi_read_data(scpi, (char *)devc->buffer, len + 1); | |
650 | return TRUE; | |
651 | } | |
652 | devc->num_block_bytes = len; | |
48460c6f | 653 | } else { |
dc89faea | 654 | devc->num_block_bytes = expected_data_bytes; |
ee7e9bee | 655 | } |
dc89faea UH |
656 | devc->num_block_read = 0; |
657 | } | |
75d8a4e5 | 658 | |
dc89faea UH |
659 | len = devc->num_block_bytes - devc->num_block_read; |
660 | if (len > ACQ_BUFFER_SIZE) | |
661 | len = ACQ_BUFFER_SIZE; | |
662 | sr_dbg("Requesting read of %d bytes", len); | |
48460c6f | 663 | |
dc89faea | 664 | len = sr_scpi_read_data(scpi, (char *)devc->buffer, len); |
48460c6f | 665 | |
dc89faea UH |
666 | if (len == -1) { |
667 | sr_err("Read error, aborting capture."); | |
668 | packet.type = SR_DF_FRAME_END; | |
695dc859 UH |
669 | sr_session_send(sdi, &packet); |
670 | sdi->driver->dev_acquisition_stop(sdi); | |
dc89faea UH |
671 | return TRUE; |
672 | } | |
673 | ||
674 | sr_dbg("Received %d bytes.", len); | |
675 | ||
676 | devc->num_block_read += len; | |
677 | ||
678 | if (ch->type == SR_CHANNEL_ANALOG) { | |
679 | vref = devc->vert_reference[ch->index]; | |
680 | vdiv = devc->vdiv[ch->index] / 25.6; | |
681 | offset = devc->vert_offset[ch->index]; | |
682 | if (devc->model->series->protocol >= PROTOCOL_V3) | |
683 | for (i = 0; i < len; i++) | |
684 | devc->data[i] = ((int)devc->buffer[i] - vref) * vdiv - offset; | |
685 | else | |
686 | for (i = 0; i < len; i++) | |
687 | devc->data[i] = (128 - devc->buffer[i]) * vdiv - offset; | |
b8f07f42 AJ |
688 | float vdivlog = log10f(vdiv); |
689 | int digits = -(int)vdivlog + (vdivlog < 0.0); | |
690 | sr_analog_init(&analog, &encoding, &meaning, &spec, digits); | |
246399f7 | 691 | analog.meaning->channels = g_slist_append(NULL, ch); |
dc89faea UH |
692 | analog.num_samples = len; |
693 | analog.data = devc->data; | |
246399f7 UH |
694 | analog.meaning->mq = SR_MQ_VOLTAGE; |
695 | analog.meaning->unit = SR_UNIT_VOLT; | |
696 | analog.meaning->mqflags = 0; | |
697 | packet.type = SR_DF_ANALOG; | |
dc89faea | 698 | packet.payload = &analog; |
695dc859 | 699 | sr_session_send(sdi, &packet); |
246399f7 | 700 | g_slist_free(analog.meaning->channels); |
dc89faea UH |
701 | } else { |
702 | logic.length = len; | |
703 | // TODO: For the MSO1000Z series, we need a way to express that | |
704 | // this data is in fact just for a single channel, with the valid | |
705 | // data for that channel in the LSB of each byte. | |
706 | logic.unitsize = devc->model->series->protocol == PROTOCOL_V4 ? 1 : 2; | |
707 | logic.data = devc->buffer; | |
708 | packet.type = SR_DF_LOGIC; | |
709 | packet.payload = &logic; | |
695dc859 | 710 | sr_session_send(sdi, &packet); |
dc89faea UH |
711 | } |
712 | ||
713 | if (devc->num_block_read == devc->num_block_bytes) { | |
714 | sr_dbg("Block has been completed"); | |
715 | if (devc->model->series->protocol >= PROTOCOL_V3) { | |
716 | /* Discard the terminating linefeed */ | |
717 | sr_scpi_read_data(scpi, (char *)devc->buffer, 1); | |
718 | } | |
719 | if (devc->format == FORMAT_IEEE488_2) { | |
720 | /* Prepare for possible next block */ | |
721 | devc->num_header_bytes = 0; | |
722 | devc->num_block_bytes = 0; | |
babab622 | 723 | if (devc->data_source != DATA_SOURCE_LIVE) |
dc89faea | 724 | rigol_ds_set_wait_event(devc, WAIT_BLOCK); |
babab622 | 725 | } |
dc89faea UH |
726 | if (!sr_scpi_read_complete(scpi)) { |
727 | sr_err("Read should have been completed"); | |
702f42e8 | 728 | packet.type = SR_DF_FRAME_END; |
695dc859 UH |
729 | sr_session_send(sdi, &packet); |
730 | sdi->driver->dev_acquisition_stop(sdi); | |
dc89faea UH |
731 | return TRUE; |
732 | } | |
733 | devc->num_block_read = 0; | |
734 | } else { | |
6433156c DE |
735 | sr_dbg("%" PRIu64 " of %" PRIu64 " block bytes read", |
736 | devc->num_block_read, devc->num_block_bytes); | |
dc89faea | 737 | } |
f76c24f6 | 738 | |
dc89faea | 739 | devc->num_channel_bytes += len; |
f76c24f6 | 740 | |
dc89faea UH |
741 | if (devc->num_channel_bytes < expected_data_bytes) |
742 | /* Don't have the full data for this channel yet, re-run. */ | |
743 | return TRUE; | |
f76c24f6 | 744 | |
dc89faea UH |
745 | /* End of data for this channel. */ |
746 | if (devc->model->series->protocol == PROTOCOL_V3) { | |
747 | /* Signal end of data download to scope */ | |
748 | if (devc->data_source != DATA_SOURCE_LIVE) | |
749 | /* | |
750 | * This causes a query error, without it switching | |
751 | * to the next channel causes an error. Fun with | |
752 | * firmware... | |
753 | */ | |
754 | rigol_ds_config_set(sdi, ":WAV:END"); | |
755 | } | |
756 | ||
757 | if (devc->channel_entry->next) { | |
758 | /* We got the frame for this channel, now get the next channel. */ | |
759 | devc->channel_entry = devc->channel_entry->next; | |
760 | rigol_ds_channel_start(sdi); | |
761 | } else { | |
762 | /* Done with this frame. */ | |
763 | packet.type = SR_DF_FRAME_END; | |
695dc859 | 764 | sr_session_send(sdi, &packet); |
dc89faea UH |
765 | |
766 | if (++devc->num_frames == devc->limit_frames) { | |
767 | /* Last frame, stop capture. */ | |
695dc859 | 768 | sdi->driver->dev_acquisition_stop(sdi); |
dc89faea UH |
769 | } else { |
770 | /* Get the next frame, starting with the first channel. */ | |
771 | devc->channel_entry = devc->enabled_channels; | |
772 | ||
773 | rigol_ds_capture_start(sdi); | |
774 | ||
775 | /* Start of next frame. */ | |
776 | packet.type = SR_DF_FRAME_BEGIN; | |
695dc859 | 777 | sr_session_send(sdi, &packet); |
75d8a4e5 | 778 | } |
f4816ac6 ML |
779 | } |
780 | ||
781 | return TRUE; | |
782 | } | |
e0b7d23c | 783 | |
3086efdd | 784 | SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi) |
254dd102 BV |
785 | { |
786 | struct dev_context *devc; | |
e264ebde | 787 | struct sr_channel *ch; |
98bfc474 | 788 | char *cmd; |
821fbcad ML |
789 | unsigned int i; |
790 | int res; | |
254dd102 BV |
791 | |
792 | devc = sdi->priv; | |
793 | ||
6bb192bc | 794 | /* Analog channel state. */ |
821fbcad ML |
795 | for (i = 0; i < devc->model->analog_channels; i++) { |
796 | cmd = g_strdup_printf(":CHAN%d:DISP?", i + 1); | |
98bfc474 | 797 | res = sr_scpi_get_bool(sdi->conn, cmd, &devc->analog_channels[i]); |
821fbcad ML |
798 | g_free(cmd); |
799 | if (res != SR_OK) | |
800 | return SR_ERR; | |
e264ebde AJ |
801 | ch = g_slist_nth_data(sdi->channels, i); |
802 | ch->enabled = devc->analog_channels[i]; | |
821fbcad ML |
803 | } |
804 | sr_dbg("Current analog channel state:"); | |
805 | for (i = 0; i < devc->model->analog_channels; i++) | |
806 | sr_dbg("CH%d %s", i + 1, devc->analog_channels[i] ? "on" : "off"); | |
6bb192bc ML |
807 | |
808 | /* Digital channel state. */ | |
bafd4890 | 809 | if (devc->model->has_digital) { |
702f42e8 | 810 | if (sr_scpi_get_bool(sdi->conn, |
01dd7a4c | 811 | devc->model->series->protocol >= PROTOCOL_V3 ? |
702f42e8 | 812 | ":LA:STAT?" : ":LA:DISP?", |
98bfc474 | 813 | &devc->la_enabled) != SR_OK) |
04e8e01e | 814 | return SR_ERR; |
04e8e01e ML |
815 | sr_dbg("Logic analyzer %s, current digital channel state:", |
816 | devc->la_enabled ? "enabled" : "disabled"); | |
effb9dd1 | 817 | for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) { |
702f42e8 | 818 | cmd = g_strdup_printf( |
01dd7a4c | 819 | devc->model->series->protocol >= PROTOCOL_V3 ? |
702f42e8 | 820 | ":LA:DIG%d:DISP?" : ":DIG%d:TURN?", i); |
98bfc474 | 821 | res = sr_scpi_get_bool(sdi->conn, cmd, &devc->digital_channels[i]); |
6bb192bc ML |
822 | g_free(cmd); |
823 | if (res != SR_OK) | |
824 | return SR_ERR; | |
e264ebde AJ |
825 | ch = g_slist_nth_data(sdi->channels, i + devc->model->analog_channels); |
826 | ch->enabled = devc->digital_channels[i]; | |
bfaf112b | 827 | sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "on" : "off"); |
6bb192bc ML |
828 | } |
829 | } | |
254dd102 BV |
830 | |
831 | /* Timebase. */ | |
334fbc2a | 832 | if (sr_scpi_get_float(sdi->conn, ":TIM:SCAL?", &devc->timebase) != SR_OK) |
254dd102 | 833 | return SR_ERR; |
bafd4890 | 834 | sr_dbg("Current timebase %g", devc->timebase); |
254dd102 | 835 | |
934cf6cf AJ |
836 | /* Probe attenuation. */ |
837 | for (i = 0; i < devc->model->analog_channels; i++) { | |
838 | cmd = g_strdup_printf(":CHAN%d:PROB?", i + 1); | |
839 | res = sr_scpi_get_float(sdi->conn, cmd, &devc->attenuation[i]); | |
840 | g_free(cmd); | |
841 | if (res != SR_OK) | |
842 | return SR_ERR; | |
843 | } | |
844 | sr_dbg("Current probe attenuation:"); | |
845 | for (i = 0; i < devc->model->analog_channels; i++) | |
846 | sr_dbg("CH%d %g", i + 1, devc->attenuation[i]); | |
847 | ||
8719638f AJ |
848 | /* Vertical gain and offset. */ |
849 | if (rigol_ds_get_dev_cfg_vertical(sdi) != SR_OK) | |
850 | return SR_ERR; | |
254dd102 BV |
851 | |
852 | /* Coupling. */ | |
821fbcad ML |
853 | for (i = 0; i < devc->model->analog_channels; i++) { |
854 | cmd = g_strdup_printf(":CHAN%d:COUP?", i + 1); | |
334fbc2a | 855 | res = sr_scpi_get_string(sdi->conn, cmd, &devc->coupling[i]); |
821fbcad ML |
856 | g_free(cmd); |
857 | if (res != SR_OK) | |
858 | return SR_ERR; | |
859 | } | |
860 | sr_dbg("Current coupling:"); | |
861 | for (i = 0; i < devc->model->analog_channels; i++) | |
862 | sr_dbg("CH%d %s", i + 1, devc->coupling[i]); | |
254dd102 BV |
863 | |
864 | /* Trigger source. */ | |
334fbc2a | 865 | if (sr_scpi_get_string(sdi->conn, ":TRIG:EDGE:SOUR?", &devc->trigger_source) != SR_OK) |
254dd102 BV |
866 | return SR_ERR; |
867 | sr_dbg("Current trigger source %s", devc->trigger_source); | |
868 | ||
869 | /* Horizontal trigger position. */ | |
334fbc2a | 870 | if (sr_scpi_get_float(sdi->conn, ":TIM:OFFS?", &devc->horiz_triggerpos) != SR_OK) |
254dd102 | 871 | return SR_ERR; |
bafd4890 | 872 | sr_dbg("Current horizontal trigger position %g", devc->horiz_triggerpos); |
254dd102 BV |
873 | |
874 | /* Trigger slope. */ | |
334fbc2a | 875 | if (sr_scpi_get_string(sdi->conn, ":TRIG:EDGE:SLOP?", &devc->trigger_slope) != SR_OK) |
254dd102 BV |
876 | return SR_ERR; |
877 | sr_dbg("Current trigger slope %s", devc->trigger_slope); | |
878 | ||
9ea62f2e AJ |
879 | /* Trigger level. */ |
880 | if (sr_scpi_get_float(sdi->conn, ":TRIG:EDGE:LEV?", &devc->trigger_level) != SR_OK) | |
881 | return SR_ERR; | |
882 | sr_dbg("Current trigger level %g", devc->trigger_level); | |
883 | ||
254dd102 BV |
884 | return SR_OK; |
885 | } | |
8719638f AJ |
886 | |
887 | SR_PRIV int rigol_ds_get_dev_cfg_vertical(const struct sr_dev_inst *sdi) | |
888 | { | |
889 | struct dev_context *devc; | |
890 | char *cmd; | |
891 | unsigned int i; | |
892 | int res; | |
893 | ||
894 | devc = sdi->priv; | |
895 | ||
896 | /* Vertical gain. */ | |
897 | for (i = 0; i < devc->model->analog_channels; i++) { | |
898 | cmd = g_strdup_printf(":CHAN%d:SCAL?", i + 1); | |
899 | res = sr_scpi_get_float(sdi->conn, cmd, &devc->vdiv[i]); | |
900 | g_free(cmd); | |
901 | if (res != SR_OK) | |
902 | return SR_ERR; | |
903 | } | |
904 | sr_dbg("Current vertical gain:"); | |
905 | for (i = 0; i < devc->model->analog_channels; i++) | |
906 | sr_dbg("CH%d %g", i + 1, devc->vdiv[i]); | |
907 | ||
908 | /* Vertical offset. */ | |
909 | for (i = 0; i < devc->model->analog_channels; i++) { | |
910 | cmd = g_strdup_printf(":CHAN%d:OFFS?", i + 1); | |
911 | res = sr_scpi_get_float(sdi->conn, cmd, &devc->vert_offset[i]); | |
912 | g_free(cmd); | |
913 | if (res != SR_OK) | |
914 | return SR_ERR; | |
915 | } | |
916 | sr_dbg("Current vertical offset:"); | |
917 | for (i = 0; i < devc->model->analog_channels; i++) | |
918 | sr_dbg("CH%d %g", i + 1, devc->vert_offset[i]); | |
919 | ||
920 | return SR_OK; | |
921 | } |