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