]> sigrok.org Git - libsigrok.git/blame - src/hardware/lecroy-xstream/api.c
drivers: Factor out std_cg_idx().
[libsigrok.git] / src / hardware / lecroy-xstream / api.c
CommitLineData
e3b83c5e
SS
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>
3f2c7c94
SS
21#include <stdlib.h>
22#include "scpi.h"
e3b83c5e
SS
23#include "protocol.h"
24
3f2c7c94
SS
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
90230cfa
SA
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,
86621306 41 SR_CONF_SAMPLERATE | SR_CONF_GET,
90230cfa
SA
42 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43 SR_CONF_NUM_HDIV | SR_CONF_GET,
90230cfa 44 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
86621306
UH
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,
90230cfa
SA
47};
48
6b82c3e5 49static const uint32_t devopts_cg_analog[] = {
90230cfa 50 SR_CONF_NUM_VDIV | SR_CONF_GET,
90230cfa 51 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
86621306 52 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
90230cfa
SA
53};
54
373e92a4 55static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
3f2c7c94
SS
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
3f2c7c94
SS
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
697fb6dd 70 if (std_str_idx_s(hw_info->manufacturer, ARRAY_AND_SIZE(manufacturers)) < 0)
3f2c7c94
SS
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
94fail:
95 sr_scpi_hw_info_free(hw_info);
4bf93988 96 sr_dev_inst_free(sdi);
3f2c7c94
SS
97 g_free(devc);
98
99 return NULL;
100}
e3b83c5e
SS
101
102static GSList *scan(struct sr_dev_driver *di, GSList *options)
103{
373e92a4 104 return sr_scpi_scan(di->context, options, probe_device);
3f2c7c94
SS
105}
106
3553451f 107static void clear_helper(struct dev_context *devc)
3f2c7c94 108{
3f2c7c94 109 lecroy_xstream_state_free(devc->model_state);
3f2c7c94 110 g_free(devc->analog_groups);
e3b83c5e
SS
111}
112
113static int dev_clear(const struct sr_dev_driver *di)
114{
3553451f 115 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
e3b83c5e
SS
116}
117
118static int dev_open(struct sr_dev_inst *sdi)
119{
6402c379 120 if (sr_scpi_open(sdi->conn) != SR_OK)
3f2c7c94 121 return SR_ERR;
e3b83c5e 122
3f2c7c94
SS
123 if (lecroy_xstream_state_get(sdi) != SR_OK)
124 return SR_ERR;
e3b83c5e 125
e3b83c5e
SS
126 return SR_OK;
127}
128
129static int dev_close(struct sr_dev_inst *sdi)
130{
f1ba6b4b 131 return sr_scpi_close(sdi->conn);
e3b83c5e
SS
132}
133
134static int config_get(uint32_t key, GVariant **data,
ea257cdc 135 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
e3b83c5e 136{
fcd6a8bd 137 int idx;
3f2c7c94
SS
138 struct dev_context *devc;
139 const struct scope_config *model;
140 struct scope_state *state;
e3b83c5e 141
3f2c7c94
SS
142 if (!sdi)
143 return SR_ERR_ARG;
144
145 devc = sdi->priv;
e3b83c5e 146
3f2c7c94
SS
147 model = devc->model_config;
148 state = devc->model_state;
149 *data = NULL;
ea257cdc 150
e3b83c5e 151 switch (key) {
3f2c7c94
SS
152 case SR_CONF_NUM_HDIV:
153 *data = g_variant_new_int32(model->num_xdivs);
3f2c7c94
SS
154 break;
155 case SR_CONF_TIMEBASE:
156 *data = g_variant_new("(tt)",
76f0fa5d
UH
157 (*model->timebases)[state->timebase][0],
158 (*model->timebases)[state->timebase][1]);
3f2c7c94
SS
159 break;
160 case SR_CONF_NUM_VDIV:
fcd6a8bd
UH
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);
3f2c7c94
SS
164 break;
165 case SR_CONF_VDIV:
fcd6a8bd
UH
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]);
3f2c7c94
SS
171 break;
172 case SR_CONF_TRIGGER_SOURCE:
173 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
3f2c7c94
SS
174 break;
175 case SR_CONF_TRIGGER_SLOPE:
176 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
3f2c7c94
SS
177 break;
178 case SR_CONF_HORIZ_TRIGGERPOS:
179 *data = g_variant_new_double(state->horiz_triggerpos);
3f2c7c94
SS
180 break;
181 case SR_CONF_COUPLING:
fcd6a8bd
UH
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]);
3f2c7c94
SS
185 break;
186 case SR_CONF_SAMPLERATE:
187 *data = g_variant_new_uint64(state->sample_rate);
3f2c7c94
SS
188 break;
189 case SR_CONF_ENABLED:
190 *data = g_variant_new_boolean(FALSE);
3f2c7c94 191 break;
e3b83c5e 192 default:
a9010323 193 return SR_ERR_NA;
e3b83c5e 194 }
ea257cdc 195
a9010323 196 return SR_OK;
e3b83c5e
SS
197}
198
ea257cdc
UH
199static int config_set(uint32_t key, GVariant *data,
200 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
e3b83c5e 201{
fcd6a8bd 202 int ret, idx, j;
3f2c7c94
SS
203 char command[MAX_COMMAND_SIZE];
204 struct dev_context *devc;
205 const struct scope_config *model;
206 struct scope_state *state;
3f2c7c94
SS
207 double tmp_d;
208 gboolean update_sample_rate;
e3b83c5e 209
3f2c7c94
SS
210 if (!sdi)
211 return SR_ERR_ARG;
e3b83c5e 212
3f2c7c94
SS
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;
e3b83c5e 220
e3b83c5e 221 switch (key) {
3f2c7c94
SS
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:
bd633efa
UH
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);
3f2c7c94
SS
233 break;
234 case SR_CONF_VDIV:
bd633efa
UH
235 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
236 return SR_ERR_ARG;
fcd6a8bd
UH
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;
bd633efa 245 ret = SR_OK;
3f2c7c94
SS
246 break;
247 case SR_CONF_TIMEBASE:
bd633efa
UH
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;
3f2c7c94
SS
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) *
76f0fa5d
UH
264 ((double)(*model->timebases)[state->timebase][0] /
265 (*model->timebases)[state->timebase][1])
3f2c7c94
SS
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:
bd633efa
UH
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);
3f2c7c94
SS
279 break;
280 case SR_CONF_COUPLING:
bd633efa
UH
281 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
282 return SR_ERR_ARG;
fcd6a8bd
UH
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;
bd633efa 291 ret = SR_OK;
3f2c7c94 292 break;
e3b83c5e
SS
293 default:
294 ret = SR_ERR_NA;
3f2c7c94 295 break;
e3b83c5e
SS
296 }
297
3f2c7c94
SS
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
e3b83c5e
SS
304 return ret;
305}
306
ea257cdc
UH
307static int config_list(uint32_t key, GVariant **data,
308 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
e3b83c5e 309{
e66d1892
UH
310 struct dev_context *devc;
311 const struct scope_config *model;
90230cfa 312
e66d1892
UH
313 devc = (sdi) ? sdi->priv : NULL;
314 model = (devc) ? devc->model_config : NULL;
90230cfa
SA
315
316 switch (key) {
e66d1892
UH
317 case SR_CONF_SCAN_OPTIONS:
318 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, NULL, NULL);
3f2c7c94 319 case SR_CONF_DEVICE_OPTIONS:
e66d1892
UH
320 if (!cg)
321 return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts);
53012da6 322 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog));
3f2c7c94
SS
323 break;
324 case SR_CONF_COUPLING:
692716f5 325 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
3f2c7c94
SS
326 break;
327 case SR_CONF_TRIGGER_SOURCE:
328 if (!model)
329 return SR_ERR_ARG;
692716f5 330 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
3f2c7c94
SS
331 break;
332 case SR_CONF_TRIGGER_SLOPE:
333 if (!model)
334 return SR_ERR_ARG;
692716f5 335 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
3f2c7c94
SS
336 break;
337 case SR_CONF_TIMEBASE:
338 if (!model)
339 return SR_ERR_ARG;
76f0fa5d 340 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
3f2c7c94
SS
341 break;
342 case SR_CONF_VDIV:
343 if (!model)
344 return SR_ERR_ARG;
76f0fa5d 345 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
3f2c7c94 346 break;
e3b83c5e
SS
347 default:
348 return SR_ERR_NA;
349 }
758906aa 350
3f2c7c94
SS
351 return SR_OK;
352}
e3b83c5e 353
3f2c7c94
SS
354SR_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),
ea257cdc 368 "COMM_FORMAT DEF9,WORD,BIN;C%d:WAVEFORM?", ch->index + 1);
3f2c7c94
SS
369 return sr_scpi_send(sdi->conn, command);
370}
371
6d13a46c 372static int setup_channels(const struct sr_dev_inst *sdi)
3f2c7c94
SS
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",
ea257cdc 394 ch->index + 1, ch->enabled ? "ON" : "OFF");
3f2c7c94
SS
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;
e3b83c5e
SS
410}
411
412static int dev_acquisition_start(const struct sr_dev_inst *sdi)
413{
3f2c7c94
SS
414 GSList *l;
415 struct sr_channel *ch;
416 struct dev_context *devc;
417 int ret;
418 struct sr_scpi_dev_inst *scpi;
419
3f2c7c94
SS
420 devc = sdi->priv;
421 scpi = sdi->conn;
ca314e06 422
3f2c7c94
SS
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 }
e3b83c5e 440
3f2c7c94
SS
441 if (!devc->enabled_channels)
442 return SR_ERR;
443
444 /*
445 * Configure the analog channels and the
446 * corresponding digital pods.
447 */
6d13a46c 448 if (setup_channels(sdi) != SR_OK) {
3f2c7c94
SS
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
467free_enabled:
468 g_slist_free(devc->enabled_channels);
469 devc->enabled_channels = NULL;
ea257cdc 470
3f2c7c94 471 return ret;
e3b83c5e
SS
472}
473
474static int dev_acquisition_stop(struct sr_dev_inst *sdi)
475{
3f2c7c94
SS
476 struct dev_context *devc;
477 struct sr_scpi_dev_inst *scpi;
478
479 std_session_send_df_end(sdi);
480
3f2c7c94
SS
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);
e3b83c5e
SS
488
489 return SR_OK;
490}
491
3f2c7c94 492static struct sr_dev_driver lecroy_xstream_driver_info = {
e3b83c5e 493 .name = "lecroy-xstream",
bb08570f 494 .longname = "LeCroy X-Stream",
e3b83c5e
SS
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};
e3b83c5e 510SR_REGISTER_DEV_DRIVER(lecroy_xstream_driver_info);