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 REAL,32;:CHAN%d:DATA?",
35 [SCPI_CMD_GET_VERTICAL_DIV] = ":CHAN%d:SCAL?",
36 [SCPI_CMD_SET_VERTICAL_DIV] = ":CHAN%d:SCAL %s",
37 [SCPI_CMD_GET_DIG_POD_STATE] = ":POD%d:STAT?",
38 [SCPI_CMD_SET_DIG_POD_STATE] = ":POD%d:STAT %d",
39 [SCPI_CMD_GET_TRIGGER_SLOPE] = ":TRIG:A:EDGE:SLOP?",
40 [SCPI_CMD_SET_TRIGGER_SLOPE] = ":TRIG:A:EDGE:SLOP %s",
41 [SCPI_CMD_GET_TRIGGER_SOURCE] = ":TRIG:A:SOUR?",
42 [SCPI_CMD_SET_TRIGGER_SOURCE] = ":TRIG:A:SOUR %s",
43 [SCPI_CMD_GET_DIG_CHAN_STATE] = ":LOG%d:STAT?",
44 [SCPI_CMD_SET_DIG_CHAN_STATE] = ":LOG%d:STAT %d",
45 [SCPI_CMD_GET_VERTICAL_OFFSET] = ":CHAN%d:POS?",
46 [SCPI_CMD_GET_HORIZ_TRIGGERPOS] = ":TIM:POS?",
47 [SCPI_CMD_SET_HORIZ_TRIGGERPOS] = ":TIM:POS %s",
48 [SCPI_CMD_GET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT?",
49 [SCPI_CMD_SET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT %d",
50 [SCPI_CMD_GET_PROBE_UNIT] = ":PROB%d:SET:ATT:UNIT?",
53 static const uint32_t hmo_devopts[] = {
55 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
56 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
57 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
58 SR_CONF_NUM_HDIV | SR_CONF_GET,
59 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
60 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
61 SR_CONF_SAMPLERATE | SR_CONF_GET,
64 static const uint32_t hmo_analog_devopts[] = {
65 SR_CONF_NUM_VDIV | SR_CONF_GET,
66 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
67 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
70 static const char *hmo_coupling_options[] = {
71 "AC", // AC with 50 Ohm termination (152x, 202x, 30xx, 1202)
72 "ACL", // AC with 1 MOhm termination
73 "DC", // DC with 50 Ohm termination
74 "DCL", // DC with 1 MOhm termination
79 static const char *scope_trigger_slopes[] = {
86 static const char *hmo_compact2_trigger_sources[] = {
105 static const char *hmo_compact4_trigger_sources[] = {
126 static const char *hmo_compact4_dig16_trigger_sources[] = {
155 static const uint64_t hmo_timebases[][2] = {
194 static const uint64_t hmo_vdivs[][2] = {
214 static const char *scope_analog_channel_names[] = {
221 static const char *scope_digital_channel_names[] = {
240 static const struct scope_config scope_models[] = {
242 /* HMO2522/3032/3042/3052 support 16 digital channels but they're not supported yet. */
243 .name = {"HMO1002", "HMO722", "HMO1022", "HMO1522", "HMO2022", "HMO2522",
244 "HMO3032", "HMO3042", "HMO3052", NULL},
245 .analog_channels = 2,
246 .digital_channels = 8,
249 .analog_names = &scope_analog_channel_names,
250 .digital_names = &scope_digital_channel_names,
252 .devopts = &hmo_devopts,
253 .num_devopts = ARRAY_SIZE(hmo_devopts),
255 .analog_devopts = &hmo_analog_devopts,
256 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
258 .coupling_options = &hmo_coupling_options,
259 .trigger_sources = &hmo_compact2_trigger_sources,
260 .trigger_slopes = &scope_trigger_slopes,
262 .timebases = &hmo_timebases,
263 .num_timebases = ARRAY_SIZE(hmo_timebases),
266 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
271 .scpi_dialect = &hameg_scpi_dialect,
274 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", NULL},
275 .analog_channels = 4,
276 .digital_channels = 8,
279 .analog_names = &scope_analog_channel_names,
280 .digital_names = &scope_digital_channel_names,
282 .devopts = &hmo_devopts,
283 .num_devopts = ARRAY_SIZE(hmo_devopts),
285 .analog_devopts = &hmo_analog_devopts,
286 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
288 .coupling_options = &hmo_coupling_options,
289 .trigger_sources = &hmo_compact4_trigger_sources,
290 .trigger_slopes = &scope_trigger_slopes,
292 .timebases = &hmo_timebases,
293 .num_timebases = ARRAY_SIZE(hmo_timebases),
296 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
301 .scpi_dialect = &hameg_scpi_dialect,
304 .name = {"HMO2524", "HMO3034", "HMO3044", "HMO3054", NULL},
305 .analog_channels = 4,
306 .digital_channels = 16,
309 .analog_names = &scope_analog_channel_names,
310 .digital_names = &scope_digital_channel_names,
312 .devopts = &hmo_devopts,
313 .num_devopts = ARRAY_SIZE(hmo_devopts),
315 .analog_devopts = &hmo_analog_devopts,
316 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
318 .coupling_options = &hmo_coupling_options,
319 .trigger_sources = &hmo_compact4_dig16_trigger_sources,
320 .trigger_slopes = &scope_trigger_slopes,
322 .timebases = &hmo_timebases,
323 .num_timebases = ARRAY_SIZE(hmo_timebases),
326 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
331 .scpi_dialect = &hameg_scpi_dialect,
335 static void scope_state_dump(const struct scope_config *config,
336 struct scope_state *state)
341 for (i = 0; i < config->analog_channels; i++) {
342 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
343 (*config->vdivs)[state->analog_channels[i].vdiv][1]);
344 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
345 i + 1, state->analog_channels[i].state ? "On" : "Off",
346 (*config->coupling_options)[state->analog_channels[i].coupling],
347 tmp, state->analog_channels[i].vertical_offset);
350 for (i = 0; i < config->digital_channels; i++) {
351 sr_info("State of digital channel %d -> %s", i,
352 state->digital_channels[i] ? "On" : "Off");
355 for (i = 0; i < config->digital_pods; i++) {
356 sr_info("State of digital POD %d -> %s", i,
357 state->digital_pods[i] ? "On" : "Off");
360 tmp = sr_period_string((*config->timebases)[state->timebase][0] *
361 (*config->timebases)[state->timebase][1]);
362 sr_info("Current timebase: %s", tmp);
365 tmp = sr_samplerate_string(state->sample_rate);
366 sr_info("Current samplerate: %s", tmp);
369 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
370 (*config->trigger_sources)[state->trigger_source],
371 (*config->trigger_slopes)[state->trigger_slope],
372 state->horiz_triggerpos);
375 static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
376 const char *command, const char *(*array)[], int *result)
381 if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK) {
386 for (i = 0; (*array)[i]; i++) {
387 if (!g_strcmp0(tmp, (*array)[i])) {
404 * This function takes a value of the form "2.000E-03" and returns the index
405 * of an array where a matching pair was found.
407 * @param value The string to be parsed.
408 * @param array The array of s/f pairs.
409 * @param array_len The number of pairs in the array.
410 * @param result The index at which a matching pair was found.
412 * @return SR_ERR on any parsing error, SR_OK otherwise.
414 static int array_float_get(gchar *value, const uint64_t array[][2],
415 int array_len, unsigned int *result)
417 struct sr_rational rval;
418 struct sr_rational aval;
420 if (sr_parse_rational(value, &rval) != SR_OK)
423 for (int i = 0; i < array_len; i++) {
424 sr_rational_set(&aval, array[i][0], array[i][1]);
425 if (sr_rational_eq(&rval, &aval)) {
434 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
435 const struct scope_config *config,
436 struct scope_state *state)
439 char command[MAX_COMMAND_SIZE];
442 for (i = 0; i < config->analog_channels; i++) {
443 g_snprintf(command, sizeof(command),
444 (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
447 if (sr_scpi_get_bool(scpi, command,
448 &state->analog_channels[i].state) != SR_OK)
451 g_snprintf(command, sizeof(command),
452 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
455 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
458 if (array_float_get(tmp_str, hmo_vdivs, ARRAY_SIZE(hmo_vdivs),
461 sr_err("Could not determine array index for vertical div scale.");
466 state->analog_channels[i].vdiv = j;
468 g_snprintf(command, sizeof(command),
469 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
472 if (sr_scpi_get_float(scpi, command,
473 &state->analog_channels[i].vertical_offset) != SR_OK)
476 g_snprintf(command, sizeof(command),
477 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
480 if (scope_state_get_array_option(scpi, command, config->coupling_options,
481 &state->analog_channels[i].coupling) != SR_OK)
484 g_snprintf(command, sizeof(command),
485 (*config->scpi_dialect)[SCPI_CMD_GET_PROBE_UNIT],
488 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
491 if (tmp_str[0] == 'A')
492 state->analog_channels[i].probe_unit = 'A';
494 state->analog_channels[i].probe_unit = 'V';
501 static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
502 const struct scope_config *config,
503 struct scope_state *state)
506 char command[MAX_COMMAND_SIZE];
508 for (i = 0; i < config->digital_channels; i++) {
509 g_snprintf(command, sizeof(command),
510 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
513 if (sr_scpi_get_bool(scpi, command,
514 &state->digital_channels[i]) != SR_OK)
518 for (i = 0; i < config->digital_pods; i++) {
519 g_snprintf(command, sizeof(command),
520 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
523 if (sr_scpi_get_bool(scpi, command,
524 &state->digital_pods[i]) != SR_OK)
531 SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
533 struct dev_context *devc;
534 struct scope_state *state;
535 const struct scope_config *config;
540 gboolean channel_found;
541 char tmp_str[MAX_COMMAND_SIZE];
545 config = devc->model_config;
546 state = devc->model_state;
547 channel_found = FALSE;
549 for (i = 0; i < config->analog_channels; i++) {
550 if (state->analog_channels[i].state) {
551 g_snprintf(chan_name, sizeof(chan_name), "CHAN%d", i + 1);
552 g_snprintf(tmp_str, sizeof(tmp_str),
553 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
555 channel_found = TRUE;
560 if (!channel_found) {
561 for (i = 0; i < config->digital_pods; i++) {
562 if (state->digital_pods[i]) {
563 g_snprintf(chan_name, sizeof(chan_name), "POD%d", i);
564 g_snprintf(tmp_str, sizeof(tmp_str),
565 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
567 channel_found = TRUE;
573 /* No channel is active, ask the instrument for the sample rate
574 * in single shot mode */
575 if (!channel_found) {
576 if (sr_scpi_get_float(sdi->conn,
577 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE],
578 &tmp_float) != SR_OK)
581 state->sample_rate = tmp_float;
583 if (sr_scpi_get_int(sdi->conn, tmp_str, &tmp) != SR_OK)
585 state->sample_rate = tmp / (((float) (*config->timebases)[state->timebase][0] /
586 (*config->timebases)[state->timebase][1]) *
593 SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
595 struct dev_context *devc;
596 struct scope_state *state;
597 const struct scope_config *config;
603 config = devc->model_config;
604 state = devc->model_state;
606 sr_info("Fetching scope state");
608 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
611 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
614 if (sr_scpi_get_float(sdi->conn,
615 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
616 &tmp_float) != SR_OK)
619 if (sr_scpi_get_string(sdi->conn,
620 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
624 if (array_float_get(tmp_str, hmo_timebases, ARRAY_SIZE(hmo_timebases),
627 sr_err("Could not determine array index for time base.");
634 if (sr_scpi_get_float(sdi->conn,
635 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
636 &tmp_float) != SR_OK)
638 state->horiz_triggerpos = tmp_float /
639 (((double) (*config->timebases)[state->timebase][0] /
640 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
641 state->horiz_triggerpos -= 0.5;
642 state->horiz_triggerpos *= -1;
644 if (scope_state_get_array_option(sdi->conn,
645 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
646 config->trigger_sources, &state->trigger_source) != SR_OK)
649 if (scope_state_get_array_option(sdi->conn,
650 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
651 config->trigger_slopes, &state->trigger_slope) != SR_OK)
654 if (hmo_update_sample_rate(sdi) != SR_OK)
657 sr_info("Fetching finished.");
659 scope_state_dump(config, state);
664 static struct scope_state *scope_state_new(const struct scope_config *config)
666 struct scope_state *state;
668 state = g_malloc0(sizeof(struct scope_state));
669 state->analog_channels = g_malloc0_n(config->analog_channels,
670 sizeof(struct analog_channel_state));
671 state->digital_channels = g_malloc0_n(
672 config->digital_channels, sizeof(gboolean));
673 state->digital_pods = g_malloc0_n(config->digital_pods,
679 SR_PRIV void hmo_scope_state_free(struct scope_state *state)
681 g_free(state->analog_channels);
682 g_free(state->digital_channels);
683 g_free(state->digital_pods);
687 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
691 unsigned int i, j, group;
692 struct sr_channel *ch;
693 struct dev_context *devc;
698 /* Find the exact model. */
699 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
700 for (j = 0; scope_models[i].name[j]; j++) {
701 if (!strcmp(sdi->model, scope_models[i].name[j])) {
706 if (model_index != -1)
710 if (model_index == -1) {
711 sr_dbg("Unsupported HMO device.");
715 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
716 scope_models[model_index].analog_channels);
718 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
719 scope_models[model_index].digital_pods);
721 /* Add analog channels. */
722 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
723 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
724 (*scope_models[model_index].analog_names)[i]);
726 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
728 devc->analog_groups[i]->name = g_strdup(
729 (char *)(*scope_models[model_index].analog_names)[i]);
730 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
732 sdi->channel_groups = g_slist_append(sdi->channel_groups,
733 devc->analog_groups[i]);
736 /* Add digital channel groups. */
737 for (i = 0; i < scope_models[model_index].digital_pods; i++) {
738 g_snprintf(tmp, 25, "POD%d", i);
740 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
742 devc->digital_groups[i]->name = g_strdup(tmp);
743 sdi->channel_groups = g_slist_append(sdi->channel_groups,
744 devc->digital_groups[i]);
747 /* Add digital channels. */
748 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
749 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
750 (*scope_models[model_index].digital_names)[i]);
753 devc->digital_groups[group]->channels = g_slist_append(
754 devc->digital_groups[group]->channels, ch);
757 devc->model_config = &scope_models[model_index];
758 devc->frame_limit = 0;
760 if (!(devc->model_state = scope_state_new(devc->model_config)))
761 return SR_ERR_MALLOC;
766 SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
768 struct sr_channel *ch;
769 struct sr_dev_inst *sdi;
770 struct dev_context *devc;
771 struct scope_state *state;
772 struct sr_datafeed_packet packet;
774 struct sr_datafeed_analog analog;
775 struct sr_analog_encoding encoding;
776 struct sr_analog_meaning meaning;
777 struct sr_analog_spec spec;
778 struct sr_datafeed_logic logic;
785 if (!(sdi = cb_data))
788 if (!(devc = sdi->priv))
791 /* Although this is correct in general, the USBTMC libusb implementation
792 * currently does not generate an event prior to the first read. Often
793 * it is ok to start reading just after the 50ms timeout. See bug #785.
794 if (revents != G_IO_IN)
798 ch = devc->current_channel->data;
799 state = devc->model_state;
802 case SR_CHANNEL_ANALOG:
803 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
805 g_byte_array_free(data, TRUE);
810 packet.type = SR_DF_FRAME_BEGIN;
811 sr_session_send(sdi, &packet);
813 packet.type = SR_DF_ANALOG;
815 analog.data = data->data;
816 analog.num_samples = data->len / sizeof(float);
817 analog.encoding = &encoding;
818 analog.meaning = &meaning;
821 encoding.unitsize = sizeof(float);
822 encoding.is_signed = TRUE;
823 encoding.is_float = TRUE;
824 encoding.is_bigendian = FALSE;
825 /* TODO: Use proper 'digits' value for this device (and its modes). */
827 encoding.is_digits_decimal = FALSE;
828 encoding.scale.p = 1;
829 encoding.scale.q = 1;
830 encoding.offset.p = 0;
831 encoding.offset.q = 1;
832 if (state->analog_channels[ch->index].probe_unit == 'V') {
833 meaning.mq = SR_MQ_VOLTAGE;
834 meaning.unit = SR_UNIT_VOLT;
836 meaning.mq = SR_MQ_CURRENT;
837 meaning.unit = SR_UNIT_AMPERE;
840 meaning.channels = g_slist_append(NULL, ch);
841 /* TODO: Use proper 'digits' value for this device (and its modes). */
842 spec.spec_digits = 2;
843 packet.payload = &analog;
844 sr_session_send(sdi, &packet);
845 g_slist_free(meaning.channels);
846 g_byte_array_free(data, TRUE);
849 case SR_CHANNEL_LOGIC:
850 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
855 packet.type = SR_DF_FRAME_BEGIN;
856 sr_session_send(sdi, &packet);
858 logic.length = data->len;
860 logic.data = data->data;
861 packet.type = SR_DF_LOGIC;
862 packet.payload = &logic;
863 sr_session_send(sdi, &packet);
864 g_byte_array_free(data, TRUE);
868 sr_err("Invalid channel type.");
872 packet.type = SR_DF_FRAME_END;
873 sr_session_send(sdi, &packet);
875 if (devc->current_channel->next) {
876 devc->current_channel = devc->current_channel->next;
877 hmo_request_data(sdi);
878 } else if (++devc->num_frames == devc->frame_limit) {
879 sdi->driver->dev_acquisition_stop(sdi);
881 devc->current_channel = devc->enabled_channels;
882 hmo_request_data(sdi);