]> sigrok.org Git - libsigrok.git/blob - src/hardware/lecroy-xstream/api.c
drivers: Factor out std_cg_idx().
[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 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
55 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
56 {
57         struct sr_dev_inst *sdi;
58         struct dev_context *devc;
59         struct sr_scpi_hw_info *hw_info;
60
61         sdi = NULL;
62         devc = NULL;
63         hw_info = NULL;
64
65         if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
66                 sr_info("Couldn't get IDN response.");
67                 goto fail;
68         }
69
70         if (std_str_idx_s(hw_info->manufacturer, ARRAY_AND_SIZE(manufacturers)) < 0)
71                 goto fail;
72
73         sdi = g_malloc0(sizeof(struct sr_dev_inst));
74         sdi->vendor = g_strdup(hw_info->manufacturer);
75         sdi->model = g_strdup(hw_info->model);
76         sdi->version = g_strdup(hw_info->firmware_version);
77         sdi->serial_num = g_strdup(hw_info->serial_number);
78         sdi->driver = &lecroy_xstream_driver_info;
79         sdi->inst_type = SR_INST_SCPI;
80         sdi->conn = scpi;
81
82         sr_scpi_hw_info_free(hw_info);
83         hw_info = NULL;
84
85         devc = g_malloc0(sizeof(struct dev_context));
86
87         sdi->priv = devc;
88
89         if (lecroy_xstream_init_device(sdi) != SR_OK)
90                 goto fail;
91
92         return sdi;
93
94 fail:
95         sr_scpi_hw_info_free(hw_info);
96         sr_dev_inst_free(sdi);
97         g_free(devc);
98
99         return NULL;
100 }
101
102 static GSList *scan(struct sr_dev_driver *di, GSList *options)
103 {
104         return sr_scpi_scan(di->context, options, probe_device);
105 }
106
107 static void clear_helper(struct dev_context *devc)
108 {
109         lecroy_xstream_state_free(devc->model_state);
110         g_free(devc->analog_groups);
111 }
112
113 static int dev_clear(const struct sr_dev_driver *di)
114 {
115         return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
116 }
117
118 static int dev_open(struct sr_dev_inst *sdi)
119 {
120         if (sr_scpi_open(sdi->conn) != SR_OK)
121                 return SR_ERR;
122
123         if (lecroy_xstream_state_get(sdi) != SR_OK)
124                 return SR_ERR;
125
126         return SR_OK;
127 }
128
129 static int dev_close(struct sr_dev_inst *sdi)
130 {
131         return sr_scpi_close(sdi->conn);
132 }
133
134 static int config_get(uint32_t key, GVariant **data,
135         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
136 {
137         int idx;
138         struct dev_context *devc;
139         const struct scope_config *model;
140         struct scope_state *state;
141
142         if (!sdi)
143                 return SR_ERR_ARG;
144
145         devc = sdi->priv;
146
147         model = devc->model_config;
148         state = devc->model_state;
149         *data = NULL;
150
151         switch (key) {
152         case SR_CONF_NUM_HDIV:
153                 *data = g_variant_new_int32(model->num_xdivs);
154                 break;
155         case SR_CONF_TIMEBASE:
156                 *data = g_variant_new("(tt)",
157                                 (*model->timebases)[state->timebase][0],
158                                 (*model->timebases)[state->timebase][1]);
159                 break;
160         case SR_CONF_NUM_VDIV:
161                 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) < 0)
162                         return SR_ERR_ARG;
163                 *data = g_variant_new_int32(model->num_ydivs);
164                 break;
165         case SR_CONF_VDIV:
166                 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
167                         return SR_ERR_ARG;
168                 *data = g_variant_new("(tt)",
169                         (*model->vdivs)[state->analog_channels[idx].vdiv][0],
170                         (*model->vdivs)[state->analog_channels[idx].vdiv][1]);
171                 break;
172         case SR_CONF_TRIGGER_SOURCE:
173                 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
174                 break;
175         case SR_CONF_TRIGGER_SLOPE:
176                 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
177                 break;
178         case SR_CONF_HORIZ_TRIGGERPOS:
179                 *data = g_variant_new_double(state->horiz_triggerpos);
180                 break;
181         case SR_CONF_COUPLING:
182                 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
183                         return SR_ERR_ARG;
184                 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]);
185                 break;
186         case SR_CONF_SAMPLERATE:
187                 *data = g_variant_new_uint64(state->sample_rate);
188                 break;
189         case SR_CONF_ENABLED:
190                 *data = g_variant_new_boolean(FALSE);
191                 break;
192         default:
193                 return SR_ERR_NA;
194         }
195
196         return SR_OK;
197 }
198
199 static int config_set(uint32_t key, GVariant *data,
200         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
201 {
202         int ret, idx, j;
203         char command[MAX_COMMAND_SIZE];
204         struct dev_context *devc;
205         const struct scope_config *model;
206         struct scope_state *state;
207         double tmp_d;
208         gboolean update_sample_rate;
209
210         if (!sdi)
211                 return SR_ERR_ARG;
212
213         devc = sdi->priv;
214
215         model = devc->model_config;
216         state = devc->model_state;
217         update_sample_rate = FALSE;
218
219         ret = SR_ERR_NA;
220
221         switch (key) {
222         case SR_CONF_LIMIT_FRAMES:
223                 devc->frame_limit = g_variant_get_uint64(data);
224                 ret = SR_OK;
225                 break;
226         case SR_CONF_TRIGGER_SOURCE:
227                 if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
228                         return SR_ERR_ARG;
229                 state->trigger_source = idx;
230                 g_snprintf(command, sizeof(command),
231                         "SET TRIGGER SOURCE %s", (*model->trigger_sources)[idx]);
232                 ret = sr_scpi_send(sdi->conn, command);
233                 break;
234         case SR_CONF_VDIV:
235                 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
236                         return SR_ERR_ARG;
237                 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
238                         return SR_ERR_ARG;
239                 state->analog_channels[j].vdiv = idx;
240                 g_snprintf(command, sizeof(command),
241                         "C%d:VDIV %E", j + 1, (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
242                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
243                     sr_scpi_get_opc(sdi->conn) != SR_OK)
244                         return SR_ERR;
245                 ret = SR_OK;
246                 break;
247         case SR_CONF_TIMEBASE:
248                 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
249                         return SR_ERR_ARG;
250                 state->timebase = idx;
251                 g_snprintf(command, sizeof(command),
252                         "TIME_DIV %E", (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
253                 ret = sr_scpi_send(sdi->conn, command);
254                 update_sample_rate = TRUE;
255                 break;
256         case SR_CONF_HORIZ_TRIGGERPOS:
257                 tmp_d = g_variant_get_double(data);
258
259                 if (tmp_d < 0.0 || tmp_d > 1.0)
260                         return SR_ERR;
261
262                 state->horiz_triggerpos = tmp_d;
263                 tmp_d = -(tmp_d - 0.5) *
264                         ((double)(*model->timebases)[state->timebase][0] /
265                          (*model->timebases)[state->timebase][1])
266                          * model->num_xdivs;
267
268                 g_snprintf(command, sizeof(command), "TRIG POS %e S", tmp_d);
269
270                 ret = sr_scpi_send(sdi->conn, command);
271                 break;
272         case SR_CONF_TRIGGER_SLOPE:
273                 if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0)
274                         return SR_ERR_ARG;
275                 state->trigger_slope = idx;
276                 g_snprintf(command, sizeof(command),
277                         "SET TRIGGER SLOPE %s", (*model->trigger_slopes)[idx]);
278                 ret = sr_scpi_send(sdi->conn, command);
279                 break;
280         case SR_CONF_COUPLING:
281                 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
282                         return SR_ERR_ARG;
283                 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
284                         return SR_ERR_ARG;
285                 state->analog_channels[j].coupling = idx;
286                 g_snprintf(command, sizeof(command), "C%d:COUPLING %s",
287                                 j + 1, (*model->coupling_options)[idx]);
288                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
289                     sr_scpi_get_opc(sdi->conn) != SR_OK)
290                         return SR_ERR;
291                 ret = SR_OK;
292                 break;
293         default:
294                 ret = SR_ERR_NA;
295                 break;
296         }
297
298         if (ret == SR_OK)
299                 ret = sr_scpi_get_opc(sdi->conn);
300
301         if (ret == SR_OK && update_sample_rate)
302                 ret = lecroy_xstream_update_sample_rate(sdi);
303
304         return ret;
305 }
306
307 static int config_list(uint32_t key, GVariant **data,
308         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
309 {
310         struct dev_context *devc;
311         const struct scope_config *model;
312
313         devc = (sdi) ? sdi->priv : NULL;
314         model = (devc) ? devc->model_config : NULL;
315
316         switch (key) {
317         case SR_CONF_SCAN_OPTIONS:
318                 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, NULL, NULL);
319         case SR_CONF_DEVICE_OPTIONS:
320                 if (!cg)
321                         return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts);
322                 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog));
323                 break;
324         case SR_CONF_COUPLING:
325                 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
326                 break;
327         case SR_CONF_TRIGGER_SOURCE:
328                 if (!model)
329                         return SR_ERR_ARG;
330                 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
331                 break;
332         case SR_CONF_TRIGGER_SLOPE:
333                 if (!model)
334                         return SR_ERR_ARG;
335                 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
336                 break;
337         case SR_CONF_TIMEBASE:
338                 if (!model)
339                         return SR_ERR_ARG;
340                 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
341                 break;
342         case SR_CONF_VDIV:
343                 if (!model)
344                         return SR_ERR_ARG;
345                 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
346                 break;
347         default:
348                 return SR_ERR_NA;
349         }
350
351         return SR_OK;
352 }
353
354 SR_PRIV int lecroy_xstream_request_data(const struct sr_dev_inst *sdi)
355 {
356         char command[MAX_COMMAND_SIZE];
357         struct sr_channel *ch;
358         struct dev_context *devc;
359
360         devc = sdi->priv;
361
362         ch = devc->current_channel->data;
363
364         if (ch->type != SR_CHANNEL_ANALOG)
365                 return SR_ERR;
366
367         g_snprintf(command, sizeof(command),
368                 "COMM_FORMAT DEF9,WORD,BIN;C%d:WAVEFORM?", ch->index + 1);
369         return sr_scpi_send(sdi->conn, command);
370 }
371
372 static int setup_channels(const struct sr_dev_inst *sdi)
373 {
374         GSList *l;
375         gboolean setup_changed;
376         char command[MAX_COMMAND_SIZE];
377         struct scope_state *state;
378         struct sr_channel *ch;
379         struct dev_context *devc;
380         struct sr_scpi_dev_inst *scpi;
381
382         devc = sdi->priv;
383         scpi = sdi->conn;
384         state = devc->model_state;
385         setup_changed = FALSE;
386
387         for (l = sdi->channels; l; l = l->next) {
388                 ch = l->data;
389                 switch (ch->type) {
390                 case SR_CHANNEL_ANALOG:
391                         if (ch->enabled == state->analog_channels[ch->index].state)
392                                 break;
393                         g_snprintf(command, sizeof(command), "C%d:TRACE %s",
394                                    ch->index + 1, ch->enabled ? "ON" : "OFF");
395
396                         if (sr_scpi_send(scpi, command) != SR_OK)
397                                 return SR_ERR;
398                         state->analog_channels[ch->index].state = ch->enabled;
399                         setup_changed = TRUE;
400                         break;
401                 default:
402                         return SR_ERR;
403                 }
404         }
405
406         if (setup_changed && lecroy_xstream_update_sample_rate(sdi) != SR_OK)
407                 return SR_ERR;
408
409         return SR_OK;
410 }
411
412 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
413 {
414         GSList *l;
415         struct sr_channel *ch;
416         struct dev_context *devc;
417         int ret;
418         struct sr_scpi_dev_inst *scpi;
419
420         devc = sdi->priv;
421         scpi = sdi->conn;
422
423         /* Preset empty results. */
424         g_slist_free(devc->enabled_channels);
425         devc->enabled_channels = NULL;
426
427         /*
428          * Contruct the list of enabled channels. Determine the highest
429          * number of digital pods involved in the acquisition.
430          */
431
432         for (l = sdi->channels; l; l = l->next) {
433                 ch = l->data;
434                 if (!ch->enabled)
435                         continue;
436                 /* Only add a single digital channel per group (pod). */
437                 devc->enabled_channels = g_slist_append(
438                         devc->enabled_channels, ch);
439         }
440
441         if (!devc->enabled_channels)
442                 return SR_ERR;
443
444         /*
445          * Configure the analog channels and the
446          * corresponding digital pods.
447          */
448         if (setup_channels(sdi) != SR_OK) {
449                 sr_err("Failed to setup channel configuration!");
450                 ret = SR_ERR;
451                 goto free_enabled;
452         }
453
454         /*
455          * Start acquisition on the first enabled channel. The
456          * receive routine will continue driving the acquisition.
457          */
458         sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
459                         lecroy_xstream_receive_data, (void *)sdi);
460
461         std_session_send_df_header(sdi);
462
463         devc->current_channel = devc->enabled_channels;
464
465         return lecroy_xstream_request_data(sdi);
466
467 free_enabled:
468         g_slist_free(devc->enabled_channels);
469         devc->enabled_channels = NULL;
470
471         return ret;
472 }
473
474 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
475 {
476         struct dev_context *devc;
477         struct sr_scpi_dev_inst *scpi;
478
479         std_session_send_df_end(sdi);
480
481         devc = sdi->priv;
482
483         devc->num_frames = 0;
484         g_slist_free(devc->enabled_channels);
485         devc->enabled_channels = NULL;
486         scpi = sdi->conn;
487         sr_scpi_source_remove(sdi->session, scpi);
488
489         return SR_OK;
490 }
491
492 static struct sr_dev_driver lecroy_xstream_driver_info = {
493         .name = "lecroy-xstream",
494         .longname = "LeCroy X-Stream",
495         .api_version = 1,
496         .init = std_init,
497         .cleanup = std_cleanup,
498         .scan = scan,
499         .dev_list = std_dev_list,
500         .dev_clear = dev_clear,
501         .config_get = config_get,
502         .config_set = config_set,
503         .config_list = config_list,
504         .dev_open = dev_open,
505         .dev_close = dev_close,
506         .dev_acquisition_start = dev_acquisition_start,
507         .dev_acquisition_stop = dev_acquisition_stop,
508         .context = NULL,
509 };
510 SR_REGISTER_DEV_DRIVER(lecroy_xstream_driver_info);