]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/hameg-hmo/protocol.c
hameg-hmo: Fix the upper limit for the vertical scale.
[libsigrok.git] / src / hardware / hameg-hmo / protocol.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
5 * Copyright (C) 2018 Guido Trentalancia <guido@trentalancia.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <config.h>
22#include <math.h>
23#include <stdlib.h>
24#include "scpi.h"
25#include "protocol.h"
26
27SR_PRIV void hmo_queue_logic_data(struct dev_context *devc,
28 size_t group, GByteArray *pod_data);
29SR_PRIV void hmo_send_logic_packet(struct sr_dev_inst *sdi,
30 struct dev_context *devc);
31SR_PRIV void hmo_cleanup_logic_data(struct dev_context *devc);
32
33static const char *hameg_scpi_dialect[] = {
34 [SCPI_CMD_GET_DIG_DATA] = ":FORM UINT,8;:POD%d:DATA?",
35 [SCPI_CMD_GET_TIMEBASE] = ":TIM:SCAL?",
36 [SCPI_CMD_SET_TIMEBASE] = ":TIM:SCAL %s",
37 [SCPI_CMD_GET_COUPLING] = ":CHAN%d:COUP?",
38 [SCPI_CMD_SET_COUPLING] = ":CHAN%d:COUP %s",
39 [SCPI_CMD_GET_SAMPLE_RATE] = ":ACQ:SRAT?",
40 [SCPI_CMD_GET_ANALOG_DATA] = ":FORM:BORD %s;" \
41 ":FORM REAL,32;:CHAN%d:DATA?",
42 [SCPI_CMD_GET_VERTICAL_DIV] = ":CHAN%d:SCAL?",
43 [SCPI_CMD_SET_VERTICAL_DIV] = ":CHAN%d:SCAL %s",
44 [SCPI_CMD_GET_DIG_POD_STATE] = ":POD%d:STAT?",
45 [SCPI_CMD_SET_DIG_POD_STATE] = ":POD%d:STAT %d",
46 [SCPI_CMD_GET_TRIGGER_SLOPE] = ":TRIG:A:EDGE:SLOP?",
47 [SCPI_CMD_SET_TRIGGER_SLOPE] = ":TRIG:A:TYPE EDGE;:TRIG:A:EDGE:SLOP %s",
48 [SCPI_CMD_GET_TRIGGER_PATTERN] = ":TRIG:A:PATT:SOUR?",
49 [SCPI_CMD_SET_TRIGGER_PATTERN] = ":TRIG:A:TYPE LOGIC;" \
50 ":TRIG:A:PATT:FUNC AND;" \
51 ":TRIG:A:PATT:COND TRUE;" \
52 ":TRIG:A:PATT:MODE OFF;" \
53 ":TRIG:A:PATT:SOUR \"%s\"",
54 [SCPI_CMD_GET_TRIGGER_SOURCE] = ":TRIG:A:SOUR?",
55 [SCPI_CMD_SET_TRIGGER_SOURCE] = ":TRIG:A:SOUR %s",
56 [SCPI_CMD_GET_DIG_CHAN_STATE] = ":LOG%d:STAT?",
57 [SCPI_CMD_SET_DIG_CHAN_STATE] = ":LOG%d:STAT %d",
58 [SCPI_CMD_GET_VERTICAL_OFFSET] = ":CHAN%d:POS?",
59 [SCPI_CMD_GET_HORIZ_TRIGGERPOS] = ":TIM:POS?",
60 [SCPI_CMD_SET_HORIZ_TRIGGERPOS] = ":TIM:POS %s",
61 [SCPI_CMD_GET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT?",
62 [SCPI_CMD_SET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT %d",
63 [SCPI_CMD_GET_PROBE_UNIT] = ":PROB%d:SET:ATT:UNIT?",
64 [SCPI_CMD_GET_DIG_POD_THRESHOLD] = ":POD%d:THR?",
65 [SCPI_CMD_SET_DIG_POD_THRESHOLD] = ":POD%d:THR %s",
66 [SCPI_CMD_GET_DIG_POD_USER_THRESHOLD] = ":POD%d:THR:UDL%d?",
67 [SCPI_CMD_SET_DIG_POD_USER_THRESHOLD] = ":POD%d:THR:UDL%d %s",
68};
69
70static const uint32_t devopts[] = {
71 SR_CONF_OSCILLOSCOPE,
72 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
73 SR_CONF_LIMIT_FRAMES | SR_CONF_SET,
74 SR_CONF_SAMPLERATE | SR_CONF_GET,
75 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
76 SR_CONF_NUM_HDIV | SR_CONF_GET,
77 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
78 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
79 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
80 SR_CONF_TRIGGER_PATTERN | SR_CONF_GET | SR_CONF_SET,
81};
82
83static const uint32_t devopts_cg_analog[] = {
84 SR_CONF_NUM_VDIV | SR_CONF_GET,
85 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
86 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
87};
88
89static const uint32_t devopts_cg_digital[] = {
90 SR_CONF_LOGIC_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
91 SR_CONF_LOGIC_THRESHOLD_CUSTOM | SR_CONF_GET | SR_CONF_SET,
92};
93
94static const char *coupling_options[] = {
95 "AC", // AC with 50 Ohm termination (152x, 202x, 30xx, 1202)
96 "ACL", // AC with 1 MOhm termination
97 "DC", // DC with 50 Ohm termination
98 "DCL", // DC with 1 MOhm termination
99 "GND",
100};
101
102static const char *scope_trigger_slopes[] = {
103 "POS",
104 "NEG",
105 "EITH",
106};
107
108/* Predefined logic thresholds. */
109static const char *logic_threshold[] = {
110 "TTL",
111 "ECL",
112 "CMOS",
113 "USER1",
114 "USER2", // overwritten by logic_threshold_custom, use USER1 for permanent setting
115};
116
117/* RTC1002, HMO Compact2 and HMO1002/HMO1202 */
118static const char *an2_dig8_trigger_sources[] = {
119 "CH1", "CH2",
120 "LINE", "EXT", "PATT", "BUS1", "BUS2",
121 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
122};
123
124/* HMO3xx2 */
125static const char *an2_dig16_trigger_sources[] = {
126 "CH1", "CH2",
127 "LINE", "EXT", "PATT", "BUS1", "BUS2",
128 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
129 "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
130};
131
132/* HMO Compact4 */
133static const char *an4_dig8_trigger_sources[] = {
134 "CH1", "CH2", "CH3", "CH4",
135 "LINE", "EXT", "PATT", "BUS1", "BUS2",
136 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
137};
138
139/* HMO3xx4 and HMO2524 */
140static const char *an4_dig16_trigger_sources[] = {
141 "CH1", "CH2", "CH3", "CH4",
142 "LINE", "EXT", "PATT", "BUS1", "BUS2",
143 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
144 "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
145};
146
147static const uint64_t timebases[][2] = {
148 /* nanoseconds */
149 { 2, 1000000000 },
150 { 5, 1000000000 },
151 { 10, 1000000000 },
152 { 20, 1000000000 },
153 { 50, 1000000000 },
154 { 100, 1000000000 },
155 { 200, 1000000000 },
156 { 500, 1000000000 },
157 /* microseconds */
158 { 1, 1000000 },
159 { 2, 1000000 },
160 { 5, 1000000 },
161 { 10, 1000000 },
162 { 20, 1000000 },
163 { 50, 1000000 },
164 { 100, 1000000 },
165 { 200, 1000000 },
166 { 500, 1000000 },
167 /* milliseconds */
168 { 1, 1000 },
169 { 2, 1000 },
170 { 5, 1000 },
171 { 10, 1000 },
172 { 20, 1000 },
173 { 50, 1000 },
174 { 100, 1000 },
175 { 200, 1000 },
176 { 500, 1000 },
177 /* seconds */
178 { 1, 1 },
179 { 2, 1 },
180 { 5, 1 },
181 { 10, 1 },
182 { 20, 1 },
183 { 50, 1 },
184};
185
186static const uint64_t vdivs[][2] = {
187 /* millivolts */
188 { 1, 1000 },
189 { 2, 1000 },
190 { 5, 1000 },
191 { 10, 1000 },
192 { 20, 1000 },
193 { 50, 1000 },
194 { 100, 1000 },
195 { 200, 1000 },
196 { 500, 1000 },
197 /* volts */
198 { 1, 1 },
199 { 2, 1 },
200 { 5, 1 },
201 { 10, 1 },
202};
203
204static const char *scope_analog_channel_names[] = {
205 "CH1", "CH2", "CH3", "CH4",
206};
207
208static const char *scope_digital_channel_names[] = {
209 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
210 "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
211};
212
213static const struct scope_config scope_models[] = {
214 {
215 /* RTC1002 and HMO722/1002/1022/1202/1522/2022 support only 8 digital channels. */
216 .name = {"RTC1002", "HMO722", "HMO1002", "HMO1022", "HMO1202", "HMO1522", "HMO2022", NULL},
217 .analog_channels = 2,
218 .digital_channels = 8,
219 .digital_pods = 1,
220
221 .analog_names = &scope_analog_channel_names,
222 .digital_names = &scope_digital_channel_names,
223
224 .devopts = &devopts,
225 .num_devopts = ARRAY_SIZE(devopts),
226
227 .devopts_cg_analog = &devopts_cg_analog,
228 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
229
230 .devopts_cg_digital = &devopts_cg_digital,
231 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
232
233 .coupling_options = &coupling_options,
234 .num_coupling_options = ARRAY_SIZE(coupling_options),
235
236 .logic_threshold = &logic_threshold,
237 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
238
239 .trigger_sources = &an2_dig8_trigger_sources,
240 .num_trigger_sources = ARRAY_SIZE(an2_dig8_trigger_sources),
241
242 .trigger_slopes = &scope_trigger_slopes,
243 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
244
245 .timebases = &timebases,
246 .num_timebases = ARRAY_SIZE(timebases),
247
248 .vdivs = &vdivs,
249 .num_vdivs = ARRAY_SIZE(vdivs),
250
251 .num_xdivs = 12,
252 .num_ydivs = 8,
253
254 .scpi_dialect = &hameg_scpi_dialect,
255 },
256 {
257 /* HMO3032/3042/3052/3522 support 16 digital channels. */
258 .name = {"HMO3032", "HMO3042", "HMO3052", "HMO3522", NULL},
259 .analog_channels = 2,
260 .digital_channels = 16,
261 .digital_pods = 2,
262
263 .analog_names = &scope_analog_channel_names,
264 .digital_names = &scope_digital_channel_names,
265
266 .devopts = &devopts,
267 .num_devopts = ARRAY_SIZE(devopts),
268
269 .devopts_cg_analog = &devopts_cg_analog,
270 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
271
272 .devopts_cg_digital = &devopts_cg_digital,
273 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
274
275 .coupling_options = &coupling_options,
276 .num_coupling_options = ARRAY_SIZE(coupling_options),
277
278 .logic_threshold = &logic_threshold,
279 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
280
281 .trigger_sources = &an2_dig16_trigger_sources,
282 .num_trigger_sources = ARRAY_SIZE(an2_dig16_trigger_sources),
283
284 .trigger_slopes = &scope_trigger_slopes,
285 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
286
287 .timebases = &timebases,
288 .num_timebases = ARRAY_SIZE(timebases),
289
290 .vdivs = &vdivs,
291 .num_vdivs = ARRAY_SIZE(vdivs),
292
293 .num_xdivs = 12,
294 .num_ydivs = 8,
295
296 .scpi_dialect = &hameg_scpi_dialect,
297 },
298 {
299 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", NULL},
300 .analog_channels = 4,
301 .digital_channels = 8,
302 .digital_pods = 1,
303
304 .analog_names = &scope_analog_channel_names,
305 .digital_names = &scope_digital_channel_names,
306
307 .devopts = &devopts,
308 .num_devopts = ARRAY_SIZE(devopts),
309
310 .devopts_cg_analog = &devopts_cg_analog,
311 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
312
313 .devopts_cg_digital = &devopts_cg_digital,
314 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
315
316 .coupling_options = &coupling_options,
317 .num_coupling_options = ARRAY_SIZE(coupling_options),
318
319 .logic_threshold = &logic_threshold,
320 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
321
322 .trigger_sources = &an4_dig8_trigger_sources,
323 .num_trigger_sources = ARRAY_SIZE(an4_dig8_trigger_sources),
324
325 .trigger_slopes = &scope_trigger_slopes,
326 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
327
328 .timebases = &timebases,
329 .num_timebases = ARRAY_SIZE(timebases),
330
331 .vdivs = &vdivs,
332 .num_vdivs = ARRAY_SIZE(vdivs),
333
334 .num_xdivs = 12,
335 .num_ydivs = 8,
336
337 .scpi_dialect = &hameg_scpi_dialect,
338 },
339 {
340 .name = {"HMO2524", "HMO3034", "HMO3044", "HMO3054", "HMO3524", NULL},
341 .analog_channels = 4,
342 .digital_channels = 16,
343 .digital_pods = 2,
344
345 .analog_names = &scope_analog_channel_names,
346 .digital_names = &scope_digital_channel_names,
347
348 .devopts = &devopts,
349 .num_devopts = ARRAY_SIZE(devopts),
350
351 .devopts_cg_analog = &devopts_cg_analog,
352 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
353
354 .devopts_cg_digital = &devopts_cg_digital,
355 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
356
357 .coupling_options = &coupling_options,
358 .num_coupling_options = ARRAY_SIZE(coupling_options),
359
360 .logic_threshold = &logic_threshold,
361 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
362
363 .trigger_sources = &an4_dig16_trigger_sources,
364 .num_trigger_sources = ARRAY_SIZE(an4_dig16_trigger_sources),
365
366 .trigger_slopes = &scope_trigger_slopes,
367 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
368
369 .timebases = &timebases,
370 .num_timebases = ARRAY_SIZE(timebases),
371
372 .vdivs = &vdivs,
373 .num_vdivs = ARRAY_SIZE(vdivs),
374
375 .num_xdivs = 12,
376 .num_ydivs = 8,
377
378 .scpi_dialect = &hameg_scpi_dialect,
379 },
380};
381
382static void scope_state_dump(const struct scope_config *config,
383 struct scope_state *state)
384{
385 unsigned int i;
386 char *tmp;
387
388 for (i = 0; i < config->analog_channels; i++) {
389 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
390 (*config->vdivs)[state->analog_channels[i].vdiv][1]);
391 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
392 i + 1, state->analog_channels[i].state ? "On" : "Off",
393 (*config->coupling_options)[state->analog_channels[i].coupling],
394 tmp, state->analog_channels[i].vertical_offset);
395 }
396
397 for (i = 0; i < config->digital_channels; i++) {
398 sr_info("State of digital channel %d -> %s", i,
399 state->digital_channels[i] ? "On" : "Off");
400 }
401
402 for (i = 0; i < config->digital_pods; i++) {
403 if (strncmp("USER", (*config->logic_threshold)[state->digital_pods[i].threshold], 4))
404 sr_info("State of digital POD %d -> %s : %s (threshold)", i + 1,
405 state->digital_pods[i].state ? "On" : "Off",
406 (*config->logic_threshold)[state->digital_pods[i].threshold]);
407 else // user-defined or custom logic threshold
408 sr_info("State of digital POD %d -> %s : %E (threshold)", i + 1,
409 state->digital_pods[i].state ? "On" : "Off",
410 state->digital_pods[i].user_threshold);
411 }
412
413 tmp = sr_period_string((*config->timebases)[state->timebase][0],
414 (*config->timebases)[state->timebase][1]);
415 sr_info("Current timebase: %s", tmp);
416 g_free(tmp);
417
418 tmp = sr_samplerate_string(state->sample_rate);
419 sr_info("Current samplerate: %s", tmp);
420 g_free(tmp);
421
422 if (!strcmp("PATT", (*config->trigger_sources)[state->trigger_source]))
423 sr_info("Current trigger: %s (pattern), %.2f (offset)",
424 state->trigger_pattern,
425 state->horiz_triggerpos);
426 else // Edge (slope) trigger
427 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
428 (*config->trigger_sources)[state->trigger_source],
429 (*config->trigger_slopes)[state->trigger_slope],
430 state->horiz_triggerpos);
431}
432
433static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
434 const char *command, const char *(*array)[], unsigned int n, int *result)
435{
436 char *tmp;
437 int idx;
438
439 if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK)
440 return SR_ERR;
441
442 if ((idx = std_str_idx_s(tmp, *array, n)) < 0) {
443 g_free(tmp);
444 return SR_ERR_ARG;
445 }
446
447 *result = idx;
448
449 g_free(tmp);
450
451 return SR_OK;
452}
453
454/**
455 * This function takes a value of the form "2.000E-03" and returns the index
456 * of an array where a matching pair was found.
457 *
458 * @param value The string to be parsed.
459 * @param array The array of s/f pairs.
460 * @param array_len The number of pairs in the array.
461 * @param result The index at which a matching pair was found.
462 *
463 * @return SR_ERR on any parsing error, SR_OK otherwise.
464 */
465static int array_float_get(gchar *value, const uint64_t array[][2],
466 int array_len, unsigned int *result)
467{
468 struct sr_rational rval;
469 struct sr_rational aval;
470
471 if (sr_parse_rational(value, &rval) != SR_OK)
472 return SR_ERR;
473
474 for (int i = 0; i < array_len; i++) {
475 sr_rational_set(&aval, array[i][0], array[i][1]);
476 if (sr_rational_eq(&rval, &aval)) {
477 *result = i;
478 return SR_OK;
479 }
480 }
481
482 return SR_ERR;
483}
484
485static struct sr_channel *get_channel_by_index_and_type(GSList *channel_lhead,
486 int index, int type)
487{
488 while (channel_lhead) {
489 struct sr_channel *ch = channel_lhead->data;
490 if (ch->index == index && ch->type == type)
491 return ch;
492
493 channel_lhead = channel_lhead->next;
494 }
495
496 return 0;
497}
498
499static int analog_channel_state_get(struct sr_dev_inst *sdi,
500 const struct scope_config *config,
501 struct scope_state *state)
502{
503 unsigned int i, j;
504 char command[MAX_COMMAND_SIZE];
505 char *tmp_str;
506 struct sr_channel *ch;
507 struct sr_scpi_dev_inst *scpi = sdi->conn;
508
509 for (i = 0; i < config->analog_channels; i++) {
510 g_snprintf(command, sizeof(command),
511 (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
512 i + 1);
513
514 if (sr_scpi_get_bool(scpi, command,
515 &state->analog_channels[i].state) != SR_OK)
516 return SR_ERR;
517
518 ch = get_channel_by_index_and_type(sdi->channels, i, SR_CHANNEL_ANALOG);
519 if (ch)
520 ch->enabled = state->analog_channels[i].state;
521
522 g_snprintf(command, sizeof(command),
523 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
524 i + 1);
525
526 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
527 return SR_ERR;
528
529 if (array_float_get(tmp_str, ARRAY_AND_SIZE(vdivs), &j) != SR_OK) {
530 g_free(tmp_str);
531 sr_err("Could not determine array index for vertical div scale.");
532 return SR_ERR;
533 }
534
535 g_free(tmp_str);
536 state->analog_channels[i].vdiv = j;
537
538 g_snprintf(command, sizeof(command),
539 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
540 i + 1);
541
542 if (sr_scpi_get_float(scpi, command,
543 &state->analog_channels[i].vertical_offset) != SR_OK)
544 return SR_ERR;
545
546 g_snprintf(command, sizeof(command),
547 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
548 i + 1);
549
550 if (scope_state_get_array_option(scpi, command, config->coupling_options,
551 config->num_coupling_options,
552 &state->analog_channels[i].coupling) != SR_OK)
553 return SR_ERR;
554
555 g_snprintf(command, sizeof(command),
556 (*config->scpi_dialect)[SCPI_CMD_GET_PROBE_UNIT],
557 i + 1);
558
559 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
560 return SR_ERR;
561
562 if (tmp_str[0] == 'A')
563 state->analog_channels[i].probe_unit = 'A';
564 else
565 state->analog_channels[i].probe_unit = 'V';
566 g_free(tmp_str);
567 }
568
569 return SR_OK;
570}
571
572static int digital_channel_state_get(struct sr_dev_inst *sdi,
573 const struct scope_config *config,
574 struct scope_state *state)
575{
576 unsigned int i;
577 int result = SR_ERR;
578 static char *logic_threshold_short[] = {};
579 char command[MAX_COMMAND_SIZE];
580 struct sr_channel *ch;
581 struct sr_scpi_dev_inst *scpi = sdi->conn;
582
583 for (i = 0; i < config->digital_channels; i++) {
584 g_snprintf(command, sizeof(command),
585 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
586 i);
587
588 if (sr_scpi_get_bool(scpi, command,
589 &state->digital_channels[i]) != SR_OK)
590 return SR_ERR;
591
592 ch = get_channel_by_index_and_type(sdi->channels, i, SR_CHANNEL_LOGIC);
593 if (ch)
594 ch->enabled = state->digital_channels[i];
595 }
596
597 /* According to the SCPI standard, the response to the command
598 * SCPI_CMD_GET_DIG_POD_THRESHOLD might return "USER" instead of
599 * "USER1".
600 *
601 * This makes more difficult to validate the response when the logic
602 * threshold is set to "USER1" and therefore we need to prevent device
603 * opening failures in such configuration case...
604 */
605 for (i = 0; i < config->num_logic_threshold; i++) {
606 logic_threshold_short[i] = g_strdup((*config->logic_threshold)[i]);
607 if (!strcmp("USER1", (*config->logic_threshold)[i]))
608 g_strlcpy(logic_threshold_short[i],
609 (*config->logic_threshold)[i], strlen((*config->logic_threshold)[i]));
610 }
611
612 for (i = 0; i < config->digital_pods; i++) {
613 g_snprintf(command, sizeof(command),
614 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
615 i + 1);
616
617 if (sr_scpi_get_bool(scpi, command,
618 &state->digital_pods[i].state) != SR_OK)
619 goto exit;
620
621 g_snprintf(command, sizeof(command),
622 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_THRESHOLD],
623 i + 1);
624
625 /* Check for both standard and shortened responses. */
626 if (scope_state_get_array_option(scpi, command, config->logic_threshold,
627 config->num_logic_threshold,
628 &state->digital_pods[i].threshold) != SR_OK)
629 if (scope_state_get_array_option(scpi, command, (const char * (*)[]) &logic_threshold_short,
630 config->num_logic_threshold,
631 &state->digital_pods[i].threshold) != SR_OK)
632 goto exit;
633
634 if (!strcmp("USER1", (*config->logic_threshold)[state->digital_pods[i].threshold]))
635 g_snprintf(command, sizeof(command),
636 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_USER_THRESHOLD],
637 i + 1, 1); // USER1 logic threshold setting
638
639 if (!strcmp("USER2", (*config->logic_threshold)[state->digital_pods[i].threshold]))
640 g_snprintf(command, sizeof(command),
641 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_USER_THRESHOLD],
642 i + 1, 2); // USER2 for custom logic_threshold setting
643
644 if (!strcmp("USER1", (*config->logic_threshold)[state->digital_pods[i].threshold]) ||
645 !strcmp("USER2", (*config->logic_threshold)[state->digital_pods[i].threshold]))
646 if (sr_scpi_get_float(scpi, command,
647 &state->digital_pods[i].user_threshold) != SR_OK)
648 goto exit;
649 }
650
651 result = SR_OK;
652
653exit:
654 for (i = 0; i < config->num_logic_threshold; i++)
655 g_free(logic_threshold_short[i]);
656
657 return result;
658}
659
660SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
661{
662 struct dev_context *devc;
663 struct scope_state *state;
664 const struct scope_config *config;
665 float tmp_float;
666
667 devc = sdi->priv;
668 config = devc->model_config;
669 state = devc->model_state;
670
671 if (sr_scpi_get_float(sdi->conn,
672 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE],
673 &tmp_float) != SR_OK)
674 return SR_ERR;
675
676 state->sample_rate = tmp_float;
677
678 return SR_OK;
679}
680
681SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
682{
683 struct dev_context *devc;
684 struct scope_state *state;
685 const struct scope_config *config;
686 float tmp_float;
687 unsigned int i;
688 char *tmp_str;
689
690 devc = sdi->priv;
691 config = devc->model_config;
692 state = devc->model_state;
693
694 sr_info("Fetching scope state");
695
696 if (analog_channel_state_get(sdi, config, state) != SR_OK)
697 return SR_ERR;
698
699 if (digital_channel_state_get(sdi, config, state) != SR_OK)
700 return SR_ERR;
701
702 if (sr_scpi_get_float(sdi->conn,
703 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
704 &tmp_float) != SR_OK)
705 return SR_ERR;
706
707 if (sr_scpi_get_string(sdi->conn,
708 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
709 &tmp_str) != SR_OK)
710 return SR_ERR;
711
712 if (array_float_get(tmp_str, ARRAY_AND_SIZE(timebases), &i) != SR_OK) {
713 g_free(tmp_str);
714 sr_err("Could not determine array index for time base.");
715 return SR_ERR;
716 }
717 g_free(tmp_str);
718
719 state->timebase = i;
720
721 if (sr_scpi_get_float(sdi->conn,
722 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
723 &tmp_float) != SR_OK)
724 return SR_ERR;
725 state->horiz_triggerpos = tmp_float /
726 (((double) (*config->timebases)[state->timebase][0] /
727 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
728 state->horiz_triggerpos -= 0.5;
729 state->horiz_triggerpos *= -1;
730
731 if (scope_state_get_array_option(sdi->conn,
732 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
733 config->trigger_sources, config->num_trigger_sources,
734 &state->trigger_source) != SR_OK)
735 return SR_ERR;
736
737 if (scope_state_get_array_option(sdi->conn,
738 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
739 config->trigger_slopes, config->num_trigger_slopes,
740 &state->trigger_slope) != SR_OK)
741 return SR_ERR;
742
743 if (sr_scpi_get_string(sdi->conn,
744 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_PATTERN],
745 &state->trigger_pattern) != SR_OK)
746 return SR_ERR;
747
748 if (hmo_update_sample_rate(sdi) != SR_OK)
749 return SR_ERR;
750
751 sr_info("Fetching finished.");
752
753 scope_state_dump(config, state);
754
755 return SR_OK;
756}
757
758static struct scope_state *scope_state_new(const struct scope_config *config)
759{
760 struct scope_state *state;
761
762 state = g_malloc0(sizeof(struct scope_state));
763 state->analog_channels = g_malloc0_n(config->analog_channels,
764 sizeof(struct analog_channel_state));
765 state->digital_channels = g_malloc0_n(
766 config->digital_channels, sizeof(gboolean));
767 state->digital_pods = g_malloc0_n(config->digital_pods,
768 sizeof(struct digital_pod_state));
769
770 return state;
771}
772
773SR_PRIV void hmo_scope_state_free(struct scope_state *state)
774{
775 g_free(state->analog_channels);
776 g_free(state->digital_channels);
777 g_free(state->digital_pods);
778 g_free(state);
779}
780
781SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
782{
783 int model_index;
784 unsigned int i, j, group;
785 struct sr_channel *ch;
786 struct dev_context *devc;
787 int ret;
788
789 devc = sdi->priv;
790 model_index = -1;
791
792 /* Find the exact model. */
793 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
794 for (j = 0; scope_models[i].name[j]; j++) {
795 if (!strcmp(sdi->model, scope_models[i].name[j])) {
796 model_index = i;
797 break;
798 }
799 }
800 if (model_index != -1)
801 break;
802 }
803
804 if (model_index == -1) {
805 sr_dbg("Unsupported device.");
806 return SR_ERR_NA;
807 }
808
809 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
810 scope_models[model_index].analog_channels);
811 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
812 scope_models[model_index].digital_pods);
813 if (!devc->analog_groups || !devc->digital_groups) {
814 g_free(devc->analog_groups);
815 g_free(devc->digital_groups);
816 return SR_ERR_MALLOC;
817 }
818
819 /* Add analog channels. */
820 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
821 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
822 (*scope_models[model_index].analog_names)[i]);
823
824 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
825
826 devc->analog_groups[i]->name = g_strdup(
827 (char *)(*scope_models[model_index].analog_names)[i]);
828 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
829
830 sdi->channel_groups = g_slist_append(sdi->channel_groups,
831 devc->analog_groups[i]);
832 }
833
834 /* Add digital channel groups. */
835 ret = SR_OK;
836 for (i = 0; i < scope_models[model_index].digital_pods; i++) {
837 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
838 if (!devc->digital_groups[i]) {
839 ret = SR_ERR_MALLOC;
840 break;
841 }
842 devc->digital_groups[i]->name = g_strdup_printf("POD%d", i + 1);
843 sdi->channel_groups = g_slist_append(sdi->channel_groups,
844 devc->digital_groups[i]);
845 }
846 if (ret != SR_OK)
847 return ret;
848
849 /* Add digital channels. */
850 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
851 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
852 (*scope_models[model_index].digital_names)[i]);
853
854 group = i / 8;
855 devc->digital_groups[group]->channels = g_slist_append(
856 devc->digital_groups[group]->channels, ch);
857 }
858
859 devc->model_config = &scope_models[model_index];
860 devc->samples_limit = 0;
861 devc->frame_limit = 0;
862
863 if (!(devc->model_state = scope_state_new(devc->model_config)))
864 return SR_ERR_MALLOC;
865
866 return SR_OK;
867}
868
869/* Queue data of one channel group, for later submission. */
870SR_PRIV void hmo_queue_logic_data(struct dev_context *devc,
871 size_t group, GByteArray *pod_data)
872{
873 size_t size;
874 GByteArray *store;
875 uint8_t *logic_data;
876 size_t idx, logic_step;
877
878 /*
879 * Upon first invocation, allocate the array which can hold the
880 * combined logic data for all channels. Assume that each channel
881 * will yield an identical number of samples per receive call.
882 *
883 * As a poor man's safety measure: (Silently) skip processing
884 * for unexpected sample counts, and ignore samples for
885 * unexpected channel groups. Don't bother with complicated
886 * resize logic, considering that many models only support one
887 * pod, and the most capable supported models have two pods of
888 * identical size. We haven't yet seen any "odd" configuration.
889 */
890 if (!devc->logic_data) {
891 size = pod_data->len * devc->pod_count;
892 store = g_byte_array_sized_new(size);
893 memset(store->data, 0, size);
894 store = g_byte_array_set_size(store, size);
895 devc->logic_data = store;
896 } else {
897 store = devc->logic_data;
898 size = store->len / devc->pod_count;
899 if (group >= devc->pod_count)
900 return;
901 }
902
903 /*
904 * Fold the data of the most recently received channel group into
905 * the storage, where data resides for all channels combined.
906 */
907 logic_data = store->data;
908 logic_data += group;
909 logic_step = devc->pod_count;
910 for (idx = 0; idx < pod_data->len; idx++) {
911 *logic_data = pod_data->data[idx];
912 logic_data += logic_step;
913 }
914
915 /* Truncate acquisition if a smaller number of samples has been requested. */
916 if (devc->samples_limit > 0 && devc->logic_data->len > devc->samples_limit * devc->pod_count)
917 devc->logic_data->len = devc->samples_limit * devc->pod_count;
918}
919
920/* Submit data for all channels, after the individual groups got collected. */
921SR_PRIV void hmo_send_logic_packet(struct sr_dev_inst *sdi,
922 struct dev_context *devc)
923{
924 struct sr_datafeed_packet packet;
925 struct sr_datafeed_logic logic;
926
927 if (!devc->logic_data)
928 return;
929
930 logic.data = devc->logic_data->data;
931 logic.length = devc->logic_data->len;
932 logic.unitsize = devc->pod_count;
933
934 packet.type = SR_DF_LOGIC;
935 packet.payload = &logic;
936
937 sr_session_send(sdi, &packet);
938}
939
940/* Undo previous resource allocation. */
941SR_PRIV void hmo_cleanup_logic_data(struct dev_context *devc)
942{
943
944 if (devc->logic_data) {
945 g_byte_array_free(devc->logic_data, TRUE);
946 devc->logic_data = NULL;
947 }
948 /*
949 * Keep 'pod_count'! It's required when more frames will be
950 * received, and does not harm when kept after acquisition.
951 */
952}
953
954SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
955{
956 struct sr_channel *ch;
957 struct sr_dev_inst *sdi;
958 struct dev_context *devc;
959 struct scope_state *state;
960 struct sr_datafeed_packet packet;
961 GByteArray *data;
962 struct sr_datafeed_analog analog;
963 struct sr_analog_encoding encoding;
964 struct sr_analog_meaning meaning;
965 struct sr_analog_spec spec;
966 struct sr_datafeed_logic logic;
967 size_t group;
968
969 (void)fd;
970 (void)revents;
971
972 data = NULL;
973
974 if (!(sdi = cb_data))
975 return TRUE;
976
977 if (!(devc = sdi->priv))
978 return TRUE;
979
980 /* Although this is correct in general, the USBTMC libusb implementation
981 * currently does not generate an event prior to the first read. Often
982 * it is ok to start reading just after the 50ms timeout. See bug #785.
983 if (revents != G_IO_IN)
984 return TRUE;
985 */
986
987 ch = devc->current_channel->data;
988 state = devc->model_state;
989
990 /*
991 * Send "frame begin" packet upon reception of data for the
992 * first enabled channel.
993 */
994 if (devc->current_channel == devc->enabled_channels) {
995 packet.type = SR_DF_FRAME_BEGIN;
996 sr_session_send(sdi, &packet);
997 }
998
999 /*
1000 * Pass on the received data of the channel(s).
1001 */
1002 switch (ch->type) {
1003 case SR_CHANNEL_ANALOG:
1004 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
1005 if (data)
1006 g_byte_array_free(data, TRUE);
1007 return TRUE;
1008 }
1009
1010 packet.type = SR_DF_ANALOG;
1011
1012 analog.data = data->data;
1013 analog.num_samples = data->len / sizeof(float);
1014 /* Truncate acquisition if a smaller number of samples has been requested. */
1015 if (devc->samples_limit > 0 && analog.num_samples > devc->samples_limit)
1016 analog.num_samples = devc->samples_limit;
1017 analog.encoding = &encoding;
1018 analog.meaning = &meaning;
1019 analog.spec = &spec;
1020
1021 encoding.unitsize = sizeof(float);
1022 encoding.is_signed = TRUE;
1023 encoding.is_float = TRUE;
1024#ifdef WORDS_BIGENDIAN
1025 encoding.is_bigendian = TRUE;
1026#else
1027 encoding.is_bigendian = FALSE;
1028#endif
1029 /* TODO: Use proper 'digits' value for this device (and its modes). */
1030 encoding.digits = 2;
1031 encoding.is_digits_decimal = FALSE;
1032 encoding.scale.p = 1;
1033 encoding.scale.q = 1;
1034 encoding.offset.p = 0;
1035 encoding.offset.q = 1;
1036 if (state->analog_channels[ch->index].probe_unit == 'V') {
1037 meaning.mq = SR_MQ_VOLTAGE;
1038 meaning.unit = SR_UNIT_VOLT;
1039 } else {
1040 meaning.mq = SR_MQ_CURRENT;
1041 meaning.unit = SR_UNIT_AMPERE;
1042 }
1043 meaning.mqflags = 0;
1044 meaning.channels = g_slist_append(NULL, ch);
1045 /* TODO: Use proper 'digits' value for this device (and its modes). */
1046 spec.spec_digits = 2;
1047 packet.payload = &analog;
1048 sr_session_send(sdi, &packet);
1049 devc->num_samples = data->len / sizeof(float);
1050 g_slist_free(meaning.channels);
1051 g_byte_array_free(data, TRUE);
1052 data = NULL;
1053 break;
1054 case SR_CHANNEL_LOGIC:
1055 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
1056 if (data)
1057 g_byte_array_free(data, TRUE);
1058 return TRUE;
1059 }
1060
1061 /*
1062 * If only data from the first pod is involved in the
1063 * acquisition, then the raw input bytes can get passed
1064 * forward for performance reasons. When the second pod
1065 * is involved (either alone, or in combination with the
1066 * first pod), then the received bytes need to be put
1067 * into memory in such a layout that all channel groups
1068 * get combined, and a unitsize larger than a single byte
1069 * applies. The "queue" logic transparently copes with
1070 * any such configuration. This works around the lack
1071 * of support for "meaning" to logic data, which is used
1072 * above for analog data.
1073 */
1074 if (devc->pod_count == 1) {
1075 packet.type = SR_DF_LOGIC;
1076 logic.data = data->data;
1077 logic.length = data->len;
1078 /* Truncate acquisition if a smaller number of samples has been requested. */
1079 if (devc->samples_limit > 0 && logic.length > devc->samples_limit)
1080 logic.length = devc->samples_limit;
1081 logic.unitsize = 1;
1082 packet.payload = &logic;
1083 sr_session_send(sdi, &packet);
1084 } else {
1085 group = ch->index / 8;
1086 hmo_queue_logic_data(devc, group, data);
1087 }
1088
1089 devc->num_samples = data->len / devc->pod_count;
1090 g_byte_array_free(data, TRUE);
1091 data = NULL;
1092 break;
1093 default:
1094 sr_err("Invalid channel type.");
1095 break;
1096 }
1097
1098 /*
1099 * Advance to the next enabled channel. When data for all enabled
1100 * channels was received, then flush potentially queued logic data,
1101 * and send the "frame end" packet.
1102 */
1103 if (devc->current_channel->next) {
1104 devc->current_channel = devc->current_channel->next;
1105 hmo_request_data(sdi);
1106 return TRUE;
1107 }
1108 hmo_send_logic_packet(sdi, devc);
1109
1110 /*
1111 * Release the logic data storage after each frame. This copes
1112 * with sample counts that differ in length per frame. -- Is
1113 * this a real constraint when acquiring multiple frames with
1114 * identical device settings?
1115 */
1116 hmo_cleanup_logic_data(devc);
1117
1118 packet.type = SR_DF_FRAME_END;
1119 sr_session_send(sdi, &packet);
1120
1121 /*
1122 * End of frame was reached. Stop acquisition after the specified
1123 * number of frames or after the specified number of samples, or
1124 * continue reception by starting over at the first enabled channel.
1125 */
1126 if (++devc->num_frames >= devc->frame_limit || devc->num_samples >= devc->samples_limit) {
1127 sr_dev_acquisition_stop(sdi);
1128 hmo_cleanup_logic_data(devc);
1129 } else {
1130 devc->current_channel = devc->enabled_channels;
1131 hmo_request_data(sdi);
1132 }
1133
1134 return TRUE;
1135}