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[] = {
92 static const char *an2_dig8_trigger_sources[] = {
94 "LINE", "EXT", "PATT", "BUS1", "BUS2",
95 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
99 static const char *an2_dig16_trigger_sources[] = {
101 "LINE", "EXT", "PATT", "BUS1", "BUS2",
102 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
103 "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
107 static const char *an4_dig8_trigger_sources[] = {
108 "CH1", "CH2", "CH3", "CH4",
109 "LINE", "EXT", "PATT", "BUS1", "BUS2",
110 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
114 static const char *an4_dig16_trigger_sources[] = {
115 "CH1", "CH2", "CH3", "CH4",
116 "LINE", "EXT", "PATT", "BUS1", "BUS2",
117 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
118 "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
121 static const uint64_t timebases[][2] = {
160 static const uint64_t vdivs[][2] = {
180 static const char *scope_analog_channel_names[] = {
181 "CH1", "CH2", "CH3", "CH4",
184 static const char *scope_digital_channel_names[] = {
185 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
186 "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
189 static const struct scope_config scope_models[] = {
191 /* HMO722/1002/1022/1522/2022 support only 8 digital channels. */
192 .name = {"HMO722", "HMO1002", "HMO1022", "HMO1522", "HMO2022", NULL},
193 .analog_channels = 2,
194 .digital_channels = 8,
197 .analog_names = &scope_analog_channel_names,
198 .digital_names = &scope_digital_channel_names,
201 .num_devopts = ARRAY_SIZE(devopts),
203 .devopts_cg_analog = &devopts_cg_analog,
204 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
206 .coupling_options = &coupling_options,
207 .num_coupling_options = ARRAY_SIZE(coupling_options),
209 .trigger_sources = &an2_dig8_trigger_sources,
210 .num_trigger_sources = ARRAY_SIZE(an2_dig8_trigger_sources),
212 .trigger_slopes = &scope_trigger_slopes,
213 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
215 .timebases = &timebases,
216 .num_timebases = ARRAY_SIZE(timebases),
219 .num_vdivs = ARRAY_SIZE(vdivs),
224 .scpi_dialect = &hameg_scpi_dialect,
227 /* HMO3032/3042/3052/3522 support 16 digital channels. */
228 .name = {"HMO3032", "HMO3042", "HMO3052", "HMO3522", NULL},
229 .analog_channels = 2,
230 .digital_channels = 16,
233 .analog_names = &scope_analog_channel_names,
234 .digital_names = &scope_digital_channel_names,
237 .num_devopts = ARRAY_SIZE(devopts),
239 .devopts_cg_analog = &devopts_cg_analog,
240 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
242 .coupling_options = &coupling_options,
243 .num_coupling_options = ARRAY_SIZE(coupling_options),
245 .trigger_sources = &an2_dig16_trigger_sources,
246 .num_trigger_sources = ARRAY_SIZE(an2_dig16_trigger_sources),
248 .trigger_slopes = &scope_trigger_slopes,
249 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
251 .timebases = &timebases,
252 .num_timebases = ARRAY_SIZE(timebases),
255 .num_vdivs = ARRAY_SIZE(vdivs),
260 .scpi_dialect = &hameg_scpi_dialect,
263 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", NULL},
264 .analog_channels = 4,
265 .digital_channels = 8,
268 .analog_names = &scope_analog_channel_names,
269 .digital_names = &scope_digital_channel_names,
272 .num_devopts = ARRAY_SIZE(devopts),
274 .devopts_cg_analog = &devopts_cg_analog,
275 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
277 .coupling_options = &coupling_options,
278 .num_coupling_options = ARRAY_SIZE(coupling_options),
280 .trigger_sources = &an4_dig8_trigger_sources,
281 .num_trigger_sources = ARRAY_SIZE(an4_dig8_trigger_sources),
283 .trigger_slopes = &scope_trigger_slopes,
284 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
286 .timebases = &timebases,
287 .num_timebases = ARRAY_SIZE(timebases),
290 .num_vdivs = ARRAY_SIZE(vdivs),
295 .scpi_dialect = &hameg_scpi_dialect,
298 .name = {"HMO2524", "HMO3034", "HMO3044", "HMO3054", "HMO3524", NULL},
299 .analog_channels = 4,
300 .digital_channels = 16,
303 .analog_names = &scope_analog_channel_names,
304 .digital_names = &scope_digital_channel_names,
307 .num_devopts = ARRAY_SIZE(devopts),
309 .devopts_cg_analog = &devopts_cg_analog,
310 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
312 .coupling_options = &coupling_options,
313 .num_coupling_options = ARRAY_SIZE(coupling_options),
315 .trigger_sources = &an4_dig16_trigger_sources,
316 .num_trigger_sources = ARRAY_SIZE(an4_dig16_trigger_sources),
318 .trigger_slopes = &scope_trigger_slopes,
319 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
321 .timebases = &timebases,
322 .num_timebases = ARRAY_SIZE(timebases),
325 .num_vdivs = ARRAY_SIZE(vdivs),
330 .scpi_dialect = &hameg_scpi_dialect,
334 static void scope_state_dump(const struct scope_config *config,
335 struct scope_state *state)
340 for (i = 0; i < config->analog_channels; i++) {
341 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
342 (*config->vdivs)[state->analog_channels[i].vdiv][1]);
343 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
344 i + 1, state->analog_channels[i].state ? "On" : "Off",
345 (*config->coupling_options)[state->analog_channels[i].coupling],
346 tmp, state->analog_channels[i].vertical_offset);
349 for (i = 0; i < config->digital_channels; i++) {
350 sr_info("State of digital channel %d -> %s", i,
351 state->digital_channels[i] ? "On" : "Off");
354 for (i = 0; i < config->digital_pods; i++) {
355 sr_info("State of digital POD %d -> %s", i,
356 state->digital_pods[i] ? "On" : "Off");
359 tmp = sr_period_string((*config->timebases)[state->timebase][0],
360 (*config->timebases)[state->timebase][1]);
361 sr_info("Current timebase: %s", tmp);
364 tmp = sr_samplerate_string(state->sample_rate);
365 sr_info("Current samplerate: %s", tmp);
368 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
369 (*config->trigger_sources)[state->trigger_source],
370 (*config->trigger_slopes)[state->trigger_slope],
371 state->horiz_triggerpos);
374 static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
375 const char *command, const char *(*array)[], unsigned int n, int *result)
380 if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK) {
385 if ((idx = std_str_idx_s(tmp, *array, n)) < 0) {
398 * This function takes a value of the form "2.000E-03" and returns the index
399 * of an array where a matching pair was found.
401 * @param value The string to be parsed.
402 * @param array The array of s/f pairs.
403 * @param array_len The number of pairs in the array.
404 * @param result The index at which a matching pair was found.
406 * @return SR_ERR on any parsing error, SR_OK otherwise.
408 static int array_float_get(gchar *value, const uint64_t array[][2],
409 int array_len, unsigned int *result)
411 struct sr_rational rval;
412 struct sr_rational aval;
414 if (sr_parse_rational(value, &rval) != SR_OK)
417 for (int i = 0; i < array_len; i++) {
418 sr_rational_set(&aval, array[i][0], array[i][1]);
419 if (sr_rational_eq(&rval, &aval)) {
428 static struct sr_channel *get_channel_by_index_and_type(GSList *channel_lhead,
431 while (channel_lhead) {
432 struct sr_channel *ch = channel_lhead->data;
433 if (ch->index == index && ch->type == type)
436 channel_lhead = channel_lhead->next;
442 static int analog_channel_state_get(struct sr_dev_inst *sdi,
443 const struct scope_config *config,
444 struct scope_state *state)
447 char command[MAX_COMMAND_SIZE];
449 struct sr_channel *ch;
450 struct sr_scpi_dev_inst *scpi = sdi->conn;
452 for (i = 0; i < config->analog_channels; i++) {
453 g_snprintf(command, sizeof(command),
454 (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
457 if (sr_scpi_get_bool(scpi, command,
458 &state->analog_channels[i].state) != SR_OK)
461 ch = get_channel_by_index_and_type(sdi->channels, i, SR_CHANNEL_ANALOG);
463 ch->enabled = state->analog_channels[i].state;
465 g_snprintf(command, sizeof(command),
466 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
469 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
472 if (array_float_get(tmp_str, ARRAY_AND_SIZE(vdivs), &j) != SR_OK) {
474 sr_err("Could not determine array index for vertical div scale.");
479 state->analog_channels[i].vdiv = j;
481 g_snprintf(command, sizeof(command),
482 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
485 if (sr_scpi_get_float(scpi, command,
486 &state->analog_channels[i].vertical_offset) != SR_OK)
489 g_snprintf(command, sizeof(command),
490 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
493 if (scope_state_get_array_option(scpi, command, config->coupling_options,
494 config->num_coupling_options,
495 &state->analog_channels[i].coupling) != SR_OK)
498 g_snprintf(command, sizeof(command),
499 (*config->scpi_dialect)[SCPI_CMD_GET_PROBE_UNIT],
502 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
505 if (tmp_str[0] == 'A')
506 state->analog_channels[i].probe_unit = 'A';
508 state->analog_channels[i].probe_unit = 'V';
515 static int digital_channel_state_get(struct sr_dev_inst *sdi,
516 const struct scope_config *config,
517 struct scope_state *state)
520 char command[MAX_COMMAND_SIZE];
521 struct sr_channel *ch;
522 struct sr_scpi_dev_inst *scpi = sdi->conn;
524 for (i = 0; i < config->digital_channels; i++) {
525 g_snprintf(command, sizeof(command),
526 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
529 if (sr_scpi_get_bool(scpi, command,
530 &state->digital_channels[i]) != SR_OK)
533 ch = get_channel_by_index_and_type(sdi->channels, i, SR_CHANNEL_LOGIC);
535 ch->enabled = state->digital_channels[i];
538 for (i = 0; i < config->digital_pods; i++) {
539 g_snprintf(command, sizeof(command),
540 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
543 if (sr_scpi_get_bool(scpi, command,
544 &state->digital_pods[i]) != SR_OK)
551 SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
553 struct dev_context *devc;
554 struct scope_state *state;
555 const struct scope_config *config;
559 gboolean channel_found;
560 char tmp_str[MAX_COMMAND_SIZE];
564 config = devc->model_config;
565 state = devc->model_state;
566 channel_found = FALSE;
568 for (i = 0; i < config->analog_channels; i++) {
569 if (!state->analog_channels[i].state)
571 g_snprintf(chan_name, sizeof(chan_name), "CHAN%d", i + 1);
572 g_snprintf(tmp_str, sizeof(tmp_str),
573 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
575 channel_found = TRUE;
579 if (!channel_found) {
580 for (i = 0; i < config->digital_pods; i++) {
581 if (!state->digital_pods[i])
583 g_snprintf(chan_name, sizeof(chan_name), "POD%d", i);
584 g_snprintf(tmp_str, sizeof(tmp_str),
585 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
587 channel_found = TRUE;
592 /* No channel is active, ask the instrument for the sample rate
593 * in single shot mode */
594 if (!channel_found) {
595 if (sr_scpi_get_float(sdi->conn,
596 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE],
597 &tmp_float) != SR_OK)
600 state->sample_rate = tmp_float;
602 if (sr_scpi_get_int(sdi->conn, tmp_str, &tmp) != SR_OK)
604 state->sample_rate = tmp / (((float) (*config->timebases)[state->timebase][0] /
605 (*config->timebases)[state->timebase][1]) *
612 SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
614 struct dev_context *devc;
615 struct scope_state *state;
616 const struct scope_config *config;
622 config = devc->model_config;
623 state = devc->model_state;
625 sr_info("Fetching scope state");
627 if (analog_channel_state_get(sdi, config, state) != SR_OK)
630 if (digital_channel_state_get(sdi, config, state) != SR_OK)
633 if (sr_scpi_get_float(sdi->conn,
634 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
635 &tmp_float) != SR_OK)
638 if (sr_scpi_get_string(sdi->conn,
639 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
643 if (array_float_get(tmp_str, ARRAY_AND_SIZE(timebases), &i) != SR_OK) {
645 sr_err("Could not determine array index for time base.");
652 if (sr_scpi_get_float(sdi->conn,
653 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
654 &tmp_float) != SR_OK)
656 state->horiz_triggerpos = tmp_float /
657 (((double) (*config->timebases)[state->timebase][0] /
658 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
659 state->horiz_triggerpos -= 0.5;
660 state->horiz_triggerpos *= -1;
662 if (scope_state_get_array_option(sdi->conn,
663 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
664 config->trigger_sources, config->num_trigger_sources,
665 &state->trigger_source) != SR_OK)
668 if (scope_state_get_array_option(sdi->conn,
669 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
670 config->trigger_slopes, config->num_trigger_slopes,
671 &state->trigger_slope) != SR_OK)
674 if (hmo_update_sample_rate(sdi) != SR_OK)
677 sr_info("Fetching finished.");
679 scope_state_dump(config, state);
684 static struct scope_state *scope_state_new(const struct scope_config *config)
686 struct scope_state *state;
688 state = g_malloc0(sizeof(struct scope_state));
689 state->analog_channels = g_malloc0_n(config->analog_channels,
690 sizeof(struct analog_channel_state));
691 state->digital_channels = g_malloc0_n(
692 config->digital_channels, sizeof(gboolean));
693 state->digital_pods = g_malloc0_n(config->digital_pods,
699 SR_PRIV void hmo_scope_state_free(struct scope_state *state)
701 g_free(state->analog_channels);
702 g_free(state->digital_channels);
703 g_free(state->digital_pods);
707 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
710 unsigned int i, j, group;
711 struct sr_channel *ch;
712 struct dev_context *devc;
718 /* Find the exact model. */
719 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
720 for (j = 0; scope_models[i].name[j]; j++) {
721 if (!strcmp(sdi->model, scope_models[i].name[j])) {
726 if (model_index != -1)
730 if (model_index == -1) {
731 sr_dbg("Unsupported HMO device.");
735 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
736 scope_models[model_index].analog_channels);
737 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
738 scope_models[model_index].digital_pods);
739 if (!devc->analog_groups || !devc->digital_groups) {
740 g_free(devc->analog_groups);
741 g_free(devc->digital_groups);
742 return SR_ERR_MALLOC;
745 /* Add analog channels. */
746 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
747 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
748 (*scope_models[model_index].analog_names)[i]);
750 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
752 devc->analog_groups[i]->name = g_strdup(
753 (char *)(*scope_models[model_index].analog_names)[i]);
754 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
756 sdi->channel_groups = g_slist_append(sdi->channel_groups,
757 devc->analog_groups[i]);
760 /* Add digital channel groups. */
762 for (i = 0; i < scope_models[model_index].digital_pods; i++) {
763 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
764 if (!devc->digital_groups[i]) {
768 devc->digital_groups[i]->name = g_strdup_printf("POD%d", i);
769 sdi->channel_groups = g_slist_append(sdi->channel_groups,
770 devc->digital_groups[i]);
775 /* Add digital channels. */
776 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
777 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
778 (*scope_models[model_index].digital_names)[i]);
781 devc->digital_groups[group]->channels = g_slist_append(
782 devc->digital_groups[group]->channels, ch);
785 devc->model_config = &scope_models[model_index];
786 devc->frame_limit = 0;
788 if (!(devc->model_state = scope_state_new(devc->model_config)))
789 return SR_ERR_MALLOC;
794 /* Queue data of one channel group, for later submission. */
795 SR_PRIV void hmo_queue_logic_data(struct dev_context *devc,
796 size_t group, GByteArray *pod_data)
801 size_t idx, logic_step;
804 * Upon first invocation, allocate the array which can hold the
805 * combined logic data for all channels. Assume that each channel
806 * will yield an identical number of samples per receive call.
808 * As a poor man's safety measure: (Silently) skip processing
809 * for unexpected sample counts, and ignore samples for
810 * unexpected channel groups. Don't bother with complicated
811 * resize logic, considering that many models only support one
812 * pod, and the most capable supported models have two pods of
813 * identical size. We haven't yet seen any "odd" configuration.
815 if (!devc->logic_data) {
816 size = pod_data->len * devc->pod_count;
817 store = g_byte_array_sized_new(size);
818 memset(store->data, 0, size);
819 store = g_byte_array_set_size(store, size);
820 devc->logic_data = store;
822 store = devc->logic_data;
823 size = store->len / devc->pod_count;
824 if (size != pod_data->len)
826 if (group >= devc->pod_count)
831 * Fold the data of the most recently received channel group into
832 * the storage, where data resides for all channels combined.
834 logic_data = store->data;
836 logic_step = devc->pod_count;
837 for (idx = 0; idx < pod_data->len; idx++) {
838 *logic_data = pod_data->data[idx];
839 logic_data += logic_step;
843 /* Submit data for all channels, after the individual groups got collected. */
844 SR_PRIV void hmo_send_logic_packet(struct sr_dev_inst *sdi,
845 struct dev_context *devc)
847 struct sr_datafeed_packet packet;
848 struct sr_datafeed_logic logic;
850 if (!devc->logic_data)
853 logic.data = devc->logic_data->data;
854 logic.length = devc->logic_data->len;
855 logic.unitsize = devc->pod_count;
857 packet.type = SR_DF_LOGIC;
858 packet.payload = &logic;
860 sr_session_send(sdi, &packet);
863 /* Undo previous resource allocation. */
864 SR_PRIV void hmo_cleanup_logic_data(struct dev_context *devc)
867 if (devc->logic_data) {
868 g_byte_array_free(devc->logic_data, TRUE);
869 devc->logic_data = NULL;
872 * Keep 'pod_count'! It's required when more frames will be
873 * received, and does not harm when kept after acquisition.
877 SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
879 struct sr_channel *ch;
880 struct sr_dev_inst *sdi;
881 struct dev_context *devc;
882 struct scope_state *state;
883 struct sr_datafeed_packet packet;
885 struct sr_datafeed_analog analog;
886 struct sr_analog_encoding encoding;
887 struct sr_analog_meaning meaning;
888 struct sr_analog_spec spec;
889 struct sr_datafeed_logic logic;
897 if (!(sdi = cb_data))
900 if (!(devc = sdi->priv))
903 /* Although this is correct in general, the USBTMC libusb implementation
904 * currently does not generate an event prior to the first read. Often
905 * it is ok to start reading just after the 50ms timeout. See bug #785.
906 if (revents != G_IO_IN)
910 ch = devc->current_channel->data;
911 state = devc->model_state;
914 * Send "frame begin" packet upon reception of data for the
915 * first enabled channel.
917 if (devc->current_channel == devc->enabled_channels) {
918 packet.type = SR_DF_FRAME_BEGIN;
919 sr_session_send(sdi, &packet);
923 * Pass on the received data of the channel(s).
926 case SR_CHANNEL_ANALOG:
927 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
929 g_byte_array_free(data, TRUE);
933 packet.type = SR_DF_ANALOG;
935 analog.data = data->data;
936 analog.num_samples = data->len / sizeof(float);
937 analog.encoding = &encoding;
938 analog.meaning = &meaning;
941 encoding.unitsize = sizeof(float);
942 encoding.is_signed = TRUE;
943 encoding.is_float = TRUE;
944 #ifdef WORDS_BIGENDIAN
945 encoding.is_bigendian = TRUE;
947 encoding.is_bigendian = FALSE;
949 /* TODO: Use proper 'digits' value for this device (and its modes). */
951 encoding.is_digits_decimal = FALSE;
952 encoding.scale.p = 1;
953 encoding.scale.q = 1;
954 encoding.offset.p = 0;
955 encoding.offset.q = 1;
956 if (state->analog_channels[ch->index].probe_unit == 'V') {
957 meaning.mq = SR_MQ_VOLTAGE;
958 meaning.unit = SR_UNIT_VOLT;
960 meaning.mq = SR_MQ_CURRENT;
961 meaning.unit = SR_UNIT_AMPERE;
964 meaning.channels = g_slist_append(NULL, ch);
965 /* TODO: Use proper 'digits' value for this device (and its modes). */
966 spec.spec_digits = 2;
967 packet.payload = &analog;
968 sr_session_send(sdi, &packet);
969 g_slist_free(meaning.channels);
970 g_byte_array_free(data, TRUE);
973 case SR_CHANNEL_LOGIC:
974 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
976 g_byte_array_free(data, TRUE);
981 * If only data from the first pod is involved in the
982 * acquisition, then the raw input bytes can get passed
983 * forward for performance reasons. When the second pod
984 * is involved (either alone, or in combination with the
985 * first pod), then the received bytes need to be put
986 * into memory in such a layout that all channel groups
987 * get combined, and a unitsize larger than a single byte
988 * applies. The "queue" logic transparently copes with
989 * any such configuration. This works around the lack
990 * of support for "meaning" to logic data, which is used
991 * above for analog data.
993 if (devc->pod_count == 1) {
994 packet.type = SR_DF_LOGIC;
995 logic.data = data->data;
996 logic.length = data->len;
998 packet.payload = &logic;
999 sr_session_send(sdi, &packet);
1001 group = ch->index / 8;
1002 hmo_queue_logic_data(devc, group, data);
1005 g_byte_array_free(data, TRUE);
1009 sr_err("Invalid channel type.");
1014 * Advance to the next enabled channel. When data for all enabled
1015 * channels was received, then flush potentially queued logic data,
1016 * and send the "frame end" packet.
1018 if (devc->current_channel->next) {
1019 devc->current_channel = devc->current_channel->next;
1020 hmo_request_data(sdi);
1023 hmo_send_logic_packet(sdi, devc);
1026 * Release the logic data storage after each frame. This copes
1027 * with sample counts that differ in length per frame. -- Is
1028 * this a real constraint when acquiring multiple frames with
1029 * identical device settings?
1031 hmo_cleanup_logic_data(devc);
1033 packet.type = SR_DF_FRAME_END;
1034 sr_session_send(sdi, &packet);
1037 * End of frame was reached. Stop acquisition after the specified
1038 * number of frames, or continue reception by starting over at
1039 * the first enabled channel.
1041 if (++devc->num_frames == devc->frame_limit) {
1042 sr_dev_acquisition_stop(sdi);
1043 hmo_cleanup_logic_data(devc);
1045 devc->current_channel = devc->enabled_channels;
1046 hmo_request_data(sdi);