2 * This file is part of the libsigrok project.
4 * Copyright (C) 2017 Sven Schnelle <svens@stackframe.org>
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.
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.
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/>.
26 struct lecroy_wavedesc_2_x {
28 uint16_t comm_order; /* 1 - little endian */
29 uint32_t wave_descriptor_length;
30 uint32_t user_text_len;
32 uint32_t trigtime_array_length;
33 uint32_t ris_time1_array_length;
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;
43 uint32_t wave_array_count;
44 uint32_t points_per_screen;
45 uint32_t first_valid_point;
46 uint32_t last_valid_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;
55 float vertical_offset;
58 uint16_t nominal_bits;
59 uint16_t nom_subarray_count;
67 } __attribute__((packed));
69 struct lecroy_wavedesc {
70 char descriptor_name[16];
71 char template_name[16];
73 struct lecroy_wavedesc_2_x version_2_x;
75 } __attribute__((packed));
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
85 static const char *scope_trigger_slopes[] = {
89 static const char *trigger_sources[] = {
90 "C1", "C2", "C3", "C4", "LINE", "EXT",
93 static const struct sr_rational timebases[] = {
95 { 20, 1000000000000 },
96 { 50, 1000000000000 },
97 { 100, 1000000000000 },
98 { 200, 1000000000000 },
99 { 500, 1000000000000 },
143 static const struct sr_rational vdivs[] = {
163 static const char *scope_analog_channel_names[] = {
164 "CH1", "CH2", "CH3", "CH4",
167 static const struct scope_config scope_models[] = {
169 .name = { "WP7000", "WP7100", "WP7200", "WP7300" },
171 .analog_channels = 4,
172 .analog_names = &scope_analog_channel_names,
174 .coupling_options = &coupling_options,
175 .num_coupling_options = ARRAY_SIZE(coupling_options),
177 .trigger_sources = &trigger_sources,
178 .num_trigger_sources = ARRAY_SIZE(trigger_sources),
180 .trigger_slopes = &scope_trigger_slopes,
181 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
183 .timebases = timebases,
184 .num_timebases = ARRAY_SIZE(timebases),
187 .num_vdivs = ARRAY_SIZE(vdivs),
194 static void scope_state_dump(const struct scope_config *config,
195 struct scope_state *state)
200 for (i = 0; i < config->analog_channels; i++) {
201 tmp = sr_voltage_string(config->vdivs[state->analog_channels[i].vdiv].p,
202 config->vdivs[state->analog_channels[i].vdiv].q);
203 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
204 i + 1, state->analog_channels[i].state ? "On" : "Off",
205 (*config->coupling_options)[state->analog_channels[i].coupling],
206 tmp, state->analog_channels[i].vertical_offset);
209 tmp = sr_period_string(config->timebases[state->timebase].p,
210 config->timebases[state->timebase].q);
211 sr_info("Current timebase: %s", tmp);
214 tmp = sr_samplerate_string(state->sample_rate);
215 sr_info("Current samplerate: %s", tmp);
218 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
219 (*config->trigger_sources)[state->trigger_source],
220 (*config->trigger_slopes)[state->trigger_slope],
221 state->horiz_triggerpos);
224 static int scope_state_get_array_option(const char *resp,
225 const char *(*array)[], unsigned int n, int *result)
229 for (i = 0; i < n; i++) {
230 if (!g_strcmp0(resp, (*array)[i])) {
240 * This function takes a value of the form "2.000E-03" and returns the index
241 * of an array where a matching pair was found.
243 * @param value The string to be parsed.
244 * @param array The array of s/f pairs.
245 * @param array_len The number of pairs in the array.
246 * @param result The index at which a matching pair was found.
248 * @return SR_ERR on any parsing error, SR_OK otherwise.
250 static int array_float_get(gchar *value, const struct sr_rational *aval,
251 int array_len, unsigned int *result)
253 struct sr_rational rval;
255 if (sr_parse_rational(value, &rval) != SR_OK)
258 for (int i = 0; i < array_len; i++) {
259 if (sr_rational_eq(&rval, aval + i)) {
268 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
269 const struct scope_config *config,
270 struct scope_state *state)
273 char command[MAX_COMMAND_SIZE];
276 for (i = 0; i < config->analog_channels; i++) {
277 g_snprintf(command, sizeof(command), "C%d:TRACE?", i + 1);
279 if (sr_scpi_get_bool(scpi, command,
280 &state->analog_channels[i].state) != SR_OK)
283 g_snprintf(command, sizeof(command), "C%d:VDIV?", i + 1);
285 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
288 if (array_float_get(tmp_str, ARRAY_AND_SIZE(vdivs), &j) != SR_OK) {
290 sr_err("Could not determine array index for vertical div scale.");
295 state->analog_channels[i].vdiv = j;
297 g_snprintf(command, sizeof(command), "C%d:OFFSET?", i + 1);
299 if (sr_scpi_get_float(scpi, command, &state->analog_channels[i].vertical_offset) != SR_OK)
302 g_snprintf(command, sizeof(command), "C%d:COUPLING?", i + 1);
304 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
308 if (scope_state_get_array_option(tmp_str, config->coupling_options,
309 config->num_coupling_options,
310 &state->analog_channels[i].coupling) != SR_OK)
319 SR_PRIV int lecroy_xstream_update_sample_rate(const struct sr_dev_inst *sdi)
321 struct dev_context *devc;
322 struct scope_state *state;
323 const struct scope_config *config;
324 float memsize, timediv;
327 state = devc->model_state;
328 config = devc->model_config;
330 if (sr_scpi_get_float(sdi->conn, "MEMORY_SIZE?", &memsize) != SR_OK)
333 if (sr_scpi_get_float(sdi->conn, "TIME_DIV?", &timediv) != SR_OK)
336 state->sample_rate = 1 / ((timediv * config->num_xdivs) / memsize);
341 SR_PRIV int lecroy_xstream_state_get(struct sr_dev_inst *sdi)
343 struct dev_context *devc;
344 struct scope_state *state;
345 const struct scope_config *config;
347 char *tmp_str, *tmp_str2, *tmpp, *p, *key;
348 char command[MAX_COMMAND_SIZE];
349 char *trig_source = NULL;
352 config = devc->model_config;
353 state = devc->model_state;
355 sr_info("Fetching scope state");
357 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
360 if (sr_scpi_get_string(sdi->conn, "TIME_DIV?", &tmp_str) != SR_OK)
363 if (array_float_get(tmp_str, ARRAY_AND_SIZE(timebases), &i) != SR_OK) {
365 sr_err("Could not determine array index for timbase scale.");
371 if (sr_scpi_get_string(sdi->conn, "TRIG_SELECT?", &tmp_str) != SR_OK)
377 while ((p = strtok_r(tmp_str2, ",", &tmpp))) {
384 } else if (!(i & 1)) {
385 if (!strcmp(key, "SR"))
391 if (!trig_source || scope_state_get_array_option(trig_source, config->trigger_sources, config->num_trigger_sources, &state->trigger_source) != SR_OK)
394 g_snprintf(command, sizeof(command), "%s:TRIG_SLOPE?", trig_source);
395 if (sr_scpi_get_string(sdi->conn, command, &tmp_str) != SR_OK)
398 if (scope_state_get_array_option(tmp_str,
399 config->trigger_slopes, config->num_trigger_slopes, &state->trigger_slope) != SR_OK)
402 if (sr_scpi_get_float(sdi->conn, "TRIG_DELAY?", &state->horiz_triggerpos) != SR_OK)
405 if (lecroy_xstream_update_sample_rate(sdi) != SR_OK)
408 sr_info("Fetching finished.");
410 scope_state_dump(config, state);
415 static struct scope_state *scope_state_new(const struct scope_config *config)
417 struct scope_state *state;
419 state = g_malloc0(sizeof(struct scope_state));
420 state->analog_channels = g_malloc0_n(config->analog_channels,
421 sizeof(struct analog_channel_state));
425 SR_PRIV void lecroy_xstream_state_free(struct scope_state *state)
427 g_free(state->analog_channels);
431 SR_PRIV int lecroy_xstream_init_device(struct sr_dev_inst *sdi)
433 char command[MAX_COMMAND_SIZE];
436 struct sr_channel *ch;
437 struct dev_context *devc;
438 gboolean channel_enabled;
443 /* Find the exact model. */
444 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
445 for (j = 0; scope_models[i].name[j]; j++) {
446 if (!strcmp(sdi->model, scope_models[i].name[j])) {
451 if (model_index != -1)
455 if (model_index == -1) {
456 sr_dbg("Unsupported LeCroy device.");
460 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
461 scope_models[model_index].analog_channels);
463 /* Add analog channels. */
464 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
465 g_snprintf(command, sizeof(command), "C%d:TRACE?", i + 1);
467 if (sr_scpi_get_bool(sdi->conn, command, &channel_enabled) != SR_OK)
470 g_snprintf(command, sizeof(command), "C%d:VDIV?", i + 1);
472 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, channel_enabled,
473 (*scope_models[model_index].analog_names)[i]);
475 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
477 devc->analog_groups[i]->name = g_strdup(
478 (char *)(*scope_models[model_index].analog_names)[i]);
479 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
481 sdi->channel_groups = g_slist_append(sdi->channel_groups,
482 devc->analog_groups[i]);
485 devc->model_config = &scope_models[model_index];
486 devc->frame_limit = 0;
488 if (!(devc->model_state = scope_state_new(devc->model_config)))
489 return SR_ERR_MALLOC;
491 /* Set the desired response mode. */
492 sr_scpi_send(sdi->conn, "COMM_HEADER OFF,WORD,BIN");
497 static int lecroy_waveform_2_x_to_analog(GByteArray *data,
498 struct lecroy_wavedesc *desc,
499 struct sr_datafeed_analog *analog)
501 struct sr_analog_encoding *encoding = analog->encoding;
502 struct sr_analog_meaning *meaning = analog->meaning;
503 struct sr_analog_spec *spec = analog->spec;
505 int16_t *waveform_data;
506 unsigned int i, num_samples;
508 data_float = g_malloc(desc->version_2_x.wave_array_count * sizeof(float));
509 num_samples = desc->version_2_x.wave_array_count;
511 waveform_data = (int16_t *)(data->data +
512 + desc->version_2_x.wave_descriptor_length
513 + desc->version_2_x.user_text_len);
515 for (i = 0; i < num_samples; i++)
516 data_float[i] = (float)waveform_data[i]
517 * desc->version_2_x.vertical_gain
518 + desc->version_2_x.vertical_offset;
520 analog->data = data_float;
521 analog->num_samples = num_samples;
523 encoding->unitsize = sizeof(float);
524 encoding->is_signed = TRUE;
525 encoding->is_float = TRUE;
526 encoding->is_bigendian = FALSE;
527 encoding->scale.p = 1;
528 encoding->scale.q = 1;
529 encoding->offset.p = 0;
530 encoding->offset.q = 1;
532 encoding->digits = 6;
533 encoding->is_digits_decimal = FALSE;
535 if (strcmp(desc->version_2_x.vertunit, "A")) {
536 meaning->mq = SR_MQ_CURRENT;
537 meaning->unit = SR_UNIT_AMPERE;
539 /* Default to voltage. */
540 meaning->mq = SR_MQ_VOLTAGE;
541 meaning->unit = SR_UNIT_VOLT;
544 meaning->mqflags = 0;
545 spec->spec_digits = 3;
550 static int lecroy_waveform_to_analog(GByteArray *data,
551 struct sr_datafeed_analog *analog)
553 struct lecroy_wavedesc *desc;
555 if (data->len < sizeof(struct lecroy_wavedesc))
558 desc = (struct lecroy_wavedesc *)data->data;
560 if (!strncmp(desc->template_name, "LECROY_2_2", 16) ||
561 !strncmp(desc->template_name, "LECROY_2_3", 16)) {
562 return lecroy_waveform_2_x_to_analog(data, desc, analog);
565 sr_err("Waveformat template '%.16s' not supported.",
566 desc->template_name);
571 SR_PRIV int lecroy_xstream_receive_data(int fd, int revents, void *cb_data)
573 struct sr_channel *ch;
574 struct sr_dev_inst *sdi;
575 struct dev_context *devc;
576 struct sr_datafeed_packet packet;
578 struct sr_datafeed_analog analog;
579 struct sr_analog_encoding encoding;
580 struct sr_analog_meaning meaning;
581 struct sr_analog_spec spec;
589 if (!(sdi = cb_data))
592 if (!(devc = sdi->priv))
595 ch = devc->current_channel->data;
598 * Send "frame begin" packet upon reception of data for the
599 * first enabled channel.
601 if (devc->current_channel == devc->enabled_channels) {
602 packet.type = SR_DF_FRAME_BEGIN;
603 sr_session_send(sdi, &packet);
606 if (ch->type != SR_CHANNEL_ANALOG)
609 /* Pass on the received data of the channel(s). */
610 if (sr_scpi_read_data(sdi->conn, buf, 4) != 4) {
611 sr_err("Reading header failed.");
615 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
617 g_byte_array_free(data, TRUE);
621 analog.encoding = &encoding;
622 analog.meaning = &meaning;
625 if (lecroy_waveform_to_analog(data, &analog) != SR_OK)
628 meaning.channels = g_slist_append(NULL, ch);
629 packet.payload = &analog;
630 packet.type = SR_DF_ANALOG;
631 sr_session_send(sdi, &packet);
633 g_byte_array_free(data, TRUE);
636 g_slist_free(meaning.channels);
640 * Advance to the next enabled channel. When data for all enabled
641 * channels was received, then flush potentially queued logic data,
642 * and send the "frame end" packet.
644 if (devc->current_channel->next) {
645 devc->current_channel = devc->current_channel->next;
646 lecroy_xstream_request_data(sdi);
650 packet.type = SR_DF_FRAME_END;
651 sr_session_send(sdi, &packet);
654 * End of frame was reached. Stop acquisition after the specified
655 * number of frames, or continue reception by starting over at
656 * the first enabled channel.
658 if (++devc->num_frames == devc->frame_limit) {
659 sr_dev_acquisition_stop(sdi);
661 devc->current_channel = devc->enabled_channels;
662 lecroy_xstream_request_data(sdi);