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/>.
22 static const char *hameg_scpi_dialect[] = {
23 [SCPI_CMD_GET_DIG_DATA] = ":POD%d:DATA?",
24 [SCPI_CMD_GET_TIMEBASE] = ":TIM:SCAL?",
25 [SCPI_CMD_SET_TIMEBASE] = ":TIM:SCAL %s",
26 [SCPI_CMD_GET_COUPLING] = ":CHAN%d:COUP?",
27 [SCPI_CMD_SET_COUPLING] = ":CHAN%d:COUP %s",
28 [SCPI_CMD_GET_SAMPLE_RATE] = ":ACQ:SRAT?",
29 [SCPI_CMD_GET_SAMPLE_RATE_LIVE] = ":%s:DATA:POINTS?",
30 [SCPI_CMD_GET_ANALOG_DATA] = ":CHAN%d:DATA?",
31 [SCPI_CMD_GET_VERTICAL_DIV] = ":CHAN%d:SCAL?",
32 [SCPI_CMD_SET_VERTICAL_DIV] = ":CHAN%d:SCAL %s",
33 [SCPI_CMD_GET_DIG_POD_STATE] = ":POD%d:STAT?",
34 [SCPI_CMD_SET_DIG_POD_STATE] = ":POD%d:STAT %d",
35 [SCPI_CMD_GET_TRIGGER_SLOPE] = ":TRIG:A:EDGE:SLOP?",
36 [SCPI_CMD_SET_TRIGGER_SLOPE] = ":TRIG:A:EDGE:SLOP %s",
37 [SCPI_CMD_GET_TRIGGER_SOURCE] = ":TRIG:A:SOUR?",
38 [SCPI_CMD_SET_TRIGGER_SOURCE] = ":TRIG:A:SOUR %s",
39 [SCPI_CMD_GET_DIG_CHAN_STATE] = ":LOG%d:STAT?",
40 [SCPI_CMD_SET_DIG_CHAN_STATE] = ":LOG%d:STAT %d",
41 [SCPI_CMD_GET_VERTICAL_OFFSET] = ":CHAN%d:POS?",
42 [SCPI_CMD_GET_HORIZ_TRIGGERPOS] = ":TIM:POS?",
43 [SCPI_CMD_SET_HORIZ_TRIGGERPOS] = ":TIM:POS %E",
44 [SCPI_CMD_GET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT?",
45 [SCPI_CMD_SET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT %d",
48 static const int32_t hmo_hwcaps[] = {
50 SR_CONF_TRIGGER_SOURCE,
53 SR_CONF_TRIGGER_SLOPE,
54 SR_CONF_HORIZ_TRIGGERPOS,
58 static const int32_t hmo_analog_caps[] = {
64 static const char *hmo_coupling_options[] = {
73 static const char *scope_trigger_slopes[] = {
79 static const char *hmo_compact2_trigger_sources[] = {
95 static const char *hmo_compact4_trigger_sources[] = {
113 static const uint64_t hmo_timebases[][2] = {
152 static const uint64_t hmo_vdivs[][2] = {
170 static const char *scope_analog_probe_names[] = {
177 static const char *scope_digital_probe_names[] = {
196 static struct scope_config scope_models[] = {
198 .name = {"HMO722", "HMO1022", "HMO1522", "HMO2022", NULL},
199 .analog_channels = 2,
200 .digital_channels = 8,
203 .analog_names = &scope_analog_probe_names,
204 .digital_names = &scope_digital_probe_names,
206 .hw_caps = &hmo_hwcaps,
207 .num_hwcaps = ARRAY_SIZE(hmo_hwcaps),
209 .analog_hwcaps = &hmo_analog_caps,
210 .num_analog_hwcaps = ARRAY_SIZE(hmo_analog_caps),
212 .coupling_options = &hmo_coupling_options,
213 .trigger_sources = &hmo_compact2_trigger_sources,
214 .trigger_slopes = &scope_trigger_slopes,
216 .timebases = &hmo_timebases,
217 .num_timebases = ARRAY_SIZE(hmo_timebases),
220 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
225 .scpi_dialect = &hameg_scpi_dialect,
228 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", NULL},
229 .analog_channels = 4,
230 .digital_channels = 8,
233 .analog_names = &scope_analog_probe_names,
234 .digital_names = &scope_digital_probe_names,
236 .hw_caps = &hmo_hwcaps,
237 .num_hwcaps = ARRAY_SIZE(hmo_hwcaps),
239 .analog_hwcaps = &hmo_analog_caps,
240 .num_analog_hwcaps = ARRAY_SIZE(hmo_analog_caps),
242 .coupling_options = &hmo_coupling_options,
243 .trigger_sources = &hmo_compact4_trigger_sources,
244 .trigger_slopes = &scope_trigger_slopes,
246 .timebases = &hmo_timebases,
247 .num_timebases = ARRAY_SIZE(hmo_timebases),
250 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
255 .scpi_dialect = &hameg_scpi_dialect,
259 static void scope_state_dump(struct scope_config *config,
260 struct scope_state *state)
265 for (i = 0; i < config->analog_channels; ++i) {
266 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
267 (*config->vdivs)[state->analog_channels[i].vdiv][1]);
268 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
269 i + 1, state->analog_channels[i].state ? "On" : "Off",
270 (*config->coupling_options)[state->analog_channels[i].coupling],
271 tmp, state->analog_channels[i].vertical_offset);
274 for (i = 0; i < config->digital_channels; ++i) {
275 sr_info("State of digital channel %d -> %s", i,
276 state->digital_channels[i] ? "On" : "Off");
279 for (i = 0; i < config->digital_pods; ++i) {
280 sr_info("State of digital POD %d -> %s", i,
281 state->digital_pods[i] ? "On" : "Off");
284 tmp = sr_period_string((*config->timebases)[state->timebase][0] *
285 (*config->timebases)[state->timebase][1]);
286 sr_info("Current timebase: %s", tmp);
289 tmp = sr_samplerate_string(state->sample_rate);
290 sr_info("Current samplerate: %s", tmp);
293 sr_info("Current trigger: %s (source), %s (slope) %2.2e (offset)",
294 (*config->trigger_sources)[state->trigger_source],
295 (*config->trigger_slopes)[state->trigger_slope],
296 state->horiz_triggerpos);
299 static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
300 const char *command, const char *(*array)[], int *result)
305 if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK) {
310 for (i = 0; (*array)[i]; ++i) {
311 if (!g_strcmp0(tmp, (*array)[i])) {
327 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
328 struct scope_config *config,
329 struct scope_state *state)
333 char command[MAX_COMMAND_SIZE];
335 for (i = 0; i < config->analog_channels; ++i) {
336 g_snprintf(command, sizeof(command),
337 (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
340 if (sr_scpi_get_bool(scpi, command,
341 &state->analog_channels[i].state) != SR_OK)
344 g_snprintf(command, sizeof(command),
345 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
348 if (sr_scpi_get_float(scpi, command, &tmp_float) != SR_OK)
350 for (j = 0; j < config->num_vdivs; j++) {
351 if (tmp_float == ((float) (*config->vdivs)[j][0] /
352 (*config->vdivs)[j][1])) {
353 state->analog_channels[i].vdiv = j;
357 if (i == config->num_vdivs)
360 g_snprintf(command, sizeof(command),
361 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
364 if (sr_scpi_get_float(scpi, command,
365 &state->analog_channels[i].vertical_offset) != SR_OK)
368 g_snprintf(command, sizeof(command),
369 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
372 if (scope_state_get_array_option(scpi, command, config->coupling_options,
373 &state->analog_channels[i].coupling) != SR_OK)
380 static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
381 struct scope_config *config,
382 struct scope_state *state)
385 char command[MAX_COMMAND_SIZE];
387 for (i = 0; i < config->digital_channels; ++i) {
388 g_snprintf(command, sizeof(command),
389 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
392 if (sr_scpi_get_bool(scpi, command,
393 &state->digital_channels[i]) != SR_OK)
397 for (i = 0; i < config->digital_pods; ++i) {
398 g_snprintf(command, sizeof(command),
399 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
402 if (sr_scpi_get_bool(scpi, command,
403 &state->digital_pods[i]) != SR_OK)
410 SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
412 struct dev_context *devc;
413 struct scope_state *state;
414 struct scope_config *config;
419 gboolean channel_found;
420 char tmp_str[MAX_COMMAND_SIZE];
424 config = devc->model_config;
425 state = devc->model_state;
426 channel_found = FALSE;
428 for (i = 0; i < config->analog_channels; ++i) {
429 if (state->analog_channels[i].state) {
430 g_snprintf(chan_name, sizeof(chan_name), "CHAN%d", i + 1);
431 g_snprintf(tmp_str, sizeof(tmp_str),
432 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
434 channel_found = TRUE;
439 if (!channel_found) {
440 for (i = 0; i < config->digital_pods; i++) {
441 if (state->digital_pods[i]) {
442 g_snprintf(chan_name, sizeof(chan_name), "POD%d", i);
443 g_snprintf(tmp_str, sizeof(tmp_str),
444 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
446 channel_found = TRUE;
452 /* No channel is active, ask the instrument for the sample rate
453 * in single shot mode */
454 if (!channel_found) {
455 g_snprintf(tmp_str, sizeof(tmp_str),
456 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE]);
458 if (sr_scpi_get_float(sdi->conn, tmp_str, &tmp_float) != SR_OK)
460 state->sample_rate = tmp_float;
462 if (sr_scpi_get_int(sdi->conn, tmp_str, &tmp) != SR_OK)
464 state->sample_rate = tmp / (((float) (*config->timebases)[state->timebase][0] /
465 (*config->timebases)[state->timebase][1]) *
472 SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
474 struct dev_context *devc;
475 struct scope_state *state;
476 struct scope_config *config;
481 config = devc->model_config;
482 state = devc->model_state;
484 sr_info("Fetching scope state");
486 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
489 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
492 if (sr_scpi_get_float(sdi->conn,
493 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
494 &tmp_float) != SR_OK)
497 for (i = 0; i < config->num_timebases; i++) {
498 if (tmp_float == ((float) (*config->timebases)[i][0] /
499 (*config->timebases)[i][1])) {
504 if (i == config->num_timebases)
507 if (sr_scpi_get_float(sdi->conn,
508 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
509 &state->horiz_triggerpos) != SR_OK)
512 if (scope_state_get_array_option(sdi->conn,
513 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
514 config->trigger_sources, &state->trigger_source) != SR_OK)
517 if (scope_state_get_array_option(sdi->conn,
518 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
519 config->trigger_slopes, &state->trigger_slope) != SR_OK)
522 if (hmo_update_sample_rate(sdi) != SR_OK)
525 sr_info("Fetching finished.");
527 scope_state_dump(config, state);
532 static struct scope_state *scope_state_new(struct scope_config *config)
534 struct scope_state *state;
536 if (!(state = g_try_malloc0(sizeof(struct scope_state))))
539 if (!(state->analog_channels = g_try_malloc0_n(config->analog_channels,
540 sizeof(struct analog_channel_state))))
543 if (!(state->digital_channels = g_try_malloc0_n(
544 config->digital_channels, sizeof(gboolean))))
547 if (!(state->digital_pods = g_try_malloc0_n(config->digital_pods,
554 if (state->analog_channels)
555 g_free(state->analog_channels);
556 if (state->digital_channels)
557 g_free(state->digital_channels);
558 if (state->digital_pods)
559 g_free(state->digital_pods);
565 SR_PRIV void hmo_scope_state_free(struct scope_state *state)
567 g_free(state->analog_channels);
568 g_free(state->digital_channels);
569 g_free(state->digital_pods);
573 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
578 struct sr_probe *probe;
579 struct dev_context *devc;
584 /* Find the exact model. */
585 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
586 for (j = 0; scope_models[i].name[j]; j++) {
587 if (!strcmp(sdi->model, scope_models[i].name[j])) {
592 if (model_index != -1)
596 if (model_index == -1) {
597 sr_dbg("Unsupported HMO device.");
601 if (!(devc->analog_groups = g_try_malloc0(sizeof(struct sr_probe_group) *
602 scope_models[model_index].analog_channels)))
603 return SR_ERR_MALLOC;
605 if (!(devc->digital_groups = g_try_malloc0(sizeof(struct sr_probe_group) *
606 scope_models[model_index].digital_pods)))
607 return SR_ERR_MALLOC;
609 /* Add analog channels. */
610 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
611 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
612 (*scope_models[model_index].analog_names)[i])))
613 return SR_ERR_MALLOC;
614 sdi->probes = g_slist_append(sdi->probes, probe);
616 devc->analog_groups[i].name =
617 (char *)(*scope_models[model_index].analog_names)[i];
618 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
620 sdi->probe_groups = g_slist_append(sdi->probe_groups,
621 &devc->analog_groups[i]);
624 /* Add digital probe groups. */
625 for (i = 0; i < scope_models[model_index].digital_pods; ++i) {
626 g_snprintf(tmp, 25, "POD%d", i);
627 devc->digital_groups[i].name = g_strdup(tmp);
628 sdi->probe_groups = g_slist_append(sdi->probe_groups,
629 &devc->digital_groups[i < 8 ? 0 : 1]);
632 /* Add digital channels. */
633 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
634 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
635 (*scope_models[model_index].digital_names)[i])))
636 return SR_ERR_MALLOC;
637 sdi->probes = g_slist_append(sdi->probes, probe);
639 devc->digital_groups[i < 8 ? 0 : 1].probes = g_slist_append(
640 devc->digital_groups[i < 8 ? 0 : 1].probes, probe);
643 devc->model_config = &scope_models[model_index];
644 devc->frame_limit = 0;
646 if (!(devc->model_state = scope_state_new(devc->model_config)))
647 return SR_ERR_MALLOC;
652 SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
654 struct sr_probe *probe;
655 struct sr_dev_inst *sdi;
656 struct dev_context *devc;
657 struct sr_datafeed_packet packet;
659 struct sr_datafeed_analog analog;
660 struct sr_datafeed_logic logic;
664 if (!(sdi = cb_data))
667 if (!(devc = sdi->priv))
670 if (revents == G_IO_IN) {
671 probe = devc->current_probe->data;
673 switch (probe->type) {
674 case SR_PROBE_ANALOG:
675 if (sr_scpi_get_floatv(sdi->conn, NULL, &data) != SR_OK) {
677 g_array_free(data, TRUE);
682 packet.type = SR_DF_FRAME_BEGIN;
683 sr_session_send(sdi, &packet);
685 analog.probes = g_slist_append(NULL, probe);
686 analog.num_samples = data->len;
687 analog.data = (float *) data->data;
688 analog.mq = SR_MQ_VOLTAGE;
689 analog.unit = SR_UNIT_VOLT;
691 packet.type = SR_DF_ANALOG;
692 packet.payload = &analog;
693 sr_session_send(cb_data, &packet);
694 g_slist_free(analog.probes);
695 g_array_free(data, TRUE);
698 if (sr_scpi_get_uint8v(sdi->conn, NULL, &data) != SR_OK) {
704 packet.type = SR_DF_FRAME_BEGIN;
705 sr_session_send(sdi, &packet);
707 logic.length = data->len;
709 logic.data = data->data;
710 packet.type = SR_DF_LOGIC;
711 packet.payload = &logic;
712 sr_session_send(cb_data, &packet);
713 g_array_free(data, TRUE);
716 sr_err("Invalid probe type.");
720 packet.type = SR_DF_FRAME_END;
721 sr_session_send(sdi, &packet);
723 if (devc->current_probe->next) {
724 devc->current_probe = devc->current_probe->next;
725 hmo_request_data(sdi);
726 } else if (++devc->num_frames == devc->frame_limit) {
727 sdi->driver->dev_acquisition_stop(sdi, cb_data);
729 devc->current_probe = devc->enabled_probes;
730 hmo_request_data(sdi);