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;
27 static const char *manufacturers[] = {
31 static const uint32_t drvopts[] = {
35 static const uint32_t scanopts[] = {
47 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
49 return std_init(sr_ctx, di, LOG_PREFIX);
52 static int check_manufacturer(const char *manufacturer)
56 for (i = 0; i < ARRAY_SIZE(manufacturers); ++i)
57 if (!strcmp(manufacturer, manufacturers[i]))
63 static struct sr_dev_inst *hmo_probe_serial_device(struct sr_scpi_dev_inst *scpi)
65 struct sr_dev_inst *sdi;
66 struct dev_context *devc;
67 struct sr_scpi_hw_info *hw_info;
73 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
74 sr_info("Couldn't get IDN response.");
78 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
81 sdi = g_malloc0(sizeof(struct sr_dev_inst));
82 sdi->status = SR_ST_ACTIVE;
83 sdi->vendor = g_strdup(hw_info->manufacturer);
84 sdi->model = g_strdup(hw_info->model);
85 sdi->version = g_strdup(hw_info->firmware_version);
86 sdi->serial_num = g_strdup(hw_info->serial_number);
87 sdi->driver = &hameg_hmo_driver_info;
88 sdi->inst_type = SR_INST_SCPI;
91 sr_scpi_hw_info_free(hw_info);
94 devc = g_malloc0(sizeof(struct dev_context));
98 if (hmo_init_device(sdi) != SR_OK)
101 sr_scpi_close(sdi->conn);
103 sdi->status = SR_ST_INACTIVE;
109 sr_scpi_hw_info_free(hw_info);
111 sr_dev_inst_free(sdi);
118 static GSList *scan(struct sr_dev_driver *di, GSList *options)
120 return sr_scpi_scan(di->priv, options, hmo_probe_serial_device);
123 static GSList *dev_list(const struct sr_dev_driver *di)
125 return ((struct drv_context *)(di->priv))->instances;
128 static void clear_helper(void *priv)
130 struct dev_context *devc;
134 hmo_scope_state_free(devc->model_state);
136 g_free(devc->analog_groups);
137 g_free(devc->digital_groups);
142 static int dev_clear(const struct sr_dev_driver *di)
144 return std_dev_clear(di, clear_helper);
147 static int dev_open(struct sr_dev_inst *sdi)
149 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
152 if (hmo_scope_state_get(sdi) != SR_OK)
155 sdi->status = SR_ST_ACTIVE;
160 static int dev_close(struct sr_dev_inst *sdi)
162 if (sdi->status == SR_ST_INACTIVE)
165 sr_scpi_close(sdi->conn);
167 sdi->status = SR_ST_INACTIVE;
172 static int cleanup(const struct sr_dev_driver *di)
179 static int check_channel_group(struct dev_context *devc,
180 const struct sr_channel_group *cg)
183 const struct scope_config *model;
185 model = devc->model_config;
190 for (i = 0; i < model->analog_channels; ++i)
191 if (cg == devc->analog_groups[i])
194 for (i = 0; i < model->digital_pods; ++i)
195 if (cg == devc->digital_groups[i])
198 sr_err("Invalid channel group specified.");
203 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
204 const struct sr_channel_group *cg)
208 struct dev_context *devc;
209 const struct scope_config *model;
210 struct scope_state *state;
212 if (!sdi || !(devc = sdi->priv))
215 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
219 model = devc->model_config;
220 state = devc->model_state;
223 case SR_CONF_NUM_HDIV:
224 *data = g_variant_new_int32(model->num_xdivs);
227 case SR_CONF_TIMEBASE:
228 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
229 (*model->timebases)[state->timebase][1]);
232 case SR_CONF_NUM_VDIV:
233 if (cg_type == CG_NONE) {
234 sr_err("No channel group specified.");
235 return SR_ERR_CHANNEL_GROUP;
236 } else if (cg_type == CG_ANALOG) {
237 for (i = 0; i < model->analog_channels; ++i) {
238 if (cg != devc->analog_groups[i])
240 *data = g_variant_new_int32(model->num_ydivs);
250 if (cg_type == CG_NONE) {
251 sr_err("No channel group specified.");
252 return SR_ERR_CHANNEL_GROUP;
253 } else if (cg_type == CG_ANALOG) {
254 for (i = 0; i < model->analog_channels; ++i) {
255 if (cg != devc->analog_groups[i])
257 *data = g_variant_new("(tt)",
258 (*model->vdivs)[state->analog_channels[i].vdiv][0],
259 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
268 case SR_CONF_TRIGGER_SOURCE:
269 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
272 case SR_CONF_TRIGGER_SLOPE:
273 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
276 case SR_CONF_HORIZ_TRIGGERPOS:
277 *data = g_variant_new_double(state->horiz_triggerpos);
280 case SR_CONF_COUPLING:
281 if (cg_type == CG_NONE) {
282 sr_err("No channel group specified.");
283 return SR_ERR_CHANNEL_GROUP;
284 } else if (cg_type == CG_ANALOG) {
285 for (i = 0; i < model->analog_channels; ++i) {
286 if (cg != devc->analog_groups[i])
288 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
297 case SR_CONF_SAMPLERATE:
298 *data = g_variant_new_uint64(state->sample_rate);
308 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
311 GVariant *rational[2];
314 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
316 for (i = 0; i < n; i++) {
317 rational[0] = g_variant_new_uint64((*array)[i][0]);
318 rational[1] = g_variant_new_uint64((*array)[i][1]);
320 /* FIXME: Valgrind reports a memory leak here. */
321 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
324 return g_variant_builder_end(&gvb);
327 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
328 const struct sr_channel_group *cg)
332 char command[MAX_COMMAND_SIZE], float_str[30];
333 struct dev_context *devc;
334 const struct scope_config *model;
335 struct scope_state *state;
339 gboolean update_sample_rate;
341 if (!sdi || !(devc = sdi->priv))
344 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
347 model = devc->model_config;
348 state = devc->model_state;
349 update_sample_rate = FALSE;
354 case SR_CONF_LIMIT_FRAMES:
355 devc->frame_limit = g_variant_get_uint64(data);
358 case SR_CONF_TRIGGER_SOURCE:
359 tmp = g_variant_get_string(data, NULL);
360 for (i = 0; (*model->trigger_sources)[i]; i++) {
361 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
363 state->trigger_source = i;
364 g_snprintf(command, sizeof(command),
365 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
366 (*model->trigger_sources)[i]);
368 ret = sr_scpi_send(sdi->conn, command);
373 if (cg_type == CG_NONE) {
374 sr_err("No channel group specified.");
375 return SR_ERR_CHANNEL_GROUP;
378 g_variant_get(data, "(tt)", &p, &q);
380 for (i = 0; i < model->num_vdivs; i++) {
381 if (p != (*model->vdivs)[i][0] ||
382 q != (*model->vdivs)[i][1])
384 for (j = 1; j <= model->analog_channels; ++j) {
385 if (cg != devc->analog_groups[j - 1])
387 state->analog_channels[j - 1].vdiv = i;
388 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
389 g_snprintf(command, sizeof(command),
390 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
393 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
394 sr_scpi_get_opc(sdi->conn) != SR_OK)
404 case SR_CONF_TIMEBASE:
405 g_variant_get(data, "(tt)", &p, &q);
407 for (i = 0; i < model->num_timebases; i++) {
408 if (p != (*model->timebases)[i][0] ||
409 q != (*model->timebases)[i][1])
412 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
413 g_snprintf(command, sizeof(command),
414 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
417 ret = sr_scpi_send(sdi->conn, command);
418 update_sample_rate = TRUE;
422 case SR_CONF_HORIZ_TRIGGERPOS:
423 tmp_d = g_variant_get_double(data);
425 if (tmp_d < 0.0 || tmp_d > 1.0)
428 state->horiz_triggerpos = tmp_d;
429 tmp_d = -(tmp_d - 0.5) *
430 ((double) (*model->timebases)[state->timebase][0] /
431 (*model->timebases)[state->timebase][1])
434 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
435 g_snprintf(command, sizeof(command),
436 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
439 ret = sr_scpi_send(sdi->conn, command);
441 case SR_CONF_TRIGGER_SLOPE:
442 tmp = g_variant_get_string(data, NULL);
444 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
447 state->trigger_slope = (tmp[0] == 'r') ? 0 : 1;
449 g_snprintf(command, sizeof(command),
450 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
451 (state->trigger_slope == 0) ? "POS" : "NEG");
453 ret = sr_scpi_send(sdi->conn, command);
455 case SR_CONF_COUPLING:
456 if (cg_type == CG_NONE) {
457 sr_err("No channel group specified.");
458 return SR_ERR_CHANNEL_GROUP;
461 tmp = g_variant_get_string(data, NULL);
463 for (i = 0; (*model->coupling_options)[i]; i++) {
464 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
466 for (j = 1; j <= model->analog_channels; ++j) {
467 if (cg != devc->analog_groups[j - 1])
469 state->analog_channels[j-1].coupling = i;
471 g_snprintf(command, sizeof(command),
472 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
475 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
476 sr_scpi_get_opc(sdi->conn) != SR_OK)
491 ret = sr_scpi_get_opc(sdi->conn);
493 if (ret == SR_OK && update_sample_rate)
494 ret = hmo_update_sample_rate(sdi);
499 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
500 const struct sr_channel_group *cg)
502 int cg_type = CG_NONE;
503 struct dev_context *devc = NULL;
504 const struct scope_config *model = NULL;
506 if (sdi && (devc = sdi->priv)) {
507 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
510 model = devc->model_config;
514 case SR_CONF_SCAN_OPTIONS:
515 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
516 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
518 case SR_CONF_DEVICE_OPTIONS:
519 if (cg_type == CG_NONE) {
521 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
522 model->devopts, model->num_devopts, sizeof(uint32_t));
524 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
525 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
526 } else if (cg_type == CG_ANALOG) {
527 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
528 model->analog_devopts, model->num_analog_devopts,
531 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
532 NULL, 0, sizeof(uint32_t));
535 case SR_CONF_COUPLING:
536 if (cg_type == CG_NONE)
537 return SR_ERR_CHANNEL_GROUP;
538 *data = g_variant_new_strv(*model->coupling_options,
539 g_strv_length((char **)*model->coupling_options));
541 case SR_CONF_TRIGGER_SOURCE:
544 *data = g_variant_new_strv(*model->trigger_sources,
545 g_strv_length((char **)*model->trigger_sources));
547 case SR_CONF_TRIGGER_SLOPE:
550 *data = g_variant_new_strv(*model->trigger_slopes,
551 g_strv_length((char **)*model->trigger_slopes));
553 case SR_CONF_TIMEBASE:
556 *data = build_tuples(model->timebases, model->num_timebases);
559 if (cg_type == CG_NONE)
560 return SR_ERR_CHANNEL_GROUP;
561 *data = build_tuples(model->vdivs, model->num_vdivs);
570 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
572 char command[MAX_COMMAND_SIZE];
573 struct sr_channel *ch;
574 struct dev_context *devc;
575 const struct scope_config *model;
578 model = devc->model_config;
580 ch = devc->current_channel->data;
583 case SR_CHANNEL_ANALOG:
584 g_snprintf(command, sizeof(command),
585 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
588 case SR_CHANNEL_LOGIC:
589 g_snprintf(command, sizeof(command),
590 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
591 ch->index < 8 ? 1 : 2);
594 sr_err("Invalid channel type.");
598 return sr_scpi_send(sdi->conn, command);
601 static int hmo_check_channels(GSList *channels)
604 struct sr_channel *ch;
605 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
607 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
609 for (l = channels; l; l = l->next) {
612 case SR_CHANNEL_ANALOG:
614 enabled_chan3 = TRUE;
615 else if (ch->index == 3)
616 enabled_chan4 = TRUE;
618 case SR_CHANNEL_LOGIC:
629 if ((enabled_pod1 && enabled_chan3) ||
630 (enabled_pod2 && enabled_chan4))
636 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
640 gboolean *pod_enabled, setup_changed;
641 char command[MAX_COMMAND_SIZE];
642 struct scope_state *state;
643 const struct scope_config *model;
644 struct sr_channel *ch;
645 struct dev_context *devc;
646 struct sr_scpi_dev_inst *scpi;
650 state = devc->model_state;
651 model = devc->model_config;
652 setup_changed = FALSE;
654 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
656 for (l = sdi->channels; l; l = l->next) {
659 case SR_CHANNEL_ANALOG:
660 if (ch->enabled == state->analog_channels[ch->index].state)
662 g_snprintf(command, sizeof(command),
663 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
664 ch->index + 1, ch->enabled);
666 if (sr_scpi_send(scpi, command) != SR_OK)
668 state->analog_channels[ch->index].state = ch->enabled;
669 setup_changed = TRUE;
671 case SR_CHANNEL_LOGIC:
673 * A digital POD needs to be enabled for every group of
677 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
679 if (ch->enabled == state->digital_channels[ch->index])
681 g_snprintf(command, sizeof(command),
682 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
683 ch->index, ch->enabled);
685 if (sr_scpi_send(scpi, command) != SR_OK)
688 state->digital_channels[ch->index] = ch->enabled;
689 setup_changed = TRUE;
696 for (i = 1; i <= model->digital_pods; ++i) {
697 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
699 g_snprintf(command, sizeof(command),
700 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
701 i, pod_enabled[i - 1]);
702 if (sr_scpi_send(scpi, command) != SR_OK)
704 state->digital_pods[i - 1] = pod_enabled[i - 1];
705 setup_changed = TRUE;
710 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
716 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
719 gboolean digital_added;
720 struct sr_channel *ch;
721 struct dev_context *devc;
722 struct sr_scpi_dev_inst *scpi;
724 if (sdi->status != SR_ST_ACTIVE)
725 return SR_ERR_DEV_CLOSED;
729 digital_added = FALSE;
731 g_slist_free(devc->enabled_channels);
732 devc->enabled_channels = NULL;
734 for (l = sdi->channels; l; l = l->next) {
738 /* Only add a single digital channel. */
739 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
740 devc->enabled_channels = g_slist_append(
741 devc->enabled_channels, ch);
742 if (ch->type == SR_CHANNEL_LOGIC)
743 digital_added = TRUE;
747 if (!devc->enabled_channels)
750 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
751 sr_err("Invalid channel configuration specified!");
755 if (hmo_setup_channels(sdi) != SR_OK) {
756 sr_err("Failed to setup channel configuration!");
760 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
761 hmo_receive_data, (void *)sdi);
763 /* Send header packet to the session bus. */
764 std_session_send_df_header(cb_data, LOG_PREFIX);
766 devc->current_channel = devc->enabled_channels;
768 return hmo_request_data(sdi);
771 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
773 struct dev_context *devc;
774 struct sr_scpi_dev_inst *scpi;
775 struct sr_datafeed_packet packet;
779 packet.type = SR_DF_END;
780 packet.payload = NULL;
781 sr_session_send(sdi, &packet);
783 if (sdi->status != SR_ST_ACTIVE)
784 return SR_ERR_DEV_CLOSED;
788 devc->num_frames = 0;
789 g_slist_free(devc->enabled_channels);
790 devc->enabled_channels = NULL;
792 sr_scpi_source_remove(sdi->session, scpi);
797 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
799 .longname = "Hameg HMO",
804 .dev_list = dev_list,
805 .dev_clear = dev_clear,
806 .config_get = config_get,
807 .config_set = config_set,
808 .config_list = config_list,
809 .dev_open = dev_open,
810 .dev_close = dev_close,
811 .dev_acquisition_start = dev_acquisition_start,
812 .dev_acquisition_stop = dev_acquisition_stop,