]> sigrok.org Git - libsigrok.git/blame - src/hardware/lecroy-xstream/api.c
saleae-logic-pro: Use sr_dev_acquisition_stop() wrapper.
[libsigrok.git] / src / hardware / lecroy-xstream / api.c
CommitLineData
e3b83c5e
SS
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>
3f2c7c94
SS
21#include <stdlib.h>
22#include "scpi.h"
e3b83c5e
SS
23#include "protocol.h"
24
3f2c7c94
SS
25static struct sr_dev_driver lecroy_xstream_driver_info;
26
27static const char *manufacturers[] = {
28 "LECROY",
29};
30
31static const uint32_t scanopts[] = {
32 SR_CONF_CONN,
33};
34
90230cfa
SA
35static const uint32_t drvopts[] = {
36 SR_CONF_OSCILLOSCOPE,
37};
38
39static const uint32_t devopts[] = {
40 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
86621306 41 SR_CONF_SAMPLERATE | SR_CONF_GET,
90230cfa
SA
42 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43 SR_CONF_NUM_HDIV | SR_CONF_GET,
90230cfa 44 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
86621306
UH
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,
90230cfa
SA
47};
48
49static const uint32_t analog_devopts[] = {
50 SR_CONF_NUM_VDIV | SR_CONF_GET,
90230cfa 51 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
86621306 52 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
90230cfa
SA
53};
54
3f2c7c94
SS
55static int check_manufacturer(const char *manufacturer)
56{
57 unsigned int i;
58
59 for (i = 0; i < ARRAY_SIZE(manufacturers); i++)
60 if (!strcmp(manufacturer, manufacturers[i]))
61 return SR_OK;
62
63 return SR_ERR;
64}
65
66static struct sr_dev_inst *probe_serial_device(struct sr_scpi_dev_inst *scpi)
67{
68 struct sr_dev_inst *sdi;
69 struct dev_context *devc;
70 struct sr_scpi_hw_info *hw_info;
71
72 sdi = NULL;
73 devc = NULL;
74 hw_info = NULL;
75
3f2c7c94
SS
76 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
77 sr_info("Couldn't get IDN response.");
78 goto fail;
79 }
80
81 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
82 goto fail;
83
84 sdi = g_malloc0(sizeof(struct sr_dev_inst));
85 sdi->vendor = g_strdup(hw_info->manufacturer);
86 sdi->model = g_strdup(hw_info->model);
87 sdi->version = g_strdup(hw_info->firmware_version);
88 sdi->serial_num = g_strdup(hw_info->serial_number);
89 sdi->driver = &lecroy_xstream_driver_info;
90 sdi->inst_type = SR_INST_SCPI;
91 sdi->conn = scpi;
92
93 sr_scpi_hw_info_free(hw_info);
94 hw_info = NULL;
95
96 devc = g_malloc0(sizeof(struct dev_context));
97
98 sdi->priv = devc;
99
100 if (lecroy_xstream_init_device(sdi) != SR_OK)
101 goto fail;
102
103 return sdi;
104
105fail:
106 sr_scpi_hw_info_free(hw_info);
4bf93988 107 sr_dev_inst_free(sdi);
3f2c7c94
SS
108 g_free(devc);
109
110 return NULL;
111}
e3b83c5e
SS
112
113static GSList *scan(struct sr_dev_driver *di, GSList *options)
114{
3f2c7c94
SS
115 return sr_scpi_scan(di->context, options, probe_serial_device);
116}
117
3553451f 118static void clear_helper(struct dev_context *devc)
3f2c7c94 119{
3f2c7c94 120 lecroy_xstream_state_free(devc->model_state);
3f2c7c94 121 g_free(devc->analog_groups);
e3b83c5e
SS
122}
123
124static int dev_clear(const struct sr_dev_driver *di)
125{
3553451f 126 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
e3b83c5e
SS
127}
128
129static int dev_open(struct sr_dev_inst *sdi)
130{
6402c379 131 if (sr_scpi_open(sdi->conn) != SR_OK)
3f2c7c94 132 return SR_ERR;
e3b83c5e 133
3f2c7c94
SS
134 if (lecroy_xstream_state_get(sdi) != SR_OK)
135 return SR_ERR;
e3b83c5e 136
e3b83c5e
SS
137 return SR_OK;
138}
139
140static int dev_close(struct sr_dev_inst *sdi)
141{
f1ba6b4b 142 return sr_scpi_close(sdi->conn);
e3b83c5e
SS
143}
144
145static int config_get(uint32_t key, GVariant **data,
ea257cdc 146 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
e3b83c5e 147{
3f2c7c94
SS
148 unsigned int i;
149 struct dev_context *devc;
150 const struct scope_config *model;
151 struct scope_state *state;
e3b83c5e 152
3f2c7c94
SS
153 if (!sdi)
154 return SR_ERR_ARG;
155
156 devc = sdi->priv;
e3b83c5e 157
3f2c7c94
SS
158 model = devc->model_config;
159 state = devc->model_state;
160 *data = NULL;
ea257cdc 161
e3b83c5e 162 switch (key) {
3f2c7c94
SS
163 case SR_CONF_NUM_HDIV:
164 *data = g_variant_new_int32(model->num_xdivs);
3f2c7c94
SS
165 break;
166 case SR_CONF_TIMEBASE:
167 *data = g_variant_new("(tt)",
ea257cdc
UH
168 model->timebases[state->timebase].p,
169 model->timebases[state->timebase].q);
3f2c7c94
SS
170 break;
171 case SR_CONF_NUM_VDIV:
172 for (i = 0; i < model->analog_channels; i++) {
173 if (cg != devc->analog_groups[i])
174 continue;
175 *data = g_variant_new_int32(model->num_ydivs);
3f2c7c94
SS
176 }
177 break;
178 case SR_CONF_VDIV:
179 for (i = 0; i < model->analog_channels; i++) {
180 if (cg != devc->analog_groups[i])
181 continue;
182 *data = g_variant_new("(tt)",
ea257cdc
UH
183 model->vdivs[state->analog_channels[i].vdiv].p,
184 model->vdivs[state->analog_channels[i].vdiv].q);
3f2c7c94
SS
185 }
186 break;
187 case SR_CONF_TRIGGER_SOURCE:
188 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
3f2c7c94
SS
189 break;
190 case SR_CONF_TRIGGER_SLOPE:
191 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
3f2c7c94
SS
192 break;
193 case SR_CONF_HORIZ_TRIGGERPOS:
194 *data = g_variant_new_double(state->horiz_triggerpos);
3f2c7c94
SS
195 break;
196 case SR_CONF_COUPLING:
3f2c7c94 197 for (i = 0; i < model->analog_channels; i++) {
ea257cdc 198 if (cg != devc->analog_groups[i])
3f2c7c94 199 continue;
3f2c7c94 200 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
3f2c7c94
SS
201 }
202 break;
203 case SR_CONF_SAMPLERATE:
204 *data = g_variant_new_uint64(state->sample_rate);
3f2c7c94
SS
205 break;
206 case SR_CONF_ENABLED:
207 *data = g_variant_new_boolean(FALSE);
3f2c7c94 208 break;
e3b83c5e 209 default:
a9010323 210 return SR_ERR_NA;
e3b83c5e 211 }
ea257cdc 212
a9010323 213 return SR_OK;
e3b83c5e
SS
214}
215
3f2c7c94
SS
216static GVariant *build_tuples(const struct sr_rational *array, unsigned int n)
217{
218 unsigned int i;
219 GVariant *rational[2];
220 GVariantBuilder gvb;
221
222 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
223
224 for (i = 0; i < n; i++) {
225 rational[0] = g_variant_new_uint64(array[i].p);
226 rational[1] = g_variant_new_uint64(array[i].q);
227
228 /* FIXME: Valgrind reports a memory leak here. */
229 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
230 }
231
232 return g_variant_builder_end(&gvb);
233}
234
ea257cdc
UH
235static int config_set(uint32_t key, GVariant *data,
236 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
e3b83c5e
SS
237{
238 int ret;
3f2c7c94
SS
239 unsigned int i, j;
240 char command[MAX_COMMAND_SIZE];
241 struct dev_context *devc;
242 const struct scope_config *model;
243 struct scope_state *state;
244 const char *tmp;
245 int64_t p;
246 uint64_t q;
247 double tmp_d;
248 gboolean update_sample_rate;
e3b83c5e 249
3f2c7c94
SS
250 if (!sdi)
251 return SR_ERR_ARG;
e3b83c5e 252
3f2c7c94
SS
253 devc = sdi->priv;
254
255 model = devc->model_config;
256 state = devc->model_state;
257 update_sample_rate = FALSE;
258
259 ret = SR_ERR_NA;
e3b83c5e 260
e3b83c5e 261 switch (key) {
3f2c7c94
SS
262 case SR_CONF_LIMIT_FRAMES:
263 devc->frame_limit = g_variant_get_uint64(data);
264 ret = SR_OK;
265 break;
266 case SR_CONF_TRIGGER_SOURCE:
267 tmp = g_variant_get_string(data, NULL);
268 for (i = 0; (*model->trigger_sources)[i]; i++) {
269 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
270 continue;
271 state->trigger_source = i;
272 g_snprintf(command, sizeof(command),
273 "SET TRIGGER SOURCE %s",
274 (*model->trigger_sources)[i]);
275
276 ret = sr_scpi_send(sdi->conn, command);
277 break;
278 }
279 break;
280 case SR_CONF_VDIV:
281 g_variant_get(data, "(tt)", &p, &q);
282
283 for (i = 0; i < model->num_vdivs; i++) {
284 if (p != model->vdivs[i].p || q != model->vdivs[i].q)
285 continue;
286 for (j = 1; j <= model->analog_channels; j++) {
287 if (cg != devc->analog_groups[j - 1])
288 continue;
289 state->analog_channels[j - 1].vdiv = i;
290 g_snprintf(command, sizeof(command),
291 "C%d:VDIV %E", j, (float)p/q);
292
293 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
294 sr_scpi_get_opc(sdi->conn) != SR_OK)
295 return SR_ERR;
296
297 break;
298 }
299
300 ret = SR_OK;
301 break;
302 }
303 break;
304 case SR_CONF_TIMEBASE:
305 g_variant_get(data, "(tt)", &p, &q);
306
307 for (i = 0; i < model->num_timebases; i++) {
308 if (p != model->timebases[i].p ||
309 q != model->timebases[i].q)
310 continue;
311 state->timebase = i;
312 g_snprintf(command, sizeof(command),
313 "TIME_DIV %E", (float)p/q);
314
315 ret = sr_scpi_send(sdi->conn, command);
316 update_sample_rate = TRUE;
317 break;
318 }
319 break;
320 case SR_CONF_HORIZ_TRIGGERPOS:
321 tmp_d = g_variant_get_double(data);
322
323 if (tmp_d < 0.0 || tmp_d > 1.0)
324 return SR_ERR;
325
326 state->horiz_triggerpos = tmp_d;
327 tmp_d = -(tmp_d - 0.5) *
328 ((double)model->timebases[state->timebase].p /
329 model->timebases[state->timebase].q)
330 * model->num_xdivs;
331
332 g_snprintf(command, sizeof(command), "TRIG POS %e S", tmp_d);
333
334 ret = sr_scpi_send(sdi->conn, command);
335 break;
336 case SR_CONF_TRIGGER_SLOPE:
337 tmp = g_variant_get_string(data, NULL);
338 for (i = 0; (*model->trigger_slopes)[i]; i++) {
339 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
340 continue;
341 state->trigger_slope = i;
342 g_snprintf(command, sizeof(command),
343 "SET TRIGGER SLOPE %s",
344 (*model->trigger_slopes)[i]);
345
346 ret = sr_scpi_send(sdi->conn, command);
347 break;
348 }
349 break;
350 case SR_CONF_COUPLING:
3f2c7c94
SS
351 tmp = g_variant_get_string(data, NULL);
352
353 for (i = 0; (*model->coupling_options)[i]; i++) {
354 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
355 continue;
356 for (j = 1; j <= model->analog_channels; j++) {
357 if (cg != devc->analog_groups[j - 1])
358 continue;
ea257cdc 359 state->analog_channels[j - 1].coupling = i;
3f2c7c94
SS
360
361 g_snprintf(command, sizeof(command),
362 "C%d:COUPLING %s", j, tmp);
363
364 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
365 sr_scpi_get_opc(sdi->conn) != SR_OK)
366 return SR_ERR;
367 break;
368 }
369
370 ret = SR_OK;
371 break;
372 }
373 break;
e3b83c5e
SS
374 default:
375 ret = SR_ERR_NA;
3f2c7c94 376 break;
e3b83c5e
SS
377 }
378
3f2c7c94
SS
379 if (ret == SR_OK)
380 ret = sr_scpi_get_opc(sdi->conn);
381
382 if (ret == SR_OK && update_sample_rate)
383 ret = lecroy_xstream_update_sample_rate(sdi);
384
e3b83c5e
SS
385 return ret;
386}
387
ea257cdc
UH
388static int config_list(uint32_t key, GVariant **data,
389 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
e3b83c5e 390{
3f2c7c94
SS
391 struct dev_context *devc = NULL;
392 const struct scope_config *model = NULL;
ea257cdc 393
e3b83c5e
SS
394 (void)cg;
395
90230cfa
SA
396 /* SR_CONF_SCAN_OPTIONS is always valid, regardless of sdi or channel group. */
397 if (key == SR_CONF_SCAN_OPTIONS) {
398 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
399 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
400 return SR_OK;
3f2c7c94
SS
401 }
402
90230cfa
SA
403 /* If sdi is NULL, nothing except SR_CONF_DEVICE_OPTIONS can be provided. */
404 if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
3f2c7c94 405 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
90230cfa
SA
406 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
407 return SR_OK;
408 }
409
410 /* Every other option requires a valid device instance. */
411 if (!sdi)
412 return SR_ERR_ARG;
413
414 devc = sdi->priv;
415 model = devc->model_config;
416
417 switch (key) {
3f2c7c94
SS
418 case SR_CONF_DEVICE_OPTIONS:
419 if (!cg) {
90230cfa
SA
420 /* If cg is NULL, only the SR_CONF_DEVICE_OPTIONS that are not
421 * specific to a channel group must be returned. */
3f2c7c94 422 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
90230cfa
SA
423 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
424 return SR_OK;
3f2c7c94
SS
425 }
426 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
90230cfa 427 analog_devopts, ARRAY_SIZE(analog_devopts),
ea257cdc 428 sizeof(uint32_t));
3f2c7c94
SS
429 break;
430 case SR_CONF_COUPLING:
431 *data = g_variant_new_strv(*model->coupling_options,
432 g_strv_length((char **)*model->coupling_options));
433 break;
434 case SR_CONF_TRIGGER_SOURCE:
435 if (!model)
436 return SR_ERR_ARG;
437 *data = g_variant_new_strv(*model->trigger_sources,
438 g_strv_length((char **)*model->trigger_sources));
439 break;
440 case SR_CONF_TRIGGER_SLOPE:
441 if (!model)
442 return SR_ERR_ARG;
443 *data = g_variant_new_strv(*model->trigger_slopes,
444 g_strv_length((char **)*model->trigger_slopes));
445 break;
446 case SR_CONF_TIMEBASE:
447 if (!model)
448 return SR_ERR_ARG;
449 *data = build_tuples(model->timebases, model->num_timebases);
450 break;
451 case SR_CONF_VDIV:
452 if (!model)
453 return SR_ERR_ARG;
454 *data = build_tuples(model->vdivs, model->num_vdivs);
455 break;
e3b83c5e
SS
456 default:
457 return SR_ERR_NA;
458 }
3f2c7c94
SS
459 return SR_OK;
460}
e3b83c5e 461
3f2c7c94
SS
462SR_PRIV int lecroy_xstream_request_data(const struct sr_dev_inst *sdi)
463{
464 char command[MAX_COMMAND_SIZE];
465 struct sr_channel *ch;
466 struct dev_context *devc;
467
468 devc = sdi->priv;
469
470 ch = devc->current_channel->data;
471
472 if (ch->type != SR_CHANNEL_ANALOG)
473 return SR_ERR;
474
475 g_snprintf(command, sizeof(command),
ea257cdc 476 "COMM_FORMAT DEF9,WORD,BIN;C%d:WAVEFORM?", ch->index + 1);
3f2c7c94
SS
477 return sr_scpi_send(sdi->conn, command);
478}
479
6d13a46c 480static int setup_channels(const struct sr_dev_inst *sdi)
3f2c7c94
SS
481{
482 GSList *l;
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;
489
490 devc = sdi->priv;
491 scpi = sdi->conn;
492 state = devc->model_state;
493 setup_changed = FALSE;
494
495 for (l = sdi->channels; l; l = l->next) {
496 ch = l->data;
497 switch (ch->type) {
498 case SR_CHANNEL_ANALOG:
499 if (ch->enabled == state->analog_channels[ch->index].state)
500 break;
501 g_snprintf(command, sizeof(command), "C%d:TRACE %s",
ea257cdc 502 ch->index + 1, ch->enabled ? "ON" : "OFF");
3f2c7c94
SS
503
504 if (sr_scpi_send(scpi, command) != SR_OK)
505 return SR_ERR;
506 state->analog_channels[ch->index].state = ch->enabled;
507 setup_changed = TRUE;
508 break;
509 default:
510 return SR_ERR;
511 }
512 }
513
514 if (setup_changed && lecroy_xstream_update_sample_rate(sdi) != SR_OK)
515 return SR_ERR;
516
517 return SR_OK;
e3b83c5e
SS
518}
519
520static int dev_acquisition_start(const struct sr_dev_inst *sdi)
521{
3f2c7c94
SS
522 GSList *l;
523 struct sr_channel *ch;
524 struct dev_context *devc;
525 int ret;
526 struct sr_scpi_dev_inst *scpi;
527
3f2c7c94
SS
528 devc = sdi->priv;
529 scpi = sdi->conn;
530 /* Preset empty results. */
531 g_slist_free(devc->enabled_channels);
532 devc->enabled_channels = NULL;
533
534 /*
535 * Contruct the list of enabled channels. Determine the highest
536 * number of digital pods involved in the acquisition.
537 */
538
539 for (l = sdi->channels; l; l = l->next) {
540 ch = l->data;
541 if (!ch->enabled)
542 continue;
543 /* Only add a single digital channel per group (pod). */
544 devc->enabled_channels = g_slist_append(
545 devc->enabled_channels, ch);
546 }
e3b83c5e 547
3f2c7c94
SS
548 if (!devc->enabled_channels)
549 return SR_ERR;
550
551 /*
552 * Configure the analog channels and the
553 * corresponding digital pods.
554 */
6d13a46c 555 if (setup_channels(sdi) != SR_OK) {
3f2c7c94
SS
556 sr_err("Failed to setup channel configuration!");
557 ret = SR_ERR;
558 goto free_enabled;
559 }
560
561 /*
562 * Start acquisition on the first enabled channel. The
563 * receive routine will continue driving the acquisition.
564 */
565 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
566 lecroy_xstream_receive_data, (void *)sdi);
567
568 std_session_send_df_header(sdi);
569
570 devc->current_channel = devc->enabled_channels;
571
572 return lecroy_xstream_request_data(sdi);
573
574free_enabled:
575 g_slist_free(devc->enabled_channels);
576 devc->enabled_channels = NULL;
ea257cdc 577
3f2c7c94 578 return ret;
e3b83c5e
SS
579}
580
581static int dev_acquisition_stop(struct sr_dev_inst *sdi)
582{
3f2c7c94
SS
583 struct dev_context *devc;
584 struct sr_scpi_dev_inst *scpi;
585
586 std_session_send_df_end(sdi);
587
3f2c7c94
SS
588 devc = sdi->priv;
589
590 devc->num_frames = 0;
591 g_slist_free(devc->enabled_channels);
592 devc->enabled_channels = NULL;
593 scpi = sdi->conn;
594 sr_scpi_source_remove(sdi->session, scpi);
e3b83c5e
SS
595
596 return SR_OK;
597}
598
3f2c7c94 599static struct sr_dev_driver lecroy_xstream_driver_info = {
e3b83c5e 600 .name = "lecroy-xstream",
bb08570f 601 .longname = "LeCroy X-Stream",
e3b83c5e
SS
602 .api_version = 1,
603 .init = std_init,
604 .cleanup = std_cleanup,
605 .scan = scan,
606 .dev_list = std_dev_list,
607 .dev_clear = dev_clear,
608 .config_get = config_get,
609 .config_set = config_set,
610 .config_list = config_list,
611 .dev_open = dev_open,
612 .dev_close = dev_close,
613 .dev_acquisition_start = dev_acquisition_start,
614 .dev_acquisition_stop = dev_acquisition_stop,
615 .context = NULL,
616};
e3b83c5e 617SR_REGISTER_DEV_DRIVER(lecroy_xstream_driver_info);