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[] = {
101 static const char *hmo_compact4_trigger_sources[] = {
119 static const uint64_t hmo_timebases[][2] = {
158 static const uint64_t hmo_vdivs[][2] = {
178 static const char *scope_analog_channel_names[] = {
185 static const char *scope_digital_channel_names[] = {
204 static const struct scope_config scope_models[] = {
206 /* HMO2522/3032/3042/3052 support 16 digital channels but they're not supported yet. */
207 .name = {"HMO1002", "HMO722", "HMO1022", "HMO1522", "HMO2022", "HMO2522",
208 "HMO3032", "HMO3042", "HMO3052", NULL},
209 .analog_channels = 2,
210 .digital_channels = 8,
213 .analog_names = &scope_analog_channel_names,
214 .digital_names = &scope_digital_channel_names,
216 .devopts = &hmo_devopts,
217 .num_devopts = ARRAY_SIZE(hmo_devopts),
219 .analog_devopts = &hmo_analog_devopts,
220 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
222 .coupling_options = &hmo_coupling_options,
223 .trigger_sources = &hmo_compact2_trigger_sources,
224 .trigger_slopes = &scope_trigger_slopes,
226 .timebases = &hmo_timebases,
227 .num_timebases = ARRAY_SIZE(hmo_timebases),
230 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
235 .scpi_dialect = &hameg_scpi_dialect,
238 /* HMO2524/3034/3044/3054 support 16 digital channels but they're not supported yet. */
239 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", "HMO2524",
240 "HMO3034", "HMO3044", "HMO3054", NULL},
241 .analog_channels = 4,
242 .digital_channels = 8,
245 .analog_names = &scope_analog_channel_names,
246 .digital_names = &scope_digital_channel_names,
248 .devopts = &hmo_devopts,
249 .num_devopts = ARRAY_SIZE(hmo_devopts),
251 .analog_devopts = &hmo_analog_devopts,
252 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
254 .coupling_options = &hmo_coupling_options,
255 .trigger_sources = &hmo_compact4_trigger_sources,
256 .trigger_slopes = &scope_trigger_slopes,
258 .timebases = &hmo_timebases,
259 .num_timebases = ARRAY_SIZE(hmo_timebases),
262 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
267 .scpi_dialect = &hameg_scpi_dialect,
271 static void scope_state_dump(const struct scope_config *config,
272 struct scope_state *state)
277 for (i = 0; i < config->analog_channels; i++) {
278 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
279 (*config->vdivs)[state->analog_channels[i].vdiv][1]);
280 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
281 i + 1, state->analog_channels[i].state ? "On" : "Off",
282 (*config->coupling_options)[state->analog_channels[i].coupling],
283 tmp, state->analog_channels[i].vertical_offset);
286 for (i = 0; i < config->digital_channels; i++) {
287 sr_info("State of digital channel %d -> %s", i,
288 state->digital_channels[i] ? "On" : "Off");
291 for (i = 0; i < config->digital_pods; i++) {
292 sr_info("State of digital POD %d -> %s", i,
293 state->digital_pods[i] ? "On" : "Off");
296 tmp = sr_period_string((*config->timebases)[state->timebase][0] *
297 (*config->timebases)[state->timebase][1]);
298 sr_info("Current timebase: %s", tmp);
301 tmp = sr_samplerate_string(state->sample_rate);
302 sr_info("Current samplerate: %s", tmp);
305 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
306 (*config->trigger_sources)[state->trigger_source],
307 (*config->trigger_slopes)[state->trigger_slope],
308 state->horiz_triggerpos);
311 static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
312 const char *command, const char *(*array)[], int *result)
317 if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK) {
322 for (i = 0; (*array)[i]; i++) {
323 if (!g_strcmp0(tmp, (*array)[i])) {
340 * This function takes a value of the form "2.000E-03", converts it to a
341 * significand / factor pair and returns the index of an array where
342 * a matching pair was found.
344 * It's a bit convoluted because of floating-point issues. The value "10.00E-09"
345 * is parsed by g_ascii_strtod() as 0.000000009999999939, for example.
346 * Therefore it's easier to break the number up into two strings and handle
349 * @param value The string to be parsed.
350 * @param array The array of s/f pairs.
351 * @param array_len The number of pairs in the array.
352 * @param result The index at which a matching pair was found.
354 * @return SR_ERR on any parsing error, SR_OK otherwise.
356 static int array_float_get(gchar *value, const uint64_t array[][2],
357 int array_len, unsigned int *result)
364 gchar ss[10], es[10];
366 memset(ss, 0, sizeof(ss));
367 memset(es, 0, sizeof(es));
369 /* Get index of the separating 'E' character and break up the string. */
370 pos = strcspn(value, "E");
372 strncpy(ss, value, pos);
373 strncpy(es, &(value[pos+1]), 3);
375 if (sr_atof_ascii(ss, &s) != SR_OK)
377 if (sr_atoi(es, &e) != SR_OK)
380 /* Transform e.g. 10^-03 to 1000 as the array stores the inverse. */
384 * Adjust the significand/factor pair to make sure
385 * that f is a multiple of 1000.
387 while ((int)fmod(log10(f), 3) > 0) {
396 /* Truncate s to circumvent rounding errors. */
397 s_int = (unsigned int)s;
399 for (i = 0; i < array_len; i++) {
400 if ((s_int == array[i][0]) && (f == array[i][1])) {
409 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
410 const struct scope_config *config,
411 struct scope_state *state)
414 char command[MAX_COMMAND_SIZE];
417 for (i = 0; i < config->analog_channels; i++) {
418 g_snprintf(command, sizeof(command),
419 (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
422 if (sr_scpi_get_bool(scpi, command,
423 &state->analog_channels[i].state) != SR_OK)
426 g_snprintf(command, sizeof(command),
427 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
430 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
433 if (array_float_get(tmp_str, hmo_vdivs, ARRAY_SIZE(hmo_vdivs),
436 sr_err("Could not determine array index for vertical div scale.");
441 state->analog_channels[i].vdiv = j;
443 g_snprintf(command, sizeof(command),
444 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
447 if (sr_scpi_get_float(scpi, command,
448 &state->analog_channels[i].vertical_offset) != SR_OK)
451 g_snprintf(command, sizeof(command),
452 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
455 if (scope_state_get_array_option(scpi, command, config->coupling_options,
456 &state->analog_channels[i].coupling) != SR_OK)
463 static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
464 const struct scope_config *config,
465 struct scope_state *state)
468 char command[MAX_COMMAND_SIZE];
470 for (i = 0; i < config->digital_channels; i++) {
471 g_snprintf(command, sizeof(command),
472 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
475 if (sr_scpi_get_bool(scpi, command,
476 &state->digital_channels[i]) != SR_OK)
480 for (i = 0; i < config->digital_pods; i++) {
481 g_snprintf(command, sizeof(command),
482 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
485 if (sr_scpi_get_bool(scpi, command,
486 &state->digital_pods[i]) != SR_OK)
493 SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
495 struct dev_context *devc;
496 struct scope_state *state;
497 const struct scope_config *config;
502 gboolean channel_found;
503 char tmp_str[MAX_COMMAND_SIZE];
507 config = devc->model_config;
508 state = devc->model_state;
509 channel_found = FALSE;
511 for (i = 0; i < config->analog_channels; i++) {
512 if (state->analog_channels[i].state) {
513 g_snprintf(chan_name, sizeof(chan_name), "CHAN%d", i + 1);
514 g_snprintf(tmp_str, sizeof(tmp_str),
515 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
517 channel_found = TRUE;
522 if (!channel_found) {
523 for (i = 0; i < config->digital_pods; i++) {
524 if (state->digital_pods[i]) {
525 g_snprintf(chan_name, sizeof(chan_name), "POD%d", i);
526 g_snprintf(tmp_str, sizeof(tmp_str),
527 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
529 channel_found = TRUE;
535 /* No channel is active, ask the instrument for the sample rate
536 * in single shot mode */
537 if (!channel_found) {
538 if (sr_scpi_get_float(sdi->conn,
539 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE],
540 &tmp_float) != SR_OK)
543 state->sample_rate = tmp_float;
545 if (sr_scpi_get_int(sdi->conn, tmp_str, &tmp) != SR_OK)
547 state->sample_rate = tmp / (((float) (*config->timebases)[state->timebase][0] /
548 (*config->timebases)[state->timebase][1]) *
555 SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
557 struct dev_context *devc;
558 struct scope_state *state;
559 const struct scope_config *config;
565 config = devc->model_config;
566 state = devc->model_state;
568 sr_info("Fetching scope state");
570 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
573 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
576 if (sr_scpi_get_float(sdi->conn,
577 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
578 &tmp_float) != SR_OK)
581 if (sr_scpi_get_string(sdi->conn,
582 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
586 if (array_float_get(tmp_str, hmo_timebases, ARRAY_SIZE(hmo_timebases),
589 sr_err("Could not determine array index for time base.");
595 if (sr_scpi_get_float(sdi->conn,
596 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
597 &tmp_float) != SR_OK)
599 state->horiz_triggerpos = tmp_float /
600 (((double) (*config->timebases)[state->timebase][0] /
601 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
602 state->horiz_triggerpos -= 0.5;
603 state->horiz_triggerpos *= -1;
605 if (scope_state_get_array_option(sdi->conn,
606 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
607 config->trigger_sources, &state->trigger_source) != SR_OK)
610 if (scope_state_get_array_option(sdi->conn,
611 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
612 config->trigger_slopes, &state->trigger_slope) != SR_OK)
615 if (hmo_update_sample_rate(sdi) != SR_OK)
618 sr_info("Fetching finished.");
620 scope_state_dump(config, state);
625 static struct scope_state *scope_state_new(const struct scope_config *config)
627 struct scope_state *state;
629 state = g_malloc0(sizeof(struct scope_state));
630 state->analog_channels = g_malloc0_n(config->analog_channels,
631 sizeof(struct analog_channel_state));
632 state->digital_channels = g_malloc0_n(
633 config->digital_channels, sizeof(gboolean));
634 state->digital_pods = g_malloc0_n(config->digital_pods,
640 SR_PRIV void hmo_scope_state_free(struct scope_state *state)
642 g_free(state->analog_channels);
643 g_free(state->digital_channels);
644 g_free(state->digital_pods);
648 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
653 struct sr_channel *ch;
654 struct dev_context *devc;
659 /* Find the exact model. */
660 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
661 for (j = 0; scope_models[i].name[j]; j++) {
662 if (!strcmp(sdi->model, scope_models[i].name[j])) {
667 if (model_index != -1)
671 if (model_index == -1) {
672 sr_dbg("Unsupported HMO device.");
676 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
677 scope_models[model_index].analog_channels);
679 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
680 scope_models[model_index].digital_pods);
682 /* Add analog channels. */
683 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
684 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
685 (*scope_models[model_index].analog_names)[i]);
687 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
689 devc->analog_groups[i]->name = g_strdup(
690 (char *)(*scope_models[model_index].analog_names)[i]);
691 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
693 sdi->channel_groups = g_slist_append(sdi->channel_groups,
694 devc->analog_groups[i]);
697 /* Add digital channel groups. */
698 for (i = 0; i < scope_models[model_index].digital_pods; i++) {
699 g_snprintf(tmp, 25, "POD%d", i);
701 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
703 devc->digital_groups[i]->name = g_strdup(tmp);
704 sdi->channel_groups = g_slist_append(sdi->channel_groups,
705 devc->digital_groups[i < 8 ? 0 : 1]);
708 /* Add digital channels. */
709 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
710 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
711 (*scope_models[model_index].digital_names)[i]);
713 devc->digital_groups[i < 8 ? 0 : 1]->channels = g_slist_append(
714 devc->digital_groups[i < 8 ? 0 : 1]->channels, ch);
717 devc->model_config = &scope_models[model_index];
718 devc->frame_limit = 0;
720 if (!(devc->model_state = scope_state_new(devc->model_config)))
721 return SR_ERR_MALLOC;
726 SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
728 struct sr_channel *ch;
729 struct sr_dev_inst *sdi;
730 struct dev_context *devc;
731 struct sr_datafeed_packet packet;
733 struct sr_datafeed_analog_old analog;
734 struct sr_datafeed_logic logic;
741 if (!(sdi = cb_data))
744 if (!(devc = sdi->priv))
747 /* Although this is correct in general, the USBTMC libusb implementation
748 * currently does not generate an event prior to the first read. Often
749 * it is ok to start reading just after the 50ms timeout. See bug #785.
750 if (revents != G_IO_IN)
754 ch = devc->current_channel->data;
757 case SR_CHANNEL_ANALOG:
758 if (sr_scpi_get_floatv(sdi->conn, NULL, &data) != SR_OK) {
760 g_array_free(data, TRUE);
765 packet.type = SR_DF_FRAME_BEGIN;
766 sr_session_send(sdi, &packet);
768 analog.channels = g_slist_append(NULL, ch);
769 analog.num_samples = data->len;
770 analog.data = (float *) data->data;
771 analog.mq = SR_MQ_VOLTAGE;
772 analog.unit = SR_UNIT_VOLT;
774 packet.type = SR_DF_ANALOG_OLD;
775 packet.payload = &analog;
776 sr_session_send(sdi, &packet);
777 g_slist_free(analog.channels);
778 g_array_free(data, TRUE);
781 case SR_CHANNEL_LOGIC:
782 if (sr_scpi_get_uint8v(sdi->conn, NULL, &data) != SR_OK) {
787 packet.type = SR_DF_FRAME_BEGIN;
788 sr_session_send(sdi, &packet);
790 logic.length = data->len;
792 logic.data = data->data;
793 packet.type = SR_DF_LOGIC;
794 packet.payload = &logic;
795 sr_session_send(sdi, &packet);
796 g_array_free(data, TRUE);
800 sr_err("Invalid channel type.");
804 packet.type = SR_DF_FRAME_END;
805 sr_session_send(sdi, &packet);
807 if (devc->current_channel->next) {
808 devc->current_channel = devc->current_channel->next;
809 hmo_request_data(sdi);
810 } else if (++devc->num_frames == devc->frame_limit) {
811 sdi->driver->dev_acquisition_stop(sdi);
813 devc->current_channel = devc->enabled_channels;
814 hmo_request_data(sdi);