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] = {
175 static const char *scope_analog_channel_names[] = {
182 static const char *scope_digital_channel_names[] = {
201 static const struct scope_config scope_models[] = {
203 /* HMO2522/3032/3042/3052 support 16 digital channels but they're not supported yet. */
204 .name = {"HMO722", "HMO1022", "HMO1522", "HMO2022", "HMO2522",
205 "HMO3032", "HMO3042", "HMO3052", NULL},
206 .analog_channels = 2,
207 .digital_channels = 8,
210 .analog_names = &scope_analog_channel_names,
211 .digital_names = &scope_digital_channel_names,
213 .devopts = &hmo_devopts,
214 .num_devopts = ARRAY_SIZE(hmo_devopts),
216 .analog_devopts = &hmo_analog_devopts,
217 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
219 .coupling_options = &hmo_coupling_options,
220 .trigger_sources = &hmo_compact2_trigger_sources,
221 .trigger_slopes = &scope_trigger_slopes,
223 .timebases = &hmo_timebases,
224 .num_timebases = ARRAY_SIZE(hmo_timebases),
227 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
232 .scpi_dialect = &hameg_scpi_dialect,
235 /* HMO2524/3034/3044/3054 support 16 digital channels but they're not supported yet. */
236 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", "HMO2524",
237 "HMO3034", "HMO3044", "HMO3054", NULL},
238 .analog_channels = 4,
239 .digital_channels = 8,
242 .analog_names = &scope_analog_channel_names,
243 .digital_names = &scope_digital_channel_names,
245 .devopts = &hmo_devopts,
246 .num_devopts = ARRAY_SIZE(hmo_devopts),
248 .analog_devopts = &hmo_analog_devopts,
249 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
251 .coupling_options = &hmo_coupling_options,
252 .trigger_sources = &hmo_compact4_trigger_sources,
253 .trigger_slopes = &scope_trigger_slopes,
255 .timebases = &hmo_timebases,
256 .num_timebases = ARRAY_SIZE(hmo_timebases),
259 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
264 .scpi_dialect = &hameg_scpi_dialect,
268 static void scope_state_dump(const struct scope_config *config,
269 struct scope_state *state)
274 for (i = 0; i < config->analog_channels; i++) {
275 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
276 (*config->vdivs)[state->analog_channels[i].vdiv][1]);
277 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
278 i + 1, state->analog_channels[i].state ? "On" : "Off",
279 (*config->coupling_options)[state->analog_channels[i].coupling],
280 tmp, state->analog_channels[i].vertical_offset);
283 for (i = 0; i < config->digital_channels; i++) {
284 sr_info("State of digital channel %d -> %s", i,
285 state->digital_channels[i] ? "On" : "Off");
288 for (i = 0; i < config->digital_pods; i++) {
289 sr_info("State of digital POD %d -> %s", i,
290 state->digital_pods[i] ? "On" : "Off");
293 tmp = sr_period_string((*config->timebases)[state->timebase][0] *
294 (*config->timebases)[state->timebase][1]);
295 sr_info("Current timebase: %s", tmp);
298 tmp = sr_samplerate_string(state->sample_rate);
299 sr_info("Current samplerate: %s", tmp);
302 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
303 (*config->trigger_sources)[state->trigger_source],
304 (*config->trigger_slopes)[state->trigger_slope],
305 state->horiz_triggerpos);
308 static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
309 const char *command, const char *(*array)[], int *result)
314 if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK) {
319 for (i = 0; (*array)[i]; i++) {
320 if (!g_strcmp0(tmp, (*array)[i])) {
337 * This function takes a value of the form "2.000E-03", converts it to a
338 * significand / factor pair and returns the index of an array where
339 * a matching pair was found.
341 * It's a bit convoluted because of floating-point issues. The value "10.00E-09"
342 * is parsed by g_ascii_strtod() as 0.000000009999999939, for example.
343 * Therefore it's easier to break the number up into two strings and handle
346 * @param value The string to be parsed.
347 * @param array The array of s/f pairs.
348 * @param array_len The number of pairs in the array.
349 * @param result The index at which a matching pair was found.
351 * @return SR_ERR on any parsing error, SR_OK otherwise.
353 static int array_float_get(gchar *value, const uint64_t array[][2],
354 int array_len, unsigned int *result)
360 gchar ss[10], es[10];
362 memset(ss, 0, sizeof(ss));
363 memset(es, 0, sizeof(es));
365 strncpy(ss, value, 5);
366 strncpy(es, &(value[6]), 3);
368 if (sr_atof_ascii(ss, &s) != SR_OK)
370 if (sr_atoi(es, &i) != SR_OK)
373 /* Transform e.g. 10^-03 to 1000 as the array stores the inverse. */
377 * Adjust the significand/factor pair to make sure
378 * that f is a multiple of 1000.
380 while ((int)fmod(log10(f), 3) > 0) {
385 /* Truncate s to circumvent rounding errors. */
386 s_int = (unsigned int)s;
388 for (i = 0; i < array_len; i++) {
389 if ((s_int == array[i][0]) && (f == array[i][1])) {
398 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
399 const struct scope_config *config,
400 struct scope_state *state)
403 char command[MAX_COMMAND_SIZE];
406 for (i = 0; i < config->analog_channels; i++) {
407 g_snprintf(command, sizeof(command),
408 (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
411 if (sr_scpi_get_bool(scpi, command,
412 &state->analog_channels[i].state) != SR_OK)
415 g_snprintf(command, sizeof(command),
416 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
419 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
422 if (array_float_get(tmp_str, hmo_vdivs, ARRAY_SIZE(hmo_vdivs),
425 sr_err("Could not determine array index for vertical div scale.");
430 state->analog_channels[i].vdiv = j;
432 g_snprintf(command, sizeof(command),
433 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
436 if (sr_scpi_get_float(scpi, command,
437 &state->analog_channels[i].vertical_offset) != SR_OK)
440 g_snprintf(command, sizeof(command),
441 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
444 if (scope_state_get_array_option(scpi, command, config->coupling_options,
445 &state->analog_channels[i].coupling) != SR_OK)
452 static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
453 const struct scope_config *config,
454 struct scope_state *state)
457 char command[MAX_COMMAND_SIZE];
459 for (i = 0; i < config->digital_channels; i++) {
460 g_snprintf(command, sizeof(command),
461 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
464 if (sr_scpi_get_bool(scpi, command,
465 &state->digital_channels[i]) != SR_OK)
469 for (i = 0; i < config->digital_pods; i++) {
470 g_snprintf(command, sizeof(command),
471 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
474 if (sr_scpi_get_bool(scpi, command,
475 &state->digital_pods[i]) != SR_OK)
482 SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
484 struct dev_context *devc;
485 struct scope_state *state;
486 const struct scope_config *config;
491 gboolean channel_found;
492 char tmp_str[MAX_COMMAND_SIZE];
496 config = devc->model_config;
497 state = devc->model_state;
498 channel_found = FALSE;
500 for (i = 0; i < config->analog_channels; i++) {
501 if (state->analog_channels[i].state) {
502 g_snprintf(chan_name, sizeof(chan_name), "CHAN%d", i + 1);
503 g_snprintf(tmp_str, sizeof(tmp_str),
504 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
506 channel_found = TRUE;
511 if (!channel_found) {
512 for (i = 0; i < config->digital_pods; i++) {
513 if (state->digital_pods[i]) {
514 g_snprintf(chan_name, sizeof(chan_name), "POD%d", i);
515 g_snprintf(tmp_str, sizeof(tmp_str),
516 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
518 channel_found = TRUE;
524 /* No channel is active, ask the instrument for the sample rate
525 * in single shot mode */
526 if (!channel_found) {
527 if (sr_scpi_get_float(sdi->conn,
528 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE],
529 &tmp_float) != SR_OK)
532 state->sample_rate = tmp_float;
534 if (sr_scpi_get_int(sdi->conn, tmp_str, &tmp) != SR_OK)
536 state->sample_rate = tmp / (((float) (*config->timebases)[state->timebase][0] /
537 (*config->timebases)[state->timebase][1]) *
544 SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
546 struct dev_context *devc;
547 struct scope_state *state;
548 const struct scope_config *config;
553 config = devc->model_config;
554 state = devc->model_state;
556 sr_info("Fetching scope state");
558 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
561 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
564 if (sr_scpi_get_float(sdi->conn,
565 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
566 &tmp_float) != SR_OK)
569 for (i = 0; i < config->num_timebases; i++) {
570 if (tmp_float == ((float) (*config->timebases)[i][0] /
571 (*config->timebases)[i][1])) {
576 if (i == config->num_timebases) {
577 sr_err("Could not determine array index for time base.");
581 if (sr_scpi_get_float(sdi->conn,
582 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
583 &tmp_float) != SR_OK)
585 state->horiz_triggerpos = tmp_float /
586 (((double) (*config->timebases)[state->timebase][0] /
587 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
588 state->horiz_triggerpos -= 0.5;
589 state->horiz_triggerpos *= -1;
591 if (scope_state_get_array_option(sdi->conn,
592 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
593 config->trigger_sources, &state->trigger_source) != SR_OK)
596 if (scope_state_get_array_option(sdi->conn,
597 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
598 config->trigger_slopes, &state->trigger_slope) != SR_OK)
601 if (hmo_update_sample_rate(sdi) != SR_OK)
604 sr_info("Fetching finished.");
606 scope_state_dump(config, state);
611 static struct scope_state *scope_state_new(const struct scope_config *config)
613 struct scope_state *state;
615 state = g_malloc0(sizeof(struct scope_state));
616 state->analog_channels = g_malloc0_n(config->analog_channels,
617 sizeof(struct analog_channel_state));
618 state->digital_channels = g_malloc0_n(
619 config->digital_channels, sizeof(gboolean));
620 state->digital_pods = g_malloc0_n(config->digital_pods,
626 SR_PRIV void hmo_scope_state_free(struct scope_state *state)
628 g_free(state->analog_channels);
629 g_free(state->digital_channels);
630 g_free(state->digital_pods);
634 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
639 struct sr_channel *ch;
640 struct dev_context *devc;
645 /* Find the exact model. */
646 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
647 for (j = 0; scope_models[i].name[j]; j++) {
648 if (!strcmp(sdi->model, scope_models[i].name[j])) {
653 if (model_index != -1)
657 if (model_index == -1) {
658 sr_dbg("Unsupported HMO device.");
662 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
663 scope_models[model_index].analog_channels);
665 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
666 scope_models[model_index].digital_pods);
668 /* Add analog channels. */
669 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
670 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
671 (*scope_models[model_index].analog_names)[i]);
673 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
675 devc->analog_groups[i]->name = g_strdup(
676 (char *)(*scope_models[model_index].analog_names)[i]);
677 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
679 sdi->channel_groups = g_slist_append(sdi->channel_groups,
680 devc->analog_groups[i]);
683 /* Add digital channel groups. */
684 for (i = 0; i < scope_models[model_index].digital_pods; i++) {
685 g_snprintf(tmp, 25, "POD%d", i);
687 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
689 devc->digital_groups[i]->name = g_strdup(tmp);
690 sdi->channel_groups = g_slist_append(sdi->channel_groups,
691 devc->digital_groups[i < 8 ? 0 : 1]);
694 /* Add digital channels. */
695 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
696 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
697 (*scope_models[model_index].digital_names)[i]);
699 devc->digital_groups[i < 8 ? 0 : 1]->channels = g_slist_append(
700 devc->digital_groups[i < 8 ? 0 : 1]->channels, ch);
703 devc->model_config = &scope_models[model_index];
704 devc->frame_limit = 0;
706 if (!(devc->model_state = scope_state_new(devc->model_config)))
707 return SR_ERR_MALLOC;
712 SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
714 struct sr_channel *ch;
715 struct sr_dev_inst *sdi;
716 struct dev_context *devc;
717 struct sr_datafeed_packet packet;
719 struct sr_datafeed_analog_old analog;
720 struct sr_datafeed_logic logic;
726 if (!(sdi = cb_data))
729 if (!(devc = sdi->priv))
732 if (revents != G_IO_IN)
735 ch = devc->current_channel->data;
738 case SR_CHANNEL_ANALOG:
739 if (sr_scpi_get_floatv(sdi->conn, NULL, &data) != SR_OK) {
741 g_array_free(data, TRUE);
746 packet.type = SR_DF_FRAME_BEGIN;
747 sr_session_send(sdi, &packet);
749 analog.channels = g_slist_append(NULL, ch);
750 analog.num_samples = data->len;
751 analog.data = (float *) data->data;
752 analog.mq = SR_MQ_VOLTAGE;
753 analog.unit = SR_UNIT_VOLT;
755 packet.type = SR_DF_ANALOG_OLD;
756 packet.payload = &analog;
757 sr_session_send(cb_data, &packet);
758 g_slist_free(analog.channels);
759 g_array_free(data, TRUE);
762 case SR_CHANNEL_LOGIC:
763 if (sr_scpi_get_uint8v(sdi->conn, NULL, &data) != SR_OK) {
768 packet.type = SR_DF_FRAME_BEGIN;
769 sr_session_send(sdi, &packet);
771 logic.length = data->len;
773 logic.data = data->data;
774 packet.type = SR_DF_LOGIC;
775 packet.payload = &logic;
776 sr_session_send(cb_data, &packet);
777 g_array_free(data, TRUE);
781 sr_err("Invalid channel type.");
785 packet.type = SR_DF_FRAME_END;
786 sr_session_send(sdi, &packet);
788 if (devc->current_channel->next) {
789 devc->current_channel = devc->current_channel->next;
790 hmo_request_data(sdi);
791 } else if (++devc->num_frames == devc->frame_limit) {
792 sdi->driver->dev_acquisition_stop(sdi, cb_data);
794 devc->current_channel = devc->enabled_channels;
795 hmo_request_data(sdi);