]>
Commit | Line | Data |
---|---|---|
e3b83c5e SS |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2017 Sven Schnelle <svens@stackframe.org> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
3f2c7c94 SS |
21 | #include <math.h> |
22 | #include <stdlib.h> | |
23 | #include "scpi.h" | |
e3b83c5e SS |
24 | #include "protocol.h" |
25 | ||
3f2c7c94 SS |
26 | struct lecroy_wavedesc_2_x { |
27 | uint16_t comm_type; | |
28 | uint16_t comm_order; /* 1 - little endian */ | |
29 | uint32_t wave_descriptor_length; | |
30 | uint32_t user_text_len; | |
31 | uint32_t res_desc1; | |
32 | uint32_t trigtime_array_length; | |
33 | uint32_t ris_time1_array_length; | |
34 | uint32_t res_array1; | |
35 | uint32_t wave_array1_length; | |
36 | uint32_t wave_array2_length; | |
37 | uint32_t wave_array3_length; | |
38 | uint32_t wave_array4_length; | |
39 | char instrument_name[16]; | |
40 | uint32_t instrument_number; | |
41 | char trace_label[16]; | |
42 | uint32_t reserved; | |
43 | uint32_t wave_array_count; | |
44 | uint32_t points_per_screen; | |
45 | uint32_t first_valid_point; | |
46 | uint32_t last_valid_point; | |
47 | uint32_t first_point; | |
48 | uint32_t sparsing_factor; | |
49 | uint32_t segment_index; | |
50 | uint32_t subarray_count; | |
51 | uint32_t sweeps_per_acq; | |
52 | uint16_t points_per_pair; | |
53 | uint16_t pair_offset; | |
54 | float vertical_gain; | |
55 | float vertical_offset; | |
56 | float max_value; | |
57 | float min_value; | |
58 | uint16_t nominal_bits; | |
59 | uint16_t nom_subarray_count; | |
60 | float horiz_interval; | |
61 | double horiz_offset; | |
62 | double pixel_offset; | |
63 | char vertunit[48]; | |
64 | char horunit[48]; | |
65 | uint32_t reserved1; | |
66 | double trigger_time; | |
67 | } __attribute__((packed)); | |
68 | ||
69 | struct lecroy_wavedesc { | |
70 | char descriptor_name[16]; | |
71 | char template_name[16]; | |
72 | union { | |
73 | struct lecroy_wavedesc_2_x version_2_x; | |
74 | }; | |
75 | } __attribute__((packed)); | |
76 | ||
6d13a46c | 77 | static const char *coupling_options[] = { |
3f2c7c94 | 78 | "A1M", // AC with 1 MOhm termination |
ea257cdc | 79 | "D50", // DC with 50 Ohm termination |
3f2c7c94 SS |
80 | "D1M", // DC with 1 MOhm termination |
81 | "GND", | |
82 | "OVL", | |
3f2c7c94 SS |
83 | }; |
84 | ||
85 | static const char *scope_trigger_slopes[] = { | |
692716f5 | 86 | "POS", "NEG", |
3f2c7c94 SS |
87 | }; |
88 | ||
6d13a46c | 89 | static const char *trigger_sources[] = { |
692716f5 | 90 | "C1", "C2", "C3", "C4", "LINE", "EXT", |
3f2c7c94 SS |
91 | }; |
92 | ||
76f0fa5d | 93 | static const uint64_t timebases[][2] = { |
3f2c7c94 SS |
94 | /* picoseconds */ |
95 | { 20, 1000000000000 }, | |
96 | { 50, 1000000000000 }, | |
97 | { 100, 1000000000000 }, | |
98 | { 200, 1000000000000 }, | |
99 | { 500, 1000000000000 }, | |
100 | /* nanoseconds */ | |
101 | { 1, 1000000000 }, | |
102 | { 2, 1000000000 }, | |
103 | { 5, 1000000000 }, | |
104 | { 10, 1000000000 }, | |
105 | { 20, 1000000000 }, | |
106 | { 50, 1000000000 }, | |
107 | { 100, 1000000000 }, | |
108 | { 200, 1000000000 }, | |
109 | { 500, 1000000000 }, | |
110 | /* microseconds */ | |
111 | { 1, 1000000 }, | |
112 | { 2, 1000000 }, | |
113 | { 5, 1000000 }, | |
114 | { 10, 1000000 }, | |
115 | { 20, 1000000 }, | |
116 | { 50, 1000000 }, | |
117 | { 100, 1000000 }, | |
118 | { 200, 1000000 }, | |
119 | { 500, 1000000 }, | |
120 | /* milliseconds */ | |
121 | { 1, 1000 }, | |
122 | { 2, 1000 }, | |
123 | { 5, 1000 }, | |
124 | { 10, 1000 }, | |
125 | { 20, 1000 }, | |
126 | { 50, 1000 }, | |
127 | { 100, 1000 }, | |
128 | { 200, 1000 }, | |
129 | { 500, 1000 }, | |
130 | /* seconds */ | |
131 | { 1, 1 }, | |
132 | { 2, 1 }, | |
133 | { 5, 1 }, | |
134 | { 10, 1 }, | |
135 | { 20, 1 }, | |
136 | { 50, 1 }, | |
137 | { 100, 1 }, | |
138 | { 200, 1 }, | |
139 | { 500, 1 }, | |
140 | { 1000, 1 }, | |
141 | }; | |
142 | ||
76f0fa5d | 143 | static const uint64_t vdivs[][2] = { |
3f2c7c94 SS |
144 | /* millivolts */ |
145 | { 1, 1000 }, | |
146 | { 2, 1000 }, | |
147 | { 5, 1000 }, | |
148 | { 10, 1000 }, | |
149 | { 20, 1000 }, | |
150 | { 50, 1000 }, | |
151 | { 100, 1000 }, | |
152 | { 200, 1000 }, | |
153 | { 500, 1000 }, | |
154 | /* volts */ | |
155 | { 1, 1 }, | |
156 | { 2, 1 }, | |
157 | { 5, 1 }, | |
158 | { 10, 1 }, | |
159 | { 20, 1 }, | |
160 | { 50, 1 }, | |
161 | }; | |
162 | ||
163 | static const char *scope_analog_channel_names[] = { | |
f8195cb2 | 164 | "CH1", "CH2", "CH3", "CH4", |
3f2c7c94 SS |
165 | }; |
166 | ||
167 | static const struct scope_config scope_models[] = { | |
168 | { | |
169 | .name = { "WP7000", "WP7100", "WP7200", "WP7300" }, | |
170 | ||
171 | .analog_channels = 4, | |
172 | .analog_names = &scope_analog_channel_names, | |
173 | ||
6d13a46c | 174 | .coupling_options = &coupling_options, |
692716f5 UH |
175 | .num_coupling_options = ARRAY_SIZE(coupling_options), |
176 | ||
6d13a46c | 177 | .trigger_sources = &trigger_sources, |
692716f5 UH |
178 | .num_trigger_sources = ARRAY_SIZE(trigger_sources), |
179 | ||
3f2c7c94 | 180 | .trigger_slopes = &scope_trigger_slopes, |
692716f5 | 181 | .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes), |
3f2c7c94 | 182 | |
76f0fa5d | 183 | .timebases = &timebases, |
6d13a46c | 184 | .num_timebases = ARRAY_SIZE(timebases), |
3f2c7c94 | 185 | |
76f0fa5d | 186 | .vdivs = &vdivs, |
6d13a46c | 187 | .num_vdivs = ARRAY_SIZE(vdivs), |
3f2c7c94 SS |
188 | |
189 | .num_xdivs = 10, | |
190 | .num_ydivs = 8, | |
191 | }, | |
192 | }; | |
193 | ||
194 | static void scope_state_dump(const struct scope_config *config, | |
195 | struct scope_state *state) | |
196 | { | |
197 | unsigned int i; | |
198 | char *tmp; | |
199 | ||
200 | for (i = 0; i < config->analog_channels; i++) { | |
76f0fa5d UH |
201 | tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0], |
202 | (*config->vdivs)[state->analog_channels[i].vdiv][1]); | |
3f2c7c94 SS |
203 | sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)", |
204 | i + 1, state->analog_channels[i].state ? "On" : "Off", | |
205 | (*config->coupling_options)[state->analog_channels[i].coupling], | |
206 | tmp, state->analog_channels[i].vertical_offset); | |
207 | } | |
208 | ||
76f0fa5d UH |
209 | tmp = sr_period_string((*config->timebases)[state->timebase][0], |
210 | (*config->timebases)[state->timebase][1]); | |
3f2c7c94 SS |
211 | sr_info("Current timebase: %s", tmp); |
212 | g_free(tmp); | |
213 | ||
214 | tmp = sr_samplerate_string(state->sample_rate); | |
215 | sr_info("Current samplerate: %s", tmp); | |
216 | g_free(tmp); | |
217 | ||
218 | sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)", | |
219 | (*config->trigger_sources)[state->trigger_source], | |
220 | (*config->trigger_slopes)[state->trigger_slope], | |
221 | state->horiz_triggerpos); | |
222 | } | |
223 | ||
224 | static int scope_state_get_array_option(const char *resp, | |
692716f5 | 225 | const char *(*array)[], unsigned int n, int *result) |
3f2c7c94 SS |
226 | { |
227 | unsigned int i; | |
228 | ||
692716f5 | 229 | for (i = 0; i < n; i++) { |
3f2c7c94 SS |
230 | if (!g_strcmp0(resp, (*array)[i])) { |
231 | *result = i; | |
232 | return SR_OK; | |
233 | } | |
234 | } | |
235 | ||
236 | return SR_ERR; | |
237 | } | |
238 | ||
239 | /** | |
240 | * This function takes a value of the form "2.000E-03" and returns the index | |
241 | * of an array where a matching pair was found. | |
242 | * | |
243 | * @param value The string to be parsed. | |
244 | * @param array The array of s/f pairs. | |
245 | * @param array_len The number of pairs in the array. | |
246 | * @param result The index at which a matching pair was found. | |
247 | * | |
248 | * @return SR_ERR on any parsing error, SR_OK otherwise. | |
249 | */ | |
76f0fa5d | 250 | static int array_float_get(gchar *value, const uint64_t array[][2], |
3f2c7c94 SS |
251 | int array_len, unsigned int *result) |
252 | { | |
253 | struct sr_rational rval; | |
76f0fa5d | 254 | struct sr_rational aval; |
3f2c7c94 SS |
255 | |
256 | if (sr_parse_rational(value, &rval) != SR_OK) | |
257 | return SR_ERR; | |
258 | ||
259 | for (int i = 0; i < array_len; i++) { | |
76f0fa5d UH |
260 | sr_rational_set(&aval, array[i][0], array[i][1]); |
261 | if (sr_rational_eq(&rval, &aval)) { | |
3f2c7c94 SS |
262 | *result = i; |
263 | return SR_OK; | |
264 | } | |
265 | } | |
266 | ||
267 | return SR_ERR; | |
268 | } | |
269 | ||
270 | static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi, | |
271 | const struct scope_config *config, | |
272 | struct scope_state *state) | |
273 | { | |
274 | unsigned int i, j; | |
275 | char command[MAX_COMMAND_SIZE]; | |
276 | char *tmp_str; | |
277 | ||
278 | for (i = 0; i < config->analog_channels; i++) { | |
ea257cdc | 279 | g_snprintf(command, sizeof(command), "C%d:TRACE?", i + 1); |
3f2c7c94 SS |
280 | |
281 | if (sr_scpi_get_bool(scpi, command, | |
ea257cdc | 282 | &state->analog_channels[i].state) != SR_OK) |
3f2c7c94 SS |
283 | return SR_ERR; |
284 | ||
ea257cdc | 285 | g_snprintf(command, sizeof(command), "C%d:VDIV?", i + 1); |
3f2c7c94 SS |
286 | |
287 | if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK) | |
288 | return SR_ERR; | |
289 | ||
53012da6 | 290 | if (array_float_get(tmp_str, ARRAY_AND_SIZE(vdivs), &j) != SR_OK) { |
3f2c7c94 SS |
291 | g_free(tmp_str); |
292 | sr_err("Could not determine array index for vertical div scale."); | |
293 | return SR_ERR; | |
294 | } | |
295 | ||
296 | g_free(tmp_str); | |
297 | state->analog_channels[i].vdiv = j; | |
298 | ||
ea257cdc | 299 | g_snprintf(command, sizeof(command), "C%d:OFFSET?", i + 1); |
3f2c7c94 SS |
300 | |
301 | if (sr_scpi_get_float(scpi, command, &state->analog_channels[i].vertical_offset) != SR_OK) | |
302 | return SR_ERR; | |
303 | ||
ea257cdc | 304 | g_snprintf(command, sizeof(command), "C%d:COUPLING?", i + 1); |
3f2c7c94 SS |
305 | |
306 | if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK) | |
307 | return SR_ERR; | |
308 | ||
309 | ||
310 | if (scope_state_get_array_option(tmp_str, config->coupling_options, | |
692716f5 | 311 | config->num_coupling_options, |
ea257cdc | 312 | &state->analog_channels[i].coupling) != SR_OK) |
3f2c7c94 SS |
313 | return SR_ERR; |
314 | ||
315 | g_free(tmp_str); | |
316 | } | |
317 | ||
318 | return SR_OK; | |
319 | } | |
320 | ||
321 | SR_PRIV int lecroy_xstream_update_sample_rate(const struct sr_dev_inst *sdi) | |
e3b83c5e | 322 | { |
e3b83c5e | 323 | struct dev_context *devc; |
3f2c7c94 SS |
324 | struct scope_state *state; |
325 | const struct scope_config *config; | |
326 | float memsize, timediv; | |
327 | ||
328 | devc = sdi->priv; | |
329 | state = devc->model_state; | |
330 | config = devc->model_config; | |
331 | ||
332 | if (sr_scpi_get_float(sdi->conn, "MEMORY_SIZE?", &memsize) != SR_OK) | |
ea257cdc | 333 | return SR_ERR; |
3f2c7c94 SS |
334 | |
335 | if (sr_scpi_get_float(sdi->conn, "TIME_DIV?", &timediv) != SR_OK) | |
336 | return SR_ERR; | |
337 | ||
ea257cdc | 338 | state->sample_rate = 1 / ((timediv * config->num_xdivs) / memsize); |
3f2c7c94 SS |
339 | |
340 | return SR_OK; | |
341 | } | |
342 | ||
343 | SR_PRIV int lecroy_xstream_state_get(struct sr_dev_inst *sdi) | |
344 | { | |
345 | struct dev_context *devc; | |
ea257cdc UH |
346 | struct scope_state *state; |
347 | const struct scope_config *config; | |
3f2c7c94 SS |
348 | unsigned int i; |
349 | char *tmp_str, *tmp_str2, *tmpp, *p, *key; | |
350 | char command[MAX_COMMAND_SIZE]; | |
351 | char *trig_source = NULL; | |
352 | ||
353 | devc = sdi->priv; | |
354 | config = devc->model_config; | |
355 | state = devc->model_state; | |
356 | ||
357 | sr_info("Fetching scope state"); | |
358 | ||
359 | if (analog_channel_state_get(sdi->conn, config, state) != SR_OK) | |
360 | return SR_ERR; | |
361 | ||
362 | if (sr_scpi_get_string(sdi->conn, "TIME_DIV?", &tmp_str) != SR_OK) | |
363 | return SR_ERR; | |
364 | ||
53012da6 | 365 | if (array_float_get(tmp_str, ARRAY_AND_SIZE(timebases), &i) != SR_OK) { |
3f2c7c94 SS |
366 | g_free(tmp_str); |
367 | sr_err("Could not determine array index for timbase scale."); | |
368 | return SR_ERR; | |
369 | } | |
370 | g_free(tmp_str); | |
371 | state->timebase = i; | |
372 | ||
373 | if (sr_scpi_get_string(sdi->conn, "TRIG_SELECT?", &tmp_str) != SR_OK) | |
374 | return SR_ERR; | |
375 | ||
7002e64a | 376 | key = tmpp = NULL; |
3f2c7c94 SS |
377 | tmp_str2 = tmp_str; |
378 | i = 0; | |
ea257cdc | 379 | while ((p = strtok_r(tmp_str2, ",", &tmpp))) { |
3f2c7c94 SS |
380 | tmp_str2 = NULL; |
381 | if (i == 0) { | |
382 | /* trigger type */ | |
383 | } else if (i & 1) { | |
384 | key = p; | |
385 | /* key */ | |
386 | } else if (!(i & 1)) { | |
387 | if (!strcmp(key, "SR")) | |
388 | trig_source = p; | |
389 | } | |
390 | i++; | |
391 | } | |
392 | ||
692716f5 | 393 | if (!trig_source || scope_state_get_array_option(trig_source, config->trigger_sources, config->num_trigger_sources, &state->trigger_source) != SR_OK) |
3f2c7c94 SS |
394 | return SR_ERR; |
395 | ||
3f2c7c94 SS |
396 | g_snprintf(command, sizeof(command), "%s:TRIG_SLOPE?", trig_source); |
397 | if (sr_scpi_get_string(sdi->conn, command, &tmp_str) != SR_OK) | |
398 | return SR_ERR; | |
399 | ||
400 | if (scope_state_get_array_option(tmp_str, | |
692716f5 | 401 | config->trigger_slopes, config->num_trigger_slopes, &state->trigger_slope) != SR_OK) |
3f2c7c94 SS |
402 | return SR_ERR; |
403 | ||
404 | if (sr_scpi_get_float(sdi->conn, "TRIG_DELAY?", &state->horiz_triggerpos) != SR_OK) | |
405 | return SR_ERR; | |
406 | ||
407 | if (lecroy_xstream_update_sample_rate(sdi) != SR_OK) | |
408 | return SR_ERR; | |
409 | ||
410 | sr_info("Fetching finished."); | |
411 | ||
412 | scope_state_dump(config, state); | |
413 | ||
414 | return SR_OK; | |
415 | } | |
416 | ||
417 | static struct scope_state *scope_state_new(const struct scope_config *config) | |
418 | { | |
419 | struct scope_state *state; | |
420 | ||
421 | state = g_malloc0(sizeof(struct scope_state)); | |
422 | state->analog_channels = g_malloc0_n(config->analog_channels, | |
423 | sizeof(struct analog_channel_state)); | |
424 | return state; | |
425 | } | |
426 | ||
427 | SR_PRIV void lecroy_xstream_state_free(struct scope_state *state) | |
428 | { | |
429 | g_free(state->analog_channels); | |
430 | g_free(state); | |
431 | } | |
432 | ||
433 | SR_PRIV int lecroy_xstream_init_device(struct sr_dev_inst *sdi) | |
434 | { | |
435 | char command[MAX_COMMAND_SIZE]; | |
436 | int model_index; | |
437 | unsigned int i, j; | |
438 | struct sr_channel *ch; | |
439 | struct dev_context *devc; | |
440 | gboolean channel_enabled; | |
441 | ||
442 | devc = sdi->priv; | |
443 | model_index = -1; | |
444 | ||
445 | /* Find the exact model. */ | |
446 | for (i = 0; i < ARRAY_SIZE(scope_models); i++) { | |
447 | for (j = 0; scope_models[i].name[j]; j++) { | |
448 | if (!strcmp(sdi->model, scope_models[i].name[j])) { | |
449 | model_index = i; | |
450 | break; | |
451 | } | |
452 | } | |
453 | if (model_index != -1) | |
454 | break; | |
455 | } | |
456 | ||
457 | if (model_index == -1) { | |
ea257cdc | 458 | sr_dbg("Unsupported LeCroy device."); |
3f2c7c94 SS |
459 | return SR_ERR_NA; |
460 | } | |
461 | ||
462 | devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) * | |
ea257cdc | 463 | scope_models[model_index].analog_channels); |
3f2c7c94 SS |
464 | |
465 | /* Add analog channels. */ | |
466 | for (i = 0; i < scope_models[model_index].analog_channels; i++) { | |
ea257cdc | 467 | g_snprintf(command, sizeof(command), "C%d:TRACE?", i + 1); |
3f2c7c94 SS |
468 | |
469 | if (sr_scpi_get_bool(sdi->conn, command, &channel_enabled) != SR_OK) | |
470 | return SR_ERR; | |
471 | ||
ea257cdc | 472 | g_snprintf(command, sizeof(command), "C%d:VDIV?", i + 1); |
3f2c7c94 SS |
473 | |
474 | ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, channel_enabled, | |
475 | (*scope_models[model_index].analog_names)[i]); | |
476 | ||
477 | devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group)); | |
478 | ||
479 | devc->analog_groups[i]->name = g_strdup( | |
480 | (char *)(*scope_models[model_index].analog_names)[i]); | |
481 | devc->analog_groups[i]->channels = g_slist_append(NULL, ch); | |
482 | ||
483 | sdi->channel_groups = g_slist_append(sdi->channel_groups, | |
484 | devc->analog_groups[i]); | |
485 | } | |
486 | ||
487 | devc->model_config = &scope_models[model_index]; | |
488 | devc->frame_limit = 0; | |
489 | ||
490 | if (!(devc->model_state = scope_state_new(devc->model_config))) | |
491 | return SR_ERR_MALLOC; | |
492 | ||
d0b913ea SA |
493 | /* Set the desired response mode. */ |
494 | sr_scpi_send(sdi->conn, "COMM_HEADER OFF,WORD,BIN"); | |
495 | ||
3f2c7c94 SS |
496 | return SR_OK; |
497 | } | |
498 | ||
499 | static int lecroy_waveform_2_x_to_analog(GByteArray *data, | |
500 | struct lecroy_wavedesc *desc, | |
501 | struct sr_datafeed_analog *analog) | |
502 | { | |
503 | struct sr_analog_encoding *encoding = analog->encoding; | |
504 | struct sr_analog_meaning *meaning = analog->meaning; | |
505 | struct sr_analog_spec *spec = analog->spec; | |
506 | float *data_float; | |
507 | int16_t *waveform_data; | |
508 | unsigned int i, num_samples; | |
509 | ||
ea257cdc | 510 | data_float = g_malloc(desc->version_2_x.wave_array_count * sizeof(float)); |
3f2c7c94 SS |
511 | num_samples = desc->version_2_x.wave_array_count; |
512 | ||
513 | waveform_data = (int16_t *)(data->data + | |
514 | + desc->version_2_x.wave_descriptor_length | |
515 | + desc->version_2_x.user_text_len); | |
516 | ||
ea257cdc | 517 | for (i = 0; i < num_samples; i++) |
3f2c7c94 SS |
518 | data_float[i] = (float)waveform_data[i] |
519 | * desc->version_2_x.vertical_gain | |
520 | + desc->version_2_x.vertical_offset; | |
521 | ||
3f2c7c94 SS |
522 | analog->data = data_float; |
523 | analog->num_samples = num_samples; | |
524 | ||
525 | encoding->unitsize = sizeof(float); | |
526 | encoding->is_signed = TRUE; | |
527 | encoding->is_float = TRUE; | |
528 | encoding->is_bigendian = FALSE; | |
529 | encoding->scale.p = 1; | |
530 | encoding->scale.q = 1; | |
531 | encoding->offset.p = 0; | |
532 | encoding->offset.q = 1; | |
533 | ||
534 | encoding->digits = 6; | |
535 | encoding->is_digits_decimal = FALSE; | |
536 | ||
537 | if (strcmp(desc->version_2_x.vertunit, "A")) { | |
538 | meaning->mq = SR_MQ_CURRENT; | |
539 | meaning->unit = SR_UNIT_AMPERE; | |
540 | } else { | |
ea257cdc | 541 | /* Default to voltage. */ |
3f2c7c94 SS |
542 | meaning->mq = SR_MQ_VOLTAGE; |
543 | meaning->unit = SR_UNIT_VOLT; | |
544 | } | |
545 | ||
546 | meaning->mqflags = 0; | |
547 | spec->spec_digits = 3; | |
ea257cdc | 548 | |
3f2c7c94 SS |
549 | return SR_OK; |
550 | } | |
551 | ||
552 | static int lecroy_waveform_to_analog(GByteArray *data, | |
553 | struct sr_datafeed_analog *analog) | |
554 | { | |
555 | struct lecroy_wavedesc *desc; | |
556 | ||
557 | if (data->len < sizeof(struct lecroy_wavedesc)) | |
558 | return SR_ERR; | |
559 | ||
560 | desc = (struct lecroy_wavedesc *)data->data; | |
561 | ||
562 | if (!strncmp(desc->template_name, "LECROY_2_2", 16) || | |
563 | !strncmp(desc->template_name, "LECROY_2_3", 16)) { | |
564 | return lecroy_waveform_2_x_to_analog(data, desc, analog); | |
565 | } | |
566 | ||
ea257cdc UH |
567 | sr_err("Waveformat template '%.16s' not supported.", |
568 | desc->template_name); | |
569 | ||
3f2c7c94 SS |
570 | return SR_ERR; |
571 | } | |
572 | ||
573 | SR_PRIV int lecroy_xstream_receive_data(int fd, int revents, void *cb_data) | |
574 | { | |
575 | struct sr_channel *ch; | |
576 | struct sr_dev_inst *sdi; | |
577 | struct dev_context *devc; | |
578 | struct sr_datafeed_packet packet; | |
579 | GByteArray *data; | |
580 | struct sr_datafeed_analog analog; | |
581 | struct sr_analog_encoding encoding; | |
582 | struct sr_analog_meaning meaning; | |
583 | struct sr_analog_spec spec; | |
584 | char buf[8]; | |
ea257cdc | 585 | |
e3b83c5e | 586 | (void)fd; |
3f2c7c94 SS |
587 | (void)revents; |
588 | ||
589 | data = NULL; | |
e3b83c5e SS |
590 | |
591 | if (!(sdi = cb_data)) | |
592 | return TRUE; | |
593 | ||
594 | if (!(devc = sdi->priv)) | |
595 | return TRUE; | |
596 | ||
3f2c7c94 SS |
597 | ch = devc->current_channel->data; |
598 | ||
599 | /* | |
600 | * Send "frame begin" packet upon reception of data for the | |
601 | * first enabled channel. | |
602 | */ | |
603 | if (devc->current_channel == devc->enabled_channels) { | |
604 | packet.type = SR_DF_FRAME_BEGIN; | |
605 | sr_session_send(sdi, &packet); | |
606 | } | |
607 | ||
608 | if (ch->type != SR_CHANNEL_ANALOG) | |
609 | return SR_ERR; | |
ea257cdc UH |
610 | |
611 | /* Pass on the received data of the channel(s). */ | |
3f2c7c94 | 612 | if (sr_scpi_read_data(sdi->conn, buf, 4) != 4) { |
ea257cdc | 613 | sr_err("Reading header failed."); |
3f2c7c94 SS |
614 | return TRUE; |
615 | } | |
616 | ||
617 | if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) { | |
618 | if (data) | |
619 | g_byte_array_free(data, TRUE); | |
620 | return TRUE; | |
621 | } | |
622 | ||
623 | analog.encoding = &encoding; | |
624 | analog.meaning = &meaning; | |
625 | analog.spec = &spec; | |
626 | ||
627 | if (lecroy_waveform_to_analog(data, &analog) != SR_OK) | |
628 | return SR_ERR; | |
629 | ||
630 | meaning.channels = g_slist_append(NULL, ch); | |
631 | packet.payload = &analog; | |
632 | packet.type = SR_DF_ANALOG; | |
633 | sr_session_send(sdi, &packet); | |
634 | ||
635 | g_byte_array_free(data, TRUE); | |
636 | data = NULL; | |
637 | ||
638 | g_slist_free(meaning.channels); | |
639 | g_free(analog.data); | |
640 | ||
3f2c7c94 SS |
641 | /* |
642 | * Advance to the next enabled channel. When data for all enabled | |
643 | * channels was received, then flush potentially queued logic data, | |
644 | * and send the "frame end" packet. | |
645 | */ | |
646 | if (devc->current_channel->next) { | |
647 | devc->current_channel = devc->current_channel->next; | |
648 | lecroy_xstream_request_data(sdi); | |
649 | return TRUE; | |
650 | } | |
651 | ||
652 | packet.type = SR_DF_FRAME_END; | |
653 | sr_session_send(sdi, &packet); | |
654 | ||
655 | /* | |
656 | * End of frame was reached. Stop acquisition after the specified | |
657 | * number of frames, or continue reception by starting over at | |
658 | * the first enabled channel. | |
659 | */ | |
660 | if (++devc->num_frames == devc->frame_limit) { | |
d2f7c417 | 661 | sr_dev_acquisition_stop(sdi); |
3f2c7c94 SS |
662 | } else { |
663 | devc->current_channel = devc->enabled_channels; | |
664 | lecroy_xstream_request_data(sdi); | |
e3b83c5e SS |
665 | } |
666 | ||
667 | return TRUE; | |
668 | } |