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[] = {
84 static const char *hmo_compact2_trigger_sources[] = {
100 static const char *hmo_compact4_trigger_sources[] = {
118 static const uint64_t hmo_timebases[][2] = {
157 static const uint64_t hmo_vdivs[][2] = {
177 static const char *scope_analog_channel_names[] = {
184 static const char *scope_digital_channel_names[] = {
203 static const struct scope_config scope_models[] = {
205 /* HMO2522/3032/3042/3052 support 16 digital channels but they're not supported yet. */
206 .name = {"HMO722", "HMO1022", "HMO1522", "HMO2022", "HMO2522",
207 "HMO3032", "HMO3042", "HMO3052", NULL},
208 .analog_channels = 2,
209 .digital_channels = 8,
212 .analog_names = &scope_analog_channel_names,
213 .digital_names = &scope_digital_channel_names,
215 .devopts = &hmo_devopts,
216 .num_devopts = ARRAY_SIZE(hmo_devopts),
218 .analog_devopts = &hmo_analog_devopts,
219 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
221 .coupling_options = &hmo_coupling_options,
222 .trigger_sources = &hmo_compact2_trigger_sources,
223 .trigger_slopes = &scope_trigger_slopes,
225 .timebases = &hmo_timebases,
226 .num_timebases = ARRAY_SIZE(hmo_timebases),
229 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
234 .scpi_dialect = &hameg_scpi_dialect,
237 /* HMO2524/3034/3044/3054 support 16 digital channels but they're not supported yet. */
238 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", "HMO2524",
239 "HMO3034", "HMO3044", "HMO3054", NULL},
240 .analog_channels = 4,
241 .digital_channels = 8,
244 .analog_names = &scope_analog_channel_names,
245 .digital_names = &scope_digital_channel_names,
247 .devopts = &hmo_devopts,
248 .num_devopts = ARRAY_SIZE(hmo_devopts),
250 .analog_devopts = &hmo_analog_devopts,
251 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
253 .coupling_options = &hmo_coupling_options,
254 .trigger_sources = &hmo_compact4_trigger_sources,
255 .trigger_slopes = &scope_trigger_slopes,
257 .timebases = &hmo_timebases,
258 .num_timebases = ARRAY_SIZE(hmo_timebases),
261 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
266 .scpi_dialect = &hameg_scpi_dialect,
270 static void scope_state_dump(const struct scope_config *config,
271 struct scope_state *state)
276 for (i = 0; i < config->analog_channels; i++) {
277 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
278 (*config->vdivs)[state->analog_channels[i].vdiv][1]);
279 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
280 i + 1, state->analog_channels[i].state ? "On" : "Off",
281 (*config->coupling_options)[state->analog_channels[i].coupling],
282 tmp, state->analog_channels[i].vertical_offset);
285 for (i = 0; i < config->digital_channels; i++) {
286 sr_info("State of digital channel %d -> %s", i,
287 state->digital_channels[i] ? "On" : "Off");
290 for (i = 0; i < config->digital_pods; i++) {
291 sr_info("State of digital POD %d -> %s", i,
292 state->digital_pods[i] ? "On" : "Off");
295 tmp = sr_period_string((*config->timebases)[state->timebase][0] *
296 (*config->timebases)[state->timebase][1]);
297 sr_info("Current timebase: %s", tmp);
300 tmp = sr_samplerate_string(state->sample_rate);
301 sr_info("Current samplerate: %s", tmp);
304 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
305 (*config->trigger_sources)[state->trigger_source],
306 (*config->trigger_slopes)[state->trigger_slope],
307 state->horiz_triggerpos);
310 static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
311 const char *command, const char *(*array)[], int *result)
316 if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK) {
321 for (i = 0; (*array)[i]; i++) {
322 if (!g_strcmp0(tmp, (*array)[i])) {
339 * This function takes a value of the form "2.000E-03", converts it to a
340 * significand / factor pair and returns the index of an array where
341 * a matching pair was found.
343 * It's a bit convoluted because of floating-point issues. The value "10.00E-09"
344 * is parsed by g_ascii_strtod() as 0.000000009999999939, for example.
345 * Therefore it's easier to break the number up into two strings and handle
348 * @param value The string to be parsed.
349 * @param array The array of s/f pairs.
350 * @param array_len The number of pairs in the array.
351 * @param result The index at which a matching pair was found.
353 * @return SR_ERR on any parsing error, SR_OK otherwise.
355 static int array_float_get(gchar *value, const uint64_t array[][2],
356 int array_len, unsigned int *result)
362 gchar ss[10], es[10];
364 memset(ss, 0, sizeof(ss));
365 memset(es, 0, sizeof(es));
367 strncpy(ss, value, 5);
368 strncpy(es, &(value[6]), 3);
370 if (sr_atof_ascii(ss, &s) != SR_OK)
372 if (sr_atoi(es, &i) != SR_OK)
375 /* Transform e.g. 10^-03 to 1000 as the array stores the inverse. */
379 * Adjust the significand/factor pair to make sure
380 * that f is a multiple of 1000.
382 while ((int)fmod(log10(f), 3) > 0) {
387 /* Truncate s to circumvent rounding errors. */
388 s_int = (unsigned int)s;
390 for (i = 0; i < array_len; i++) {
391 if ((s_int == array[i][0]) && (f == array[i][1])) {
400 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
401 const struct scope_config *config,
402 struct scope_state *state)
405 char command[MAX_COMMAND_SIZE];
408 for (i = 0; i < config->analog_channels; i++) {
409 g_snprintf(command, sizeof(command),
410 (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
413 if (sr_scpi_get_bool(scpi, command,
414 &state->analog_channels[i].state) != SR_OK)
417 g_snprintf(command, sizeof(command),
418 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
421 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
424 if (array_float_get(tmp_str, hmo_vdivs, ARRAY_SIZE(hmo_vdivs),
427 sr_err("Could not determine array index for vertical div scale.");
432 state->analog_channels[i].vdiv = j;
434 g_snprintf(command, sizeof(command),
435 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
438 if (sr_scpi_get_float(scpi, command,
439 &state->analog_channels[i].vertical_offset) != SR_OK)
442 g_snprintf(command, sizeof(command),
443 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
446 if (scope_state_get_array_option(scpi, command, config->coupling_options,
447 &state->analog_channels[i].coupling) != SR_OK)
454 static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
455 const struct scope_config *config,
456 struct scope_state *state)
459 char command[MAX_COMMAND_SIZE];
461 for (i = 0; i < config->digital_channels; i++) {
462 g_snprintf(command, sizeof(command),
463 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
466 if (sr_scpi_get_bool(scpi, command,
467 &state->digital_channels[i]) != SR_OK)
471 for (i = 0; i < config->digital_pods; i++) {
472 g_snprintf(command, sizeof(command),
473 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
476 if (sr_scpi_get_bool(scpi, command,
477 &state->digital_pods[i]) != SR_OK)
484 SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
486 struct dev_context *devc;
487 struct scope_state *state;
488 const struct scope_config *config;
493 gboolean channel_found;
494 char tmp_str[MAX_COMMAND_SIZE];
498 config = devc->model_config;
499 state = devc->model_state;
500 channel_found = FALSE;
502 for (i = 0; i < config->analog_channels; i++) {
503 if (state->analog_channels[i].state) {
504 g_snprintf(chan_name, sizeof(chan_name), "CHAN%d", i + 1);
505 g_snprintf(tmp_str, sizeof(tmp_str),
506 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
508 channel_found = TRUE;
513 if (!channel_found) {
514 for (i = 0; i < config->digital_pods; i++) {
515 if (state->digital_pods[i]) {
516 g_snprintf(chan_name, sizeof(chan_name), "POD%d", i);
517 g_snprintf(tmp_str, sizeof(tmp_str),
518 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
520 channel_found = TRUE;
526 /* No channel is active, ask the instrument for the sample rate
527 * in single shot mode */
528 if (!channel_found) {
529 if (sr_scpi_get_float(sdi->conn,
530 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE],
531 &tmp_float) != SR_OK)
534 state->sample_rate = tmp_float;
536 if (sr_scpi_get_int(sdi->conn, tmp_str, &tmp) != SR_OK)
538 state->sample_rate = tmp / (((float) (*config->timebases)[state->timebase][0] /
539 (*config->timebases)[state->timebase][1]) *
546 SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
548 struct dev_context *devc;
549 struct scope_state *state;
550 const struct scope_config *config;
555 config = devc->model_config;
556 state = devc->model_state;
558 sr_info("Fetching scope state");
560 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
563 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
566 if (sr_scpi_get_float(sdi->conn,
567 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
568 &tmp_float) != SR_OK)
571 for (i = 0; i < config->num_timebases; i++) {
572 if (tmp_float == ((float) (*config->timebases)[i][0] /
573 (*config->timebases)[i][1])) {
578 if (i == config->num_timebases) {
579 sr_err("Could not determine array index for time base.");
583 if (sr_scpi_get_float(sdi->conn,
584 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
585 &tmp_float) != SR_OK)
587 state->horiz_triggerpos = tmp_float /
588 (((double) (*config->timebases)[state->timebase][0] /
589 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
590 state->horiz_triggerpos -= 0.5;
591 state->horiz_triggerpos *= -1;
593 if (scope_state_get_array_option(sdi->conn,
594 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
595 config->trigger_sources, &state->trigger_source) != SR_OK)
598 if (scope_state_get_array_option(sdi->conn,
599 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
600 config->trigger_slopes, &state->trigger_slope) != SR_OK)
603 if (hmo_update_sample_rate(sdi) != SR_OK)
606 sr_info("Fetching finished.");
608 scope_state_dump(config, state);
613 static struct scope_state *scope_state_new(const struct scope_config *config)
615 struct scope_state *state;
617 state = g_malloc0(sizeof(struct scope_state));
618 state->analog_channels = g_malloc0_n(config->analog_channels,
619 sizeof(struct analog_channel_state));
620 state->digital_channels = g_malloc0_n(
621 config->digital_channels, sizeof(gboolean));
622 state->digital_pods = g_malloc0_n(config->digital_pods,
628 SR_PRIV void hmo_scope_state_free(struct scope_state *state)
630 g_free(state->analog_channels);
631 g_free(state->digital_channels);
632 g_free(state->digital_pods);
636 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
641 struct sr_channel *ch;
642 struct dev_context *devc;
647 /* Find the exact model. */
648 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
649 for (j = 0; scope_models[i].name[j]; j++) {
650 if (!strcmp(sdi->model, scope_models[i].name[j])) {
655 if (model_index != -1)
659 if (model_index == -1) {
660 sr_dbg("Unsupported HMO device.");
664 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
665 scope_models[model_index].analog_channels);
667 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
668 scope_models[model_index].digital_pods);
670 /* Add analog channels. */
671 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
672 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
673 (*scope_models[model_index].analog_names)[i]);
675 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
677 devc->analog_groups[i]->name = g_strdup(
678 (char *)(*scope_models[model_index].analog_names)[i]);
679 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
681 sdi->channel_groups = g_slist_append(sdi->channel_groups,
682 devc->analog_groups[i]);
685 /* Add digital channel groups. */
686 for (i = 0; i < scope_models[model_index].digital_pods; i++) {
687 g_snprintf(tmp, 25, "POD%d", i);
689 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
691 devc->digital_groups[i]->name = g_strdup(tmp);
692 sdi->channel_groups = g_slist_append(sdi->channel_groups,
693 devc->digital_groups[i < 8 ? 0 : 1]);
696 /* Add digital channels. */
697 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
698 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
699 (*scope_models[model_index].digital_names)[i]);
701 devc->digital_groups[i < 8 ? 0 : 1]->channels = g_slist_append(
702 devc->digital_groups[i < 8 ? 0 : 1]->channels, ch);
705 devc->model_config = &scope_models[model_index];
706 devc->frame_limit = 0;
708 if (!(devc->model_state = scope_state_new(devc->model_config)))
709 return SR_ERR_MALLOC;
714 SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
716 struct sr_channel *ch;
717 struct sr_dev_inst *sdi;
718 struct dev_context *devc;
719 struct sr_datafeed_packet packet;
721 struct sr_datafeed_analog_old analog;
722 struct sr_datafeed_logic logic;
728 if (!(sdi = cb_data))
731 if (!(devc = sdi->priv))
734 if (revents != G_IO_IN)
737 ch = devc->current_channel->data;
740 case SR_CHANNEL_ANALOG:
741 if (sr_scpi_get_floatv(sdi->conn, NULL, &data) != SR_OK) {
743 g_array_free(data, TRUE);
748 packet.type = SR_DF_FRAME_BEGIN;
749 sr_session_send(sdi, &packet);
751 analog.channels = g_slist_append(NULL, ch);
752 analog.num_samples = data->len;
753 analog.data = (float *) data->data;
754 analog.mq = SR_MQ_VOLTAGE;
755 analog.unit = SR_UNIT_VOLT;
757 packet.type = SR_DF_ANALOG_OLD;
758 packet.payload = &analog;
759 sr_session_send(cb_data, &packet);
760 g_slist_free(analog.channels);
761 g_array_free(data, TRUE);
764 case SR_CHANNEL_LOGIC:
765 if (sr_scpi_get_uint8v(sdi->conn, NULL, &data) != SR_OK) {
770 packet.type = SR_DF_FRAME_BEGIN;
771 sr_session_send(sdi, &packet);
773 logic.length = data->len;
775 logic.data = data->data;
776 packet.type = SR_DF_LOGIC;
777 packet.payload = &logic;
778 sr_session_send(cb_data, &packet);
779 g_array_free(data, TRUE);
783 sr_err("Invalid channel type.");
787 packet.type = SR_DF_FRAME_END;
788 sr_session_send(sdi, &packet);
790 if (devc->current_channel->next) {
791 devc->current_channel = devc->current_channel->next;
792 hmo_request_data(sdi);
793 } else if (++devc->num_frames == devc->frame_limit) {
794 sdi->driver->dev_acquisition_stop(sdi, cb_data);
796 devc->current_channel = devc->enabled_channels;
797 hmo_request_data(sdi);