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 *manufacturers[] = {
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 %E",
30 [SCPI_CMD_GET_COUPLING] = ":CHAN%d:COUP?",
31 [SCPI_CMD_SET_COUPLING] = ":CHAN%d:COUP %s",
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 %E",
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 %E",
46 [SCPI_CMD_GET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT?",
47 [SCPI_CMD_SET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT %d",
50 static const int32_t hmo_hwcaps[] = {
52 SR_CONF_TRIGGER_SOURCE,
55 SR_CONF_TRIGGER_SLOPE,
56 SR_CONF_HORIZ_TRIGGERPOS,
59 static const int32_t hmo_analog_caps[] = {
65 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 int check_manufacturer(const char *manufacturer)
263 for (i = 0; i < ARRAY_SIZE(manufacturers); ++i)
264 if (!strcmp(manufacturer, manufacturers[i]))
270 static void scope_state_dump(struct scope_config *config,
271 struct scope_state *state)
275 for (i = 0; i < config->analog_channels; ++i) {
276 sr_info("State of analog channel %d -> %s : %s %.3eV %.3e offset", i + 1,
277 state->analog_channels[i].state ? "On" : "Off",
278 (*config->coupling_options)[state->analog_channels[i].coupling],
279 state->analog_channels[i].vdiv, state->analog_channels[i].vertical_offset);
282 for (i = 0; i < config->digital_channels; ++i) {
283 sr_info("State of digital channel %d -> %s", i,
284 state->digital_channels[i] ? "On" : "Off");
287 for (i = 0; i < config->digital_pods; ++i) {
288 sr_info("State of digital POD %d -> %s", i,
289 state->digital_pods[i] ? "On" : "Off");
292 sr_info("Current timebase: %.2es", state->timebase);
293 sr_info("Current trigger: %s (source), %s (slope) %.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_serial_dev_inst *serial,
300 const char *command, const char *(*array)[], int *result)
305 if (sr_scpi_get_string(serial, 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_serial_dev_inst *serial,
328 struct scope_config *config,
329 struct scope_state *state)
332 char command[MAX_COMMAND_SIZE];
334 for (i = 0; i < config->analog_channels; ++i) {
335 g_snprintf(command, sizeof(command),
336 (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
339 if (sr_scpi_get_bool(serial, command,
340 &state->analog_channels[i].state) != SR_OK)
343 g_snprintf(command, sizeof(command),
344 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
347 if (sr_scpi_get_float(serial, command,
348 &state->analog_channels[i].vdiv) != SR_OK)
351 g_snprintf(command, sizeof(command),
352 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
355 if (sr_scpi_get_float(serial, command,
356 &state->analog_channels[i].vertical_offset) != SR_OK)
359 g_snprintf(command, sizeof(command),
360 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
363 if (scope_state_get_array_option(serial, command, config->coupling_options,
364 &state->analog_channels[i].coupling) != SR_OK)
371 static int digital_channel_state_get(struct sr_serial_dev_inst *serial,
372 struct scope_config *config,
373 struct scope_state *state)
376 char command[MAX_COMMAND_SIZE];
378 for (i = 0; i < config->digital_channels; ++i) {
379 g_snprintf(command, sizeof(command),
380 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
383 if (sr_scpi_get_bool(serial, command,
384 &state->digital_channels[i]) != SR_OK)
388 for (i = 0; i < config->digital_pods; ++i) {
389 g_snprintf(command, sizeof(command),
390 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
393 if (sr_scpi_get_bool(serial, command,
394 &state->digital_pods[i]) != SR_OK)
401 SR_PRIV int scope_state_get(struct sr_dev_inst *sdi)
403 struct dev_context *devc;
404 struct scope_state *state;
405 struct scope_config *config;
408 config = devc->model_config;
409 state = devc->model_state;
411 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
414 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
417 /* TODO: Check if value is sensible. */
418 if (sr_scpi_get_float(sdi->conn,
419 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
420 &state->timebase) != SR_OK)
423 if (sr_scpi_get_float(sdi->conn,
424 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
425 &state->horiz_triggerpos) != SR_OK)
428 if (scope_state_get_array_option(sdi->conn,
429 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
430 config->trigger_sources, &state->trigger_source) != SR_OK)
433 if (scope_state_get_array_option(sdi->conn,
434 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
435 config->trigger_slopes, &state->trigger_slope) != SR_OK)
438 scope_state_dump(config, state);
443 SR_PRIV struct scope_state *scope_state_new(struct scope_config *config)
445 struct scope_state *state;
447 if (!(state = g_try_malloc0(sizeof(struct scope_state))))
450 if (!(state->analog_channels = g_try_malloc0_n(config->analog_channels,
451 sizeof(struct analog_channel_state))))
454 if (!(state->digital_channels = g_try_malloc0_n(
455 config->digital_channels, sizeof(gboolean))))
458 if (!(state->digital_pods = g_try_malloc0_n(config->digital_pods,
465 if (state->analog_channels)
466 g_free(state->analog_channels);
467 if (state->digital_channels)
468 g_free(state->digital_channels);
469 if (state->digital_pods)
470 g_free(state->digital_pods);
476 SR_PRIV void scope_state_free(struct scope_state *state)
478 g_free(state->analog_channels);
479 g_free(state->digital_channels);
480 g_free(state->digital_pods);
484 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
489 struct sr_probe *probe;
490 struct dev_context *devc;
495 /* Find the exact model. */
496 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
497 for (j = 0; scope_models[i].name[j]; j++) {
498 if (!strcmp(sdi->model, scope_models[i].name[j])) {
503 if (model_index != -1)
507 if (model_index == -1) {
508 sr_dbg("Unsupported HMO device.");
512 if (!(devc->analog_groups = g_try_malloc0(sizeof(struct sr_probe_group) *
513 scope_models[model_index].analog_channels)))
514 return SR_ERR_MALLOC;
516 if (!(devc->digital_groups = g_try_malloc0(sizeof(struct sr_probe_group) *
517 scope_models[model_index].digital_pods)))
518 return SR_ERR_MALLOC;
520 /* Add analog channels. */
521 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
522 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
523 (*scope_models[model_index].analog_names)[i])))
524 return SR_ERR_MALLOC;
525 sdi->probes = g_slist_append(sdi->probes, probe);
527 devc->analog_groups[i].name =
528 (char *)(*scope_models[model_index].analog_names)[i];
529 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
531 sdi->probe_groups = g_slist_append(sdi->probe_groups,
532 &devc->analog_groups[i]);
535 /* Add digital probe groups. */
536 for (i = 0; i < scope_models[model_index].digital_pods; ++i) {
537 g_snprintf(tmp, 25, "POD%d", i);
538 devc->digital_groups[i].name = g_strdup(tmp);
539 sdi->probe_groups = g_slist_append(sdi->probe_groups,
540 &devc->digital_groups[i < 8 ? 0 : 1]);
543 /* Add digital channels. */
544 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
545 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
546 (*scope_models[model_index].digital_names)[i])))
547 return SR_ERR_MALLOC;
548 sdi->probes = g_slist_append(sdi->probes, probe);
550 devc->digital_groups[i < 8 ? 0 : 1].probes = g_slist_append(
551 devc->digital_groups[i < 8 ? 0 : 1].probes, probe);
554 devc->model_config = &scope_models[model_index];
555 devc->frame_limit = 0;
557 if (!(devc->model_state = scope_state_new(devc->model_config)))
558 return SR_ERR_MALLOC;
563 SR_PRIV struct sr_dev_inst *hameg_probe_serial_device(const char *serial_device,
564 const char *serial_options)
566 struct sr_dev_inst *sdi;
567 struct dev_context *devc;
568 struct sr_scpi_hw_info *hw_info;
569 struct sr_serial_dev_inst *serial;
576 if (!(serial = sr_serial_dev_inst_new(serial_device, serial_options)))
579 sr_info("Probing %s.", serial_device);
580 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
583 if (sr_scpi_get_hw_id(serial, &hw_info) != SR_OK) {
584 sr_info("Couldn't get IDN response.");
588 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
591 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
592 hw_info->manufacturer, hw_info->model,
593 hw_info->firmware_version))) {
596 sr_scpi_hw_info_free(hw_info);
599 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
604 sdi->inst_type = SR_INST_SERIAL;
607 if (hmo_init_device(sdi) != SR_OK)
614 sr_scpi_hw_info_free(hw_info);
616 sr_serial_dev_inst_free(serial);
618 sr_dev_inst_free(sdi);
625 SR_PRIV int hameg_hmo_receive_data(int fd, int revents, void *cb_data)
627 struct sr_probe *probe;
628 struct sr_dev_inst *sdi;
629 struct dev_context *devc;
630 struct sr_datafeed_packet packet;
632 struct sr_datafeed_analog analog;
633 struct sr_datafeed_logic logic;
637 if (!(sdi = cb_data))
640 if (!(devc = sdi->priv))
643 if (revents == G_IO_IN) {
644 probe = devc->current_probe->data;
646 switch (probe->type) {
647 case SR_PROBE_ANALOG:
648 if (sr_scpi_get_floatv(sdi->conn, NULL, &data) != SR_OK) {
650 g_array_free(data, TRUE);
655 packet.type = SR_DF_FRAME_BEGIN;
656 sr_session_send(sdi, &packet);
658 analog.probes = g_slist_append(NULL, probe);
659 analog.num_samples = data->len;
660 analog.data = (float *) data->data;
661 analog.mq = SR_MQ_VOLTAGE;
662 analog.unit = SR_UNIT_VOLT;
664 packet.type = SR_DF_ANALOG;
665 packet.payload = &analog;
666 sr_session_send(cb_data, &packet);
667 g_slist_free(analog.probes);
668 g_array_free(data, TRUE);
671 if (sr_scpi_get_uint8v(sdi->conn, NULL, &data) != SR_OK) {
677 packet.type = SR_DF_FRAME_BEGIN;
678 sr_session_send(sdi, &packet);
680 logic.length = data->len;
682 logic.data = data->data;
683 packet.type = SR_DF_LOGIC;
684 packet.payload = &logic;
685 sr_session_send(cb_data, &packet);
686 g_array_free(data, TRUE);
689 sr_err("Invalid probe type.");
693 packet.type = SR_DF_FRAME_END;
694 sr_session_send(sdi, &packet);
696 if (devc->current_probe->next) {
697 devc->current_probe = devc->current_probe->next;
698 hmo_request_data(sdi);
699 } else if (++devc->num_frames == devc->frame_limit) {
700 packet.type = SR_DF_END;
701 sr_session_send(sdi, &packet);
702 sdi->driver->dev_acquisition_stop(sdi, cb_data);
704 devc->current_probe = devc->enabled_probes;
705 hmo_request_data(sdi);