]> sigrok.org Git - libsigrok.git/blame - src/hardware/lecroy-xstream/protocol.c
sr_dev_close(): Factor out SR_ERR_DEV_CLOSED check.
[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[] = {
3f2c7c94 78 "A1M", // AC with 1 MOhm termination
ea257cdc 79 "D50", // DC with 50 Ohm termination
3f2c7c94
SS
80 "D1M", // DC with 1 MOhm termination
81 "GND",
82 "OVL",
83 NULL,
84};
85
86static const char *scope_trigger_slopes[] = {
87 "POS",
88 "NEG",
89 NULL,
90};
91
6d13a46c 92static const char *trigger_sources[] = {
3f2c7c94
SS
93 "C1",
94 "C2",
95 "C3",
96 "C4",
97 "LINE",
98 "EXT",
99 NULL,
100};
101
6d13a46c 102static const struct sr_rational timebases[] = {
3f2c7c94
SS
103 /* picoseconds */
104 { 20, 1000000000000 },
105 { 50, 1000000000000 },
106 { 100, 1000000000000 },
107 { 200, 1000000000000 },
108 { 500, 1000000000000 },
109 /* nanoseconds */
110 { 1, 1000000000 },
111 { 2, 1000000000 },
112 { 5, 1000000000 },
113 { 10, 1000000000 },
114 { 20, 1000000000 },
115 { 50, 1000000000 },
116 { 100, 1000000000 },
117 { 200, 1000000000 },
118 { 500, 1000000000 },
119 /* microseconds */
120 { 1, 1000000 },
121 { 2, 1000000 },
122 { 5, 1000000 },
123 { 10, 1000000 },
124 { 20, 1000000 },
125 { 50, 1000000 },
126 { 100, 1000000 },
127 { 200, 1000000 },
128 { 500, 1000000 },
129 /* milliseconds */
130 { 1, 1000 },
131 { 2, 1000 },
132 { 5, 1000 },
133 { 10, 1000 },
134 { 20, 1000 },
135 { 50, 1000 },
136 { 100, 1000 },
137 { 200, 1000 },
138 { 500, 1000 },
139 /* seconds */
140 { 1, 1 },
141 { 2, 1 },
142 { 5, 1 },
143 { 10, 1 },
144 { 20, 1 },
145 { 50, 1 },
146 { 100, 1 },
147 { 200, 1 },
148 { 500, 1 },
149 { 1000, 1 },
150};
151
6d13a46c 152static const struct sr_rational vdivs[] = {
3f2c7c94
SS
153 /* millivolts */
154 { 1, 1000 },
155 { 2, 1000 },
156 { 5, 1000 },
157 { 10, 1000 },
158 { 20, 1000 },
159 { 50, 1000 },
160 { 100, 1000 },
161 { 200, 1000 },
162 { 500, 1000 },
163 /* volts */
164 { 1, 1 },
165 { 2, 1 },
166 { 5, 1 },
167 { 10, 1 },
168 { 20, 1 },
169 { 50, 1 },
170};
171
172static const char *scope_analog_channel_names[] = {
173 "CH1",
174 "CH2",
175 "CH3",
176 "CH4",
177};
178
179static const struct scope_config scope_models[] = {
180 {
181 .name = { "WP7000", "WP7100", "WP7200", "WP7300" },
182
183 .analog_channels = 4,
184 .analog_names = &scope_analog_channel_names,
185
6d13a46c
UH
186 .coupling_options = &coupling_options,
187 .trigger_sources = &trigger_sources,
3f2c7c94
SS
188 .trigger_slopes = &scope_trigger_slopes,
189
6d13a46c
UH
190 .timebases = timebases,
191 .num_timebases = ARRAY_SIZE(timebases),
3f2c7c94 192
6d13a46c
UH
193 .vdivs = vdivs,
194 .num_vdivs = ARRAY_SIZE(vdivs),
3f2c7c94
SS
195
196 .num_xdivs = 10,
197 .num_ydivs = 8,
198 },
199};
200
201static void scope_state_dump(const struct scope_config *config,
202 struct scope_state *state)
203{
204 unsigned int i;
205 char *tmp;
206
207 for (i = 0; i < config->analog_channels; i++) {
208 tmp = sr_voltage_string(config->vdivs[state->analog_channels[i].vdiv].p,
209 config->vdivs[state->analog_channels[i].vdiv].q);
210 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
211 i + 1, state->analog_channels[i].state ? "On" : "Off",
212 (*config->coupling_options)[state->analog_channels[i].coupling],
213 tmp, state->analog_channels[i].vertical_offset);
214 }
215
6984cfb2
SA
216 tmp = sr_period_string(config->timebases[state->timebase].p,
217 config->timebases[state->timebase].q);
3f2c7c94
SS
218 sr_info("Current timebase: %s", tmp);
219 g_free(tmp);
220
221 tmp = sr_samplerate_string(state->sample_rate);
222 sr_info("Current samplerate: %s", tmp);
223 g_free(tmp);
224
225 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
226 (*config->trigger_sources)[state->trigger_source],
227 (*config->trigger_slopes)[state->trigger_slope],
228 state->horiz_triggerpos);
229}
230
231static int scope_state_get_array_option(const char *resp,
ea257cdc 232 const char *(*array)[], int *result)
3f2c7c94
SS
233{
234 unsigned int i;
235
236 for (i = 0; (*array)[i]; i++) {
237 if (!g_strcmp0(resp, (*array)[i])) {
238 *result = i;
239 return SR_OK;
240 }
241 }
242
243 return SR_ERR;
244}
245
246/**
247 * This function takes a value of the form "2.000E-03" and returns the index
248 * of an array where a matching pair was found.
249 *
250 * @param value The string to be parsed.
251 * @param array The array of s/f pairs.
252 * @param array_len The number of pairs in the array.
253 * @param result The index at which a matching pair was found.
254 *
255 * @return SR_ERR on any parsing error, SR_OK otherwise.
256 */
257static int array_float_get(gchar *value, const struct sr_rational *aval,
258 int array_len, unsigned int *result)
259{
260 struct sr_rational rval;
261
262 if (sr_parse_rational(value, &rval) != SR_OK)
263 return SR_ERR;
264
265 for (int i = 0; i < array_len; i++) {
ea257cdc 266 if (sr_rational_eq(&rval, aval + i)) {
3f2c7c94
SS
267 *result = i;
268 return SR_OK;
269 }
270 }
271
272 return SR_ERR;
273}
274
275static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
276 const struct scope_config *config,
277 struct scope_state *state)
278{
279 unsigned int i, j;
280 char command[MAX_COMMAND_SIZE];
281 char *tmp_str;
282
283 for (i = 0; i < config->analog_channels; i++) {
ea257cdc 284 g_snprintf(command, sizeof(command), "C%d:TRACE?", i + 1);
3f2c7c94
SS
285
286 if (sr_scpi_get_bool(scpi, command,
ea257cdc 287 &state->analog_channels[i].state) != SR_OK)
3f2c7c94
SS
288 return SR_ERR;
289
ea257cdc 290 g_snprintf(command, sizeof(command), "C%d:VDIV?", i + 1);
3f2c7c94
SS
291
292 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
293 return SR_ERR;
294
6d13a46c 295 if (array_float_get(tmp_str, vdivs, ARRAY_SIZE(vdivs), &j) != SR_OK) {
3f2c7c94
SS
296 g_free(tmp_str);
297 sr_err("Could not determine array index for vertical div scale.");
298 return SR_ERR;
299 }
300
301 g_free(tmp_str);
302 state->analog_channels[i].vdiv = j;
303
ea257cdc 304 g_snprintf(command, sizeof(command), "C%d:OFFSET?", i + 1);
3f2c7c94
SS
305
306 if (sr_scpi_get_float(scpi, command, &state->analog_channels[i].vertical_offset) != SR_OK)
307 return SR_ERR;
308
ea257cdc 309 g_snprintf(command, sizeof(command), "C%d:COUPLING?", i + 1);
3f2c7c94
SS
310
311 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
312 return SR_ERR;
313
314
315 if (scope_state_get_array_option(tmp_str, config->coupling_options,
ea257cdc 316 &state->analog_channels[i].coupling) != SR_OK)
3f2c7c94
SS
317 return SR_ERR;
318
319 g_free(tmp_str);
320 }
321
322 return SR_OK;
323}
324
325SR_PRIV int lecroy_xstream_update_sample_rate(const struct sr_dev_inst *sdi)
e3b83c5e 326{
e3b83c5e 327 struct dev_context *devc;
3f2c7c94
SS
328 struct scope_state *state;
329 const struct scope_config *config;
330 float memsize, timediv;
331
332 devc = sdi->priv;
333 state = devc->model_state;
334 config = devc->model_config;
335
336 if (sr_scpi_get_float(sdi->conn, "MEMORY_SIZE?", &memsize) != SR_OK)
ea257cdc 337 return SR_ERR;
3f2c7c94
SS
338
339 if (sr_scpi_get_float(sdi->conn, "TIME_DIV?", &timediv) != SR_OK)
340 return SR_ERR;
341
ea257cdc 342 state->sample_rate = 1 / ((timediv * config->num_xdivs) / memsize);
3f2c7c94
SS
343
344 return SR_OK;
345}
346
347SR_PRIV int lecroy_xstream_state_get(struct sr_dev_inst *sdi)
348{
349 struct dev_context *devc;
ea257cdc
UH
350 struct scope_state *state;
351 const struct scope_config *config;
3f2c7c94
SS
352 unsigned int i;
353 char *tmp_str, *tmp_str2, *tmpp, *p, *key;
354 char command[MAX_COMMAND_SIZE];
355 char *trig_source = NULL;
356
357 devc = sdi->priv;
358 config = devc->model_config;
359 state = devc->model_state;
360
361 sr_info("Fetching scope state");
362
363 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
364 return SR_ERR;
365
366 if (sr_scpi_get_string(sdi->conn, "TIME_DIV?", &tmp_str) != SR_OK)
367 return SR_ERR;
368
6d13a46c 369 if (array_float_get(tmp_str, timebases, ARRAY_SIZE(timebases), &i) != SR_OK) {
3f2c7c94
SS
370 g_free(tmp_str);
371 sr_err("Could not determine array index for timbase scale.");
372 return SR_ERR;
373 }
374 g_free(tmp_str);
375 state->timebase = i;
376
377 if (sr_scpi_get_string(sdi->conn, "TRIG_SELECT?", &tmp_str) != SR_OK)
378 return SR_ERR;
379
7002e64a 380 key = tmpp = NULL;
3f2c7c94
SS
381 tmp_str2 = tmp_str;
382 i = 0;
ea257cdc 383 while ((p = strtok_r(tmp_str2, ",", &tmpp))) {
3f2c7c94
SS
384 tmp_str2 = NULL;
385 if (i == 0) {
386 /* trigger type */
387 } else if (i & 1) {
388 key = p;
389 /* key */
390 } else if (!(i & 1)) {
391 if (!strcmp(key, "SR"))
392 trig_source = p;
393 }
394 i++;
395 }
396
ea257cdc 397 if (!trig_source || scope_state_get_array_option(trig_source, config->trigger_sources, &state->trigger_source) != SR_OK)
3f2c7c94
SS
398 return SR_ERR;
399
3f2c7c94
SS
400 g_snprintf(command, sizeof(command), "%s:TRIG_SLOPE?", trig_source);
401 if (sr_scpi_get_string(sdi->conn, command, &tmp_str) != SR_OK)
402 return SR_ERR;
403
404 if (scope_state_get_array_option(tmp_str,
405 config->trigger_slopes, &state->trigger_slope) != SR_OK)
406 return SR_ERR;
407
408 if (sr_scpi_get_float(sdi->conn, "TRIG_DELAY?", &state->horiz_triggerpos) != SR_OK)
409 return SR_ERR;
410
411 if (lecroy_xstream_update_sample_rate(sdi) != SR_OK)
412 return SR_ERR;
413
414 sr_info("Fetching finished.");
415
416 scope_state_dump(config, state);
417
418 return SR_OK;
419}
420
421static struct scope_state *scope_state_new(const struct scope_config *config)
422{
423 struct scope_state *state;
424
425 state = g_malloc0(sizeof(struct scope_state));
426 state->analog_channels = g_malloc0_n(config->analog_channels,
427 sizeof(struct analog_channel_state));
428 return state;
429}
430
431SR_PRIV void lecroy_xstream_state_free(struct scope_state *state)
432{
433 g_free(state->analog_channels);
434 g_free(state);
435}
436
437SR_PRIV int lecroy_xstream_init_device(struct sr_dev_inst *sdi)
438{
439 char command[MAX_COMMAND_SIZE];
440 int model_index;
441 unsigned int i, j;
442 struct sr_channel *ch;
443 struct dev_context *devc;
444 gboolean channel_enabled;
445
446 devc = sdi->priv;
447 model_index = -1;
448
449 /* Find the exact model. */
450 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
451 for (j = 0; scope_models[i].name[j]; j++) {
452 if (!strcmp(sdi->model, scope_models[i].name[j])) {
453 model_index = i;
454 break;
455 }
456 }
457 if (model_index != -1)
458 break;
459 }
460
461 if (model_index == -1) {
ea257cdc 462 sr_dbg("Unsupported LeCroy device.");
3f2c7c94
SS
463 return SR_ERR_NA;
464 }
465
466 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
ea257cdc 467 scope_models[model_index].analog_channels);
3f2c7c94
SS
468
469 /* Add analog channels. */
470 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
ea257cdc 471 g_snprintf(command, sizeof(command), "C%d:TRACE?", i + 1);
3f2c7c94
SS
472
473 if (sr_scpi_get_bool(sdi->conn, command, &channel_enabled) != SR_OK)
474 return SR_ERR;
475
ea257cdc 476 g_snprintf(command, sizeof(command), "C%d:VDIV?", i + 1);
3f2c7c94
SS
477
478 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, channel_enabled,
479 (*scope_models[model_index].analog_names)[i]);
480
481 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
482
483 devc->analog_groups[i]->name = g_strdup(
484 (char *)(*scope_models[model_index].analog_names)[i]);
485 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
486
487 sdi->channel_groups = g_slist_append(sdi->channel_groups,
488 devc->analog_groups[i]);
489 }
490
491 devc->model_config = &scope_models[model_index];
492 devc->frame_limit = 0;
493
494 if (!(devc->model_state = scope_state_new(devc->model_config)))
495 return SR_ERR_MALLOC;
496
d0b913ea
SA
497 /* Set the desired response mode. */
498 sr_scpi_send(sdi->conn, "COMM_HEADER OFF,WORD,BIN");
499
3f2c7c94
SS
500 return SR_OK;
501}
502
503static int lecroy_waveform_2_x_to_analog(GByteArray *data,
504 struct lecroy_wavedesc *desc,
505 struct sr_datafeed_analog *analog)
506{
507 struct sr_analog_encoding *encoding = analog->encoding;
508 struct sr_analog_meaning *meaning = analog->meaning;
509 struct sr_analog_spec *spec = analog->spec;
510 float *data_float;
511 int16_t *waveform_data;
512 unsigned int i, num_samples;
513
ea257cdc 514 data_float = g_malloc(desc->version_2_x.wave_array_count * sizeof(float));
3f2c7c94
SS
515 num_samples = desc->version_2_x.wave_array_count;
516
517 waveform_data = (int16_t *)(data->data +
518 + desc->version_2_x.wave_descriptor_length
519 + desc->version_2_x.user_text_len);
520
ea257cdc 521 for (i = 0; i < num_samples; i++)
3f2c7c94
SS
522 data_float[i] = (float)waveform_data[i]
523 * desc->version_2_x.vertical_gain
524 + desc->version_2_x.vertical_offset;
525
3f2c7c94
SS
526 analog->data = data_float;
527 analog->num_samples = num_samples;
528
529 encoding->unitsize = sizeof(float);
530 encoding->is_signed = TRUE;
531 encoding->is_float = TRUE;
532 encoding->is_bigendian = FALSE;
533 encoding->scale.p = 1;
534 encoding->scale.q = 1;
535 encoding->offset.p = 0;
536 encoding->offset.q = 1;
537
538 encoding->digits = 6;
539 encoding->is_digits_decimal = FALSE;
540
541 if (strcmp(desc->version_2_x.vertunit, "A")) {
542 meaning->mq = SR_MQ_CURRENT;
543 meaning->unit = SR_UNIT_AMPERE;
544 } else {
ea257cdc 545 /* Default to voltage. */
3f2c7c94
SS
546 meaning->mq = SR_MQ_VOLTAGE;
547 meaning->unit = SR_UNIT_VOLT;
548 }
549
550 meaning->mqflags = 0;
551 spec->spec_digits = 3;
ea257cdc 552
3f2c7c94
SS
553 return SR_OK;
554}
555
556static int lecroy_waveform_to_analog(GByteArray *data,
557 struct sr_datafeed_analog *analog)
558{
559 struct lecroy_wavedesc *desc;
560
561 if (data->len < sizeof(struct lecroy_wavedesc))
562 return SR_ERR;
563
564 desc = (struct lecroy_wavedesc *)data->data;
565
566 if (!strncmp(desc->template_name, "LECROY_2_2", 16) ||
567 !strncmp(desc->template_name, "LECROY_2_3", 16)) {
568 return lecroy_waveform_2_x_to_analog(data, desc, analog);
569 }
570
ea257cdc
UH
571 sr_err("Waveformat template '%.16s' not supported.",
572 desc->template_name);
573
3f2c7c94
SS
574 return SR_ERR;
575}
576
577SR_PRIV int lecroy_xstream_receive_data(int fd, int revents, void *cb_data)
578{
579 struct sr_channel *ch;
580 struct sr_dev_inst *sdi;
581 struct dev_context *devc;
582 struct sr_datafeed_packet packet;
583 GByteArray *data;
584 struct sr_datafeed_analog analog;
585 struct sr_analog_encoding encoding;
586 struct sr_analog_meaning meaning;
587 struct sr_analog_spec spec;
588 char buf[8];
ea257cdc 589
e3b83c5e 590 (void)fd;
3f2c7c94
SS
591 (void)revents;
592
593 data = NULL;
e3b83c5e
SS
594
595 if (!(sdi = cb_data))
596 return TRUE;
597
598 if (!(devc = sdi->priv))
599 return TRUE;
600
3f2c7c94
SS
601 ch = devc->current_channel->data;
602
603 /*
604 * Send "frame begin" packet upon reception of data for the
605 * first enabled channel.
606 */
607 if (devc->current_channel == devc->enabled_channels) {
608 packet.type = SR_DF_FRAME_BEGIN;
609 sr_session_send(sdi, &packet);
610 }
611
612 if (ch->type != SR_CHANNEL_ANALOG)
613 return SR_ERR;
ea257cdc
UH
614
615 /* Pass on the received data of the channel(s). */
3f2c7c94 616 if (sr_scpi_read_data(sdi->conn, buf, 4) != 4) {
ea257cdc 617 sr_err("Reading header failed.");
3f2c7c94
SS
618 return TRUE;
619 }
620
621 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
622 if (data)
623 g_byte_array_free(data, TRUE);
624 return TRUE;
625 }
626
627 analog.encoding = &encoding;
628 analog.meaning = &meaning;
629 analog.spec = &spec;
630
631 if (lecroy_waveform_to_analog(data, &analog) != SR_OK)
632 return SR_ERR;
633
634 meaning.channels = g_slist_append(NULL, ch);
635 packet.payload = &analog;
636 packet.type = SR_DF_ANALOG;
637 sr_session_send(sdi, &packet);
638
639 g_byte_array_free(data, TRUE);
640 data = NULL;
641
642 g_slist_free(meaning.channels);
643 g_free(analog.data);
644
3f2c7c94
SS
645 /*
646 * Advance to the next enabled channel. When data for all enabled
647 * channels was received, then flush potentially queued logic data,
648 * and send the "frame end" packet.
649 */
650 if (devc->current_channel->next) {
651 devc->current_channel = devc->current_channel->next;
652 lecroy_xstream_request_data(sdi);
653 return TRUE;
654 }
655
656 packet.type = SR_DF_FRAME_END;
657 sr_session_send(sdi, &packet);
658
659 /*
660 * End of frame was reached. Stop acquisition after the specified
661 * number of frames, or continue reception by starting over at
662 * the first enabled channel.
663 */
664 if (++devc->num_frames == devc->frame_limit) {
d2f7c417 665 sr_dev_acquisition_stop(sdi);
3f2c7c94
SS
666 } else {
667 devc->current_channel = devc->enabled_channels;
668 lecroy_xstream_request_data(sdi);
e3b83c5e
SS
669 }
670
671 return TRUE;
672}