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 static const char *hameg_scpi_dialect[] = {
27 [SCPI_CMD_GET_DIG_DATA] = ":FORM UINT,8;:POD%d:DATA?",
28 [SCPI_CMD_GET_TIMEBASE] = ":TIM:SCAL?",
29 [SCPI_CMD_SET_TIMEBASE] = ":TIM:SCAL %s",
30 [SCPI_CMD_GET_COUPLING] = ":CHAN%d:COUP?",
31 [SCPI_CMD_SET_COUPLING] = ":CHAN%d:COUP %s",
32 [SCPI_CMD_GET_SAMPLE_RATE] = ":ACQ:SRAT?",
33 [SCPI_CMD_GET_SAMPLE_RATE_LIVE] = ":%s:DATA:POINTS?",
34 [SCPI_CMD_GET_ANALOG_DATA] = ":FORM:BORD %s;" \
35 ":FORM REAL,32;:CHAN%d:DATA?",
36 [SCPI_CMD_GET_VERTICAL_DIV] = ":CHAN%d:SCAL?",
37 [SCPI_CMD_SET_VERTICAL_DIV] = ":CHAN%d:SCAL %s",
38 [SCPI_CMD_GET_DIG_POD_STATE] = ":POD%d:STAT?",
39 [SCPI_CMD_SET_DIG_POD_STATE] = ":POD%d:STAT %d",
40 [SCPI_CMD_GET_TRIGGER_SLOPE] = ":TRIG:A:EDGE:SLOP?",
41 [SCPI_CMD_SET_TRIGGER_SLOPE] = ":TRIG:A:EDGE:SLOP %s",
42 [SCPI_CMD_GET_TRIGGER_SOURCE] = ":TRIG:A:SOUR?",
43 [SCPI_CMD_SET_TRIGGER_SOURCE] = ":TRIG:A:SOUR %s",
44 [SCPI_CMD_GET_DIG_CHAN_STATE] = ":LOG%d:STAT?",
45 [SCPI_CMD_SET_DIG_CHAN_STATE] = ":LOG%d:STAT %d",
46 [SCPI_CMD_GET_VERTICAL_OFFSET] = ":CHAN%d:POS?",
47 [SCPI_CMD_GET_HORIZ_TRIGGERPOS] = ":TIM:POS?",
48 [SCPI_CMD_SET_HORIZ_TRIGGERPOS] = ":TIM:POS %s",
49 [SCPI_CMD_GET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT?",
50 [SCPI_CMD_SET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT %d",
51 [SCPI_CMD_GET_PROBE_UNIT] = ":PROB%d:SET:ATT:UNIT?",
54 static const uint32_t hmo_devopts[] = {
56 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
57 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
58 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
59 SR_CONF_NUM_HDIV | SR_CONF_GET,
60 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
61 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
62 SR_CONF_SAMPLERATE | SR_CONF_GET,
65 static const uint32_t hmo_analog_devopts[] = {
66 SR_CONF_NUM_VDIV | SR_CONF_GET,
67 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
68 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
71 static const char *hmo_coupling_options[] = {
72 "AC", // AC with 50 Ohm termination (152x, 202x, 30xx, 1202)
73 "ACL", // AC with 1 MOhm termination
74 "DC", // DC with 50 Ohm termination
75 "DCL", // DC with 1 MOhm termination
80 static const char *scope_trigger_slopes[] = {
87 static const char *hmo_compact2_trigger_sources[] = {
106 static const char *hmo_compact4_trigger_sources[] = {
127 static const char *hmo_compact4_dig16_trigger_sources[] = {
156 static const uint64_t hmo_timebases[][2] = {
195 static const uint64_t hmo_vdivs[][2] = {
215 static const char *scope_analog_channel_names[] = {
222 static const char *scope_digital_channel_names[] = {
241 static const struct scope_config scope_models[] = {
243 /* HMO2522/3032/3042/3052 support 16 digital channels but they're not supported yet. */
244 .name = {"HMO1002", "HMO722", "HMO1022", "HMO1522", "HMO2022", "HMO2522",
245 "HMO3032", "HMO3042", "HMO3052", NULL},
246 .analog_channels = 2,
247 .digital_channels = 8,
250 .analog_names = &scope_analog_channel_names,
251 .digital_names = &scope_digital_channel_names,
253 .devopts = &hmo_devopts,
254 .num_devopts = ARRAY_SIZE(hmo_devopts),
256 .analog_devopts = &hmo_analog_devopts,
257 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
259 .coupling_options = &hmo_coupling_options,
260 .trigger_sources = &hmo_compact2_trigger_sources,
261 .trigger_slopes = &scope_trigger_slopes,
263 .timebases = &hmo_timebases,
264 .num_timebases = ARRAY_SIZE(hmo_timebases),
267 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
272 .scpi_dialect = &hameg_scpi_dialect,
275 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", NULL},
276 .analog_channels = 4,
277 .digital_channels = 8,
280 .analog_names = &scope_analog_channel_names,
281 .digital_names = &scope_digital_channel_names,
283 .devopts = &hmo_devopts,
284 .num_devopts = ARRAY_SIZE(hmo_devopts),
286 .analog_devopts = &hmo_analog_devopts,
287 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
289 .coupling_options = &hmo_coupling_options,
290 .trigger_sources = &hmo_compact4_trigger_sources,
291 .trigger_slopes = &scope_trigger_slopes,
293 .timebases = &hmo_timebases,
294 .num_timebases = ARRAY_SIZE(hmo_timebases),
297 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
302 .scpi_dialect = &hameg_scpi_dialect,
305 .name = {"HMO2524", "HMO3034", "HMO3044", "HMO3054", NULL},
306 .analog_channels = 4,
307 .digital_channels = 16,
310 .analog_names = &scope_analog_channel_names,
311 .digital_names = &scope_digital_channel_names,
313 .devopts = &hmo_devopts,
314 .num_devopts = ARRAY_SIZE(hmo_devopts),
316 .analog_devopts = &hmo_analog_devopts,
317 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
319 .coupling_options = &hmo_coupling_options,
320 .trigger_sources = &hmo_compact4_dig16_trigger_sources,
321 .trigger_slopes = &scope_trigger_slopes,
323 .timebases = &hmo_timebases,
324 .num_timebases = ARRAY_SIZE(hmo_timebases),
327 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
332 .scpi_dialect = &hameg_scpi_dialect,
336 static void scope_state_dump(const struct scope_config *config,
337 struct scope_state *state)
342 for (i = 0; i < config->analog_channels; i++) {
343 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
344 (*config->vdivs)[state->analog_channels[i].vdiv][1]);
345 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
346 i + 1, state->analog_channels[i].state ? "On" : "Off",
347 (*config->coupling_options)[state->analog_channels[i].coupling],
348 tmp, state->analog_channels[i].vertical_offset);
351 for (i = 0; i < config->digital_channels; i++) {
352 sr_info("State of digital channel %d -> %s", i,
353 state->digital_channels[i] ? "On" : "Off");
356 for (i = 0; i < config->digital_pods; i++) {
357 sr_info("State of digital POD %d -> %s", i,
358 state->digital_pods[i] ? "On" : "Off");
361 tmp = sr_period_string((*config->timebases)[state->timebase][0] *
362 (*config->timebases)[state->timebase][1]);
363 sr_info("Current timebase: %s", tmp);
366 tmp = sr_samplerate_string(state->sample_rate);
367 sr_info("Current samplerate: %s", tmp);
370 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
371 (*config->trigger_sources)[state->trigger_source],
372 (*config->trigger_slopes)[state->trigger_slope],
373 state->horiz_triggerpos);
376 static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
377 const char *command, const char *(*array)[], int *result)
382 if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK) {
387 for (i = 0; (*array)[i]; i++) {
388 if (!g_strcmp0(tmp, (*array)[i])) {
405 * This function takes a value of the form "2.000E-03" and returns the index
406 * of an array where a matching pair was found.
408 * @param value The string to be parsed.
409 * @param array The array of s/f pairs.
410 * @param array_len The number of pairs in the array.
411 * @param result The index at which a matching pair was found.
413 * @return SR_ERR on any parsing error, SR_OK otherwise.
415 static int array_float_get(gchar *value, const uint64_t array[][2],
416 int array_len, unsigned int *result)
418 struct sr_rational rval;
419 struct sr_rational aval;
421 if (sr_parse_rational(value, &rval) != SR_OK)
424 for (int i = 0; i < array_len; i++) {
425 sr_rational_set(&aval, array[i][0], array[i][1]);
426 if (sr_rational_eq(&rval, &aval)) {
435 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
436 const struct scope_config *config,
437 struct scope_state *state)
440 char command[MAX_COMMAND_SIZE];
443 for (i = 0; i < config->analog_channels; i++) {
444 g_snprintf(command, sizeof(command),
445 (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
448 if (sr_scpi_get_bool(scpi, command,
449 &state->analog_channels[i].state) != SR_OK)
452 g_snprintf(command, sizeof(command),
453 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
456 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
459 if (array_float_get(tmp_str, hmo_vdivs, ARRAY_SIZE(hmo_vdivs),
462 sr_err("Could not determine array index for vertical div scale.");
467 state->analog_channels[i].vdiv = j;
469 g_snprintf(command, sizeof(command),
470 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
473 if (sr_scpi_get_float(scpi, command,
474 &state->analog_channels[i].vertical_offset) != SR_OK)
477 g_snprintf(command, sizeof(command),
478 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
481 if (scope_state_get_array_option(scpi, command, config->coupling_options,
482 &state->analog_channels[i].coupling) != SR_OK)
485 g_snprintf(command, sizeof(command),
486 (*config->scpi_dialect)[SCPI_CMD_GET_PROBE_UNIT],
489 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
492 if (tmp_str[0] == 'A')
493 state->analog_channels[i].probe_unit = 'A';
495 state->analog_channels[i].probe_unit = 'V';
502 static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
503 const struct scope_config *config,
504 struct scope_state *state)
507 char command[MAX_COMMAND_SIZE];
509 for (i = 0; i < config->digital_channels; i++) {
510 g_snprintf(command, sizeof(command),
511 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
514 if (sr_scpi_get_bool(scpi, command,
515 &state->digital_channels[i]) != SR_OK)
519 for (i = 0; i < config->digital_pods; i++) {
520 g_snprintf(command, sizeof(command),
521 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
524 if (sr_scpi_get_bool(scpi, command,
525 &state->digital_pods[i]) != SR_OK)
532 SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
534 struct dev_context *devc;
535 struct scope_state *state;
536 const struct scope_config *config;
541 gboolean channel_found;
542 char tmp_str[MAX_COMMAND_SIZE];
546 config = devc->model_config;
547 state = devc->model_state;
548 channel_found = FALSE;
550 for (i = 0; i < config->analog_channels; i++) {
551 if (state->analog_channels[i].state) {
552 g_snprintf(chan_name, sizeof(chan_name), "CHAN%d", i + 1);
553 g_snprintf(tmp_str, sizeof(tmp_str),
554 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
556 channel_found = TRUE;
561 if (!channel_found) {
562 for (i = 0; i < config->digital_pods; i++) {
563 if (state->digital_pods[i]) {
564 g_snprintf(chan_name, sizeof(chan_name), "POD%d", i);
565 g_snprintf(tmp_str, sizeof(tmp_str),
566 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
568 channel_found = TRUE;
574 /* No channel is active, ask the instrument for the sample rate
575 * in single shot mode */
576 if (!channel_found) {
577 if (sr_scpi_get_float(sdi->conn,
578 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE],
579 &tmp_float) != SR_OK)
582 state->sample_rate = tmp_float;
584 if (sr_scpi_get_int(sdi->conn, tmp_str, &tmp) != SR_OK)
586 state->sample_rate = tmp / (((float) (*config->timebases)[state->timebase][0] /
587 (*config->timebases)[state->timebase][1]) *
594 SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
596 struct dev_context *devc;
597 struct scope_state *state;
598 const struct scope_config *config;
604 config = devc->model_config;
605 state = devc->model_state;
607 sr_info("Fetching scope state");
609 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
612 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
615 if (sr_scpi_get_float(sdi->conn,
616 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
617 &tmp_float) != SR_OK)
620 if (sr_scpi_get_string(sdi->conn,
621 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
625 if (array_float_get(tmp_str, hmo_timebases, ARRAY_SIZE(hmo_timebases),
628 sr_err("Could not determine array index for time base.");
635 if (sr_scpi_get_float(sdi->conn,
636 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
637 &tmp_float) != SR_OK)
639 state->horiz_triggerpos = tmp_float /
640 (((double) (*config->timebases)[state->timebase][0] /
641 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
642 state->horiz_triggerpos -= 0.5;
643 state->horiz_triggerpos *= -1;
645 if (scope_state_get_array_option(sdi->conn,
646 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
647 config->trigger_sources, &state->trigger_source) != SR_OK)
650 if (scope_state_get_array_option(sdi->conn,
651 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
652 config->trigger_slopes, &state->trigger_slope) != SR_OK)
655 if (hmo_update_sample_rate(sdi) != SR_OK)
658 sr_info("Fetching finished.");
660 scope_state_dump(config, state);
665 static struct scope_state *scope_state_new(const struct scope_config *config)
667 struct scope_state *state;
669 state = g_malloc0(sizeof(struct scope_state));
670 state->analog_channels = g_malloc0_n(config->analog_channels,
671 sizeof(struct analog_channel_state));
672 state->digital_channels = g_malloc0_n(
673 config->digital_channels, sizeof(gboolean));
674 state->digital_pods = g_malloc0_n(config->digital_pods,
680 SR_PRIV void hmo_scope_state_free(struct scope_state *state)
682 g_free(state->analog_channels);
683 g_free(state->digital_channels);
684 g_free(state->digital_pods);
688 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
692 unsigned int i, j, group;
693 struct sr_channel *ch;
694 struct dev_context *devc;
699 /* Find the exact model. */
700 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
701 for (j = 0; scope_models[i].name[j]; j++) {
702 if (!strcmp(sdi->model, scope_models[i].name[j])) {
707 if (model_index != -1)
711 if (model_index == -1) {
712 sr_dbg("Unsupported HMO device.");
716 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
717 scope_models[model_index].analog_channels);
719 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
720 scope_models[model_index].digital_pods);
722 /* Add analog channels. */
723 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
724 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
725 (*scope_models[model_index].analog_names)[i]);
727 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
729 devc->analog_groups[i]->name = g_strdup(
730 (char *)(*scope_models[model_index].analog_names)[i]);
731 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
733 sdi->channel_groups = g_slist_append(sdi->channel_groups,
734 devc->analog_groups[i]);
737 /* Add digital channel groups. */
738 for (i = 0; i < scope_models[model_index].digital_pods; i++) {
739 g_snprintf(tmp, 25, "POD%d", i);
741 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
743 devc->digital_groups[i]->name = g_strdup(tmp);
744 sdi->channel_groups = g_slist_append(sdi->channel_groups,
745 devc->digital_groups[i]);
748 /* Add digital channels. */
749 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
750 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
751 (*scope_models[model_index].digital_names)[i]);
754 devc->digital_groups[group]->channels = g_slist_append(
755 devc->digital_groups[group]->channels, ch);
758 devc->model_config = &scope_models[model_index];
759 devc->frame_limit = 0;
761 if (!(devc->model_state = scope_state_new(devc->model_config)))
762 return SR_ERR_MALLOC;
767 SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
769 struct sr_channel *ch;
770 struct sr_dev_inst *sdi;
771 struct dev_context *devc;
772 struct scope_state *state;
773 struct sr_datafeed_packet packet;
775 struct sr_datafeed_analog analog;
776 struct sr_analog_encoding encoding;
777 struct sr_analog_meaning meaning;
778 struct sr_analog_spec spec;
779 struct sr_datafeed_logic logic;
786 if (!(sdi = cb_data))
789 if (!(devc = sdi->priv))
792 /* Although this is correct in general, the USBTMC libusb implementation
793 * currently does not generate an event prior to the first read. Often
794 * it is ok to start reading just after the 50ms timeout. See bug #785.
795 if (revents != G_IO_IN)
799 ch = devc->current_channel->data;
800 state = devc->model_state;
803 case SR_CHANNEL_ANALOG:
804 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
806 g_byte_array_free(data, TRUE);
811 packet.type = SR_DF_FRAME_BEGIN;
812 sr_session_send(sdi, &packet);
814 packet.type = SR_DF_ANALOG;
816 analog.data = data->data;
817 analog.num_samples = data->len / sizeof(float);
818 analog.encoding = &encoding;
819 analog.meaning = &meaning;
822 encoding.unitsize = sizeof(float);
823 encoding.is_signed = TRUE;
824 encoding.is_float = TRUE;
825 #ifdef WORDS_BIGENDIAN
826 encoding.is_bigendian = TRUE;
828 encoding.is_bigendian = FALSE;
830 /* TODO: Use proper 'digits' value for this device (and its modes). */
832 encoding.is_digits_decimal = FALSE;
833 encoding.scale.p = 1;
834 encoding.scale.q = 1;
835 encoding.offset.p = 0;
836 encoding.offset.q = 1;
837 if (state->analog_channels[ch->index].probe_unit == 'V') {
838 meaning.mq = SR_MQ_VOLTAGE;
839 meaning.unit = SR_UNIT_VOLT;
841 meaning.mq = SR_MQ_CURRENT;
842 meaning.unit = SR_UNIT_AMPERE;
845 meaning.channels = g_slist_append(NULL, ch);
846 /* TODO: Use proper 'digits' value for this device (and its modes). */
847 spec.spec_digits = 2;
848 packet.payload = &analog;
849 sr_session_send(sdi, &packet);
850 g_slist_free(meaning.channels);
851 g_byte_array_free(data, TRUE);
854 case SR_CHANNEL_LOGIC:
855 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
860 packet.type = SR_DF_FRAME_BEGIN;
861 sr_session_send(sdi, &packet);
863 logic.length = data->len;
865 logic.data = data->data;
866 packet.type = SR_DF_LOGIC;
867 packet.payload = &logic;
868 sr_session_send(sdi, &packet);
869 g_byte_array_free(data, TRUE);
873 sr_err("Invalid channel type.");
877 packet.type = SR_DF_FRAME_END;
878 sr_session_send(sdi, &packet);
880 if (devc->current_channel->next) {
881 devc->current_channel = devc->current_channel->next;
882 hmo_request_data(sdi);
883 } else if (++devc->num_frames == devc->frame_limit) {
884 sdi->driver->dev_acquisition_stop(sdi);
886 devc->current_channel = devc->enabled_channels;
887 hmo_request_data(sdi);