]>
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; | |
209 | unsigned int i, j; | |
210 | char command[MAX_COMMAND_SIZE]; | |
211 | struct dev_context *devc; | |
212 | const struct scope_config *model; | |
213 | struct scope_state *state; | |
214 | const char *tmp; | |
215 | uint64_t p, q; | |
216 | double tmp_d; | |
217 | gboolean update_sample_rate; | |
218 | ||
219 | if (!sdi) | |
220 | return SR_ERR_ARG; | |
221 | ||
222 | devc = sdi->priv; | |
223 | ||
224 | model = devc->model_config; | |
225 | state = devc->model_state; | |
226 | update_sample_rate = FALSE; | |
227 | ||
228 | ret = SR_ERR_NA; | |
229 | ||
230 | switch (key) { | |
231 | case SR_CONF_LIMIT_FRAMES: | |
232 | devc->frame_limit = g_variant_get_uint64(data); | |
233 | ret = SR_OK; | |
234 | break; | |
235 | case SR_CONF_TRIGGER_SOURCE: | |
236 | tmp = g_variant_get_string(data, NULL); | |
237 | for (i = 0; i < model->num_trigger_sources; i++) { | |
238 | if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0) | |
239 | continue; | |
240 | state->trigger_source = i; | |
241 | g_snprintf(command, sizeof(command), | |
242 | "SET TRIGGER SOURCE %s", | |
243 | (*model->trigger_sources)[i]); | |
244 | ||
245 | ret = sr_scpi_send(sdi->conn, command); | |
246 | break; | |
247 | } | |
248 | break; | |
249 | case SR_CONF_VDIV: | |
250 | g_variant_get(data, "(tt)", &p, &q); | |
251 | ||
252 | for (i = 0; i < model->num_vdivs; i++) { | |
253 | if (p != (*model->vdivs)[i][0] || q != (*model->vdivs)[i][1]) | |
254 | continue; | |
255 | for (j = 1; j <= model->analog_channels; j++) { | |
256 | if (cg != devc->analog_groups[j - 1]) | |
257 | continue; | |
258 | state->analog_channels[j - 1].vdiv = i; | |
259 | g_snprintf(command, sizeof(command), | |
260 | "C%d:VDIV %E", j, (float)p/q); | |
261 | ||
262 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
263 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
264 | return SR_ERR; | |
265 | ||
266 | break; | |
267 | } | |
268 | ||
269 | ret = SR_OK; | |
270 | break; | |
271 | } | |
272 | break; | |
273 | case SR_CONF_TIMEBASE: | |
274 | g_variant_get(data, "(tt)", &p, &q); | |
275 | ||
276 | for (i = 0; i < model->num_timebases; i++) { | |
277 | if (p != (*model->timebases)[i][0] || | |
278 | q != (*model->timebases)[i][1]) | |
279 | continue; | |
280 | state->timebase = i; | |
281 | g_snprintf(command, sizeof(command), | |
282 | "TIME_DIV %E", (float)p/q); | |
283 | ||
284 | ret = sr_scpi_send(sdi->conn, command); | |
285 | update_sample_rate = TRUE; | |
286 | break; | |
287 | } | |
288 | break; | |
289 | case SR_CONF_HORIZ_TRIGGERPOS: | |
290 | tmp_d = g_variant_get_double(data); | |
291 | ||
292 | if (tmp_d < 0.0 || tmp_d > 1.0) | |
293 | return SR_ERR; | |
294 | ||
295 | state->horiz_triggerpos = tmp_d; | |
296 | tmp_d = -(tmp_d - 0.5) * | |
297 | ((double)(*model->timebases)[state->timebase][0] / | |
298 | (*model->timebases)[state->timebase][1]) | |
299 | * model->num_xdivs; | |
300 | ||
301 | g_snprintf(command, sizeof(command), "TRIG POS %e S", tmp_d); | |
302 | ||
303 | ret = sr_scpi_send(sdi->conn, command); | |
304 | break; | |
305 | case SR_CONF_TRIGGER_SLOPE: | |
306 | tmp = g_variant_get_string(data, NULL); | |
307 | for (i = 0; i < model->num_trigger_slopes; i++) { | |
308 | if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0) | |
309 | continue; | |
310 | state->trigger_slope = i; | |
311 | g_snprintf(command, sizeof(command), | |
312 | "SET TRIGGER SLOPE %s", | |
313 | (*model->trigger_slopes)[i]); | |
314 | ||
315 | ret = sr_scpi_send(sdi->conn, command); | |
316 | break; | |
317 | } | |
318 | break; | |
319 | case SR_CONF_COUPLING: | |
320 | tmp = g_variant_get_string(data, NULL); | |
321 | ||
322 | for (i = 0; i < model->num_coupling_options; i++) { | |
323 | if (strcmp(tmp, (*model->coupling_options)[i]) != 0) | |
324 | continue; | |
325 | for (j = 1; j <= model->analog_channels; j++) { | |
326 | if (cg != devc->analog_groups[j - 1]) | |
327 | continue; | |
328 | state->analog_channels[j - 1].coupling = i; | |
329 | ||
330 | g_snprintf(command, sizeof(command), | |
331 | "C%d:COUPLING %s", j, tmp); | |
332 | ||
333 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
334 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
335 | return SR_ERR; | |
336 | break; | |
337 | } | |
338 | ||
339 | ret = SR_OK; | |
340 | break; | |
341 | } | |
342 | break; | |
343 | default: | |
344 | ret = SR_ERR_NA; | |
345 | break; | |
346 | } | |
347 | ||
348 | if (ret == SR_OK) | |
349 | ret = sr_scpi_get_opc(sdi->conn); | |
350 | ||
351 | if (ret == SR_OK && update_sample_rate) | |
352 | ret = lecroy_xstream_update_sample_rate(sdi); | |
353 | ||
354 | return ret; | |
355 | } | |
356 | ||
357 | static int config_list(uint32_t key, GVariant **data, | |
358 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
359 | { | |
360 | struct dev_context *devc; | |
361 | const struct scope_config *model; | |
362 | ||
363 | devc = (sdi) ? sdi->priv : NULL; | |
364 | model = (devc) ? devc->model_config : NULL; | |
365 | ||
366 | switch (key) { | |
367 | case SR_CONF_SCAN_OPTIONS: | |
368 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, NULL, NULL); | |
369 | case SR_CONF_DEVICE_OPTIONS: | |
370 | if (!cg) | |
371 | return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts); | |
372 | *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog)); | |
373 | break; | |
374 | case SR_CONF_COUPLING: | |
375 | *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options); | |
376 | break; | |
377 | case SR_CONF_TRIGGER_SOURCE: | |
378 | if (!model) | |
379 | return SR_ERR_ARG; | |
380 | *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources); | |
381 | break; | |
382 | case SR_CONF_TRIGGER_SLOPE: | |
383 | if (!model) | |
384 | return SR_ERR_ARG; | |
385 | *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes); | |
386 | break; | |
387 | case SR_CONF_TIMEBASE: | |
388 | if (!model) | |
389 | return SR_ERR_ARG; | |
390 | *data = std_gvar_tuple_array(*model->timebases, model->num_timebases); | |
391 | break; | |
392 | case SR_CONF_VDIV: | |
393 | if (!model) | |
394 | return SR_ERR_ARG; | |
395 | *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs); | |
396 | break; | |
397 | default: | |
398 | return SR_ERR_NA; | |
399 | } | |
400 | ||
401 | return SR_OK; | |
402 | } | |
403 | ||
404 | SR_PRIV int lecroy_xstream_request_data(const struct sr_dev_inst *sdi) | |
405 | { | |
406 | char command[MAX_COMMAND_SIZE]; | |
407 | struct sr_channel *ch; | |
408 | struct dev_context *devc; | |
409 | ||
410 | devc = sdi->priv; | |
411 | ||
412 | ch = devc->current_channel->data; | |
413 | ||
414 | if (ch->type != SR_CHANNEL_ANALOG) | |
415 | return SR_ERR; | |
416 | ||
417 | g_snprintf(command, sizeof(command), | |
418 | "COMM_FORMAT DEF9,WORD,BIN;C%d:WAVEFORM?", ch->index + 1); | |
419 | return sr_scpi_send(sdi->conn, command); | |
420 | } | |
421 | ||
422 | static int setup_channels(const struct sr_dev_inst *sdi) | |
423 | { | |
424 | GSList *l; | |
425 | gboolean setup_changed; | |
426 | char command[MAX_COMMAND_SIZE]; | |
427 | struct scope_state *state; | |
428 | struct sr_channel *ch; | |
429 | struct dev_context *devc; | |
430 | struct sr_scpi_dev_inst *scpi; | |
431 | ||
432 | devc = sdi->priv; | |
433 | scpi = sdi->conn; | |
434 | state = devc->model_state; | |
435 | setup_changed = FALSE; | |
436 | ||
437 | for (l = sdi->channels; l; l = l->next) { | |
438 | ch = l->data; | |
439 | switch (ch->type) { | |
440 | case SR_CHANNEL_ANALOG: | |
441 | if (ch->enabled == state->analog_channels[ch->index].state) | |
442 | break; | |
443 | g_snprintf(command, sizeof(command), "C%d:TRACE %s", | |
444 | ch->index + 1, ch->enabled ? "ON" : "OFF"); | |
445 | ||
446 | if (sr_scpi_send(scpi, command) != SR_OK) | |
447 | return SR_ERR; | |
448 | state->analog_channels[ch->index].state = ch->enabled; | |
449 | setup_changed = TRUE; | |
450 | break; | |
451 | default: | |
452 | return SR_ERR; | |
453 | } | |
454 | } | |
455 | ||
456 | if (setup_changed && lecroy_xstream_update_sample_rate(sdi) != SR_OK) | |
457 | return SR_ERR; | |
458 | ||
459 | return SR_OK; | |
460 | } | |
461 | ||
462 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
463 | { | |
464 | GSList *l; | |
465 | struct sr_channel *ch; | |
466 | struct dev_context *devc; | |
467 | int ret; | |
468 | struct sr_scpi_dev_inst *scpi; | |
469 | ||
470 | devc = sdi->priv; | |
471 | scpi = sdi->conn; | |
472 | ||
473 | /* Preset empty results. */ | |
474 | g_slist_free(devc->enabled_channels); | |
475 | devc->enabled_channels = NULL; | |
476 | ||
477 | /* | |
478 | * Contruct the list of enabled channels. Determine the highest | |
479 | * number of digital pods involved in the acquisition. | |
480 | */ | |
481 | ||
482 | for (l = sdi->channels; l; l = l->next) { | |
483 | ch = l->data; | |
484 | if (!ch->enabled) | |
485 | continue; | |
486 | /* Only add a single digital channel per group (pod). */ | |
487 | devc->enabled_channels = g_slist_append( | |
488 | devc->enabled_channels, ch); | |
489 | } | |
490 | ||
491 | if (!devc->enabled_channels) | |
492 | return SR_ERR; | |
493 | ||
494 | /* | |
495 | * Configure the analog channels and the | |
496 | * corresponding digital pods. | |
497 | */ | |
498 | if (setup_channels(sdi) != SR_OK) { | |
499 | sr_err("Failed to setup channel configuration!"); | |
500 | ret = SR_ERR; | |
501 | goto free_enabled; | |
502 | } | |
503 | ||
504 | /* | |
505 | * Start acquisition on the first enabled channel. The | |
506 | * receive routine will continue driving the acquisition. | |
507 | */ | |
508 | sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50, | |
509 | lecroy_xstream_receive_data, (void *)sdi); | |
510 | ||
511 | std_session_send_df_header(sdi); | |
512 | ||
513 | devc->current_channel = devc->enabled_channels; | |
514 | ||
515 | return lecroy_xstream_request_data(sdi); | |
516 | ||
517 | free_enabled: | |
518 | g_slist_free(devc->enabled_channels); | |
519 | devc->enabled_channels = NULL; | |
520 | ||
521 | return ret; | |
522 | } | |
523 | ||
524 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
525 | { | |
526 | struct dev_context *devc; | |
527 | struct sr_scpi_dev_inst *scpi; | |
528 | ||
529 | std_session_send_df_end(sdi); | |
530 | ||
531 | devc = sdi->priv; | |
532 | ||
533 | devc->num_frames = 0; | |
534 | g_slist_free(devc->enabled_channels); | |
535 | devc->enabled_channels = NULL; | |
536 | scpi = sdi->conn; | |
537 | sr_scpi_source_remove(sdi->session, scpi); | |
538 | ||
539 | return SR_OK; | |
540 | } | |
541 | ||
542 | static struct sr_dev_driver lecroy_xstream_driver_info = { | |
543 | .name = "lecroy-xstream", | |
544 | .longname = "LeCroy X-Stream", | |
545 | .api_version = 1, | |
546 | .init = std_init, | |
547 | .cleanup = std_cleanup, | |
548 | .scan = scan, | |
549 | .dev_list = std_dev_list, | |
550 | .dev_clear = dev_clear, | |
551 | .config_get = config_get, | |
552 | .config_set = config_set, | |
553 | .config_list = config_list, | |
554 | .dev_open = dev_open, | |
555 | .dev_close = dev_close, | |
556 | .dev_acquisition_start = dev_acquisition_start, | |
557 | .dev_acquisition_stop = dev_acquisition_stop, | |
558 | .context = NULL, | |
559 | }; | |
560 | SR_REGISTER_DEV_DRIVER(lecroy_xstream_driver_info); |