]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2016 mhooijboer <marchelh@gmail.com> | |
5 | * Copyright (C) 2012 Martin Ling <martin-git@earth.li> | |
6 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
7 | * Copyright (C) 2013 Mathias Grimmberger <mgri@zaphod.sax.de> | |
8 | * | |
9 | * This program is free software: you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation, either version 3 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | */ | |
22 | ||
23 | #define _GNU_SOURCE | |
24 | ||
25 | #include <config.h> | |
26 | #include <errno.h> | |
27 | #include <glib.h> | |
28 | #include <math.h> | |
29 | #include <stdlib.h> | |
30 | #include <stdarg.h> | |
31 | #include <string.h> | |
32 | #include <time.h> | |
33 | #include <unistd.h> | |
34 | #include <libsigrok/libsigrok.h> | |
35 | #include "libsigrok-internal.h" | |
36 | #include "scpi.h" | |
37 | #include "protocol.h" | |
38 | ||
39 | /* Set the next event to wait for in siglent_sds_receive(). */ | |
40 | static void siglent_sds_set_wait_event(struct dev_context *devc, enum wait_events event) | |
41 | { | |
42 | if (event == WAIT_STOP) { | |
43 | devc->wait_status = 2; | |
44 | } else { | |
45 | devc->wait_status = 1; | |
46 | devc->wait_event = event; | |
47 | } | |
48 | } | |
49 | ||
50 | /* | |
51 | * Waiting for a event will return a timeout after 2 to 3 seconds in order | |
52 | * to not block the application. | |
53 | */ | |
54 | static int siglent_sds_event_wait(const struct sr_dev_inst *sdi) | |
55 | { | |
56 | char *buf; | |
57 | long s; | |
58 | int out; | |
59 | struct dev_context *devc; | |
60 | time_t start; | |
61 | ||
62 | if (!(devc = sdi->priv)) | |
63 | return SR_ERR; | |
64 | ||
65 | start = time(NULL); | |
66 | ||
67 | s = 10000; /* Sleep time for status refresh. */ | |
68 | if (devc->wait_status == 1) { | |
69 | do { | |
70 | if (time(NULL) - start >= 3) { | |
71 | sr_dbg("Timeout waiting for trigger."); | |
72 | return SR_ERR_TIMEOUT; | |
73 | } | |
74 | ||
75 | if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK) | |
76 | return SR_ERR; | |
77 | sr_atoi(buf, &out); | |
78 | g_free(buf); | |
79 | g_usleep(s); | |
80 | } while (out == 0); | |
81 | ||
82 | sr_dbg("Device triggered."); | |
83 | ||
84 | if ((devc->timebase < 0.51) && (devc->timebase > 0.99e-6)) { | |
85 | /* | |
86 | * Timebase * num hor. divs * 85(%) * 1e6(usecs) / 100 | |
87 | * -> 85 percent of sweep time | |
88 | */ | |
89 | s = (devc->timebase * devc->model->series->num_horizontal_divs * 1000); | |
90 | sr_spew("Sleeping for %ld usecs after trigger, " | |
91 | "to let the acq buffer in the device fill", s); | |
92 | g_usleep(s); | |
93 | } | |
94 | } | |
95 | if (devc->wait_status == 2) { | |
96 | do { | |
97 | if (time(NULL) - start >= 3) { | |
98 | sr_dbg("Timeout waiting for trigger."); | |
99 | return SR_ERR_TIMEOUT; | |
100 | } | |
101 | if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK) | |
102 | return SR_ERR; | |
103 | sr_atoi(buf, &out); | |
104 | g_free(buf); | |
105 | g_usleep(s); | |
106 | /* XXX | |
107 | * Now this loop condition looks suspicious! A bitwise | |
108 | * OR of a variable and a non-zero literal should be | |
109 | * non-zero. Logical AND of several non-zero values | |
110 | * should be non-zero. Are many parts of the condition | |
111 | * not taking effect? Was some different condition meant | |
112 | * to get encoded? This needs review, and adjustment. | |
113 | */ | |
114 | } while (out != DEVICE_STATE_TRIG_RDY || out != DEVICE_STATE_DATA_TRIG_RDY || out != DEVICE_STATE_STOPPED); | |
115 | ||
116 | sr_dbg("Device triggered."); | |
117 | ||
118 | siglent_sds_set_wait_event(devc, WAIT_NONE); | |
119 | } | |
120 | ||
121 | return SR_OK; | |
122 | } | |
123 | ||
124 | static int siglent_sds_trigger_wait(const struct sr_dev_inst *sdi) | |
125 | { | |
126 | struct dev_context *devc; | |
127 | ||
128 | if (!(devc = sdi->priv)) | |
129 | return SR_ERR; | |
130 | return siglent_sds_event_wait(sdi); | |
131 | } | |
132 | ||
133 | /* Wait for scope to got to "Stop" in single shot mode. */ | |
134 | static int siglent_sds_stop_wait(const struct sr_dev_inst *sdi) | |
135 | { | |
136 | return siglent_sds_event_wait(sdi); | |
137 | } | |
138 | ||
139 | /* Send a configuration setting. */ | |
140 | SR_PRIV int siglent_sds_config_set(const struct sr_dev_inst *sdi, const char *format, ...) | |
141 | { | |
142 | va_list args; | |
143 | int ret; | |
144 | ||
145 | va_start(args, format); | |
146 | ret = sr_scpi_send_variadic(sdi->conn, format, args); | |
147 | va_end(args); | |
148 | ||
149 | return ret; | |
150 | } | |
151 | ||
152 | /* Start capturing a new frameset. */ | |
153 | SR_PRIV int siglent_sds_capture_start(const struct sr_dev_inst *sdi) | |
154 | { | |
155 | struct dev_context *devc; | |
156 | ||
157 | if (!(devc = sdi->priv)) | |
158 | return SR_ERR; | |
159 | ||
160 | switch (devc->model->series->protocol) { | |
161 | case SPO_MODEL: | |
162 | if (devc->data_source == DATA_SOURCE_SCREEN) { | |
163 | char *buf; | |
164 | int out; | |
165 | ||
166 | sr_dbg("Starting data capture for active frameset %" PRIu64 " of %" PRIu64, | |
167 | devc->num_frames + 1, devc->limit_frames); | |
168 | if (siglent_sds_config_set(sdi, "ARM") != SR_OK) | |
169 | return SR_ERR; | |
170 | if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK) | |
171 | return SR_ERR; | |
172 | sr_atoi(buf, &out); | |
173 | g_free(buf); | |
174 | if (out == DEVICE_STATE_TRIG_RDY) { | |
175 | siglent_sds_set_wait_event(devc, WAIT_TRIGGER); | |
176 | } else if (out == DEVICE_STATE_DATA_TRIG_RDY) { | |
177 | sr_spew("Device triggered."); | |
178 | siglent_sds_set_wait_event(devc, WAIT_BLOCK); | |
179 | return SR_OK; | |
180 | } else { | |
181 | sr_spew("Device did not enter ARM mode."); | |
182 | return SR_ERR; | |
183 | } | |
184 | } else { /* TODO: Implement history retrieval. */ | |
185 | unsigned int framecount; | |
186 | char buf[200]; | |
187 | int ret; | |
188 | ||
189 | sr_dbg("Starting data capture for history frameset."); | |
190 | if (siglent_sds_config_set(sdi, "FPAR?") != SR_OK) | |
191 | return SR_ERR; | |
192 | ret = sr_scpi_read_data(sdi->conn, buf, 200); | |
193 | if (ret < 0) { | |
194 | sr_err("Read error while reading data header."); | |
195 | return SR_ERR; | |
196 | } | |
197 | memcpy(&framecount, buf + 40, 4); | |
198 | if (devc->limit_frames > framecount) | |
199 | sr_err("Frame limit higher than frames in buffer of device!"); | |
200 | else if (devc->limit_frames == 0) | |
201 | devc->limit_frames = framecount; | |
202 | sr_dbg("Starting data capture for history frameset %" PRIu64 " of %" PRIu64, | |
203 | devc->num_frames + 1, devc->limit_frames); | |
204 | if (siglent_sds_config_set(sdi, "FRAM %i", devc->num_frames + 1) != SR_OK) | |
205 | return SR_ERR; | |
206 | if (siglent_sds_channel_start(sdi) != SR_OK) | |
207 | return SR_ERR; | |
208 | siglent_sds_set_wait_event(devc, WAIT_STOP); | |
209 | } | |
210 | break; | |
211 | case ESERIES: | |
212 | if (devc->data_source == DATA_SOURCE_SCREEN) { | |
213 | char *buf; | |
214 | int out; | |
215 | ||
216 | sr_dbg("Starting data capture for active frameset %" PRIu64 " of %" PRIu64, | |
217 | devc->num_frames + 1, devc->limit_frames); | |
218 | if (siglent_sds_config_set(sdi, "ARM") != SR_OK) | |
219 | return SR_ERR; | |
220 | if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK) | |
221 | return SR_ERR; | |
222 | sr_atoi(buf, &out); | |
223 | g_free(buf); | |
224 | if (out == DEVICE_STATE_TRIG_RDY) { | |
225 | siglent_sds_set_wait_event(devc, WAIT_TRIGGER); | |
226 | } else if (out == DEVICE_STATE_DATA_TRIG_RDY) { | |
227 | sr_spew("Device triggered."); | |
228 | siglent_sds_set_wait_event(devc, WAIT_BLOCK); | |
229 | return SR_OK; | |
230 | } else { | |
231 | sr_spew("Device did not enter ARM mode."); | |
232 | return SR_ERR; | |
233 | } | |
234 | } else { /* TODO: Implement history retrieval. */ | |
235 | unsigned int framecount; | |
236 | char buf[200]; | |
237 | int ret; | |
238 | ||
239 | sr_dbg("Starting data capture for history frameset."); | |
240 | if (siglent_sds_config_set(sdi, "FPAR?") != SR_OK) | |
241 | return SR_ERR; | |
242 | ret = sr_scpi_read_data(sdi->conn, buf, 200); | |
243 | if (ret < 0) { | |
244 | sr_err("Read error while reading data header."); | |
245 | return SR_ERR; | |
246 | } | |
247 | memcpy(&framecount, buf + 40, 4); | |
248 | if (devc->limit_frames > framecount) | |
249 | sr_err("Frame limit higher than frames in buffer of device!"); | |
250 | else if (devc->limit_frames == 0) | |
251 | devc->limit_frames = framecount; | |
252 | sr_dbg("Starting data capture for history frameset %" PRIu64 " of %" PRIu64, | |
253 | devc->num_frames + 1, devc->limit_frames); | |
254 | if (siglent_sds_config_set(sdi, "FRAM %i", devc->num_frames + 1) != SR_OK) | |
255 | return SR_ERR; | |
256 | if (siglent_sds_channel_start(sdi) != SR_OK) | |
257 | return SR_ERR; | |
258 | siglent_sds_set_wait_event(devc, WAIT_STOP); | |
259 | } | |
260 | break; | |
261 | case NON_SPO_MODEL: | |
262 | siglent_sds_set_wait_event(devc, WAIT_TRIGGER); | |
263 | break; | |
264 | } | |
265 | ||
266 | return SR_OK; | |
267 | } | |
268 | ||
269 | /* Start reading data from the current channel. */ | |
270 | SR_PRIV int siglent_sds_channel_start(const struct sr_dev_inst *sdi) | |
271 | { | |
272 | struct dev_context *devc; | |
273 | struct sr_channel *ch; | |
274 | const char *s; | |
275 | ||
276 | if (!(devc = sdi->priv)) | |
277 | return SR_ERR; | |
278 | ||
279 | ch = devc->channel_entry->data; | |
280 | ||
281 | sr_dbg("Start reading data from channel %s.", ch->name); | |
282 | ||
283 | switch (devc->model->series->protocol) { | |
284 | case NON_SPO_MODEL: | |
285 | case SPO_MODEL: | |
286 | s = (ch->type == SR_CHANNEL_LOGIC) ? "D%d:WF?" : "C%d:WF? ALL"; | |
287 | if (sr_scpi_send(sdi->conn, s, ch->index + 1) != SR_OK) | |
288 | return SR_ERR; | |
289 | siglent_sds_set_wait_event(devc, WAIT_NONE); | |
290 | break; | |
291 | case ESERIES: | |
292 | if (ch->type == SR_CHANNEL_ANALOG) { | |
293 | if (sr_scpi_send(sdi->conn, "C%d:WF? ALL", | |
294 | ch->index + 1) != SR_OK) | |
295 | return SR_ERR; | |
296 | } | |
297 | siglent_sds_set_wait_event(devc, WAIT_NONE); | |
298 | if (sr_scpi_read_begin(sdi->conn) != SR_OK) | |
299 | return TRUE; | |
300 | siglent_sds_set_wait_event(devc, WAIT_BLOCK); | |
301 | break; | |
302 | } | |
303 | ||
304 | devc->num_channel_bytes = 0; | |
305 | devc->num_header_bytes = 0; | |
306 | devc->num_block_bytes = 0; | |
307 | ||
308 | return SR_OK; | |
309 | } | |
310 | ||
311 | /* Read the header of a data block. */ | |
312 | static int siglent_sds_read_header(struct sr_dev_inst *sdi) | |
313 | { | |
314 | struct sr_scpi_dev_inst *scpi = sdi->conn; | |
315 | struct dev_context *devc = sdi->priv; | |
316 | char *buf = (char *)devc->buffer; | |
317 | int ret, desc_length; | |
318 | int block_offset = 15; /* Offset for descriptor block. */ | |
319 | long data_length = 0; | |
320 | ||
321 | /* Read header from device. */ | |
322 | ret = sr_scpi_read_data(scpi, buf, SIGLENT_HEADER_SIZE); | |
323 | if (ret < SIGLENT_HEADER_SIZE) { | |
324 | sr_err("Read error while reading data header."); | |
325 | return SR_ERR; | |
326 | } | |
327 | sr_dbg("Device returned %i bytes.", ret); | |
328 | devc->num_header_bytes += ret; | |
329 | buf += block_offset; /* Skip to start descriptor block. */ | |
330 | ||
331 | /* Parse WaveDescriptor header. */ | |
332 | memcpy(&desc_length, buf + 36, 4); /* Descriptor block length */ | |
333 | memcpy(&data_length, buf + 60, 4); /* Data block length */ | |
334 | ||
335 | devc->block_header_size = desc_length + 15; | |
336 | devc->num_samples = data_length; | |
337 | ||
338 | sr_dbg("Received data block header: '%s' -> block length %d.", buf, ret); | |
339 | ||
340 | return ret; | |
341 | } | |
342 | ||
343 | static int siglent_sds_get_digital(const struct sr_dev_inst *sdi, struct sr_channel *ch) | |
344 | { | |
345 | struct sr_scpi_dev_inst *scpi = sdi->conn; | |
346 | struct dev_context *devc = sdi->priv; | |
347 | GArray *tmp_samplebuf; /* Temp buffer while iterating over the scope samples */ | |
348 | char *buf = (char *)devc->buffer; /* Buffer from scope */ | |
349 | uint8_t tmp_value; /* Holding temp value from data */ | |
350 | GArray *data_low_channels, *data_high_channels, *buffdata; | |
351 | GSList *l; | |
352 | gboolean low_channels; /* Lower channels enabled */ | |
353 | gboolean high_channels; /* Higher channels enabled */ | |
354 | int len, channel_index; | |
355 | uint64_t samples_index; | |
356 | ||
357 | len = 0; | |
358 | channel_index = 0; | |
359 | low_channels = FALSE; | |
360 | high_channels = FALSE; | |
361 | data_low_channels = g_array_new(FALSE, TRUE, sizeof(uint8_t)); | |
362 | data_high_channels = g_array_new(FALSE, TRUE, sizeof(uint8_t)); | |
363 | ||
364 | for (l = sdi->channels; l; l = l->next) { | |
365 | ch = l->data; | |
366 | samples_index = 0; | |
367 | if (ch->type == SR_CHANNEL_LOGIC) { | |
368 | if (ch->enabled) { | |
369 | if (sr_scpi_send(sdi->conn, "D%d:WF? DAT2", ch->index) != SR_OK) | |
370 | return SR_ERR; | |
371 | if (sr_scpi_read_begin(scpi) != SR_OK) | |
372 | return TRUE; | |
373 | len = sr_scpi_read_data(scpi, buf, -1); | |
374 | if (len < 0) | |
375 | return TRUE; | |
376 | len -= 15; | |
377 | buffdata = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len); | |
378 | buf += 15; /* Skipping the data header. */ | |
379 | g_array_append_vals(buffdata, buf, len); | |
380 | tmp_samplebuf = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len); /* New temp buffer. */ | |
381 | for (uint64_t cur_sample_index = 0; cur_sample_index < devc->memory_depth_digital; cur_sample_index++) { | |
382 | char sample = (char)g_array_index(buffdata, uint8_t, cur_sample_index); | |
383 | for (int ii = 0; ii < 8; ii++, sample >>= 1) { | |
384 | if (ch->index < 8) { | |
385 | channel_index = ch->index; | |
386 | if (data_low_channels->len <= samples_index) { | |
387 | tmp_value = 0; /* New sample. */ | |
388 | low_channels = TRUE; /* We have at least one enabled low channel. */ | |
389 | } else { | |
390 | /* Get previous stored sample from low channel buffer. */ | |
391 | tmp_value = g_array_index(data_low_channels, uint8_t, samples_index); | |
392 | } | |
393 | } else { | |
394 | channel_index = ch->index - 8; | |
395 | if (data_high_channels->len <= samples_index) { | |
396 | tmp_value = 0; /* New sample. */ | |
397 | high_channels = TRUE; /* We have at least one enabled high channel. */ | |
398 | } else { | |
399 | /* Get previous stored sample from high channel buffer. */ | |
400 | tmp_value = g_array_index(data_high_channels, uint8_t, samples_index); | |
401 | } | |
402 | } | |
403 | /* Check if the current scope sample bit is set. */ | |
404 | if (sample & 0x1) | |
405 | tmp_value |= 1UL << channel_index; /* Set current scope sample bit based on channel index. */ | |
406 | g_array_append_val(tmp_samplebuf, tmp_value); | |
407 | samples_index++; | |
408 | } | |
409 | } | |
410 | ||
411 | /* Clear the buffers to prepare for the new samples */ | |
412 | if (ch->index < 8) { | |
413 | g_array_free(data_low_channels, FALSE); | |
414 | data_low_channels = g_array_new(FALSE, FALSE, sizeof(uint8_t)); | |
415 | } else { | |
416 | g_array_free(data_high_channels, FALSE); | |
417 | data_high_channels = g_array_new(FALSE, FALSE, sizeof(uint8_t)); | |
418 | } | |
419 | ||
420 | /* Storing the converted temp values from the the scope into the buffers. */ | |
421 | for (uint64_t index = 0; index < tmp_samplebuf->len; index++) { | |
422 | uint8_t value = g_array_index(tmp_samplebuf, uint8_t, index); | |
423 | if (ch->index < 8) | |
424 | g_array_append_val(data_low_channels, value); | |
425 | else | |
426 | g_array_append_val(data_high_channels, value); | |
427 | } | |
428 | g_array_free(tmp_samplebuf, TRUE); | |
429 | g_array_free(buffdata, TRUE); | |
430 | } | |
431 | } | |
432 | } | |
433 | ||
434 | /* Combining the lower and higher channel buffers into one buffer for sigrok. */ | |
435 | devc->dig_buffer = g_array_new(FALSE, FALSE, sizeof(uint8_t)); | |
436 | for (uint64_t index = 0; index < devc->memory_depth_digital; index++) { | |
437 | uint8_t value; | |
438 | if (low_channels) { | |
439 | value = g_array_index(data_low_channels, uint8_t, index); | |
440 | g_array_append_val(devc->dig_buffer, value); | |
441 | } else { | |
442 | value = 0; | |
443 | g_array_append_val(devc->dig_buffer, value); | |
444 | } | |
445 | if (high_channels) { | |
446 | value = g_array_index(data_high_channels, uint8_t, index); | |
447 | g_array_append_val(devc->dig_buffer, value); | |
448 | } else { | |
449 | value = 0; | |
450 | g_array_append_val(devc->dig_buffer, value); | |
451 | } | |
452 | } | |
453 | ||
454 | g_array_free(data_low_channels, TRUE); | |
455 | g_array_free(data_high_channels, TRUE); | |
456 | ||
457 | return len; | |
458 | } | |
459 | ||
460 | SR_PRIV int siglent_sds_receive(int fd, int revents, void *cb_data) | |
461 | { | |
462 | struct sr_dev_inst *sdi; | |
463 | struct sr_scpi_dev_inst *scpi; | |
464 | struct dev_context *devc; | |
465 | struct sr_datafeed_packet packet; | |
466 | struct sr_datafeed_analog analog; | |
467 | struct sr_analog_encoding encoding; | |
468 | struct sr_analog_meaning meaning; | |
469 | struct sr_analog_spec spec; | |
470 | struct sr_datafeed_logic logic; | |
471 | struct sr_channel *ch; | |
472 | int len, i; | |
473 | float wait; | |
474 | gboolean read_complete = FALSE; | |
475 | ||
476 | (void)fd; | |
477 | ||
478 | if (!(sdi = cb_data)) | |
479 | return TRUE; | |
480 | ||
481 | if (!(devc = sdi->priv)) | |
482 | return TRUE; | |
483 | ||
484 | scpi = sdi->conn; | |
485 | ||
486 | if (!(revents == G_IO_IN || revents == 0)) | |
487 | return TRUE; | |
488 | ||
489 | switch (devc->wait_event) { | |
490 | case WAIT_NONE: | |
491 | break; | |
492 | case WAIT_TRIGGER: | |
493 | if (siglent_sds_trigger_wait(sdi) != SR_OK) | |
494 | return TRUE; | |
495 | if (siglent_sds_channel_start(sdi) != SR_OK) | |
496 | return TRUE; | |
497 | return TRUE; | |
498 | case WAIT_BLOCK: | |
499 | if (siglent_sds_channel_start(sdi) != SR_OK) | |
500 | return TRUE; | |
501 | break; | |
502 | case WAIT_STOP: | |
503 | if (siglent_sds_stop_wait(sdi) != SR_OK) | |
504 | return TRUE; | |
505 | if (siglent_sds_channel_start(sdi) != SR_OK) | |
506 | return TRUE; | |
507 | return TRUE; | |
508 | default: | |
509 | sr_err("BUG: Unknown event target encountered."); | |
510 | break; | |
511 | } | |
512 | ||
513 | ch = devc->channel_entry->data; | |
514 | len = 0; | |
515 | ||
516 | if (ch->type == SR_CHANNEL_ANALOG) { | |
517 | if (devc->num_block_bytes == 0) { | |
518 | /* Wait for the device to fill its output buffers. */ | |
519 | switch (devc->model->series->protocol) { | |
520 | case NON_SPO_MODEL: | |
521 | case SPO_MODEL: | |
522 | /* The older models need more time to prepare the the output buffers due to CPU speed. */ | |
523 | wait = (devc->memory_depth_analog * 2.5); | |
524 | sr_dbg("Waiting %.f0 ms for device to prepare the output buffers", wait / 1000); | |
525 | g_usleep(wait); | |
526 | if (sr_scpi_read_begin(scpi) != SR_OK) | |
527 | return TRUE; | |
528 | break; | |
529 | case ESERIES: | |
530 | /* The newer models (ending with the E) have faster CPUs but still need time when a slow timebase is selected. */ | |
531 | if (sr_scpi_read_begin(scpi) != SR_OK) | |
532 | return TRUE; | |
533 | wait = ((devc->timebase * devc->model->series->num_horizontal_divs) * 100000); | |
534 | sr_dbg("Waiting %.f0 ms for device to prepare the output buffers", wait / 1000); | |
535 | g_usleep(wait); | |
536 | break; | |
537 | } | |
538 | ||
539 | sr_dbg("New block with header expected."); | |
540 | len = siglent_sds_read_header(sdi); | |
541 | if (len == 0) | |
542 | /* Still reading the header. */ | |
543 | return TRUE; | |
544 | if (len == -1) { | |
545 | sr_err("Read error, aborting capture."); | |
546 | std_session_send_df_frame_end(sdi); | |
547 | sdi->driver->dev_acquisition_stop(sdi); | |
548 | return TRUE; | |
549 | } | |
550 | devc->num_block_bytes = len; | |
551 | devc->num_block_read = 0; | |
552 | ||
553 | if (len == -1) { | |
554 | sr_err("Read error, aborting capture."); | |
555 | std_session_send_df_frame_end(sdi); | |
556 | sdi->driver->dev_acquisition_stop(sdi); | |
557 | return TRUE; | |
558 | } | |
559 | ||
560 | do { | |
561 | read_complete = FALSE; | |
562 | if (devc->num_block_bytes > devc->num_samples) { | |
563 | /* We received all data as one block. */ | |
564 | /* Offset the data block buffer past the IEEE header and description header. */ | |
565 | devc->buffer += devc->block_header_size; | |
566 | len = devc->num_samples; | |
567 | } else { | |
568 | sr_dbg("Requesting: %" PRIu64 " bytes.", devc->num_samples - devc->num_block_bytes); | |
569 | len = sr_scpi_read_data(scpi, (char *)devc->buffer, devc->num_samples-devc->num_block_bytes); | |
570 | if (len == -1) { | |
571 | sr_err("Read error, aborting capture."); | |
572 | std_session_send_df_frame_end(sdi); | |
573 | sdi->driver->dev_acquisition_stop(sdi); | |
574 | return TRUE; | |
575 | } | |
576 | devc->num_block_read++; | |
577 | devc->num_block_bytes += len; | |
578 | } | |
579 | sr_dbg("Received block: %i, %d bytes.", devc->num_block_read, len); | |
580 | if (ch->type == SR_CHANNEL_ANALOG) { | |
581 | float vdiv = devc->vdiv[ch->index]; | |
582 | float offset = devc->vert_offset[ch->index]; | |
583 | GArray *float_data; | |
584 | static GArray *data; | |
585 | float voltage, vdivlog; | |
586 | int digits; | |
587 | ||
588 | data = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len); | |
589 | g_array_append_vals(data, devc->buffer, len); | |
590 | float_data = g_array_new(FALSE, FALSE, sizeof(float)); | |
591 | for (i = 0; i < len; i++) { | |
592 | voltage = (float)g_array_index(data, int8_t, i) / 25; | |
593 | voltage = ((vdiv * voltage) - offset); | |
594 | g_array_append_val(float_data, voltage); | |
595 | } | |
596 | vdivlog = log10f(vdiv); | |
597 | digits = -(int) vdivlog + (vdivlog < 0.0); | |
598 | sr_analog_init(&analog, &encoding, &meaning, &spec, digits); | |
599 | analog.meaning->channels = g_slist_append(NULL, ch); | |
600 | analog.num_samples = float_data->len; | |
601 | analog.data = (float *)float_data->data; | |
602 | analog.meaning->mq = SR_MQ_VOLTAGE; | |
603 | analog.meaning->unit = SR_UNIT_VOLT; | |
604 | analog.meaning->mqflags = 0; | |
605 | packet.type = SR_DF_ANALOG; | |
606 | packet.payload = &analog; | |
607 | sr_session_send(sdi, &packet); | |
608 | g_slist_free(analog.meaning->channels); | |
609 | g_array_free(data, TRUE); | |
610 | } | |
611 | len = 0; | |
612 | if (devc->num_samples == (devc->num_block_bytes - SIGLENT_HEADER_SIZE)) { | |
613 | sr_dbg("Transfer has been completed."); | |
614 | devc->num_header_bytes = 0; | |
615 | devc->num_block_bytes = 0; | |
616 | read_complete = TRUE; | |
617 | if (!sr_scpi_read_complete(scpi)) { | |
618 | sr_err("Read should have been completed."); | |
619 | std_session_send_df_frame_end(sdi); | |
620 | sdi->driver->dev_acquisition_stop(sdi); | |
621 | return TRUE; | |
622 | } | |
623 | devc->num_block_read = 0; | |
624 | } else { | |
625 | sr_dbg("%" PRIu64 " of %" PRIu64 " block bytes read.", | |
626 | devc->num_block_bytes, devc->num_samples); | |
627 | } | |
628 | } while (!read_complete); | |
629 | ||
630 | if (devc->channel_entry->next) { | |
631 | /* We got the frame for this channel, now get the next channel. */ | |
632 | devc->channel_entry = devc->channel_entry->next; | |
633 | siglent_sds_channel_start(sdi); | |
634 | } else { | |
635 | /* Done with this frame. */ | |
636 | std_session_send_df_frame_end(sdi); | |
637 | if (++devc->num_frames == devc->limit_frames) { | |
638 | /* Last frame, stop capture. */ | |
639 | sdi->driver->dev_acquisition_stop(sdi); | |
640 | } else { | |
641 | /* Get the next frame, starting with the first channel. */ | |
642 | devc->channel_entry = devc->enabled_channels; | |
643 | siglent_sds_capture_start(sdi); | |
644 | ||
645 | /* Start of next frame. */ | |
646 | std_session_send_df_frame_begin(sdi); | |
647 | } | |
648 | } | |
649 | } | |
650 | } else { | |
651 | if (!siglent_sds_get_digital(sdi, ch)) | |
652 | return TRUE; | |
653 | logic.length = devc->dig_buffer->len; | |
654 | logic.unitsize = 2; | |
655 | logic.data = devc->dig_buffer->data; | |
656 | packet.type = SR_DF_LOGIC; | |
657 | packet.payload = &logic; | |
658 | sr_session_send(sdi, &packet); | |
659 | std_session_send_df_frame_end(sdi); | |
660 | sdi->driver->dev_acquisition_stop(sdi); | |
661 | ||
662 | if (++devc->num_frames == devc->limit_frames) { | |
663 | /* Last frame, stop capture. */ | |
664 | sdi->driver->dev_acquisition_stop(sdi); | |
665 | } else { | |
666 | /* Get the next frame, starting with the first channel. */ | |
667 | devc->channel_entry = devc->enabled_channels; | |
668 | siglent_sds_capture_start(sdi); | |
669 | ||
670 | /* Start of next frame. */ | |
671 | std_session_send_df_frame_begin(sdi); | |
672 | } | |
673 | } | |
674 | ||
675 | // sr_session_send(sdi, &packet); | |
676 | // std_session_send_df_frame_end(sdi); | |
677 | // sdi->driver->dev_acquisition_stop(sdi); | |
678 | ||
679 | return TRUE; | |
680 | } | |
681 | ||
682 | SR_PRIV int siglent_sds_get_dev_cfg(const struct sr_dev_inst *sdi) | |
683 | { | |
684 | struct dev_context *devc; | |
685 | struct sr_channel *ch; | |
686 | char *cmd, *response; | |
687 | unsigned int i; | |
688 | int res, num_tokens; | |
689 | gchar **tokens; | |
690 | int len; | |
691 | float trigger_pos; | |
692 | ||
693 | devc = sdi->priv; | |
694 | ||
695 | /* Analog channel state. */ | |
696 | for (i = 0; i < devc->model->analog_channels; i++) { | |
697 | cmd = g_strdup_printf("C%i:TRA?", i + 1); | |
698 | res = sr_scpi_get_bool(sdi->conn, cmd, &devc->analog_channels[i]); | |
699 | g_free(cmd); | |
700 | if (res != SR_OK) | |
701 | return SR_ERR; | |
702 | ch = g_slist_nth_data(sdi->channels, i); | |
703 | ch->enabled = devc->analog_channels[i]; | |
704 | } | |
705 | sr_dbg("Current analog channel state:"); | |
706 | for (i = 0; i < devc->model->analog_channels; i++) | |
707 | sr_dbg("CH%d %s", i + 1, devc->analog_channels[i] ? "On" : "Off"); | |
708 | ||
709 | /* Digital channel state. */ | |
710 | if (devc->model->has_digital) { | |
711 | gboolean status; | |
712 | ||
713 | sr_dbg("Check logic analyzer channel state."); | |
714 | devc->la_enabled = FALSE; | |
715 | cmd = g_strdup_printf("DI:SW?"); | |
716 | res = sr_scpi_get_bool(sdi->conn, cmd, &status); | |
717 | g_free(cmd); | |
718 | if (res != SR_OK) | |
719 | return SR_ERR; | |
720 | sr_dbg("Logic analyzer status: %s", status ? "On" : "Off"); | |
721 | if (status) { | |
722 | devc->la_enabled = TRUE; | |
723 | for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) { | |
724 | cmd = g_strdup_printf("D%i:TRA?", i); | |
725 | res = sr_scpi_get_bool(sdi->conn, cmd, &devc->digital_channels[i]); | |
726 | g_free(cmd); | |
727 | if (res != SR_OK) | |
728 | return SR_ERR; | |
729 | ch = g_slist_nth_data(sdi->channels, i + devc->model->analog_channels); | |
730 | ch->enabled = devc->digital_channels[i]; | |
731 | sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "On" : "Off"); | |
732 | } | |
733 | } else { | |
734 | for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) { | |
735 | ch = g_slist_nth_data(sdi->channels, i + devc->model->analog_channels); | |
736 | devc->digital_channels[i] = FALSE; | |
737 | ch->enabled = devc->digital_channels[i]; | |
738 | sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "On" : "Off"); | |
739 | } | |
740 | } | |
741 | } | |
742 | ||
743 | /* Timebase. */ | |
744 | if (sr_scpi_get_float(sdi->conn, ":TDIV?", &devc->timebase) != SR_OK) | |
745 | return SR_ERR; | |
746 | sr_dbg("Current timebase: %g.", devc->timebase); | |
747 | ||
748 | /* Probe attenuation. */ | |
749 | for (i = 0; i < devc->model->analog_channels; i++) { | |
750 | cmd = g_strdup_printf("C%d:ATTN?", i + 1); | |
751 | res = sr_scpi_get_float(sdi->conn, cmd, &devc->attenuation[i]); | |
752 | g_free(cmd); | |
753 | if (res != SR_OK) | |
754 | return SR_ERR; | |
755 | } | |
756 | sr_dbg("Current probe attenuation:"); | |
757 | for (i = 0; i < devc->model->analog_channels; i++) | |
758 | sr_dbg("CH%d %g", i + 1, devc->attenuation[i]); | |
759 | ||
760 | /* Vertical gain and offset. */ | |
761 | if (siglent_sds_get_dev_cfg_vertical(sdi) != SR_OK) | |
762 | return SR_ERR; | |
763 | ||
764 | /* Coupling. */ | |
765 | for (i = 0; i < devc->model->analog_channels; i++) { | |
766 | cmd = g_strdup_printf("C%d:CPL?", i + 1); | |
767 | g_free(devc->coupling[i]); | |
768 | devc->coupling[i] = NULL; | |
769 | res = sr_scpi_get_string(sdi->conn, cmd, &devc->coupling[i]); | |
770 | g_free(cmd); | |
771 | if (res != SR_OK) | |
772 | return SR_ERR; | |
773 | } | |
774 | ||
775 | sr_dbg("Current coupling:"); | |
776 | for (i = 0; i < devc->model->analog_channels; i++) | |
777 | sr_dbg("CH%d %s", i + 1, devc->coupling[i]); | |
778 | ||
779 | /* Trigger source. */ | |
780 | response = NULL; | |
781 | tokens = NULL; | |
782 | if (sr_scpi_get_string(sdi->conn, "TRSE?", &response) != SR_OK) | |
783 | return SR_ERR; | |
784 | tokens = g_strsplit(response, ",", 0); | |
785 | num_tokens = g_strv_length(tokens); | |
786 | if (num_tokens < 4) { | |
787 | sr_dbg("IDN response not according to spec: %80.s.", response); | |
788 | g_strfreev(tokens); | |
789 | g_free(response); | |
790 | return SR_ERR_DATA; | |
791 | } | |
792 | g_free(response); | |
793 | devc->trigger_source = g_strstrip(g_strdup(tokens[2])); | |
794 | sr_dbg("Current trigger source: %s.", devc->trigger_source); | |
795 | ||
796 | /* TODO: Horizontal trigger position. */ | |
797 | response = ""; | |
798 | trigger_pos = 0; | |
799 | // if (sr_scpi_get_string(sdi->conn, g_strdup_printf("%s:TRDL?", devc->trigger_source), &response) != SR_OK) | |
800 | // return SR_ERR; | |
801 | // len = strlen(response); | |
802 | len = strlen(tokens[4]); | |
803 | if (!g_ascii_strcasecmp(tokens[4] + (len - 2), "us")) { | |
804 | trigger_pos = atof(tokens[4]) / SR_GHZ(1); | |
805 | sr_dbg("Current trigger position us %s.", tokens[4] ); | |
806 | } else if (!g_ascii_strcasecmp(tokens[4] + (len - 2), "ns")) { | |
807 | trigger_pos = atof(tokens[4]) / SR_MHZ(1); | |
808 | sr_dbg("Current trigger position ms %s.", tokens[4] ); | |
809 | } else if (!g_ascii_strcasecmp(tokens[4] + (len - 2), "ms")) { | |
810 | trigger_pos = atof(tokens[4]) / SR_KHZ(1); | |
811 | sr_dbg("Current trigger position ns %s.", tokens[4] ); | |
812 | } else if (!g_ascii_strcasecmp(tokens[4] + (len - 2), "s")) { | |
813 | trigger_pos = atof(tokens[4]); | |
814 | sr_dbg("Current trigger position s %s.", tokens[4] ); | |
815 | }; | |
816 | devc->horiz_triggerpos = trigger_pos; | |
817 | ||
818 | sr_dbg("Current horizontal trigger position %.10f.", devc->horiz_triggerpos); | |
819 | ||
820 | /* Trigger slope. */ | |
821 | cmd = g_strdup_printf("%s:TRSL?", devc->trigger_source); | |
822 | g_free(devc->trigger_slope); | |
823 | devc->trigger_slope = NULL; | |
824 | res = sr_scpi_get_string(sdi->conn, cmd, &devc->trigger_slope); | |
825 | g_free(cmd); | |
826 | if (res != SR_OK) | |
827 | return SR_ERR; | |
828 | sr_dbg("Current trigger slope: %s.", devc->trigger_slope); | |
829 | ||
830 | /* Trigger level, only when analog channel. */ | |
831 | if (g_str_has_prefix(tokens[2], "C")) { | |
832 | cmd = g_strdup_printf("%s:TRLV?", devc->trigger_source); | |
833 | res = sr_scpi_get_float(sdi->conn, cmd, &devc->trigger_level); | |
834 | g_free(cmd); | |
835 | if (res != SR_OK) | |
836 | return SR_ERR; | |
837 | sr_dbg("Current trigger level: %g.", devc->trigger_level); | |
838 | } | |
839 | ||
840 | return SR_OK; | |
841 | } | |
842 | ||
843 | SR_PRIV int siglent_sds_get_dev_cfg_vertical(const struct sr_dev_inst *sdi) | |
844 | { | |
845 | struct dev_context *devc; | |
846 | char *cmd; | |
847 | unsigned int i; | |
848 | int res; | |
849 | ||
850 | devc = sdi->priv; | |
851 | ||
852 | /* Vertical gain. */ | |
853 | for (i = 0; i < devc->model->analog_channels; i++) { | |
854 | cmd = g_strdup_printf("C%d:VDIV?", i + 1); | |
855 | res = sr_scpi_get_float(sdi->conn, cmd, &devc->vdiv[i]); | |
856 | g_free(cmd); | |
857 | if (res != SR_OK) | |
858 | return SR_ERR; | |
859 | } | |
860 | sr_dbg("Current vertical gain:"); | |
861 | for (i = 0; i < devc->model->analog_channels; i++) | |
862 | sr_dbg("CH%d %g", i + 1, devc->vdiv[i]); | |
863 | ||
864 | /* Vertical offset. */ | |
865 | for (i = 0; i < devc->model->analog_channels; i++) { | |
866 | cmd = g_strdup_printf("C%d:OFST?", i + 1); | |
867 | res = sr_scpi_get_float(sdi->conn, cmd, &devc->vert_offset[i]); | |
868 | g_free(cmd); | |
869 | if (res != SR_OK) | |
870 | return SR_ERR; | |
871 | } | |
872 | sr_dbg("Current vertical offset:"); | |
873 | for (i = 0; i < devc->model->analog_channels; i++) | |
874 | sr_dbg("CH%d %g", i + 1, devc->vert_offset[i]); | |
875 | ||
876 | return SR_OK; | |
877 | } | |
878 | ||
879 | SR_PRIV int siglent_sds_get_dev_cfg_horizontal(const struct sr_dev_inst *sdi) | |
880 | { | |
881 | struct dev_context *devc; | |
882 | char *cmd; | |
883 | int res; | |
884 | char *sample_points_string; | |
885 | float samplerate_scope, fvalue; | |
886 | ||
887 | devc = sdi->priv; | |
888 | ||
889 | switch (devc->model->series->protocol) { | |
890 | case SPO_MODEL: | |
891 | case NON_SPO_MODEL: | |
892 | cmd = g_strdup_printf("SANU? C1"); | |
893 | res = sr_scpi_get_string(sdi->conn, cmd, &sample_points_string); | |
894 | g_free(cmd); | |
895 | samplerate_scope = 0; | |
896 | fvalue = 0; | |
897 | if (res != SR_OK) { | |
898 | g_free(sample_points_string); | |
899 | return SR_ERR; | |
900 | } | |
901 | if (g_strstr_len(sample_points_string, -1, "Mpts") != NULL) { | |
902 | sample_points_string[strlen(sample_points_string) - 4] = '\0'; | |
903 | if (sr_atof_ascii(sample_points_string, &fvalue) != SR_OK) { | |
904 | sr_dbg("Invalid float converted from scope response."); | |
905 | g_free(sample_points_string); | |
906 | return SR_ERR; | |
907 | } | |
908 | samplerate_scope = fvalue * 1000000; | |
909 | } else if (g_strstr_len(sample_points_string, -1, "Kpts") != NULL) { | |
910 | sample_points_string[strlen(sample_points_string) - 4] = '\0'; | |
911 | if (sr_atof_ascii(sample_points_string, &fvalue) != SR_OK) { | |
912 | sr_dbg("Invalid float converted from scope response."); | |
913 | g_free(sample_points_string); | |
914 | return SR_ERR; | |
915 | } | |
916 | samplerate_scope = fvalue * 10000; | |
917 | } else { | |
918 | samplerate_scope = fvalue; | |
919 | } | |
920 | g_free(sample_points_string); | |
921 | devc->memory_depth_analog = samplerate_scope; | |
922 | break; | |
923 | case ESERIES: | |
924 | cmd = g_strdup_printf("SANU? C1"); | |
925 | if (sr_scpi_get_float(sdi->conn, cmd, &fvalue) != SR_OK) | |
926 | return SR_ERR; | |
927 | devc->memory_depth_analog = (long)fvalue; | |
928 | if (devc->la_enabled) { | |
929 | cmd = g_strdup_printf("SANU? D0"); | |
930 | if (sr_scpi_get_float(sdi->conn, cmd, &fvalue) != SR_OK) | |
931 | return SR_ERR; | |
932 | devc->memory_depth_digital = (long)fvalue; | |
933 | } | |
934 | g_free(cmd); | |
935 | break; | |
936 | }; | |
937 | ||
938 | /* Get the timebase. */ | |
939 | if (sr_scpi_get_float(sdi->conn, ":TDIV?", &devc->timebase) != SR_OK) | |
940 | return SR_ERR; | |
941 | ||
942 | sr_dbg("Current timebase: %g.", devc->timebase); | |
943 | devc->samplerate = devc->memory_depth_analog / (devc->timebase * devc->model->series->num_horizontal_divs); | |
944 | sr_dbg("Current samplerate: %0f.", devc->samplerate); | |
945 | sr_dbg("Current memory depth: %" PRIu64 ".", devc->memory_depth_analog); | |
946 | ||
947 | return SR_OK; | |
948 | } |