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]) == 0)
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)[],
306 if (sr_scpi_get_string(serial, 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_serial_dev_inst *serial,
330 struct scope_config *config,
331 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(serial, 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(serial, command,
350 &state->analog_channels[i].vdiv) != SR_OK)
353 g_snprintf(command, sizeof(command),
354 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
357 if (sr_scpi_get_float(serial, command,
358 &state->analog_channels[i].vertical_offset) != SR_OK)
361 g_snprintf(command, sizeof(command),
362 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
365 if (scope_state_get_array_option(serial, command, config->coupling_options,
366 &state->analog_channels[i].coupling) != SR_OK)
373 static int digital_channel_state_get(struct sr_serial_dev_inst *serial,
374 struct scope_config *config,
375 struct scope_state *state)
378 char command[MAX_COMMAND_SIZE];
380 for (i = 0; i < config->digital_channels; ++i) {
381 g_snprintf(command, sizeof(command),
382 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
385 if (sr_scpi_get_bool(serial, command,
386 &state->digital_channels[i]) != SR_OK)
390 for (i = 0; i < config->digital_pods; ++i) {
391 g_snprintf(command, sizeof(command),
392 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
395 if (sr_scpi_get_bool(serial, command,
396 &state->digital_pods[i]) != SR_OK)
403 SR_PRIV int scope_state_get(struct sr_dev_inst *sdi)
405 struct dev_context *devc;
406 struct scope_state *state;
407 struct scope_config *config;
410 config = devc->model_config;
411 state = devc->model_state;
413 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
416 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
419 /* TODO check if value is sensible */
420 if (sr_scpi_get_float(sdi->conn, (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
421 &state->timebase) != SR_OK)
424 if (sr_scpi_get_float(sdi->conn, (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
425 &state->horiz_triggerpos) != SR_OK)
428 if (scope_state_get_array_option(sdi->conn, (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
429 config->trigger_sources, &state->trigger_source) != SR_OK)
432 if (scope_state_get_array_option(sdi->conn, (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
433 config->trigger_slopes, &state->trigger_slope) != SR_OK)
436 scope_state_dump(config, state);
441 SR_PRIV struct scope_state *scope_state_new(struct scope_config *config)
443 struct scope_state *state;
445 if (!(state = g_try_malloc0(sizeof(struct scope_state))))
448 if (!(state->analog_channels = g_try_malloc0_n(config->analog_channels,
449 sizeof(struct analog_channel_state))))
452 if (!(state->digital_channels = g_try_malloc0_n(config->digital_channels,
456 if (!(state->digital_pods = g_try_malloc0_n(config->digital_pods,
463 if (state->analog_channels)
464 g_free(state->analog_channels);
465 if (state->digital_channels)
466 g_free(state->digital_channels);
467 if (state->digital_pods)
468 g_free(state->digital_pods);
474 SR_PRIV void scope_state_free(struct scope_state *state)
476 g_free(state->analog_channels);
477 g_free(state->digital_channels);
478 g_free(state->digital_pods);
482 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 = (char *) (*scope_models[model_index].analog_names)[i];
528 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
530 sdi->probe_groups = g_slist_append(sdi->probe_groups,
531 &devc->analog_groups[i]);
534 /* Add digital probe groups */
535 for (i = 0; i < scope_models[model_index].digital_pods; ++i) {
536 g_snprintf(tmp, 25, "POD%d", i);
537 devc->digital_groups[i].name = g_strdup(tmp);
538 sdi->probe_groups = g_slist_append(sdi->probe_groups,
539 &devc->digital_groups[i < 8 ? 0 : 1]);
542 /* Add digital channels */
543 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
544 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
545 (*scope_models[model_index].digital_names)[i])))
546 return SR_ERR_MALLOC;
547 sdi->probes = g_slist_append(sdi->probes, probe);
549 devc->digital_groups[i < 8 ? 0 : 1].probes = g_slist_append(devc->digital_groups[i < 8 ? 0 : 1].probes,
553 devc->model_config = &scope_models[model_index];
554 devc->frame_limit = 0;
556 if (!(devc->model_state = scope_state_new(devc->model_config)))
557 return SR_ERR_MALLOC;
562 SR_PRIV struct sr_dev_inst *hameg_probe_serial_device(const char *serial_device,
563 const char *serial_options)
565 struct sr_dev_inst *sdi;
566 struct dev_context *devc;
567 struct sr_scpi_hw_info *hw_info;
568 struct sr_serial_dev_inst *serial;
575 if (!(serial = sr_serial_dev_inst_new(serial_device, serial_options)))
578 sr_info("Probing %s.", serial_device);
579 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
582 if (sr_scpi_get_hw_id(serial, &hw_info) != SR_OK) {
583 sr_info("Couldn't get IDN response");
587 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
590 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
591 hw_info->manufacturer, hw_info->model,
592 hw_info->firmware_version))) {
595 sr_scpi_hw_info_free(hw_info);
598 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
603 sdi->inst_type = SR_INST_SERIAL;
606 if (hmo_init_device(sdi) != SR_OK)
613 sr_scpi_hw_info_free(hw_info);
615 sr_serial_dev_inst_free(serial);
617 sr_dev_inst_free(sdi);
624 SR_PRIV int hameg_hmo_receive_data(int fd, int revents, void *cb_data)
626 struct sr_probe *probe;
627 struct sr_dev_inst *sdi;
628 struct dev_context *devc;
629 struct sr_datafeed_packet packet;
633 if (!(sdi = cb_data))
636 if (!(devc = sdi->priv))
639 if (revents == G_IO_IN) {
640 probe = devc->current_probe->data;
642 switch (probe->type) {
643 case SR_PROBE_ANALOG:
646 struct sr_datafeed_analog 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);
675 struct sr_datafeed_logic logic;
677 if (sr_scpi_get_uint8v(sdi->conn, NULL, &data) != SR_OK) {
683 packet.type = SR_DF_FRAME_BEGIN;
684 sr_session_send(sdi, &packet);
686 logic.length = data->len;
688 logic.data = data->data;
689 packet.type = SR_DF_LOGIC;
690 packet.payload = &logic;
691 sr_session_send(cb_data, &packet);
692 g_array_free(data, TRUE);
697 sr_err("Invalid probe type");
701 packet.type = SR_DF_FRAME_END;
702 sr_session_send(sdi, &packet);
704 if (devc->current_probe->next) {
705 devc->current_probe = devc->current_probe->next;
706 hmo_request_data(sdi);
707 } else if (++devc->num_frames == devc->frame_limit) {
708 packet.type = SR_DF_END;
709 sr_session_send(sdi, &packet);
710 sdi->driver->dev_acquisition_stop(sdi, cb_data);
712 devc->current_probe = devc->enabled_probes;
713 hmo_request_data(sdi);