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 int check_manufacturer(const char *manufacturer)
39 for (i = 0; i < ARRAY_SIZE(manufacturers); i++)
40 if (!strcmp(manufacturer, manufacturers[i]))
46 static struct sr_dev_inst *probe_serial_device(struct sr_scpi_dev_inst *scpi)
48 struct sr_dev_inst *sdi;
49 struct dev_context *devc;
50 struct sr_scpi_hw_info *hw_info;
56 sr_scpi_send(scpi, "COMM_HEADER OFF,WORD,BIN");
57 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
58 sr_info("Couldn't get IDN response.");
62 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
65 sdi = g_malloc0(sizeof(struct sr_dev_inst));
66 sdi->vendor = g_strdup(hw_info->manufacturer);
67 sdi->model = g_strdup(hw_info->model);
68 sdi->version = g_strdup(hw_info->firmware_version);
69 sdi->serial_num = g_strdup(hw_info->serial_number);
70 sdi->driver = &lecroy_xstream_driver_info;
71 sdi->inst_type = SR_INST_SCPI;
74 sr_scpi_hw_info_free(hw_info);
77 devc = g_malloc0(sizeof(struct dev_context));
81 if (lecroy_xstream_init_device(sdi) != SR_OK)
87 sr_scpi_hw_info_free(hw_info);
89 sr_dev_inst_free(sdi);
95 static GSList *scan(struct sr_dev_driver *di, GSList *options)
97 return sr_scpi_scan(di->context, options, probe_serial_device);
100 static void clear_helper(void *priv)
102 struct dev_context *devc;
106 lecroy_xstream_state_free(devc->model_state);
108 g_free(devc->analog_groups);
113 static int dev_clear(const struct sr_dev_driver *di)
115 return std_dev_clear(di, clear_helper);
118 static int dev_open(struct sr_dev_inst *sdi)
120 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
123 if (lecroy_xstream_state_get(sdi) != SR_OK)
126 sdi->status = SR_ST_ACTIVE;
131 static int dev_close(struct sr_dev_inst *sdi)
133 if (sdi->status == SR_ST_INACTIVE)
136 sr_scpi_close(sdi->conn);
138 sdi->status = SR_ST_INACTIVE;
143 static int config_get(uint32_t key, GVariant **data,
144 const struct sr_dev_inst *sdi,
145 const struct sr_channel_group *cg)
149 struct dev_context *devc;
150 const struct scope_config *model;
151 struct scope_state *state;
159 model = devc->model_config;
160 state = devc->model_state;
163 case SR_CONF_NUM_HDIV:
164 *data = g_variant_new_int32(model->num_xdivs);
167 case SR_CONF_TIMEBASE:
168 *data = g_variant_new("(tt)",
169 model->timebases[state->timebase].p,
170 model->timebases[state->timebase].q);
173 case SR_CONF_NUM_VDIV:
174 for (i = 0; i < model->analog_channels; i++) {
175 if (cg != devc->analog_groups[i])
177 *data = g_variant_new_int32(model->num_ydivs);
182 for (i = 0; i < model->analog_channels; i++) {
183 if (cg != devc->analog_groups[i])
185 *data = g_variant_new("(tt)",
186 model->vdivs[state->analog_channels[i].vdiv].p,
187 model->vdivs[state->analog_channels[i].vdiv].q);
191 case SR_CONF_TRIGGER_SOURCE:
192 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
195 case SR_CONF_TRIGGER_SLOPE:
196 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
199 case SR_CONF_HORIZ_TRIGGERPOS:
200 *data = g_variant_new_double(state->horiz_triggerpos);
203 case SR_CONF_COUPLING:
205 for (i = 0; i < model->analog_channels; i++) {
206 if (cg != devc->analog_groups[i]) {
209 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
213 case SR_CONF_SAMPLERATE:
214 *data = g_variant_new_uint64(state->sample_rate);
217 case SR_CONF_ENABLED:
218 *data = g_variant_new_boolean(FALSE);
227 static GVariant *build_tuples(const struct sr_rational *array, unsigned int n)
230 GVariant *rational[2];
233 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
235 for (i = 0; i < n; i++) {
236 rational[0] = g_variant_new_uint64(array[i].p);
237 rational[1] = g_variant_new_uint64(array[i].q);
239 /* FIXME: Valgrind reports a memory leak here. */
240 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
243 return g_variant_builder_end(&gvb);
246 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
247 const struct sr_channel_group *cg)
251 char command[MAX_COMMAND_SIZE];
252 struct dev_context *devc;
253 const struct scope_config *model;
254 struct scope_state *state;
259 gboolean update_sample_rate;
266 model = devc->model_config;
267 state = devc->model_state;
268 update_sample_rate = FALSE;
273 case SR_CONF_LIMIT_FRAMES:
274 devc->frame_limit = g_variant_get_uint64(data);
277 case SR_CONF_TRIGGER_SOURCE:
278 tmp = g_variant_get_string(data, NULL);
279 for (i = 0; (*model->trigger_sources)[i]; i++) {
280 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
282 state->trigger_source = i;
283 g_snprintf(command, sizeof(command),
284 "SET TRIGGER SOURCE %s",
285 (*model->trigger_sources)[i]);
287 ret = sr_scpi_send(sdi->conn, command);
292 g_variant_get(data, "(tt)", &p, &q);
294 for (i = 0; i < model->num_vdivs; i++) {
295 if (p != model->vdivs[i].p || q != model->vdivs[i].q)
297 for (j = 1; j <= model->analog_channels; j++) {
298 if (cg != devc->analog_groups[j - 1])
300 state->analog_channels[j - 1].vdiv = i;
301 g_snprintf(command, sizeof(command),
302 "C%d:VDIV %E", j, (float)p/q);
304 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
305 sr_scpi_get_opc(sdi->conn) != SR_OK)
315 case SR_CONF_TIMEBASE:
316 g_variant_get(data, "(tt)", &p, &q);
318 for (i = 0; i < model->num_timebases; i++) {
319 if (p != model->timebases[i].p ||
320 q != model->timebases[i].q)
323 g_snprintf(command, sizeof(command),
324 "TIME_DIV %E", (float)p/q);
326 ret = sr_scpi_send(sdi->conn, command);
327 update_sample_rate = TRUE;
331 case SR_CONF_HORIZ_TRIGGERPOS:
332 tmp_d = g_variant_get_double(data);
334 if (tmp_d < 0.0 || tmp_d > 1.0)
337 state->horiz_triggerpos = tmp_d;
338 tmp_d = -(tmp_d - 0.5) *
339 ((double)model->timebases[state->timebase].p /
340 model->timebases[state->timebase].q)
343 g_snprintf(command, sizeof(command), "TRIG POS %e S", tmp_d);
345 ret = sr_scpi_send(sdi->conn, command);
347 case SR_CONF_TRIGGER_SLOPE:
348 tmp = g_variant_get_string(data, NULL);
349 for (i = 0; (*model->trigger_slopes)[i]; i++) {
350 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
352 state->trigger_slope = i;
353 g_snprintf(command, sizeof(command),
354 "SET TRIGGER SLOPE %s",
355 (*model->trigger_slopes)[i]);
357 ret = sr_scpi_send(sdi->conn, command);
361 case SR_CONF_COUPLING:
363 tmp = g_variant_get_string(data, NULL);
365 for (i = 0; (*model->coupling_options)[i]; i++) {
366 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
368 for (j = 1; j <= model->analog_channels; j++) {
369 if (cg != devc->analog_groups[j - 1])
371 state->analog_channels[j-1].coupling = i;
373 g_snprintf(command, sizeof(command),
374 "C%d:COUPLING %s", j, tmp);
376 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
377 sr_scpi_get_opc(sdi->conn) != SR_OK)
392 ret = sr_scpi_get_opc(sdi->conn);
394 if (ret == SR_OK && update_sample_rate)
395 ret = lecroy_xstream_update_sample_rate(sdi);
400 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
401 const struct sr_channel_group *cg)
403 struct dev_context *devc = NULL;
404 const struct scope_config *model = NULL;
409 model = devc->model_config;
413 case SR_CONF_SCAN_OPTIONS:
414 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
415 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
417 case SR_CONF_DEVICE_OPTIONS:
419 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
425 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
426 model->analog_devopts,
427 model->num_analog_devopts,
430 case SR_CONF_COUPLING:
431 *data = g_variant_new_strv(*model->coupling_options,
432 g_strv_length((char **)*model->coupling_options));
434 case SR_CONF_TRIGGER_SOURCE:
437 *data = g_variant_new_strv(*model->trigger_sources,
438 g_strv_length((char **)*model->trigger_sources));
440 case SR_CONF_TRIGGER_SLOPE:
443 *data = g_variant_new_strv(*model->trigger_slopes,
444 g_strv_length((char **)*model->trigger_slopes));
446 case SR_CONF_TIMEBASE:
449 *data = build_tuples(model->timebases, model->num_timebases);
454 *data = build_tuples(model->vdivs, model->num_vdivs);
462 SR_PRIV int lecroy_xstream_request_data(const struct sr_dev_inst *sdi)
464 char command[MAX_COMMAND_SIZE];
465 struct sr_channel *ch;
466 struct dev_context *devc;
470 ch = devc->current_channel->data;
472 if (ch->type != SR_CHANNEL_ANALOG)
475 g_snprintf(command, sizeof(command),
476 "COMM_FORMAT DEF9,WORD,BIN;C%d:WAVEFORM?", ch->index+1);
477 return sr_scpi_send(sdi->conn, command);
480 static int lecroy_setup_channels(const struct sr_dev_inst *sdi)
483 gboolean setup_changed;
484 char command[MAX_COMMAND_SIZE];
485 struct scope_state *state;
486 struct sr_channel *ch;
487 struct dev_context *devc;
488 struct sr_scpi_dev_inst *scpi;
492 state = devc->model_state;
493 setup_changed = FALSE;
495 for (l = sdi->channels; l; l = l->next) {
498 case SR_CHANNEL_ANALOG:
499 if (ch->enabled == state->analog_channels[ch->index].state)
501 g_snprintf(command, sizeof(command), "C%d:TRACE %s",
502 ch->index+1, ch->enabled ? "ON" : "OFF");
504 if (sr_scpi_send(scpi, command) != SR_OK)
506 state->analog_channels[ch->index].state = ch->enabled;
507 setup_changed = TRUE;
514 if (setup_changed && lecroy_xstream_update_sample_rate(sdi) != SR_OK)
520 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
523 struct sr_channel *ch;
524 struct dev_context *devc;
526 struct sr_scpi_dev_inst *scpi;
528 if (sdi->status != SR_ST_ACTIVE)
529 return SR_ERR_DEV_CLOSED;
533 /* Preset empty results. */
534 g_slist_free(devc->enabled_channels);
535 devc->enabled_channels = NULL;
538 * Contruct the list of enabled channels. Determine the highest
539 * number of digital pods involved in the acquisition.
542 for (l = sdi->channels; l; l = l->next) {
546 /* Only add a single digital channel per group (pod). */
547 devc->enabled_channels = g_slist_append(
548 devc->enabled_channels, ch);
551 if (!devc->enabled_channels)
555 * Configure the analog channels and the
556 * corresponding digital pods.
558 if (lecroy_setup_channels(sdi) != SR_OK) {
559 sr_err("Failed to setup channel configuration!");
565 * Start acquisition on the first enabled channel. The
566 * receive routine will continue driving the acquisition.
568 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
569 lecroy_xstream_receive_data, (void *)sdi);
571 std_session_send_df_header(sdi);
573 devc->current_channel = devc->enabled_channels;
575 return lecroy_xstream_request_data(sdi);
578 g_slist_free(devc->enabled_channels);
579 devc->enabled_channels = NULL;
583 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
585 struct dev_context *devc;
586 struct sr_scpi_dev_inst *scpi;
588 std_session_send_df_end(sdi);
590 if (sdi->status != SR_ST_ACTIVE)
591 return SR_ERR_DEV_CLOSED;
595 devc->num_frames = 0;
596 g_slist_free(devc->enabled_channels);
597 devc->enabled_channels = NULL;
599 sr_scpi_source_remove(sdi->session, scpi);
604 static struct sr_dev_driver lecroy_xstream_driver_info = {
605 .name = "lecroy-xstream",
606 .longname = "LeCroy X-Stream",
609 .cleanup = std_cleanup,
611 .dev_list = std_dev_list,
612 .dev_clear = dev_clear,
613 .config_get = config_get,
614 .config_set = config_set,
615 .config_list = config_list,
616 .dev_open = dev_open,
617 .dev_close = dev_close,
618 .dev_acquisition_start = dev_acquisition_start,
619 .dev_acquisition_stop = dev_acquisition_stop,
622 SR_REGISTER_DEV_DRIVER(lecroy_xstream_driver_info);