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 uint64_t timebases[][2] = {
95 { 20, UINT64_C(1000000000000) },
96 { 50, UINT64_C(1000000000000) },
97 { 100, UINT64_C(1000000000000) },
98 { 200, UINT64_C(1000000000000) },
99 { 500, UINT64_C(1000000000000) },
143 static const uint64_t vdivs[][2] = {
163 static const char *scope_analog_channel_names[] = {
164 "CH1", "CH2", "CH3", "CH4",
167 static const struct scope_config scope_models[] = {
172 .analog_channels = 4,
173 .analog_names = &scope_analog_channel_names,
175 .coupling_options = &coupling_options,
176 .num_coupling_options = ARRAY_SIZE(coupling_options),
178 .trigger_sources = &trigger_sources,
179 .num_trigger_sources = ARRAY_SIZE(trigger_sources),
181 .trigger_slopes = &scope_trigger_slopes,
182 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
184 .timebases = &timebases,
185 .num_timebases = ARRAY_SIZE(timebases),
188 .num_vdivs = ARRAY_SIZE(vdivs),
195 static void scope_state_dump(const struct scope_config *config,
196 struct scope_state *state)
201 for (i = 0; i < config->analog_channels; i++) {
202 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
203 (*config->vdivs)[state->analog_channels[i].vdiv][1]);
204 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
205 i + 1, state->analog_channels[i].state ? "On" : "Off",
206 (*config->coupling_options)[state->analog_channels[i].coupling],
207 tmp, state->analog_channels[i].vertical_offset);
210 tmp = sr_period_string((*config->timebases)[state->timebase][0],
211 (*config->timebases)[state->timebase][1]);
212 sr_info("Current timebase: %s", tmp);
215 tmp = sr_samplerate_string(state->sample_rate);
216 sr_info("Current samplerate: %s", tmp);
219 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
220 (*config->trigger_sources)[state->trigger_source],
221 (*config->trigger_slopes)[state->trigger_slope],
222 state->horiz_triggerpos);
225 static int scope_state_get_array_option(const char *resp,
226 const char *(*array)[], unsigned int n, int *result)
230 for (i = 0; i < n; i++) {
231 if (!g_strcmp0(resp, (*array)[i])) {
241 * This function takes a value of the form "2.000E-03" and returns the index
242 * of an array where a matching pair was found.
244 * @param value The string to be parsed.
245 * @param array The array of s/f pairs.
246 * @param array_len The number of pairs in the array.
247 * @param result The index at which a matching pair was found.
249 * @return SR_ERR on any parsing error, SR_OK otherwise.
251 static int array_float_get(gchar *value, const uint64_t array[][2],
252 int array_len, unsigned int *result)
254 struct sr_rational rval;
255 struct sr_rational aval;
257 if (sr_parse_rational(value, &rval) != SR_OK)
260 for (int i = 0; i < array_len; i++) {
261 sr_rational_set(&aval, array[i][0], array[i][1]);
262 if (sr_rational_eq(&rval, &aval)) {
271 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
272 const struct scope_config *config, struct scope_state *state)
275 char command[MAX_COMMAND_SIZE];
278 for (i = 0; i < config->analog_channels; i++) {
279 g_snprintf(command, sizeof(command), "C%d:TRACE?", i + 1);
281 if (sr_scpi_get_bool(scpi, command,
282 &state->analog_channels[i].state) != SR_OK)
285 g_snprintf(command, sizeof(command), "C%d:VDIV?", i + 1);
287 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
290 if (array_float_get(tmp_str, ARRAY_AND_SIZE(vdivs), &j) != SR_OK) {
292 sr_err("Could not determine array index for vertical div scale.");
297 state->analog_channels[i].vdiv = j;
299 g_snprintf(command, sizeof(command), "C%d:OFFSET?", i + 1);
301 if (sr_scpi_get_float(scpi, command, &state->analog_channels[i].vertical_offset) != SR_OK)
304 g_snprintf(command, sizeof(command), "C%d:COUPLING?", i + 1);
306 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
310 if (scope_state_get_array_option(tmp_str, config->coupling_options,
311 config->num_coupling_options,
312 &state->analog_channels[i].coupling) != SR_OK)
321 SR_PRIV int lecroy_xstream_channel_state_set(const struct sr_dev_inst *sdi,
322 const int ch_index, gboolean ch_state)
325 struct sr_channel *ch;
326 struct dev_context *devc = NULL;
327 struct scope_state *state;
328 char command[MAX_COMMAND_SIZE];
335 state = devc->model_state;
338 for (l = sdi->channels; l; l = l->next) {
342 case SR_CHANNEL_ANALOG:
343 if (ch->index == ch_index) {
344 g_snprintf(command, sizeof(command), "C%d:TRACE %s", ch_index + 1,
345 (ch_state ? "ON" : "OFF"));
346 if ((sr_scpi_send(sdi->conn, command) != SR_OK ||
347 sr_scpi_get_opc(sdi->conn) != SR_OK)) {
352 ch->enabled = ch_state;
353 state->analog_channels[ch->index].state = ch_state;
363 if ((result == SR_OK) && !chan_found)
369 SR_PRIV int lecroy_xstream_update_sample_rate(const struct sr_dev_inst *sdi,
372 struct dev_context *devc;
373 struct scope_state *state;
374 const struct scope_config *config;
378 config = devc->model_config;
379 state = devc->model_state;
381 if (sr_scpi_get_double(sdi->conn, "TIME_DIV?", &time_div) != SR_OK)
384 state->sample_rate = num_of_samples / (time_div * config->num_xdivs);
389 SR_PRIV int lecroy_xstream_state_get(struct sr_dev_inst *sdi)
391 struct dev_context *devc;
392 struct scope_state *state;
393 const struct scope_config *config;
395 char *tmp_str, *tmp_str2, *tmpp, *p, *key;
396 char command[MAX_COMMAND_SIZE];
397 char *trig_source = NULL;
400 config = devc->model_config;
401 state = devc->model_state;
403 sr_info("Fetching scope state");
405 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
408 if (sr_scpi_get_string(sdi->conn, "TIME_DIV?", &tmp_str) != SR_OK)
411 if (array_float_get(tmp_str, ARRAY_AND_SIZE(timebases), &i) != SR_OK) {
413 sr_err("Could not determine array index for timbase scale.");
419 if (sr_scpi_get_string(sdi->conn, "TRIG_SELECT?", &tmp_str) != SR_OK)
425 while ((p = strtok_r(tmp_str2, ",", &tmpp))) {
432 } else if (!(i & 1)) {
433 if (!strcmp(key, "SR"))
440 if (!trig_source || scope_state_get_array_option(trig_source,
441 config->trigger_sources, config->num_trigger_sources,
442 &state->trigger_source) != SR_OK)
445 g_snprintf(command, sizeof(command), "%s:TRIG_SLOPE?", trig_source);
446 if (sr_scpi_get_string(sdi->conn, command, &tmp_str) != SR_OK)
449 if (scope_state_get_array_option(tmp_str, config->trigger_slopes,
450 config->num_trigger_slopes, &state->trigger_slope) != SR_OK)
454 if (sr_scpi_get_float(sdi->conn, "TRIG_DELAY?", &state->horiz_triggerpos) != SR_OK)
457 sr_info("Fetching finished.");
459 scope_state_dump(config, state);
464 static struct scope_state *scope_state_new(const struct scope_config *config)
466 struct scope_state *state;
468 state = g_malloc0(sizeof(struct scope_state));
469 state->analog_channels = g_malloc0_n(config->analog_channels,
470 sizeof(struct analog_channel_state));
474 SR_PRIV void lecroy_xstream_state_free(struct scope_state *state)
476 g_free(state->analog_channels);
480 SR_PRIV int lecroy_xstream_init_device(struct sr_dev_inst *sdi)
482 char command[MAX_COMMAND_SIZE];
485 struct sr_channel *ch;
486 struct dev_context *devc;
487 gboolean channel_enabled;
492 /* Find the exact model. */
493 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
494 for (j = 0; scope_models[i].name[j]; j++) {
495 if (!strcmp(sdi->model, scope_models[i].name[j])) {
500 if (model_index != -1)
504 if (model_index == -1) {
505 sr_dbg("Unknown LeCroy device, using default config.");
506 for (i = 0; i < ARRAY_SIZE(scope_models); i++)
507 if (scope_models[i].name[0] == NULL)
511 /* Set the desired response and format modes. */
512 sr_scpi_send(sdi->conn, "COMM_HEADER OFF");
513 sr_scpi_send(sdi->conn, "COMM_FORMAT DEF9,WORD,BIN");
515 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
516 scope_models[model_index].analog_channels);
518 /* Add analog channels. */
519 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
520 g_snprintf(command, sizeof(command), "C%d:TRACE?", i + 1);
522 if (sr_scpi_get_bool(sdi->conn, command, &channel_enabled) != SR_OK)
525 g_snprintf(command, sizeof(command), "C%d:VDIV?", i + 1);
527 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, channel_enabled,
528 (*scope_models[model_index].analog_names)[i]);
530 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
532 devc->analog_groups[i]->name = g_strdup(
533 (char *)(*scope_models[model_index].analog_names)[i]);
534 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
536 sdi->channel_groups = g_slist_append(sdi->channel_groups,
537 devc->analog_groups[i]);
540 devc->model_config = &scope_models[model_index];
541 devc->frame_limit = 0;
542 devc->model_state = scope_state_new(devc->model_config);
547 static int lecroy_waveform_2_x_to_analog(GByteArray *data,
548 struct lecroy_wavedesc *desc, struct sr_datafeed_analog *analog)
550 struct sr_analog_encoding *encoding = analog->encoding;
551 struct sr_analog_meaning *meaning = analog->meaning;
552 struct sr_analog_spec *spec = analog->spec;
554 int16_t *waveform_data;
555 unsigned int i, num_samples;
557 data_float = g_malloc(desc->version_2_x.wave_array_count * sizeof(float));
558 num_samples = desc->version_2_x.wave_array_count;
560 waveform_data = (int16_t*)(data->data +
561 + desc->version_2_x.wave_descriptor_length
562 + desc->version_2_x.user_text_len);
564 for (i = 0; i < num_samples; i++)
565 data_float[i] = (float)waveform_data[i]
566 * desc->version_2_x.vertical_gain
567 + desc->version_2_x.vertical_offset;
569 analog->data = data_float;
570 analog->num_samples = num_samples;
572 encoding->unitsize = sizeof(float);
573 encoding->is_signed = TRUE;
574 encoding->is_float = TRUE;
575 encoding->is_bigendian = FALSE;
576 encoding->scale.p = 1;
577 encoding->scale.q = 1;
578 encoding->offset.p = 0;
579 encoding->offset.q = 1;
581 encoding->digits = 6;
582 encoding->is_digits_decimal = FALSE;
584 if (strcmp(desc->version_2_x.vertunit, "A")) {
585 meaning->mq = SR_MQ_CURRENT;
586 meaning->unit = SR_UNIT_AMPERE;
588 /* Default to voltage. */
589 meaning->mq = SR_MQ_VOLTAGE;
590 meaning->unit = SR_UNIT_VOLT;
593 meaning->mqflags = 0;
594 spec->spec_digits = 3;
599 static int lecroy_waveform_to_analog(GByteArray *data,
600 struct sr_datafeed_analog *analog)
602 struct lecroy_wavedesc *desc;
604 if (data->len < sizeof(struct lecroy_wavedesc))
607 desc = (struct lecroy_wavedesc*)data->data;
609 if (!strncmp(desc->template_name, "LECROY_2_2", 16) ||
610 !strncmp(desc->template_name, "LECROY_2_3", 16)) {
611 return lecroy_waveform_2_x_to_analog(data, desc, analog);
614 sr_err("Waveformat template '%.16s' not supported.", desc->template_name);
618 SR_PRIV int lecroy_xstream_receive_data(int fd, int revents, void *cb_data)
620 char command[MAX_COMMAND_SIZE];
621 struct sr_channel *ch;
622 struct sr_dev_inst *sdi;
623 struct dev_context *devc;
624 struct scope_state *state;
625 struct sr_datafeed_packet packet;
627 struct sr_datafeed_analog analog;
628 struct sr_analog_encoding encoding;
629 struct sr_analog_meaning meaning;
630 struct sr_analog_spec spec;
635 if (!(sdi = cb_data))
638 if (!(devc = sdi->priv))
641 ch = devc->current_channel->data;
642 state = devc->model_state;
644 if (ch->type != SR_CHANNEL_ANALOG)
648 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
650 g_byte_array_free(data, TRUE);
654 analog.encoding = &encoding;
655 analog.meaning = &meaning;
658 if (lecroy_waveform_to_analog(data, &analog) != SR_OK)
661 if (analog.num_samples == 0) {
663 g_byte_array_free(data, TRUE);
665 /* No data available, we have to acquire data first. */
666 g_snprintf(command, sizeof(command), "ARM;WAIT;*OPC;C%d:WAVEFORM?", ch->index + 1);
667 sr_scpi_send(sdi->conn, command);
669 state->sample_rate = 0;
672 /* Update sample rate if needed. */
673 if (state->sample_rate == 0)
674 if (lecroy_xstream_update_sample_rate(sdi, analog.num_samples) != SR_OK) {
676 g_byte_array_free(data, TRUE);
682 * Send "frame begin" packet upon reception of data for the
683 * first enabled channel.
685 if (devc->current_channel == devc->enabled_channels)
686 std_session_send_df_frame_begin(sdi);
688 meaning.channels = g_slist_append(NULL, ch);
689 packet.payload = &analog;
690 packet.type = SR_DF_ANALOG;
691 sr_session_send(sdi, &packet);
693 g_byte_array_free(data, TRUE);
696 g_slist_free(meaning.channels);
700 * Advance to the next enabled channel. When data for all enabled
701 * channels was received, then flush potentially queued logic data,
702 * and send the "frame end" packet.
704 if (devc->current_channel->next) {
705 devc->current_channel = devc->current_channel->next;
706 lecroy_xstream_request_data(sdi);
710 std_session_send_df_frame_end(sdi);
713 * End of frame was reached. Stop acquisition after the specified
714 * number of frames, or continue reception by starting over at
715 * the first enabled channel.
718 if (devc->frame_limit && (devc->num_frames == devc->frame_limit)) {
719 sr_dev_acquisition_stop(sdi);
721 devc->current_channel = devc->enabled_channels;
723 /* Wait for trigger, then begin fetching data. */
724 g_snprintf(command, sizeof(command), "ARM;WAIT;*OPC");
725 sr_scpi_send(sdi->conn, command);
727 lecroy_xstream_request_data(sdi);