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