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] = ":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] = ":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",
52 static const uint32_t hmo_devopts[] = {
54 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
55 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
56 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
57 SR_CONF_NUM_HDIV | SR_CONF_GET,
58 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
59 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
60 SR_CONF_SAMPLERATE | SR_CONF_GET,
63 static const uint32_t hmo_analog_devopts[] = {
64 SR_CONF_NUM_VDIV | SR_CONF_GET,
65 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
66 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
69 static const char *hmo_coupling_options[] = {
78 static const char *scope_trigger_slopes[] = {
85 static const char *hmo_compact2_trigger_sources[] = {
104 static const char *hmo_compact4_trigger_sources[] = {
125 static const uint64_t hmo_timebases[][2] = {
164 static const uint64_t hmo_vdivs[][2] = {
184 static const char *scope_analog_channel_names[] = {
191 static const char *scope_digital_channel_names[] = {
210 static const struct scope_config scope_models[] = {
212 /* HMO2522/3032/3042/3052 support 16 digital channels but they're not supported yet. */
213 .name = {"HMO1002", "HMO722", "HMO1022", "HMO1522", "HMO2022", "HMO2522",
214 "HMO3032", "HMO3042", "HMO3052", NULL},
215 .analog_channels = 2,
216 .digital_channels = 8,
219 .analog_names = &scope_analog_channel_names,
220 .digital_names = &scope_digital_channel_names,
222 .devopts = &hmo_devopts,
223 .num_devopts = ARRAY_SIZE(hmo_devopts),
225 .analog_devopts = &hmo_analog_devopts,
226 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
228 .coupling_options = &hmo_coupling_options,
229 .trigger_sources = &hmo_compact2_trigger_sources,
230 .trigger_slopes = &scope_trigger_slopes,
232 .timebases = &hmo_timebases,
233 .num_timebases = ARRAY_SIZE(hmo_timebases),
236 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
241 .scpi_dialect = &hameg_scpi_dialect,
244 /* HMO2524/3034/3044/3054 support 16 digital channels but they're not supported yet. */
245 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", "HMO2524",
246 "HMO3034", "HMO3044", "HMO3054", NULL},
247 .analog_channels = 4,
248 .digital_channels = 8,
251 .analog_names = &scope_analog_channel_names,
252 .digital_names = &scope_digital_channel_names,
254 .devopts = &hmo_devopts,
255 .num_devopts = ARRAY_SIZE(hmo_devopts),
257 .analog_devopts = &hmo_analog_devopts,
258 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
260 .coupling_options = &hmo_coupling_options,
261 .trigger_sources = &hmo_compact4_trigger_sources,
262 .trigger_slopes = &scope_trigger_slopes,
264 .timebases = &hmo_timebases,
265 .num_timebases = ARRAY_SIZE(hmo_timebases),
268 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
273 .scpi_dialect = &hameg_scpi_dialect,
277 static void scope_state_dump(const struct scope_config *config,
278 struct scope_state *state)
283 for (i = 0; i < config->analog_channels; i++) {
284 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
285 (*config->vdivs)[state->analog_channels[i].vdiv][1]);
286 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
287 i + 1, state->analog_channels[i].state ? "On" : "Off",
288 (*config->coupling_options)[state->analog_channels[i].coupling],
289 tmp, state->analog_channels[i].vertical_offset);
292 for (i = 0; i < config->digital_channels; i++) {
293 sr_info("State of digital channel %d -> %s", i,
294 state->digital_channels[i] ? "On" : "Off");
297 for (i = 0; i < config->digital_pods; i++) {
298 sr_info("State of digital POD %d -> %s", i,
299 state->digital_pods[i] ? "On" : "Off");
302 tmp = sr_period_string((*config->timebases)[state->timebase][0] *
303 (*config->timebases)[state->timebase][1]);
304 sr_info("Current timebase: %s", tmp);
307 tmp = sr_samplerate_string(state->sample_rate);
308 sr_info("Current samplerate: %s", tmp);
311 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
312 (*config->trigger_sources)[state->trigger_source],
313 (*config->trigger_slopes)[state->trigger_slope],
314 state->horiz_triggerpos);
317 static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
318 const char *command, const char *(*array)[], int *result)
323 if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK) {
328 for (i = 0; (*array)[i]; i++) {
329 if (!g_strcmp0(tmp, (*array)[i])) {
346 * This function takes a value of the form "2.000E-03", converts it to a
347 * significand / factor pair and returns the index of an array where
348 * a matching pair was found.
350 * It's a bit convoluted because of floating-point issues. The value "10.00E-09"
351 * is parsed by g_ascii_strtod() as 0.000000009999999939, for example.
352 * Therefore it's easier to break the number up into two strings and handle
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)
370 gchar ss[10], es[10];
372 memset(ss, 0, sizeof(ss));
373 memset(es, 0, sizeof(es));
375 /* Get index of the separating 'E' character and break up the string. */
376 pos = strcspn(value, "E");
378 strncpy(ss, value, pos);
379 strncpy(es, &(value[pos+1]), 3);
381 if (sr_atof_ascii(ss, &s) != SR_OK)
383 if (sr_atoi(es, &e) != SR_OK)
386 /* Transform e.g. 10^-03 to 1000 as the array stores the inverse. */
390 * Adjust the significand/factor pair to make sure
391 * that f is a multiple of 1000.
393 while ((int)fmod(log10(f), 3) > 0) {
402 /* Truncate s to circumvent rounding errors. */
403 s_int = (unsigned int)s;
405 for (i = 0; i < array_len; i++) {
406 if ((s_int == array[i][0]) && (f == array[i][1])) {
415 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
416 const struct scope_config *config,
417 struct scope_state *state)
420 char command[MAX_COMMAND_SIZE];
423 for (i = 0; i < config->analog_channels; i++) {
424 g_snprintf(command, sizeof(command),
425 (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
428 if (sr_scpi_get_bool(scpi, command,
429 &state->analog_channels[i].state) != SR_OK)
432 g_snprintf(command, sizeof(command),
433 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
436 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
439 if (array_float_get(tmp_str, hmo_vdivs, ARRAY_SIZE(hmo_vdivs),
442 sr_err("Could not determine array index for vertical div scale.");
447 state->analog_channels[i].vdiv = j;
449 g_snprintf(command, sizeof(command),
450 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
453 if (sr_scpi_get_float(scpi, command,
454 &state->analog_channels[i].vertical_offset) != SR_OK)
457 g_snprintf(command, sizeof(command),
458 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
461 if (scope_state_get_array_option(scpi, command, config->coupling_options,
462 &state->analog_channels[i].coupling) != SR_OK)
469 static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
470 const struct scope_config *config,
471 struct scope_state *state)
474 char command[MAX_COMMAND_SIZE];
476 for (i = 0; i < config->digital_channels; i++) {
477 g_snprintf(command, sizeof(command),
478 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
481 if (sr_scpi_get_bool(scpi, command,
482 &state->digital_channels[i]) != SR_OK)
486 for (i = 0; i < config->digital_pods; i++) {
487 g_snprintf(command, sizeof(command),
488 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
491 if (sr_scpi_get_bool(scpi, command,
492 &state->digital_pods[i]) != SR_OK)
499 SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
501 struct dev_context *devc;
502 struct scope_state *state;
503 const struct scope_config *config;
508 gboolean channel_found;
509 char tmp_str[MAX_COMMAND_SIZE];
513 config = devc->model_config;
514 state = devc->model_state;
515 channel_found = FALSE;
517 for (i = 0; i < config->analog_channels; i++) {
518 if (state->analog_channels[i].state) {
519 g_snprintf(chan_name, sizeof(chan_name), "CHAN%d", i + 1);
520 g_snprintf(tmp_str, sizeof(tmp_str),
521 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
523 channel_found = TRUE;
528 if (!channel_found) {
529 for (i = 0; i < config->digital_pods; i++) {
530 if (state->digital_pods[i]) {
531 g_snprintf(chan_name, sizeof(chan_name), "POD%d", i);
532 g_snprintf(tmp_str, sizeof(tmp_str),
533 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
535 channel_found = TRUE;
541 /* No channel is active, ask the instrument for the sample rate
542 * in single shot mode */
543 if (!channel_found) {
544 if (sr_scpi_get_float(sdi->conn,
545 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE],
546 &tmp_float) != SR_OK)
549 state->sample_rate = tmp_float;
551 if (sr_scpi_get_int(sdi->conn, tmp_str, &tmp) != SR_OK)
553 state->sample_rate = tmp / (((float) (*config->timebases)[state->timebase][0] /
554 (*config->timebases)[state->timebase][1]) *
561 SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
563 struct dev_context *devc;
564 struct scope_state *state;
565 const struct scope_config *config;
571 config = devc->model_config;
572 state = devc->model_state;
574 sr_info("Fetching scope state");
576 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
579 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
582 if (sr_scpi_get_float(sdi->conn,
583 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
584 &tmp_float) != SR_OK)
587 if (sr_scpi_get_string(sdi->conn,
588 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
592 if (array_float_get(tmp_str, hmo_timebases, ARRAY_SIZE(hmo_timebases),
595 sr_err("Could not determine array index for time base.");
601 if (sr_scpi_get_float(sdi->conn,
602 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
603 &tmp_float) != SR_OK)
605 state->horiz_triggerpos = tmp_float /
606 (((double) (*config->timebases)[state->timebase][0] /
607 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
608 state->horiz_triggerpos -= 0.5;
609 state->horiz_triggerpos *= -1;
611 if (scope_state_get_array_option(sdi->conn,
612 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
613 config->trigger_sources, &state->trigger_source) != SR_OK)
616 if (scope_state_get_array_option(sdi->conn,
617 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
618 config->trigger_slopes, &state->trigger_slope) != SR_OK)
621 if (hmo_update_sample_rate(sdi) != SR_OK)
624 sr_info("Fetching finished.");
626 scope_state_dump(config, state);
631 static struct scope_state *scope_state_new(const struct scope_config *config)
633 struct scope_state *state;
635 state = g_malloc0(sizeof(struct scope_state));
636 state->analog_channels = g_malloc0_n(config->analog_channels,
637 sizeof(struct analog_channel_state));
638 state->digital_channels = g_malloc0_n(
639 config->digital_channels, sizeof(gboolean));
640 state->digital_pods = g_malloc0_n(config->digital_pods,
646 SR_PRIV void hmo_scope_state_free(struct scope_state *state)
648 g_free(state->analog_channels);
649 g_free(state->digital_channels);
650 g_free(state->digital_pods);
654 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
659 struct sr_channel *ch;
660 struct dev_context *devc;
665 /* Find the exact model. */
666 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
667 for (j = 0; scope_models[i].name[j]; j++) {
668 if (!strcmp(sdi->model, scope_models[i].name[j])) {
673 if (model_index != -1)
677 if (model_index == -1) {
678 sr_dbg("Unsupported HMO device.");
682 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
683 scope_models[model_index].analog_channels);
685 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
686 scope_models[model_index].digital_pods);
688 /* Add analog channels. */
689 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
690 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
691 (*scope_models[model_index].analog_names)[i]);
693 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
695 devc->analog_groups[i]->name = g_strdup(
696 (char *)(*scope_models[model_index].analog_names)[i]);
697 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
699 sdi->channel_groups = g_slist_append(sdi->channel_groups,
700 devc->analog_groups[i]);
703 /* Add digital channel groups. */
704 for (i = 0; i < scope_models[model_index].digital_pods; i++) {
705 g_snprintf(tmp, 25, "POD%d", i);
707 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
709 devc->digital_groups[i]->name = g_strdup(tmp);
710 sdi->channel_groups = g_slist_append(sdi->channel_groups,
711 devc->digital_groups[i < 8 ? 0 : 1]);
714 /* Add digital channels. */
715 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
716 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
717 (*scope_models[model_index].digital_names)[i]);
719 devc->digital_groups[i < 8 ? 0 : 1]->channels = g_slist_append(
720 devc->digital_groups[i < 8 ? 0 : 1]->channels, ch);
723 devc->model_config = &scope_models[model_index];
724 devc->frame_limit = 0;
726 if (!(devc->model_state = scope_state_new(devc->model_config)))
727 return SR_ERR_MALLOC;
732 SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
734 struct sr_channel *ch;
735 struct sr_dev_inst *sdi;
736 struct dev_context *devc;
737 struct sr_datafeed_packet packet;
739 struct sr_datafeed_analog_old analog;
740 struct sr_datafeed_logic logic;
747 if (!(sdi = cb_data))
750 if (!(devc = sdi->priv))
753 /* Although this is correct in general, the USBTMC libusb implementation
754 * currently does not generate an event prior to the first read. Often
755 * it is ok to start reading just after the 50ms timeout. See bug #785.
756 if (revents != G_IO_IN)
760 ch = devc->current_channel->data;
763 case SR_CHANNEL_ANALOG:
764 if (sr_scpi_get_floatv(sdi->conn, NULL, &data) != SR_OK) {
766 g_array_free(data, TRUE);
771 packet.type = SR_DF_FRAME_BEGIN;
772 sr_session_send(sdi, &packet);
774 analog.channels = g_slist_append(NULL, ch);
775 analog.num_samples = data->len;
776 analog.data = (float *) data->data;
777 analog.mq = SR_MQ_VOLTAGE;
778 analog.unit = SR_UNIT_VOLT;
780 packet.type = SR_DF_ANALOG_OLD;
781 packet.payload = &analog;
782 sr_session_send(sdi, &packet);
783 g_slist_free(analog.channels);
784 g_array_free(data, TRUE);
787 case SR_CHANNEL_LOGIC:
788 if (sr_scpi_get_uint8v(sdi->conn, NULL, &data) != SR_OK) {
793 packet.type = SR_DF_FRAME_BEGIN;
794 sr_session_send(sdi, &packet);
796 logic.length = data->len;
798 logic.data = data->data;
799 packet.type = SR_DF_LOGIC;
800 packet.payload = &logic;
801 sr_session_send(sdi, &packet);
802 g_array_free(data, TRUE);
806 sr_err("Invalid channel type.");
810 packet.type = SR_DF_FRAME_END;
811 sr_session_send(sdi, &packet);
813 if (devc->current_channel->next) {
814 devc->current_channel = devc->current_channel->next;
815 hmo_request_data(sdi);
816 } else if (++devc->num_frames == devc->frame_limit) {
817 sdi->driver->dev_acquisition_stop(sdi);
819 devc->current_channel = devc->enabled_channels;
820 hmo_request_data(sdi);