]>
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 | 223 | if (ch->type == SR_CHANNEL_LOGIC) { |
bbcffe51 | 224 | if (rigol_ds_config_set(sdi, ":WAV:SOUR LA") != SR_OK) |
01dd7a4c ML |
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; |
47b821dc | 336 | int buffer_samples; |
babab622 ML |
337 | |
338 | if (!(devc = sdi->priv)) | |
339 | return SR_ERR; | |
340 | ||
16e96ca3 VO |
341 | const gboolean first_frame = (devc->num_frames == 0); |
342 | ||
8cbe5339 VO |
343 | uint64_t limit_frames = devc->limit_frames; |
344 | if (devc->num_frames_segmented != 0 && devc->num_frames_segmented < limit_frames) | |
345 | limit_frames = devc->num_frames_segmented; | |
346 | if (limit_frames == 0) | |
9ad1d49c UH |
347 | sr_dbg("Starting data capture for frameset %" PRIu64, |
348 | devc->num_frames + 1); | |
349 | else | |
350 | sr_dbg("Starting data capture for frameset %" PRIu64 " of %" | |
8cbe5339 | 351 | PRIu64, devc->num_frames + 1, limit_frames); |
babab622 | 352 | |
569d4dbd ML |
353 | switch (devc->model->series->protocol) { |
354 | case PROTOCOL_V1: | |
355 | rigol_ds_set_wait_event(devc, WAIT_TRIGGER); | |
356 | break; | |
357 | case PROTOCOL_V2: | |
358 | if (devc->data_source == DATA_SOURCE_LIVE) { | |
359 | if (rigol_ds_config_set(sdi, ":WAV:POIN:MODE NORMAL") != SR_OK) | |
e086b750 | 360 | return SR_ERR; |
569d4dbd | 361 | rigol_ds_set_wait_event(devc, WAIT_TRIGGER); |
e086b750 | 362 | } else { |
e086b750 ML |
363 | if (rigol_ds_config_set(sdi, ":STOP") != SR_OK) |
364 | return SR_ERR; | |
365 | if (rigol_ds_config_set(sdi, ":WAV:POIN:MODE RAW") != SR_OK) | |
366 | return SR_ERR; | |
367 | if (sr_scpi_get_string(sdi->conn, ":TRIG:MODE?", &trig_mode) != SR_OK) | |
368 | return SR_ERR; | |
369 | if (rigol_ds_config_set(sdi, ":TRIG:%s:SWE SING", trig_mode) != SR_OK) | |
370 | return SR_ERR; | |
371 | if (rigol_ds_config_set(sdi, ":RUN") != SR_OK) | |
372 | return SR_ERR; | |
569d4dbd ML |
373 | rigol_ds_set_wait_event(devc, WAIT_STOP); |
374 | } | |
375 | break; | |
376 | case PROTOCOL_V3: | |
702f42e8 | 377 | case PROTOCOL_V4: |
f6129c8f | 378 | case PROTOCOL_V5: |
16e96ca3 | 379 | if (first_frame && rigol_ds_config_set(sdi, ":WAV:FORM BYTE") != SR_OK) |
569d4dbd ML |
380 | return SR_ERR; |
381 | if (devc->data_source == DATA_SOURCE_LIVE) { | |
16e96ca3 | 382 | if (first_frame && rigol_ds_config_set(sdi, ":WAV:MODE NORM") != SR_OK) |
569d4dbd | 383 | return SR_ERR; |
702f42e8 ML |
384 | devc->analog_frame_size = devc->model->series->live_samples; |
385 | devc->digital_frame_size = devc->model->series->live_samples; | |
569d4dbd | 386 | rigol_ds_set_wait_event(devc, WAIT_TRIGGER); |
e086b750 | 387 | } else { |
702f42e8 | 388 | if (devc->model->series->protocol == PROTOCOL_V3) { |
16e96ca3 | 389 | if (first_frame && rigol_ds_config_set(sdi, ":WAV:MODE RAW") != SR_OK) |
702f42e8 | 390 | return SR_ERR; |
f6129c8f | 391 | } else if (devc->model->series->protocol >= PROTOCOL_V4) { |
702f42e8 ML |
392 | num_channels = 0; |
393 | ||
394 | /* Channels 3 and 4 are multiplexed with D0-7 and D8-15 */ | |
395 | for (i = 0; i < devc->model->analog_channels; i++) { | |
396 | if (devc->analog_channels[i]) { | |
397 | num_channels++; | |
398 | } else if (i >= 2 && devc->model->has_digital) { | |
399 | for (j = 0; j < 8; j++) { | |
400 | if (devc->digital_channels[8 * (i - 2) + j]) { | |
401 | num_channels++; | |
402 | break; | |
403 | } | |
404 | } | |
405 | } | |
406 | } | |
407 | ||
47b821dc | 408 | buffer_samples = devc->model->series->buffer_samples; |
16e96ca3 | 409 | if (first_frame && buffer_samples == 0) |
47b821dc ML |
410 | { |
411 | /* The DS4000 series does not have a fixed memory depth, it | |
412 | * can be chosen from the menu and also varies with number | |
413 | * of active channels. Retrieve the actual number with the | |
414 | * ACQ:MDEP command. */ | |
415 | sr_scpi_get_int(sdi->conn, "ACQ:MDEP?", &buffer_samples); | |
416 | devc->analog_frame_size = devc->digital_frame_size = | |
417 | buffer_samples; | |
418 | } | |
16e96ca3 | 419 | else if (first_frame) |
47b821dc ML |
420 | { |
421 | /* The DS1000Z series has a fixed memory depth which we | |
422 | * need to divide correctly according to the number of | |
423 | * active channels. */ | |
424 | devc->analog_frame_size = devc->digital_frame_size = | |
425 | num_channels == 1 ? | |
426 | buffer_samples : | |
427 | num_channels == 2 ? | |
428 | buffer_samples / 2 : | |
429 | buffer_samples / 4; | |
430 | } | |
702f42e8 ML |
431 | } |
432 | ||
16e96ca3 | 433 | if (devc->data_source == DATA_SOURCE_LIVE && rigol_ds_config_set(sdi, ":SINGL") != SR_OK) |
e086b750 | 434 | return SR_ERR; |
569d4dbd | 435 | rigol_ds_set_wait_event(devc, WAIT_STOP); |
19f31c8a | 436 | if (devc->data_source == DATA_SOURCE_SEGMENTED && |
06ed999a | 437 | devc->model->series->protocol <= PROTOCOL_V4) |
8cbe5339 VO |
438 | if (rigol_ds_config_set(sdi, "FUNC:WREP:FCUR %d", devc->num_frames + 1) != SR_OK) |
439 | return SR_ERR; | |
e086b750 | 440 | } |
569d4dbd | 441 | break; |
bafd4890 ML |
442 | } |
443 | ||
444 | return SR_OK; | |
445 | } | |
446 | ||
babab622 ML |
447 | /* Start reading data from the current channel */ |
448 | SR_PRIV int rigol_ds_channel_start(const struct sr_dev_inst *sdi) | |
449 | { | |
450 | struct dev_context *devc; | |
ba7dd8bb | 451 | struct sr_channel *ch; |
babab622 ML |
452 | |
453 | if (!(devc = sdi->priv)) | |
454 | return SR_ERR; | |
455 | ||
ba7dd8bb | 456 | ch = devc->channel_entry->data; |
821fbcad | 457 | |
ba7dd8bb | 458 | sr_dbg("Starting reading data from channel %d", ch->index + 1); |
babab622 | 459 | |
16e96ca3 VO |
460 | const gboolean first_frame = (devc->num_frames == 0); |
461 | ||
2ea67fc9 | 462 | switch (devc->model->series->protocol) { |
702f42e8 ML |
463 | case PROTOCOL_V1: |
464 | case PROTOCOL_V2: | |
3f239f08 | 465 | if (ch->type == SR_CHANNEL_LOGIC) { |
677f85d0 ML |
466 | if (sr_scpi_send(sdi->conn, ":WAV:DATA? DIG") != SR_OK) |
467 | return SR_ERR; | |
468 | } else { | |
821fbcad | 469 | if (sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%d", |
ba7dd8bb | 470 | ch->index + 1) != SR_OK) |
677f85d0 ML |
471 | return SR_ERR; |
472 | } | |
e086b750 | 473 | rigol_ds_set_wait_event(devc, WAIT_NONE); |
702f42e8 ML |
474 | break; |
475 | case PROTOCOL_V3: | |
01dd7a4c | 476 | if (ch->type == SR_CHANNEL_LOGIC) { |
bbcffe51 | 477 | if (rigol_ds_config_set(sdi, ":WAV:SOUR LA") != SR_OK) |
01dd7a4c ML |
478 | return SR_ERR; |
479 | } else { | |
480 | if (rigol_ds_config_set(sdi, ":WAV:SOUR CHAN%d", | |
481 | ch->index + 1) != SR_OK) | |
482 | return SR_ERR; | |
483 | } | |
677f85d0 | 484 | if (devc->data_source != DATA_SOURCE_LIVE) { |
38354d9d | 485 | if (rigol_ds_config_set(sdi, ":WAV:RES") != SR_OK) |
677f85d0 | 486 | return SR_ERR; |
38354d9d | 487 | if (rigol_ds_config_set(sdi, ":WAV:BEG") != SR_OK) |
677f85d0 | 488 | return SR_ERR; |
aff00e40 | 489 | } |
702f42e8 ML |
490 | break; |
491 | case PROTOCOL_V4: | |
f6129c8f | 492 | case PROTOCOL_V5: |
702f42e8 ML |
493 | if (ch->type == SR_CHANNEL_ANALOG) { |
494 | if (rigol_ds_config_set(sdi, ":WAV:SOUR CHAN%d", | |
495 | ch->index + 1) != SR_OK) | |
496 | return SR_ERR; | |
497 | } else { | |
498 | if (rigol_ds_config_set(sdi, ":WAV:SOUR D%d", | |
499 | ch->index) != SR_OK) | |
500 | return SR_ERR; | |
501 | } | |
502 | ||
16e96ca3 | 503 | if (first_frame && rigol_ds_config_set(sdi, |
702f42e8 ML |
504 | devc->data_source == DATA_SOURCE_LIVE ? |
505 | ":WAV:MODE NORM" :":WAV:MODE RAW") != SR_OK) | |
506 | return SR_ERR; | |
704910e3 VO |
507 | |
508 | if (devc->data_source != DATA_SOURCE_LIVE) { | |
509 | if (rigol_ds_config_set(sdi, ":WAV:RES") != SR_OK) | |
510 | return SR_ERR; | |
511 | } | |
702f42e8 ML |
512 | break; |
513 | } | |
514 | ||
515 | if (devc->model->series->protocol >= PROTOCOL_V3 && | |
516 | ch->type == SR_CHANNEL_ANALOG) { | |
6b04525b | 517 | /* Vertical increment. */ |
16e96ca3 | 518 | if (first_frame && sr_scpi_get_float(sdi->conn, ":WAV:YINC?", |
6b04525b | 519 | &devc->vert_inc[ch->index]) != SR_OK) |
55bece00 UH |
520 | return SR_ERR; |
521 | /* Vertical origin. */ | |
16e96ca3 | 522 | if (first_frame && sr_scpi_get_float(sdi->conn, ":WAV:YOR?", |
55bece00 UH |
523 | &devc->vert_origin[ch->index]) != SR_OK) |
524 | return SR_ERR; | |
702f42e8 | 525 | /* Vertical reference. */ |
16e96ca3 | 526 | if (first_frame && sr_scpi_get_int(sdi->conn, ":WAV:YREF?", |
702f42e8 ML |
527 | &devc->vert_reference[ch->index]) != SR_OK) |
528 | return SR_ERR; | |
6b04525b VO |
529 | } else if (ch->type == SR_CHANNEL_ANALOG) { |
530 | devc->vert_inc[ch->index] = devc->vdiv[ch->index] / 25.6; | |
677f85d0 | 531 | } |
babab622 | 532 | |
aff00e40 ML |
533 | rigol_ds_set_wait_event(devc, WAIT_BLOCK); |
534 | ||
f76c24f6 | 535 | devc->num_channel_bytes = 0; |
aff00e40 | 536 | devc->num_header_bytes = 0; |
babab622 ML |
537 | devc->num_block_bytes = 0; |
538 | ||
539 | return SR_OK; | |
540 | } | |
541 | ||
542 | /* Read the header of a data block */ | |
aff00e40 | 543 | static int rigol_ds_read_header(struct sr_dev_inst *sdi) |
bafd4890 | 544 | { |
aff00e40 ML |
545 | struct sr_scpi_dev_inst *scpi = sdi->conn; |
546 | struct dev_context *devc = sdi->priv; | |
547 | char *buf = (char *) devc->buffer; | |
fe0d9caa ML |
548 | size_t header_length; |
549 | int ret; | |
aff00e40 ML |
550 | |
551 | /* Try to read the hashsign and length digit. */ | |
552 | if (devc->num_header_bytes < 2) { | |
fe0d9caa | 553 | ret = sr_scpi_read_data(scpi, buf + devc->num_header_bytes, |
aff00e40 | 554 | 2 - devc->num_header_bytes); |
fe0d9caa | 555 | if (ret < 0) { |
aff00e40 ML |
556 | sr_err("Read error while reading data header."); |
557 | return SR_ERR; | |
558 | } | |
fe0d9caa | 559 | devc->num_header_bytes += ret; |
bafd4890 | 560 | } |
aff00e40 ML |
561 | |
562 | if (devc->num_header_bytes < 2) | |
563 | return 0; | |
564 | ||
565 | if (buf[0] != '#' || !isdigit(buf[1]) || buf[1] == '0') { | |
566 | sr_err("Received invalid data block header '%c%c'.", buf[0], buf[1]); | |
567 | return SR_ERR; | |
bafd4890 | 568 | } |
bafd4890 | 569 | |
fe0d9caa | 570 | header_length = 2 + buf[1] - '0'; |
aff00e40 ML |
571 | |
572 | /* Try to read the length. */ | |
fe0d9caa ML |
573 | if (devc->num_header_bytes < header_length) { |
574 | ret = sr_scpi_read_data(scpi, buf + devc->num_header_bytes, | |
575 | header_length - devc->num_header_bytes); | |
576 | if (ret < 0) { | |
aff00e40 ML |
577 | sr_err("Read error while reading data header."); |
578 | return SR_ERR; | |
579 | } | |
fe0d9caa | 580 | devc->num_header_bytes += ret; |
bafd4890 | 581 | } |
aff00e40 | 582 | |
fe0d9caa | 583 | if (devc->num_header_bytes < header_length) |
aff00e40 ML |
584 | return 0; |
585 | ||
586 | /* Read the data length. */ | |
fe0d9caa | 587 | buf[header_length] = '\0'; |
aff00e40 | 588 | |
fe0d9caa | 589 | if (parse_int(buf + 2, &ret) != SR_OK) { |
aff00e40 | 590 | sr_err("Received invalid data block length '%s'.", buf + 2); |
bafd4890 ML |
591 | return -1; |
592 | } | |
593 | ||
fe0d9caa | 594 | sr_dbg("Received data block header: '%s' -> block length %d", buf, ret); |
bafd4890 | 595 | |
fe0d9caa | 596 | return ret; |
bafd4890 ML |
597 | } |
598 | ||
3086efdd | 599 | SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) |
f4816ac6 | 600 | { |
e0b7d23c | 601 | struct sr_dev_inst *sdi; |
ae1bc1cc | 602 | struct sr_scpi_dev_inst *scpi; |
f4816ac6 | 603 | struct dev_context *devc; |
e0b7d23c | 604 | struct sr_datafeed_packet packet; |
246399f7 UH |
605 | struct sr_datafeed_analog analog; |
606 | struct sr_analog_encoding encoding; | |
607 | struct sr_analog_meaning meaning; | |
608 | struct sr_analog_spec spec; | |
6bb192bc | 609 | struct sr_datafeed_logic logic; |
6b04525b | 610 | double vdiv, offset, origin; |
f80a0bf2 | 611 | int len, i, vref; |
ba7dd8bb | 612 | struct sr_channel *ch; |
bac11aeb | 613 | gsize expected_data_bytes; |
f4816ac6 | 614 | |
decfe89d | 615 | (void)fd; |
9bd4c956 | 616 | |
f4816ac6 ML |
617 | if (!(sdi = cb_data)) |
618 | return TRUE; | |
619 | ||
620 | if (!(devc = sdi->priv)) | |
621 | return TRUE; | |
622 | ||
ae1bc1cc | 623 | scpi = sdi->conn; |
9bd4c956 | 624 | |
dc89faea UH |
625 | if (!(revents == G_IO_IN || revents == 0)) |
626 | return TRUE; | |
627 | ||
16e96ca3 VO |
628 | const gboolean first_frame = (devc->num_frames == 0); |
629 | ||
dc89faea UH |
630 | switch (devc->wait_event) { |
631 | case WAIT_NONE: | |
632 | break; | |
633 | case WAIT_TRIGGER: | |
634 | if (rigol_ds_trigger_wait(sdi) != SR_OK) | |
3918fbb0 | 635 | return TRUE; |
dc89faea | 636 | if (rigol_ds_channel_start(sdi) != SR_OK) |
e086b750 | 637 | return TRUE; |
dc89faea UH |
638 | return TRUE; |
639 | case WAIT_BLOCK: | |
640 | if (rigol_ds_block_wait(sdi) != SR_OK) | |
641 | return TRUE; | |
642 | break; | |
643 | case WAIT_STOP: | |
644 | if (rigol_ds_stop_wait(sdi) != SR_OK) | |
645 | return TRUE; | |
646 | if (rigol_ds_check_stop(sdi) != SR_OK) | |
647 | return TRUE; | |
648 | if (rigol_ds_channel_start(sdi) != SR_OK) | |
649 | return TRUE; | |
650 | return TRUE; | |
651 | default: | |
652 | sr_err("BUG: Unknown event target encountered"); | |
653 | break; | |
654 | } | |
f76c24f6 | 655 | |
dc89faea | 656 | ch = devc->channel_entry->data; |
702f42e8 | 657 | |
dc89faea UH |
658 | expected_data_bytes = ch->type == SR_CHANNEL_ANALOG ? |
659 | devc->analog_frame_size : devc->digital_frame_size; | |
bac11aeb | 660 | |
dc89faea UH |
661 | if (devc->num_block_bytes == 0) { |
662 | if (devc->model->series->protocol >= PROTOCOL_V4) { | |
16e96ca3 | 663 | if (first_frame && rigol_ds_config_set(sdi, ":WAV:START %d", |
dc89faea UH |
664 | devc->num_channel_bytes + 1) != SR_OK) |
665 | return TRUE; | |
16e96ca3 | 666 | if (first_frame && rigol_ds_config_set(sdi, ":WAV:STOP %d", |
dc89faea UH |
667 | MIN(devc->num_channel_bytes + ACQ_BLOCK_SIZE, |
668 | devc->analog_frame_size)) != SR_OK) | |
05c644ea | 669 | return TRUE; |
bafd4890 | 670 | } |
f80a0bf2 | 671 | |
704910e3 VO |
672 | if (devc->model->series->protocol >= PROTOCOL_V3) { |
673 | if (rigol_ds_config_set(sdi, ":WAV:BEG") != SR_OK) | |
674 | return TRUE; | |
dc89faea UH |
675 | if (sr_scpi_send(sdi->conn, ":WAV:DATA?") != SR_OK) |
676 | return TRUE; | |
704910e3 | 677 | } |
f80a0bf2 | 678 | |
dc89faea | 679 | if (sr_scpi_read_begin(scpi) != SR_OK) |
7d63347e | 680 | return TRUE; |
6bb192bc | 681 | |
dc89faea UH |
682 | if (devc->format == FORMAT_IEEE488_2) { |
683 | sr_dbg("New block header expected"); | |
684 | len = rigol_ds_read_header(sdi); | |
685 | if (len == 0) | |
686 | /* Still reading the header. */ | |
687 | return TRUE; | |
688 | if (len == -1) { | |
350501d0 | 689 | sr_err("Error while reading block header, aborting capture."); |
4c5f7006 | 690 | std_session_send_df_frame_end(sdi); |
d2f7c417 | 691 | sr_dev_acquisition_stop(sdi); |
3ed7a40c ML |
692 | return TRUE; |
693 | } | |
69990295 AL |
694 | /* At slow timebases in live capture the DS2072 and |
695 | * DS1054Z sometimes return "short" data blocks, with | |
dc89faea | 696 | * apparently no way to get the rest of the data. |
69990295 AL |
697 | * Discard these, the complete data block will appear |
698 | * eventually. | |
dc89faea UH |
699 | */ |
700 | if (devc->data_source == DATA_SOURCE_LIVE | |
701 | && (unsigned)len < expected_data_bytes) { | |
69990295 | 702 | sr_dbg("Discarding short data block: got %d/%d bytes\n", len, (int)expected_data_bytes); |
dc89faea | 703 | sr_scpi_read_data(scpi, (char *)devc->buffer, len + 1); |
69990295 | 704 | devc->num_header_bytes = 0; |
dc89faea UH |
705 | return TRUE; |
706 | } | |
707 | devc->num_block_bytes = len; | |
48460c6f | 708 | } else { |
dc89faea | 709 | devc->num_block_bytes = expected_data_bytes; |
ee7e9bee | 710 | } |
dc89faea UH |
711 | devc->num_block_read = 0; |
712 | } | |
75d8a4e5 | 713 | |
dc89faea UH |
714 | len = devc->num_block_bytes - devc->num_block_read; |
715 | if (len > ACQ_BUFFER_SIZE) | |
716 | len = ACQ_BUFFER_SIZE; | |
717 | sr_dbg("Requesting read of %d bytes", len); | |
48460c6f | 718 | |
dc89faea | 719 | len = sr_scpi_read_data(scpi, (char *)devc->buffer, len); |
48460c6f | 720 | |
dc89faea | 721 | if (len == -1) { |
350501d0 | 722 | sr_err("Error while reading block data, aborting capture."); |
4c5f7006 | 723 | std_session_send_df_frame_end(sdi); |
d2f7c417 | 724 | sr_dev_acquisition_stop(sdi); |
dc89faea UH |
725 | return TRUE; |
726 | } | |
727 | ||
728 | sr_dbg("Received %d bytes.", len); | |
729 | ||
730 | devc->num_block_read += len; | |
731 | ||
732 | if (ch->type == SR_CHANNEL_ANALOG) { | |
733 | vref = devc->vert_reference[ch->index]; | |
6b04525b VO |
734 | vdiv = devc->vert_inc[ch->index]; |
735 | origin = devc->vert_origin[ch->index]; | |
dc89faea UH |
736 | offset = devc->vert_offset[ch->index]; |
737 | if (devc->model->series->protocol >= PROTOCOL_V3) | |
738 | for (i = 0; i < len; i++) | |
6b04525b | 739 | devc->data[i] = ((int)devc->buffer[i] - vref - origin) * vdiv; |
dc89faea UH |
740 | else |
741 | for (i = 0; i < len; i++) | |
742 | devc->data[i] = (128 - devc->buffer[i]) * vdiv - offset; | |
b8f07f42 AJ |
743 | float vdivlog = log10f(vdiv); |
744 | int digits = -(int)vdivlog + (vdivlog < 0.0); | |
745 | sr_analog_init(&analog, &encoding, &meaning, &spec, digits); | |
246399f7 | 746 | analog.meaning->channels = g_slist_append(NULL, ch); |
dc89faea UH |
747 | analog.num_samples = len; |
748 | analog.data = devc->data; | |
246399f7 UH |
749 | analog.meaning->mq = SR_MQ_VOLTAGE; |
750 | analog.meaning->unit = SR_UNIT_VOLT; | |
751 | analog.meaning->mqflags = 0; | |
752 | packet.type = SR_DF_ANALOG; | |
dc89faea | 753 | packet.payload = &analog; |
695dc859 | 754 | sr_session_send(sdi, &packet); |
246399f7 | 755 | g_slist_free(analog.meaning->channels); |
dc89faea UH |
756 | } else { |
757 | logic.length = len; | |
758 | // TODO: For the MSO1000Z series, we need a way to express that | |
759 | // this data is in fact just for a single channel, with the valid | |
760 | // data for that channel in the LSB of each byte. | |
f6129c8f | 761 | logic.unitsize = devc->model->series->protocol >= PROTOCOL_V4 ? 1 : 2; |
dc89faea UH |
762 | logic.data = devc->buffer; |
763 | packet.type = SR_DF_LOGIC; | |
764 | packet.payload = &logic; | |
695dc859 | 765 | sr_session_send(sdi, &packet); |
dc89faea UH |
766 | } |
767 | ||
768 | if (devc->num_block_read == devc->num_block_bytes) { | |
769 | sr_dbg("Block has been completed"); | |
770 | if (devc->model->series->protocol >= PROTOCOL_V3) { | |
771 | /* Discard the terminating linefeed */ | |
772 | sr_scpi_read_data(scpi, (char *)devc->buffer, 1); | |
773 | } | |
774 | if (devc->format == FORMAT_IEEE488_2) { | |
775 | /* Prepare for possible next block */ | |
776 | devc->num_header_bytes = 0; | |
777 | devc->num_block_bytes = 0; | |
babab622 | 778 | if (devc->data_source != DATA_SOURCE_LIVE) |
dc89faea | 779 | rigol_ds_set_wait_event(devc, WAIT_BLOCK); |
babab622 | 780 | } |
ce97fc3f | 781 | if (!sr_scpi_read_complete(scpi) && !devc->channel_entry->next) { |
dc89faea | 782 | sr_err("Read should have been completed"); |
dc89faea UH |
783 | } |
784 | devc->num_block_read = 0; | |
785 | } else { | |
6433156c DE |
786 | sr_dbg("%" PRIu64 " of %" PRIu64 " block bytes read", |
787 | devc->num_block_read, devc->num_block_bytes); | |
dc89faea | 788 | } |
f76c24f6 | 789 | |
dc89faea | 790 | devc->num_channel_bytes += len; |
f76c24f6 | 791 | |
dc89faea UH |
792 | if (devc->num_channel_bytes < expected_data_bytes) |
793 | /* Don't have the full data for this channel yet, re-run. */ | |
794 | return TRUE; | |
f76c24f6 | 795 | |
dc89faea UH |
796 | /* End of data for this channel. */ |
797 | if (devc->model->series->protocol == PROTOCOL_V3) { | |
798 | /* Signal end of data download to scope */ | |
799 | if (devc->data_source != DATA_SOURCE_LIVE) | |
800 | /* | |
801 | * This causes a query error, without it switching | |
802 | * to the next channel causes an error. Fun with | |
803 | * firmware... | |
804 | */ | |
805 | rigol_ds_config_set(sdi, ":WAV:END"); | |
806 | } | |
807 | ||
808 | if (devc->channel_entry->next) { | |
809 | /* We got the frame for this channel, now get the next channel. */ | |
810 | devc->channel_entry = devc->channel_entry->next; | |
811 | rigol_ds_channel_start(sdi); | |
812 | } else { | |
813 | /* Done with this frame. */ | |
4c5f7006 | 814 | std_session_send_df_frame_end(sdi); |
dc89faea | 815 | |
19f31c8a VO |
816 | devc->num_frames++; |
817 | ||
818 | /* V5 has no way to read the number of recorded frames, so try to set the | |
819 | * next frame and read it back instead. | |
820 | */ | |
821 | if (devc->data_source == DATA_SOURCE_SEGMENTED && | |
822 | devc->model->series->protocol == PROTOCOL_V5) { | |
823 | int frames = 0; | |
824 | if (rigol_ds_config_set(sdi, "REC:CURR %d", devc->num_frames + 1) != SR_OK) | |
825 | return SR_ERR; | |
826 | if (sr_scpi_get_int(sdi->conn, "REC:CURR?", &frames) != SR_OK) | |
827 | return SR_ERR; | |
828 | devc->num_frames_segmented = frames; | |
829 | } | |
830 | ||
831 | if (devc->num_frames == devc->limit_frames || | |
8cbe5339 VO |
832 | devc->num_frames == devc->num_frames_segmented || |
833 | devc->data_source == DATA_SOURCE_MEMORY) { | |
dc89faea | 834 | /* Last frame, stop capture. */ |
d2f7c417 | 835 | sr_dev_acquisition_stop(sdi); |
dc89faea UH |
836 | } else { |
837 | /* Get the next frame, starting with the first channel. */ | |
838 | devc->channel_entry = devc->enabled_channels; | |
839 | ||
840 | rigol_ds_capture_start(sdi); | |
841 | ||
842 | /* Start of next frame. */ | |
4c5f7006 | 843 | std_session_send_df_frame_begin(sdi); |
75d8a4e5 | 844 | } |
f4816ac6 ML |
845 | } |
846 | ||
847 | return TRUE; | |
848 | } | |
e0b7d23c | 849 | |
3086efdd | 850 | SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi) |
254dd102 BV |
851 | { |
852 | struct dev_context *devc; | |
e264ebde | 853 | struct sr_channel *ch; |
98bfc474 | 854 | char *cmd; |
821fbcad ML |
855 | unsigned int i; |
856 | int res; | |
254dd102 BV |
857 | |
858 | devc = sdi->priv; | |
859 | ||
6bb192bc | 860 | /* Analog channel state. */ |
821fbcad ML |
861 | for (i = 0; i < devc->model->analog_channels; i++) { |
862 | cmd = g_strdup_printf(":CHAN%d:DISP?", i + 1); | |
98bfc474 | 863 | res = sr_scpi_get_bool(sdi->conn, cmd, &devc->analog_channels[i]); |
821fbcad ML |
864 | g_free(cmd); |
865 | if (res != SR_OK) | |
866 | return SR_ERR; | |
e264ebde AJ |
867 | ch = g_slist_nth_data(sdi->channels, i); |
868 | ch->enabled = devc->analog_channels[i]; | |
821fbcad ML |
869 | } |
870 | sr_dbg("Current analog channel state:"); | |
871 | for (i = 0; i < devc->model->analog_channels; i++) | |
872 | sr_dbg("CH%d %s", i + 1, devc->analog_channels[i] ? "on" : "off"); | |
6bb192bc ML |
873 | |
874 | /* Digital channel state. */ | |
bafd4890 | 875 | if (devc->model->has_digital) { |
702f42e8 | 876 | if (sr_scpi_get_bool(sdi->conn, |
01dd7a4c | 877 | devc->model->series->protocol >= PROTOCOL_V3 ? |
702f42e8 | 878 | ":LA:STAT?" : ":LA:DISP?", |
98bfc474 | 879 | &devc->la_enabled) != SR_OK) |
04e8e01e | 880 | return SR_ERR; |
04e8e01e ML |
881 | sr_dbg("Logic analyzer %s, current digital channel state:", |
882 | devc->la_enabled ? "enabled" : "disabled"); | |
effb9dd1 | 883 | for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) { |
f6129c8f SR |
884 | if (devc->model->series->protocol >= PROTOCOL_V5) |
885 | cmd = g_strdup_printf(":LA:DISP? D%d", i); | |
886 | else if (devc->model->series->protocol >= PROTOCOL_V3) | |
887 | cmd = g_strdup_printf(":LA:DIG%d:DISP?", i); | |
888 | else | |
889 | cmd = g_strdup_printf(":DIG%d:TURN?", i); | |
98bfc474 | 890 | res = sr_scpi_get_bool(sdi->conn, cmd, &devc->digital_channels[i]); |
6bb192bc ML |
891 | g_free(cmd); |
892 | if (res != SR_OK) | |
893 | return SR_ERR; | |
e264ebde AJ |
894 | ch = g_slist_nth_data(sdi->channels, i + devc->model->analog_channels); |
895 | ch->enabled = devc->digital_channels[i]; | |
bfaf112b | 896 | sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "on" : "off"); |
6bb192bc ML |
897 | } |
898 | } | |
254dd102 BV |
899 | |
900 | /* Timebase. */ | |
334fbc2a | 901 | if (sr_scpi_get_float(sdi->conn, ":TIM:SCAL?", &devc->timebase) != SR_OK) |
254dd102 | 902 | return SR_ERR; |
bafd4890 | 903 | sr_dbg("Current timebase %g", devc->timebase); |
254dd102 | 904 | |
934cf6cf AJ |
905 | /* Probe attenuation. */ |
906 | for (i = 0; i < devc->model->analog_channels; i++) { | |
907 | cmd = g_strdup_printf(":CHAN%d:PROB?", i + 1); | |
34ce4d82 VO |
908 | |
909 | /* DSO1000B series prints an X after the probe factor, so | |
910 | * we get a string and check for that instead of only handling | |
911 | * floats. */ | |
912 | char *response; | |
913 | res = sr_scpi_get_string(sdi->conn, cmd, &response); | |
914 | if (res != SR_OK) | |
915 | return SR_ERR; | |
916 | ||
917 | int len = strlen(response); | |
918 | if (response[len-1] == 'X') | |
919 | response[len-1] = 0; | |
920 | ||
921 | res = sr_atof_ascii(response, &devc->attenuation[i]); | |
922 | g_free(response); | |
934cf6cf AJ |
923 | g_free(cmd); |
924 | if (res != SR_OK) | |
925 | return SR_ERR; | |
926 | } | |
927 | sr_dbg("Current probe attenuation:"); | |
928 | for (i = 0; i < devc->model->analog_channels; i++) | |
929 | sr_dbg("CH%d %g", i + 1, devc->attenuation[i]); | |
930 | ||
8719638f AJ |
931 | /* Vertical gain and offset. */ |
932 | if (rigol_ds_get_dev_cfg_vertical(sdi) != SR_OK) | |
933 | return SR_ERR; | |
254dd102 BV |
934 | |
935 | /* Coupling. */ | |
821fbcad ML |
936 | for (i = 0; i < devc->model->analog_channels; i++) { |
937 | cmd = g_strdup_printf(":CHAN%d:COUP?", i + 1); | |
334fbc2a | 938 | res = sr_scpi_get_string(sdi->conn, cmd, &devc->coupling[i]); |
821fbcad ML |
939 | g_free(cmd); |
940 | if (res != SR_OK) | |
941 | return SR_ERR; | |
942 | } | |
943 | sr_dbg("Current coupling:"); | |
944 | for (i = 0; i < devc->model->analog_channels; i++) | |
945 | sr_dbg("CH%d %s", i + 1, devc->coupling[i]); | |
254dd102 BV |
946 | |
947 | /* Trigger source. */ | |
334fbc2a | 948 | if (sr_scpi_get_string(sdi->conn, ":TRIG:EDGE:SOUR?", &devc->trigger_source) != SR_OK) |
254dd102 BV |
949 | return SR_ERR; |
950 | sr_dbg("Current trigger source %s", devc->trigger_source); | |
951 | ||
952 | /* Horizontal trigger position. */ | |
0f523f2b UH |
953 | if (sr_scpi_get_float(sdi->conn, devc->model->cmds[CMD_GET_HORIZ_TRIGGERPOS].str, |
954 | &devc->horiz_triggerpos) != SR_OK) | |
254dd102 | 955 | return SR_ERR; |
bafd4890 | 956 | sr_dbg("Current horizontal trigger position %g", devc->horiz_triggerpos); |
254dd102 BV |
957 | |
958 | /* Trigger slope. */ | |
334fbc2a | 959 | if (sr_scpi_get_string(sdi->conn, ":TRIG:EDGE:SLOP?", &devc->trigger_slope) != SR_OK) |
254dd102 BV |
960 | return SR_ERR; |
961 | sr_dbg("Current trigger slope %s", devc->trigger_slope); | |
962 | ||
9ea62f2e AJ |
963 | /* Trigger level. */ |
964 | if (sr_scpi_get_float(sdi->conn, ":TRIG:EDGE:LEV?", &devc->trigger_level) != SR_OK) | |
965 | return SR_ERR; | |
966 | sr_dbg("Current trigger level %g", devc->trigger_level); | |
967 | ||
254dd102 BV |
968 | return SR_OK; |
969 | } | |
8719638f AJ |
970 | |
971 | SR_PRIV int rigol_ds_get_dev_cfg_vertical(const struct sr_dev_inst *sdi) | |
972 | { | |
973 | struct dev_context *devc; | |
974 | char *cmd; | |
975 | unsigned int i; | |
976 | int res; | |
977 | ||
978 | devc = sdi->priv; | |
979 | ||
980 | /* Vertical gain. */ | |
981 | for (i = 0; i < devc->model->analog_channels; i++) { | |
982 | cmd = g_strdup_printf(":CHAN%d:SCAL?", i + 1); | |
983 | res = sr_scpi_get_float(sdi->conn, cmd, &devc->vdiv[i]); | |
984 | g_free(cmd); | |
985 | if (res != SR_OK) | |
986 | return SR_ERR; | |
987 | } | |
988 | sr_dbg("Current vertical gain:"); | |
989 | for (i = 0; i < devc->model->analog_channels; i++) | |
990 | sr_dbg("CH%d %g", i + 1, devc->vdiv[i]); | |
991 | ||
992 | /* Vertical offset. */ | |
993 | for (i = 0; i < devc->model->analog_channels; i++) { | |
994 | cmd = g_strdup_printf(":CHAN%d:OFFS?", i + 1); | |
995 | res = sr_scpi_get_float(sdi->conn, cmd, &devc->vert_offset[i]); | |
996 | g_free(cmd); | |
997 | if (res != SR_OK) | |
998 | return SR_ERR; | |
999 | } | |
1000 | sr_dbg("Current vertical offset:"); | |
1001 | for (i = 0; i < devc->model->analog_channels; i++) | |
1002 | sr_dbg("CH%d %g", i + 1, devc->vert_offset[i]); | |
1003 | ||
1004 | return SR_OK; | |
1005 | } |