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