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