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/>.
24 static const char *hameg_scpi_dialect[] = {
25 [SCPI_CMD_GET_DIG_DATA] = ":POD%d:DATA?",
26 [SCPI_CMD_GET_TIMEBASE] = ":TIM:SCAL?",
27 [SCPI_CMD_SET_TIMEBASE] = ":TIM:SCAL %s",
28 [SCPI_CMD_GET_COUPLING] = ":CHAN%d:COUP?",
29 [SCPI_CMD_SET_COUPLING] = ":CHAN%d:COUP %s",
30 [SCPI_CMD_GET_SAMPLE_RATE] = ":ACQ:SRAT?",
31 [SCPI_CMD_GET_SAMPLE_RATE_LIVE] = ":%s:DATA:POINTS?",
32 [SCPI_CMD_GET_ANALOG_DATA] = ":CHAN%d:DATA?",
33 [SCPI_CMD_GET_VERTICAL_DIV] = ":CHAN%d:SCAL?",
34 [SCPI_CMD_SET_VERTICAL_DIV] = ":CHAN%d:SCAL %s",
35 [SCPI_CMD_GET_DIG_POD_STATE] = ":POD%d:STAT?",
36 [SCPI_CMD_SET_DIG_POD_STATE] = ":POD%d:STAT %d",
37 [SCPI_CMD_GET_TRIGGER_SLOPE] = ":TRIG:A:EDGE:SLOP?",
38 [SCPI_CMD_SET_TRIGGER_SLOPE] = ":TRIG:A:EDGE:SLOP %s",
39 [SCPI_CMD_GET_TRIGGER_SOURCE] = ":TRIG:A:SOUR?",
40 [SCPI_CMD_SET_TRIGGER_SOURCE] = ":TRIG:A:SOUR %s",
41 [SCPI_CMD_GET_DIG_CHAN_STATE] = ":LOG%d:STAT?",
42 [SCPI_CMD_SET_DIG_CHAN_STATE] = ":LOG%d:STAT %d",
43 [SCPI_CMD_GET_VERTICAL_OFFSET] = ":CHAN%d:POS?",
44 [SCPI_CMD_GET_HORIZ_TRIGGERPOS] = ":TIM:POS?",
45 [SCPI_CMD_SET_HORIZ_TRIGGERPOS] = ":TIM:POS %s",
46 [SCPI_CMD_GET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT?",
47 [SCPI_CMD_SET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT %d",
50 static const uint32_t hmo_devopts[] = {
52 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
53 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
54 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
55 SR_CONF_NUM_HDIV | SR_CONF_GET,
56 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
57 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
58 SR_CONF_SAMPLERATE | SR_CONF_GET,
61 static const uint32_t hmo_analog_devopts[] = {
62 SR_CONF_NUM_VDIV | SR_CONF_GET,
63 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
64 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
67 static const char *hmo_coupling_options[] = {
76 static const char *scope_trigger_slopes[] = {
82 static const char *hmo_compact2_trigger_sources[] = {
98 static const char *hmo_compact4_trigger_sources[] = {
116 static const uint64_t hmo_timebases[][2] = {
155 static const uint64_t hmo_vdivs[][2] = {
173 static const char *scope_analog_channel_names[] = {
180 static const char *scope_digital_channel_names[] = {
199 static const struct scope_config scope_models[] = {
201 .name = {"HMO722", "HMO1022", "HMO1522", "HMO2022", NULL},
202 .analog_channels = 2,
203 .digital_channels = 8,
206 .analog_names = &scope_analog_channel_names,
207 .digital_names = &scope_digital_channel_names,
209 .devopts = &hmo_devopts,
210 .num_devopts = ARRAY_SIZE(hmo_devopts),
212 .analog_devopts = &hmo_analog_devopts,
213 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
215 .coupling_options = &hmo_coupling_options,
216 .trigger_sources = &hmo_compact2_trigger_sources,
217 .trigger_slopes = &scope_trigger_slopes,
219 .timebases = &hmo_timebases,
220 .num_timebases = ARRAY_SIZE(hmo_timebases),
223 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
228 .scpi_dialect = &hameg_scpi_dialect,
231 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", NULL},
232 .analog_channels = 4,
233 .digital_channels = 8,
236 .analog_names = &scope_analog_channel_names,
237 .digital_names = &scope_digital_channel_names,
239 .devopts = &hmo_devopts,
240 .num_devopts = ARRAY_SIZE(hmo_devopts),
242 .analog_devopts = &hmo_analog_devopts,
243 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
245 .coupling_options = &hmo_coupling_options,
246 .trigger_sources = &hmo_compact4_trigger_sources,
247 .trigger_slopes = &scope_trigger_slopes,
249 .timebases = &hmo_timebases,
250 .num_timebases = ARRAY_SIZE(hmo_timebases),
253 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
258 .scpi_dialect = &hameg_scpi_dialect,
262 static void scope_state_dump(const struct scope_config *config,
263 struct scope_state *state)
268 for (i = 0; i < config->analog_channels; i++) {
269 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
270 (*config->vdivs)[state->analog_channels[i].vdiv][1]);
271 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
272 i + 1, state->analog_channels[i].state ? "On" : "Off",
273 (*config->coupling_options)[state->analog_channels[i].coupling],
274 tmp, state->analog_channels[i].vertical_offset);
277 for (i = 0; i < config->digital_channels; i++) {
278 sr_info("State of digital channel %d -> %s", i,
279 state->digital_channels[i] ? "On" : "Off");
282 for (i = 0; i < config->digital_pods; i++) {
283 sr_info("State of digital POD %d -> %s", i,
284 state->digital_pods[i] ? "On" : "Off");
287 tmp = sr_period_string((*config->timebases)[state->timebase][0] *
288 (*config->timebases)[state->timebase][1]);
289 sr_info("Current timebase: %s", tmp);
292 tmp = sr_samplerate_string(state->sample_rate);
293 sr_info("Current samplerate: %s", tmp);
296 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
297 (*config->trigger_sources)[state->trigger_source],
298 (*config->trigger_slopes)[state->trigger_slope],
299 state->horiz_triggerpos);
302 static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
303 const char *command, const char *(*array)[], int *result)
308 if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK) {
313 for (i = 0; (*array)[i]; i++) {
314 if (!g_strcmp0(tmp, (*array)[i])) {
331 * This function takes a value of the form "2.000E-03", converts it to a
332 * significand / factor pair and returns the index of an array where
333 * a matching pair was found.
335 * It's a bit convoluted because of floating-point issues. The value "10.00E-09"
336 * is parsed by g_ascii_strtod() as 0.000000009999999939, for example.
337 * Therefore it's easier to break the number up into two strings and handle
340 * @param value The string to be parsed.
341 * @param array The array of s/f pairs.
342 * @param array_len The number of pairs in the array.
343 * @param result The index at which a matching pair was found.
345 * @return SR_ERR on any parsing error, SR_OK otherwise.
347 static int array_float_get(gchar *value, const uint64_t array[][2],
348 int array_len, int *result)
354 gchar ss[10], es[10];
356 memset(ss, 0, sizeof(ss));
357 memset(es, 0, sizeof(es));
359 strncpy(ss, value, 5);
360 strncpy(es, &(value[6]), 3);
362 if (sr_atof_ascii(ss, &s) != SR_OK)
364 if (sr_atoi(es, &i) != SR_OK)
367 /* Transform e.g. 10^-03 to 1000 as the array stores the inverse. */
371 * Adjust the significand/factor pair to make sure
372 * that f is a multiple of 1000.
374 while ((int)fmod(log10(f), 3) > 0) {
379 /* Truncate s to circumvent rounding errors. */
380 s_int = (unsigned int)s;
382 for (i = 0; i < array_len; i++) {
383 if ((s_int == array[i][0]) && (f == array[i][1])) {
392 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
393 const struct scope_config *config,
394 struct scope_state *state)
397 char command[MAX_COMMAND_SIZE];
400 for (i = 0; i < config->analog_channels; i++) {
401 g_snprintf(command, sizeof(command),
402 (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
405 if (sr_scpi_get_bool(scpi, command,
406 &state->analog_channels[i].state) != SR_OK)
409 g_snprintf(command, sizeof(command),
410 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
413 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
416 if (array_float_get(tmp_str, hmo_vdivs, ARRAY_SIZE(hmo_vdivs),
419 sr_err("Could not determine array index for vertical div scale.");
424 state->analog_channels[i].vdiv = j;
426 g_snprintf(command, sizeof(command),
427 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
430 if (sr_scpi_get_float(scpi, command,
431 &state->analog_channels[i].vertical_offset) != SR_OK)
434 g_snprintf(command, sizeof(command),
435 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
438 if (scope_state_get_array_option(scpi, command, config->coupling_options,
439 &state->analog_channels[i].coupling) != SR_OK)
446 static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
447 const struct scope_config *config,
448 struct scope_state *state)
451 char command[MAX_COMMAND_SIZE];
453 for (i = 0; i < config->digital_channels; i++) {
454 g_snprintf(command, sizeof(command),
455 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
458 if (sr_scpi_get_bool(scpi, command,
459 &state->digital_channels[i]) != SR_OK)
463 for (i = 0; i < config->digital_pods; i++) {
464 g_snprintf(command, sizeof(command),
465 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
468 if (sr_scpi_get_bool(scpi, command,
469 &state->digital_pods[i]) != SR_OK)
476 SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
478 struct dev_context *devc;
479 struct scope_state *state;
480 const struct scope_config *config;
485 gboolean channel_found;
486 char tmp_str[MAX_COMMAND_SIZE];
490 config = devc->model_config;
491 state = devc->model_state;
492 channel_found = FALSE;
494 for (i = 0; i < config->analog_channels; i++) {
495 if (state->analog_channels[i].state) {
496 g_snprintf(chan_name, sizeof(chan_name), "CHAN%d", i + 1);
497 g_snprintf(tmp_str, sizeof(tmp_str),
498 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
500 channel_found = TRUE;
505 if (!channel_found) {
506 for (i = 0; i < config->digital_pods; i++) {
507 if (state->digital_pods[i]) {
508 g_snprintf(chan_name, sizeof(chan_name), "POD%d", i);
509 g_snprintf(tmp_str, sizeof(tmp_str),
510 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
512 channel_found = TRUE;
518 /* No channel is active, ask the instrument for the sample rate
519 * in single shot mode */
520 if (!channel_found) {
521 if (sr_scpi_get_float(sdi->conn,
522 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE],
523 &tmp_float) != SR_OK)
526 state->sample_rate = tmp_float;
528 if (sr_scpi_get_int(sdi->conn, tmp_str, &tmp) != SR_OK)
530 state->sample_rate = tmp / (((float) (*config->timebases)[state->timebase][0] /
531 (*config->timebases)[state->timebase][1]) *
538 SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
540 struct dev_context *devc;
541 struct scope_state *state;
542 const struct scope_config *config;
547 config = devc->model_config;
548 state = devc->model_state;
550 sr_info("Fetching scope state");
552 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
555 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
558 if (sr_scpi_get_float(sdi->conn,
559 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
560 &tmp_float) != SR_OK)
563 for (i = 0; i < config->num_timebases; i++) {
564 if (tmp_float == ((float) (*config->timebases)[i][0] /
565 (*config->timebases)[i][1])) {
570 if (i == config->num_timebases) {
571 sr_err("Could not determine array index for time base.");
575 if (sr_scpi_get_float(sdi->conn,
576 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
577 &tmp_float) != SR_OK)
579 state->horiz_triggerpos = tmp_float /
580 (((double) (*config->timebases)[state->timebase][0] /
581 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
582 state->horiz_triggerpos -= 0.5;
583 state->horiz_triggerpos *= -1;
585 if (scope_state_get_array_option(sdi->conn,
586 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
587 config->trigger_sources, &state->trigger_source) != SR_OK)
590 if (scope_state_get_array_option(sdi->conn,
591 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
592 config->trigger_slopes, &state->trigger_slope) != SR_OK)
595 if (hmo_update_sample_rate(sdi) != SR_OK)
598 sr_info("Fetching finished.");
600 scope_state_dump(config, state);
605 static struct scope_state *scope_state_new(const struct scope_config *config)
607 struct scope_state *state;
609 state = g_malloc0(sizeof(struct scope_state));
610 state->analog_channels = g_malloc0_n(config->analog_channels,
611 sizeof(struct analog_channel_state));
612 state->digital_channels = g_malloc0_n(
613 config->digital_channels, sizeof(gboolean));
614 state->digital_pods = g_malloc0_n(config->digital_pods,
620 SR_PRIV void hmo_scope_state_free(struct scope_state *state)
622 g_free(state->analog_channels);
623 g_free(state->digital_channels);
624 g_free(state->digital_pods);
628 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
633 struct sr_channel *ch;
634 struct dev_context *devc;
639 /* Find the exact model. */
640 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
641 for (j = 0; scope_models[i].name[j]; j++) {
642 if (!strcmp(sdi->model, scope_models[i].name[j])) {
647 if (model_index != -1)
651 if (model_index == -1) {
652 sr_dbg("Unsupported HMO device.");
656 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
657 scope_models[model_index].analog_channels);
659 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
660 scope_models[model_index].digital_pods);
662 /* Add analog channels. */
663 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
664 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
665 (*scope_models[model_index].analog_names)[i]);
667 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
669 devc->analog_groups[i]->name = g_strdup(
670 (char *)(*scope_models[model_index].analog_names)[i]);
671 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
673 sdi->channel_groups = g_slist_append(sdi->channel_groups,
674 devc->analog_groups[i]);
677 /* Add digital channel groups. */
678 for (i = 0; i < scope_models[model_index].digital_pods; i++) {
679 g_snprintf(tmp, 25, "POD%d", i);
681 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
683 devc->digital_groups[i]->name = g_strdup(tmp);
684 sdi->channel_groups = g_slist_append(sdi->channel_groups,
685 devc->digital_groups[i < 8 ? 0 : 1]);
688 /* Add digital channels. */
689 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
690 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
691 (*scope_models[model_index].digital_names)[i]);
693 devc->digital_groups[i < 8 ? 0 : 1]->channels = g_slist_append(
694 devc->digital_groups[i < 8 ? 0 : 1]->channels, ch);
697 devc->model_config = &scope_models[model_index];
698 devc->frame_limit = 0;
700 if (!(devc->model_state = scope_state_new(devc->model_config)))
701 return SR_ERR_MALLOC;
706 SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
708 struct sr_channel *ch;
709 struct sr_dev_inst *sdi;
710 struct dev_context *devc;
711 struct sr_datafeed_packet packet;
713 struct sr_datafeed_analog_old analog;
714 struct sr_datafeed_logic logic;
720 if (!(sdi = cb_data))
723 if (!(devc = sdi->priv))
726 if (revents != G_IO_IN)
729 ch = devc->current_channel->data;
732 case SR_CHANNEL_ANALOG:
733 if (sr_scpi_get_floatv(sdi->conn, NULL, &data) != SR_OK) {
735 g_array_free(data, TRUE);
740 packet.type = SR_DF_FRAME_BEGIN;
741 sr_session_send(sdi, &packet);
743 analog.channels = g_slist_append(NULL, ch);
744 analog.num_samples = data->len;
745 analog.data = (float *) data->data;
746 analog.mq = SR_MQ_VOLTAGE;
747 analog.unit = SR_UNIT_VOLT;
749 packet.type = SR_DF_ANALOG_OLD;
750 packet.payload = &analog;
751 sr_session_send(cb_data, &packet);
752 g_slist_free(analog.channels);
753 g_array_free(data, TRUE);
756 case SR_CHANNEL_LOGIC:
757 if (sr_scpi_get_uint8v(sdi->conn, NULL, &data) != SR_OK) {
762 packet.type = SR_DF_FRAME_BEGIN;
763 sr_session_send(sdi, &packet);
765 logic.length = data->len;
767 logic.data = data->data;
768 packet.type = SR_DF_LOGIC;
769 packet.payload = &logic;
770 sr_session_send(cb_data, &packet);
771 g_array_free(data, TRUE);
775 sr_err("Invalid channel type.");
779 packet.type = SR_DF_FRAME_END;
780 sr_session_send(sdi, &packet);
782 if (devc->current_channel->next) {
783 devc->current_channel = devc->current_channel->next;
784 hmo_request_data(sdi);
785 } else if (++devc->num_frames == devc->frame_limit) {
786 sdi->driver->dev_acquisition_stop(sdi, cb_data);
788 devc->current_channel = devc->enabled_channels;
789 hmo_request_data(sdi);