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 %s",
44 [SCPI_CMD_GET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT?",
45 [SCPI_CMD_SET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT %d",
48 static const uint32_t hmo_devopts[] = {
50 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
51 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
52 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
53 SR_CONF_NUM_HDIV | SR_CONF_GET,
54 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
55 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
56 SR_CONF_SAMPLERATE | SR_CONF_GET,
59 static const uint32_t hmo_analog_devopts[] = {
60 SR_CONF_NUM_VDIV | SR_CONF_GET,
61 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
62 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
65 static const char *hmo_coupling_options[] = {
74 static const char *scope_trigger_slopes[] = {
80 static const char *hmo_compact2_trigger_sources[] = {
96 static const char *hmo_compact4_trigger_sources[] = {
114 static const uint64_t hmo_timebases[][2] = {
153 static const uint64_t hmo_vdivs[][2] = {
171 static const char *scope_analog_channel_names[] = {
178 static const char *scope_digital_channel_names[] = {
197 static const struct scope_config scope_models[] = {
199 .name = {"HMO722", "HMO1022", "HMO1522", "HMO2022", NULL},
200 .analog_channels = 2,
201 .digital_channels = 8,
204 .analog_names = &scope_analog_channel_names,
205 .digital_names = &scope_digital_channel_names,
207 .devopts = &hmo_devopts,
208 .num_devopts = ARRAY_SIZE(hmo_devopts),
210 .analog_devopts = &hmo_analog_devopts,
211 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
213 .coupling_options = &hmo_coupling_options,
214 .trigger_sources = &hmo_compact2_trigger_sources,
215 .trigger_slopes = &scope_trigger_slopes,
217 .timebases = &hmo_timebases,
218 .num_timebases = ARRAY_SIZE(hmo_timebases),
221 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
226 .scpi_dialect = &hameg_scpi_dialect,
229 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", NULL},
230 .analog_channels = 4,
231 .digital_channels = 8,
234 .analog_names = &scope_analog_channel_names,
235 .digital_names = &scope_digital_channel_names,
237 .devopts = &hmo_devopts,
238 .num_devopts = ARRAY_SIZE(hmo_devopts),
240 .analog_devopts = &hmo_analog_devopts,
241 .num_analog_devopts = ARRAY_SIZE(hmo_analog_devopts),
243 .coupling_options = &hmo_coupling_options,
244 .trigger_sources = &hmo_compact4_trigger_sources,
245 .trigger_slopes = &scope_trigger_slopes,
247 .timebases = &hmo_timebases,
248 .num_timebases = ARRAY_SIZE(hmo_timebases),
251 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
256 .scpi_dialect = &hameg_scpi_dialect,
260 static void scope_state_dump(const struct scope_config *config,
261 struct scope_state *state)
266 for (i = 0; i < config->analog_channels; ++i) {
267 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
268 (*config->vdivs)[state->analog_channels[i].vdiv][1]);
269 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
270 i + 1, state->analog_channels[i].state ? "On" : "Off",
271 (*config->coupling_options)[state->analog_channels[i].coupling],
272 tmp, state->analog_channels[i].vertical_offset);
275 for (i = 0; i < config->digital_channels; ++i) {
276 sr_info("State of digital channel %d -> %s", i,
277 state->digital_channels[i] ? "On" : "Off");
280 for (i = 0; i < config->digital_pods; ++i) {
281 sr_info("State of digital POD %d -> %s", i,
282 state->digital_pods[i] ? "On" : "Off");
285 tmp = sr_period_string((*config->timebases)[state->timebase][0] *
286 (*config->timebases)[state->timebase][1]);
287 sr_info("Current timebase: %s", tmp);
290 tmp = sr_samplerate_string(state->sample_rate);
291 sr_info("Current samplerate: %s", tmp);
294 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
295 (*config->trigger_sources)[state->trigger_source],
296 (*config->trigger_slopes)[state->trigger_slope],
297 state->horiz_triggerpos);
300 static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
301 const char *command, const char *(*array)[], int *result)
306 if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK) {
311 for (i = 0; (*array)[i]; ++i) {
312 if (!g_strcmp0(tmp, (*array)[i])) {
328 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
329 const struct scope_config *config,
330 struct scope_state *state)
334 char command[MAX_COMMAND_SIZE];
336 for (i = 0; i < config->analog_channels; ++i) {
337 g_snprintf(command, sizeof(command),
338 (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
341 if (sr_scpi_get_bool(scpi, command,
342 &state->analog_channels[i].state) != SR_OK)
345 g_snprintf(command, sizeof(command),
346 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
349 if (sr_scpi_get_float(scpi, command, &tmp_float) != SR_OK)
351 for (j = 0; j < config->num_vdivs; j++) {
352 if (tmp_float == ((float) (*config->vdivs)[j][0] /
353 (*config->vdivs)[j][1])) {
354 state->analog_channels[i].vdiv = j;
358 if (j == config->num_vdivs) {
359 sr_err("Could not determine array index for vertical div scale.");
363 g_snprintf(command, sizeof(command),
364 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
367 if (sr_scpi_get_float(scpi, command,
368 &state->analog_channels[i].vertical_offset) != SR_OK)
371 g_snprintf(command, sizeof(command),
372 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
375 if (scope_state_get_array_option(scpi, command, config->coupling_options,
376 &state->analog_channels[i].coupling) != SR_OK)
383 static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
384 const struct scope_config *config,
385 struct scope_state *state)
388 char command[MAX_COMMAND_SIZE];
390 for (i = 0; i < config->digital_channels; ++i) {
391 g_snprintf(command, sizeof(command),
392 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
395 if (sr_scpi_get_bool(scpi, command,
396 &state->digital_channels[i]) != SR_OK)
400 for (i = 0; i < config->digital_pods; ++i) {
401 g_snprintf(command, sizeof(command),
402 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
405 if (sr_scpi_get_bool(scpi, command,
406 &state->digital_pods[i]) != SR_OK)
413 SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
415 struct dev_context *devc;
416 struct scope_state *state;
417 const struct scope_config *config;
422 gboolean channel_found;
423 char tmp_str[MAX_COMMAND_SIZE];
427 config = devc->model_config;
428 state = devc->model_state;
429 channel_found = FALSE;
431 for (i = 0; i < config->analog_channels; ++i) {
432 if (state->analog_channels[i].state) {
433 g_snprintf(chan_name, sizeof(chan_name), "CHAN%d", i + 1);
434 g_snprintf(tmp_str, sizeof(tmp_str),
435 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
437 channel_found = TRUE;
442 if (!channel_found) {
443 for (i = 0; i < config->digital_pods; i++) {
444 if (state->digital_pods[i]) {
445 g_snprintf(chan_name, sizeof(chan_name), "POD%d", i);
446 g_snprintf(tmp_str, sizeof(tmp_str),
447 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
449 channel_found = TRUE;
455 /* No channel is active, ask the instrument for the sample rate
456 * in single shot mode */
457 if (!channel_found) {
458 if (sr_scpi_get_float(sdi->conn,
459 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE],
460 &tmp_float) != SR_OK)
463 state->sample_rate = tmp_float;
465 if (sr_scpi_get_int(sdi->conn, tmp_str, &tmp) != SR_OK)
467 state->sample_rate = tmp / (((float) (*config->timebases)[state->timebase][0] /
468 (*config->timebases)[state->timebase][1]) *
475 SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
477 struct dev_context *devc;
478 struct scope_state *state;
479 const struct scope_config *config;
484 config = devc->model_config;
485 state = devc->model_state;
487 sr_info("Fetching scope state");
489 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
492 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
495 if (sr_scpi_get_float(sdi->conn,
496 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
497 &tmp_float) != SR_OK)
500 for (i = 0; i < config->num_timebases; i++) {
501 if (tmp_float == ((float) (*config->timebases)[i][0] /
502 (*config->timebases)[i][1])) {
507 if (i == config->num_timebases) {
508 sr_err("Could not determine array index for time base.");
512 if (sr_scpi_get_float(sdi->conn,
513 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
514 &tmp_float) != SR_OK)
516 state->horiz_triggerpos = tmp_float /
517 (((double) (*config->timebases)[state->timebase][0] /
518 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
519 state->horiz_triggerpos -= 0.5;
520 state->horiz_triggerpos *= -1;
522 if (scope_state_get_array_option(sdi->conn,
523 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
524 config->trigger_sources, &state->trigger_source) != SR_OK)
527 if (scope_state_get_array_option(sdi->conn,
528 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
529 config->trigger_slopes, &state->trigger_slope) != SR_OK)
532 if (hmo_update_sample_rate(sdi) != SR_OK)
535 sr_info("Fetching finished.");
537 scope_state_dump(config, state);
542 static struct scope_state *scope_state_new(const struct scope_config *config)
544 struct scope_state *state;
546 if (!(state = g_try_malloc0(sizeof(struct scope_state))))
549 if (!(state->analog_channels = g_try_malloc0_n(config->analog_channels,
550 sizeof(struct analog_channel_state))))
553 if (!(state->digital_channels = g_try_malloc0_n(
554 config->digital_channels, sizeof(gboolean))))
557 if (!(state->digital_pods = g_try_malloc0_n(config->digital_pods,
564 if (state->analog_channels)
565 g_free(state->analog_channels);
566 if (state->digital_channels)
567 g_free(state->digital_channels);
568 if (state->digital_pods)
569 g_free(state->digital_pods);
575 SR_PRIV void hmo_scope_state_free(struct scope_state *state)
577 g_free(state->analog_channels);
578 g_free(state->digital_channels);
579 g_free(state->digital_pods);
583 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
588 struct sr_channel *ch;
589 struct dev_context *devc;
594 /* Find the exact model. */
595 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
596 for (j = 0; scope_models[i].name[j]; j++) {
597 if (!strcmp(sdi->model, scope_models[i].name[j])) {
602 if (model_index != -1)
606 if (model_index == -1) {
607 sr_dbg("Unsupported HMO device.");
611 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
612 scope_models[model_index].analog_channels);
614 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
615 scope_models[model_index].digital_pods);
617 /* Add analog channels. */
618 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
619 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
620 (*scope_models[model_index].analog_names)[i]);
622 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
624 devc->analog_groups[i]->name = g_strdup(
625 (char *)(*scope_models[model_index].analog_names)[i]);
626 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
628 sdi->channel_groups = g_slist_append(sdi->channel_groups,
629 devc->analog_groups[i]);
632 /* Add digital channel groups. */
633 for (i = 0; i < scope_models[model_index].digital_pods; ++i) {
634 g_snprintf(tmp, 25, "POD%d", i);
636 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
638 devc->digital_groups[i]->name = g_strdup(tmp);
639 sdi->channel_groups = g_slist_append(sdi->channel_groups,
640 devc->digital_groups[i < 8 ? 0 : 1]);
643 /* Add digital channels. */
644 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
645 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
646 (*scope_models[model_index].digital_names)[i]);
648 devc->digital_groups[i < 8 ? 0 : 1]->channels = g_slist_append(
649 devc->digital_groups[i < 8 ? 0 : 1]->channels, ch);
652 devc->model_config = &scope_models[model_index];
653 devc->frame_limit = 0;
655 if (!(devc->model_state = scope_state_new(devc->model_config)))
656 return SR_ERR_MALLOC;
661 SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
663 struct sr_channel *ch;
664 struct sr_dev_inst *sdi;
665 struct dev_context *devc;
666 struct sr_datafeed_packet packet;
668 struct sr_datafeed_analog analog;
669 struct sr_datafeed_logic logic;
675 if (!(sdi = cb_data))
678 if (!(devc = sdi->priv))
681 if (revents == G_IO_IN) {
682 ch = devc->current_channel->data;
685 case SR_CHANNEL_ANALOG:
686 if (sr_scpi_get_floatv(sdi->conn, NULL, &data) != SR_OK) {
688 g_array_free(data, TRUE);
693 packet.type = SR_DF_FRAME_BEGIN;
694 sr_session_send(sdi, &packet);
696 analog.channels = g_slist_append(NULL, ch);
697 analog.num_samples = data->len;
698 analog.data = (float *) data->data;
699 analog.mq = SR_MQ_VOLTAGE;
700 analog.unit = SR_UNIT_VOLT;
702 packet.type = SR_DF_ANALOG;
703 packet.payload = &analog;
704 sr_session_send(cb_data, &packet);
705 g_slist_free(analog.channels);
706 g_array_free(data, TRUE);
709 case SR_CHANNEL_LOGIC:
710 if (sr_scpi_get_uint8v(sdi->conn, NULL, &data) != SR_OK) {
716 packet.type = SR_DF_FRAME_BEGIN;
717 sr_session_send(sdi, &packet);
719 logic.length = data->len;
721 logic.data = data->data;
722 packet.type = SR_DF_LOGIC;
723 packet.payload = &logic;
724 sr_session_send(cb_data, &packet);
725 g_array_free(data, TRUE);
729 sr_err("Invalid channel type.");
733 packet.type = SR_DF_FRAME_END;
734 sr_session_send(sdi, &packet);
736 if (devc->current_channel->next) {
737 devc->current_channel = devc->current_channel->next;
738 hmo_request_data(sdi);
739 } else if (++devc->num_frames == devc->frame_limit) {
740 sdi->driver->dev_acquisition_stop(sdi, cb_data);
742 devc->current_channel = devc->enabled_channels;
743 hmo_request_data(sdi);