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