2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
5 * Copyright (C) 2018 Guido Trentalancia <guido@trentalancia.com>
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.
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.
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/>.
27 SR_PRIV void hmo_queue_logic_data(struct dev_context *devc,
28 size_t group, GByteArray *pod_data);
29 SR_PRIV void hmo_send_logic_packet(struct sr_dev_inst *sdi,
30 struct dev_context *devc);
31 SR_PRIV void hmo_cleanup_logic_data(struct dev_context *devc);
33 static 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",
70 static const uint32_t devopts[] = {
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,
83 static 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,
89 static 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,
94 static 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
102 static const char *scope_trigger_slopes[] = {
108 /* Predefined logic thresholds. */
109 static const char *logic_threshold[] = {
114 "USER2", // overwritten by logic_threshold_custom, use USER1 for permanent setting
117 /* RTC1002, HMO Compact2 and HMO1002/HMO1202 */
118 static const char *an2_dig8_trigger_sources[] = {
120 "LINE", "EXT", "PATT", "BUS1", "BUS2",
121 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
125 static const char *an2_dig16_trigger_sources[] = {
127 "LINE", "EXT", "PATT", "BUS1", "BUS2",
128 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
129 "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
133 static 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",
139 /* HMO3xx4 and HMO2524 */
140 static 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",
147 static const uint64_t timebases[][2] = {
186 static const uint64_t vdivs[][2] = {
204 static const char *scope_analog_channel_names[] = {
205 "CH1", "CH2", "CH3", "CH4",
208 static const char *scope_digital_channel_names[] = {
209 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
210 "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
213 static const struct scope_config scope_models[] = {
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,
221 .analog_names = &scope_analog_channel_names,
222 .digital_names = &scope_digital_channel_names,
225 .num_devopts = ARRAY_SIZE(devopts),
227 .devopts_cg_analog = &devopts_cg_analog,
228 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
230 .devopts_cg_digital = &devopts_cg_digital,
231 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
233 .coupling_options = &coupling_options,
234 .num_coupling_options = ARRAY_SIZE(coupling_options),
236 .logic_threshold = &logic_threshold,
237 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
239 .trigger_sources = &an2_dig8_trigger_sources,
240 .num_trigger_sources = ARRAY_SIZE(an2_dig8_trigger_sources),
242 .trigger_slopes = &scope_trigger_slopes,
243 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
245 .timebases = &timebases,
246 .num_timebases = ARRAY_SIZE(timebases),
249 .num_vdivs = ARRAY_SIZE(vdivs),
254 .scpi_dialect = &hameg_scpi_dialect,
257 /* HMO3032/3042/3052/3522 support 16 digital channels. */
258 .name = {"HMO3032", "HMO3042", "HMO3052", "HMO3522", NULL},
259 .analog_channels = 2,
260 .digital_channels = 16,
263 .analog_names = &scope_analog_channel_names,
264 .digital_names = &scope_digital_channel_names,
267 .num_devopts = ARRAY_SIZE(devopts),
269 .devopts_cg_analog = &devopts_cg_analog,
270 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
272 .devopts_cg_digital = &devopts_cg_digital,
273 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
275 .coupling_options = &coupling_options,
276 .num_coupling_options = ARRAY_SIZE(coupling_options),
278 .logic_threshold = &logic_threshold,
279 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
281 .trigger_sources = &an2_dig16_trigger_sources,
282 .num_trigger_sources = ARRAY_SIZE(an2_dig16_trigger_sources),
284 .trigger_slopes = &scope_trigger_slopes,
285 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
287 .timebases = &timebases,
288 .num_timebases = ARRAY_SIZE(timebases),
291 .num_vdivs = ARRAY_SIZE(vdivs),
296 .scpi_dialect = &hameg_scpi_dialect,
299 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", NULL},
300 .analog_channels = 4,
301 .digital_channels = 8,
304 .analog_names = &scope_analog_channel_names,
305 .digital_names = &scope_digital_channel_names,
308 .num_devopts = ARRAY_SIZE(devopts),
310 .devopts_cg_analog = &devopts_cg_analog,
311 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
313 .devopts_cg_digital = &devopts_cg_digital,
314 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
316 .coupling_options = &coupling_options,
317 .num_coupling_options = ARRAY_SIZE(coupling_options),
319 .logic_threshold = &logic_threshold,
320 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
322 .trigger_sources = &an4_dig8_trigger_sources,
323 .num_trigger_sources = ARRAY_SIZE(an4_dig8_trigger_sources),
325 .trigger_slopes = &scope_trigger_slopes,
326 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
328 .timebases = &timebases,
329 .num_timebases = ARRAY_SIZE(timebases),
332 .num_vdivs = ARRAY_SIZE(vdivs),
337 .scpi_dialect = &hameg_scpi_dialect,
340 .name = {"HMO2524", "HMO3034", "HMO3044", "HMO3054", "HMO3524", NULL},
341 .analog_channels = 4,
342 .digital_channels = 16,
345 .analog_names = &scope_analog_channel_names,
346 .digital_names = &scope_digital_channel_names,
349 .num_devopts = ARRAY_SIZE(devopts),
351 .devopts_cg_analog = &devopts_cg_analog,
352 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
354 .devopts_cg_digital = &devopts_cg_digital,
355 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
357 .coupling_options = &coupling_options,
358 .num_coupling_options = ARRAY_SIZE(coupling_options),
360 .logic_threshold = &logic_threshold,
361 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
363 .trigger_sources = &an4_dig16_trigger_sources,
364 .num_trigger_sources = ARRAY_SIZE(an4_dig16_trigger_sources),
366 .trigger_slopes = &scope_trigger_slopes,
367 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
369 .timebases = &timebases,
370 .num_timebases = ARRAY_SIZE(timebases),
373 .num_vdivs = ARRAY_SIZE(vdivs),
378 .scpi_dialect = &hameg_scpi_dialect,
382 static void scope_state_dump(const struct scope_config *config,
383 struct scope_state *state)
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);
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");
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);
413 tmp = sr_period_string((*config->timebases)[state->timebase][0],
414 (*config->timebases)[state->timebase][1]);
415 sr_info("Current timebase: %s", tmp);
418 tmp = sr_samplerate_string(state->sample_rate);
419 sr_info("Current samplerate: %s", tmp);
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);
433 static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
434 const char *command, const char *(*array)[], unsigned int n, int *result)
439 if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK)
442 if ((idx = std_str_idx_s(tmp, *array, n)) < 0) {
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.
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.
463 * @return SR_ERR on any parsing error, SR_OK otherwise.
465 static int array_float_get(gchar *value, const uint64_t array[][2],
466 int array_len, unsigned int *result)
468 struct sr_rational rval;
469 struct sr_rational aval;
471 if (sr_parse_rational(value, &rval) != SR_OK)
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)) {
485 static struct sr_channel *get_channel_by_index_and_type(GSList *channel_lhead,
488 while (channel_lhead) {
489 struct sr_channel *ch = channel_lhead->data;
490 if (ch->index == index && ch->type == type)
493 channel_lhead = channel_lhead->next;
499 static int analog_channel_state_get(struct sr_dev_inst *sdi,
500 const struct scope_config *config,
501 struct scope_state *state)
504 char command[MAX_COMMAND_SIZE];
506 struct sr_channel *ch;
507 struct sr_scpi_dev_inst *scpi = sdi->conn;
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],
514 if (sr_scpi_get_bool(scpi, command,
515 &state->analog_channels[i].state) != SR_OK)
518 ch = get_channel_by_index_and_type(sdi->channels, i, SR_CHANNEL_ANALOG);
520 ch->enabled = state->analog_channels[i].state;
522 g_snprintf(command, sizeof(command),
523 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
526 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
529 if (array_float_get(tmp_str, ARRAY_AND_SIZE(vdivs), &j) != SR_OK) {
531 sr_err("Could not determine array index for vertical div scale.");
536 state->analog_channels[i].vdiv = j;
538 g_snprintf(command, sizeof(command),
539 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
542 if (sr_scpi_get_float(scpi, command,
543 &state->analog_channels[i].vertical_offset) != SR_OK)
546 g_snprintf(command, sizeof(command),
547 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
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)
555 g_snprintf(command, sizeof(command),
556 (*config->scpi_dialect)[SCPI_CMD_GET_PROBE_UNIT],
559 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
562 if (tmp_str[0] == 'A')
563 state->analog_channels[i].probe_unit = 'A';
565 state->analog_channels[i].probe_unit = 'V';
572 static int digital_channel_state_get(struct sr_dev_inst *sdi,
573 const struct scope_config *config,
574 struct scope_state *state)
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;
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],
588 if (sr_scpi_get_bool(scpi, command,
589 &state->digital_channels[i]) != SR_OK)
592 ch = get_channel_by_index_and_type(sdi->channels, i, SR_CHANNEL_LOGIC);
594 ch->enabled = state->digital_channels[i];
597 /* According to the SCPI standard, the response to the command
598 * SCPI_CMD_GET_DIG_POD_THRESHOLD might return "USER" instead of
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...
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]));
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],
617 if (sr_scpi_get_bool(scpi, command,
618 &state->digital_pods[i].state) != SR_OK)
621 g_snprintf(command, sizeof(command),
622 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_THRESHOLD],
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)
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
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
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)
654 for (i = 0; i < config->num_logic_threshold; i++)
655 g_free(logic_threshold_short[i]);
660 SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
662 struct dev_context *devc;
663 struct scope_state *state;
664 const struct scope_config *config;
668 config = devc->model_config;
669 state = devc->model_state;
671 if (sr_scpi_get_float(sdi->conn,
672 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE],
673 &tmp_float) != SR_OK)
676 state->sample_rate = tmp_float;
681 SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
683 struct dev_context *devc;
684 struct scope_state *state;
685 const struct scope_config *config;
691 config = devc->model_config;
692 state = devc->model_state;
694 sr_info("Fetching scope state");
696 if (analog_channel_state_get(sdi, config, state) != SR_OK)
699 if (digital_channel_state_get(sdi, config, state) != SR_OK)
702 if (sr_scpi_get_float(sdi->conn,
703 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
704 &tmp_float) != SR_OK)
707 if (sr_scpi_get_string(sdi->conn,
708 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
712 if (array_float_get(tmp_str, ARRAY_AND_SIZE(timebases), &i) != SR_OK) {
714 sr_err("Could not determine array index for time base.");
721 if (sr_scpi_get_float(sdi->conn,
722 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
723 &tmp_float) != SR_OK)
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;
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)
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)
743 if (sr_scpi_get_string(sdi->conn,
744 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_PATTERN],
745 &state->trigger_pattern) != SR_OK)
748 if (hmo_update_sample_rate(sdi) != SR_OK)
751 sr_info("Fetching finished.");
753 scope_state_dump(config, state);
758 static struct scope_state *scope_state_new(const struct scope_config *config)
760 struct scope_state *state;
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));
773 SR_PRIV void hmo_scope_state_free(struct scope_state *state)
775 g_free(state->analog_channels);
776 g_free(state->digital_channels);
777 g_free(state->digital_pods);
781 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
784 unsigned int i, j, group;
785 struct sr_channel *ch;
786 struct dev_context *devc;
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])) {
800 if (model_index != -1)
804 if (model_index == -1) {
805 sr_dbg("Unsupported device.");
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;
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]);
824 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
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);
830 sdi->channel_groups = g_slist_append(sdi->channel_groups,
831 devc->analog_groups[i]);
834 /* Add digital channel groups. */
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]) {
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]);
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]);
855 devc->digital_groups[group]->channels = g_slist_append(
856 devc->digital_groups[group]->channels, ch);
859 devc->model_config = &scope_models[model_index];
860 devc->samples_limit = 0;
861 devc->frame_limit = 0;
863 if (!(devc->model_state = scope_state_new(devc->model_config)))
864 return SR_ERR_MALLOC;
869 /* Queue data of one channel group, for later submission. */
870 SR_PRIV void hmo_queue_logic_data(struct dev_context *devc,
871 size_t group, GByteArray *pod_data)
876 size_t idx, logic_step;
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.
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.
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;
897 store = devc->logic_data;
898 size = store->len / devc->pod_count;
899 if (group >= devc->pod_count)
904 * Fold the data of the most recently received channel group into
905 * the storage, where data resides for all channels combined.
907 logic_data = store->data;
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;
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;
920 /* Submit data for all channels, after the individual groups got collected. */
921 SR_PRIV void hmo_send_logic_packet(struct sr_dev_inst *sdi,
922 struct dev_context *devc)
924 struct sr_datafeed_packet packet;
925 struct sr_datafeed_logic logic;
927 if (!devc->logic_data)
930 logic.data = devc->logic_data->data;
931 logic.length = devc->logic_data->len;
932 logic.unitsize = devc->pod_count;
934 packet.type = SR_DF_LOGIC;
935 packet.payload = &logic;
937 sr_session_send(sdi, &packet);
940 /* Undo previous resource allocation. */
941 SR_PRIV void hmo_cleanup_logic_data(struct dev_context *devc)
944 if (devc->logic_data) {
945 g_byte_array_free(devc->logic_data, TRUE);
946 devc->logic_data = NULL;
949 * Keep 'pod_count'! It's required when more frames will be
950 * received, and does not harm when kept after acquisition.
954 SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
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;
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;
974 if (!(sdi = cb_data))
977 if (!(devc = sdi->priv))
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)
987 ch = devc->current_channel->data;
988 state = devc->model_state;
991 * Send "frame begin" packet upon reception of data for the
992 * first enabled channel.
994 if (devc->current_channel == devc->enabled_channels) {
995 packet.type = SR_DF_FRAME_BEGIN;
996 sr_session_send(sdi, &packet);
1000 * Pass on the received data of the channel(s).
1003 case SR_CHANNEL_ANALOG:
1004 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
1006 g_byte_array_free(data, TRUE);
1010 packet.type = SR_DF_ANALOG;
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;
1021 encoding.unitsize = sizeof(float);
1022 encoding.is_signed = TRUE;
1023 encoding.is_float = TRUE;
1024 #ifdef WORDS_BIGENDIAN
1025 encoding.is_bigendian = TRUE;
1027 encoding.is_bigendian = FALSE;
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;
1040 meaning.mq = SR_MQ_CURRENT;
1041 meaning.unit = SR_UNIT_AMPERE;
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);
1054 case SR_CHANNEL_LOGIC:
1055 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
1057 g_byte_array_free(data, TRUE);
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.
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;
1082 packet.payload = &logic;
1083 sr_session_send(sdi, &packet);
1085 group = ch->index / 8;
1086 hmo_queue_logic_data(devc, group, data);
1089 devc->num_samples = data->len / devc->pod_count;
1090 g_byte_array_free(data, TRUE);
1094 sr_err("Invalid channel type.");
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.
1103 if (devc->current_channel->next) {
1104 devc->current_channel = devc->current_channel->next;
1105 hmo_request_data(sdi);
1108 hmo_send_logic_packet(sdi, devc);
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?
1116 hmo_cleanup_logic_data(devc);
1118 packet.type = SR_DF_FRAME_END;
1119 sr_session_send(sdi, &packet);
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.
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);
1130 devc->current_channel = devc->enabled_channels;
1131 hmo_request_data(sdi);