]> sigrok.org Git - libsigrok.git/blame - src/hardware/yokogawa-dlm/api.c
yokogawa-dlm: Introduce config_channel_set()
[libsigrok.git] / src / hardware / yokogawa-dlm / api.c
CommitLineData
10763937
SA
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 abraxa (Soeren Apel) <soeren@apelpie.net>
5 * Based on the Hameg HMO driver by poljar (Damir Jelić) <poljarinho@gmail.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <stdlib.h>
22#include "protocol.h"
23
8ab929d6 24SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info;
8ab929d6 25
329733d9
UH
26static const char *MANUFACTURER_ID = "YOKOGAWA";
27static const char *MANUFACTURER_NAME = "Yokogawa";
8ab929d6 28
f3c60fb6
SA
29static const uint32_t dlm_scanopts[] = {
30 SR_CONF_CONN,
31};
32
33static const uint32_t dlm_drvopts[] = {
cf0280fa
AJ
34 SR_CONF_LOGIC_ANALYZER,
35 SR_CONF_OSCILLOSCOPE,
36};
37
f3c60fb6
SA
38static const uint32_t dlm_devopts[] = {
39 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
40 SR_CONF_SAMPLERATE | SR_CONF_GET,
41 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
42 SR_CONF_NUM_HDIV | SR_CONF_GET,
43 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
44 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
45 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46};
47
48static const uint32_t dlm_analog_devopts[] = {
49 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
50 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
51 SR_CONF_NUM_VDIV | SR_CONF_GET,
52};
53
54static const uint32_t dlm_digital_devopts[] = {
55};
56
8ab929d6
SA
57enum {
58 CG_INVALID = -1,
59 CG_NONE,
60 CG_ANALOG,
61 CG_DIGITAL,
62};
63
4f840ce9 64static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
8ab929d6
SA
65{
66 return std_init(sr_ctx, di, LOG_PREFIX);
67}
68
69static struct sr_dev_inst *probe_usbtmc_device(struct sr_scpi_dev_inst *scpi)
70{
71 struct sr_dev_inst *sdi;
72 struct dev_context *devc;
73 struct sr_scpi_hw_info *hw_info;
74 char *model_name;
75 int model_index;
76
77 sdi = NULL;
78 devc = NULL;
79 hw_info = NULL;
80
81 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
82 sr_info("Couldn't get IDN response.");
83 goto fail;
84 }
85
86 if (strcmp(hw_info->manufacturer, MANUFACTURER_ID) != 0)
87 goto fail;
88
89 if (dlm_model_get(hw_info->model, &model_name, &model_index) != SR_OK)
90 goto fail;
91
aac29cc1 92 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
93 sdi->status = SR_ST_ACTIVE;
94 sdi->vendor = g_strdup(MANUFACTURER_NAME);
95 sdi->model = g_strdup(model_name);
96 sdi->version = g_strdup(hw_info->firmware_version);
8ab929d6 97
d1314831
SA
98 sdi->serial_num = g_strdup(hw_info->serial_number);
99
8ab929d6
SA
100 sr_scpi_hw_info_free(hw_info);
101 hw_info = NULL;
102
ac10a927 103 devc = g_malloc0(sizeof(struct dev_context));
8ab929d6 104
4f840ce9 105 sdi->driver = &yokogawa_dlm_driver_info;
8ab929d6
SA
106 sdi->priv = devc;
107 sdi->inst_type = SR_INST_SCPI;
108 sdi->conn = scpi;
109
110 if (dlm_device_init(sdi, model_index) != SR_OK)
111 goto fail;
112
113 sr_scpi_close(sdi->conn);
114
115 sdi->status = SR_ST_INACTIVE;
116 return sdi;
117
118fail:
119 if (hw_info)
120 sr_scpi_hw_info_free(hw_info);
121 if (sdi)
122 sr_dev_inst_free(sdi);
b1f83103 123 g_free(devc);
8ab929d6
SA
124
125 return NULL;
126}
127
4f840ce9 128static GSList *scan(struct sr_dev_driver *di, GSList *options)
8ab929d6
SA
129{
130 return sr_scpi_scan(di->priv, options, probe_usbtmc_device);
131}
132
4f840ce9 133static GSList *dev_list(const struct sr_dev_driver *di)
8ab929d6
SA
134{
135 return ((struct drv_context *)(di->priv))->instances;
136}
137
138static void clear_helper(void *priv)
139{
140 struct dev_context *devc;
141
142 devc = priv;
143
144 dlm_scope_state_destroy(devc->model_state);
145
146 g_free(devc->analog_groups);
147 g_free(devc->digital_groups);
148 g_free(devc);
149}
150
4f840ce9 151static int dev_clear(const struct sr_dev_driver *di)
8ab929d6
SA
152{
153 return std_dev_clear(di, clear_helper);
154}
155
156static int dev_open(struct sr_dev_inst *sdi)
157{
158 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
159 return SR_ERR;
160
161 if (dlm_scope_state_query(sdi) != SR_OK)
162 return SR_ERR;
163
164 sdi->status = SR_ST_ACTIVE;
165
166 return SR_OK;
167}
168
169static int dev_close(struct sr_dev_inst *sdi)
170{
171 if (sdi->status == SR_ST_INACTIVE)
172 return SR_OK;
173
174 sr_scpi_close(sdi->conn);
175
176 sdi->status = SR_ST_INACTIVE;
177
178 return SR_OK;
179}
180
4f840ce9 181static int cleanup(const struct sr_dev_driver *di)
8ab929d6 182{
4f840ce9 183 dev_clear(di);
8ab929d6
SA
184
185 return SR_OK;
186}
187
188/**
189 * Check which category a given channel group belongs to.
190 *
191 * @param devc Our internal device context.
192 * @param cg The channel group to check.
193 *
194 * @retval CG_NONE cg is NULL
195 * @retval CG_ANALOG cg is an analog group
196 * @retval CG_DIGITAL cg is a digital group
197 * @retval CG_INVALID cg is something else
198 */
199static int check_channel_group(struct dev_context *devc,
ac10a927 200 const struct sr_channel_group *cg)
8ab929d6
SA
201{
202 unsigned int i;
329733d9 203 const struct scope_config *model;
8ab929d6
SA
204
205 model = devc->model_config;
206
207 if (!cg)
208 return CG_NONE;
209
210 for (i = 0; i < model->analog_channels; ++i)
211 if (cg == devc->analog_groups[i])
212 return CG_ANALOG;
213
214 for (i = 0; i < model->pods; ++i)
215 if (cg == devc->digital_groups[i])
216 return CG_DIGITAL;
217
218 sr_err("Invalid channel group specified.");
219 return CG_INVALID;
220}
221
584560f1 222static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
ac10a927 223 const struct sr_channel_group *cg)
8ab929d6
SA
224{
225 int ret, cg_type;
226 unsigned int i;
227 struct dev_context *devc;
329733d9 228 const struct scope_config *model;
8ab929d6
SA
229 struct scope_state *state;
230
231 if (!sdi || !(devc = sdi->priv))
232 return SR_ERR_ARG;
233
234 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
235 return SR_ERR;
236
237 ret = SR_ERR_NA;
238 model = devc->model_config;
239 state = devc->model_state;
240
241 switch (key) {
bf622e6d 242 case SR_CONF_NUM_HDIV:
8ab929d6
SA
243 *data = g_variant_new_int32(model->num_xdivs);
244 ret = SR_OK;
245 break;
246 case SR_CONF_TIMEBASE:
247 *data = g_variant_new("(tt)",
f3c60fb6
SA
248 dlm_timebases[state->timebase][0],
249 dlm_timebases[state->timebase][1]);
8ab929d6
SA
250 ret = SR_OK;
251 break;
252 case SR_CONF_NUM_VDIV:
253 if (cg_type == CG_NONE) {
254 sr_err("No channel group specified.");
255 return SR_ERR_CHANNEL_GROUP;
256 } else if (cg_type == CG_ANALOG) {
257 *data = g_variant_new_int32(model->num_ydivs);
258 ret = SR_OK;
259 break;
260 } else {
261 ret = SR_ERR_NA;
262 }
263 break;
264 case SR_CONF_VDIV:
265 ret = SR_ERR_NA;
266 if (cg_type == CG_NONE) {
267 sr_err("No channel group specified.");
268 return SR_ERR_CHANNEL_GROUP;
269 } else if (cg_type != CG_ANALOG)
270 break;
271
272 for (i = 0; i < model->analog_channels; ++i) {
273 if (cg != devc->analog_groups[i])
274 continue;
275 *data = g_variant_new("(tt)",
f3c60fb6
SA
276 dlm_vdivs[state->analog_states[i].vdiv][0],
277 dlm_vdivs[state->analog_states[i].vdiv][1]);
8ab929d6
SA
278 ret = SR_OK;
279 break;
280 }
281 break;
282 case SR_CONF_TRIGGER_SOURCE:
283 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
284 ret = SR_OK;
285 break;
286 case SR_CONF_TRIGGER_SLOPE:
f3c60fb6 287 *data = g_variant_new_string(dlm_trigger_slopes[state->trigger_slope]);
8ab929d6
SA
288 ret = SR_OK;
289 break;
290 case SR_CONF_HORIZ_TRIGGERPOS:
291 *data = g_variant_new_double(state->horiz_triggerpos);
292 ret = SR_OK;
293 break;
294 case SR_CONF_COUPLING:
295 ret = SR_ERR_NA;
296 if (cg_type == CG_NONE) {
297 sr_err("No channel group specified.");
298 return SR_ERR_CHANNEL_GROUP;
299 } else if (cg_type != CG_ANALOG)
300 break;
301
302 for (i = 0; i < model->analog_channels; ++i) {
303 if (cg != devc->analog_groups[i])
304 continue;
305 *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
306 ret = SR_OK;
307 break;
308 }
309 break;
310 case SR_CONF_SAMPLERATE:
311 *data = g_variant_new_uint64(state->sample_rate);
312 ret = SR_OK;
313 break;
314 default:
315 ret = SR_ERR_NA;
316 }
317
318 return ret;
319}
320
321static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
322{
323 unsigned int i;
324 GVariant *rational[2];
325 GVariantBuilder gvb;
326
327 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
328
329 for (i = 0; i < n; i++) {
330 rational[0] = g_variant_new_uint64((*array)[i][0]);
331 rational[1] = g_variant_new_uint64((*array)[i][1]);
332
333 /* FIXME: Valgrind reports a memory leak here. */
334 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
335 }
336
337 return g_variant_builder_end(&gvb);
338}
339
584560f1 340static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
ac10a927 341 const struct sr_channel_group *cg)
8ab929d6
SA
342{
343 int ret, cg_type;
344 unsigned int i, j;
345 char float_str[30];
346 struct dev_context *devc;
329733d9 347 const struct scope_config *model;
8ab929d6
SA
348 struct scope_state *state;
349 const char *tmp;
350 uint64_t p, q;
351 double tmp_d;
352 gboolean update_sample_rate;
353
354 if (!sdi || !(devc = sdi->priv))
355 return SR_ERR_ARG;
356
357 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
358 return SR_ERR;
359
360 model = devc->model_config;
361 state = devc->model_state;
362 update_sample_rate = FALSE;
363
364 ret = SR_ERR_NA;
365
366 switch (key) {
367 case SR_CONF_LIMIT_FRAMES:
368 devc->frame_limit = g_variant_get_uint64(data);
369 ret = SR_OK;
370 break;
371 case SR_CONF_TRIGGER_SOURCE:
372 tmp = g_variant_get_string(data, NULL);
373 for (i = 0; (*model->trigger_sources)[i]; i++) {
374 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
375 continue;
376 state->trigger_source = i;
377 /* TODO: A and B trigger support possible? */
378 ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
379 break;
380 }
381 break;
382 case SR_CONF_VDIV:
383 if (cg_type == CG_NONE) {
384 sr_err("No channel group specified.");
385 return SR_ERR_CHANNEL_GROUP;
386 }
387
388 g_variant_get(data, "(tt)", &p, &q);
389
f3c60fb6
SA
390 for (i = 0; i < ARRAY_SIZE(dlm_vdivs); i++) {
391 if (p != dlm_vdivs[i][0] ||
392 q != dlm_vdivs[i][1])
8ab929d6
SA
393 continue;
394 for (j = 1; j <= model->analog_channels; ++j) {
395 if (cg != devc->analog_groups[j - 1])
396 continue;
397 state->analog_states[j - 1].vdiv = i;
398 g_ascii_formatd(float_str, sizeof(float_str),
399 "%E", (float) p / q);
400 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
ac10a927 401 sr_scpi_get_opc(sdi->conn) != SR_OK)
8ab929d6
SA
402 return SR_ERR;
403
404 break;
405 }
406
407 ret = SR_OK;
408 break;
409 }
410 break;
411 case SR_CONF_TIMEBASE:
412 g_variant_get(data, "(tt)", &p, &q);
413
f3c60fb6
SA
414 for (i = 0; i < ARRAY_SIZE(dlm_timebases); i++) {
415 if (p != dlm_timebases[i][0] ||
416 q != dlm_timebases[i][1])
8ab929d6
SA
417 continue;
418 state->timebase = i;
419 g_ascii_formatd(float_str, sizeof(float_str),
420 "%E", (float) p / q);
421 ret = dlm_timebase_set(sdi->conn, float_str);
422 update_sample_rate = TRUE;
423 break;
424 }
425 break;
426 case SR_CONF_HORIZ_TRIGGERPOS:
427 tmp_d = g_variant_get_double(data);
428
429 /* TODO: Check if the calculation makes sense for the DLM. */
430 if (tmp_d < 0.0 || tmp_d > 1.0)
431 return SR_ERR;
432
433 state->horiz_triggerpos = tmp_d;
434 tmp_d = -(tmp_d - 0.5) *
f3c60fb6
SA
435 ((double) dlm_timebases[state->timebase][0] /
436 dlm_timebases[state->timebase][1])
ac10a927 437 * model->num_xdivs;
8ab929d6
SA
438
439 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
440 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
441 break;
442 case SR_CONF_TRIGGER_SLOPE:
443 tmp = g_variant_get_string(data, NULL);
444
445 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
446 return SR_ERR_ARG;
447
448 /* Note: See dlm_trigger_slopes[] in protocol.c. */
449 state->trigger_slope = (tmp[0] == 'r') ?
450 SLOPE_POSITIVE : SLOPE_NEGATIVE;
451
452 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
453 break;
454 case SR_CONF_COUPLING:
455 if (cg_type == CG_NONE) {
456 sr_err("No channel group specified.");
457 return SR_ERR_CHANNEL_GROUP;
458 }
459
460 tmp = g_variant_get_string(data, NULL);
461
462 for (i = 0; (*model->coupling_options)[i]; i++) {
463 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
464 continue;
465 for (j = 1; j <= model->analog_channels; ++j) {
466 if (cg != devc->analog_groups[j - 1])
467 continue;
468 state->analog_states[j-1].coupling = i;
469
470 if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
ac10a927 471 sr_scpi_get_opc(sdi->conn) != SR_OK)
8ab929d6
SA
472 return SR_ERR;
473 break;
474 }
475
476 ret = SR_OK;
477 break;
478 }
479 break;
480 default:
481 ret = SR_ERR_NA;
482 break;
483 }
484
485 if (ret == SR_OK)
486 ret = sr_scpi_get_opc(sdi->conn);
487
488 if (ret == SR_OK && update_sample_rate)
489 ret = dlm_sample_rate_query(sdi);
490
491 return ret;
492}
493
c65a021c
SA
494static int config_channel_set(const struct sr_dev_inst *sdi,
495 struct sr_channel *ch, unsigned int changes)
496{
497 /* Curretly we only handle SR_CHANNEL_SET_ENABLED. */
498 if (changes != SR_CHANNEL_SET_ENABLED)
499 return SR_ERR_NA;
500
501 return dlm_channel_state_set(sdi, ch->index, ch->enabled);
502}
503
584560f1 504static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
ac10a927 505 const struct sr_channel_group *cg)
8ab929d6 506{
cf0280fa
AJ
507 int cg_type = CG_NONE;
508 struct dev_context *devc = NULL;
329733d9 509 const struct scope_config *model = NULL;
8ab929d6 510
f3c60fb6
SA
511 /* SR_CONF_SCAN_OPTIONS is always valid, regardless of sdi or probe group. */
512 if (key == SR_CONF_SCAN_OPTIONS) {
513 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
514 dlm_scanopts, ARRAY_SIZE(dlm_scanopts), sizeof(uint32_t));
515 return SR_OK;
516 }
8ab929d6 517
f3c60fb6
SA
518 /* If sdi is NULL, nothing except SR_CONF_DEVICE_OPTIONS can be provided. */
519 if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
520 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
521 dlm_drvopts, ARRAY_SIZE(dlm_drvopts), sizeof(uint32_t));
522 return SR_OK;
cf0280fa 523 }
8ab929d6 524
f3c60fb6
SA
525 if (!sdi)
526 return SR_ERR_ARG;
527
528 devc = sdi->priv;
529 model = devc->model_config;
530
531 /* If cg is NULL, only the SR_CONF_DEVICE_OPTIONS that are not
532 * specific to a probe group must be returned. */
533 if (!cg) {
534 switch (key) {
535 case SR_CONF_DEVICE_OPTIONS:
536 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
537 dlm_devopts, ARRAY_SIZE(dlm_devopts), sizeof(uint32_t));
538 return SR_OK;
539 case SR_CONF_TIMEBASE:
540 *data = build_tuples(&dlm_timebases, ARRAY_SIZE(dlm_timebases));
541 return SR_OK;
542 case SR_CONF_TRIGGER_SOURCE:
543 if (!model)
544 return SR_ERR_ARG;
545 *data = g_variant_new_strv(*model->trigger_sources,
546 g_strv_length((char **)*model->trigger_sources));
547 return SR_OK;
548 case SR_CONF_TRIGGER_SLOPE:
549 *data = g_variant_new_strv(dlm_trigger_slopes,
550 g_strv_length((char **)dlm_trigger_slopes));
551 return SR_OK;
552 case SR_CONF_NUM_HDIV:
553 *data = g_variant_new_uint32(ARRAY_SIZE(dlm_timebases));
554 return SR_OK;
555 default:
556 return SR_ERR_NA;
557 }
558 }
559
560 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
561 return SR_ERR;
562
8ab929d6 563 switch (key) {
8ab929d6 564 case SR_CONF_DEVICE_OPTIONS:
f3c60fb6 565 if (cg_type == CG_ANALOG) {
584560f1 566 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f3c60fb6
SA
567 dlm_analog_devopts, ARRAY_SIZE(dlm_analog_devopts), sizeof(uint32_t));
568 } else if (cg_type == CG_DIGITAL) {
569 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
570 dlm_digital_devopts, ARRAY_SIZE(dlm_digital_devopts), sizeof(uint32_t));
8ab929d6 571 } else {
584560f1
BV
572 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
573 NULL, 0, sizeof(uint32_t));
8ab929d6
SA
574 }
575 break;
576 case SR_CONF_COUPLING:
577 if (cg_type == CG_NONE)
578 return SR_ERR_CHANNEL_GROUP;
579 *data = g_variant_new_strv(*model->coupling_options,
ac10a927 580 g_strv_length((char **)*model->coupling_options));
8ab929d6 581 break;
8ab929d6
SA
582 case SR_CONF_VDIV:
583 if (cg_type == CG_NONE)
584 return SR_ERR_CHANNEL_GROUP;
f3c60fb6 585 *data = build_tuples(&dlm_vdivs, ARRAY_SIZE(dlm_vdivs));
8ab929d6
SA
586 break;
587 default:
588 return SR_ERR_NA;
589 }
590
591 return SR_OK;
592}
593
594static int dlm_check_channels(GSList *channels)
595{
596 GSList *l;
597 struct sr_channel *ch;
598 gboolean enabled_pod1, enabled_chan4;
599
600 enabled_pod1 = enabled_chan4 = FALSE;
601
602 /* Note: On the DLM2000, CH4 and Logic are shared. */
603 /* TODO Handle non-DLM2000 models. */
604 for (l = channels; l; l = l->next) {
605 ch = l->data;
606 switch (ch->type) {
607 case SR_CHANNEL_ANALOG:
608 if (ch->index == 3)
609 enabled_chan4 = TRUE;
610 break;
611 case SR_CHANNEL_LOGIC:
612 enabled_pod1 = TRUE;
613 break;
614 default:
615 return SR_ERR;
616 }
617 }
618
619 if (enabled_pod1 && enabled_chan4)
620 return SR_ERR;
621
622 return SR_OK;
623}
624
625static int dlm_setup_channels(const struct sr_dev_inst *sdi)
626{
627 GSList *l;
628 unsigned int i;
629 gboolean *pod_enabled, setup_changed;
630 struct scope_state *state;
329733d9 631 const struct scope_config *model;
8ab929d6
SA
632 struct sr_channel *ch;
633 struct dev_context *devc;
634 struct sr_scpi_dev_inst *scpi;
635
636 devc = sdi->priv;
637 scpi = sdi->conn;
638 state = devc->model_state;
639 model = devc->model_config;
640 setup_changed = FALSE;
641
ac10a927 642 pod_enabled = g_malloc0(sizeof(gboolean) * model->pods);
8ab929d6
SA
643
644 for (l = sdi->channels; l; l = l->next) {
645 ch = l->data;
646 switch (ch->type) {
647 case SR_CHANNEL_ANALOG:
648 if (ch->enabled == state->analog_states[ch->index].state)
649 break;
650
651 if (dlm_analog_chan_state_set(scpi, ch->index + 1,
ac10a927 652 ch->enabled) != SR_OK)
8ab929d6
SA
653 return SR_ERR;
654
655 state->analog_states[ch->index].state = ch->enabled;
656 setup_changed = TRUE;
657 break;
658 case SR_CHANNEL_LOGIC:
6fd78a9f 659 i = ch->index - DLM_DIG_CHAN_INDEX_OFFS;
8ab929d6 660 if (ch->enabled)
6fd78a9f 661 pod_enabled[i / 8] = TRUE;
8ab929d6 662
6fd78a9f 663 if (ch->enabled == state->digital_states[i])
8ab929d6
SA
664 break;
665
6fd78a9f 666 if (dlm_digital_chan_state_set(scpi, i + 1,
ac10a927 667 ch->enabled) != SR_OK)
8ab929d6
SA
668 return SR_ERR;
669
6fd78a9f 670 state->digital_states[i] = ch->enabled;
8ab929d6
SA
671 setup_changed = TRUE;
672 break;
673 default:
674 return SR_ERR;
675 }
676 }
677
678 for (i = 1; i <= model->pods; ++i) {
679 if (state->pod_states[i - 1] == pod_enabled[i - 1])
680 continue;
681
682 if (dlm_digital_pod_state_set(scpi, i,
ac10a927 683 pod_enabled[i - 1]) != SR_OK)
8ab929d6
SA
684 return SR_ERR;
685
686 state->pod_states[i - 1] = pod_enabled[i - 1];
687 setup_changed = TRUE;
688 }
689
690 g_free(pod_enabled);
691
692 if (setup_changed && dlm_sample_rate_query(sdi) != SR_OK)
693 return SR_ERR;
694
695 return SR_OK;
696}
697
698static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
699{
700 GSList *l;
701 gboolean digital_added;
702 struct sr_channel *ch;
703 struct dev_context *devc;
704 struct sr_scpi_dev_inst *scpi;
705
706 (void)cb_data;
707
ac10a927
SA
708 if (sdi->status != SR_ST_ACTIVE)
709 return SR_ERR_DEV_CLOSED;
8ab929d6
SA
710
711 scpi = sdi->conn;
712 devc = sdi->priv;
713 digital_added = FALSE;
714
715 g_slist_free(devc->enabled_channels);
716 devc->enabled_channels = NULL;
717
718 for (l = sdi->channels; l; l = l->next) {
719 ch = l->data;
720 if (!ch->enabled)
721 continue;
722 /* Only add a single digital channel. */
723 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
724 devc->enabled_channels = g_slist_append(
ac10a927
SA
725 devc->enabled_channels, ch);
726 if (ch->type == SR_CHANNEL_LOGIC)
727 digital_added = TRUE;
8ab929d6
SA
728 }
729 }
730
731 if (!devc->enabled_channels)
732 return SR_ERR;
733
734 if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
735 sr_err("Invalid channel configuration specified!");
736 return SR_ERR_NA;
737 }
738
739 if (dlm_setup_channels(sdi) != SR_OK) {
740 sr_err("Failed to setup channel configuration!");
741 return SR_ERR;
742 }
743
af3487ec
SA
744 /* Request data for the first enabled channel. */
745 devc->current_channel = devc->enabled_channels;
746 dlm_channel_data_request(sdi);
747
0028d5a1
SA
748 /* Call our callback when data comes in or after 5ms. */
749 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5,
8ab929d6
SA
750 dlm_data_receive, (void *)sdi);
751
752 return SR_OK;
753}
754
755static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
756{
757 struct dev_context *devc;
8ab929d6
SA
758 struct sr_datafeed_packet packet;
759
760 (void)cb_data;
761
762 packet.type = SR_DF_END;
763 packet.payload = NULL;
764 sr_session_send(sdi, &packet);
765
766 if (sdi->status != SR_ST_ACTIVE)
767 return SR_ERR_DEV_CLOSED;
768
769 devc = sdi->priv;
770
771 devc->num_frames = 0;
772 g_slist_free(devc->enabled_channels);
773 devc->enabled_channels = NULL;
af3487ec
SA
774
775 sr_scpi_source_remove(sdi->session, sdi->conn);
8ab929d6
SA
776
777 return SR_OK;
778}
10763937
SA
779
780SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info = {
781 .name = "yokogawa-dlm",
ac10a927 782 .longname = "Yokogawa DL/DLM",
10763937 783 .api_version = 1,
8ab929d6
SA
784 .init = init,
785 .cleanup = cleanup,
786 .scan = scan,
787 .dev_list = dev_list,
788 .dev_clear = dev_clear,
789 .config_get = config_get,
790 .config_set = config_set,
c65a021c 791 .config_channel_set = config_channel_set,
8ab929d6
SA
792 .config_list = config_list,
793 .dev_open = dev_open,
794 .dev_close = dev_close,
795 .dev_acquisition_start = dev_acquisition_start,
796 .dev_acquisition_stop = dev_acquisition_stop,
10763937
SA
797 .priv = NULL,
798};