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