]> sigrok.org Git - libsigrok.git/blame - src/hardware/lecroy-xstream/api.c
output/csv: use intermediate time_t var, silence compiler warning
[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));
3f2c7c94
SS
86 sdi->priv = devc;
87
88 if (lecroy_xstream_init_device(sdi) != SR_OK)
89 goto fail;
90
91 return sdi;
92
93fail:
94 sr_scpi_hw_info_free(hw_info);
4bf93988 95 sr_dev_inst_free(sdi);
3f2c7c94
SS
96 g_free(devc);
97
98 return NULL;
99}
e3b83c5e
SS
100
101static GSList *scan(struct sr_dev_driver *di, GSList *options)
102{
373e92a4 103 return sr_scpi_scan(di->context, options, probe_device);
3f2c7c94
SS
104}
105
3553451f 106static void clear_helper(struct dev_context *devc)
3f2c7c94 107{
3f2c7c94 108 lecroy_xstream_state_free(devc->model_state);
3f2c7c94 109 g_free(devc->analog_groups);
e3b83c5e
SS
110}
111
112static int dev_clear(const struct sr_dev_driver *di)
113{
3553451f 114 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
e3b83c5e
SS
115}
116
117static int dev_open(struct sr_dev_inst *sdi)
118{
6402c379 119 if (sr_scpi_open(sdi->conn) != SR_OK)
3f2c7c94 120 return SR_ERR;
e3b83c5e 121
3f2c7c94
SS
122 if (lecroy_xstream_state_get(sdi) != SR_OK)
123 return SR_ERR;
e3b83c5e 124
e3b83c5e
SS
125 return SR_OK;
126}
127
128static int dev_close(struct sr_dev_inst *sdi)
129{
f1ba6b4b 130 return sr_scpi_close(sdi->conn);
e3b83c5e
SS
131}
132
133static int config_get(uint32_t key, GVariant **data,
952c7376 134 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
e3b83c5e 135{
fcd6a8bd 136 int idx;
3f2c7c94
SS
137 struct dev_context *devc;
138 const struct scope_config *model;
139 struct scope_state *state;
e3b83c5e 140
3f2c7c94
SS
141 if (!sdi)
142 return SR_ERR_ARG;
143
144 devc = sdi->priv;
e3b83c5e 145
3f2c7c94
SS
146 model = devc->model_config;
147 state = devc->model_state;
148 *data = NULL;
ea257cdc 149
e3b83c5e 150 switch (key) {
3f2c7c94
SS
151 case SR_CONF_NUM_HDIV:
152 *data = g_variant_new_int32(model->num_xdivs);
3f2c7c94
SS
153 break;
154 case SR_CONF_TIMEBASE:
155 *data = g_variant_new("(tt)",
76f0fa5d
UH
156 (*model->timebases)[state->timebase][0],
157 (*model->timebases)[state->timebase][1]);
3f2c7c94
SS
158 break;
159 case SR_CONF_NUM_VDIV:
fcd6a8bd
UH
160 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) < 0)
161 return SR_ERR_ARG;
162 *data = g_variant_new_int32(model->num_ydivs);
3f2c7c94
SS
163 break;
164 case SR_CONF_VDIV:
fcd6a8bd
UH
165 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
166 return SR_ERR_ARG;
167 *data = g_variant_new("(tt)",
952c7376
SA
168 (*model->vdivs)[state->analog_channels[idx].vdiv][0],
169 (*model->vdivs)[state->analog_channels[idx].vdiv][1]);
3f2c7c94
SS
170 break;
171 case SR_CONF_TRIGGER_SOURCE:
172 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
3f2c7c94
SS
173 break;
174 case SR_CONF_TRIGGER_SLOPE:
175 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
3f2c7c94
SS
176 break;
177 case SR_CONF_HORIZ_TRIGGERPOS:
178 *data = g_variant_new_double(state->horiz_triggerpos);
3f2c7c94
SS
179 break;
180 case SR_CONF_COUPLING:
fcd6a8bd
UH
181 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
182 return SR_ERR_ARG;
183 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]);
3f2c7c94
SS
184 break;
185 case SR_CONF_SAMPLERATE:
186 *data = g_variant_new_uint64(state->sample_rate);
3f2c7c94
SS
187 break;
188 case SR_CONF_ENABLED:
189 *data = g_variant_new_boolean(FALSE);
3f2c7c94 190 break;
e3b83c5e 191 default:
a9010323 192 return SR_ERR_NA;
e3b83c5e 193 }
ea257cdc 194
a9010323 195 return SR_OK;
e3b83c5e
SS
196}
197
ea257cdc 198static int config_set(uint32_t key, GVariant *data,
952c7376 199 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
e3b83c5e 200{
fcd6a8bd 201 int ret, idx, j;
3f2c7c94
SS
202 char command[MAX_COMMAND_SIZE];
203 struct dev_context *devc;
204 const struct scope_config *model;
205 struct scope_state *state;
3f2c7c94 206 double tmp_d;
e3b83c5e 207
3f2c7c94
SS
208 if (!sdi)
209 return SR_ERR_ARG;
e3b83c5e 210
3f2c7c94
SS
211 devc = sdi->priv;
212
213 model = devc->model_config;
214 state = devc->model_state;
3f2c7c94 215
e3b83c5e 216 switch (key) {
3f2c7c94
SS
217 case SR_CONF_LIMIT_FRAMES:
218 devc->frame_limit = g_variant_get_uint64(data);
219 ret = SR_OK;
220 break;
221 case SR_CONF_TRIGGER_SOURCE:
bd633efa
UH
222 if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
223 return SR_ERR_ARG;
224 state->trigger_source = idx;
225 g_snprintf(command, sizeof(command),
5000c12f 226 "TRIG_SELECT EDGE,SR,%s", (*model->trigger_sources)[idx]);
bd633efa 227 ret = sr_scpi_send(sdi->conn, command);
3f2c7c94
SS
228 break;
229 case SR_CONF_VDIV:
bd633efa
UH
230 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
231 return SR_ERR_ARG;
fcd6a8bd
UH
232 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
233 return SR_ERR_ARG;
234 state->analog_channels[j].vdiv = idx;
235 g_snprintf(command, sizeof(command),
952c7376
SA
236 "C%d:VDIV %E", j + 1, (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
237 if (sr_scpi_send(sdi->conn, command) != SR_OK || sr_scpi_get_opc(sdi->conn) != SR_OK)
fcd6a8bd 238 return SR_ERR;
bd633efa 239 ret = SR_OK;
3f2c7c94
SS
240 break;
241 case SR_CONF_TIMEBASE:
bd633efa
UH
242 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
243 return SR_ERR_ARG;
244 state->timebase = idx;
245 g_snprintf(command, sizeof(command),
952c7376 246 "TIME_DIV %E", (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
bd633efa 247 ret = sr_scpi_send(sdi->conn, command);
3f2c7c94
SS
248 break;
249 case SR_CONF_HORIZ_TRIGGERPOS:
250 tmp_d = g_variant_get_double(data);
251
252 if (tmp_d < 0.0 || tmp_d > 1.0)
253 return SR_ERR;
254
255 state->horiz_triggerpos = tmp_d;
256 tmp_d = -(tmp_d - 0.5) *
952c7376
SA
257 ((double)(*model->timebases)[state->timebase][0] /
258 (*model->timebases)[state->timebase][1])
259 * model->num_xdivs;
3f2c7c94
SS
260
261 g_snprintf(command, sizeof(command), "TRIG POS %e S", tmp_d);
262
263 ret = sr_scpi_send(sdi->conn, command);
264 break;
265 case SR_CONF_TRIGGER_SLOPE:
bd633efa
UH
266 if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0)
267 return SR_ERR_ARG;
268 state->trigger_slope = idx;
269 g_snprintf(command, sizeof(command),
5000c12f
SA
270 "%s:TRIG_SLOPE %s", (*model->trigger_sources)[state->trigger_source],
271 (*model->trigger_slopes)[idx]);
bd633efa 272 ret = sr_scpi_send(sdi->conn, command);
3f2c7c94
SS
273 break;
274 case SR_CONF_COUPLING:
bd633efa
UH
275 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
276 return SR_ERR_ARG;
fcd6a8bd
UH
277 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
278 return SR_ERR_ARG;
279 state->analog_channels[j].coupling = idx;
280 g_snprintf(command, sizeof(command), "C%d:COUPLING %s",
281 j + 1, (*model->coupling_options)[idx]);
952c7376 282 if (sr_scpi_send(sdi->conn, command) != SR_OK || sr_scpi_get_opc(sdi->conn) != SR_OK)
fcd6a8bd 283 return SR_ERR;
bd633efa 284 ret = SR_OK;
3f2c7c94 285 break;
e3b83c5e
SS
286 default:
287 ret = SR_ERR_NA;
3f2c7c94 288 break;
e3b83c5e
SS
289 }
290
3f2c7c94
SS
291 if (ret == SR_OK)
292 ret = sr_scpi_get_opc(sdi->conn);
293
e3b83c5e
SS
294 return ret;
295}
296
79100d4e
SA
297static int config_channel_set(const struct sr_dev_inst *sdi,
298 struct sr_channel *ch, unsigned int changes)
299{
300 /* Currently we only handle SR_CHANNEL_SET_ENABLED. */
301 if (changes != SR_CHANNEL_SET_ENABLED)
302 return SR_ERR_NA;
303
304 return lecroy_xstream_channel_state_set(sdi, ch->index, ch->enabled);
305}
306
ea257cdc 307static int config_list(uint32_t key, GVariant **data,
952c7376 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 317 case SR_CONF_SCAN_OPTIONS:
23772462 318 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, NO_OPTS, NO_OPTS);
3f2c7c94 319 case SR_CONF_DEVICE_OPTIONS:
e66d1892 320 if (!cg)
23772462 321 return STD_CONFIG_LIST(key, data, sdi, cg, NO_OPTS, drvopts, devopts);
53012da6 322 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog));
3f2c7c94
SS
323 break;
324 case SR_CONF_COUPLING:
f62c2ad6
GS
325 if (!model)
326 return SR_ERR_ARG;
692716f5 327 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
3f2c7c94
SS
328 break;
329 case SR_CONF_TRIGGER_SOURCE:
330 if (!model)
331 return SR_ERR_ARG;
692716f5 332 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
3f2c7c94
SS
333 break;
334 case SR_CONF_TRIGGER_SLOPE:
335 if (!model)
336 return SR_ERR_ARG;
692716f5 337 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
3f2c7c94
SS
338 break;
339 case SR_CONF_TIMEBASE:
340 if (!model)
341 return SR_ERR_ARG;
76f0fa5d 342 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
3f2c7c94
SS
343 break;
344 case SR_CONF_VDIV:
345 if (!model)
346 return SR_ERR_ARG;
76f0fa5d 347 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
3f2c7c94 348 break;
e3b83c5e
SS
349 default:
350 return SR_ERR_NA;
351 }
758906aa 352
3f2c7c94
SS
353 return SR_OK;
354}
e3b83c5e 355
3f2c7c94
SS
356SR_PRIV int lecroy_xstream_request_data(const struct sr_dev_inst *sdi)
357{
358 char command[MAX_COMMAND_SIZE];
359 struct sr_channel *ch;
360 struct dev_context *devc;
361
362 devc = sdi->priv;
724b4945
SA
363
364 /*
365 * We may be left with an invalid current_channel if acquisition was
366 * already stopped but we are processing the last pending events.
367 */
368 if (!devc->current_channel)
369 return SR_ERR_NA;
370
3f2c7c94
SS
371 ch = devc->current_channel->data;
372
373 if (ch->type != SR_CHANNEL_ANALOG)
374 return SR_ERR;
375
952c7376 376 g_snprintf(command, sizeof(command), "C%d:WAVEFORM?", ch->index + 1);
3f2c7c94
SS
377 return sr_scpi_send(sdi->conn, command);
378}
379
6d13a46c 380static int setup_channels(const struct sr_dev_inst *sdi)
3f2c7c94
SS
381{
382 GSList *l;
3f2c7c94
SS
383 char command[MAX_COMMAND_SIZE];
384 struct scope_state *state;
385 struct sr_channel *ch;
386 struct dev_context *devc;
387 struct sr_scpi_dev_inst *scpi;
388
389 devc = sdi->priv;
390 scpi = sdi->conn;
391 state = devc->model_state;
3f2c7c94
SS
392
393 for (l = sdi->channels; l; l = l->next) {
394 ch = l->data;
395 switch (ch->type) {
396 case SR_CHANNEL_ANALOG:
397 if (ch->enabled == state->analog_channels[ch->index].state)
398 break;
399 g_snprintf(command, sizeof(command), "C%d:TRACE %s",
952c7376 400 ch->index + 1, ch->enabled ? "ON" : "OFF");
3f2c7c94
SS
401
402 if (sr_scpi_send(scpi, command) != SR_OK)
403 return SR_ERR;
952c7376 404
3f2c7c94 405 state->analog_channels[ch->index].state = ch->enabled;
3f2c7c94
SS
406 break;
407 default:
408 return SR_ERR;
409 }
410 }
411
3f2c7c94 412 return SR_OK;
e3b83c5e
SS
413}
414
415static int dev_acquisition_start(const struct sr_dev_inst *sdi)
416{
3f2c7c94
SS
417 GSList *l;
418 struct sr_channel *ch;
419 struct dev_context *devc;
e0b6855b 420 struct scope_state *state;
3f2c7c94
SS
421 int ret;
422 struct sr_scpi_dev_inst *scpi;
423
3f2c7c94
SS
424 devc = sdi->priv;
425 scpi = sdi->conn;
ca314e06 426
3f2c7c94
SS
427 /* Preset empty results. */
428 g_slist_free(devc->enabled_channels);
429 devc->enabled_channels = NULL;
e0b6855b
SA
430 state = devc->model_state;
431 state->sample_rate = 0;
3f2c7c94 432
6e9606db 433 /* Contruct the list of enabled channels. */
3f2c7c94
SS
434 for (l = sdi->channels; l; l = l->next) {
435 ch = l->data;
436 if (!ch->enabled)
437 continue;
6e9606db
SA
438
439 devc->enabled_channels = g_slist_append(devc->enabled_channels, ch);
3f2c7c94 440 }
e3b83c5e 441
3f2c7c94
SS
442 if (!devc->enabled_channels)
443 return SR_ERR;
444
6e9606db 445 /* Configure the analog channels. */
6d13a46c 446 if (setup_channels(sdi) != SR_OK) {
3f2c7c94
SS
447 sr_err("Failed to setup channel configuration!");
448 ret = SR_ERR;
449 goto free_enabled;
450 }
451
452 /*
453 * Start acquisition on the first enabled channel. The
454 * receive routine will continue driving the acquisition.
455 */
456 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
457 lecroy_xstream_receive_data, (void *)sdi);
458
459 std_session_send_df_header(sdi);
460
461 devc->current_channel = devc->enabled_channels;
462
463 return lecroy_xstream_request_data(sdi);
464
465free_enabled:
466 g_slist_free(devc->enabled_channels);
467 devc->enabled_channels = NULL;
ea257cdc 468
3f2c7c94 469 return ret;
e3b83c5e
SS
470}
471
472static int dev_acquisition_stop(struct sr_dev_inst *sdi)
473{
3f2c7c94
SS
474 struct dev_context *devc;
475 struct sr_scpi_dev_inst *scpi;
476
477 std_session_send_df_end(sdi);
478
3f2c7c94
SS
479 devc = sdi->priv;
480
481 devc->num_frames = 0;
482 g_slist_free(devc->enabled_channels);
483 devc->enabled_channels = NULL;
484 scpi = sdi->conn;
485 sr_scpi_source_remove(sdi->session, scpi);
e3b83c5e
SS
486
487 return SR_OK;
488}
489
3f2c7c94 490static struct sr_dev_driver lecroy_xstream_driver_info = {
e3b83c5e 491 .name = "lecroy-xstream",
bb08570f 492 .longname = "LeCroy X-Stream",
e3b83c5e
SS
493 .api_version = 1,
494 .init = std_init,
495 .cleanup = std_cleanup,
496 .scan = scan,
497 .dev_list = std_dev_list,
498 .dev_clear = dev_clear,
499 .config_get = config_get,
500 .config_set = config_set,
79100d4e 501 .config_channel_set = config_channel_set,
e3b83c5e
SS
502 .config_list = config_list,
503 .dev_open = dev_open,
504 .dev_close = dev_close,
505 .dev_acquisition_start = dev_acquisition_start,
506 .dev_acquisition_stop = dev_acquisition_stop,
507 .context = NULL,
508};
e3b83c5e 509SR_REGISTER_DEV_DRIVER(lecroy_xstream_driver_info);