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