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));
88 if (lecroy_xstream_init_device(sdi) != SR_OK)
94 sr_scpi_hw_info_free(hw_info);
95 sr_dev_inst_free(sdi);
101 static GSList *scan(struct sr_dev_driver *di, GSList *options)
103 return sr_scpi_scan(di->context, options, probe_device);
106 static void clear_helper(struct dev_context *devc)
108 lecroy_xstream_state_free(devc->model_state);
109 g_free(devc->analog_groups);
112 static int dev_clear(const struct sr_dev_driver *di)
114 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
117 static int dev_open(struct sr_dev_inst *sdi)
119 if (sr_scpi_open(sdi->conn) != SR_OK)
122 if (lecroy_xstream_state_get(sdi) != SR_OK)
128 static int dev_close(struct sr_dev_inst *sdi)
130 return sr_scpi_close(sdi->conn);
133 static int config_get(uint32_t key, GVariant **data,
134 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
137 struct dev_context *devc;
138 const struct scope_config *model;
139 struct scope_state *state;
146 model = devc->model_config;
147 state = devc->model_state;
151 case SR_CONF_NUM_HDIV:
152 *data = g_variant_new_int32(model->num_xdivs);
154 case SR_CONF_TIMEBASE:
155 *data = g_variant_new("(tt)",
156 (*model->timebases)[state->timebase][0],
157 (*model->timebases)[state->timebase][1]);
159 case SR_CONF_NUM_VDIV:
160 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) < 0)
162 *data = g_variant_new_int32(model->num_ydivs);
165 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
167 *data = g_variant_new("(tt)",
168 (*model->vdivs)[state->analog_channels[idx].vdiv][0],
169 (*model->vdivs)[state->analog_channels[idx].vdiv][1]);
171 case SR_CONF_TRIGGER_SOURCE:
172 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
174 case SR_CONF_TRIGGER_SLOPE:
175 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
177 case SR_CONF_HORIZ_TRIGGERPOS:
178 *data = g_variant_new_double(state->horiz_triggerpos);
180 case SR_CONF_COUPLING:
181 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
183 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]);
185 case SR_CONF_SAMPLERATE:
186 *data = g_variant_new_uint64(state->sample_rate);
188 case SR_CONF_ENABLED:
189 *data = g_variant_new_boolean(FALSE);
198 static int config_set(uint32_t key, GVariant *data,
199 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
202 char command[MAX_COMMAND_SIZE];
203 struct dev_context *devc;
204 const struct scope_config *model;
205 struct scope_state *state;
213 model = devc->model_config;
214 state = devc->model_state;
217 case SR_CONF_LIMIT_FRAMES:
218 devc->frame_limit = g_variant_get_uint64(data);
221 case SR_CONF_TRIGGER_SOURCE:
222 if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
224 state->trigger_source = idx;
225 g_snprintf(command, sizeof(command),
226 "TRIG_SELECT EDGE,SR,%s", (*model->trigger_sources)[idx]);
227 ret = sr_scpi_send(sdi->conn, command);
230 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
232 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
234 state->analog_channels[j].vdiv = idx;
235 g_snprintf(command, sizeof(command),
236 "C%d:VDIV %E", j + 1, (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
237 if (sr_scpi_send(sdi->conn, command) != SR_OK || sr_scpi_get_opc(sdi->conn) != SR_OK)
241 case SR_CONF_TIMEBASE:
242 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
244 state->timebase = idx;
245 g_snprintf(command, sizeof(command),
246 "TIME_DIV %E", (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
247 ret = sr_scpi_send(sdi->conn, command);
249 case SR_CONF_HORIZ_TRIGGERPOS:
250 tmp_d = g_variant_get_double(data);
252 if (tmp_d < 0.0 || tmp_d > 1.0)
255 state->horiz_triggerpos = tmp_d;
256 tmp_d = -(tmp_d - 0.5) *
257 ((double)(*model->timebases)[state->timebase][0] /
258 (*model->timebases)[state->timebase][1])
261 g_snprintf(command, sizeof(command), "TRIG POS %e S", tmp_d);
263 ret = sr_scpi_send(sdi->conn, command);
265 case SR_CONF_TRIGGER_SLOPE:
266 if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0)
268 state->trigger_slope = idx;
269 g_snprintf(command, sizeof(command),
270 "%s:TRIG_SLOPE %s", (*model->trigger_sources)[state->trigger_source],
271 (*model->trigger_slopes)[idx]);
272 ret = sr_scpi_send(sdi->conn, command);
274 case SR_CONF_COUPLING:
275 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
277 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
279 state->analog_channels[j].coupling = idx;
280 g_snprintf(command, sizeof(command), "C%d:COUPLING %s",
281 j + 1, (*model->coupling_options)[idx]);
282 if (sr_scpi_send(sdi->conn, command) != SR_OK || sr_scpi_get_opc(sdi->conn) != SR_OK)
292 ret = sr_scpi_get_opc(sdi->conn);
297 static int config_channel_set(const struct sr_dev_inst *sdi,
298 struct sr_channel *ch, unsigned int changes)
300 /* Currently we only handle SR_CHANNEL_SET_ENABLED. */
301 if (changes != SR_CHANNEL_SET_ENABLED)
304 return lecroy_xstream_channel_state_set(sdi, ch->index, ch->enabled);
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, NO_OPTS, NO_OPTS);
319 case SR_CONF_DEVICE_OPTIONS:
321 return STD_CONFIG_LIST(key, data, sdi, cg, NO_OPTS, drvopts, devopts);
322 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog));
324 case SR_CONF_COUPLING:
327 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
329 case SR_CONF_TRIGGER_SOURCE:
332 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
334 case SR_CONF_TRIGGER_SLOPE:
337 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
339 case SR_CONF_TIMEBASE:
342 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
347 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
356 SR_PRIV int lecroy_xstream_request_data(const struct sr_dev_inst *sdi)
358 char command[MAX_COMMAND_SIZE];
359 struct sr_channel *ch;
360 struct dev_context *devc;
365 * We may be left with an invalid current_channel if acquisition was
366 * already stopped but we are processing the last pending events.
368 if (!devc->current_channel)
371 ch = devc->current_channel->data;
373 if (ch->type != SR_CHANNEL_ANALOG)
376 g_snprintf(command, sizeof(command), "C%d:WAVEFORM?", ch->index + 1);
377 return sr_scpi_send(sdi->conn, command);
380 static int setup_channels(const struct sr_dev_inst *sdi)
383 char command[MAX_COMMAND_SIZE];
384 struct scope_state *state;
385 struct sr_channel *ch;
386 struct dev_context *devc;
387 struct sr_scpi_dev_inst *scpi;
391 state = devc->model_state;
393 for (l = sdi->channels; l; l = l->next) {
396 case SR_CHANNEL_ANALOG:
397 if (ch->enabled == state->analog_channels[ch->index].state)
399 g_snprintf(command, sizeof(command), "C%d:TRACE %s",
400 ch->index + 1, ch->enabled ? "ON" : "OFF");
402 if (sr_scpi_send(scpi, command) != SR_OK)
405 state->analog_channels[ch->index].state = ch->enabled;
415 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
418 struct sr_channel *ch;
419 struct dev_context *devc;
420 struct scope_state *state;
422 struct sr_scpi_dev_inst *scpi;
427 /* Preset empty results. */
428 g_slist_free(devc->enabled_channels);
429 devc->enabled_channels = NULL;
430 state = devc->model_state;
431 state->sample_rate = 0;
433 /* Contruct the list of enabled channels. */
434 for (l = sdi->channels; l; l = l->next) {
439 devc->enabled_channels = g_slist_append(devc->enabled_channels, ch);
442 if (!devc->enabled_channels)
445 /* Configure the analog channels. */
446 if (setup_channels(sdi) != SR_OK) {
447 sr_err("Failed to setup channel configuration!");
453 * Start acquisition on the first enabled channel. The
454 * receive routine will continue driving the acquisition.
456 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
457 lecroy_xstream_receive_data, (void *)sdi);
459 std_session_send_df_header(sdi);
461 devc->current_channel = devc->enabled_channels;
463 return lecroy_xstream_request_data(sdi);
466 g_slist_free(devc->enabled_channels);
467 devc->enabled_channels = NULL;
472 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
474 struct dev_context *devc;
475 struct sr_scpi_dev_inst *scpi;
477 std_session_send_df_end(sdi);
481 devc->num_frames = 0;
482 g_slist_free(devc->enabled_channels);
483 devc->enabled_channels = NULL;
485 sr_scpi_source_remove(sdi->session, scpi);
490 static struct sr_dev_driver lecroy_xstream_driver_info = {
491 .name = "lecroy-xstream",
492 .longname = "LeCroy X-Stream",
495 .cleanup = std_cleanup,
497 .dev_list = std_dev_list,
498 .dev_clear = dev_clear,
499 .config_get = config_get,
500 .config_set = config_set,
501 .config_channel_set = config_channel_set,
502 .config_list = config_list,
503 .dev_open = dev_open,
504 .dev_close = dev_close,
505 .dev_acquisition_start = dev_acquisition_start,
506 .dev_acquisition_stop = dev_acquisition_stop,
509 SR_REGISTER_DEV_DRIVER(lecroy_xstream_driver_info);