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