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