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/>.
25 static struct sr_dev_driver lecroy_xstream_driver_info;
27 static const char *manufacturers[] = {
31 static const uint32_t scanopts[] = {
35 static const uint32_t drvopts[] = {
39 static const uint32_t devopts[] = {
40 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
41 SR_CONF_SAMPLERATE | SR_CONF_GET,
42 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43 SR_CONF_NUM_HDIV | SR_CONF_GET,
44 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
45 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
49 static const uint32_t analog_devopts[] = {
50 SR_CONF_NUM_VDIV | SR_CONF_GET,
51 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
52 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
55 static int check_manufacturer(const char *manufacturer)
59 for (i = 0; i < ARRAY_SIZE(manufacturers); i++)
60 if (!strcmp(manufacturer, manufacturers[i]))
66 static struct sr_dev_inst *probe_serial_device(struct sr_scpi_dev_inst *scpi)
68 struct sr_dev_inst *sdi;
69 struct dev_context *devc;
70 struct sr_scpi_hw_info *hw_info;
76 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
77 sr_info("Couldn't get IDN response.");
81 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
84 sdi = g_malloc0(sizeof(struct sr_dev_inst));
85 sdi->vendor = g_strdup(hw_info->manufacturer);
86 sdi->model = g_strdup(hw_info->model);
87 sdi->version = g_strdup(hw_info->firmware_version);
88 sdi->serial_num = g_strdup(hw_info->serial_number);
89 sdi->driver = &lecroy_xstream_driver_info;
90 sdi->inst_type = SR_INST_SCPI;
93 sr_scpi_hw_info_free(hw_info);
96 devc = g_malloc0(sizeof(struct dev_context));
100 if (lecroy_xstream_init_device(sdi) != SR_OK)
106 sr_scpi_hw_info_free(hw_info);
107 sr_dev_inst_free(sdi);
113 static GSList *scan(struct sr_dev_driver *di, GSList *options)
115 return sr_scpi_scan(di->context, options, probe_serial_device);
118 static void clear_helper(void *priv)
120 struct dev_context *devc;
124 lecroy_xstream_state_free(devc->model_state);
126 g_free(devc->analog_groups);
131 static int dev_clear(const struct sr_dev_driver *di)
133 return std_dev_clear_with_callback(di, clear_helper);
136 static int dev_open(struct sr_dev_inst *sdi)
138 if (sr_scpi_open(sdi->conn) != SR_OK)
141 if (lecroy_xstream_state_get(sdi) != SR_OK)
147 static int dev_close(struct sr_dev_inst *sdi)
149 return sr_scpi_close(sdi->conn);
152 static int config_get(uint32_t key, GVariant **data,
153 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
157 struct dev_context *devc;
158 const struct scope_config *model;
159 struct scope_state *state;
167 model = devc->model_config;
168 state = devc->model_state;
172 case SR_CONF_NUM_HDIV:
173 *data = g_variant_new_int32(model->num_xdivs);
176 case SR_CONF_TIMEBASE:
177 *data = g_variant_new("(tt)",
178 model->timebases[state->timebase].p,
179 model->timebases[state->timebase].q);
182 case SR_CONF_NUM_VDIV:
183 for (i = 0; i < model->analog_channels; i++) {
184 if (cg != devc->analog_groups[i])
186 *data = g_variant_new_int32(model->num_ydivs);
191 for (i = 0; i < model->analog_channels; i++) {
192 if (cg != devc->analog_groups[i])
194 *data = g_variant_new("(tt)",
195 model->vdivs[state->analog_channels[i].vdiv].p,
196 model->vdivs[state->analog_channels[i].vdiv].q);
200 case SR_CONF_TRIGGER_SOURCE:
201 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
204 case SR_CONF_TRIGGER_SLOPE:
205 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
208 case SR_CONF_HORIZ_TRIGGERPOS:
209 *data = g_variant_new_double(state->horiz_triggerpos);
212 case SR_CONF_COUPLING:
213 for (i = 0; i < model->analog_channels; i++) {
214 if (cg != devc->analog_groups[i])
216 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
220 case SR_CONF_SAMPLERATE:
221 *data = g_variant_new_uint64(state->sample_rate);
224 case SR_CONF_ENABLED:
225 *data = g_variant_new_boolean(FALSE);
235 static GVariant *build_tuples(const struct sr_rational *array, unsigned int n)
238 GVariant *rational[2];
241 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
243 for (i = 0; i < n; i++) {
244 rational[0] = g_variant_new_uint64(array[i].p);
245 rational[1] = g_variant_new_uint64(array[i].q);
247 /* FIXME: Valgrind reports a memory leak here. */
248 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
251 return g_variant_builder_end(&gvb);
254 static int config_set(uint32_t key, GVariant *data,
255 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
259 char command[MAX_COMMAND_SIZE];
260 struct dev_context *devc;
261 const struct scope_config *model;
262 struct scope_state *state;
267 gboolean update_sample_rate;
274 model = devc->model_config;
275 state = devc->model_state;
276 update_sample_rate = FALSE;
281 case SR_CONF_LIMIT_FRAMES:
282 devc->frame_limit = g_variant_get_uint64(data);
285 case SR_CONF_TRIGGER_SOURCE:
286 tmp = g_variant_get_string(data, NULL);
287 for (i = 0; (*model->trigger_sources)[i]; i++) {
288 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
290 state->trigger_source = i;
291 g_snprintf(command, sizeof(command),
292 "SET TRIGGER SOURCE %s",
293 (*model->trigger_sources)[i]);
295 ret = sr_scpi_send(sdi->conn, command);
300 g_variant_get(data, "(tt)", &p, &q);
302 for (i = 0; i < model->num_vdivs; i++) {
303 if (p != model->vdivs[i].p || q != model->vdivs[i].q)
305 for (j = 1; j <= model->analog_channels; j++) {
306 if (cg != devc->analog_groups[j - 1])
308 state->analog_channels[j - 1].vdiv = i;
309 g_snprintf(command, sizeof(command),
310 "C%d:VDIV %E", j, (float)p/q);
312 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
313 sr_scpi_get_opc(sdi->conn) != SR_OK)
323 case SR_CONF_TIMEBASE:
324 g_variant_get(data, "(tt)", &p, &q);
326 for (i = 0; i < model->num_timebases; i++) {
327 if (p != model->timebases[i].p ||
328 q != model->timebases[i].q)
331 g_snprintf(command, sizeof(command),
332 "TIME_DIV %E", (float)p/q);
334 ret = sr_scpi_send(sdi->conn, command);
335 update_sample_rate = TRUE;
339 case SR_CONF_HORIZ_TRIGGERPOS:
340 tmp_d = g_variant_get_double(data);
342 if (tmp_d < 0.0 || tmp_d > 1.0)
345 state->horiz_triggerpos = tmp_d;
346 tmp_d = -(tmp_d - 0.5) *
347 ((double)model->timebases[state->timebase].p /
348 model->timebases[state->timebase].q)
351 g_snprintf(command, sizeof(command), "TRIG POS %e S", tmp_d);
353 ret = sr_scpi_send(sdi->conn, command);
355 case SR_CONF_TRIGGER_SLOPE:
356 tmp = g_variant_get_string(data, NULL);
357 for (i = 0; (*model->trigger_slopes)[i]; i++) {
358 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
360 state->trigger_slope = i;
361 g_snprintf(command, sizeof(command),
362 "SET TRIGGER SLOPE %s",
363 (*model->trigger_slopes)[i]);
365 ret = sr_scpi_send(sdi->conn, command);
369 case SR_CONF_COUPLING:
370 tmp = g_variant_get_string(data, NULL);
372 for (i = 0; (*model->coupling_options)[i]; i++) {
373 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
375 for (j = 1; j <= model->analog_channels; j++) {
376 if (cg != devc->analog_groups[j - 1])
378 state->analog_channels[j - 1].coupling = i;
380 g_snprintf(command, sizeof(command),
381 "C%d:COUPLING %s", j, tmp);
383 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
384 sr_scpi_get_opc(sdi->conn) != SR_OK)
399 ret = sr_scpi_get_opc(sdi->conn);
401 if (ret == SR_OK && update_sample_rate)
402 ret = lecroy_xstream_update_sample_rate(sdi);
407 static int config_list(uint32_t key, GVariant **data,
408 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
410 struct dev_context *devc = NULL;
411 const struct scope_config *model = NULL;
415 /* SR_CONF_SCAN_OPTIONS is always valid, regardless of sdi or channel group. */
416 if (key == SR_CONF_SCAN_OPTIONS) {
417 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
418 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
422 /* If sdi is NULL, nothing except SR_CONF_DEVICE_OPTIONS can be provided. */
423 if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
424 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
425 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
429 /* Every other option requires a valid device instance. */
434 model = devc->model_config;
437 case SR_CONF_DEVICE_OPTIONS:
439 /* If cg is NULL, only the SR_CONF_DEVICE_OPTIONS that are not
440 * specific to a channel group must be returned. */
441 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
442 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
445 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
446 analog_devopts, ARRAY_SIZE(analog_devopts),
449 case SR_CONF_COUPLING:
450 *data = g_variant_new_strv(*model->coupling_options,
451 g_strv_length((char **)*model->coupling_options));
453 case SR_CONF_TRIGGER_SOURCE:
456 *data = g_variant_new_strv(*model->trigger_sources,
457 g_strv_length((char **)*model->trigger_sources));
459 case SR_CONF_TRIGGER_SLOPE:
462 *data = g_variant_new_strv(*model->trigger_slopes,
463 g_strv_length((char **)*model->trigger_slopes));
465 case SR_CONF_TIMEBASE:
468 *data = build_tuples(model->timebases, model->num_timebases);
473 *data = build_tuples(model->vdivs, model->num_vdivs);
481 SR_PRIV int lecroy_xstream_request_data(const struct sr_dev_inst *sdi)
483 char command[MAX_COMMAND_SIZE];
484 struct sr_channel *ch;
485 struct dev_context *devc;
489 ch = devc->current_channel->data;
491 if (ch->type != SR_CHANNEL_ANALOG)
494 g_snprintf(command, sizeof(command),
495 "COMM_FORMAT DEF9,WORD,BIN;C%d:WAVEFORM?", ch->index + 1);
496 return sr_scpi_send(sdi->conn, command);
499 static int setup_channels(const struct sr_dev_inst *sdi)
502 gboolean setup_changed;
503 char command[MAX_COMMAND_SIZE];
504 struct scope_state *state;
505 struct sr_channel *ch;
506 struct dev_context *devc;
507 struct sr_scpi_dev_inst *scpi;
511 state = devc->model_state;
512 setup_changed = FALSE;
514 for (l = sdi->channels; l; l = l->next) {
517 case SR_CHANNEL_ANALOG:
518 if (ch->enabled == state->analog_channels[ch->index].state)
520 g_snprintf(command, sizeof(command), "C%d:TRACE %s",
521 ch->index + 1, ch->enabled ? "ON" : "OFF");
523 if (sr_scpi_send(scpi, command) != SR_OK)
525 state->analog_channels[ch->index].state = ch->enabled;
526 setup_changed = TRUE;
533 if (setup_changed && lecroy_xstream_update_sample_rate(sdi) != SR_OK)
539 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
542 struct sr_channel *ch;
543 struct dev_context *devc;
545 struct sr_scpi_dev_inst *scpi;
549 /* Preset empty results. */
550 g_slist_free(devc->enabled_channels);
551 devc->enabled_channels = NULL;
554 * Contruct the list of enabled channels. Determine the highest
555 * number of digital pods involved in the acquisition.
558 for (l = sdi->channels; l; l = l->next) {
562 /* Only add a single digital channel per group (pod). */
563 devc->enabled_channels = g_slist_append(
564 devc->enabled_channels, ch);
567 if (!devc->enabled_channels)
571 * Configure the analog channels and the
572 * corresponding digital pods.
574 if (setup_channels(sdi) != SR_OK) {
575 sr_err("Failed to setup channel configuration!");
581 * Start acquisition on the first enabled channel. The
582 * receive routine will continue driving the acquisition.
584 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
585 lecroy_xstream_receive_data, (void *)sdi);
587 std_session_send_df_header(sdi);
589 devc->current_channel = devc->enabled_channels;
591 return lecroy_xstream_request_data(sdi);
594 g_slist_free(devc->enabled_channels);
595 devc->enabled_channels = NULL;
600 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
602 struct dev_context *devc;
603 struct sr_scpi_dev_inst *scpi;
605 std_session_send_df_end(sdi);
609 devc->num_frames = 0;
610 g_slist_free(devc->enabled_channels);
611 devc->enabled_channels = NULL;
613 sr_scpi_source_remove(sdi->session, scpi);
618 static struct sr_dev_driver lecroy_xstream_driver_info = {
619 .name = "lecroy-xstream",
620 .longname = "LeCroy X-Stream",
623 .cleanup = std_cleanup,
625 .dev_list = std_dev_list,
626 .dev_clear = dev_clear,
627 .config_get = config_get,
628 .config_set = config_set,
629 .config_list = config_list,
630 .dev_open = dev_open,
631 .dev_close = dev_close,
632 .dev_acquisition_start = dev_acquisition_start,
633 .dev_acquisition_stop = dev_acquisition_stop,
636 SR_REGISTER_DEV_DRIVER(lecroy_xstream_driver_info);