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 uint32_t drvopts[] = {
36 static const uint32_t scanopts[] = {
48 static int init(struct sr_context *sr_ctx)
50 return std_init(sr_ctx, di, LOG_PREFIX);
53 static int check_manufacturer(const char *manufacturer)
57 for (i = 0; i < ARRAY_SIZE(manufacturers); ++i)
58 if (!strcmp(manufacturer, manufacturers[i]))
64 static struct sr_dev_inst *hmo_probe_serial_device(struct sr_scpi_dev_inst *scpi)
66 struct sr_dev_inst *sdi;
67 struct dev_context *devc;
68 struct sr_scpi_hw_info *hw_info;
74 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
75 sr_info("Couldn't get IDN response.");
79 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
82 sdi = g_malloc0(sizeof(struct sr_dev_inst));
83 sdi->status = SR_ST_ACTIVE;
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);
89 sdi->inst_type = SR_INST_SCPI;
92 sr_scpi_hw_info_free(hw_info);
95 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
100 if (hmo_init_device(sdi) != SR_OK)
103 sr_scpi_close(sdi->conn);
105 sdi->status = SR_ST_INACTIVE;
111 sr_scpi_hw_info_free(hw_info);
113 sr_dev_inst_free(sdi);
120 static GSList *scan(GSList *options)
122 return sr_scpi_scan(di->priv, options, hmo_probe_serial_device);
125 static GSList *dev_list(void)
127 return ((struct drv_context *)(di->priv))->instances;
130 static void clear_helper(void *priv)
132 struct dev_context *devc;
136 hmo_scope_state_free(devc->model_state);
138 g_free(devc->analog_groups);
139 g_free(devc->digital_groups);
144 static int dev_clear(void)
146 return std_dev_clear(di, clear_helper);
149 static int dev_open(struct sr_dev_inst *sdi)
151 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
154 if (hmo_scope_state_get(sdi) != SR_OK)
157 sdi->status = SR_ST_ACTIVE;
162 static int dev_close(struct sr_dev_inst *sdi)
164 if (sdi->status == SR_ST_INACTIVE)
167 sr_scpi_close(sdi->conn);
169 sdi->status = SR_ST_INACTIVE;
174 static int cleanup(void)
181 static int check_channel_group(struct dev_context *devc,
182 const struct sr_channel_group *cg)
185 struct scope_config *model;
187 model = devc->model_config;
192 for (i = 0; i < model->analog_channels; ++i)
193 if (cg == devc->analog_groups[i])
196 for (i = 0; i < model->digital_pods; ++i)
197 if (cg == devc->digital_groups[i])
200 sr_err("Invalid channel group specified.");
205 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
206 const struct sr_channel_group *cg)
210 struct dev_context *devc;
211 struct scope_config *model;
212 struct scope_state *state;
214 if (!sdi || !(devc = sdi->priv))
217 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
221 model = devc->model_config;
222 state = devc->model_state;
225 case SR_CONF_NUM_TIMEBASE:
226 *data = g_variant_new_int32(model->num_xdivs);
229 case SR_CONF_TIMEBASE:
230 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
231 (*model->timebases)[state->timebase][1]);
234 case SR_CONF_NUM_VDIV:
235 if (cg_type == CG_NONE) {
236 sr_err("No channel group specified.");
237 return SR_ERR_CHANNEL_GROUP;
238 } else if (cg_type == CG_ANALOG) {
239 for (i = 0; i < model->analog_channels; ++i) {
240 if (cg != devc->analog_groups[i])
242 *data = g_variant_new_int32(model->num_ydivs);
252 if (cg_type == CG_NONE) {
253 sr_err("No channel group specified.");
254 return SR_ERR_CHANNEL_GROUP;
255 } else if (cg_type == CG_ANALOG) {
256 for (i = 0; i < model->analog_channels; ++i) {
257 if (cg != devc->analog_groups[i])
259 *data = g_variant_new("(tt)",
260 (*model->vdivs)[state->analog_channels[i].vdiv][0],
261 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
270 case SR_CONF_TRIGGER_SOURCE:
271 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
274 case SR_CONF_TRIGGER_SLOPE:
275 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
278 case SR_CONF_HORIZ_TRIGGERPOS:
279 *data = g_variant_new_double(state->horiz_triggerpos);
282 case SR_CONF_COUPLING:
283 if (cg_type == CG_NONE) {
284 sr_err("No channel group specified.");
285 return SR_ERR_CHANNEL_GROUP;
286 } else if (cg_type == CG_ANALOG) {
287 for (i = 0; i < model->analog_channels; ++i) {
288 if (cg != devc->analog_groups[i])
290 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
299 case SR_CONF_SAMPLERATE:
300 *data = g_variant_new_uint64(state->sample_rate);
310 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
313 GVariant *rational[2];
316 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
318 for (i = 0; i < n; i++) {
319 rational[0] = g_variant_new_uint64((*array)[i][0]);
320 rational[1] = g_variant_new_uint64((*array)[i][1]);
322 /* FIXME: Valgrind reports a memory leak here. */
323 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
326 return g_variant_builder_end(&gvb);
329 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
330 const struct sr_channel_group *cg)
334 char command[MAX_COMMAND_SIZE], float_str[30];
335 struct dev_context *devc;
336 struct scope_config *model;
337 struct scope_state *state;
341 gboolean update_sample_rate;
343 if (!sdi || !(devc = sdi->priv))
346 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
349 model = devc->model_config;
350 state = devc->model_state;
351 update_sample_rate = FALSE;
356 case SR_CONF_LIMIT_FRAMES:
357 devc->frame_limit = g_variant_get_uint64(data);
360 case SR_CONF_TRIGGER_SOURCE:
361 tmp = g_variant_get_string(data, NULL);
362 for (i = 0; (*model->trigger_sources)[i]; i++) {
363 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
365 state->trigger_source = i;
366 g_snprintf(command, sizeof(command),
367 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
368 (*model->trigger_sources)[i]);
370 ret = sr_scpi_send(sdi->conn, command);
375 if (cg_type == CG_NONE) {
376 sr_err("No channel group specified.");
377 return SR_ERR_CHANNEL_GROUP;
380 g_variant_get(data, "(tt)", &p, &q);
382 for (i = 0; i < model->num_vdivs; i++) {
383 if (p != (*model->vdivs)[i][0] ||
384 q != (*model->vdivs)[i][1])
386 for (j = 1; j <= model->analog_channels; ++j) {
387 if (cg != devc->analog_groups[j - 1])
389 state->analog_channels[j - 1].vdiv = i;
390 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
391 g_snprintf(command, sizeof(command),
392 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
395 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
396 sr_scpi_get_opc(sdi->conn) != SR_OK)
406 case SR_CONF_TIMEBASE:
407 g_variant_get(data, "(tt)", &p, &q);
409 for (i = 0; i < model->num_timebases; i++) {
410 if (p != (*model->timebases)[i][0] ||
411 q != (*model->timebases)[i][1])
414 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
415 g_snprintf(command, sizeof(command),
416 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
419 ret = sr_scpi_send(sdi->conn, command);
420 update_sample_rate = TRUE;
424 case SR_CONF_HORIZ_TRIGGERPOS:
425 tmp_d = g_variant_get_double(data);
427 if (tmp_d < 0.0 || tmp_d > 1.0)
430 state->horiz_triggerpos = tmp_d;
431 tmp_d = -(tmp_d - 0.5) *
432 ((double) (*model->timebases)[state->timebase][0] /
433 (*model->timebases)[state->timebase][1])
436 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
437 g_snprintf(command, sizeof(command),
438 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
441 ret = sr_scpi_send(sdi->conn, command);
443 case SR_CONF_TRIGGER_SLOPE:
444 tmp = g_variant_get_string(data, NULL);
446 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
449 state->trigger_slope = (tmp[0] == 'r') ? 0 : 1;
451 g_snprintf(command, sizeof(command),
452 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
453 (state->trigger_slope == 0) ? "POS" : "NEG");
455 ret = sr_scpi_send(sdi->conn, command);
457 case SR_CONF_COUPLING:
458 if (cg_type == CG_NONE) {
459 sr_err("No channel group specified.");
460 return SR_ERR_CHANNEL_GROUP;
463 tmp = g_variant_get_string(data, NULL);
465 for (i = 0; (*model->coupling_options)[i]; i++) {
466 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
468 for (j = 1; j <= model->analog_channels; ++j) {
469 if (cg != devc->analog_groups[j - 1])
471 state->analog_channels[j-1].coupling = i;
473 g_snprintf(command, sizeof(command),
474 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
477 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
478 sr_scpi_get_opc(sdi->conn) != SR_OK)
493 ret = sr_scpi_get_opc(sdi->conn);
495 if (ret == SR_OK && update_sample_rate)
496 ret = hmo_update_sample_rate(sdi);
501 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
502 const struct sr_channel_group *cg)
504 int cg_type = CG_NONE;
505 struct dev_context *devc = NULL;
506 struct scope_config *model = NULL;
508 if (sdi && (devc = sdi->priv)) {
509 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
512 model = devc->model_config;
516 case SR_CONF_SCAN_OPTIONS:
517 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
518 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
520 case SR_CONF_DEVICE_OPTIONS:
521 if (cg_type == CG_NONE) {
523 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
524 model->devopts, model->num_devopts, sizeof(uint32_t));
526 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
527 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
528 } else if (cg_type == CG_ANALOG) {
529 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
530 model->analog_devopts, model->num_analog_devopts,
533 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
534 NULL, 0, sizeof(uint32_t));
537 case SR_CONF_COUPLING:
538 if (cg_type == CG_NONE)
539 return SR_ERR_CHANNEL_GROUP;
540 *data = g_variant_new_strv(*model->coupling_options,
541 g_strv_length((char **)*model->coupling_options));
543 case SR_CONF_TRIGGER_SOURCE:
546 *data = g_variant_new_strv(*model->trigger_sources,
547 g_strv_length((char **)*model->trigger_sources));
549 case SR_CONF_TRIGGER_SLOPE:
552 *data = g_variant_new_strv(*model->trigger_slopes,
553 g_strv_length((char **)*model->trigger_slopes));
555 case SR_CONF_TIMEBASE:
558 *data = build_tuples(model->timebases, model->num_timebases);
561 if (cg_type == CG_NONE)
562 return SR_ERR_CHANNEL_GROUP;
563 *data = build_tuples(model->vdivs, model->num_vdivs);
572 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
574 char command[MAX_COMMAND_SIZE];
575 struct sr_channel *ch;
576 struct dev_context *devc;
577 struct scope_config *model;
580 model = devc->model_config;
582 ch = devc->current_channel->data;
585 case SR_CHANNEL_ANALOG:
586 g_snprintf(command, sizeof(command),
587 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
590 case SR_CHANNEL_LOGIC:
591 g_snprintf(command, sizeof(command),
592 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
593 ch->index < 8 ? 1 : 2);
596 sr_err("Invalid channel type.");
600 return sr_scpi_send(sdi->conn, command);
603 static int hmo_check_channels(GSList *channels)
606 struct sr_channel *ch;
607 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
609 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
611 for (l = channels; l; l = l->next) {
614 case SR_CHANNEL_ANALOG:
616 enabled_chan3 = TRUE;
617 else if (ch->index == 3)
618 enabled_chan4 = TRUE;
620 case SR_CHANNEL_LOGIC:
631 if ((enabled_pod1 && enabled_chan3) ||
632 (enabled_pod2 && enabled_chan4))
638 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
642 gboolean *pod_enabled, setup_changed;
643 char command[MAX_COMMAND_SIZE];
644 struct scope_state *state;
645 struct scope_config *model;
646 struct sr_channel *ch;
647 struct dev_context *devc;
648 struct sr_scpi_dev_inst *scpi;
652 state = devc->model_state;
653 model = devc->model_config;
654 setup_changed = FALSE;
656 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
658 for (l = sdi->channels; l; l = l->next) {
661 case SR_CHANNEL_ANALOG:
662 if (ch->enabled == state->analog_channels[ch->index].state)
664 g_snprintf(command, sizeof(command),
665 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
666 ch->index + 1, ch->enabled);
668 if (sr_scpi_send(scpi, command) != SR_OK)
670 state->analog_channels[ch->index].state = ch->enabled;
671 setup_changed = TRUE;
673 case SR_CHANNEL_LOGIC:
675 * A digital POD needs to be enabled for every group of
679 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
681 if (ch->enabled == state->digital_channels[ch->index])
683 g_snprintf(command, sizeof(command),
684 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
685 ch->index, ch->enabled);
687 if (sr_scpi_send(scpi, command) != SR_OK)
690 state->digital_channels[ch->index] = ch->enabled;
691 setup_changed = TRUE;
698 for (i = 1; i <= model->digital_pods; ++i) {
699 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
701 g_snprintf(command, sizeof(command),
702 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
703 i, pod_enabled[i - 1]);
704 if (sr_scpi_send(scpi, command) != SR_OK)
706 state->digital_pods[i - 1] = pod_enabled[i - 1];
707 setup_changed = TRUE;
712 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
718 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
721 gboolean digital_added;
722 struct sr_channel *ch;
723 struct dev_context *devc;
724 struct sr_scpi_dev_inst *scpi;
726 if (sdi->status != SR_ST_ACTIVE)
727 return SR_ERR_DEV_CLOSED;
731 digital_added = FALSE;
733 g_slist_free(devc->enabled_channels);
734 devc->enabled_channels = NULL;
736 for (l = sdi->channels; l; l = l->next) {
740 /* Only add a single digital channel. */
741 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
742 devc->enabled_channels = g_slist_append(
743 devc->enabled_channels, ch);
744 if (ch->type == SR_CHANNEL_LOGIC)
745 digital_added = TRUE;
749 if (!devc->enabled_channels)
752 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
753 sr_err("Invalid channel configuration specified!");
757 if (hmo_setup_channels(sdi) != SR_OK) {
758 sr_err("Failed to setup channel configuration!");
762 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
763 hmo_receive_data, (void *)sdi);
765 /* Send header packet to the session bus. */
766 std_session_send_df_header(cb_data, LOG_PREFIX);
768 devc->current_channel = devc->enabled_channels;
770 return hmo_request_data(sdi);
773 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
775 struct dev_context *devc;
776 struct sr_scpi_dev_inst *scpi;
777 struct sr_datafeed_packet packet;
781 packet.type = SR_DF_END;
782 packet.payload = NULL;
783 sr_session_send(sdi, &packet);
785 if (sdi->status != SR_ST_ACTIVE)
786 return SR_ERR_DEV_CLOSED;
790 devc->num_frames = 0;
791 g_slist_free(devc->enabled_channels);
792 devc->enabled_channels = NULL;
794 sr_scpi_source_remove(sdi->session, scpi);
799 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
801 .longname = "Hameg HMO",
806 .dev_list = dev_list,
807 .dev_clear = dev_clear,
808 .config_get = config_get,
809 .config_set = config_set,
810 .config_list = config_list,
811 .dev_open = dev_open,
812 .dev_close = dev_close,
813 .dev_acquisition_start = dev_acquisition_start,
814 .dev_acquisition_stop = dev_acquisition_stop,