]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2017 Sven Schnelle <svens@stackframe.org> | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
21 | #include <stdlib.h> | |
22 | #include "scpi.h" | |
23 | #include "protocol.h" | |
24 | ||
25 | static struct sr_dev_driver lecroy_xstream_driver_info; | |
26 | ||
27 | static const char *manufacturers[] = { | |
28 | "LECROY", | |
29 | }; | |
30 | ||
31 | static const uint32_t scanopts[] = { | |
32 | SR_CONF_CONN, | |
33 | }; | |
34 | ||
35 | static const uint32_t drvopts[] = { | |
36 | SR_CONF_OSCILLOSCOPE, | |
37 | }; | |
38 | ||
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, | |
47 | }; | |
48 | ||
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, | |
53 | }; | |
54 | ||
55 | static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) | |
56 | { | |
57 | struct sr_dev_inst *sdi; | |
58 | struct dev_context *devc; | |
59 | struct sr_scpi_hw_info *hw_info; | |
60 | ||
61 | sdi = NULL; | |
62 | devc = NULL; | |
63 | hw_info = NULL; | |
64 | ||
65 | if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) { | |
66 | sr_info("Couldn't get IDN response."); | |
67 | goto fail; | |
68 | } | |
69 | ||
70 | if (std_str_idx_s(hw_info->manufacturer, ARRAY_AND_SIZE(manufacturers)) < 0) | |
71 | goto fail; | |
72 | ||
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; | |
80 | sdi->conn = scpi; | |
81 | ||
82 | sr_scpi_hw_info_free(hw_info); | |
83 | hw_info = NULL; | |
84 | ||
85 | devc = g_malloc0(sizeof(struct dev_context)); | |
86 | ||
87 | sdi->priv = devc; | |
88 | ||
89 | if (lecroy_xstream_init_device(sdi) != SR_OK) | |
90 | goto fail; | |
91 | ||
92 | return sdi; | |
93 | ||
94 | fail: | |
95 | sr_scpi_hw_info_free(hw_info); | |
96 | sr_dev_inst_free(sdi); | |
97 | g_free(devc); | |
98 | ||
99 | return NULL; | |
100 | } | |
101 | ||
102 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
103 | { | |
104 | return sr_scpi_scan(di->context, options, probe_device); | |
105 | } | |
106 | ||
107 | static void clear_helper(struct dev_context *devc) | |
108 | { | |
109 | lecroy_xstream_state_free(devc->model_state); | |
110 | g_free(devc->analog_groups); | |
111 | } | |
112 | ||
113 | static int dev_clear(const struct sr_dev_driver *di) | |
114 | { | |
115 | return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper); | |
116 | } | |
117 | ||
118 | static int dev_open(struct sr_dev_inst *sdi) | |
119 | { | |
120 | if (sr_scpi_open(sdi->conn) != SR_OK) | |
121 | return SR_ERR; | |
122 | ||
123 | if (lecroy_xstream_state_get(sdi) != SR_OK) | |
124 | return SR_ERR; | |
125 | ||
126 | return SR_OK; | |
127 | } | |
128 | ||
129 | static int dev_close(struct sr_dev_inst *sdi) | |
130 | { | |
131 | return sr_scpi_close(sdi->conn); | |
132 | } | |
133 | ||
134 | static int config_get(uint32_t key, GVariant **data, | |
135 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
136 | { | |
137 | unsigned int i; | |
138 | struct dev_context *devc; | |
139 | const struct scope_config *model; | |
140 | struct scope_state *state; | |
141 | ||
142 | if (!sdi) | |
143 | return SR_ERR_ARG; | |
144 | ||
145 | devc = sdi->priv; | |
146 | ||
147 | model = devc->model_config; | |
148 | state = devc->model_state; | |
149 | *data = NULL; | |
150 | ||
151 | switch (key) { | |
152 | case SR_CONF_NUM_HDIV: | |
153 | *data = g_variant_new_int32(model->num_xdivs); | |
154 | break; | |
155 | case SR_CONF_TIMEBASE: | |
156 | *data = g_variant_new("(tt)", | |
157 | (*model->timebases)[state->timebase][0], | |
158 | (*model->timebases)[state->timebase][1]); | |
159 | break; | |
160 | case SR_CONF_NUM_VDIV: | |
161 | for (i = 0; i < model->analog_channels; i++) { | |
162 | if (cg != devc->analog_groups[i]) | |
163 | continue; | |
164 | *data = g_variant_new_int32(model->num_ydivs); | |
165 | } | |
166 | break; | |
167 | case SR_CONF_VDIV: | |
168 | for (i = 0; i < model->analog_channels; i++) { | |
169 | if (cg != devc->analog_groups[i]) | |
170 | continue; | |
171 | *data = g_variant_new("(tt)", | |
172 | (*model->vdivs)[state->analog_channels[i].vdiv][0], | |
173 | (*model->vdivs)[state->analog_channels[i].vdiv][1]); | |
174 | } | |
175 | break; | |
176 | case SR_CONF_TRIGGER_SOURCE: | |
177 | *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]); | |
178 | break; | |
179 | case SR_CONF_TRIGGER_SLOPE: | |
180 | *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]); | |
181 | break; | |
182 | case SR_CONF_HORIZ_TRIGGERPOS: | |
183 | *data = g_variant_new_double(state->horiz_triggerpos); | |
184 | break; | |
185 | case SR_CONF_COUPLING: | |
186 | for (i = 0; i < model->analog_channels; i++) { | |
187 | if (cg != devc->analog_groups[i]) | |
188 | continue; | |
189 | *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]); | |
190 | } | |
191 | break; | |
192 | case SR_CONF_SAMPLERATE: | |
193 | *data = g_variant_new_uint64(state->sample_rate); | |
194 | break; | |
195 | case SR_CONF_ENABLED: | |
196 | *data = g_variant_new_boolean(FALSE); | |
197 | break; | |
198 | default: | |
199 | return SR_ERR_NA; | |
200 | } | |
201 | ||
202 | return SR_OK; | |
203 | } | |
204 | ||
205 | static int config_set(uint32_t key, GVariant *data, | |
206 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
207 | { | |
208 | int ret, idx; | |
209 | unsigned int j; | |
210 | char command[MAX_COMMAND_SIZE]; | |
211 | struct dev_context *devc; | |
212 | const struct scope_config *model; | |
213 | struct scope_state *state; | |
214 | double tmp_d; | |
215 | gboolean update_sample_rate; | |
216 | ||
217 | if (!sdi) | |
218 | return SR_ERR_ARG; | |
219 | ||
220 | devc = sdi->priv; | |
221 | ||
222 | model = devc->model_config; | |
223 | state = devc->model_state; | |
224 | update_sample_rate = FALSE; | |
225 | ||
226 | ret = SR_ERR_NA; | |
227 | ||
228 | switch (key) { | |
229 | case SR_CONF_LIMIT_FRAMES: | |
230 | devc->frame_limit = g_variant_get_uint64(data); | |
231 | ret = SR_OK; | |
232 | break; | |
233 | case SR_CONF_TRIGGER_SOURCE: | |
234 | if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0) | |
235 | return SR_ERR_ARG; | |
236 | state->trigger_source = idx; | |
237 | g_snprintf(command, sizeof(command), | |
238 | "SET TRIGGER SOURCE %s", (*model->trigger_sources)[idx]); | |
239 | ret = sr_scpi_send(sdi->conn, command); | |
240 | break; | |
241 | case SR_CONF_VDIV: | |
242 | if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0) | |
243 | return SR_ERR_ARG; | |
244 | for (j = 0; j < model->analog_channels; j++) { | |
245 | if (cg != devc->analog_groups[j]) | |
246 | continue; | |
247 | state->analog_channels[j].vdiv = idx; | |
248 | g_snprintf(command, sizeof(command), | |
249 | "C%d:VDIV %E", j + 1, (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]); | |
250 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
251 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
252 | return SR_ERR; | |
253 | break; | |
254 | } | |
255 | ret = SR_OK; | |
256 | break; | |
257 | case SR_CONF_TIMEBASE: | |
258 | if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0) | |
259 | return SR_ERR_ARG; | |
260 | state->timebase = idx; | |
261 | g_snprintf(command, sizeof(command), | |
262 | "TIME_DIV %E", (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]); | |
263 | ret = sr_scpi_send(sdi->conn, command); | |
264 | update_sample_rate = TRUE; | |
265 | break; | |
266 | case SR_CONF_HORIZ_TRIGGERPOS: | |
267 | tmp_d = g_variant_get_double(data); | |
268 | ||
269 | if (tmp_d < 0.0 || tmp_d > 1.0) | |
270 | return SR_ERR; | |
271 | ||
272 | state->horiz_triggerpos = tmp_d; | |
273 | tmp_d = -(tmp_d - 0.5) * | |
274 | ((double)(*model->timebases)[state->timebase][0] / | |
275 | (*model->timebases)[state->timebase][1]) | |
276 | * model->num_xdivs; | |
277 | ||
278 | g_snprintf(command, sizeof(command), "TRIG POS %e S", tmp_d); | |
279 | ||
280 | ret = sr_scpi_send(sdi->conn, command); | |
281 | break; | |
282 | case SR_CONF_TRIGGER_SLOPE: | |
283 | if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0) | |
284 | return SR_ERR_ARG; | |
285 | state->trigger_slope = idx; | |
286 | g_snprintf(command, sizeof(command), | |
287 | "SET TRIGGER SLOPE %s", (*model->trigger_slopes)[idx]); | |
288 | ret = sr_scpi_send(sdi->conn, command); | |
289 | break; | |
290 | case SR_CONF_COUPLING: | |
291 | if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0) | |
292 | return SR_ERR_ARG; | |
293 | for (j = 0; j < model->analog_channels; j++) { | |
294 | if (cg != devc->analog_groups[j]) | |
295 | continue; | |
296 | state->analog_channels[j].coupling = idx; | |
297 | g_snprintf(command, sizeof(command), "C%d:COUPLING %s", | |
298 | j + 1, (*model->coupling_options)[idx]); | |
299 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
300 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
301 | return SR_ERR; | |
302 | break; | |
303 | } | |
304 | ret = SR_OK; | |
305 | break; | |
306 | default: | |
307 | ret = SR_ERR_NA; | |
308 | break; | |
309 | } | |
310 | ||
311 | if (ret == SR_OK) | |
312 | ret = sr_scpi_get_opc(sdi->conn); | |
313 | ||
314 | if (ret == SR_OK && update_sample_rate) | |
315 | ret = lecroy_xstream_update_sample_rate(sdi); | |
316 | ||
317 | return ret; | |
318 | } | |
319 | ||
320 | static int config_list(uint32_t key, GVariant **data, | |
321 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
322 | { | |
323 | struct dev_context *devc; | |
324 | const struct scope_config *model; | |
325 | ||
326 | devc = (sdi) ? sdi->priv : NULL; | |
327 | model = (devc) ? devc->model_config : NULL; | |
328 | ||
329 | switch (key) { | |
330 | case SR_CONF_SCAN_OPTIONS: | |
331 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, NULL, NULL); | |
332 | case SR_CONF_DEVICE_OPTIONS: | |
333 | if (!cg) | |
334 | return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts); | |
335 | *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog)); | |
336 | break; | |
337 | case SR_CONF_COUPLING: | |
338 | *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options); | |
339 | break; | |
340 | case SR_CONF_TRIGGER_SOURCE: | |
341 | if (!model) | |
342 | return SR_ERR_ARG; | |
343 | *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources); | |
344 | break; | |
345 | case SR_CONF_TRIGGER_SLOPE: | |
346 | if (!model) | |
347 | return SR_ERR_ARG; | |
348 | *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes); | |
349 | break; | |
350 | case SR_CONF_TIMEBASE: | |
351 | if (!model) | |
352 | return SR_ERR_ARG; | |
353 | *data = std_gvar_tuple_array(*model->timebases, model->num_timebases); | |
354 | break; | |
355 | case SR_CONF_VDIV: | |
356 | if (!model) | |
357 | return SR_ERR_ARG; | |
358 | *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs); | |
359 | break; | |
360 | default: | |
361 | return SR_ERR_NA; | |
362 | } | |
363 | ||
364 | return SR_OK; | |
365 | } | |
366 | ||
367 | SR_PRIV int lecroy_xstream_request_data(const struct sr_dev_inst *sdi) | |
368 | { | |
369 | char command[MAX_COMMAND_SIZE]; | |
370 | struct sr_channel *ch; | |
371 | struct dev_context *devc; | |
372 | ||
373 | devc = sdi->priv; | |
374 | ||
375 | ch = devc->current_channel->data; | |
376 | ||
377 | if (ch->type != SR_CHANNEL_ANALOG) | |
378 | return SR_ERR; | |
379 | ||
380 | g_snprintf(command, sizeof(command), | |
381 | "COMM_FORMAT DEF9,WORD,BIN;C%d:WAVEFORM?", ch->index + 1); | |
382 | return sr_scpi_send(sdi->conn, command); | |
383 | } | |
384 | ||
385 | static int setup_channels(const struct sr_dev_inst *sdi) | |
386 | { | |
387 | GSList *l; | |
388 | gboolean setup_changed; | |
389 | char command[MAX_COMMAND_SIZE]; | |
390 | struct scope_state *state; | |
391 | struct sr_channel *ch; | |
392 | struct dev_context *devc; | |
393 | struct sr_scpi_dev_inst *scpi; | |
394 | ||
395 | devc = sdi->priv; | |
396 | scpi = sdi->conn; | |
397 | state = devc->model_state; | |
398 | setup_changed = FALSE; | |
399 | ||
400 | for (l = sdi->channels; l; l = l->next) { | |
401 | ch = l->data; | |
402 | switch (ch->type) { | |
403 | case SR_CHANNEL_ANALOG: | |
404 | if (ch->enabled == state->analog_channels[ch->index].state) | |
405 | break; | |
406 | g_snprintf(command, sizeof(command), "C%d:TRACE %s", | |
407 | ch->index + 1, ch->enabled ? "ON" : "OFF"); | |
408 | ||
409 | if (sr_scpi_send(scpi, command) != SR_OK) | |
410 | return SR_ERR; | |
411 | state->analog_channels[ch->index].state = ch->enabled; | |
412 | setup_changed = TRUE; | |
413 | break; | |
414 | default: | |
415 | return SR_ERR; | |
416 | } | |
417 | } | |
418 | ||
419 | if (setup_changed && lecroy_xstream_update_sample_rate(sdi) != SR_OK) | |
420 | return SR_ERR; | |
421 | ||
422 | return SR_OK; | |
423 | } | |
424 | ||
425 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
426 | { | |
427 | GSList *l; | |
428 | struct sr_channel *ch; | |
429 | struct dev_context *devc; | |
430 | int ret; | |
431 | struct sr_scpi_dev_inst *scpi; | |
432 | ||
433 | devc = sdi->priv; | |
434 | scpi = sdi->conn; | |
435 | ||
436 | /* Preset empty results. */ | |
437 | g_slist_free(devc->enabled_channels); | |
438 | devc->enabled_channels = NULL; | |
439 | ||
440 | /* | |
441 | * Contruct the list of enabled channels. Determine the highest | |
442 | * number of digital pods involved in the acquisition. | |
443 | */ | |
444 | ||
445 | for (l = sdi->channels; l; l = l->next) { | |
446 | ch = l->data; | |
447 | if (!ch->enabled) | |
448 | continue; | |
449 | /* Only add a single digital channel per group (pod). */ | |
450 | devc->enabled_channels = g_slist_append( | |
451 | devc->enabled_channels, ch); | |
452 | } | |
453 | ||
454 | if (!devc->enabled_channels) | |
455 | return SR_ERR; | |
456 | ||
457 | /* | |
458 | * Configure the analog channels and the | |
459 | * corresponding digital pods. | |
460 | */ | |
461 | if (setup_channels(sdi) != SR_OK) { | |
462 | sr_err("Failed to setup channel configuration!"); | |
463 | ret = SR_ERR; | |
464 | goto free_enabled; | |
465 | } | |
466 | ||
467 | /* | |
468 | * Start acquisition on the first enabled channel. The | |
469 | * receive routine will continue driving the acquisition. | |
470 | */ | |
471 | sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50, | |
472 | lecroy_xstream_receive_data, (void *)sdi); | |
473 | ||
474 | std_session_send_df_header(sdi); | |
475 | ||
476 | devc->current_channel = devc->enabled_channels; | |
477 | ||
478 | return lecroy_xstream_request_data(sdi); | |
479 | ||
480 | free_enabled: | |
481 | g_slist_free(devc->enabled_channels); | |
482 | devc->enabled_channels = NULL; | |
483 | ||
484 | return ret; | |
485 | } | |
486 | ||
487 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
488 | { | |
489 | struct dev_context *devc; | |
490 | struct sr_scpi_dev_inst *scpi; | |
491 | ||
492 | std_session_send_df_end(sdi); | |
493 | ||
494 | devc = sdi->priv; | |
495 | ||
496 | devc->num_frames = 0; | |
497 | g_slist_free(devc->enabled_channels); | |
498 | devc->enabled_channels = NULL; | |
499 | scpi = sdi->conn; | |
500 | sr_scpi_source_remove(sdi->session, scpi); | |
501 | ||
502 | return SR_OK; | |
503 | } | |
504 | ||
505 | static struct sr_dev_driver lecroy_xstream_driver_info = { | |
506 | .name = "lecroy-xstream", | |
507 | .longname = "LeCroy X-Stream", | |
508 | .api_version = 1, | |
509 | .init = std_init, | |
510 | .cleanup = std_cleanup, | |
511 | .scan = scan, | |
512 | .dev_list = std_dev_list, | |
513 | .dev_clear = dev_clear, | |
514 | .config_get = config_get, | |
515 | .config_set = config_set, | |
516 | .config_list = config_list, | |
517 | .dev_open = dev_open, | |
518 | .dev_close = dev_close, | |
519 | .dev_acquisition_start = dev_acquisition_start, | |
520 | .dev_acquisition_stop = dev_acquisition_stop, | |
521 | .context = NULL, | |
522 | }; | |
523 | SR_REGISTER_DEV_DRIVER(lecroy_xstream_driver_info); |