]> sigrok.org Git - libsigrok.git/blame - src/hardware/lecroy-xstream/protocol.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / lecroy-xstream / protocol.c
CommitLineData
e3b83c5e
SS
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2017 Sven Schnelle <svens@stackframe.org>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <config.h>
3f2c7c94
SS
21#include <math.h>
22#include <stdlib.h>
23#include "scpi.h"
e3b83c5e
SS
24#include "protocol.h"
25
3f2c7c94
SS
26struct lecroy_wavedesc_2_x {
27 uint16_t comm_type;
28 uint16_t comm_order; /* 1 - little endian */
29 uint32_t wave_descriptor_length;
30 uint32_t user_text_len;
31 uint32_t res_desc1;
32 uint32_t trigtime_array_length;
33 uint32_t ris_time1_array_length;
34 uint32_t res_array1;
35 uint32_t wave_array1_length;
36 uint32_t wave_array2_length;
37 uint32_t wave_array3_length;
38 uint32_t wave_array4_length;
39 char instrument_name[16];
40 uint32_t instrument_number;
41 char trace_label[16];
42 uint32_t reserved;
43 uint32_t wave_array_count;
44 uint32_t points_per_screen;
45 uint32_t first_valid_point;
46 uint32_t last_valid_point;
47 uint32_t first_point;
48 uint32_t sparsing_factor;
49 uint32_t segment_index;
50 uint32_t subarray_count;
51 uint32_t sweeps_per_acq;
52 uint16_t points_per_pair;
53 uint16_t pair_offset;
54 float vertical_gain;
55 float vertical_offset;
56 float max_value;
57 float min_value;
58 uint16_t nominal_bits;
59 uint16_t nom_subarray_count;
60 float horiz_interval;
61 double horiz_offset;
62 double pixel_offset;
63 char vertunit[48];
64 char horunit[48];
65 uint32_t reserved1;
66 double trigger_time;
67} __attribute__((packed));
68
69struct lecroy_wavedesc {
70 char descriptor_name[16];
71 char template_name[16];
72 union {
73 struct lecroy_wavedesc_2_x version_2_x;
74 };
75} __attribute__((packed));
76
6d13a46c 77static const char *coupling_options[] = {
952c7376
SA
78 "A1M", ///< AC with 1 MOhm termination
79 "D50", ///< DC with 50 Ohm termination
80 "D1M", ///< DC with 1 MOhm termination
3f2c7c94
SS
81 "GND",
82 "OVL",
3f2c7c94
SS
83};
84
85static const char *scope_trigger_slopes[] = {
692716f5 86 "POS", "NEG",
3f2c7c94
SS
87};
88
6d13a46c 89static const char *trigger_sources[] = {
692716f5 90 "C1", "C2", "C3", "C4", "LINE", "EXT",
3f2c7c94
SS
91};
92
76f0fa5d 93static const uint64_t timebases[][2] = {
3f2c7c94 94 /* picoseconds */
3f4c1174
UH
95 { 20, UINT64_C(1000000000000) },
96 { 50, UINT64_C(1000000000000) },
97 { 100, UINT64_C(1000000000000) },
98 { 200, UINT64_C(1000000000000) },
99 { 500, UINT64_C(1000000000000) },
3f2c7c94
SS
100 /* nanoseconds */
101 { 1, 1000000000 },
102 { 2, 1000000000 },
103 { 5, 1000000000 },
104 { 10, 1000000000 },
105 { 20, 1000000000 },
106 { 50, 1000000000 },
107 { 100, 1000000000 },
108 { 200, 1000000000 },
109 { 500, 1000000000 },
110 /* microseconds */
111 { 1, 1000000 },
112 { 2, 1000000 },
113 { 5, 1000000 },
114 { 10, 1000000 },
115 { 20, 1000000 },
116 { 50, 1000000 },
117 { 100, 1000000 },
118 { 200, 1000000 },
119 { 500, 1000000 },
120 /* milliseconds */
121 { 1, 1000 },
122 { 2, 1000 },
123 { 5, 1000 },
124 { 10, 1000 },
125 { 20, 1000 },
126 { 50, 1000 },
127 { 100, 1000 },
128 { 200, 1000 },
129 { 500, 1000 },
130 /* seconds */
131 { 1, 1 },
132 { 2, 1 },
133 { 5, 1 },
134 { 10, 1 },
135 { 20, 1 },
136 { 50, 1 },
137 { 100, 1 },
138 { 200, 1 },
139 { 500, 1 },
140 { 1000, 1 },
141};
142
76f0fa5d 143static const uint64_t vdivs[][2] = {
3f2c7c94
SS
144 /* millivolts */
145 { 1, 1000 },
146 { 2, 1000 },
147 { 5, 1000 },
148 { 10, 1000 },
149 { 20, 1000 },
150 { 50, 1000 },
151 { 100, 1000 },
152 { 200, 1000 },
153 { 500, 1000 },
154 /* volts */
155 { 1, 1 },
156 { 2, 1 },
157 { 5, 1 },
158 { 10, 1 },
159 { 20, 1 },
160 { 50, 1 },
161};
162
163static const char *scope_analog_channel_names[] = {
f8195cb2 164 "CH1", "CH2", "CH3", "CH4",
3f2c7c94
SS
165};
166
167static const struct scope_config scope_models[] = {
168 {
e7d2cd1e
SA
169 /* Default config */
170 .name = {NULL},
3f2c7c94
SS
171
172 .analog_channels = 4,
173 .analog_names = &scope_analog_channel_names,
174
6d13a46c 175 .coupling_options = &coupling_options,
692716f5
UH
176 .num_coupling_options = ARRAY_SIZE(coupling_options),
177
6d13a46c 178 .trigger_sources = &trigger_sources,
692716f5
UH
179 .num_trigger_sources = ARRAY_SIZE(trigger_sources),
180
3f2c7c94 181 .trigger_slopes = &scope_trigger_slopes,
692716f5 182 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
3f2c7c94 183
76f0fa5d 184 .timebases = &timebases,
6d13a46c 185 .num_timebases = ARRAY_SIZE(timebases),
3f2c7c94 186
76f0fa5d 187 .vdivs = &vdivs,
6d13a46c 188 .num_vdivs = ARRAY_SIZE(vdivs),
3f2c7c94
SS
189
190 .num_xdivs = 10,
191 .num_ydivs = 8,
192 },
193};
194
195static void scope_state_dump(const struct scope_config *config,
952c7376 196 struct scope_state *state)
3f2c7c94
SS
197{
198 unsigned int i;
199 char *tmp;
200
201 for (i = 0; i < config->analog_channels; i++) {
76f0fa5d 202 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
952c7376 203 (*config->vdivs)[state->analog_channels[i].vdiv][1]);
3f2c7c94 204 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
952c7376
SA
205 i + 1, state->analog_channels[i].state ? "On" : "Off",
206 (*config->coupling_options)[state->analog_channels[i].coupling],
207 tmp, state->analog_channels[i].vertical_offset);
3f2c7c94
SS
208 }
209
76f0fa5d 210 tmp = sr_period_string((*config->timebases)[state->timebase][0],
952c7376 211 (*config->timebases)[state->timebase][1]);
3f2c7c94
SS
212 sr_info("Current timebase: %s", tmp);
213 g_free(tmp);
214
215 tmp = sr_samplerate_string(state->sample_rate);
216 sr_info("Current samplerate: %s", tmp);
217 g_free(tmp);
218
219 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
952c7376
SA
220 (*config->trigger_sources)[state->trigger_source],
221 (*config->trigger_slopes)[state->trigger_slope],
222 state->horiz_triggerpos);
3f2c7c94
SS
223}
224
225static int scope_state_get_array_option(const char *resp,
952c7376 226 const char *(*array)[], unsigned int n, int *result)
3f2c7c94
SS
227{
228 unsigned int i;
229
692716f5 230 for (i = 0; i < n; i++) {
3f2c7c94
SS
231 if (!g_strcmp0(resp, (*array)[i])) {
232 *result = i;
233 return SR_OK;
234 }
235 }
236
237 return SR_ERR;
238}
239
240/**
241 * This function takes a value of the form "2.000E-03" and returns the index
242 * of an array where a matching pair was found.
243 *
244 * @param value The string to be parsed.
245 * @param array The array of s/f pairs.
246 * @param array_len The number of pairs in the array.
247 * @param result The index at which a matching pair was found.
248 *
249 * @return SR_ERR on any parsing error, SR_OK otherwise.
250 */
76f0fa5d 251static int array_float_get(gchar *value, const uint64_t array[][2],
3f2c7c94
SS
252 int array_len, unsigned int *result)
253{
254 struct sr_rational rval;
76f0fa5d 255 struct sr_rational aval;
3f2c7c94
SS
256
257 if (sr_parse_rational(value, &rval) != SR_OK)
258 return SR_ERR;
259
260 for (int i = 0; i < array_len; i++) {
76f0fa5d
UH
261 sr_rational_set(&aval, array[i][0], array[i][1]);
262 if (sr_rational_eq(&rval, &aval)) {
3f2c7c94
SS
263 *result = i;
264 return SR_OK;
265 }
266 }
267
268 return SR_ERR;
269}
270
271static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
952c7376 272 const struct scope_config *config, struct scope_state *state)
3f2c7c94
SS
273{
274 unsigned int i, j;
275 char command[MAX_COMMAND_SIZE];
276 char *tmp_str;
277
278 for (i = 0; i < config->analog_channels; i++) {
ea257cdc 279 g_snprintf(command, sizeof(command), "C%d:TRACE?", i + 1);
3f2c7c94
SS
280
281 if (sr_scpi_get_bool(scpi, command,
ea257cdc 282 &state->analog_channels[i].state) != SR_OK)
3f2c7c94
SS
283 return SR_ERR;
284
ea257cdc 285 g_snprintf(command, sizeof(command), "C%d:VDIV?", i + 1);
3f2c7c94
SS
286
287 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
288 return SR_ERR;
289
952c7376 290 if (array_float_get(tmp_str, ARRAY_AND_SIZE(vdivs), &j) != SR_OK) {
3f2c7c94
SS
291 g_free(tmp_str);
292 sr_err("Could not determine array index for vertical div scale.");
293 return SR_ERR;
294 }
295
296 g_free(tmp_str);
297 state->analog_channels[i].vdiv = j;
298
ea257cdc 299 g_snprintf(command, sizeof(command), "C%d:OFFSET?", i + 1);
3f2c7c94
SS
300
301 if (sr_scpi_get_float(scpi, command, &state->analog_channels[i].vertical_offset) != SR_OK)
302 return SR_ERR;
303
ea257cdc 304 g_snprintf(command, sizeof(command), "C%d:COUPLING?", i + 1);
3f2c7c94
SS
305
306 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
307 return SR_ERR;
308
309
310 if (scope_state_get_array_option(tmp_str, config->coupling_options,
692716f5 311 config->num_coupling_options,
ea257cdc 312 &state->analog_channels[i].coupling) != SR_OK)
3f2c7c94
SS
313 return SR_ERR;
314
315 g_free(tmp_str);
316 }
317
318 return SR_OK;
319}
320
79100d4e
SA
321SR_PRIV int lecroy_xstream_channel_state_set(const struct sr_dev_inst *sdi,
322 const int ch_index, gboolean ch_state)
323{
324 GSList *l;
325 struct sr_channel *ch;
326 struct dev_context *devc = NULL;
327 struct scope_state *state;
328 char command[MAX_COMMAND_SIZE];
329 gboolean chan_found;
330 int result;
331
332 result = SR_OK;
333
334 devc = sdi->priv;
335 state = devc->model_state;
336 chan_found = FALSE;
337
338 for (l = sdi->channels; l; l = l->next) {
339 ch = l->data;
340
341 switch (ch->type) {
342 case SR_CHANNEL_ANALOG:
343 if (ch->index == ch_index) {
344 g_snprintf(command, sizeof(command), "C%d:TRACE %s", ch_index + 1,
345 (ch_state ? "ON" : "OFF"));
346 if ((sr_scpi_send(sdi->conn, command) != SR_OK ||
347 sr_scpi_get_opc(sdi->conn) != SR_OK)) {
348 result = SR_ERR;
349 break;
350 }
351
352 ch->enabled = ch_state;
353 state->analog_channels[ch->index].state = ch_state;
354 chan_found = TRUE;
355 break;
356 }
357 break;
358 default:
359 result = SR_ERR_NA;
360 }
361 }
362
363 if ((result == SR_OK) && !chan_found)
364 result = SR_ERR_BUG;
365
366 return result;
367}
368
e0b6855b
SA
369SR_PRIV int lecroy_xstream_update_sample_rate(const struct sr_dev_inst *sdi,
370 int num_of_samples)
e3b83c5e 371{
e3b83c5e 372 struct dev_context *devc;
3f2c7c94
SS
373 struct scope_state *state;
374 const struct scope_config *config;
e0b6855b 375 double time_div;
3f2c7c94
SS
376
377 devc = sdi->priv;
3f2c7c94 378 config = devc->model_config;
e0b6855b 379 state = devc->model_state;
3f2c7c94 380
e0b6855b 381 if (sr_scpi_get_double(sdi->conn, "TIME_DIV?", &time_div) != SR_OK)
3f2c7c94
SS
382 return SR_ERR;
383
e0b6855b 384 state->sample_rate = num_of_samples / (time_div * config->num_xdivs);
3f2c7c94
SS
385
386 return SR_OK;
387}
388
389SR_PRIV int lecroy_xstream_state_get(struct sr_dev_inst *sdi)
390{
391 struct dev_context *devc;
ea257cdc
UH
392 struct scope_state *state;
393 const struct scope_config *config;
3f2c7c94
SS
394 unsigned int i;
395 char *tmp_str, *tmp_str2, *tmpp, *p, *key;
396 char command[MAX_COMMAND_SIZE];
397 char *trig_source = NULL;
398
399 devc = sdi->priv;
400 config = devc->model_config;
401 state = devc->model_state;
402
403 sr_info("Fetching scope state");
404
405 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
406 return SR_ERR;
407
408 if (sr_scpi_get_string(sdi->conn, "TIME_DIV?", &tmp_str) != SR_OK)
409 return SR_ERR;
410
53012da6 411 if (array_float_get(tmp_str, ARRAY_AND_SIZE(timebases), &i) != SR_OK) {
3f2c7c94
SS
412 g_free(tmp_str);
413 sr_err("Could not determine array index for timbase scale.");
414 return SR_ERR;
415 }
416 g_free(tmp_str);
417 state->timebase = i;
418
419 if (sr_scpi_get_string(sdi->conn, "TRIG_SELECT?", &tmp_str) != SR_OK)
420 return SR_ERR;
421
7002e64a 422 key = tmpp = NULL;
3f2c7c94
SS
423 tmp_str2 = tmp_str;
424 i = 0;
ea257cdc 425 while ((p = strtok_r(tmp_str2, ",", &tmpp))) {
3f2c7c94
SS
426 tmp_str2 = NULL;
427 if (i == 0) {
428 /* trigger type */
429 } else if (i & 1) {
430 key = p;
431 /* key */
432 } else if (!(i & 1)) {
433 if (!strcmp(key, "SR"))
434 trig_source = p;
435 }
436 i++;
437 }
36165cf0 438 g_free(tmp_str);
3f2c7c94 439
952c7376
SA
440 if (!trig_source || scope_state_get_array_option(trig_source,
441 config->trigger_sources, config->num_trigger_sources,
442 &state->trigger_source) != SR_OK)
3f2c7c94
SS
443 return SR_ERR;
444
3f2c7c94
SS
445 g_snprintf(command, sizeof(command), "%s:TRIG_SLOPE?", trig_source);
446 if (sr_scpi_get_string(sdi->conn, command, &tmp_str) != SR_OK)
447 return SR_ERR;
448
952c7376
SA
449 if (scope_state_get_array_option(tmp_str, config->trigger_slopes,
450 config->num_trigger_slopes, &state->trigger_slope) != SR_OK)
3f2c7c94 451 return SR_ERR;
36165cf0 452 g_free(tmp_str);
3f2c7c94 453
952c7376 454 if (sr_scpi_get_float(sdi->conn, "TRIG_DELAY?", &state->horiz_triggerpos) != SR_OK)
3f2c7c94
SS
455 return SR_ERR;
456
3f2c7c94
SS
457 sr_info("Fetching finished.");
458
459 scope_state_dump(config, state);
460
461 return SR_OK;
462}
463
464static struct scope_state *scope_state_new(const struct scope_config *config)
465{
466 struct scope_state *state;
467
468 state = g_malloc0(sizeof(struct scope_state));
469 state->analog_channels = g_malloc0_n(config->analog_channels,
470 sizeof(struct analog_channel_state));
471 return state;
472}
473
474SR_PRIV void lecroy_xstream_state_free(struct scope_state *state)
475{
476 g_free(state->analog_channels);
477 g_free(state);
478}
479
480SR_PRIV int lecroy_xstream_init_device(struct sr_dev_inst *sdi)
481{
482 char command[MAX_COMMAND_SIZE];
483 int model_index;
484 unsigned int i, j;
485 struct sr_channel *ch;
486 struct dev_context *devc;
487 gboolean channel_enabled;
488
489 devc = sdi->priv;
490 model_index = -1;
491
492 /* Find the exact model. */
493 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
494 for (j = 0; scope_models[i].name[j]; j++) {
495 if (!strcmp(sdi->model, scope_models[i].name[j])) {
496 model_index = i;
497 break;
498 }
499 }
500 if (model_index != -1)
501 break;
502 }
503
504 if (model_index == -1) {
e7d2cd1e
SA
505 sr_dbg("Unknown LeCroy device, using default config.");
506 for (i = 0; i < ARRAY_SIZE(scope_models); i++)
507 if (scope_models[i].name[0] == NULL)
508 model_index = i;
3f2c7c94
SS
509 }
510
6158728c
SP
511 /* Set the desired response and format modes. */
512 sr_scpi_send(sdi->conn, "COMM_HEADER OFF");
952c7376 513 sr_scpi_send(sdi->conn, "COMM_FORMAT DEF9,WORD,BIN");
6158728c 514
3f2c7c94 515 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
ea257cdc 516 scope_models[model_index].analog_channels);
3f2c7c94
SS
517
518 /* Add analog channels. */
519 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
ea257cdc 520 g_snprintf(command, sizeof(command), "C%d:TRACE?", i + 1);
3f2c7c94
SS
521
522 if (sr_scpi_get_bool(sdi->conn, command, &channel_enabled) != SR_OK)
523 return SR_ERR;
524
ea257cdc 525 g_snprintf(command, sizeof(command), "C%d:VDIV?", i + 1);
3f2c7c94
SS
526
527 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, channel_enabled,
952c7376 528 (*scope_models[model_index].analog_names)[i]);
3f2c7c94 529
d810901a
GS
530 devc->analog_groups[i] = sr_channel_group_new(sdi,
531 (*scope_models[model_index].analog_names)[i], NULL);
3f2c7c94 532 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
3f2c7c94
SS
533 }
534
535 devc->model_config = &scope_models[model_index];
536 devc->frame_limit = 0;
952c7376 537 devc->model_state = scope_state_new(devc->model_config);
3f2c7c94
SS
538
539 return SR_OK;
540}
541
542static int lecroy_waveform_2_x_to_analog(GByteArray *data,
952c7376 543 struct lecroy_wavedesc *desc, struct sr_datafeed_analog *analog)
3f2c7c94
SS
544{
545 struct sr_analog_encoding *encoding = analog->encoding;
546 struct sr_analog_meaning *meaning = analog->meaning;
547 struct sr_analog_spec *spec = analog->spec;
548 float *data_float;
549 int16_t *waveform_data;
550 unsigned int i, num_samples;
551
ea257cdc 552 data_float = g_malloc(desc->version_2_x.wave_array_count * sizeof(float));
3f2c7c94
SS
553 num_samples = desc->version_2_x.wave_array_count;
554
952c7376
SA
555 waveform_data = (int16_t*)(data->data +
556 + desc->version_2_x.wave_descriptor_length
557 + desc->version_2_x.user_text_len);
3f2c7c94 558
ea257cdc 559 for (i = 0; i < num_samples; i++)
3f2c7c94
SS
560 data_float[i] = (float)waveform_data[i]
561 * desc->version_2_x.vertical_gain
562 + desc->version_2_x.vertical_offset;
563
3f2c7c94
SS
564 analog->data = data_float;
565 analog->num_samples = num_samples;
566
567 encoding->unitsize = sizeof(float);
568 encoding->is_signed = TRUE;
569 encoding->is_float = TRUE;
570 encoding->is_bigendian = FALSE;
571 encoding->scale.p = 1;
572 encoding->scale.q = 1;
573 encoding->offset.p = 0;
574 encoding->offset.q = 1;
575
576 encoding->digits = 6;
577 encoding->is_digits_decimal = FALSE;
578
579 if (strcmp(desc->version_2_x.vertunit, "A")) {
580 meaning->mq = SR_MQ_CURRENT;
581 meaning->unit = SR_UNIT_AMPERE;
582 } else {
ea257cdc 583 /* Default to voltage. */
3f2c7c94
SS
584 meaning->mq = SR_MQ_VOLTAGE;
585 meaning->unit = SR_UNIT_VOLT;
586 }
587
588 meaning->mqflags = 0;
589 spec->spec_digits = 3;
ea257cdc 590
3f2c7c94
SS
591 return SR_OK;
592}
593
594static int lecroy_waveform_to_analog(GByteArray *data,
952c7376 595 struct sr_datafeed_analog *analog)
3f2c7c94
SS
596{
597 struct lecroy_wavedesc *desc;
598
599 if (data->len < sizeof(struct lecroy_wavedesc))
600 return SR_ERR;
601
952c7376 602 desc = (struct lecroy_wavedesc*)data->data;
3f2c7c94
SS
603
604 if (!strncmp(desc->template_name, "LECROY_2_2", 16) ||
605 !strncmp(desc->template_name, "LECROY_2_3", 16)) {
606 return lecroy_waveform_2_x_to_analog(data, desc, analog);
607 }
608
952c7376 609 sr_err("Waveformat template '%.16s' not supported.", desc->template_name);
3f2c7c94
SS
610 return SR_ERR;
611}
612
613SR_PRIV int lecroy_xstream_receive_data(int fd, int revents, void *cb_data)
614{
86f76e6c 615 char command[MAX_COMMAND_SIZE];
3f2c7c94
SS
616 struct sr_channel *ch;
617 struct sr_dev_inst *sdi;
618 struct dev_context *devc;
e0b6855b 619 struct scope_state *state;
3f2c7c94
SS
620 struct sr_datafeed_packet packet;
621 GByteArray *data;
622 struct sr_datafeed_analog analog;
623 struct sr_analog_encoding encoding;
624 struct sr_analog_meaning meaning;
625 struct sr_analog_spec spec;
ea257cdc 626
e3b83c5e 627 (void)fd;
3f2c7c94
SS
628 (void)revents;
629
e3b83c5e
SS
630 if (!(sdi = cb_data))
631 return TRUE;
632
633 if (!(devc = sdi->priv))
634 return TRUE;
635
3f2c7c94 636 ch = devc->current_channel->data;
e0b6855b 637 state = devc->model_state;
3f2c7c94 638
3f2c7c94
SS
639 if (ch->type != SR_CHANNEL_ANALOG)
640 return SR_ERR;
ea257cdc 641
36165cf0 642 data = NULL;
3f2c7c94
SS
643 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
644 if (data)
645 g_byte_array_free(data, TRUE);
646 return TRUE;
647 }
648
649 analog.encoding = &encoding;
650 analog.meaning = &meaning;
651 analog.spec = &spec;
652
653 if (lecroy_waveform_to_analog(data, &analog) != SR_OK)
654 return SR_ERR;
655
86f76e6c 656 if (analog.num_samples == 0) {
f62c2ad6 657 g_free(analog.data);
36165cf0 658 g_byte_array_free(data, TRUE);
f62c2ad6 659
86f76e6c
SA
660 /* No data available, we have to acquire data first. */
661 g_snprintf(command, sizeof(command), "ARM;WAIT;*OPC;C%d:WAVEFORM?", ch->index + 1);
662 sr_scpi_send(sdi->conn, command);
e0b6855b
SA
663
664 state->sample_rate = 0;
86f76e6c 665 return TRUE;
e0b6855b
SA
666 } else {
667 /* Update sample rate if needed. */
668 if (state->sample_rate == 0)
d5db6ea7
UH
669 if (lecroy_xstream_update_sample_rate(sdi, analog.num_samples) != SR_OK) {
670 g_free(analog.data);
36165cf0 671 g_byte_array_free(data, TRUE);
e0b6855b 672 return SR_ERR;
d5db6ea7 673 }
86f76e6c
SA
674 }
675
676 /*
677 * Send "frame begin" packet upon reception of data for the
678 * first enabled channel.
679 */
4c5f7006
UH
680 if (devc->current_channel == devc->enabled_channels)
681 std_session_send_df_frame_begin(sdi);
86f76e6c 682
3f2c7c94
SS
683 meaning.channels = g_slist_append(NULL, ch);
684 packet.payload = &analog;
685 packet.type = SR_DF_ANALOG;
686 sr_session_send(sdi, &packet);
687
688 g_byte_array_free(data, TRUE);
689 data = NULL;
690
691 g_slist_free(meaning.channels);
692 g_free(analog.data);
693
3f2c7c94
SS
694 /*
695 * Advance to the next enabled channel. When data for all enabled
696 * channels was received, then flush potentially queued logic data,
697 * and send the "frame end" packet.
698 */
699 if (devc->current_channel->next) {
700 devc->current_channel = devc->current_channel->next;
701 lecroy_xstream_request_data(sdi);
702 return TRUE;
703 }
704
4c5f7006 705 std_session_send_df_frame_end(sdi);
3f2c7c94
SS
706
707 /*
708 * End of frame was reached. Stop acquisition after the specified
709 * number of frames, or continue reception by starting over at
710 * the first enabled channel.
711 */
24985129
SA
712 devc->num_frames++;
713 if (devc->frame_limit && (devc->num_frames == devc->frame_limit)) {
d2f7c417 714 sr_dev_acquisition_stop(sdi);
3f2c7c94
SS
715 } else {
716 devc->current_channel = devc->enabled_channels;
d8fb599c
SA
717
718 /* Wait for trigger, then begin fetching data. */
719 g_snprintf(command, sizeof(command), "ARM;WAIT;*OPC");
720 sr_scpi_send(sdi->conn, command);
721
3f2c7c94 722 lecroy_xstream_request_data(sdi);
e3b83c5e
SS
723 }
724
725 return TRUE;
726}