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 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);
32 struct lecroy_wavedesc_2_x {
34 uint16_t comm_order; /* 1 - little endian */
35 uint32_t wave_descriptor_length;
36 uint32_t user_text_len;
38 uint32_t trigtime_array_length;
39 uint32_t ris_time1_array_length;
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;
49 uint32_t wave_array_count;
50 uint32_t points_per_screen;
51 uint32_t first_valid_point;
52 uint32_t last_valid_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;
61 float vertical_offset;
64 uint16_t nominal_bits;
65 uint16_t nom_subarray_count;
73 } __attribute__((packed));
75 struct lecroy_wavedesc {
76 char descriptor_name[16];
77 char template_name[16];
79 struct lecroy_wavedesc_2_x version_2_x;
81 } __attribute__((packed));
83 static const uint32_t lecroy_devopts[] = {
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,
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,
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
109 static const char *scope_trigger_slopes[] = {
115 static const char *lecroy_xstream_trigger_sources[] = {
125 static const struct sr_rational lecroy_timebases[] = {
127 { 20, 1000000000000 },
128 { 50, 1000000000000 },
129 { 100, 1000000000000 },
130 { 200, 1000000000000 },
131 { 500, 1000000000000 },
175 static const struct sr_rational lecroy_vdivs[] = {
195 static const char *scope_analog_channel_names[] = {
202 static const struct scope_config scope_models[] = {
204 .name = { "WP7000", "WP7100", "WP7200", "WP7300" },
206 .analog_channels = 4,
207 .analog_names = &scope_analog_channel_names,
209 .devopts = &lecroy_devopts,
210 .num_devopts = ARRAY_SIZE(lecroy_devopts),
212 .analog_devopts = &lecroy_analog_devopts,
213 .num_analog_devopts = ARRAY_SIZE(lecroy_analog_devopts),
215 .coupling_options = &lecroy_coupling_options,
216 .trigger_sources = &lecroy_xstream_trigger_sources,
217 .trigger_slopes = &scope_trigger_slopes,
219 .timebases = lecroy_timebases,
220 .num_timebases = ARRAY_SIZE(lecroy_timebases),
222 .vdivs = lecroy_vdivs,
223 .num_vdivs = ARRAY_SIZE(lecroy_vdivs),
230 static void scope_state_dump(const struct scope_config *config,
231 struct scope_state *state)
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);
245 tmp = sr_period_string_f(1.0/(((float)config->timebases[state->timebase].p) /
246 ((float)config->timebases[state->timebase].q)), 0);
247 sr_info("Current timebase: %s", tmp);
250 tmp = sr_samplerate_string(state->sample_rate);
251 sr_info("Current samplerate: %s", tmp);
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);
260 static int scope_state_get_array_option(const char *resp,
261 const char *(*array)[],
266 for (i = 0; (*array)[i]; i++) {
267 if (!g_strcmp0(resp, (*array)[i])) {
277 * This function takes a value of the form "2.000E-03" and returns the index
278 * of an array where a matching pair was found.
280 * @param value The string to be parsed.
281 * @param array The array of s/f pairs.
282 * @param array_len The number of pairs in the array.
283 * @param result The index at which a matching pair was found.
285 * @return SR_ERR on any parsing error, SR_OK otherwise.
287 static int array_float_get(gchar *value, const struct sr_rational *aval,
288 int array_len, unsigned int *result)
290 struct sr_rational rval;
292 if (sr_parse_rational(value, &rval) != SR_OK)
295 for (int i = 0; i < array_len; i++) {
296 if (sr_rational_eq(&rval, aval+i)) {
305 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
306 const struct scope_config *config,
307 struct scope_state *state)
310 char command[MAX_COMMAND_SIZE];
313 for (i = 0; i < config->analog_channels; i++) {
314 g_snprintf(command, sizeof(command), "C%d:TRACE?", i+1);
316 if (sr_scpi_get_bool(scpi, command,
317 &state->analog_channels[i].state) != SR_OK)
320 g_snprintf(command, sizeof(command), "C%d:VDIV?", i+1);
322 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
325 if (array_float_get(tmp_str, lecroy_vdivs, ARRAY_SIZE(lecroy_vdivs),
328 sr_err("Could not determine array index for vertical div scale.");
333 state->analog_channels[i].vdiv = j;
335 g_snprintf(command, sizeof(command), "C%d:OFFSET?", i+1);
337 if (sr_scpi_get_float(scpi, command, &state->analog_channels[i].vertical_offset) != SR_OK)
340 g_snprintf(command, sizeof(command), "C%d:COUPLING?", i+1);
342 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
346 if (scope_state_get_array_option(tmp_str, config->coupling_options,
347 &state->analog_channels[i].coupling) != SR_OK)
356 SR_PRIV int lecroy_xstream_update_sample_rate(const struct sr_dev_inst *sdi)
358 struct dev_context *devc;
359 struct scope_state *state;
360 const struct scope_config *config;
361 float memsize, timediv;
364 state = devc->model_state;
365 config = devc->model_config;
367 if (sr_scpi_get_float(sdi->conn, "MEMORY_SIZE?", &memsize) != SR_OK)
370 if (sr_scpi_get_float(sdi->conn, "TIME_DIV?", &timediv) != SR_OK)
373 state->sample_rate = 1/((timediv * config->num_xdivs) / memsize);
378 SR_PRIV int lecroy_xstream_state_get(struct sr_dev_inst *sdi)
380 struct dev_context *devc;
381 struct scope_state *state
382 ; const struct scope_config *config;
384 char *tmp_str, *tmp_str2, *tmpp, *p, *key;
385 char command[MAX_COMMAND_SIZE];
386 char *trig_source = NULL;
389 config = devc->model_config;
390 state = devc->model_state;
392 sr_info("Fetching scope state");
394 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
397 if (sr_scpi_get_string(sdi->conn, "TIME_DIV?", &tmp_str) != SR_OK)
400 if (array_float_get(tmp_str, lecroy_timebases, ARRAY_SIZE(lecroy_timebases),
403 sr_err("Could not determine array index for timbase scale.");
409 if (sr_scpi_get_string(sdi->conn, "TRIG_SELECT?", &tmp_str) != SR_OK)
414 while((p = strtok_r(tmp_str2, ",", &tmpp))) {
421 } else if (!(i & 1)) {
422 if (!strcmp(key, "SR"))
428 if (!trig_source || scope_state_get_array_option(trig_source, config->trigger_sources,
429 &state->trigger_source) != SR_OK)
433 g_snprintf(command, sizeof(command), "%s:TRIG_SLOPE?", trig_source);
434 if (sr_scpi_get_string(sdi->conn, command, &tmp_str) != SR_OK)
437 if (scope_state_get_array_option(tmp_str,
438 config->trigger_slopes, &state->trigger_slope) != SR_OK)
441 if (sr_scpi_get_float(sdi->conn, "TRIG_DELAY?", &state->horiz_triggerpos) != SR_OK)
444 if (lecroy_xstream_update_sample_rate(sdi) != SR_OK)
447 sr_info("Fetching finished.");
449 scope_state_dump(config, state);
454 static struct scope_state *scope_state_new(const struct scope_config *config)
456 struct scope_state *state;
458 state = g_malloc0(sizeof(struct scope_state));
459 state->analog_channels = g_malloc0_n(config->analog_channels,
460 sizeof(struct analog_channel_state));
464 SR_PRIV void lecroy_xstream_state_free(struct scope_state *state)
466 g_free(state->analog_channels);
470 SR_PRIV int lecroy_xstream_init_device(struct sr_dev_inst *sdi)
472 char command[MAX_COMMAND_SIZE];
475 struct sr_channel *ch;
476 struct dev_context *devc;
477 gboolean channel_enabled;
482 /* Find the exact model. */
483 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
484 for (j = 0; scope_models[i].name[j]; j++) {
485 if (!strcmp(sdi->model, scope_models[i].name[j])) {
490 if (model_index != -1)
494 if (model_index == -1) {
495 sr_dbg("Unsupported LECROY device.");
499 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
500 scope_models[model_index].analog_channels);
502 /* Add analog channels. */
503 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
505 g_snprintf(command, sizeof(command), "C%d:TRACE?", i+1);
507 if (sr_scpi_get_bool(sdi->conn, command, &channel_enabled) != SR_OK)
510 g_snprintf(command, sizeof(command), "C%d:VDIV?", i+1);
512 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, channel_enabled,
513 (*scope_models[model_index].analog_names)[i]);
515 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
517 devc->analog_groups[i]->name = g_strdup(
518 (char *)(*scope_models[model_index].analog_names)[i]);
519 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
521 sdi->channel_groups = g_slist_append(sdi->channel_groups,
522 devc->analog_groups[i]);
525 devc->model_config = &scope_models[model_index];
526 devc->frame_limit = 0;
528 if (!(devc->model_state = scope_state_new(devc->model_config)))
529 return SR_ERR_MALLOC;
531 /* Set the desired response mode. */
532 sr_scpi_send(sdi->conn, "COMM_HEADER OFF,WORD,BIN");
537 static int lecroy_waveform_2_x_to_analog(GByteArray *data,
538 struct lecroy_wavedesc *desc,
539 struct sr_datafeed_analog *analog)
541 struct sr_analog_encoding *encoding = analog->encoding;
542 struct sr_analog_meaning *meaning = analog->meaning;
543 struct sr_analog_spec *spec = analog->spec;
545 int16_t *waveform_data;
546 unsigned int i, num_samples;
548 data_float = g_malloc(desc->version_2_x.wave_array_count * sizeof(float));
549 num_samples = desc->version_2_x.wave_array_count;
551 waveform_data = (int16_t *)(data->data +
552 + desc->version_2_x.wave_descriptor_length
553 + desc->version_2_x.user_text_len);
555 for(i = 0; i < num_samples; i++)
556 data_float[i] = (float)waveform_data[i]
557 * desc->version_2_x.vertical_gain
558 + desc->version_2_x.vertical_offset;
561 analog->data = data_float;
562 analog->num_samples = num_samples;
564 encoding->unitsize = sizeof(float);
565 encoding->is_signed = TRUE;
566 encoding->is_float = TRUE;
567 encoding->is_bigendian = FALSE;
568 encoding->scale.p = 1;
569 encoding->scale.q = 1;
570 encoding->offset.p = 0;
571 encoding->offset.q = 1;
573 encoding->digits = 6;
574 encoding->is_digits_decimal = FALSE;
576 if (strcmp(desc->version_2_x.vertunit, "A")) {
577 meaning->mq = SR_MQ_CURRENT;
578 meaning->unit = SR_UNIT_AMPERE;
580 /* default to voltage */
581 meaning->mq = SR_MQ_VOLTAGE;
582 meaning->unit = SR_UNIT_VOLT;
585 meaning->mqflags = 0;
586 spec->spec_digits = 3;
590 static int lecroy_waveform_to_analog(GByteArray *data,
591 struct sr_datafeed_analog *analog)
593 struct lecroy_wavedesc *desc;
595 if (data->len < sizeof(struct lecroy_wavedesc))
598 desc = (struct lecroy_wavedesc *)data->data;
600 if (!strncmp(desc->template_name, "LECROY_2_2", 16) ||
601 !strncmp(desc->template_name, "LECROY_2_3", 16)) {
602 return lecroy_waveform_2_x_to_analog(data, desc, analog);
605 sr_err("Waveformat template '%.16s' not supported\n", desc->template_name);
609 SR_PRIV int lecroy_xstream_receive_data(int fd, int revents, void *cb_data)
611 struct sr_channel *ch;
612 struct sr_dev_inst *sdi;
613 struct dev_context *devc;
614 struct sr_datafeed_packet packet;
616 struct sr_datafeed_analog analog;
617 struct sr_analog_encoding encoding;
618 struct sr_analog_meaning meaning;
619 struct sr_analog_spec spec;
626 if (!(sdi = cb_data))
629 if (!(devc = sdi->priv))
632 ch = devc->current_channel->data;
635 * Send "frame begin" packet upon reception of data for the
636 * first enabled channel.
638 if (devc->current_channel == devc->enabled_channels) {
639 packet.type = SR_DF_FRAME_BEGIN;
640 sr_session_send(sdi, &packet);
643 if (ch->type != SR_CHANNEL_ANALOG)
646 * Pass on the received data of the channel(s).
648 if (sr_scpi_read_data(sdi->conn, buf, 4) != 4) {
649 sr_err("reading header failed\n");
653 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
655 g_byte_array_free(data, TRUE);
659 analog.encoding = &encoding;
660 analog.meaning = &meaning;
663 if (lecroy_waveform_to_analog(data, &analog) != SR_OK)
666 meaning.channels = g_slist_append(NULL, ch);
667 packet.payload = &analog;
668 packet.type = SR_DF_ANALOG;
669 sr_session_send(sdi, &packet);
671 g_byte_array_free(data, TRUE);
674 g_slist_free(meaning.channels);
679 * Advance to the next enabled channel. When data for all enabled
680 * channels was received, then flush potentially queued logic data,
681 * and send the "frame end" packet.
683 if (devc->current_channel->next) {
684 devc->current_channel = devc->current_channel->next;
685 lecroy_xstream_request_data(sdi);
689 packet.type = SR_DF_FRAME_END;
690 sr_session_send(sdi, &packet);
693 * End of frame was reached. Stop acquisition after the specified
694 * number of frames, or continue reception by starting over at
695 * the first enabled channel.
697 if (++devc->num_frames == devc->frame_limit) {
698 sdi->driver->dev_acquisition_stop(sdi);
700 devc->current_channel = devc->enabled_channels;
701 lecroy_xstream_request_data(sdi);