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);
117 static GSList *scan(struct sr_dev_driver *di, GSList *options)
119 return sr_scpi_scan(di->priv, options, hmo_probe_serial_device);
122 static GSList *dev_list(const struct sr_dev_driver *di)
124 return ((struct drv_context *)(di->priv))->instances;
127 static void clear_helper(void *priv)
129 struct dev_context *devc;
133 hmo_scope_state_free(devc->model_state);
135 g_free(devc->analog_groups);
136 g_free(devc->digital_groups);
141 static int dev_clear(const struct sr_dev_driver *di)
143 return std_dev_clear(di, clear_helper);
146 static int dev_open(struct sr_dev_inst *sdi)
148 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
151 if (hmo_scope_state_get(sdi) != SR_OK)
154 sdi->status = SR_ST_ACTIVE;
159 static int dev_close(struct sr_dev_inst *sdi)
161 if (sdi->status == SR_ST_INACTIVE)
164 sr_scpi_close(sdi->conn);
166 sdi->status = SR_ST_INACTIVE;
171 static int cleanup(const struct sr_dev_driver *di)
178 static int check_channel_group(struct dev_context *devc,
179 const struct sr_channel_group *cg)
182 const struct scope_config *model;
184 model = devc->model_config;
189 for (i = 0; i < model->analog_channels; ++i)
190 if (cg == devc->analog_groups[i])
193 for (i = 0; i < model->digital_pods; ++i)
194 if (cg == devc->digital_groups[i])
197 sr_err("Invalid channel group specified.");
202 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
203 const struct sr_channel_group *cg)
207 struct dev_context *devc;
208 const struct scope_config *model;
209 struct scope_state *state;
211 if (!sdi || !(devc = sdi->priv))
214 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
218 model = devc->model_config;
219 state = devc->model_state;
222 case SR_CONF_NUM_HDIV:
223 *data = g_variant_new_int32(model->num_xdivs);
226 case SR_CONF_TIMEBASE:
227 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
228 (*model->timebases)[state->timebase][1]);
231 case SR_CONF_NUM_VDIV:
232 if (cg_type == CG_NONE) {
233 sr_err("No channel group specified.");
234 return SR_ERR_CHANNEL_GROUP;
235 } else if (cg_type == CG_ANALOG) {
236 for (i = 0; i < model->analog_channels; ++i) {
237 if (cg != devc->analog_groups[i])
239 *data = g_variant_new_int32(model->num_ydivs);
249 if (cg_type == CG_NONE) {
250 sr_err("No channel group specified.");
251 return SR_ERR_CHANNEL_GROUP;
252 } else if (cg_type == CG_ANALOG) {
253 for (i = 0; i < model->analog_channels; ++i) {
254 if (cg != devc->analog_groups[i])
256 *data = g_variant_new("(tt)",
257 (*model->vdivs)[state->analog_channels[i].vdiv][0],
258 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
267 case SR_CONF_TRIGGER_SOURCE:
268 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
271 case SR_CONF_TRIGGER_SLOPE:
272 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
275 case SR_CONF_HORIZ_TRIGGERPOS:
276 *data = g_variant_new_double(state->horiz_triggerpos);
279 case SR_CONF_COUPLING:
280 if (cg_type == CG_NONE) {
281 sr_err("No channel group specified.");
282 return SR_ERR_CHANNEL_GROUP;
283 } else if (cg_type == CG_ANALOG) {
284 for (i = 0; i < model->analog_channels; ++i) {
285 if (cg != devc->analog_groups[i])
287 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
296 case SR_CONF_SAMPLERATE:
297 *data = g_variant_new_uint64(state->sample_rate);
307 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
310 GVariant *rational[2];
313 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
315 for (i = 0; i < n; i++) {
316 rational[0] = g_variant_new_uint64((*array)[i][0]);
317 rational[1] = g_variant_new_uint64((*array)[i][1]);
319 /* FIXME: Valgrind reports a memory leak here. */
320 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
323 return g_variant_builder_end(&gvb);
326 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
327 const struct sr_channel_group *cg)
331 char command[MAX_COMMAND_SIZE], float_str[30];
332 struct dev_context *devc;
333 const struct scope_config *model;
334 struct scope_state *state;
338 gboolean update_sample_rate;
340 if (!sdi || !(devc = sdi->priv))
343 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
346 model = devc->model_config;
347 state = devc->model_state;
348 update_sample_rate = FALSE;
353 case SR_CONF_LIMIT_FRAMES:
354 devc->frame_limit = g_variant_get_uint64(data);
357 case SR_CONF_TRIGGER_SOURCE:
358 tmp = g_variant_get_string(data, NULL);
359 for (i = 0; (*model->trigger_sources)[i]; i++) {
360 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
362 state->trigger_source = i;
363 g_snprintf(command, sizeof(command),
364 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
365 (*model->trigger_sources)[i]);
367 ret = sr_scpi_send(sdi->conn, command);
372 if (cg_type == CG_NONE) {
373 sr_err("No channel group specified.");
374 return SR_ERR_CHANNEL_GROUP;
377 g_variant_get(data, "(tt)", &p, &q);
379 for (i = 0; i < model->num_vdivs; i++) {
380 if (p != (*model->vdivs)[i][0] ||
381 q != (*model->vdivs)[i][1])
383 for (j = 1; j <= model->analog_channels; ++j) {
384 if (cg != devc->analog_groups[j - 1])
386 state->analog_channels[j - 1].vdiv = i;
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_VERTICAL_DIV],
392 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
393 sr_scpi_get_opc(sdi->conn) != SR_OK)
403 case SR_CONF_TIMEBASE:
404 g_variant_get(data, "(tt)", &p, &q);
406 for (i = 0; i < model->num_timebases; i++) {
407 if (p != (*model->timebases)[i][0] ||
408 q != (*model->timebases)[i][1])
411 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
412 g_snprintf(command, sizeof(command),
413 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
416 ret = sr_scpi_send(sdi->conn, command);
417 update_sample_rate = TRUE;
421 case SR_CONF_HORIZ_TRIGGERPOS:
422 tmp_d = g_variant_get_double(data);
424 if (tmp_d < 0.0 || tmp_d > 1.0)
427 state->horiz_triggerpos = tmp_d;
428 tmp_d = -(tmp_d - 0.5) *
429 ((double) (*model->timebases)[state->timebase][0] /
430 (*model->timebases)[state->timebase][1])
433 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
434 g_snprintf(command, sizeof(command),
435 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
438 ret = sr_scpi_send(sdi->conn, command);
440 case SR_CONF_TRIGGER_SLOPE:
441 tmp = g_variant_get_string(data, NULL);
443 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
446 state->trigger_slope = (tmp[0] == 'r') ? 0 : 1;
448 g_snprintf(command, sizeof(command),
449 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
450 (state->trigger_slope == 0) ? "POS" : "NEG");
452 ret = sr_scpi_send(sdi->conn, command);
454 case SR_CONF_COUPLING:
455 if (cg_type == CG_NONE) {
456 sr_err("No channel group specified.");
457 return SR_ERR_CHANNEL_GROUP;
460 tmp = g_variant_get_string(data, NULL);
462 for (i = 0; (*model->coupling_options)[i]; i++) {
463 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
465 for (j = 1; j <= model->analog_channels; ++j) {
466 if (cg != devc->analog_groups[j - 1])
468 state->analog_channels[j-1].coupling = i;
470 g_snprintf(command, sizeof(command),
471 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
474 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
475 sr_scpi_get_opc(sdi->conn) != SR_OK)
490 ret = sr_scpi_get_opc(sdi->conn);
492 if (ret == SR_OK && update_sample_rate)
493 ret = hmo_update_sample_rate(sdi);
498 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
499 const struct sr_channel_group *cg)
501 int cg_type = CG_NONE;
502 struct dev_context *devc = NULL;
503 const struct scope_config *model = NULL;
505 if (sdi && (devc = sdi->priv)) {
506 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
509 model = devc->model_config;
513 case SR_CONF_SCAN_OPTIONS:
514 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
515 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
517 case SR_CONF_DEVICE_OPTIONS:
518 if (cg_type == CG_NONE) {
520 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
521 model->devopts, model->num_devopts, sizeof(uint32_t));
523 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
524 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
525 } else if (cg_type == CG_ANALOG) {
526 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
527 model->analog_devopts, model->num_analog_devopts,
530 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
531 NULL, 0, sizeof(uint32_t));
534 case SR_CONF_COUPLING:
535 if (cg_type == CG_NONE)
536 return SR_ERR_CHANNEL_GROUP;
537 *data = g_variant_new_strv(*model->coupling_options,
538 g_strv_length((char **)*model->coupling_options));
540 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:
549 *data = g_variant_new_strv(*model->trigger_slopes,
550 g_strv_length((char **)*model->trigger_slopes));
552 case SR_CONF_TIMEBASE:
555 *data = build_tuples(model->timebases, model->num_timebases);
558 if (cg_type == CG_NONE)
559 return SR_ERR_CHANNEL_GROUP;
560 *data = build_tuples(model->vdivs, model->num_vdivs);
569 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
571 char command[MAX_COMMAND_SIZE];
572 struct sr_channel *ch;
573 struct dev_context *devc;
574 const struct scope_config *model;
577 model = devc->model_config;
579 ch = devc->current_channel->data;
582 case SR_CHANNEL_ANALOG:
583 g_snprintf(command, sizeof(command),
584 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
587 case SR_CHANNEL_LOGIC:
588 g_snprintf(command, sizeof(command),
589 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
590 ch->index < 8 ? 1 : 2);
593 sr_err("Invalid channel type.");
597 return sr_scpi_send(sdi->conn, command);
600 static int hmo_check_channels(GSList *channels)
603 struct sr_channel *ch;
604 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
606 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
608 for (l = channels; l; l = l->next) {
611 case SR_CHANNEL_ANALOG:
613 enabled_chan3 = TRUE;
614 else if (ch->index == 3)
615 enabled_chan4 = TRUE;
617 case SR_CHANNEL_LOGIC:
628 if ((enabled_pod1 && enabled_chan3) ||
629 (enabled_pod2 && enabled_chan4))
635 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
639 gboolean *pod_enabled, setup_changed;
640 char command[MAX_COMMAND_SIZE];
641 struct scope_state *state;
642 const struct scope_config *model;
643 struct sr_channel *ch;
644 struct dev_context *devc;
645 struct sr_scpi_dev_inst *scpi;
649 state = devc->model_state;
650 model = devc->model_config;
651 setup_changed = FALSE;
653 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
655 for (l = sdi->channels; l; l = l->next) {
658 case SR_CHANNEL_ANALOG:
659 if (ch->enabled == state->analog_channels[ch->index].state)
661 g_snprintf(command, sizeof(command),
662 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
663 ch->index + 1, ch->enabled);
665 if (sr_scpi_send(scpi, command) != SR_OK)
667 state->analog_channels[ch->index].state = ch->enabled;
668 setup_changed = TRUE;
670 case SR_CHANNEL_LOGIC:
672 * A digital POD needs to be enabled for every group of
676 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
678 if (ch->enabled == state->digital_channels[ch->index])
680 g_snprintf(command, sizeof(command),
681 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
682 ch->index, ch->enabled);
684 if (sr_scpi_send(scpi, command) != SR_OK)
687 state->digital_channels[ch->index] = ch->enabled;
688 setup_changed = TRUE;
695 for (i = 1; i <= model->digital_pods; ++i) {
696 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
698 g_snprintf(command, sizeof(command),
699 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
700 i, pod_enabled[i - 1]);
701 if (sr_scpi_send(scpi, command) != SR_OK)
703 state->digital_pods[i - 1] = pod_enabled[i - 1];
704 setup_changed = TRUE;
709 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
715 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
718 gboolean digital_added;
719 struct sr_channel *ch;
720 struct dev_context *devc;
721 struct sr_scpi_dev_inst *scpi;
723 if (sdi->status != SR_ST_ACTIVE)
724 return SR_ERR_DEV_CLOSED;
728 digital_added = FALSE;
730 g_slist_free(devc->enabled_channels);
731 devc->enabled_channels = NULL;
733 for (l = sdi->channels; l; l = l->next) {
737 /* Only add a single digital channel. */
738 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
739 devc->enabled_channels = g_slist_append(
740 devc->enabled_channels, ch);
741 if (ch->type == SR_CHANNEL_LOGIC)
742 digital_added = TRUE;
746 if (!devc->enabled_channels)
749 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
750 sr_err("Invalid channel configuration specified!");
754 if (hmo_setup_channels(sdi) != SR_OK) {
755 sr_err("Failed to setup channel configuration!");
759 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
760 hmo_receive_data, (void *)sdi);
762 /* Send header packet to the session bus. */
763 std_session_send_df_header(cb_data, LOG_PREFIX);
765 devc->current_channel = devc->enabled_channels;
767 return hmo_request_data(sdi);
770 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
772 struct dev_context *devc;
773 struct sr_scpi_dev_inst *scpi;
774 struct sr_datafeed_packet packet;
778 packet.type = SR_DF_END;
779 packet.payload = NULL;
780 sr_session_send(sdi, &packet);
782 if (sdi->status != SR_ST_ACTIVE)
783 return SR_ERR_DEV_CLOSED;
787 devc->num_frames = 0;
788 g_slist_free(devc->enabled_channels);
789 devc->enabled_channels = NULL;
791 sr_scpi_source_remove(sdi->session, scpi);
796 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
798 .longname = "Hameg HMO",
803 .dev_list = dev_list,
804 .dev_clear = dev_clear,
805 .config_get = config_get,
806 .config_set = config_set,
807 .config_list = config_list,
808 .dev_open = dev_open,
809 .dev_close = dev_close,
810 .dev_acquisition_start = dev_acquisition_start,
811 .dev_acquisition_stop = dev_acquisition_stop,