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/>.
23 static const char *hameg_scpi_dialect[] = {
24 [SCPI_CMD_GET_DIG_DATA] = ":POD%d:DATA?",
25 [SCPI_CMD_GET_TIMEBASE] = ":TIM:SCAL?",
26 [SCPI_CMD_SET_TIMEBASE] = ":TIM:SCAL %s",
27 [SCPI_CMD_GET_COUPLING] = ":CHAN%d:COUP?",
28 [SCPI_CMD_SET_COUPLING] = ":CHAN%d:COUP %s",
29 [SCPI_CMD_GET_SAMPLE_RATE] = ":ACQ:SRAT?",
30 [SCPI_CMD_GET_SAMPLE_RATE_LIVE] = ":%s:DATA:POINTS?",
31 [SCPI_CMD_GET_ANALOG_DATA] = ":CHAN%d:DATA?",
32 [SCPI_CMD_GET_VERTICAL_DIV] = ":CHAN%d:SCAL?",
33 [SCPI_CMD_SET_VERTICAL_DIV] = ":CHAN%d:SCAL %s",
34 [SCPI_CMD_GET_DIG_POD_STATE] = ":POD%d:STAT?",
35 [SCPI_CMD_SET_DIG_POD_STATE] = ":POD%d:STAT %d",
36 [SCPI_CMD_GET_TRIGGER_SLOPE] = ":TRIG:A:EDGE:SLOP?",
37 [SCPI_CMD_SET_TRIGGER_SLOPE] = ":TRIG:A:EDGE:SLOP %s",
38 [SCPI_CMD_GET_TRIGGER_SOURCE] = ":TRIG:A:SOUR?",
39 [SCPI_CMD_SET_TRIGGER_SOURCE] = ":TRIG:A:SOUR %s",
40 [SCPI_CMD_GET_DIG_CHAN_STATE] = ":LOG%d:STAT?",
41 [SCPI_CMD_SET_DIG_CHAN_STATE] = ":LOG%d:STAT %d",
42 [SCPI_CMD_GET_VERTICAL_OFFSET] = ":CHAN%d:POS?",
43 [SCPI_CMD_GET_HORIZ_TRIGGERPOS] = ":TIM:POS?",
44 [SCPI_CMD_SET_HORIZ_TRIGGERPOS] = ":TIM:POS %s",
45 [SCPI_CMD_GET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT?",
46 [SCPI_CMD_SET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT %d",
49 static const uint32_t hmo_devopts[] = {
51 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
52 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
53 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
54 SR_CONF_NUM_HDIV | SR_CONF_GET,
55 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
56 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
57 SR_CONF_SAMPLERATE | SR_CONF_GET,
60 static const uint32_t hmo_analog_devopts[] = {
61 SR_CONF_NUM_VDIV | SR_CONF_GET,
62 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
63 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
66 static const char *hmo_coupling_options[] = {
75 static const char *scope_trigger_slopes[] = {
81 static const char *hmo_compact2_trigger_sources[] = {
97 static const char *hmo_compact4_trigger_sources[] = {
115 static const uint64_t hmo_timebases[][2] = {
154 static const uint64_t hmo_vdivs[][2] = {
172 static const char *scope_analog_channel_names[] = {
179 static const char *scope_digital_channel_names[] = {
198 static const struct scope_config scope_models[] = {
200 .name = {"HMO722", "HMO1022", "HMO1522", "HMO2022", NULL},
201 .analog_channels = 2,
202 .digital_channels = 8,
205 .analog_names = &scope_analog_channel_names,
206 .digital_names = &scope_digital_channel_names,
208 .devopts = &hmo_devopts,
209 .num_devopts = ARRAY_SIZE(hmo_devopts),
211 .analog_devopts = &hmo_analog_devopts,
212 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
214 .coupling_options = &hmo_coupling_options,
215 .trigger_sources = &hmo_compact2_trigger_sources,
216 .trigger_slopes = &scope_trigger_slopes,
218 .timebases = &hmo_timebases,
219 .num_timebases = ARRAY_SIZE(hmo_timebases),
222 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
227 .scpi_dialect = &hameg_scpi_dialect,
230 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", NULL},
231 .analog_channels = 4,
232 .digital_channels = 8,
235 .analog_names = &scope_analog_channel_names,
236 .digital_names = &scope_digital_channel_names,
238 .devopts = &hmo_devopts,
239 .num_devopts = ARRAY_SIZE(hmo_devopts),
241 .analog_devopts = &hmo_analog_devopts,
242 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
244 .coupling_options = &hmo_coupling_options,
245 .trigger_sources = &hmo_compact4_trigger_sources,
246 .trigger_slopes = &scope_trigger_slopes,
248 .timebases = &hmo_timebases,
249 .num_timebases = ARRAY_SIZE(hmo_timebases),
252 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
257 .scpi_dialect = &hameg_scpi_dialect,
261 static void scope_state_dump(const struct scope_config *config,
262 struct scope_state *state)
267 for (i = 0; i < config->analog_channels; ++i) {
268 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
269 (*config->vdivs)[state->analog_channels[i].vdiv][1]);
270 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
271 i + 1, state->analog_channels[i].state ? "On" : "Off",
272 (*config->coupling_options)[state->analog_channels[i].coupling],
273 tmp, state->analog_channels[i].vertical_offset);
276 for (i = 0; i < config->digital_channels; ++i) {
277 sr_info("State of digital channel %d -> %s", i,
278 state->digital_channels[i] ? "On" : "Off");
281 for (i = 0; i < config->digital_pods; ++i) {
282 sr_info("State of digital POD %d -> %s", i,
283 state->digital_pods[i] ? "On" : "Off");
286 tmp = sr_period_string((*config->timebases)[state->timebase][0] *
287 (*config->timebases)[state->timebase][1]);
288 sr_info("Current timebase: %s", tmp);
291 tmp = sr_samplerate_string(state->sample_rate);
292 sr_info("Current samplerate: %s", tmp);
295 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
296 (*config->trigger_sources)[state->trigger_source],
297 (*config->trigger_slopes)[state->trigger_slope],
298 state->horiz_triggerpos);
301 static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
302 const char *command, const char *(*array)[], int *result)
307 if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK) {
312 for (i = 0; (*array)[i]; ++i) {
313 if (!g_strcmp0(tmp, (*array)[i])) {
329 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
330 const struct scope_config *config,
331 struct scope_state *state)
335 char command[MAX_COMMAND_SIZE];
337 for (i = 0; i < config->analog_channels; ++i) {
338 g_snprintf(command, sizeof(command),
339 (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
342 if (sr_scpi_get_bool(scpi, command,
343 &state->analog_channels[i].state) != SR_OK)
346 g_snprintf(command, sizeof(command),
347 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
350 if (sr_scpi_get_float(scpi, command, &tmp_float) != SR_OK)
352 for (j = 0; j < config->num_vdivs; j++) {
353 if (tmp_float == ((float) (*config->vdivs)[j][0] /
354 (*config->vdivs)[j][1])) {
355 state->analog_channels[i].vdiv = j;
359 if (j == config->num_vdivs) {
360 sr_err("Could not determine array index for vertical div scale.");
364 g_snprintf(command, sizeof(command),
365 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
368 if (sr_scpi_get_float(scpi, command,
369 &state->analog_channels[i].vertical_offset) != SR_OK)
372 g_snprintf(command, sizeof(command),
373 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
376 if (scope_state_get_array_option(scpi, command, config->coupling_options,
377 &state->analog_channels[i].coupling) != SR_OK)
384 static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
385 const struct scope_config *config,
386 struct scope_state *state)
389 char command[MAX_COMMAND_SIZE];
391 for (i = 0; i < config->digital_channels; ++i) {
392 g_snprintf(command, sizeof(command),
393 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
396 if (sr_scpi_get_bool(scpi, command,
397 &state->digital_channels[i]) != SR_OK)
401 for (i = 0; i < config->digital_pods; ++i) {
402 g_snprintf(command, sizeof(command),
403 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
406 if (sr_scpi_get_bool(scpi, command,
407 &state->digital_pods[i]) != SR_OK)
414 SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
416 struct dev_context *devc;
417 struct scope_state *state;
418 const struct scope_config *config;
423 gboolean channel_found;
424 char tmp_str[MAX_COMMAND_SIZE];
428 config = devc->model_config;
429 state = devc->model_state;
430 channel_found = FALSE;
432 for (i = 0; i < config->analog_channels; ++i) {
433 if (state->analog_channels[i].state) {
434 g_snprintf(chan_name, sizeof(chan_name), "CHAN%d", i + 1);
435 g_snprintf(tmp_str, sizeof(tmp_str),
436 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
438 channel_found = TRUE;
443 if (!channel_found) {
444 for (i = 0; i < config->digital_pods; i++) {
445 if (state->digital_pods[i]) {
446 g_snprintf(chan_name, sizeof(chan_name), "POD%d", i);
447 g_snprintf(tmp_str, sizeof(tmp_str),
448 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
450 channel_found = TRUE;
456 /* No channel is active, ask the instrument for the sample rate
457 * in single shot mode */
458 if (!channel_found) {
459 if (sr_scpi_get_float(sdi->conn,
460 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE],
461 &tmp_float) != SR_OK)
464 state->sample_rate = tmp_float;
466 if (sr_scpi_get_int(sdi->conn, tmp_str, &tmp) != SR_OK)
468 state->sample_rate = tmp / (((float) (*config->timebases)[state->timebase][0] /
469 (*config->timebases)[state->timebase][1]) *
476 SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
478 struct dev_context *devc;
479 struct scope_state *state;
480 const struct scope_config *config;
485 config = devc->model_config;
486 state = devc->model_state;
488 sr_info("Fetching scope state");
490 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
493 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
496 if (sr_scpi_get_float(sdi->conn,
497 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
498 &tmp_float) != SR_OK)
501 for (i = 0; i < config->num_timebases; i++) {
502 if (tmp_float == ((float) (*config->timebases)[i][0] /
503 (*config->timebases)[i][1])) {
508 if (i == config->num_timebases) {
509 sr_err("Could not determine array index for time base.");
513 if (sr_scpi_get_float(sdi->conn,
514 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
515 &tmp_float) != SR_OK)
517 state->horiz_triggerpos = tmp_float /
518 (((double) (*config->timebases)[state->timebase][0] /
519 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
520 state->horiz_triggerpos -= 0.5;
521 state->horiz_triggerpos *= -1;
523 if (scope_state_get_array_option(sdi->conn,
524 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
525 config->trigger_sources, &state->trigger_source) != SR_OK)
528 if (scope_state_get_array_option(sdi->conn,
529 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
530 config->trigger_slopes, &state->trigger_slope) != SR_OK)
533 if (hmo_update_sample_rate(sdi) != SR_OK)
536 sr_info("Fetching finished.");
538 scope_state_dump(config, state);
543 static struct scope_state *scope_state_new(const struct scope_config *config)
545 struct scope_state *state;
547 state = g_malloc0(sizeof(struct scope_state));
548 state->analog_channels = g_malloc0_n(config->analog_channels,
549 sizeof(struct analog_channel_state));
550 state->digital_channels = g_malloc0_n(
551 config->digital_channels, sizeof(gboolean));
552 state->digital_pods = g_malloc0_n(config->digital_pods,
558 SR_PRIV void hmo_scope_state_free(struct scope_state *state)
560 g_free(state->analog_channels);
561 g_free(state->digital_channels);
562 g_free(state->digital_pods);
566 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
571 struct sr_channel *ch;
572 struct dev_context *devc;
577 /* Find the exact model. */
578 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
579 for (j = 0; scope_models[i].name[j]; j++) {
580 if (!strcmp(sdi->model, scope_models[i].name[j])) {
585 if (model_index != -1)
589 if (model_index == -1) {
590 sr_dbg("Unsupported HMO device.");
594 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
595 scope_models[model_index].analog_channels);
597 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
598 scope_models[model_index].digital_pods);
600 /* Add analog channels. */
601 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
602 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
603 (*scope_models[model_index].analog_names)[i]);
605 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
607 devc->analog_groups[i]->name = g_strdup(
608 (char *)(*scope_models[model_index].analog_names)[i]);
609 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
611 sdi->channel_groups = g_slist_append(sdi->channel_groups,
612 devc->analog_groups[i]);
615 /* Add digital channel groups. */
616 for (i = 0; i < scope_models[model_index].digital_pods; ++i) {
617 g_snprintf(tmp, 25, "POD%d", i);
619 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
621 devc->digital_groups[i]->name = g_strdup(tmp);
622 sdi->channel_groups = g_slist_append(sdi->channel_groups,
623 devc->digital_groups[i < 8 ? 0 : 1]);
626 /* Add digital channels. */
627 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
628 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
629 (*scope_models[model_index].digital_names)[i]);
631 devc->digital_groups[i < 8 ? 0 : 1]->channels = g_slist_append(
632 devc->digital_groups[i < 8 ? 0 : 1]->channels, ch);
635 devc->model_config = &scope_models[model_index];
636 devc->frame_limit = 0;
638 if (!(devc->model_state = scope_state_new(devc->model_config)))
639 return SR_ERR_MALLOC;
644 SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
646 struct sr_channel *ch;
647 struct sr_dev_inst *sdi;
648 struct dev_context *devc;
649 struct sr_datafeed_packet packet;
651 struct sr_datafeed_analog analog;
652 struct sr_datafeed_logic logic;
658 if (!(sdi = cb_data))
661 if (!(devc = sdi->priv))
664 if (revents != G_IO_IN)
667 ch = devc->current_channel->data;
670 case SR_CHANNEL_ANALOG:
671 if (sr_scpi_get_floatv(sdi->conn, NULL, &data) != SR_OK) {
673 g_array_free(data, TRUE);
678 packet.type = SR_DF_FRAME_BEGIN;
679 sr_session_send(sdi, &packet);
681 analog.channels = g_slist_append(NULL, ch);
682 analog.num_samples = data->len;
683 analog.data = (float *) data->data;
684 analog.mq = SR_MQ_VOLTAGE;
685 analog.unit = SR_UNIT_VOLT;
687 packet.type = SR_DF_ANALOG;
688 packet.payload = &analog;
689 sr_session_send(cb_data, &packet);
690 g_slist_free(analog.channels);
691 g_array_free(data, TRUE);
694 case SR_CHANNEL_LOGIC:
695 if (sr_scpi_get_uint8v(sdi->conn, NULL, &data) != SR_OK) {
700 packet.type = SR_DF_FRAME_BEGIN;
701 sr_session_send(sdi, &packet);
703 logic.length = data->len;
705 logic.data = data->data;
706 packet.type = SR_DF_LOGIC;
707 packet.payload = &logic;
708 sr_session_send(cb_data, &packet);
709 g_array_free(data, TRUE);
713 sr_err("Invalid channel type.");
717 packet.type = SR_DF_FRAME_END;
718 sr_session_send(sdi, &packet);
720 if (devc->current_channel->next) {
721 devc->current_channel = devc->current_channel->next;
722 hmo_request_data(sdi);
723 } else if (++devc->num_frames == devc->frame_limit) {
724 sdi->driver->dev_acquisition_stop(sdi, cb_data);
726 devc->current_channel = devc->enabled_channels;
727 hmo_request_data(sdi);