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 #define SERIALCOMM "115200/8n1/flow=1"
25 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info;
26 static struct sr_dev_driver *di = &hameg_hmo_driver_info;
28 static const char *manufacturers[] = {
32 static const int32_t hwopts[] = {
44 static int init(struct sr_context *sr_ctx)
46 return std_init(sr_ctx, di, LOG_PREFIX);
49 static int check_manufacturer(const char *manufacturer)
53 for (i = 0; i < ARRAY_SIZE(manufacturers); ++i)
54 if (!strcmp(manufacturer, manufacturers[i]))
60 static struct sr_dev_inst *hmo_probe_serial_device(struct sr_scpi_dev_inst *scpi)
62 struct sr_dev_inst *sdi;
63 struct dev_context *devc;
64 struct sr_scpi_hw_info *hw_info;
70 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
71 sr_info("Couldn't get IDN response.");
75 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
78 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
79 hw_info->manufacturer, hw_info->model,
80 hw_info->firmware_version))) {
83 sr_scpi_hw_info_free(hw_info);
86 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
91 sdi->inst_type = SR_INST_SCPI;
94 if (hmo_init_device(sdi) != SR_OK)
97 sr_scpi_close(sdi->conn);
99 sdi->status = SR_ST_INACTIVE;
105 sr_scpi_hw_info_free(hw_info);
107 sr_dev_inst_free(sdi);
114 static GSList *scan(GSList *options)
116 return sr_scpi_scan(di->priv, options, hmo_probe_serial_device);
119 static GSList *dev_list(void)
121 return ((struct drv_context *)(di->priv))->instances;
124 static void clear_helper(void *priv)
127 struct dev_context *devc;
128 struct scope_config *model;
131 model = devc->model_config;
133 hmo_scope_state_free(devc->model_state);
135 for (i = 0; i < model->analog_channels; ++i)
136 g_slist_free(devc->analog_groups[i].probes);
138 for (i = 0; i < model->digital_pods; ++i) {
139 g_slist_free(devc->digital_groups[i].probes);
140 g_free(devc->digital_groups[i].name);
143 g_free(devc->analog_groups);
144 g_free(devc->digital_groups);
149 static int dev_clear(void)
151 return std_dev_clear(di, clear_helper);
154 static int dev_open(struct sr_dev_inst *sdi)
156 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
159 if (hmo_scope_state_get(sdi) != SR_OK)
162 sdi->status = SR_ST_ACTIVE;
167 static int dev_close(struct sr_dev_inst *sdi)
169 if (sdi->status == SR_ST_INACTIVE)
172 sr_scpi_close(sdi->conn);
174 sdi->status = SR_ST_INACTIVE;
179 static int cleanup(void)
186 static int check_probe_group(struct dev_context *devc,
187 const struct sr_probe_group *probe_group)
190 struct scope_config *model;
192 model = devc->model_config;
197 for (i = 0; i < model->analog_channels; ++i)
198 if (probe_group == &devc->analog_groups[i])
201 for (i = 0; i < model->digital_pods; ++i)
202 if (probe_group == &devc->digital_groups[i])
205 sr_err("Invalid probe group specified.");
210 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
211 const struct sr_probe_group *probe_group)
215 struct dev_context *devc;
216 struct scope_config *model;
217 struct scope_state *state;
219 if (!sdi || !(devc = sdi->priv))
222 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
226 model = devc->model_config;
227 state = devc->model_state;
230 case SR_CONF_NUM_TIMEBASE:
231 *data = g_variant_new_int32(model->num_xdivs);
234 case SR_CONF_NUM_VDIV:
235 if (pg_type == PG_NONE) {
236 sr_err("No probe group specified.");
237 return SR_ERR_PROBE_GROUP;
238 } else if (pg_type == PG_ANALOG) {
239 for (i = 0; i < model->analog_channels; ++i) {
240 if (probe_group != &devc->analog_groups[i])
242 *data = g_variant_new_int32(model->num_ydivs);
251 case SR_CONF_TRIGGER_SOURCE:
252 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
255 case SR_CONF_COUPLING:
256 if (pg_type == PG_NONE) {
257 sr_err("No probe group specified.");
258 return SR_ERR_PROBE_GROUP;
259 } else if (pg_type == PG_ANALOG) {
260 for (i = 0; i < model->analog_channels; ++i) {
261 if (probe_group != &devc->analog_groups[i])
263 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
272 case SR_CONF_SAMPLERATE:
273 *data = g_variant_new_uint64(state->sample_rate);
283 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
286 GVariant *rational[2];
289 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
291 for (i = 0; i < n; i++) {
292 rational[0] = g_variant_new_uint64((*array)[i][0]);
293 rational[1] = g_variant_new_uint64((*array)[i][1]);
295 /* FIXME: Valgrind reports a memory leak here. */
296 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
299 return g_variant_builder_end(&gvb);
302 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
303 const struct sr_probe_group *probe_group)
307 char command[MAX_COMMAND_SIZE], float_str[30];
308 struct dev_context *devc;
309 struct scope_config *model;
310 struct scope_state *state;
314 gboolean update_sample_rate;
316 if (!sdi || !(devc = sdi->priv))
319 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
322 model = devc->model_config;
323 state = devc->model_state;
324 update_sample_rate = FALSE;
329 case SR_CONF_LIMIT_FRAMES:
330 devc->frame_limit = g_variant_get_uint64(data);
333 case SR_CONF_TRIGGER_SOURCE:
334 tmp = g_variant_get_string(data, NULL);
335 for (i = 0; (*model->trigger_sources)[i]; i++) {
336 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
338 state->trigger_source = i;
339 g_snprintf(command, sizeof(command),
340 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
341 (*model->trigger_sources)[i]);
343 ret = sr_scpi_send(sdi->conn, command);
348 if (pg_type == PG_NONE) {
349 sr_err("No probe group specified.");
350 return SR_ERR_PROBE_GROUP;
353 g_variant_get(data, "(tt)", &p, &q);
355 for (i = 0; i < model->num_vdivs; i++) {
356 if (p != (*model->vdivs)[i][0] ||
357 q != (*model->vdivs)[i][1])
359 for (j = 1; j <= model->analog_channels; ++j) {
360 if (probe_group != &devc->analog_groups[j - 1])
362 state->analog_channels[j - 1].vdiv = i;
363 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
364 g_snprintf(command, sizeof(command),
365 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
368 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
369 sr_scpi_get_opc(sdi->conn) != SR_OK)
379 case SR_CONF_TIMEBASE:
380 g_variant_get(data, "(tt)", &p, &q);
382 for (i = 0; i < model->num_timebases; i++) {
383 if (p != (*model->timebases)[i][0] ||
384 q != (*model->timebases)[i][1])
387 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
388 g_snprintf(command, sizeof(command),
389 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
392 ret = sr_scpi_send(sdi->conn, command);
393 update_sample_rate = TRUE;
397 case SR_CONF_HORIZ_TRIGGERPOS:
398 tmp_d = g_variant_get_double(data);
400 if (tmp_d < 0.0 || tmp_d > 1.0)
403 state->horiz_triggerpos = tmp_d;
404 tmp_d = -(tmp_d - 0.5) *
405 ((double) (*model->timebases)[state->timebase][0] /
406 (*model->timebases)[state->timebase][1])
409 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
410 g_snprintf(command, sizeof(command),
411 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
414 ret = sr_scpi_send(sdi->conn, command);
416 case SR_CONF_TRIGGER_SLOPE:
417 tmp = g_variant_get_string(data, NULL);
419 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
422 state->trigger_slope = (tmp[0] == 'r') ? 0 : 1;
424 g_snprintf(command, sizeof(command),
425 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
426 (state->trigger_slope == 0) ? "POS" : "NEG");
428 ret = sr_scpi_send(sdi->conn, command);
430 case SR_CONF_COUPLING:
431 if (pg_type == PG_NONE) {
432 sr_err("No probe group specified.");
433 return SR_ERR_PROBE_GROUP;
436 tmp = g_variant_get_string(data, NULL);
438 for (i = 0; (*model->coupling_options)[i]; i++) {
439 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
441 for (j = 1; j <= model->analog_channels; ++j) {
442 if (probe_group != &devc->analog_groups[j - 1])
444 state->analog_channels[j-1].coupling = i;
446 g_snprintf(command, sizeof(command),
447 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
450 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
451 sr_scpi_get_opc(sdi->conn) != SR_OK)
466 ret = sr_scpi_get_opc(sdi->conn);
468 if (ret == SR_OK && update_sample_rate)
469 ret = hmo_update_sample_rate(sdi);
474 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
475 const struct sr_probe_group *probe_group)
478 struct dev_context *devc;
479 struct scope_config *model;
481 if (!sdi || !(devc = sdi->priv))
484 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
487 model = devc->model_config;
490 case SR_CONF_DEVICE_OPTIONS:
491 if (pg_type == PG_NONE) {
492 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
493 model->hw_caps, model->num_hwcaps,
495 } else if (pg_type == PG_ANALOG) {
496 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
497 model->analog_hwcaps, model->num_analog_hwcaps,
500 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
501 NULL, 0, sizeof(int32_t));
504 case SR_CONF_COUPLING:
505 if (pg_type == PG_NONE)
506 return SR_ERR_PROBE_GROUP;
507 *data = g_variant_new_strv(*model->coupling_options,
508 g_strv_length((char **)*model->coupling_options));
510 case SR_CONF_TRIGGER_SOURCE:
511 *data = g_variant_new_strv(*model->trigger_sources,
512 g_strv_length((char **)*model->trigger_sources));
514 case SR_CONF_TRIGGER_SLOPE:
515 *data = g_variant_new_strv(*model->trigger_slopes,
516 g_strv_length((char **)*model->trigger_slopes));
518 case SR_CONF_TIMEBASE:
519 *data = build_tuples(model->timebases, model->num_timebases);
522 if (pg_type == PG_NONE)
523 return SR_ERR_PROBE_GROUP;
524 *data = build_tuples(model->vdivs, model->num_vdivs);
533 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
535 char command[MAX_COMMAND_SIZE];
536 struct sr_probe *probe;
537 struct dev_context *devc;
538 struct scope_config *model;
541 model = devc->model_config;
543 probe = devc->current_probe->data;
545 switch (probe->type) {
546 case SR_PROBE_ANALOG:
547 g_snprintf(command, sizeof(command),
548 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
552 g_snprintf(command, sizeof(command),
553 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
554 probe->index < 8 ? 1 : 2);
557 sr_err("Invalid probe type.");
561 return sr_scpi_send(sdi->conn, command);
564 static int hmo_check_probes(GSList *probes)
567 struct sr_probe *probe;
568 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
570 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
572 for (l = probes; l; l = l->next) {
574 switch (probe->type) {
575 case SR_PROBE_ANALOG:
576 if (probe->index == 2)
577 enabled_chan3 = TRUE;
578 else if (probe->index == 3)
579 enabled_chan4 = TRUE;
582 if (probe->index < 8)
592 if ((enabled_pod1 && enabled_chan3) ||
593 (enabled_pod2 && enabled_chan4))
599 static int hmo_setup_probes(const struct sr_dev_inst *sdi)
603 gboolean *pod_enabled, setup_changed;
604 char command[MAX_COMMAND_SIZE];
605 struct scope_state *state;
606 struct scope_config *model;
607 struct sr_probe *probe;
608 struct dev_context *devc;
609 struct sr_scpi_dev_inst *scpi;
613 state = devc->model_state;
614 model = devc->model_config;
615 setup_changed = FALSE;
617 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
619 for (l = sdi->probes; l; l = l->next) {
621 switch (probe->type) {
622 case SR_PROBE_ANALOG:
623 if (probe->enabled == state->analog_channels[probe->index].state)
625 g_snprintf(command, sizeof(command),
626 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
627 probe->index + 1, probe->enabled);
629 if (sr_scpi_send(scpi, command) != SR_OK)
631 state->analog_channels[probe->index].state = probe->enabled;
632 setup_changed = TRUE;
636 * A digital POD needs to be enabled for every group of
640 pod_enabled[probe->index < 8 ? 0 : 1] = TRUE;
642 if (probe->enabled == state->digital_channels[probe->index])
644 g_snprintf(command, sizeof(command),
645 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
646 probe->index, probe->enabled);
648 if (sr_scpi_send(scpi, command) != SR_OK)
651 state->digital_channels[probe->index] = probe->enabled;
652 setup_changed = TRUE;
659 for (i = 1; i <= model->digital_pods; ++i) {
660 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
662 g_snprintf(command, sizeof(command),
663 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
664 i, pod_enabled[i - 1]);
665 if (sr_scpi_send(scpi, command) != SR_OK)
667 state->digital_pods[i - 1] = pod_enabled[i - 1];
668 setup_changed = TRUE;
673 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
679 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
682 gboolean digital_added;
683 struct sr_probe *probe;
684 struct dev_context *devc;
685 struct sr_scpi_dev_inst *scpi;
687 if (sdi->status != SR_ST_ACTIVE)
688 return SR_ERR_DEV_CLOSED;
692 digital_added = FALSE;
694 for (l = sdi->probes; l; l = l->next) {
698 /* Only add a single digital probe. */
699 if (probe->type != SR_PROBE_LOGIC || !digital_added) {
700 devc->enabled_probes = g_slist_append(
701 devc->enabled_probes, probe);
702 if (probe->type == SR_PROBE_LOGIC)
703 digital_added = TRUE;
707 if (!devc->enabled_probes)
710 if (hmo_check_probes(devc->enabled_probes) != SR_OK) {
711 sr_err("Invalid probe configuration specified!");
715 if (hmo_setup_probes(sdi) != SR_OK) {
716 sr_err("Failed to setup probe configuration!");
720 sr_scpi_source_add(scpi, G_IO_IN, 50, hmo_receive_data, (void *)sdi);
722 /* Send header packet to the session bus. */
723 std_session_send_df_header(cb_data, LOG_PREFIX);
725 devc->current_probe = devc->enabled_probes;
727 return hmo_request_data(sdi);
730 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
732 struct dev_context *devc;
733 struct sr_scpi_dev_inst *scpi;
734 struct sr_datafeed_packet packet;
738 packet.type = SR_DF_END;
739 packet.payload = NULL;
740 sr_session_send(sdi, &packet);
742 if (sdi->status != SR_ST_ACTIVE)
743 return SR_ERR_DEV_CLOSED;
747 devc->num_frames = 0;
748 g_slist_free(devc->enabled_probes);
749 devc->enabled_probes = NULL;
751 sr_scpi_source_remove(scpi);
756 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
758 .longname = "Hameg HMO",
763 .dev_list = dev_list,
764 .dev_clear = dev_clear,
765 .config_get = config_get,
766 .config_set = config_set,
767 .config_list = config_list,
768 .dev_open = dev_open,
769 .dev_close = dev_close,
770 .dev_acquisition_start = dev_acquisition_start,
771 .dev_acquisition_stop = dev_acquisition_stop,