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 = {"HMO1002", "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)
363 gchar ss[10], es[10];
365 memset(ss, 0, sizeof(ss));
366 memset(es, 0, sizeof(es));
368 /* Get index of the separating 'E' character and break up the string. */
369 pos = strcspn(value, "E");
371 strncpy(ss, value, pos);
372 strncpy(es, &(value[pos+1]), 3);
374 if (sr_atof_ascii(ss, &s) != SR_OK)
376 if (sr_atoi(es, &e) != SR_OK)
379 /* Transform e.g. 10^-03 to 1000 as the array stores the inverse. */
383 * Adjust the significand/factor pair to make sure
384 * that f is a multiple of 1000.
386 while ((int)fmod(log10(f), 3) > 0) {
395 /* Truncate s to circumvent rounding errors. */
396 s_int = (unsigned int)s;
398 for (i = 0; i < array_len; i++) {
399 if ((s_int == array[i][0]) && (f == array[i][1])) {
408 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
409 const struct scope_config *config,
410 struct scope_state *state)
413 char command[MAX_COMMAND_SIZE];
416 for (i = 0; i < config->analog_channels; i++) {
417 g_snprintf(command, sizeof(command),
418 (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
421 if (sr_scpi_get_bool(scpi, command,
422 &state->analog_channels[i].state) != SR_OK)
425 g_snprintf(command, sizeof(command),
426 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
429 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
432 if (array_float_get(tmp_str, hmo_vdivs, ARRAY_SIZE(hmo_vdivs),
435 sr_err("Could not determine array index for vertical div scale.");
440 state->analog_channels[i].vdiv = j;
442 g_snprintf(command, sizeof(command),
443 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
446 if (sr_scpi_get_float(scpi, command,
447 &state->analog_channels[i].vertical_offset) != SR_OK)
450 g_snprintf(command, sizeof(command),
451 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
454 if (scope_state_get_array_option(scpi, command, config->coupling_options,
455 &state->analog_channels[i].coupling) != SR_OK)
462 static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
463 const struct scope_config *config,
464 struct scope_state *state)
467 char command[MAX_COMMAND_SIZE];
469 for (i = 0; i < config->digital_channels; i++) {
470 g_snprintf(command, sizeof(command),
471 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
474 if (sr_scpi_get_bool(scpi, command,
475 &state->digital_channels[i]) != SR_OK)
479 for (i = 0; i < config->digital_pods; i++) {
480 g_snprintf(command, sizeof(command),
481 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
484 if (sr_scpi_get_bool(scpi, command,
485 &state->digital_pods[i]) != SR_OK)
492 SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
494 struct dev_context *devc;
495 struct scope_state *state;
496 const struct scope_config *config;
501 gboolean channel_found;
502 char tmp_str[MAX_COMMAND_SIZE];
506 config = devc->model_config;
507 state = devc->model_state;
508 channel_found = FALSE;
510 for (i = 0; i < config->analog_channels; i++) {
511 if (state->analog_channels[i].state) {
512 g_snprintf(chan_name, sizeof(chan_name), "CHAN%d", i + 1);
513 g_snprintf(tmp_str, sizeof(tmp_str),
514 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
516 channel_found = TRUE;
521 if (!channel_found) {
522 for (i = 0; i < config->digital_pods; i++) {
523 if (state->digital_pods[i]) {
524 g_snprintf(chan_name, sizeof(chan_name), "POD%d", i);
525 g_snprintf(tmp_str, sizeof(tmp_str),
526 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
528 channel_found = TRUE;
534 /* No channel is active, ask the instrument for the sample rate
535 * in single shot mode */
536 if (!channel_found) {
537 if (sr_scpi_get_float(sdi->conn,
538 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE],
539 &tmp_float) != SR_OK)
542 state->sample_rate = tmp_float;
544 if (sr_scpi_get_int(sdi->conn, tmp_str, &tmp) != SR_OK)
546 state->sample_rate = tmp / (((float) (*config->timebases)[state->timebase][0] /
547 (*config->timebases)[state->timebase][1]) *
554 SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
556 struct dev_context *devc;
557 struct scope_state *state;
558 const struct scope_config *config;
564 config = devc->model_config;
565 state = devc->model_state;
567 sr_info("Fetching scope state");
569 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
572 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
575 if (sr_scpi_get_float(sdi->conn,
576 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
577 &tmp_float) != SR_OK)
580 if (sr_scpi_get_string(sdi->conn,
581 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
585 if (array_float_get(tmp_str, hmo_timebases, ARRAY_SIZE(hmo_timebases),
588 sr_err("Could not determine array index for time base.");
594 if (sr_scpi_get_float(sdi->conn,
595 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
596 &tmp_float) != SR_OK)
598 state->horiz_triggerpos = tmp_float /
599 (((double) (*config->timebases)[state->timebase][0] /
600 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
601 state->horiz_triggerpos -= 0.5;
602 state->horiz_triggerpos *= -1;
604 if (scope_state_get_array_option(sdi->conn,
605 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
606 config->trigger_sources, &state->trigger_source) != SR_OK)
609 if (scope_state_get_array_option(sdi->conn,
610 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
611 config->trigger_slopes, &state->trigger_slope) != SR_OK)
614 if (hmo_update_sample_rate(sdi) != SR_OK)
617 sr_info("Fetching finished.");
619 scope_state_dump(config, state);
624 static struct scope_state *scope_state_new(const struct scope_config *config)
626 struct scope_state *state;
628 state = g_malloc0(sizeof(struct scope_state));
629 state->analog_channels = g_malloc0_n(config->analog_channels,
630 sizeof(struct analog_channel_state));
631 state->digital_channels = g_malloc0_n(
632 config->digital_channels, sizeof(gboolean));
633 state->digital_pods = g_malloc0_n(config->digital_pods,
639 SR_PRIV void hmo_scope_state_free(struct scope_state *state)
641 g_free(state->analog_channels);
642 g_free(state->digital_channels);
643 g_free(state->digital_pods);
647 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
652 struct sr_channel *ch;
653 struct dev_context *devc;
658 /* Find the exact model. */
659 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
660 for (j = 0; scope_models[i].name[j]; j++) {
661 if (!strcmp(sdi->model, scope_models[i].name[j])) {
666 if (model_index != -1)
670 if (model_index == -1) {
671 sr_dbg("Unsupported HMO device.");
675 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
676 scope_models[model_index].analog_channels);
678 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
679 scope_models[model_index].digital_pods);
681 /* Add analog channels. */
682 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
683 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
684 (*scope_models[model_index].analog_names)[i]);
686 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
688 devc->analog_groups[i]->name = g_strdup(
689 (char *)(*scope_models[model_index].analog_names)[i]);
690 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
692 sdi->channel_groups = g_slist_append(sdi->channel_groups,
693 devc->analog_groups[i]);
696 /* Add digital channel groups. */
697 for (i = 0; i < scope_models[model_index].digital_pods; i++) {
698 g_snprintf(tmp, 25, "POD%d", i);
700 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
702 devc->digital_groups[i]->name = g_strdup(tmp);
703 sdi->channel_groups = g_slist_append(sdi->channel_groups,
704 devc->digital_groups[i < 8 ? 0 : 1]);
707 /* Add digital channels. */
708 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
709 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
710 (*scope_models[model_index].digital_names)[i]);
712 devc->digital_groups[i < 8 ? 0 : 1]->channels = g_slist_append(
713 devc->digital_groups[i < 8 ? 0 : 1]->channels, ch);
716 devc->model_config = &scope_models[model_index];
717 devc->frame_limit = 0;
719 if (!(devc->model_state = scope_state_new(devc->model_config)))
720 return SR_ERR_MALLOC;
725 SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
727 struct sr_channel *ch;
728 struct sr_dev_inst *sdi;
729 struct dev_context *devc;
730 struct sr_datafeed_packet packet;
732 struct sr_datafeed_analog_old analog;
733 struct sr_datafeed_logic logic;
739 if (!(sdi = cb_data))
742 if (!(devc = sdi->priv))
745 if (revents != G_IO_IN)
748 ch = devc->current_channel->data;
751 case SR_CHANNEL_ANALOG:
752 if (sr_scpi_get_floatv(sdi->conn, NULL, &data) != SR_OK) {
754 g_array_free(data, TRUE);
759 packet.type = SR_DF_FRAME_BEGIN;
760 sr_session_send(sdi, &packet);
762 analog.channels = g_slist_append(NULL, ch);
763 analog.num_samples = data->len;
764 analog.data = (float *) data->data;
765 analog.mq = SR_MQ_VOLTAGE;
766 analog.unit = SR_UNIT_VOLT;
768 packet.type = SR_DF_ANALOG_OLD;
769 packet.payload = &analog;
770 sr_session_send(cb_data, &packet);
771 g_slist_free(analog.channels);
772 g_array_free(data, TRUE);
775 case SR_CHANNEL_LOGIC:
776 if (sr_scpi_get_uint8v(sdi->conn, NULL, &data) != SR_OK) {
781 packet.type = SR_DF_FRAME_BEGIN;
782 sr_session_send(sdi, &packet);
784 logic.length = data->len;
786 logic.data = data->data;
787 packet.type = SR_DF_LOGIC;
788 packet.payload = &logic;
789 sr_session_send(cb_data, &packet);
790 g_array_free(data, TRUE);
794 sr_err("Invalid channel type.");
798 packet.type = SR_DF_FRAME_END;
799 sr_session_send(sdi, &packet);
801 if (devc->current_channel->next) {
802 devc->current_channel = devc->current_channel->next;
803 hmo_request_data(sdi);
804 } else if (++devc->num_frames == devc->frame_limit) {
805 sdi->driver->dev_acquisition_stop(sdi, cb_data);
807 devc->current_channel = devc->enabled_channels;
808 hmo_request_data(sdi);