2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
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.
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.
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/>.
26 SR_PRIV void hmo_queue_logic_data(struct dev_context *devc,
27 size_t group, GByteArray *pod_data);
28 SR_PRIV void hmo_send_logic_packet(struct sr_dev_inst *sdi,
29 struct dev_context *devc);
30 SR_PRIV void hmo_cleanup_logic_data(struct dev_context *devc);
32 static const char *hameg_scpi_dialect[] = {
33 [SCPI_CMD_GET_DIG_DATA] = ":FORM UINT,8;:POD%d:DATA?",
34 [SCPI_CMD_GET_TIMEBASE] = ":TIM:SCAL?",
35 [SCPI_CMD_SET_TIMEBASE] = ":TIM:SCAL %s",
36 [SCPI_CMD_GET_COUPLING] = ":CHAN%d:COUP?",
37 [SCPI_CMD_SET_COUPLING] = ":CHAN%d:COUP %s",
38 [SCPI_CMD_GET_SAMPLE_RATE] = ":ACQ:SRAT?",
39 [SCPI_CMD_GET_SAMPLE_RATE_LIVE] = ":%s:DATA:POINTS?",
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:EDGE:SLOP %s",
48 [SCPI_CMD_GET_TRIGGER_SOURCE] = ":TRIG:A:SOUR?",
49 [SCPI_CMD_SET_TRIGGER_SOURCE] = ":TRIG:A:SOUR %s",
50 [SCPI_CMD_GET_DIG_CHAN_STATE] = ":LOG%d:STAT?",
51 [SCPI_CMD_SET_DIG_CHAN_STATE] = ":LOG%d:STAT %d",
52 [SCPI_CMD_GET_VERTICAL_OFFSET] = ":CHAN%d:POS?",
53 [SCPI_CMD_GET_HORIZ_TRIGGERPOS] = ":TIM:POS?",
54 [SCPI_CMD_SET_HORIZ_TRIGGERPOS] = ":TIM:POS %s",
55 [SCPI_CMD_GET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT?",
56 [SCPI_CMD_SET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT %d",
57 [SCPI_CMD_GET_PROBE_UNIT] = ":PROB%d:SET:ATT:UNIT?",
60 static const uint32_t devopts[] = {
62 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
63 SR_CONF_SAMPLERATE | SR_CONF_GET,
64 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
65 SR_CONF_NUM_HDIV | SR_CONF_GET,
66 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
67 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
68 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
71 static const uint32_t devopts_cg_analog[] = {
72 SR_CONF_NUM_VDIV | SR_CONF_GET,
73 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
74 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
77 static const char *coupling_options[] = {
78 "AC", // AC with 50 Ohm termination (152x, 202x, 30xx, 1202)
79 "ACL", // AC with 1 MOhm termination
80 "DC", // DC with 50 Ohm termination
81 "DCL", // DC with 1 MOhm termination
85 static const char *scope_trigger_slopes[] = {
91 static const char *compact2_trigger_sources[] = {
93 "LINE", "EXT", "PATT", "BUS1", "BUS2",
94 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
97 static const char *compact4_trigger_sources[] = {
98 "CH1", "CH2", "CH3", "CH4",
99 "LINE", "EXT", "PATT", "BUS1", "BUS2",
100 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
103 static const char *compact4_dig16_trigger_sources[] = {
104 "CH1", "CH2", "CH3", "CH4",
105 "LINE", "EXT", "PATT", "BUS1", "BUS2",
106 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
107 "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
110 static const uint64_t timebases[][2] = {
149 static const uint64_t vdivs[][2] = {
169 static const char *scope_analog_channel_names[] = {
170 "CH1", "CH2", "CH3", "CH4",
173 static const char *scope_digital_channel_names[] = {
174 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
175 "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
178 static const struct scope_config scope_models[] = {
180 /* HMO2522/3032/3042/3052 support 16 digital channels but they're not supported yet. */
181 .name = {"HMO1002", "HMO722", "HMO1022", "HMO1522", "HMO2022", "HMO2522",
182 "HMO3032", "HMO3042", "HMO3052", NULL},
183 .analog_channels = 2,
184 .digital_channels = 8,
187 .analog_names = &scope_analog_channel_names,
188 .digital_names = &scope_digital_channel_names,
191 .num_devopts = ARRAY_SIZE(devopts),
193 .devopts_cg_analog = &devopts_cg_analog,
194 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
196 .coupling_options = &coupling_options,
197 .num_coupling_options = ARRAY_SIZE(coupling_options),
199 .trigger_sources = &compact2_trigger_sources,
200 .num_trigger_sources = ARRAY_SIZE(compact2_trigger_sources),
202 .trigger_slopes = &scope_trigger_slopes,
203 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
205 .timebases = &timebases,
206 .num_timebases = ARRAY_SIZE(timebases),
209 .num_vdivs = ARRAY_SIZE(vdivs),
214 .scpi_dialect = &hameg_scpi_dialect,
217 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", NULL},
218 .analog_channels = 4,
219 .digital_channels = 8,
222 .analog_names = &scope_analog_channel_names,
223 .digital_names = &scope_digital_channel_names,
226 .num_devopts = ARRAY_SIZE(devopts),
228 .devopts_cg_analog = &devopts_cg_analog,
229 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
231 .coupling_options = &coupling_options,
232 .num_coupling_options = ARRAY_SIZE(coupling_options),
234 .trigger_sources = &compact4_trigger_sources,
235 .num_trigger_sources = ARRAY_SIZE(compact4_trigger_sources),
237 .trigger_slopes = &scope_trigger_slopes,
238 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
240 .timebases = &timebases,
241 .num_timebases = ARRAY_SIZE(timebases),
244 .num_vdivs = ARRAY_SIZE(vdivs),
249 .scpi_dialect = &hameg_scpi_dialect,
252 .name = {"HMO2524", "HMO3034", "HMO3044", "HMO3054", "HMO3524", NULL},
253 .analog_channels = 4,
254 .digital_channels = 16,
257 .analog_names = &scope_analog_channel_names,
258 .digital_names = &scope_digital_channel_names,
261 .num_devopts = ARRAY_SIZE(devopts),
263 .devopts_cg_analog = &devopts_cg_analog,
264 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
266 .coupling_options = &coupling_options,
267 .num_coupling_options = ARRAY_SIZE(coupling_options),
269 .trigger_sources = &compact4_dig16_trigger_sources,
270 .num_trigger_sources = ARRAY_SIZE(compact4_dig16_trigger_sources),
272 .trigger_slopes = &scope_trigger_slopes,
273 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
275 .timebases = &timebases,
276 .num_timebases = ARRAY_SIZE(timebases),
279 .num_vdivs = ARRAY_SIZE(vdivs),
284 .scpi_dialect = &hameg_scpi_dialect,
288 static void scope_state_dump(const struct scope_config *config,
289 struct scope_state *state)
294 for (i = 0; i < config->analog_channels; i++) {
295 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
296 (*config->vdivs)[state->analog_channels[i].vdiv][1]);
297 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
298 i + 1, state->analog_channels[i].state ? "On" : "Off",
299 (*config->coupling_options)[state->analog_channels[i].coupling],
300 tmp, state->analog_channels[i].vertical_offset);
303 for (i = 0; i < config->digital_channels; i++) {
304 sr_info("State of digital channel %d -> %s", i,
305 state->digital_channels[i] ? "On" : "Off");
308 for (i = 0; i < config->digital_pods; i++) {
309 sr_info("State of digital POD %d -> %s", i,
310 state->digital_pods[i] ? "On" : "Off");
313 tmp = sr_period_string((*config->timebases)[state->timebase][0],
314 (*config->timebases)[state->timebase][1]);
315 sr_info("Current timebase: %s", tmp);
318 tmp = sr_samplerate_string(state->sample_rate);
319 sr_info("Current samplerate: %s", tmp);
322 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
323 (*config->trigger_sources)[state->trigger_source],
324 (*config->trigger_slopes)[state->trigger_slope],
325 state->horiz_triggerpos);
328 static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
329 const char *command, const char *(*array)[], unsigned int n, int *result)
334 if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK) {
339 if ((idx = std_str_idx_s(tmp, *array, n)) < 0) {
352 * This function takes a value of the form "2.000E-03" and returns the index
353 * of an array where a matching pair was found.
355 * @param value The string to be parsed.
356 * @param array The array of s/f pairs.
357 * @param array_len The number of pairs in the array.
358 * @param result The index at which a matching pair was found.
360 * @return SR_ERR on any parsing error, SR_OK otherwise.
362 static int array_float_get(gchar *value, const uint64_t array[][2],
363 int array_len, unsigned int *result)
365 struct sr_rational rval;
366 struct sr_rational aval;
368 if (sr_parse_rational(value, &rval) != SR_OK)
371 for (int i = 0; i < array_len; i++) {
372 sr_rational_set(&aval, array[i][0], array[i][1]);
373 if (sr_rational_eq(&rval, &aval)) {
382 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
383 const struct scope_config *config,
384 struct scope_state *state)
387 char command[MAX_COMMAND_SIZE];
390 for (i = 0; i < config->analog_channels; i++) {
391 g_snprintf(command, sizeof(command),
392 (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
395 if (sr_scpi_get_bool(scpi, command,
396 &state->analog_channels[i].state) != SR_OK)
399 g_snprintf(command, sizeof(command),
400 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
403 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
406 if (array_float_get(tmp_str, ARRAY_AND_SIZE(vdivs), &j) != SR_OK) {
408 sr_err("Could not determine array index for vertical div scale.");
413 state->analog_channels[i].vdiv = j;
415 g_snprintf(command, sizeof(command),
416 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
419 if (sr_scpi_get_float(scpi, command,
420 &state->analog_channels[i].vertical_offset) != SR_OK)
423 g_snprintf(command, sizeof(command),
424 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
427 if (scope_state_get_array_option(scpi, command, config->coupling_options,
428 config->num_coupling_options,
429 &state->analog_channels[i].coupling) != SR_OK)
432 g_snprintf(command, sizeof(command),
433 (*config->scpi_dialect)[SCPI_CMD_GET_PROBE_UNIT],
436 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
439 if (tmp_str[0] == 'A')
440 state->analog_channels[i].probe_unit = 'A';
442 state->analog_channels[i].probe_unit = 'V';
449 static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
450 const struct scope_config *config,
451 struct scope_state *state)
454 char command[MAX_COMMAND_SIZE];
456 for (i = 0; i < config->digital_channels; i++) {
457 g_snprintf(command, sizeof(command),
458 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
461 if (sr_scpi_get_bool(scpi, command,
462 &state->digital_channels[i]) != SR_OK)
466 for (i = 0; i < config->digital_pods; i++) {
467 g_snprintf(command, sizeof(command),
468 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
471 if (sr_scpi_get_bool(scpi, command,
472 &state->digital_pods[i]) != SR_OK)
479 SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
481 struct dev_context *devc;
482 struct scope_state *state;
483 const struct scope_config *config;
487 gboolean channel_found;
488 char tmp_str[MAX_COMMAND_SIZE];
492 config = devc->model_config;
493 state = devc->model_state;
494 channel_found = FALSE;
496 for (i = 0; i < config->analog_channels; i++) {
497 if (!state->analog_channels[i].state)
499 g_snprintf(chan_name, sizeof(chan_name), "CHAN%d", i + 1);
500 g_snprintf(tmp_str, sizeof(tmp_str),
501 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
503 channel_found = TRUE;
507 if (!channel_found) {
508 for (i = 0; i < config->digital_pods; i++) {
509 if (!state->digital_pods[i])
511 g_snprintf(chan_name, sizeof(chan_name), "POD%d", i);
512 g_snprintf(tmp_str, sizeof(tmp_str),
513 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
515 channel_found = TRUE;
520 /* No channel is active, ask the instrument for the sample rate
521 * in single shot mode */
522 if (!channel_found) {
523 if (sr_scpi_get_float(sdi->conn,
524 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE],
525 &tmp_float) != SR_OK)
528 state->sample_rate = tmp_float;
530 if (sr_scpi_get_int(sdi->conn, tmp_str, &tmp) != SR_OK)
532 state->sample_rate = tmp / (((float) (*config->timebases)[state->timebase][0] /
533 (*config->timebases)[state->timebase][1]) *
540 SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
542 struct dev_context *devc;
543 struct scope_state *state;
544 const struct scope_config *config;
550 config = devc->model_config;
551 state = devc->model_state;
553 sr_info("Fetching scope state");
555 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
558 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
561 if (sr_scpi_get_float(sdi->conn,
562 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
563 &tmp_float) != SR_OK)
566 if (sr_scpi_get_string(sdi->conn,
567 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
571 if (array_float_get(tmp_str, ARRAY_AND_SIZE(timebases), &i) != SR_OK) {
573 sr_err("Could not determine array index for time base.");
580 if (sr_scpi_get_float(sdi->conn,
581 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
582 &tmp_float) != SR_OK)
584 state->horiz_triggerpos = tmp_float /
585 (((double) (*config->timebases)[state->timebase][0] /
586 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
587 state->horiz_triggerpos -= 0.5;
588 state->horiz_triggerpos *= -1;
590 if (scope_state_get_array_option(sdi->conn,
591 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
592 config->trigger_sources, config->num_trigger_sources,
593 &state->trigger_source) != SR_OK)
596 if (scope_state_get_array_option(sdi->conn,
597 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
598 config->trigger_slopes, config->num_trigger_slopes,
599 &state->trigger_slope) != SR_OK)
602 if (hmo_update_sample_rate(sdi) != SR_OK)
605 sr_info("Fetching finished.");
607 scope_state_dump(config, state);
612 static struct scope_state *scope_state_new(const struct scope_config *config)
614 struct scope_state *state;
616 state = g_malloc0(sizeof(struct scope_state));
617 state->analog_channels = g_malloc0_n(config->analog_channels,
618 sizeof(struct analog_channel_state));
619 state->digital_channels = g_malloc0_n(
620 config->digital_channels, sizeof(gboolean));
621 state->digital_pods = g_malloc0_n(config->digital_pods,
627 SR_PRIV void hmo_scope_state_free(struct scope_state *state)
629 g_free(state->analog_channels);
630 g_free(state->digital_channels);
631 g_free(state->digital_pods);
635 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
638 unsigned int i, j, group;
639 struct sr_channel *ch;
640 struct dev_context *devc;
645 /* Find the exact model. */
646 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
647 for (j = 0; scope_models[i].name[j]; j++) {
648 if (!strcmp(sdi->model, scope_models[i].name[j])) {
653 if (model_index != -1)
657 if (model_index == -1) {
658 sr_dbg("Unsupported HMO device.");
662 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
663 scope_models[model_index].analog_channels);
665 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
666 scope_models[model_index].digital_pods);
668 /* Add analog channels. */
669 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
670 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
671 (*scope_models[model_index].analog_names)[i]);
673 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
675 devc->analog_groups[i]->name = g_strdup(
676 (char *)(*scope_models[model_index].analog_names)[i]);
677 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
679 sdi->channel_groups = g_slist_append(sdi->channel_groups,
680 devc->analog_groups[i]);
683 /* Add digital channel groups. */
684 for (i = 0; i < scope_models[model_index].digital_pods; i++) {
685 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
686 devc->digital_groups[i]->name = g_strdup_printf("POD%d", i);
687 sdi->channel_groups = g_slist_append(sdi->channel_groups,
688 devc->digital_groups[i]);
691 /* Add digital channels. */
692 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
693 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
694 (*scope_models[model_index].digital_names)[i]);
697 devc->digital_groups[group]->channels = g_slist_append(
698 devc->digital_groups[group]->channels, ch);
701 devc->model_config = &scope_models[model_index];
702 devc->frame_limit = 0;
704 if (!(devc->model_state = scope_state_new(devc->model_config)))
705 return SR_ERR_MALLOC;
710 /* Queue data of one channel group, for later submission. */
711 SR_PRIV void hmo_queue_logic_data(struct dev_context *devc,
712 size_t group, GByteArray *pod_data)
717 size_t idx, logic_step;
720 * Upon first invocation, allocate the array which can hold the
721 * combined logic data for all channels. Assume that each channel
722 * will yield an identical number of samples per receive call.
724 * As a poor man's safety measure: (Silently) skip processing
725 * for unexpected sample counts, and ignore samples for
726 * unexpected channel groups. Don't bother with complicated
727 * resize logic, considering that many models only support one
728 * pod, and the most capable supported models have two pods of
729 * identical size. We haven't yet seen any "odd" configuration.
731 if (!devc->logic_data) {
732 size = pod_data->len * devc->pod_count;
733 store = g_byte_array_sized_new(size);
734 memset(store->data, 0, size);
735 store = g_byte_array_set_size(store, size);
736 devc->logic_data = store;
738 store = devc->logic_data;
739 size = store->len / devc->pod_count;
740 if (size != pod_data->len)
742 if (group >= devc->pod_count)
747 * Fold the data of the most recently received channel group into
748 * the storage, where data resides for all channels combined.
750 logic_data = store->data;
752 logic_step = devc->pod_count;
753 for (idx = 0; idx < pod_data->len; idx++) {
754 *logic_data = pod_data->data[idx];
755 logic_data += logic_step;
759 /* Submit data for all channels, after the individual groups got collected. */
760 SR_PRIV void hmo_send_logic_packet(struct sr_dev_inst *sdi,
761 struct dev_context *devc)
763 struct sr_datafeed_packet packet;
764 struct sr_datafeed_logic logic;
766 if (!devc->logic_data)
769 logic.data = devc->logic_data->data;
770 logic.length = devc->logic_data->len;
771 logic.unitsize = devc->pod_count;
773 packet.type = SR_DF_LOGIC;
774 packet.payload = &logic;
776 sr_session_send(sdi, &packet);
779 /* Undo previous resource allocation. */
780 SR_PRIV void hmo_cleanup_logic_data(struct dev_context *devc)
783 if (devc->logic_data) {
784 g_byte_array_free(devc->logic_data, TRUE);
785 devc->logic_data = NULL;
788 * Keep 'pod_count'! It's required when more frames will be
789 * received, and does not harm when kept after acquisition.
793 SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
795 struct sr_channel *ch;
796 struct sr_dev_inst *sdi;
797 struct dev_context *devc;
798 struct scope_state *state;
799 struct sr_datafeed_packet packet;
801 struct sr_datafeed_analog analog;
802 struct sr_analog_encoding encoding;
803 struct sr_analog_meaning meaning;
804 struct sr_analog_spec spec;
805 struct sr_datafeed_logic logic;
813 if (!(sdi = cb_data))
816 if (!(devc = sdi->priv))
819 /* Although this is correct in general, the USBTMC libusb implementation
820 * currently does not generate an event prior to the first read. Often
821 * it is ok to start reading just after the 50ms timeout. See bug #785.
822 if (revents != G_IO_IN)
826 ch = devc->current_channel->data;
827 state = devc->model_state;
830 * Send "frame begin" packet upon reception of data for the
831 * first enabled channel.
833 if (devc->current_channel == devc->enabled_channels) {
834 packet.type = SR_DF_FRAME_BEGIN;
835 sr_session_send(sdi, &packet);
839 * Pass on the received data of the channel(s).
842 case SR_CHANNEL_ANALOG:
843 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
845 g_byte_array_free(data, TRUE);
850 packet.type = SR_DF_ANALOG;
852 analog.data = data->data;
853 analog.num_samples = data->len / sizeof(float);
854 analog.encoding = &encoding;
855 analog.meaning = &meaning;
858 encoding.unitsize = sizeof(float);
859 encoding.is_signed = TRUE;
860 encoding.is_float = TRUE;
861 #ifdef WORDS_BIGENDIAN
862 encoding.is_bigendian = TRUE;
864 encoding.is_bigendian = FALSE;
866 /* TODO: Use proper 'digits' value for this device (and its modes). */
868 encoding.is_digits_decimal = FALSE;
869 encoding.scale.p = 1;
870 encoding.scale.q = 1;
871 encoding.offset.p = 0;
872 encoding.offset.q = 1;
873 if (state->analog_channels[ch->index].probe_unit == 'V') {
874 meaning.mq = SR_MQ_VOLTAGE;
875 meaning.unit = SR_UNIT_VOLT;
877 meaning.mq = SR_MQ_CURRENT;
878 meaning.unit = SR_UNIT_AMPERE;
881 meaning.channels = g_slist_append(NULL, ch);
882 /* TODO: Use proper 'digits' value for this device (and its modes). */
883 spec.spec_digits = 2;
884 packet.payload = &analog;
885 sr_session_send(sdi, &packet);
886 g_slist_free(meaning.channels);
887 g_byte_array_free(data, TRUE);
890 case SR_CHANNEL_LOGIC:
891 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
897 * If only data from the first pod is involved in the
898 * acquisition, then the raw input bytes can get passed
899 * forward for performance reasons. When the second pod
900 * is involved (either alone, or in combination with the
901 * first pod), then the received bytes need to be put
902 * into memory in such a layout that all channel groups
903 * get combined, and a unitsize larger than a single byte
904 * applies. The "queue" logic transparently copes with
905 * any such configuration. This works around the lack
906 * of support for "meaning" to logic data, which is used
907 * above for analog data.
909 if (devc->pod_count == 1) {
910 packet.type = SR_DF_LOGIC;
911 logic.data = data->data;
912 logic.length = data->len;
914 packet.payload = &logic;
915 sr_session_send(sdi, &packet);
917 group = ch->index / 8;
918 hmo_queue_logic_data(devc, group, data);
921 g_byte_array_free(data, TRUE);
925 sr_err("Invalid channel type.");
930 * Advance to the next enabled channel. When data for all enabled
931 * channels was received, then flush potentially queued logic data,
932 * and send the "frame end" packet.
934 if (devc->current_channel->next) {
935 devc->current_channel = devc->current_channel->next;
936 hmo_request_data(sdi);
939 hmo_send_logic_packet(sdi, devc);
942 * Release the logic data storage after each frame. This copes
943 * with sample counts that differ in length per frame. -- Is
944 * this a real constraint when acquiring multiple frames with
945 * identical device settings?
947 hmo_cleanup_logic_data(devc);
949 packet.type = SR_DF_FRAME_END;
950 sr_session_send(sdi, &packet);
953 * End of frame was reached. Stop acquisition after the specified
954 * number of frames, or continue reception by starting over at
955 * the first enabled channel.
957 if (++devc->num_frames == devc->frame_limit) {
958 sr_dev_acquisition_stop(sdi);
959 hmo_cleanup_logic_data(devc);
961 devc->current_channel = devc->enabled_channels;
962 hmo_request_data(sdi);