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 devopts_cg_analog[] = {
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 struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
57 struct sr_dev_inst *sdi;
58 struct dev_context *devc;
59 struct sr_scpi_hw_info *hw_info;
65 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
66 sr_info("Couldn't get IDN response.");
70 if (std_str_idx_s(hw_info->manufacturer, ARRAY_AND_SIZE(manufacturers)) < 0)
73 sdi = g_malloc0(sizeof(struct sr_dev_inst));
74 sdi->vendor = g_strdup(hw_info->manufacturer);
75 sdi->model = g_strdup(hw_info->model);
76 sdi->version = g_strdup(hw_info->firmware_version);
77 sdi->serial_num = g_strdup(hw_info->serial_number);
78 sdi->driver = &lecroy_xstream_driver_info;
79 sdi->inst_type = SR_INST_SCPI;
82 sr_scpi_hw_info_free(hw_info);
85 devc = g_malloc0(sizeof(struct dev_context));
89 if (lecroy_xstream_init_device(sdi) != SR_OK)
95 sr_scpi_hw_info_free(hw_info);
96 sr_dev_inst_free(sdi);
102 static GSList *scan(struct sr_dev_driver *di, GSList *options)
104 return sr_scpi_scan(di->context, options, probe_device);
107 static void clear_helper(struct dev_context *devc)
109 lecroy_xstream_state_free(devc->model_state);
110 g_free(devc->analog_groups);
113 static int dev_clear(const struct sr_dev_driver *di)
115 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
118 static int dev_open(struct sr_dev_inst *sdi)
120 if (sr_scpi_open(sdi->conn) != SR_OK)
123 if (lecroy_xstream_state_get(sdi) != SR_OK)
129 static int dev_close(struct sr_dev_inst *sdi)
131 return sr_scpi_close(sdi->conn);
134 static int config_get(uint32_t key, GVariant **data,
135 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
138 struct dev_context *devc;
139 const struct scope_config *model;
140 struct scope_state *state;
147 model = devc->model_config;
148 state = devc->model_state;
152 case SR_CONF_NUM_HDIV:
153 *data = g_variant_new_int32(model->num_xdivs);
155 case SR_CONF_TIMEBASE:
156 *data = g_variant_new("(tt)",
157 (*model->timebases)[state->timebase][0],
158 (*model->timebases)[state->timebase][1]);
160 case SR_CONF_NUM_VDIV:
161 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) < 0)
163 *data = g_variant_new_int32(model->num_ydivs);
166 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
168 *data = g_variant_new("(tt)",
169 (*model->vdivs)[state->analog_channels[idx].vdiv][0],
170 (*model->vdivs)[state->analog_channels[idx].vdiv][1]);
172 case SR_CONF_TRIGGER_SOURCE:
173 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
175 case SR_CONF_TRIGGER_SLOPE:
176 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
178 case SR_CONF_HORIZ_TRIGGERPOS:
179 *data = g_variant_new_double(state->horiz_triggerpos);
181 case SR_CONF_COUPLING:
182 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
184 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]);
186 case SR_CONF_SAMPLERATE:
187 *data = g_variant_new_uint64(state->sample_rate);
189 case SR_CONF_ENABLED:
190 *data = g_variant_new_boolean(FALSE);
199 static int config_set(uint32_t key, GVariant *data,
200 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
203 char command[MAX_COMMAND_SIZE];
204 struct dev_context *devc;
205 const struct scope_config *model;
206 struct scope_state *state;
208 gboolean update_sample_rate;
215 model = devc->model_config;
216 state = devc->model_state;
217 update_sample_rate = FALSE;
222 case SR_CONF_LIMIT_FRAMES:
223 devc->frame_limit = g_variant_get_uint64(data);
226 case SR_CONF_TRIGGER_SOURCE:
227 if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
229 state->trigger_source = idx;
230 g_snprintf(command, sizeof(command),
231 "SET TRIGGER SOURCE %s", (*model->trigger_sources)[idx]);
232 ret = sr_scpi_send(sdi->conn, command);
235 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
237 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
239 state->analog_channels[j].vdiv = idx;
240 g_snprintf(command, sizeof(command),
241 "C%d:VDIV %E", j + 1, (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
242 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
243 sr_scpi_get_opc(sdi->conn) != SR_OK)
247 case SR_CONF_TIMEBASE:
248 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
250 state->timebase = idx;
251 g_snprintf(command, sizeof(command),
252 "TIME_DIV %E", (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
253 ret = sr_scpi_send(sdi->conn, command);
254 update_sample_rate = TRUE;
256 case SR_CONF_HORIZ_TRIGGERPOS:
257 tmp_d = g_variant_get_double(data);
259 if (tmp_d < 0.0 || tmp_d > 1.0)
262 state->horiz_triggerpos = tmp_d;
263 tmp_d = -(tmp_d - 0.5) *
264 ((double)(*model->timebases)[state->timebase][0] /
265 (*model->timebases)[state->timebase][1])
268 g_snprintf(command, sizeof(command), "TRIG POS %e S", tmp_d);
270 ret = sr_scpi_send(sdi->conn, command);
272 case SR_CONF_TRIGGER_SLOPE:
273 if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0)
275 state->trigger_slope = idx;
276 g_snprintf(command, sizeof(command),
277 "SET TRIGGER SLOPE %s", (*model->trigger_slopes)[idx]);
278 ret = sr_scpi_send(sdi->conn, command);
280 case SR_CONF_COUPLING:
281 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
283 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
285 state->analog_channels[j].coupling = idx;
286 g_snprintf(command, sizeof(command), "C%d:COUPLING %s",
287 j + 1, (*model->coupling_options)[idx]);
288 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
289 sr_scpi_get_opc(sdi->conn) != SR_OK)
299 ret = sr_scpi_get_opc(sdi->conn);
301 if (ret == SR_OK && update_sample_rate)
302 ret = lecroy_xstream_update_sample_rate(sdi);
307 static int config_list(uint32_t key, GVariant **data,
308 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
310 struct dev_context *devc;
311 const struct scope_config *model;
313 devc = (sdi) ? sdi->priv : NULL;
314 model = (devc) ? devc->model_config : NULL;
317 case SR_CONF_SCAN_OPTIONS:
318 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, NULL, NULL);
319 case SR_CONF_DEVICE_OPTIONS:
321 return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts);
322 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog));
324 case SR_CONF_COUPLING:
325 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
327 case SR_CONF_TRIGGER_SOURCE:
330 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
332 case SR_CONF_TRIGGER_SLOPE:
335 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
337 case SR_CONF_TIMEBASE:
340 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
345 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
354 SR_PRIV int lecroy_xstream_request_data(const struct sr_dev_inst *sdi)
356 char command[MAX_COMMAND_SIZE];
357 struct sr_channel *ch;
358 struct dev_context *devc;
362 ch = devc->current_channel->data;
364 if (ch->type != SR_CHANNEL_ANALOG)
367 g_snprintf(command, sizeof(command),
368 "COMM_FORMAT DEF9,WORD,BIN;C%d:WAVEFORM?", ch->index + 1);
369 return sr_scpi_send(sdi->conn, command);
372 static int setup_channels(const struct sr_dev_inst *sdi)
375 gboolean setup_changed;
376 char command[MAX_COMMAND_SIZE];
377 struct scope_state *state;
378 struct sr_channel *ch;
379 struct dev_context *devc;
380 struct sr_scpi_dev_inst *scpi;
384 state = devc->model_state;
385 setup_changed = FALSE;
387 for (l = sdi->channels; l; l = l->next) {
390 case SR_CHANNEL_ANALOG:
391 if (ch->enabled == state->analog_channels[ch->index].state)
393 g_snprintf(command, sizeof(command), "C%d:TRACE %s",
394 ch->index + 1, ch->enabled ? "ON" : "OFF");
396 if (sr_scpi_send(scpi, command) != SR_OK)
398 state->analog_channels[ch->index].state = ch->enabled;
399 setup_changed = TRUE;
406 if (setup_changed && lecroy_xstream_update_sample_rate(sdi) != SR_OK)
412 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
415 struct sr_channel *ch;
416 struct dev_context *devc;
418 struct sr_scpi_dev_inst *scpi;
423 /* Preset empty results. */
424 g_slist_free(devc->enabled_channels);
425 devc->enabled_channels = NULL;
428 * Contruct the list of enabled channels. Determine the highest
429 * number of digital pods involved in the acquisition.
432 for (l = sdi->channels; l; l = l->next) {
436 /* Only add a single digital channel per group (pod). */
437 devc->enabled_channels = g_slist_append(
438 devc->enabled_channels, ch);
441 if (!devc->enabled_channels)
445 * Configure the analog channels and the
446 * corresponding digital pods.
448 if (setup_channels(sdi) != SR_OK) {
449 sr_err("Failed to setup channel configuration!");
455 * Start acquisition on the first enabled channel. The
456 * receive routine will continue driving the acquisition.
458 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
459 lecroy_xstream_receive_data, (void *)sdi);
461 std_session_send_df_header(sdi);
463 devc->current_channel = devc->enabled_channels;
465 return lecroy_xstream_request_data(sdi);
468 g_slist_free(devc->enabled_channels);
469 devc->enabled_channels = NULL;
474 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
476 struct dev_context *devc;
477 struct sr_scpi_dev_inst *scpi;
479 std_session_send_df_end(sdi);
483 devc->num_frames = 0;
484 g_slist_free(devc->enabled_channels);
485 devc->enabled_channels = NULL;
487 sr_scpi_source_remove(sdi->session, scpi);
492 static struct sr_dev_driver lecroy_xstream_driver_info = {
493 .name = "lecroy-xstream",
494 .longname = "LeCroy X-Stream",
497 .cleanup = std_cleanup,
499 .dev_list = std_dev_list,
500 .dev_clear = dev_clear,
501 .config_get = config_get,
502 .config_set = config_set,
503 .config_list = config_list,
504 .dev_open = dev_open,
505 .dev_close = dev_close,
506 .dev_acquisition_start = dev_acquisition_start,
507 .dev_acquisition_stop = dev_acquisition_stop,
510 SR_REGISTER_DEV_DRIVER(lecroy_xstream_driver_info);