]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/lecroy-xstream/api.c
drivers: Replace struct voltage_threshold with an array.
[libsigrok.git] / src / hardware / lecroy-xstream / api.c
... / ...
CommitLineData
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>
21#include <stdlib.h>
22#include "scpi.h"
23#include "protocol.h"
24
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 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,
41 SR_CONF_SAMPLERATE | SR_CONF_GET,
42 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43 SR_CONF_NUM_HDIV | SR_CONF_GET,
44 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
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,
47};
48
49static const uint32_t devopts_cg_analog[] = {
50 SR_CONF_NUM_VDIV | SR_CONF_GET,
51 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
52 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
53};
54
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
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);
107 sr_dev_inst_free(sdi);
108 g_free(devc);
109
110 return NULL;
111}
112
113static GSList *scan(struct sr_dev_driver *di, GSList *options)
114{
115 return sr_scpi_scan(di->context, options, probe_serial_device);
116}
117
118static void clear_helper(struct dev_context *devc)
119{
120 lecroy_xstream_state_free(devc->model_state);
121 g_free(devc->analog_groups);
122}
123
124static int dev_clear(const struct sr_dev_driver *di)
125{
126 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
127}
128
129static int dev_open(struct sr_dev_inst *sdi)
130{
131 if (sr_scpi_open(sdi->conn) != SR_OK)
132 return SR_ERR;
133
134 if (lecroy_xstream_state_get(sdi) != SR_OK)
135 return SR_ERR;
136
137 return SR_OK;
138}
139
140static int dev_close(struct sr_dev_inst *sdi)
141{
142 return sr_scpi_close(sdi->conn);
143}
144
145static int config_get(uint32_t key, GVariant **data,
146 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
147{
148 unsigned int i;
149 struct dev_context *devc;
150 const struct scope_config *model;
151 struct scope_state *state;
152
153 if (!sdi)
154 return SR_ERR_ARG;
155
156 devc = sdi->priv;
157
158 model = devc->model_config;
159 state = devc->model_state;
160 *data = NULL;
161
162 switch (key) {
163 case SR_CONF_NUM_HDIV:
164 *data = g_variant_new_int32(model->num_xdivs);
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 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);
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)",
183 model->vdivs[state->analog_channels[i].vdiv].p,
184 model->vdivs[state->analog_channels[i].vdiv].q);
185 }
186 break;
187 case SR_CONF_TRIGGER_SOURCE:
188 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
189 break;
190 case SR_CONF_TRIGGER_SLOPE:
191 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
192 break;
193 case SR_CONF_HORIZ_TRIGGERPOS:
194 *data = g_variant_new_double(state->horiz_triggerpos);
195 break;
196 case SR_CONF_COUPLING:
197 for (i = 0; i < model->analog_channels; i++) {
198 if (cg != devc->analog_groups[i])
199 continue;
200 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
201 }
202 break;
203 case SR_CONF_SAMPLERATE:
204 *data = g_variant_new_uint64(state->sample_rate);
205 break;
206 case SR_CONF_ENABLED:
207 *data = g_variant_new_boolean(FALSE);
208 break;
209 default:
210 return SR_ERR_NA;
211 }
212
213 return SR_OK;
214}
215
216static int config_set(uint32_t key, GVariant *data,
217 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
218{
219 int ret;
220 unsigned int i, j;
221 char command[MAX_COMMAND_SIZE];
222 struct dev_context *devc;
223 const struct scope_config *model;
224 struct scope_state *state;
225 const char *tmp;
226 int64_t p;
227 uint64_t q;
228 double tmp_d;
229 gboolean update_sample_rate;
230
231 if (!sdi)
232 return SR_ERR_ARG;
233
234 devc = sdi->priv;
235
236 model = devc->model_config;
237 state = devc->model_state;
238 update_sample_rate = FALSE;
239
240 ret = SR_ERR_NA;
241
242 switch (key) {
243 case SR_CONF_LIMIT_FRAMES:
244 devc->frame_limit = g_variant_get_uint64(data);
245 ret = SR_OK;
246 break;
247 case SR_CONF_TRIGGER_SOURCE:
248 tmp = g_variant_get_string(data, NULL);
249 for (i = 0; (*model->trigger_sources)[i]; i++) {
250 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
251 continue;
252 state->trigger_source = i;
253 g_snprintf(command, sizeof(command),
254 "SET TRIGGER SOURCE %s",
255 (*model->trigger_sources)[i]);
256
257 ret = sr_scpi_send(sdi->conn, command);
258 break;
259 }
260 break;
261 case SR_CONF_VDIV:
262 g_variant_get(data, "(tt)", &p, &q);
263
264 for (i = 0; i < model->num_vdivs; i++) {
265 if (p != model->vdivs[i].p || q != model->vdivs[i].q)
266 continue;
267 for (j = 1; j <= model->analog_channels; j++) {
268 if (cg != devc->analog_groups[j - 1])
269 continue;
270 state->analog_channels[j - 1].vdiv = i;
271 g_snprintf(command, sizeof(command),
272 "C%d:VDIV %E", j, (float)p/q);
273
274 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
275 sr_scpi_get_opc(sdi->conn) != SR_OK)
276 return SR_ERR;
277
278 break;
279 }
280
281 ret = SR_OK;
282 break;
283 }
284 break;
285 case SR_CONF_TIMEBASE:
286 g_variant_get(data, "(tt)", &p, &q);
287
288 for (i = 0; i < model->num_timebases; i++) {
289 if (p != model->timebases[i].p ||
290 q != model->timebases[i].q)
291 continue;
292 state->timebase = i;
293 g_snprintf(command, sizeof(command),
294 "TIME_DIV %E", (float)p/q);
295
296 ret = sr_scpi_send(sdi->conn, command);
297 update_sample_rate = TRUE;
298 break;
299 }
300 break;
301 case SR_CONF_HORIZ_TRIGGERPOS:
302 tmp_d = g_variant_get_double(data);
303
304 if (tmp_d < 0.0 || tmp_d > 1.0)
305 return SR_ERR;
306
307 state->horiz_triggerpos = tmp_d;
308 tmp_d = -(tmp_d - 0.5) *
309 ((double)model->timebases[state->timebase].p /
310 model->timebases[state->timebase].q)
311 * model->num_xdivs;
312
313 g_snprintf(command, sizeof(command), "TRIG POS %e S", tmp_d);
314
315 ret = sr_scpi_send(sdi->conn, command);
316 break;
317 case SR_CONF_TRIGGER_SLOPE:
318 tmp = g_variant_get_string(data, NULL);
319 for (i = 0; (*model->trigger_slopes)[i]; i++) {
320 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
321 continue;
322 state->trigger_slope = i;
323 g_snprintf(command, sizeof(command),
324 "SET TRIGGER SLOPE %s",
325 (*model->trigger_slopes)[i]);
326
327 ret = sr_scpi_send(sdi->conn, command);
328 break;
329 }
330 break;
331 case SR_CONF_COUPLING:
332 tmp = g_variant_get_string(data, NULL);
333
334 for (i = 0; (*model->coupling_options)[i]; i++) {
335 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
336 continue;
337 for (j = 1; j <= model->analog_channels; j++) {
338 if (cg != devc->analog_groups[j - 1])
339 continue;
340 state->analog_channels[j - 1].coupling = i;
341
342 g_snprintf(command, sizeof(command),
343 "C%d:COUPLING %s", j, tmp);
344
345 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
346 sr_scpi_get_opc(sdi->conn) != SR_OK)
347 return SR_ERR;
348 break;
349 }
350
351 ret = SR_OK;
352 break;
353 }
354 break;
355 default:
356 ret = SR_ERR_NA;
357 break;
358 }
359
360 if (ret == SR_OK)
361 ret = sr_scpi_get_opc(sdi->conn);
362
363 if (ret == SR_OK && update_sample_rate)
364 ret = lecroy_xstream_update_sample_rate(sdi);
365
366 return ret;
367}
368
369static int config_list(uint32_t key, GVariant **data,
370 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
371{
372 struct dev_context *devc;
373 const struct scope_config *model;
374
375 devc = (sdi) ? sdi->priv : NULL;
376 model = (devc) ? devc->model_config : NULL;
377
378 switch (key) {
379 case SR_CONF_SCAN_OPTIONS:
380 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, NULL, NULL);
381 case SR_CONF_DEVICE_OPTIONS:
382 if (!cg)
383 return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts);
384 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog));
385 break;
386 case SR_CONF_COUPLING:
387 *data = g_variant_new_strv(*model->coupling_options,
388 g_strv_length((char **)*model->coupling_options));
389 break;
390 case SR_CONF_TRIGGER_SOURCE:
391 if (!model)
392 return SR_ERR_ARG;
393 *data = g_variant_new_strv(*model->trigger_sources,
394 g_strv_length((char **)*model->trigger_sources));
395 break;
396 case SR_CONF_TRIGGER_SLOPE:
397 if (!model)
398 return SR_ERR_ARG;
399 *data = g_variant_new_strv(*model->trigger_slopes,
400 g_strv_length((char **)*model->trigger_slopes));
401 break;
402 case SR_CONF_TIMEBASE:
403 if (!model)
404 return SR_ERR_ARG;
405 *data = std_gvar_tuple_rational(model->timebases, model->num_timebases);
406 break;
407 case SR_CONF_VDIV:
408 if (!model)
409 return SR_ERR_ARG;
410 *data = std_gvar_tuple_rational(model->vdivs, model->num_vdivs);
411 break;
412 default:
413 return SR_ERR_NA;
414 }
415 return SR_OK;
416}
417
418SR_PRIV int lecroy_xstream_request_data(const struct sr_dev_inst *sdi)
419{
420 char command[MAX_COMMAND_SIZE];
421 struct sr_channel *ch;
422 struct dev_context *devc;
423
424 devc = sdi->priv;
425
426 ch = devc->current_channel->data;
427
428 if (ch->type != SR_CHANNEL_ANALOG)
429 return SR_ERR;
430
431 g_snprintf(command, sizeof(command),
432 "COMM_FORMAT DEF9,WORD,BIN;C%d:WAVEFORM?", ch->index + 1);
433 return sr_scpi_send(sdi->conn, command);
434}
435
436static int setup_channels(const struct sr_dev_inst *sdi)
437{
438 GSList *l;
439 gboolean setup_changed;
440 char command[MAX_COMMAND_SIZE];
441 struct scope_state *state;
442 struct sr_channel *ch;
443 struct dev_context *devc;
444 struct sr_scpi_dev_inst *scpi;
445
446 devc = sdi->priv;
447 scpi = sdi->conn;
448 state = devc->model_state;
449 setup_changed = FALSE;
450
451 for (l = sdi->channels; l; l = l->next) {
452 ch = l->data;
453 switch (ch->type) {
454 case SR_CHANNEL_ANALOG:
455 if (ch->enabled == state->analog_channels[ch->index].state)
456 break;
457 g_snprintf(command, sizeof(command), "C%d:TRACE %s",
458 ch->index + 1, ch->enabled ? "ON" : "OFF");
459
460 if (sr_scpi_send(scpi, command) != SR_OK)
461 return SR_ERR;
462 state->analog_channels[ch->index].state = ch->enabled;
463 setup_changed = TRUE;
464 break;
465 default:
466 return SR_ERR;
467 }
468 }
469
470 if (setup_changed && lecroy_xstream_update_sample_rate(sdi) != SR_OK)
471 return SR_ERR;
472
473 return SR_OK;
474}
475
476static int dev_acquisition_start(const struct sr_dev_inst *sdi)
477{
478 GSList *l;
479 struct sr_channel *ch;
480 struct dev_context *devc;
481 int ret;
482 struct sr_scpi_dev_inst *scpi;
483
484 devc = sdi->priv;
485 scpi = sdi->conn;
486
487 /* Preset empty results. */
488 g_slist_free(devc->enabled_channels);
489 devc->enabled_channels = NULL;
490
491 /*
492 * Contruct the list of enabled channels. Determine the highest
493 * number of digital pods involved in the acquisition.
494 */
495
496 for (l = sdi->channels; l; l = l->next) {
497 ch = l->data;
498 if (!ch->enabled)
499 continue;
500 /* Only add a single digital channel per group (pod). */
501 devc->enabled_channels = g_slist_append(
502 devc->enabled_channels, ch);
503 }
504
505 if (!devc->enabled_channels)
506 return SR_ERR;
507
508 /*
509 * Configure the analog channels and the
510 * corresponding digital pods.
511 */
512 if (setup_channels(sdi) != SR_OK) {
513 sr_err("Failed to setup channel configuration!");
514 ret = SR_ERR;
515 goto free_enabled;
516 }
517
518 /*
519 * Start acquisition on the first enabled channel. The
520 * receive routine will continue driving the acquisition.
521 */
522 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
523 lecroy_xstream_receive_data, (void *)sdi);
524
525 std_session_send_df_header(sdi);
526
527 devc->current_channel = devc->enabled_channels;
528
529 return lecroy_xstream_request_data(sdi);
530
531free_enabled:
532 g_slist_free(devc->enabled_channels);
533 devc->enabled_channels = NULL;
534
535 return ret;
536}
537
538static int dev_acquisition_stop(struct sr_dev_inst *sdi)
539{
540 struct dev_context *devc;
541 struct sr_scpi_dev_inst *scpi;
542
543 std_session_send_df_end(sdi);
544
545 devc = sdi->priv;
546
547 devc->num_frames = 0;
548 g_slist_free(devc->enabled_channels);
549 devc->enabled_channels = NULL;
550 scpi = sdi->conn;
551 sr_scpi_source_remove(sdi->session, scpi);
552
553 return SR_OK;
554}
555
556static struct sr_dev_driver lecroy_xstream_driver_info = {
557 .name = "lecroy-xstream",
558 .longname = "LeCroy X-Stream",
559 .api_version = 1,
560 .init = std_init,
561 .cleanup = std_cleanup,
562 .scan = scan,
563 .dev_list = std_dev_list,
564 .dev_clear = dev_clear,
565 .config_get = config_get,
566 .config_set = config_set,
567 .config_list = config_list,
568 .dev_open = dev_open,
569 .dev_close = dev_close,
570 .dev_acquisition_start = dev_acquisition_start,
571 .dev_acquisition_stop = dev_acquisition_stop,
572 .context = NULL,
573};
574SR_REGISTER_DEV_DRIVER(lecroy_xstream_driver_info);