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