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