]> sigrok.org Git - libsigrok.git/blame - src/hardware/yokogawa-dlm/protocol.c
SR_DF_ANALOG_OLD and sr_datafeed_analog_old renames.
[libsigrok.git] / src / hardware / yokogawa-dlm / protocol.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
a9308652
UH
21/**
22 * @file
23 *
8ab929d6
SA
24 * <em>Yokogawa DL/DLM series</em> oscilloscope driver
25 * @internal
26 */
27
6ec6c43b 28#include <config.h>
5a1afc09 29#include "scpi.h"
10763937
SA
30#include "protocol.h"
31
8ab929d6
SA
32static const char *dlm_coupling_options[] = {
33 "AC",
34 "DC",
35 "DC50",
36 "GND",
37 NULL,
38};
39
8ab929d6
SA
40static const char *dlm_2ch_trigger_sources[] = {
41 "1",
42 "2",
43 "LINE",
44 "EXT",
45 NULL,
46};
47
48/* TODO: Is BITx handled correctly or is Dx required? */
49static const char *dlm_4ch_trigger_sources[] = {
50 "1",
51 "2",
52 "3",
53 "4",
54 "LINE",
55 "EXT",
56 "BIT1",
57 "BIT2",
58 "BIT3",
59 "BIT4",
60 "BIT5",
61 "BIT6",
62 "BIT7",
63 "BIT8",
64 NULL,
65};
66
a9308652 67/* Note: Values must correlate to the trigger_slopes values. */
f3c60fb6
SA
68const char *dlm_trigger_slopes[3] = {
69 "r",
70 "f",
71 NULL,
72};
73
74const uint64_t dlm_timebases[36][2] = {
8ab929d6
SA
75 /* nanoseconds */
76 { 1, 1000000000 },
77 { 2, 1000000000 },
78 { 5, 1000000000 },
79 { 10, 1000000000 },
80 { 20, 1000000000 },
81 { 50, 1000000000 },
82 { 100, 1000000000 },
83 { 200, 1000000000 },
84 { 500, 1000000000 },
85 /* microseconds */
86 { 1, 1000000 },
87 { 2, 1000000 },
88 { 5, 1000000 },
89 { 10, 1000000 },
90 { 20, 1000000 },
91 { 50, 1000000 },
92 { 100, 1000000 },
93 { 200, 1000000 },
94 { 500, 1000000 },
95 /* milliseconds */
96 { 1, 1000 },
97 { 2, 1000 },
98 { 5, 1000 },
99 { 10, 1000 },
100 { 20, 1000 },
101 { 50, 1000 },
102 { 100, 1000 },
103 { 200, 1000 },
104 { 500, 1000 },
105 /* seconds */
106 { 1, 1 },
107 { 2, 1 },
108 { 5, 1 },
109 { 10, 1 },
110 { 20, 1 },
111 { 50, 1 },
112 { 100, 1 },
113 { 200, 1 },
114 { 500, 1 },
115};
116
f3c60fb6 117const uint64_t dlm_vdivs[17][2] = {
8ab929d6
SA
118 /* millivolts */
119 { 2, 1000 },
120 { 5, 1000 },
121 { 10, 1000 },
122 { 20, 1000 },
123 { 50, 1000 },
124 { 100, 1000 },
125 { 200, 1000 },
126 { 500, 1000 },
127 /* volts */
128 { 1, 1 },
129 { 2, 1 },
130 { 5, 1 },
131 { 10, 1 },
132 { 20, 1 },
133 { 50, 1 },
134 { 100, 1 },
135 { 200, 1 },
136 { 500, 1 },
137};
138
139static const char *scope_analog_channel_names[] = {
140 "1",
141 "2",
142 "3",
a9308652 143 "4",
8ab929d6
SA
144};
145
7048bb1f 146static const char *scope_digital_channel_names_8[] = {
8ab929d6
SA
147 "D0",
148 "D1",
149 "D2",
150 "D3",
151 "D4",
152 "D5",
153 "D6",
a9308652 154 "D7",
8ab929d6
SA
155};
156
7048bb1f
SA
157static const char *scope_digital_channel_names_32[] = {
158 "A0",
159 "A1",
160 "A2",
161 "A3",
162 "A4",
163 "A5",
164 "A6",
165 "A7",
166 "B0",
167 "B1",
168 "B2",
169 "B3",
170 "B4",
171 "B5",
172 "B6",
173 "B7",
174 "C0",
175 "C1",
176 "C2",
177 "C3",
178 "C4",
179 "C5",
180 "C6",
181 "C7",
182 "D0",
183 "D1",
184 "D2",
185 "D3",
186 "D4",
187 "D5",
188 "D6",
189 "D7",
190};
191
329733d9 192static const struct scope_config scope_models[] = {
8ab929d6
SA
193 {
194 .model_id = {"710105", "710115", "710125", NULL},
195 .model_name = {"DLM2022", "DLM2032", "DLM2052", NULL},
196 .analog_channels = 2,
197 .digital_channels = 0,
198 .pods = 0,
199
200 .analog_names = &scope_analog_channel_names,
7048bb1f 201 .digital_names = &scope_digital_channel_names_8,
8ab929d6 202
8ab929d6
SA
203 .coupling_options = &dlm_coupling_options,
204 .trigger_sources = &dlm_2ch_trigger_sources,
8ab929d6
SA
205
206 .num_xdivs = 10,
207 .num_ydivs = 8,
208 },
209 {
210 .model_id = {"710110", "710120", "710130", NULL},
211 .model_name = {"DLM2024", "DLM2034", "DLM2054", NULL},
212 .analog_channels = 4,
213 .digital_channels = 8,
214 .pods = 1,
215
216 .analog_names = &scope_analog_channel_names,
7048bb1f
SA
217 .digital_names = &scope_digital_channel_names_8,
218
7048bb1f
SA
219 .coupling_options = &dlm_coupling_options,
220 .trigger_sources = &dlm_4ch_trigger_sources,
7048bb1f
SA
221
222 .num_xdivs = 10,
223 .num_ydivs = 8,
224 },
225 {
226 .model_id = {"701307", "701308", "701310", "701311",
227 "701312", "701313", NULL},
228 .model_name = {"DL9040", "DL9040L", "DL9140", "DL9140L",
229 "DL9240", "DL9240L", NULL},
230 .analog_channels = 4,
231 .digital_channels = 0,
232 .pods = 0,
233
234 .analog_names = &scope_analog_channel_names,
235 .digital_names = NULL,
236
7048bb1f
SA
237 .coupling_options = &dlm_coupling_options,
238 .trigger_sources = &dlm_4ch_trigger_sources,
7048bb1f
SA
239
240 .num_xdivs = 10,
241 .num_ydivs = 8,
242 },
243 {
244 .model_id = {"701320", "701321", NULL},
245 .model_name = {"DL9505L", "DL9510L", NULL},
246 .analog_channels = 4,
247 .digital_channels = 16,
248 .pods = 4,
249
250 .analog_names = &scope_analog_channel_names,
251 .digital_names = &scope_digital_channel_names_32,
252
7048bb1f
SA
253 .coupling_options = &dlm_coupling_options,
254 .trigger_sources = &dlm_4ch_trigger_sources,
7048bb1f
SA
255
256 .num_xdivs = 10,
257 .num_ydivs = 8,
258 },
259 {
260 .model_id = {"701330", "701331", NULL},
261 .model_name = {"DL9705L", "DL9710L", NULL},
262 .analog_channels = 4,
263 .digital_channels = 32,
264 .pods = 4,
265
266 .analog_names = &scope_analog_channel_names,
267 .digital_names = &scope_digital_channel_names_32,
8ab929d6 268
8ab929d6
SA
269 .coupling_options = &dlm_coupling_options,
270 .trigger_sources = &dlm_4ch_trigger_sources,
8ab929d6
SA
271
272 .num_xdivs = 10,
273 .num_ydivs = 8,
274 },
275};
276
277/**
278 * Prints out the state of the device as we currently know it.
279 *
280 * @param config This is the scope configuration.
281 * @param state The current scope state to print.
282 */
329733d9 283static void scope_state_dump(const struct scope_config *config,
8ab929d6
SA
284 struct scope_state *state)
285{
286 unsigned int i;
287 char *tmp;
288
a9308652 289 for (i = 0; i < config->analog_channels; i++) {
f3c60fb6
SA
290 tmp = sr_voltage_string(dlm_vdivs[state->analog_states[i].vdiv][0],
291 dlm_vdivs[state->analog_states[i].vdiv][1]);
a9308652 292 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
ac10a927
SA
293 i + 1, state->analog_states[i].state ? "On" : "Off",
294 (*config->coupling_options)[state->analog_states[i].coupling],
295 tmp, state->analog_states[i].vertical_offset);
8ab929d6
SA
296 }
297
a9308652 298 for (i = 0; i < config->digital_channels; i++) {
8ab929d6 299 sr_info("State of digital channel %d -> %s", i,
ac10a927 300 state->digital_states[i] ? "On" : "Off");
8ab929d6
SA
301 }
302
a9308652 303 for (i = 0; i < config->pods; i++) {
8ab929d6 304 sr_info("State of digital POD %d -> %s", i,
ac10a927 305 state->pod_states[i] ? "On" : "Off");
8ab929d6
SA
306 }
307
f3c60fb6
SA
308 tmp = sr_period_string(dlm_timebases[state->timebase][0] *
309 dlm_timebases[state->timebase][1]);
8ab929d6
SA
310 sr_info("Current timebase: %s", tmp);
311 g_free(tmp);
312
313 tmp = sr_samplerate_string(state->sample_rate);
314 sr_info("Current samplerate: %s", tmp);
315 g_free(tmp);
316
af3487ec 317 sr_info("Current samples per acquisition (i.e. frame): %d",
ac10a927 318 state->samples_per_frame);
af3487ec 319
8ab929d6 320 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
ac10a927 321 (*config->trigger_sources)[state->trigger_source],
f3c60fb6 322 dlm_trigger_slopes[state->trigger_slope],
ac10a927 323 state->horiz_triggerpos);
8ab929d6
SA
324}
325
326/**
327 * Searches through an array of strings and returns the index to the
328 * array where a given string is located.
329 *
330 * @param value The string to search for.
331 * @param array The array of strings.
332 * @param result The index at which value is located in array. -1 on error.
333 *
334 * @return SR_ERR when value couldn't be found, SR_OK otherwise.
335 */
336static int array_option_get(char *value, const char *(*array)[],
337 int *result)
338{
339 unsigned int i;
340
341 *result = -1;
342
a9308652 343 for (i = 0; (*array)[i]; i++)
8ab929d6
SA
344 if (!g_strcmp0(value, (*array)[i])) {
345 *result = i;
346 break;
347 }
348
349 if (*result == -1)
350 return SR_ERR;
351
352 return SR_OK;
353}
354
355/**
356 * This function takes a value of the form "2.000E-03", converts it to a
357 * significand / factor pair and returns the index of an array where
358 * a matching pair was found.
359 *
360 * It's a bit convoluted because of floating-point issues. The value "10.00E-09"
361 * is parsed by g_ascii_strtod() as 0.000000009999999939, for example.
362 * Therefore it's easier to break the number up into two strings and handle
363 * them separately.
364 *
365 * @param value The string to be parsed.
366 * @param array The array of s/f pairs.
367 * @param array_len The number of pairs in the array.
368 * @param result The index at which a matching pair was found.
369 *
370 * @return SR_ERR on any parsing error, SR_OK otherwise.
371 */
372static int array_float_get(gchar *value, const uint64_t array[][2],
373 int array_len, int *result)
374{
375 int i;
376 uint64_t f;
377 float s;
ac10a927 378 unsigned int s_int;
8ab929d6
SA
379 gchar ss[10], es[10];
380
381 memset(ss, 0, sizeof(ss));
382 memset(es, 0, sizeof(es));
383
384 strncpy(ss, value, 5);
385 strncpy(es, &(value[6]), 3);
386
387 if (sr_atof_ascii(ss, &s) != SR_OK)
388 return SR_ERR;
389 if (sr_atoi(es, &i) != SR_OK)
390 return SR_ERR;
391
392 /* Transform e.g. 10^-03 to 1000 as the array stores the inverse. */
393 f = pow(10, abs(i));
394
a9308652
UH
395 /*
396 * Adjust the significand/factor pair to make sure
8ab929d6
SA
397 * that f is a multiple of 1000.
398 */
399 while ((int)fmod(log10(f), 3) > 0) { s *= 10; f *= 10; }
400
401 /* Truncate s to circumvent rounding errors. */
ac10a927 402 s_int = (unsigned int)s;
8ab929d6
SA
403
404 for (i = 0; i < array_len; i++) {
ac10a927 405 if ( (s_int == array[i][0]) && (f == array[i][1]) ) {
8ab929d6
SA
406 *result = i;
407 return SR_OK;
408 }
409 }
410
411 return SR_ERR;
412}
413
414/**
415 * Obtains information about all analog channels from the oscilloscope.
416 * The internal state information is updated accordingly.
417 *
c65a021c 418 * @param sdi The device instance.
8ab929d6
SA
419 * @param config The device's device configuration.
420 * @param state The device's state information.
421 *
422 * @return SR_ERR on error, SR_OK otherwise.
423 */
c65a021c 424static int analog_channel_state_get(const struct sr_dev_inst *sdi,
329733d9 425 const struct scope_config *config,
ac10a927 426 struct scope_state *state)
8ab929d6 427{
c65a021c 428 struct sr_scpi_dev_inst *scpi;
8ab929d6 429 int i, j;
c65a021c
SA
430 GSList *l;
431 struct sr_channel *ch;
8ab929d6
SA
432 gchar *response;
433
c65a021c
SA
434 scpi = sdi->conn;
435
a9308652 436 for (i = 0; i < config->analog_channels; i++) {
8ab929d6
SA
437
438 if (dlm_analog_chan_state_get(scpi, i + 1,
ac10a927 439 &state->analog_states[i].state) != SR_OK)
8ab929d6
SA
440 return SR_ERR;
441
c65a021c
SA
442 for (l = sdi->channels; l; l = l->next) {
443 ch = l->data;
444 if (ch->index == i) {
445 ch->enabled = state->analog_states[i].state;
446 break;
447 }
448 }
449
8ab929d6
SA
450 if (dlm_analog_chan_vdiv_get(scpi, i + 1, &response) != SR_OK)
451 return SR_ERR;
452
f3c60fb6 453 if (array_float_get(response, dlm_vdivs, ARRAY_SIZE(dlm_vdivs),
ac10a927 454 &j) != SR_OK) {
8ab929d6
SA
455 g_free(response);
456 return SR_ERR;
457 }
458
459 g_free(response);
460 state->analog_states[i].vdiv = j;
461
462 if (dlm_analog_chan_voffs_get(scpi, i + 1,
ac10a927 463 &state->analog_states[i].vertical_offset) != SR_OK)
8ab929d6
SA
464 return SR_ERR;
465
466 if (dlm_analog_chan_wrange_get(scpi, i + 1,
ac10a927 467 &state->analog_states[i].waveform_range) != SR_OK)
8ab929d6
SA
468 return SR_ERR;
469
470 if (dlm_analog_chan_woffs_get(scpi, i + 1,
ac10a927 471 &state->analog_states[i].waveform_offset) != SR_OK)
8ab929d6
SA
472 return SR_ERR;
473
474 if (dlm_analog_chan_coupl_get(scpi, i + 1, &response) != SR_OK) {
475 g_free(response);
476 return SR_ERR;
477 }
478
479 if (array_option_get(response, config->coupling_options,
ac10a927 480 &state->analog_states[i].coupling) != SR_OK) {
8ab929d6
SA
481 g_free(response);
482 return SR_ERR;
483 }
484 g_free(response);
485 }
486
487 return SR_OK;
488}
489
490/**
491 * Obtains information about all digital channels from the oscilloscope.
492 * The internal state information is updated accordingly.
493 *
c65a021c 494 * @param sdi The device instance.
8ab929d6
SA
495 * @param config The device's device configuration.
496 * @param state The device's state information.
497 *
498 * @return SR_ERR on error, SR_OK otherwise.
499 */
c65a021c 500static int digital_channel_state_get(const struct sr_dev_inst *sdi,
329733d9 501 const struct scope_config *config,
ac10a927 502 struct scope_state *state)
8ab929d6 503{
c65a021c
SA
504 struct sr_scpi_dev_inst *scpi;
505 int i;
506 GSList *l;
507 struct sr_channel *ch;
508
509 scpi = sdi->conn;
8ab929d6 510
a9308652
UH
511 if (!config->digital_channels) {
512 sr_warn("Tried obtaining digital channel states on a " \
513 "model without digital inputs.");
514 return SR_OK;
515 }
8ab929d6 516
a9308652 517 for (i = 0; i < config->digital_channels; i++) {
8ab929d6
SA
518 if (dlm_digital_chan_state_get(scpi, i + 1,
519 &state->digital_states[i]) != SR_OK) {
520 return SR_ERR;
521 }
c65a021c
SA
522
523 for (l = sdi->channels; l; l = l->next) {
524 ch = l->data;
525 if (ch->index == i + DLM_DIG_CHAN_INDEX_OFFS) {
526 ch->enabled = state->digital_states[i];
527 break;
528 }
529 }
8ab929d6
SA
530 }
531
a9308652 532 if (!config->pods) {
8ab929d6
SA
533 sr_warn("Tried obtaining pod states on a model without pods.");
534 return SR_OK;
535 }
536
a9308652 537 for (i = 0; i < config->pods; i++) {
8ab929d6
SA
538 if (dlm_digital_pod_state_get(scpi, i + 'A',
539 &state->pod_states[i]) != SR_OK)
540 return SR_ERR;
541 }
542
543 return SR_OK;
544}
545
c65a021c
SA
546SR_PRIV int dlm_channel_state_set(const struct sr_dev_inst *sdi,
547 const int ch_index, gboolean ch_state)
548{
549 GSList *l;
550 struct sr_channel *ch;
551 struct dev_context *devc = NULL;
552 struct scope_state *state;
553 const struct scope_config *model = NULL;
554 struct sr_scpi_dev_inst *scpi;
555 gboolean chan_found;
556 gboolean *pod_enabled;
557 int i, result;
558
559 result = SR_OK;
560
561 scpi = sdi->conn;
562 devc = sdi->priv;
563 state = devc->model_state;
564 model = devc->model_config;
565 chan_found = FALSE;
566
567 pod_enabled = g_malloc0(sizeof(gboolean) * model->pods);
568
569 for (l = sdi->channels; l; l = l->next) {
570 ch = l->data;
571
572 switch (ch->type) {
573 case SR_CHANNEL_ANALOG:
574 if (ch->index == ch_index) {
575 if (dlm_analog_chan_state_set(scpi, ch->index + 1, ch_state) != SR_OK) {
576 result = SR_ERR;
577 break;
578 }
579
580 ch->enabled = ch_state;
581 state->analog_states[ch->index].state = ch_state;
582 chan_found = TRUE;
583 break;
584 }
585 break;
586 case SR_CHANNEL_LOGIC:
587 i = ch->index - DLM_DIG_CHAN_INDEX_OFFS;
588
589 if (ch->index == ch_index) {
590 if (dlm_digital_chan_state_set(scpi, i + 1, ch_state) != SR_OK) {
591 result = SR_ERR;
592 break;
593 }
594
a9308652
UH
595 ch->enabled = ch_state;
596 state->digital_states[i] = ch_state;
597 chan_found = TRUE;
c65a021c 598
a9308652
UH
599 /* The corresponding pod has to be enabled also. */
600 pod_enabled[i / 8] |= ch->enabled;
601 } else {
c65a021c
SA
602 /* Also check all other channels. Maybe we can disable a pod. */
603 pod_enabled[i / 8] |= ch->enabled;
a9308652 604 }
c65a021c
SA
605 break;
606 default:
607 result = SR_ERR_NA;
608 }
609 }
610
a9308652 611 for (i = 0; i < model->pods; i++) {
c65a021c
SA
612 if (state->pod_states[i] == pod_enabled[i])
613 continue;
614
615 if (dlm_digital_pod_state_set(scpi, i + 1, pod_enabled[i]) != SR_OK) {
616 result = SR_ERR;
617 break;
618 }
619
620 state->pod_states[i] = pod_enabled[i];
621 }
622
623 g_free(pod_enabled);
624
625 if ((result == SR_OK) && !chan_found)
626 result = SR_ERR_BUG;
627
628 return result;
629}
630
8ab929d6
SA
631/**
632 * Obtains information about the sample rate from the oscilloscope.
633 * The internal state information is updated accordingly.
634 *
635 * @param sdi The device instance.
636 *
637 * @return SR_ERR on error, SR_OK otherwise.
638 */
639SR_PRIV int dlm_sample_rate_query(const struct sr_dev_inst *sdi)
640{
641 struct dev_context *devc;
642 struct scope_state *state;
643 float tmp_float;
644
645 devc = sdi->priv;
646 state = devc->model_state;
647
a9308652
UH
648 /*
649 * No need to find an active channel to query the sample rate:
8ab929d6
SA
650 * querying any channel will do, so we use channel 1 all the time.
651 */
652 if (dlm_analog_chan_srate_get(sdi->conn, 1, &tmp_float) != SR_OK)
653 return SR_ERR;
654
655 state->sample_rate = tmp_float;
656
657 return SR_OK;
658}
659
660/**
661 * Obtains information about the current device state from the oscilloscope,
662 * including all analog and digital channel configurations.
663 * The internal state information is updated accordingly.
664 *
665 * @param sdi The device instance.
666 *
667 * @return SR_ERR on error, SR_OK otherwise.
668 */
669SR_PRIV int dlm_scope_state_query(struct sr_dev_inst *sdi)
670{
671 struct dev_context *devc;
672 struct scope_state *state;
329733d9 673 const struct scope_config *config;
8ab929d6
SA
674 float tmp_float;
675 gchar *response;
676 int i;
677
678 devc = sdi->priv;
679 config = devc->model_config;
680 state = devc->model_state;
681
c65a021c 682 if (analog_channel_state_get(sdi, config, state) != SR_OK)
8ab929d6
SA
683 return SR_ERR;
684
c65a021c 685 if (digital_channel_state_get(sdi, config, state) != SR_OK)
8ab929d6
SA
686 return SR_ERR;
687
688 if (dlm_timebase_get(sdi->conn, &response) != SR_OK)
689 return SR_ERR;
690
f3c60fb6
SA
691 if (array_float_get(response, dlm_timebases,
692 ARRAY_SIZE(dlm_timebases), &i) != SR_OK) {
8ab929d6
SA
693 g_free(response);
694 return SR_ERR;
695 }
696
697 g_free(response);
698 state->timebase = i;
699
700 if (dlm_horiz_trigger_pos_get(sdi->conn, &tmp_float) != SR_OK)
701 return SR_ERR;
702
703 /* TODO: Check if the calculation makes sense for the DLM. */
704 state->horiz_triggerpos = tmp_float /
f3c60fb6
SA
705 (((double)dlm_timebases[state->timebase][0] /
706 dlm_timebases[state->timebase][1]) * config->num_xdivs);
8ab929d6
SA
707 state->horiz_triggerpos -= 0.5;
708 state->horiz_triggerpos *= -1;
709
710 if (dlm_trigger_source_get(sdi->conn, &response) != SR_OK) {
711 g_free(response);
712 return SR_ERR;
713 }
714
715 if (array_option_get(response, config->trigger_sources,
ac10a927 716 &state->trigger_source) != SR_OK) {
8ab929d6
SA
717 g_free(response);
718 return SR_ERR;
719 }
720
721 g_free(response);
722
723 if (dlm_trigger_slope_get(sdi->conn, &i) != SR_OK)
724 return SR_ERR;
725
726 state->trigger_slope = i;
727
af3487ec
SA
728 if (dlm_acq_length_get(sdi->conn, &state->samples_per_frame) != SR_OK) {
729 sr_err("Failed to query acquisition length.");
730 return SR_ERR;
731 }
732
8ab929d6
SA
733 dlm_sample_rate_query(sdi);
734
735 scope_state_dump(config, state);
736
737 return SR_OK;
738}
739
740/**
741 * Creates a new device state structure.
742 *
743 * @param config The device configuration to use.
744 *
ac10a927 745 * @return The newly allocated scope_state struct.
8ab929d6 746 */
329733d9 747static struct scope_state *dlm_scope_state_new(const struct scope_config *config)
8ab929d6
SA
748{
749 struct scope_state *state;
750
ac10a927 751 state = g_malloc0(sizeof(struct scope_state));
8ab929d6
SA
752
753 state->analog_states = g_malloc0(config->analog_channels *
ac10a927 754 sizeof(struct analog_channel_state));
8ab929d6
SA
755
756 state->digital_states = g_malloc0(config->digital_channels *
ac10a927 757 sizeof(gboolean));
8ab929d6
SA
758
759 state->pod_states = g_malloc0(config->pods * sizeof(gboolean));
760
761 return state;
762}
763
764/**
765 * Frees the memory that was allocated by a call to dlm_scope_state_new().
766 *
767 * @param state The device state structure whose memory is to be freed.
768 */
769SR_PRIV void dlm_scope_state_destroy(struct scope_state *state)
770{
771 g_free(state->analog_states);
772 g_free(state->digital_states);
773 g_free(state->pod_states);
774 g_free(state);
775}
776
777SR_PRIV int dlm_model_get(char *model_id, char **model_name, int *model_index)
778{
779 unsigned int i, j;
780
781 *model_index = -1;
782 *model_name = NULL;
783
784 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
785 for (j = 0; scope_models[i].model_id[j]; j++) {
786 if (!strcmp(model_id, scope_models[i].model_id[j])) {
787 *model_index = i;
788 *model_name = (char *)scope_models[i].model_name[j];
789 break;
790 }
791 }
792 if (*model_index != -1)
793 break;
794 }
795
796 if (*model_index == -1) {
797 sr_err("Found unsupported DLM device with model identifier %s.",
ac10a927 798 model_id);
8ab929d6
SA
799 return SR_ERR_NA;
800 }
801
802 return SR_OK;
803}
804
805/**
806 * Attempts to initialize a DL/DLM device and prepares internal structures
807 * if a suitable device was found.
808 *
809 * @param sdi The device instance.
810 */
811SR_PRIV int dlm_device_init(struct sr_dev_inst *sdi, int model_index)
812{
813 char tmp[25];
814 int i;
815 struct sr_channel *ch;
816 struct dev_context *devc;
817
818 devc = sdi->priv;
819
820 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
ac10a927 821 scope_models[model_index].analog_channels);
8ab929d6
SA
822
823 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
f77afcf0 824 scope_models[model_index].pods);
8ab929d6 825
6fd78a9f 826 /* Add analog channels, each in its own group. */
8ab929d6 827 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
5e23fcab 828 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
c368e6f3 829 (*scope_models[model_index].analog_names)[i]);
8ab929d6
SA
830
831 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
832
833 devc->analog_groups[i]->name = g_strdup(
ac10a927 834 (char *)(*scope_models[model_index].analog_names)[i]);
8ab929d6
SA
835 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
836
837 sdi->channel_groups = g_slist_append(sdi->channel_groups,
ac10a927 838 devc->analog_groups[i]);
8ab929d6
SA
839 }
840
841 /* Add digital channel groups. */
a9308652 842 for (i = 0; i < scope_models[model_index].pods; i++) {
8ab929d6
SA
843 g_snprintf(tmp, sizeof(tmp), "POD%d", i);
844
845 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
846 if (!devc->digital_groups[i])
847 return SR_ERR_MALLOC;
848
849 devc->digital_groups[i]->name = g_strdup(tmp);
850 sdi->channel_groups = g_slist_append(sdi->channel_groups,
851 devc->digital_groups[i]);
852 }
853
854 /* Add digital channels. */
855 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
6fd78a9f
SA
856 ch = sr_channel_new(sdi, DLM_DIG_CHAN_INDEX_OFFS + i,
857 SR_CHANNEL_LOGIC, TRUE,
c368e6f3 858 (*scope_models[model_index].digital_names)[i]);
8ab929d6
SA
859
860 devc->digital_groups[i / 8]->channels = g_slist_append(
ac10a927 861 devc->digital_groups[i / 8]->channels, ch);
8ab929d6
SA
862 }
863 devc->model_config = &scope_models[model_index];
864 devc->frame_limit = 0;
865
866 if (!(devc->model_state = dlm_scope_state_new(devc->model_config)))
867 return SR_ERR_MALLOC;
868
869 /* Disable non-standard response behavior. */
870 if (dlm_response_headers_set(sdi->conn, FALSE) != SR_OK)
871 return SR_ERR;
872
873 return SR_OK;
874}
875
af3487ec 876SR_PRIV int dlm_channel_data_request(const struct sr_dev_inst *sdi)
8ab929d6 877{
af3487ec
SA
878 struct dev_context *devc;
879 struct sr_channel *ch;
880 int result;
8ab929d6 881
af3487ec
SA
882 devc = sdi->priv;
883 ch = devc->current_channel->data;
8ab929d6 884
af3487ec
SA
885 switch (ch->type) {
886 case SR_CHANNEL_ANALOG:
887 result = dlm_analog_data_get(sdi->conn, ch->index + 1);
888 break;
889 case SR_CHANNEL_LOGIC:
890 result = dlm_digital_data_get(sdi->conn);
891 break;
892 default:
893 sr_err("Invalid channel type encountered (%d).",
894 ch->type);
895 result = SR_ERR;
8ab929d6
SA
896 }
897
af3487ec
SA
898 if (result == SR_OK)
899 devc->data_pending = TRUE;
900 else
901 devc->data_pending = FALSE;
902
903 return result;
8ab929d6
SA
904}
905
906/**
907 * Reads and removes the block data header from a given data input.
908 * Format is #ndddd... with n being the number of decimal digits d.
909 * The string dddd... contains the decimal-encoded length of the data.
910 * Example: #9000000013 would yield a length of 13 bytes.
911 *
912 * @param data The input data.
913 * @param len The determined input data length.
914 */
915static int dlm_block_data_header_process(GArray *data, int *len)
916{
917 int i, n;
918 gchar s[20];
919
920 if (g_array_index(data, gchar, 0) != '#')
921 return SR_ERR;
922
923 n = (uint8_t)(g_array_index(data, gchar, 1) - '0');
924
925 for (i = 0; i < n; i++)
926 s[i] = g_array_index(data, gchar, 2 + i);
927 s[i] = 0;
928
929 if (sr_atoi(s, len) != SR_OK)
930 return SR_ERR;
931
932 g_array_remove_range(data, 0, 2 + n);
933
934 return SR_OK;
935}
936
937/**
938 * Turns raw sample data into voltages and sends them off to the session bus.
939 *
940 * @param data The raw sample data.
8ab929d6
SA
941 * @ch_state Pointer to the state of the channel whose data we're processing.
942 * @sdi The device instance.
943 *
944 * @return SR_ERR when data is trucated, SR_OK otherwise.
945 */
af3487ec 946static int dlm_analog_samples_send(GArray *data,
ac10a927
SA
947 struct analog_channel_state *ch_state,
948 struct sr_dev_inst *sdi)
8ab929d6 949{
af3487ec 950 uint32_t i, samples;
8ab929d6
SA
951 float voltage, range, offset;
952 GArray *float_data;
953 struct dev_context *devc;
af3487ec 954 struct scope_state *model_state;
8ab929d6 955 struct sr_channel *ch;
5faebab2 956 struct sr_datafeed_analog_old analog;
8ab929d6
SA
957 struct sr_datafeed_packet packet;
958
af3487ec
SA
959 devc = sdi->priv;
960 model_state = devc->model_state;
961 samples = model_state->samples_per_frame;
962 ch = devc->current_channel->data;
963
8ab929d6
SA
964 if (data->len < samples * sizeof(uint8_t)) {
965 sr_err("Truncated waveform data packet received.");
966 return SR_ERR;
967 }
968
a9308652 969 range = ch_state->waveform_range;
8ab929d6
SA
970 offset = ch_state->waveform_offset;
971
a9308652
UH
972 /*
973 * Convert byte sample to voltage according to
8ab929d6
SA
974 * page 269 of the Communication Interface User's Manual.
975 */
976 float_data = g_array_new(FALSE, FALSE, sizeof(float));
977 for (i = 0; i < samples; i++) {
978 voltage = (float)g_array_index(data, int8_t, i);
979 voltage = (range * voltage /
ac10a927 980 DLM_DIVISION_FOR_BYTE_FORMAT) + offset;
8ab929d6
SA
981 g_array_append_val(float_data, voltage);
982 }
983
984 analog.channels = g_slist_append(NULL, ch);
985 analog.num_samples = float_data->len;
986 analog.data = (float*)float_data->data;
987 analog.mq = SR_MQ_VOLTAGE;
988 analog.unit = SR_UNIT_VOLT;
989 analog.mqflags = 0;
5faebab2 990 packet.type = SR_DF_ANALOG_OLD;
8ab929d6
SA
991 packet.payload = &analog;
992 sr_session_send(sdi, &packet);
993 g_slist_free(analog.channels);
994
995 g_array_free(float_data, TRUE);
996 g_array_remove_range(data, 0, samples * sizeof(uint8_t));
997
998 return SR_OK;
999}
1000
1001/**
1002 * Sends logic sample data off to the session bus.
1003 *
1004 * @param data The raw sample data.
8ab929d6
SA
1005 * @ch_state Pointer to the state of the channel whose data we're processing.
1006 * @sdi The device instance.
1007 *
1008 * @return SR_ERR when data is trucated, SR_OK otherwise.
1009 */
af3487ec 1010static int dlm_digital_samples_send(GArray *data,
ac10a927 1011 struct sr_dev_inst *sdi)
8ab929d6 1012{
af3487ec
SA
1013 struct dev_context *devc;
1014 struct scope_state *model_state;
1015 uint32_t samples;
8ab929d6
SA
1016 struct sr_datafeed_logic logic;
1017 struct sr_datafeed_packet packet;
1018
af3487ec
SA
1019 devc = sdi->priv;
1020 model_state = devc->model_state;
1021 samples = model_state->samples_per_frame;
1022
8ab929d6
SA
1023 if (data->len < samples * sizeof(uint8_t)) {
1024 sr_err("Truncated waveform data packet received.");
1025 return SR_ERR;
1026 }
1027
1028 logic.length = samples;
1029 logic.unitsize = 1;
1030 logic.data = data->data;
1031 packet.type = SR_DF_LOGIC;
1032 packet.payload = &logic;
1033 sr_session_send(sdi, &packet);
1034
1035 g_array_remove_range(data, 0, samples * sizeof(uint8_t));
1036
1037 return SR_OK;
1038}
1039
1040/**
1041 * Attempts to query sample data from the oscilloscope in order to send it
1042 * to the session bus for further processing.
1043 *
1044 * @param fd The file descriptor used as the event source.
1045 * @param revents The received events.
1046 * @param cb_data Callback data, in this case our device instance.
1047 *
1048 * @return TRUE in case of success or a recoverable error,
a9308652 1049 * FALSE when a fatal error was encountered.
8ab929d6
SA
1050 */
1051SR_PRIV int dlm_data_receive(int fd, int revents, void *cb_data)
1052{
8ab929d6
SA
1053 struct sr_dev_inst *sdi;
1054 struct scope_state *model_state;
1055 struct dev_context *devc;
af3487ec 1056 struct sr_channel *ch;
8ab929d6 1057 struct sr_datafeed_packet packet;
af3487ec
SA
1058 int chunk_len, num_bytes;
1059 static GArray *data = NULL;
8ab929d6
SA
1060
1061 (void)fd;
1062 (void)revents;
1063
1064 if (!(sdi = cb_data))
1065 return FALSE;
1066
1067 if (!(devc = sdi->priv))
1068 return FALSE;
1069
1070 if (!(model_state = (struct scope_state*)devc->model_state))
1071 return FALSE;
1072
af3487ec
SA
1073 /* Are we waiting for a response from the device? */
1074 if (!devc->data_pending)
8ab929d6 1075 return TRUE;
af3487ec
SA
1076
1077 /* Check if a new query response is coming our way. */
1078 if (!data) {
1079 if (sr_scpi_read_begin(sdi->conn) == SR_OK)
1080 /* The 16 here accounts for the header and EOL. */
1081 data = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t),
ac10a927 1082 16 + model_state->samples_per_frame);
af3487ec
SA
1083 else
1084 return TRUE;
8ab929d6
SA
1085 }
1086
af3487ec
SA
1087 /* Store incoming data. */
1088 chunk_len = sr_scpi_read_data(sdi->conn, devc->receive_buffer,
ac10a927 1089 RECEIVE_BUFFER_SIZE);
af3487ec
SA
1090 if (chunk_len < 0) {
1091 sr_err("Error while reading data: %d", chunk_len);
1092 goto fail;
1093 }
1094 g_array_append_vals(data, devc->receive_buffer, chunk_len);
8ab929d6 1095
af3487ec
SA
1096 /* Read the entire query response before processing. */
1097 if (!sr_scpi_read_complete(sdi->conn))
1098 return TRUE;
8ab929d6 1099
af3487ec
SA
1100 /* We finished reading and are no longer waiting for data. */
1101 devc->data_pending = FALSE;
8ab929d6 1102
af3487ec
SA
1103 /* Signal the beginning of a new frame if this is the first channel. */
1104 if (devc->current_channel == devc->enabled_channels) {
1105 packet.type = SR_DF_FRAME_BEGIN;
1106 sr_session_send(sdi, &packet);
1107 }
8ab929d6 1108
af3487ec
SA
1109 if (dlm_block_data_header_process(data, &num_bytes) != SR_OK) {
1110 sr_err("Encountered malformed block data header.");
1111 goto fail;
1112 }
8ab929d6 1113
af3487ec
SA
1114 if (num_bytes == 0) {
1115 sr_warn("Zero-length waveform data packet received. " \
ac10a927
SA
1116 "Live mode not supported yet, stopping " \
1117 "acquisition and retrying.");
af3487ec
SA
1118 /* Don't care about return value here. */
1119 dlm_acquisition_stop(sdi->conn);
1120 g_array_free(data, TRUE);
0028d5a1 1121 dlm_channel_data_request(sdi);
af3487ec
SA
1122 return TRUE;
1123 }
8ab929d6 1124
af3487ec
SA
1125 ch = devc->current_channel->data;
1126 switch (ch->type) {
1127 case SR_CHANNEL_ANALOG:
1128 if (dlm_analog_samples_send(data,
1129 &model_state->analog_states[ch->index],
1130 sdi) != SR_OK)
8ab929d6 1131 goto fail;
af3487ec
SA
1132 break;
1133 case SR_CHANNEL_LOGIC:
1134 if (dlm_digital_samples_send(data, sdi) != SR_OK)
1135 goto fail;
1136 break;
1137 default:
1138 sr_err("Invalid channel type encountered.");
1139 break;
1140 }
8ab929d6 1141
af3487ec
SA
1142 g_array_free(data, TRUE);
1143 data = NULL;
8ab929d6 1144
a9308652
UH
1145 /*
1146 * Signal the end of this frame if this was the last enabled channel
af3487ec
SA
1147 * and set the next enabled channel. Then, request its data.
1148 */
1149 if (!devc->current_channel->next) {
1150 packet.type = SR_DF_FRAME_END;
1151 sr_session_send(sdi, &packet);
1152 devc->current_channel = devc->enabled_channels;
1153
a9308652
UH
1154 /*
1155 * As of now we only support importing the current acquisition
af3487ec
SA
1156 * data so we're going to stop at this point.
1157 */
1158 sdi->driver->dev_acquisition_stop(sdi, cb_data);
1159 return TRUE;
1160 } else
1161 devc->current_channel = devc->current_channel->next;
8ab929d6 1162
af3487ec 1163 if (dlm_channel_data_request(sdi) != SR_OK) {
f3f19d11 1164 sr_err("Failed to request acquisition data.");
af3487ec 1165 goto fail;
8ab929d6
SA
1166 }
1167
8ab929d6
SA
1168 return TRUE;
1169
1170fail:
af3487ec 1171 if (data) {
8ab929d6 1172 g_array_free(data, TRUE);
af3487ec
SA
1173 data = NULL;
1174 }
8ab929d6 1175
af3487ec 1176 return FALSE;
8ab929d6 1177}