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);
129 static int dev_clear(const struct sr_dev_driver *di)
131 return std_dev_clear_with_callback(di, clear_helper);
134 static int dev_open(struct sr_dev_inst *sdi)
136 if (sr_scpi_open(sdi->conn) != SR_OK)
139 if (lecroy_xstream_state_get(sdi) != SR_OK)
145 static int dev_close(struct sr_dev_inst *sdi)
147 return sr_scpi_close(sdi->conn);
150 static int config_get(uint32_t key, GVariant **data,
151 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
155 struct dev_context *devc;
156 const struct scope_config *model;
157 struct scope_state *state;
165 model = devc->model_config;
166 state = devc->model_state;
170 case SR_CONF_NUM_HDIV:
171 *data = g_variant_new_int32(model->num_xdivs);
174 case SR_CONF_TIMEBASE:
175 *data = g_variant_new("(tt)",
176 model->timebases[state->timebase].p,
177 model->timebases[state->timebase].q);
180 case SR_CONF_NUM_VDIV:
181 for (i = 0; i < model->analog_channels; i++) {
182 if (cg != devc->analog_groups[i])
184 *data = g_variant_new_int32(model->num_ydivs);
189 for (i = 0; i < model->analog_channels; i++) {
190 if (cg != devc->analog_groups[i])
192 *data = g_variant_new("(tt)",
193 model->vdivs[state->analog_channels[i].vdiv].p,
194 model->vdivs[state->analog_channels[i].vdiv].q);
198 case SR_CONF_TRIGGER_SOURCE:
199 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
202 case SR_CONF_TRIGGER_SLOPE:
203 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
206 case SR_CONF_HORIZ_TRIGGERPOS:
207 *data = g_variant_new_double(state->horiz_triggerpos);
210 case SR_CONF_COUPLING:
211 for (i = 0; i < model->analog_channels; i++) {
212 if (cg != devc->analog_groups[i])
214 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
218 case SR_CONF_SAMPLERATE:
219 *data = g_variant_new_uint64(state->sample_rate);
222 case SR_CONF_ENABLED:
223 *data = g_variant_new_boolean(FALSE);
233 static GVariant *build_tuples(const struct sr_rational *array, unsigned int n)
236 GVariant *rational[2];
239 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
241 for (i = 0; i < n; i++) {
242 rational[0] = g_variant_new_uint64(array[i].p);
243 rational[1] = g_variant_new_uint64(array[i].q);
245 /* FIXME: Valgrind reports a memory leak here. */
246 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
249 return g_variant_builder_end(&gvb);
252 static int config_set(uint32_t key, GVariant *data,
253 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
257 char command[MAX_COMMAND_SIZE];
258 struct dev_context *devc;
259 const struct scope_config *model;
260 struct scope_state *state;
265 gboolean update_sample_rate;
272 model = devc->model_config;
273 state = devc->model_state;
274 update_sample_rate = FALSE;
279 case SR_CONF_LIMIT_FRAMES:
280 devc->frame_limit = g_variant_get_uint64(data);
283 case SR_CONF_TRIGGER_SOURCE:
284 tmp = g_variant_get_string(data, NULL);
285 for (i = 0; (*model->trigger_sources)[i]; i++) {
286 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
288 state->trigger_source = i;
289 g_snprintf(command, sizeof(command),
290 "SET TRIGGER SOURCE %s",
291 (*model->trigger_sources)[i]);
293 ret = sr_scpi_send(sdi->conn, command);
298 g_variant_get(data, "(tt)", &p, &q);
300 for (i = 0; i < model->num_vdivs; i++) {
301 if (p != model->vdivs[i].p || q != model->vdivs[i].q)
303 for (j = 1; j <= model->analog_channels; j++) {
304 if (cg != devc->analog_groups[j - 1])
306 state->analog_channels[j - 1].vdiv = i;
307 g_snprintf(command, sizeof(command),
308 "C%d:VDIV %E", j, (float)p/q);
310 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
311 sr_scpi_get_opc(sdi->conn) != SR_OK)
321 case SR_CONF_TIMEBASE:
322 g_variant_get(data, "(tt)", &p, &q);
324 for (i = 0; i < model->num_timebases; i++) {
325 if (p != model->timebases[i].p ||
326 q != model->timebases[i].q)
329 g_snprintf(command, sizeof(command),
330 "TIME_DIV %E", (float)p/q);
332 ret = sr_scpi_send(sdi->conn, command);
333 update_sample_rate = TRUE;
337 case SR_CONF_HORIZ_TRIGGERPOS:
338 tmp_d = g_variant_get_double(data);
340 if (tmp_d < 0.0 || tmp_d > 1.0)
343 state->horiz_triggerpos = tmp_d;
344 tmp_d = -(tmp_d - 0.5) *
345 ((double)model->timebases[state->timebase].p /
346 model->timebases[state->timebase].q)
349 g_snprintf(command, sizeof(command), "TRIG POS %e S", tmp_d);
351 ret = sr_scpi_send(sdi->conn, command);
353 case SR_CONF_TRIGGER_SLOPE:
354 tmp = g_variant_get_string(data, NULL);
355 for (i = 0; (*model->trigger_slopes)[i]; i++) {
356 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
358 state->trigger_slope = i;
359 g_snprintf(command, sizeof(command),
360 "SET TRIGGER SLOPE %s",
361 (*model->trigger_slopes)[i]);
363 ret = sr_scpi_send(sdi->conn, command);
367 case SR_CONF_COUPLING:
368 tmp = g_variant_get_string(data, NULL);
370 for (i = 0; (*model->coupling_options)[i]; i++) {
371 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
373 for (j = 1; j <= model->analog_channels; j++) {
374 if (cg != devc->analog_groups[j - 1])
376 state->analog_channels[j - 1].coupling = i;
378 g_snprintf(command, sizeof(command),
379 "C%d:COUPLING %s", j, tmp);
381 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
382 sr_scpi_get_opc(sdi->conn) != SR_OK)
397 ret = sr_scpi_get_opc(sdi->conn);
399 if (ret == SR_OK && update_sample_rate)
400 ret = lecroy_xstream_update_sample_rate(sdi);
405 static int config_list(uint32_t key, GVariant **data,
406 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
408 struct dev_context *devc = NULL;
409 const struct scope_config *model = NULL;
413 /* SR_CONF_SCAN_OPTIONS is always valid, regardless of sdi or channel group. */
414 if (key == SR_CONF_SCAN_OPTIONS) {
415 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
416 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
420 /* If sdi is NULL, nothing except SR_CONF_DEVICE_OPTIONS can be provided. */
421 if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
422 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
423 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
427 /* Every other option requires a valid device instance. */
432 model = devc->model_config;
435 case SR_CONF_DEVICE_OPTIONS:
437 /* If cg is NULL, only the SR_CONF_DEVICE_OPTIONS that are not
438 * specific to a channel group must be returned. */
439 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
440 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
443 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
444 analog_devopts, ARRAY_SIZE(analog_devopts),
447 case SR_CONF_COUPLING:
448 *data = g_variant_new_strv(*model->coupling_options,
449 g_strv_length((char **)*model->coupling_options));
451 case SR_CONF_TRIGGER_SOURCE:
454 *data = g_variant_new_strv(*model->trigger_sources,
455 g_strv_length((char **)*model->trigger_sources));
457 case SR_CONF_TRIGGER_SLOPE:
460 *data = g_variant_new_strv(*model->trigger_slopes,
461 g_strv_length((char **)*model->trigger_slopes));
463 case SR_CONF_TIMEBASE:
466 *data = build_tuples(model->timebases, model->num_timebases);
471 *data = build_tuples(model->vdivs, model->num_vdivs);
479 SR_PRIV int lecroy_xstream_request_data(const struct sr_dev_inst *sdi)
481 char command[MAX_COMMAND_SIZE];
482 struct sr_channel *ch;
483 struct dev_context *devc;
487 ch = devc->current_channel->data;
489 if (ch->type != SR_CHANNEL_ANALOG)
492 g_snprintf(command, sizeof(command),
493 "COMM_FORMAT DEF9,WORD,BIN;C%d:WAVEFORM?", ch->index + 1);
494 return sr_scpi_send(sdi->conn, command);
497 static int setup_channels(const struct sr_dev_inst *sdi)
500 gboolean setup_changed;
501 char command[MAX_COMMAND_SIZE];
502 struct scope_state *state;
503 struct sr_channel *ch;
504 struct dev_context *devc;
505 struct sr_scpi_dev_inst *scpi;
509 state = devc->model_state;
510 setup_changed = FALSE;
512 for (l = sdi->channels; l; l = l->next) {
515 case SR_CHANNEL_ANALOG:
516 if (ch->enabled == state->analog_channels[ch->index].state)
518 g_snprintf(command, sizeof(command), "C%d:TRACE %s",
519 ch->index + 1, ch->enabled ? "ON" : "OFF");
521 if (sr_scpi_send(scpi, command) != SR_OK)
523 state->analog_channels[ch->index].state = ch->enabled;
524 setup_changed = TRUE;
531 if (setup_changed && lecroy_xstream_update_sample_rate(sdi) != SR_OK)
537 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
540 struct sr_channel *ch;
541 struct dev_context *devc;
543 struct sr_scpi_dev_inst *scpi;
547 /* Preset empty results. */
548 g_slist_free(devc->enabled_channels);
549 devc->enabled_channels = NULL;
552 * Contruct the list of enabled channels. Determine the highest
553 * number of digital pods involved in the acquisition.
556 for (l = sdi->channels; l; l = l->next) {
560 /* Only add a single digital channel per group (pod). */
561 devc->enabled_channels = g_slist_append(
562 devc->enabled_channels, ch);
565 if (!devc->enabled_channels)
569 * Configure the analog channels and the
570 * corresponding digital pods.
572 if (setup_channels(sdi) != SR_OK) {
573 sr_err("Failed to setup channel configuration!");
579 * Start acquisition on the first enabled channel. The
580 * receive routine will continue driving the acquisition.
582 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
583 lecroy_xstream_receive_data, (void *)sdi);
585 std_session_send_df_header(sdi);
587 devc->current_channel = devc->enabled_channels;
589 return lecroy_xstream_request_data(sdi);
592 g_slist_free(devc->enabled_channels);
593 devc->enabled_channels = NULL;
598 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
600 struct dev_context *devc;
601 struct sr_scpi_dev_inst *scpi;
603 std_session_send_df_end(sdi);
607 devc->num_frames = 0;
608 g_slist_free(devc->enabled_channels);
609 devc->enabled_channels = NULL;
611 sr_scpi_source_remove(sdi->session, scpi);
616 static struct sr_dev_driver lecroy_xstream_driver_info = {
617 .name = "lecroy-xstream",
618 .longname = "LeCroy X-Stream",
621 .cleanup = std_cleanup,
623 .dev_list = std_dev_list,
624 .dev_clear = dev_clear,
625 .config_get = config_get,
626 .config_set = config_set,
627 .config_list = config_list,
628 .dev_open = dev_open,
629 .dev_close = dev_close,
630 .dev_acquisition_start = dev_acquisition_start,
631 .dev_acquisition_stop = dev_acquisition_stop,
634 SR_REGISTER_DEV_DRIVER(lecroy_xstream_driver_info);