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].channels);
138 for (i = 0; i < model->digital_pods; ++i) {
139 g_slist_free(devc->digital_groups[i].channels);
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_channel_group(struct dev_context *devc,
187 const struct sr_channel_group *cg)
190 struct scope_config *model;
192 model = devc->model_config;
197 for (i = 0; i < model->analog_channels; ++i)
198 if (cg == &devc->analog_groups[i])
201 for (i = 0; i < model->digital_pods; ++i)
202 if (cg == &devc->digital_groups[i])
205 sr_err("Invalid channel group specified.");
210 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
211 const struct sr_channel_group *cg)
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_channel_group(devc, cg)) == 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_TIMEBASE:
235 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
236 (*model->timebases)[state->timebase][1]);
239 case SR_CONF_NUM_VDIV:
240 if (pg_type == PG_NONE) {
241 sr_err("No channel group specified.");
242 return SR_ERR_CHANNEL_GROUP;
243 } else if (pg_type == PG_ANALOG) {
244 for (i = 0; i < model->analog_channels; ++i) {
245 if (cg != &devc->analog_groups[i])
247 *data = g_variant_new_int32(model->num_ydivs);
257 if (pg_type == PG_NONE) {
258 sr_err("No probe group specified.");
259 return SR_ERR_PROBE_GROUP;
260 } else if (pg_type == PG_ANALOG) {
261 for (i = 0; i < model->analog_channels; ++i) {
262 if (probe_group != &devc->analog_groups[i])
264 *data = g_variant_new("(tt)",
265 (*model->vdivs)[state->analog_channels[i].vdiv][0],
266 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
275 case SR_CONF_TRIGGER_SOURCE:
276 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
279 case SR_CONF_TRIGGER_SLOPE:
280 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
283 case SR_CONF_HORIZ_TRIGGERPOS:
284 *data = g_variant_new_double(state->horiz_triggerpos);
287 case SR_CONF_COUPLING:
288 if (pg_type == PG_NONE) {
289 sr_err("No channel group specified.");
290 return SR_ERR_CHANNEL_GROUP;
291 } else if (pg_type == PG_ANALOG) {
292 for (i = 0; i < model->analog_channels; ++i) {
293 if (cg != &devc->analog_groups[i])
295 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
304 case SR_CONF_SAMPLERATE:
305 *data = g_variant_new_uint64(state->sample_rate);
315 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
318 GVariant *rational[2];
321 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
323 for (i = 0; i < n; i++) {
324 rational[0] = g_variant_new_uint64((*array)[i][0]);
325 rational[1] = g_variant_new_uint64((*array)[i][1]);
327 /* FIXME: Valgrind reports a memory leak here. */
328 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
331 return g_variant_builder_end(&gvb);
334 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
335 const struct sr_channel_group *cg)
339 char command[MAX_COMMAND_SIZE], float_str[30];
340 struct dev_context *devc;
341 struct scope_config *model;
342 struct scope_state *state;
346 gboolean update_sample_rate;
348 if (!sdi || !(devc = sdi->priv))
351 if ((pg_type = check_channel_group(devc, cg)) == PG_INVALID)
354 model = devc->model_config;
355 state = devc->model_state;
356 update_sample_rate = FALSE;
361 case SR_CONF_LIMIT_FRAMES:
362 devc->frame_limit = g_variant_get_uint64(data);
365 case SR_CONF_TRIGGER_SOURCE:
366 tmp = g_variant_get_string(data, NULL);
367 for (i = 0; (*model->trigger_sources)[i]; i++) {
368 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
370 state->trigger_source = i;
371 g_snprintf(command, sizeof(command),
372 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
373 (*model->trigger_sources)[i]);
375 ret = sr_scpi_send(sdi->conn, command);
380 if (pg_type == PG_NONE) {
381 sr_err("No channel group specified.");
382 return SR_ERR_CHANNEL_GROUP;
385 g_variant_get(data, "(tt)", &p, &q);
387 for (i = 0; i < model->num_vdivs; i++) {
388 if (p != (*model->vdivs)[i][0] ||
389 q != (*model->vdivs)[i][1])
391 for (j = 1; j <= model->analog_channels; ++j) {
392 if (cg != &devc->analog_groups[j - 1])
394 state->analog_channels[j - 1].vdiv = i;
395 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
396 g_snprintf(command, sizeof(command),
397 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
400 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
401 sr_scpi_get_opc(sdi->conn) != SR_OK)
411 case SR_CONF_TIMEBASE:
412 g_variant_get(data, "(tt)", &p, &q);
414 for (i = 0; i < model->num_timebases; i++) {
415 if (p != (*model->timebases)[i][0] ||
416 q != (*model->timebases)[i][1])
419 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
420 g_snprintf(command, sizeof(command),
421 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
424 ret = sr_scpi_send(sdi->conn, command);
425 update_sample_rate = TRUE;
429 case SR_CONF_HORIZ_TRIGGERPOS:
430 tmp_d = g_variant_get_double(data);
432 if (tmp_d < 0.0 || tmp_d > 1.0)
435 state->horiz_triggerpos = tmp_d;
436 tmp_d = -(tmp_d - 0.5) *
437 ((double) (*model->timebases)[state->timebase][0] /
438 (*model->timebases)[state->timebase][1])
441 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
442 g_snprintf(command, sizeof(command),
443 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
446 ret = sr_scpi_send(sdi->conn, command);
448 case SR_CONF_TRIGGER_SLOPE:
449 tmp = g_variant_get_string(data, NULL);
451 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
454 state->trigger_slope = (tmp[0] == 'r') ? 0 : 1;
456 g_snprintf(command, sizeof(command),
457 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
458 (state->trigger_slope == 0) ? "POS" : "NEG");
460 ret = sr_scpi_send(sdi->conn, command);
462 case SR_CONF_COUPLING:
463 if (pg_type == PG_NONE) {
464 sr_err("No channel group specified.");
465 return SR_ERR_CHANNEL_GROUP;
468 tmp = g_variant_get_string(data, NULL);
470 for (i = 0; (*model->coupling_options)[i]; i++) {
471 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
473 for (j = 1; j <= model->analog_channels; ++j) {
474 if (cg != &devc->analog_groups[j - 1])
476 state->analog_channels[j-1].coupling = i;
478 g_snprintf(command, sizeof(command),
479 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
482 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
483 sr_scpi_get_opc(sdi->conn) != SR_OK)
498 ret = sr_scpi_get_opc(sdi->conn);
500 if (ret == SR_OK && update_sample_rate)
501 ret = hmo_update_sample_rate(sdi);
506 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
507 const struct sr_channel_group *cg)
510 struct dev_context *devc;
511 struct scope_config *model;
513 if (!sdi || !(devc = sdi->priv))
516 if ((pg_type = check_channel_group(devc, cg)) == PG_INVALID)
519 model = devc->model_config;
522 case SR_CONF_DEVICE_OPTIONS:
523 if (pg_type == PG_NONE) {
524 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
525 model->hw_caps, model->num_hwcaps,
527 } else if (pg_type == PG_ANALOG) {
528 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
529 model->analog_hwcaps, model->num_analog_hwcaps,
532 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
533 NULL, 0, sizeof(int32_t));
536 case SR_CONF_COUPLING:
537 if (pg_type == PG_NONE)
538 return SR_ERR_CHANNEL_GROUP;
539 *data = g_variant_new_strv(*model->coupling_options,
540 g_strv_length((char **)*model->coupling_options));
542 case SR_CONF_TRIGGER_SOURCE:
543 *data = g_variant_new_strv(*model->trigger_sources,
544 g_strv_length((char **)*model->trigger_sources));
546 case SR_CONF_TRIGGER_SLOPE:
547 *data = g_variant_new_strv(*model->trigger_slopes,
548 g_strv_length((char **)*model->trigger_slopes));
550 case SR_CONF_TIMEBASE:
551 *data = build_tuples(model->timebases, model->num_timebases);
554 if (pg_type == PG_NONE)
555 return SR_ERR_CHANNEL_GROUP;
556 *data = build_tuples(model->vdivs, model->num_vdivs);
565 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
567 char command[MAX_COMMAND_SIZE];
568 struct sr_channel *ch;
569 struct dev_context *devc;
570 struct scope_config *model;
573 model = devc->model_config;
575 ch = devc->current_channel->data;
578 case SR_CHANNEL_ANALOG:
579 g_snprintf(command, sizeof(command),
580 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
583 case SR_CHANNEL_LOGIC:
584 g_snprintf(command, sizeof(command),
585 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
586 ch->index < 8 ? 1 : 2);
589 sr_err("Invalid channel type.");
593 return sr_scpi_send(sdi->conn, command);
596 static int hmo_check_channels(GSList *channels)
599 struct sr_channel *ch;
600 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
602 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
604 for (l = channels; l; l = l->next) {
607 case SR_CHANNEL_ANALOG:
609 enabled_chan3 = TRUE;
610 else if (ch->index == 3)
611 enabled_chan4 = TRUE;
613 case SR_CHANNEL_LOGIC:
624 if ((enabled_pod1 && enabled_chan3) ||
625 (enabled_pod2 && enabled_chan4))
631 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
635 gboolean *pod_enabled, setup_changed;
636 char command[MAX_COMMAND_SIZE];
637 struct scope_state *state;
638 struct scope_config *model;
639 struct sr_channel *ch;
640 struct dev_context *devc;
641 struct sr_scpi_dev_inst *scpi;
645 state = devc->model_state;
646 model = devc->model_config;
647 setup_changed = FALSE;
649 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
651 for (l = sdi->channels; l; l = l->next) {
654 case SR_CHANNEL_ANALOG:
655 if (ch->enabled == state->analog_channels[ch->index].state)
657 g_snprintf(command, sizeof(command),
658 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
659 ch->index + 1, ch->enabled);
661 if (sr_scpi_send(scpi, command) != SR_OK)
663 state->analog_channels[ch->index].state = ch->enabled;
664 setup_changed = TRUE;
666 case SR_CHANNEL_LOGIC:
668 * A digital POD needs to be enabled for every group of
672 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
674 if (ch->enabled == state->digital_channels[ch->index])
676 g_snprintf(command, sizeof(command),
677 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
678 ch->index, ch->enabled);
680 if (sr_scpi_send(scpi, command) != SR_OK)
683 state->digital_channels[ch->index] = ch->enabled;
684 setup_changed = TRUE;
691 for (i = 1; i <= model->digital_pods; ++i) {
692 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
694 g_snprintf(command, sizeof(command),
695 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
696 i, pod_enabled[i - 1]);
697 if (sr_scpi_send(scpi, command) != SR_OK)
699 state->digital_pods[i - 1] = pod_enabled[i - 1];
700 setup_changed = TRUE;
705 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
711 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
714 gboolean digital_added;
715 struct sr_channel *ch;
716 struct dev_context *devc;
717 struct sr_scpi_dev_inst *scpi;
719 if (sdi->status != SR_ST_ACTIVE)
720 return SR_ERR_DEV_CLOSED;
724 digital_added = FALSE;
726 for (l = sdi->channels; l; l = l->next) {
730 /* Only add a single digital channel. */
731 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
732 devc->enabled_channels = g_slist_append(
733 devc->enabled_channels, ch);
734 if (ch->type == SR_CHANNEL_LOGIC)
735 digital_added = TRUE;
739 if (!devc->enabled_channels)
742 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
743 sr_err("Invalid channel configuration specified!");
747 if (hmo_setup_channels(sdi) != SR_OK) {
748 sr_err("Failed to setup channel configuration!");
752 sr_scpi_source_add(scpi, G_IO_IN, 50, hmo_receive_data, (void *)sdi);
754 /* Send header packet to the session bus. */
755 std_session_send_df_header(cb_data, LOG_PREFIX);
757 devc->current_channel = devc->enabled_channels;
759 return hmo_request_data(sdi);
762 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
764 struct dev_context *devc;
765 struct sr_scpi_dev_inst *scpi;
766 struct sr_datafeed_packet packet;
770 packet.type = SR_DF_END;
771 packet.payload = NULL;
772 sr_session_send(sdi, &packet);
774 if (sdi->status != SR_ST_ACTIVE)
775 return SR_ERR_DEV_CLOSED;
779 devc->num_frames = 0;
780 g_slist_free(devc->enabled_channels);
781 devc->enabled_channels = NULL;
783 sr_scpi_source_remove(scpi);
788 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
790 .longname = "Hameg HMO",
795 .dev_list = dev_list,
796 .dev_clear = dev_clear,
797 .config_get = config_get,
798 .config_set = config_set,
799 .config_list = config_list,
800 .dev_open = dev_open,
801 .dev_close = dev_close,
802 .dev_acquisition_start = dev_acquisition_start,
803 .dev_acquisition_stop = dev_acquisition_stop,