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/>.
25 #define SERIALCOMM "115200/8n1/flow=1"
27 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info;
29 static const char *manufacturers[] = {
33 static const uint32_t drvopts[] = {
37 static const uint32_t scanopts[] = {
49 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
51 return std_init(sr_ctx, di, LOG_PREFIX);
54 static int check_manufacturer(const char *manufacturer)
58 for (i = 0; i < ARRAY_SIZE(manufacturers); i++)
59 if (!strcmp(manufacturer, manufacturers[i]))
65 static struct sr_dev_inst *hmo_probe_serial_device(struct sr_scpi_dev_inst *scpi)
67 struct sr_dev_inst *sdi;
68 struct dev_context *devc;
69 struct sr_scpi_hw_info *hw_info;
75 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
76 sr_info("Couldn't get IDN response.");
80 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
83 sdi = g_malloc0(sizeof(struct sr_dev_inst));
84 sdi->vendor = g_strdup(hw_info->manufacturer);
85 sdi->model = g_strdup(hw_info->model);
86 sdi->version = g_strdup(hw_info->firmware_version);
87 sdi->serial_num = g_strdup(hw_info->serial_number);
88 sdi->driver = &hameg_hmo_driver_info;
89 sdi->inst_type = SR_INST_SCPI;
92 sr_scpi_hw_info_free(hw_info);
95 devc = g_malloc0(sizeof(struct dev_context));
99 if (hmo_init_device(sdi) != SR_OK)
106 sr_scpi_hw_info_free(hw_info);
108 sr_dev_inst_free(sdi);
114 static GSList *scan(struct sr_dev_driver *di, GSList *options)
116 return sr_scpi_scan(di->context, options, hmo_probe_serial_device);
119 static GSList *dev_list(const struct sr_dev_driver *di)
121 return ((struct drv_context *)(di->context))->instances;
124 static void clear_helper(void *priv)
126 struct dev_context *devc;
130 hmo_scope_state_free(devc->model_state);
132 g_free(devc->analog_groups);
133 g_free(devc->digital_groups);
138 static int dev_clear(const struct sr_dev_driver *di)
140 return std_dev_clear(di, clear_helper);
143 static int dev_open(struct sr_dev_inst *sdi)
145 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
148 if (hmo_scope_state_get(sdi) != SR_OK)
151 sdi->status = SR_ST_ACTIVE;
156 static int dev_close(struct sr_dev_inst *sdi)
158 if (sdi->status == SR_ST_INACTIVE)
161 sr_scpi_close(sdi->conn);
163 sdi->status = SR_ST_INACTIVE;
168 static int cleanup(const struct sr_dev_driver *di)
175 static int check_channel_group(struct dev_context *devc,
176 const struct sr_channel_group *cg)
179 const struct scope_config *model;
181 model = devc->model_config;
186 for (i = 0; i < model->analog_channels; i++)
187 if (cg == devc->analog_groups[i])
190 for (i = 0; i < model->digital_pods; i++)
191 if (cg == devc->digital_groups[i])
194 sr_err("Invalid channel group specified.");
199 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
200 const struct sr_channel_group *cg)
204 struct dev_context *devc;
205 const struct scope_config *model;
206 struct scope_state *state;
208 if (!sdi || !(devc = sdi->priv))
211 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
215 model = devc->model_config;
216 state = devc->model_state;
219 case SR_CONF_NUM_HDIV:
220 *data = g_variant_new_int32(model->num_xdivs);
223 case SR_CONF_TIMEBASE:
224 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
225 (*model->timebases)[state->timebase][1]);
228 case SR_CONF_NUM_VDIV:
229 if (cg_type == CG_NONE) {
230 sr_err("No channel group specified.");
231 return SR_ERR_CHANNEL_GROUP;
232 } else if (cg_type == CG_ANALOG) {
233 for (i = 0; i < model->analog_channels; i++) {
234 if (cg != devc->analog_groups[i])
236 *data = g_variant_new_int32(model->num_ydivs);
246 if (cg_type == CG_NONE) {
247 sr_err("No channel group specified.");
248 return SR_ERR_CHANNEL_GROUP;
249 } else if (cg_type == CG_ANALOG) {
250 for (i = 0; i < model->analog_channels; i++) {
251 if (cg != devc->analog_groups[i])
253 *data = g_variant_new("(tt)",
254 (*model->vdivs)[state->analog_channels[i].vdiv][0],
255 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
264 case SR_CONF_TRIGGER_SOURCE:
265 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
268 case SR_CONF_TRIGGER_SLOPE:
269 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
272 case SR_CONF_HORIZ_TRIGGERPOS:
273 *data = g_variant_new_double(state->horiz_triggerpos);
276 case SR_CONF_COUPLING:
277 if (cg_type == CG_NONE) {
278 sr_err("No channel group specified.");
279 return SR_ERR_CHANNEL_GROUP;
280 } else if (cg_type == CG_ANALOG) {
281 for (i = 0; i < model->analog_channels; i++) {
282 if (cg != devc->analog_groups[i])
284 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
293 case SR_CONF_SAMPLERATE:
294 *data = g_variant_new_uint64(state->sample_rate);
304 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
307 GVariant *rational[2];
310 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
312 for (i = 0; i < n; i++) {
313 rational[0] = g_variant_new_uint64((*array)[i][0]);
314 rational[1] = g_variant_new_uint64((*array)[i][1]);
316 /* FIXME: Valgrind reports a memory leak here. */
317 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
320 return g_variant_builder_end(&gvb);
323 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
324 const struct sr_channel_group *cg)
328 char command[MAX_COMMAND_SIZE], float_str[30];
329 struct dev_context *devc;
330 const struct scope_config *model;
331 struct scope_state *state;
335 gboolean update_sample_rate;
337 if (!sdi || !(devc = sdi->priv))
340 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
343 model = devc->model_config;
344 state = devc->model_state;
345 update_sample_rate = FALSE;
350 case SR_CONF_LIMIT_FRAMES:
351 devc->frame_limit = g_variant_get_uint64(data);
354 case SR_CONF_TRIGGER_SOURCE:
355 tmp = g_variant_get_string(data, NULL);
356 for (i = 0; (*model->trigger_sources)[i]; i++) {
357 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
359 state->trigger_source = i;
360 g_snprintf(command, sizeof(command),
361 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
362 (*model->trigger_sources)[i]);
364 ret = sr_scpi_send(sdi->conn, command);
369 if (cg_type == CG_NONE) {
370 sr_err("No channel group specified.");
371 return SR_ERR_CHANNEL_GROUP;
374 g_variant_get(data, "(tt)", &p, &q);
376 for (i = 0; i < model->num_vdivs; i++) {
377 if (p != (*model->vdivs)[i][0] ||
378 q != (*model->vdivs)[i][1])
380 for (j = 1; j <= model->analog_channels; j++) {
381 if (cg != devc->analog_groups[j - 1])
383 state->analog_channels[j - 1].vdiv = i;
384 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
385 g_snprintf(command, sizeof(command),
386 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
389 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
390 sr_scpi_get_opc(sdi->conn) != SR_OK)
400 case SR_CONF_TIMEBASE:
401 g_variant_get(data, "(tt)", &p, &q);
403 for (i = 0; i < model->num_timebases; i++) {
404 if (p != (*model->timebases)[i][0] ||
405 q != (*model->timebases)[i][1])
408 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
409 g_snprintf(command, sizeof(command),
410 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
413 ret = sr_scpi_send(sdi->conn, command);
414 update_sample_rate = TRUE;
418 case SR_CONF_HORIZ_TRIGGERPOS:
419 tmp_d = g_variant_get_double(data);
421 if (tmp_d < 0.0 || tmp_d > 1.0)
424 state->horiz_triggerpos = tmp_d;
425 tmp_d = -(tmp_d - 0.5) *
426 ((double) (*model->timebases)[state->timebase][0] /
427 (*model->timebases)[state->timebase][1])
430 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
431 g_snprintf(command, sizeof(command),
432 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
435 ret = sr_scpi_send(sdi->conn, command);
437 case SR_CONF_TRIGGER_SLOPE:
438 tmp = g_variant_get_string(data, NULL);
440 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
443 state->trigger_slope = (tmp[0] == 'r') ? 0 : 1;
445 g_snprintf(command, sizeof(command),
446 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
447 (state->trigger_slope == 0) ? "POS" : "NEG");
449 ret = sr_scpi_send(sdi->conn, command);
451 case SR_CONF_COUPLING:
452 if (cg_type == CG_NONE) {
453 sr_err("No channel group specified.");
454 return SR_ERR_CHANNEL_GROUP;
457 tmp = g_variant_get_string(data, NULL);
459 for (i = 0; (*model->coupling_options)[i]; i++) {
460 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
462 for (j = 1; j <= model->analog_channels; j++) {
463 if (cg != devc->analog_groups[j - 1])
465 state->analog_channels[j-1].coupling = i;
467 g_snprintf(command, sizeof(command),
468 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
471 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
472 sr_scpi_get_opc(sdi->conn) != SR_OK)
487 ret = sr_scpi_get_opc(sdi->conn);
489 if (ret == SR_OK && update_sample_rate)
490 ret = hmo_update_sample_rate(sdi);
495 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
496 const struct sr_channel_group *cg)
498 int cg_type = CG_NONE;
499 struct dev_context *devc = NULL;
500 const struct scope_config *model = NULL;
502 if (sdi && (devc = sdi->priv)) {
503 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
506 model = devc->model_config;
510 case SR_CONF_SCAN_OPTIONS:
511 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
512 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
514 case SR_CONF_DEVICE_OPTIONS:
515 if (cg_type == CG_NONE) {
517 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
518 model->devopts, model->num_devopts, sizeof(uint32_t));
520 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
521 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
522 } else if (cg_type == CG_ANALOG) {
523 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
524 model->analog_devopts, model->num_analog_devopts,
527 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
528 NULL, 0, sizeof(uint32_t));
531 case SR_CONF_COUPLING:
532 if (cg_type == CG_NONE)
533 return SR_ERR_CHANNEL_GROUP;
534 *data = g_variant_new_strv(*model->coupling_options,
535 g_strv_length((char **)*model->coupling_options));
537 case SR_CONF_TRIGGER_SOURCE:
540 *data = g_variant_new_strv(*model->trigger_sources,
541 g_strv_length((char **)*model->trigger_sources));
543 case SR_CONF_TRIGGER_SLOPE:
546 *data = g_variant_new_strv(*model->trigger_slopes,
547 g_strv_length((char **)*model->trigger_slopes));
549 case SR_CONF_TIMEBASE:
552 *data = build_tuples(model->timebases, model->num_timebases);
555 if (cg_type == CG_NONE)
556 return SR_ERR_CHANNEL_GROUP;
557 *data = build_tuples(model->vdivs, model->num_vdivs);
566 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
568 char command[MAX_COMMAND_SIZE];
569 struct sr_channel *ch;
570 struct dev_context *devc;
571 const struct scope_config *model;
574 model = devc->model_config;
576 ch = devc->current_channel->data;
579 case SR_CHANNEL_ANALOG:
580 g_snprintf(command, sizeof(command),
581 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
584 case SR_CHANNEL_LOGIC:
585 g_snprintf(command, sizeof(command),
586 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
587 ch->index < 8 ? 1 : 2);
590 sr_err("Invalid channel type.");
594 return sr_scpi_send(sdi->conn, command);
597 static int hmo_check_channels(GSList *channels)
600 struct sr_channel *ch;
601 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
603 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
605 for (l = channels; l; l = l->next) {
608 case SR_CHANNEL_ANALOG:
610 enabled_chan3 = TRUE;
611 else if (ch->index == 3)
612 enabled_chan4 = TRUE;
614 case SR_CHANNEL_LOGIC:
625 if ((enabled_pod1 && enabled_chan3) ||
626 (enabled_pod2 && enabled_chan4))
632 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
636 gboolean *pod_enabled, setup_changed;
637 char command[MAX_COMMAND_SIZE];
638 struct scope_state *state;
639 const struct scope_config *model;
640 struct sr_channel *ch;
641 struct dev_context *devc;
642 struct sr_scpi_dev_inst *scpi;
646 state = devc->model_state;
647 model = devc->model_config;
648 setup_changed = FALSE;
650 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
652 for (l = sdi->channels; l; l = l->next) {
655 case SR_CHANNEL_ANALOG:
656 if (ch->enabled == state->analog_channels[ch->index].state)
658 g_snprintf(command, sizeof(command),
659 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
660 ch->index + 1, ch->enabled);
662 if (sr_scpi_send(scpi, command) != SR_OK)
664 state->analog_channels[ch->index].state = ch->enabled;
665 setup_changed = TRUE;
667 case SR_CHANNEL_LOGIC:
669 * A digital POD needs to be enabled for every group of
673 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
675 if (ch->enabled == state->digital_channels[ch->index])
677 g_snprintf(command, sizeof(command),
678 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
679 ch->index, ch->enabled);
681 if (sr_scpi_send(scpi, command) != SR_OK)
684 state->digital_channels[ch->index] = ch->enabled;
685 setup_changed = TRUE;
692 for (i = 1; i <= model->digital_pods; i++) {
693 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
695 g_snprintf(command, sizeof(command),
696 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
697 i, pod_enabled[i - 1]);
698 if (sr_scpi_send(scpi, command) != SR_OK)
700 state->digital_pods[i - 1] = pod_enabled[i - 1];
701 setup_changed = TRUE;
706 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
712 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
715 gboolean digital_added;
716 struct sr_channel *ch;
717 struct dev_context *devc;
718 struct sr_scpi_dev_inst *scpi;
720 if (sdi->status != SR_ST_ACTIVE)
721 return SR_ERR_DEV_CLOSED;
725 digital_added = FALSE;
727 g_slist_free(devc->enabled_channels);
728 devc->enabled_channels = NULL;
730 for (l = sdi->channels; l; l = l->next) {
734 /* Only add a single digital channel. */
735 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
736 devc->enabled_channels = g_slist_append(
737 devc->enabled_channels, ch);
738 if (ch->type == SR_CHANNEL_LOGIC)
739 digital_added = TRUE;
743 if (!devc->enabled_channels)
746 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
747 sr_err("Invalid channel configuration specified!");
751 if (hmo_setup_channels(sdi) != SR_OK) {
752 sr_err("Failed to setup channel configuration!");
756 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
757 hmo_receive_data, (void *)sdi);
759 /* Send header packet to the session bus. */
760 std_session_send_df_header(cb_data, LOG_PREFIX);
762 devc->current_channel = devc->enabled_channels;
764 return hmo_request_data(sdi);
767 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
769 struct dev_context *devc;
770 struct sr_scpi_dev_inst *scpi;
771 struct sr_datafeed_packet packet;
775 packet.type = SR_DF_END;
776 packet.payload = NULL;
777 sr_session_send(sdi, &packet);
779 if (sdi->status != SR_ST_ACTIVE)
780 return SR_ERR_DEV_CLOSED;
784 devc->num_frames = 0;
785 g_slist_free(devc->enabled_channels);
786 devc->enabled_channels = NULL;
788 sr_scpi_source_remove(sdi->session, scpi);
793 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
795 .longname = "Hameg HMO",
800 .dev_list = dev_list,
801 .dev_clear = dev_clear,
802 .config_get = config_get,
803 .config_set = config_set,
804 .config_list = config_list,
805 .dev_open = dev_open,
806 .dev_close = dev_close,
807 .dev_acquisition_start = dev_acquisition_start,
808 .dev_acquisition_stop = dev_acquisition_stop,