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