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[] = {
34 static const uint32_t drvopts[] = {
38 static const uint32_t scanopts[] = {
50 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
52 return std_init(sr_ctx, di, LOG_PREFIX);
55 static int check_manufacturer(const char *manufacturer)
59 for (i = 0; i < ARRAY_SIZE(manufacturers); i++)
60 if (!strcmp(manufacturer, manufacturers[i]))
66 static struct sr_dev_inst *hmo_probe_serial_device(struct sr_scpi_dev_inst *scpi)
68 struct sr_dev_inst *sdi;
69 struct dev_context *devc;
70 struct sr_scpi_hw_info *hw_info;
76 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
77 sr_info("Couldn't get IDN response.");
81 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
84 sdi = g_malloc0(sizeof(struct sr_dev_inst));
85 sdi->vendor = g_strdup(hw_info->manufacturer);
86 sdi->model = g_strdup(hw_info->model);
87 sdi->version = g_strdup(hw_info->firmware_version);
88 sdi->serial_num = g_strdup(hw_info->serial_number);
89 sdi->driver = &hameg_hmo_driver_info;
90 sdi->inst_type = SR_INST_SCPI;
93 sr_scpi_hw_info_free(hw_info);
96 devc = g_malloc0(sizeof(struct dev_context));
100 if (hmo_init_device(sdi) != SR_OK)
107 sr_scpi_hw_info_free(hw_info);
109 sr_dev_inst_free(sdi);
115 static GSList *scan(struct sr_dev_driver *di, GSList *options)
117 return sr_scpi_scan(di->context, options, hmo_probe_serial_device);
120 static GSList *dev_list(const struct sr_dev_driver *di)
122 return ((struct drv_context *)(di->context))->instances;
125 static void clear_helper(void *priv)
127 struct dev_context *devc;
131 hmo_scope_state_free(devc->model_state);
133 g_free(devc->analog_groups);
134 g_free(devc->digital_groups);
139 static int dev_clear(const struct sr_dev_driver *di)
141 return std_dev_clear(di, clear_helper);
144 static int dev_open(struct sr_dev_inst *sdi)
146 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
149 if (hmo_scope_state_get(sdi) != SR_OK)
152 sdi->status = SR_ST_ACTIVE;
157 static int dev_close(struct sr_dev_inst *sdi)
159 if (sdi->status == SR_ST_INACTIVE)
162 sr_scpi_close(sdi->conn);
164 sdi->status = SR_ST_INACTIVE;
169 static int cleanup(const struct sr_dev_driver *di)
176 static int check_channel_group(struct dev_context *devc,
177 const struct sr_channel_group *cg)
180 const struct scope_config *model;
182 model = devc->model_config;
187 for (i = 0; i < model->analog_channels; i++)
188 if (cg == devc->analog_groups[i])
191 for (i = 0; i < model->digital_pods; i++)
192 if (cg == devc->digital_groups[i])
195 sr_err("Invalid channel group specified.");
200 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
201 const struct sr_channel_group *cg)
205 struct dev_context *devc;
206 const struct scope_config *model;
207 struct scope_state *state;
209 if (!sdi || !(devc = sdi->priv))
212 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
216 model = devc->model_config;
217 state = devc->model_state;
220 case SR_CONF_NUM_HDIV:
221 *data = g_variant_new_int32(model->num_xdivs);
224 case SR_CONF_TIMEBASE:
225 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
226 (*model->timebases)[state->timebase][1]);
229 case SR_CONF_NUM_VDIV:
230 if (cg_type == CG_NONE) {
231 sr_err("No channel group specified.");
232 return SR_ERR_CHANNEL_GROUP;
233 } else if (cg_type == CG_ANALOG) {
234 for (i = 0; i < model->analog_channels; i++) {
235 if (cg != devc->analog_groups[i])
237 *data = g_variant_new_int32(model->num_ydivs);
247 if (cg_type == CG_NONE) {
248 sr_err("No channel group specified.");
249 return SR_ERR_CHANNEL_GROUP;
250 } else if (cg_type == CG_ANALOG) {
251 for (i = 0; i < model->analog_channels; i++) {
252 if (cg != devc->analog_groups[i])
254 *data = g_variant_new("(tt)",
255 (*model->vdivs)[state->analog_channels[i].vdiv][0],
256 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
265 case SR_CONF_TRIGGER_SOURCE:
266 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
269 case SR_CONF_TRIGGER_SLOPE:
270 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
273 case SR_CONF_HORIZ_TRIGGERPOS:
274 *data = g_variant_new_double(state->horiz_triggerpos);
277 case SR_CONF_COUPLING:
278 if (cg_type == CG_NONE) {
279 sr_err("No channel group specified.");
280 return SR_ERR_CHANNEL_GROUP;
281 } else if (cg_type == CG_ANALOG) {
282 for (i = 0; i < model->analog_channels; i++) {
283 if (cg != devc->analog_groups[i])
285 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
294 case SR_CONF_SAMPLERATE:
295 *data = g_variant_new_uint64(state->sample_rate);
305 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
308 GVariant *rational[2];
311 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
313 for (i = 0; i < n; i++) {
314 rational[0] = g_variant_new_uint64((*array)[i][0]);
315 rational[1] = g_variant_new_uint64((*array)[i][1]);
317 /* FIXME: Valgrind reports a memory leak here. */
318 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
321 return g_variant_builder_end(&gvb);
324 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
325 const struct sr_channel_group *cg)
329 char command[MAX_COMMAND_SIZE], float_str[30];
330 struct dev_context *devc;
331 const struct scope_config *model;
332 struct scope_state *state;
336 gboolean update_sample_rate;
338 if (!sdi || !(devc = sdi->priv))
341 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
344 model = devc->model_config;
345 state = devc->model_state;
346 update_sample_rate = FALSE;
351 case SR_CONF_LIMIT_FRAMES:
352 devc->frame_limit = g_variant_get_uint64(data);
355 case SR_CONF_TRIGGER_SOURCE:
356 tmp = g_variant_get_string(data, NULL);
357 for (i = 0; (*model->trigger_sources)[i]; i++) {
358 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
360 state->trigger_source = i;
361 g_snprintf(command, sizeof(command),
362 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
363 (*model->trigger_sources)[i]);
365 ret = sr_scpi_send(sdi->conn, command);
370 if (cg_type == CG_NONE) {
371 sr_err("No channel group specified.");
372 return SR_ERR_CHANNEL_GROUP;
375 g_variant_get(data, "(tt)", &p, &q);
377 for (i = 0; i < model->num_vdivs; i++) {
378 if (p != (*model->vdivs)[i][0] ||
379 q != (*model->vdivs)[i][1])
381 for (j = 1; j <= model->analog_channels; j++) {
382 if (cg != devc->analog_groups[j - 1])
384 state->analog_channels[j - 1].vdiv = i;
385 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
386 g_snprintf(command, sizeof(command),
387 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
390 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
391 sr_scpi_get_opc(sdi->conn) != SR_OK)
401 case SR_CONF_TIMEBASE:
402 g_variant_get(data, "(tt)", &p, &q);
404 for (i = 0; i < model->num_timebases; i++) {
405 if (p != (*model->timebases)[i][0] ||
406 q != (*model->timebases)[i][1])
409 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
410 g_snprintf(command, sizeof(command),
411 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
414 ret = sr_scpi_send(sdi->conn, command);
415 update_sample_rate = TRUE;
419 case SR_CONF_HORIZ_TRIGGERPOS:
420 tmp_d = g_variant_get_double(data);
422 if (tmp_d < 0.0 || tmp_d > 1.0)
425 state->horiz_triggerpos = tmp_d;
426 tmp_d = -(tmp_d - 0.5) *
427 ((double) (*model->timebases)[state->timebase][0] /
428 (*model->timebases)[state->timebase][1])
431 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
432 g_snprintf(command, sizeof(command),
433 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
436 ret = sr_scpi_send(sdi->conn, command);
438 case SR_CONF_TRIGGER_SLOPE:
439 tmp = g_variant_get_string(data, NULL);
440 for (i = 0; (*model->trigger_slopes)[i]; i++) {
441 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
443 state->trigger_slope = i;
444 g_snprintf(command, sizeof(command),
445 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
446 (*model->trigger_slopes)[i]);
448 ret = sr_scpi_send(sdi->conn, command);
452 case SR_CONF_COUPLING:
453 if (cg_type == CG_NONE) {
454 sr_err("No channel group specified.");
455 return SR_ERR_CHANNEL_GROUP;
458 tmp = g_variant_get_string(data, NULL);
460 for (i = 0; (*model->coupling_options)[i]; i++) {
461 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
463 for (j = 1; j <= model->analog_channels; j++) {
464 if (cg != devc->analog_groups[j - 1])
466 state->analog_channels[j-1].coupling = i;
468 g_snprintf(command, sizeof(command),
469 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
472 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
473 sr_scpi_get_opc(sdi->conn) != SR_OK)
488 ret = sr_scpi_get_opc(sdi->conn);
490 if (ret == SR_OK && update_sample_rate)
491 ret = hmo_update_sample_rate(sdi);
496 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
497 const struct sr_channel_group *cg)
499 int cg_type = CG_NONE;
500 struct dev_context *devc = NULL;
501 const struct scope_config *model = NULL;
503 if (sdi && (devc = sdi->priv)) {
504 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
507 model = devc->model_config;
511 case SR_CONF_SCAN_OPTIONS:
512 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
513 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
515 case SR_CONF_DEVICE_OPTIONS:
516 if (cg_type == CG_NONE) {
518 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
519 model->devopts, model->num_devopts, sizeof(uint32_t));
521 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
522 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
523 } else if (cg_type == CG_ANALOG) {
524 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
525 model->analog_devopts, model->num_analog_devopts,
528 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
529 NULL, 0, sizeof(uint32_t));
532 case SR_CONF_COUPLING:
533 if (cg_type == CG_NONE)
534 return SR_ERR_CHANNEL_GROUP;
535 *data = g_variant_new_strv(*model->coupling_options,
536 g_strv_length((char **)*model->coupling_options));
538 case SR_CONF_TRIGGER_SOURCE:
541 *data = g_variant_new_strv(*model->trigger_sources,
542 g_strv_length((char **)*model->trigger_sources));
544 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:
553 *data = build_tuples(model->timebases, model->num_timebases);
556 if (cg_type == CG_NONE)
557 return SR_ERR_CHANNEL_GROUP;
558 *data = build_tuples(model->vdivs, model->num_vdivs);
567 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
569 char command[MAX_COMMAND_SIZE];
570 struct sr_channel *ch;
571 struct dev_context *devc;
572 const struct scope_config *model;
575 model = devc->model_config;
577 ch = devc->current_channel->data;
580 case SR_CHANNEL_ANALOG:
581 g_snprintf(command, sizeof(command),
582 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
585 case SR_CHANNEL_LOGIC:
586 g_snprintf(command, sizeof(command),
587 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
588 ch->index < 8 ? 1 : 2);
591 sr_err("Invalid channel type.");
595 return sr_scpi_send(sdi->conn, command);
598 static int hmo_check_channels(GSList *channels)
601 struct sr_channel *ch;
602 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
604 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
606 for (l = channels; l; l = l->next) {
609 case SR_CHANNEL_ANALOG:
611 enabled_chan3 = TRUE;
612 else if (ch->index == 3)
613 enabled_chan4 = TRUE;
615 case SR_CHANNEL_LOGIC:
626 if ((enabled_pod1 && enabled_chan3) ||
627 (enabled_pod2 && enabled_chan4))
633 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
637 gboolean *pod_enabled, setup_changed;
638 char command[MAX_COMMAND_SIZE];
639 struct scope_state *state;
640 const struct scope_config *model;
641 struct sr_channel *ch;
642 struct dev_context *devc;
643 struct sr_scpi_dev_inst *scpi;
647 state = devc->model_state;
648 model = devc->model_config;
649 setup_changed = FALSE;
651 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
653 for (l = sdi->channels; l; l = l->next) {
656 case SR_CHANNEL_ANALOG:
657 if (ch->enabled == state->analog_channels[ch->index].state)
659 g_snprintf(command, sizeof(command),
660 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
661 ch->index + 1, ch->enabled);
663 if (sr_scpi_send(scpi, command) != SR_OK)
665 state->analog_channels[ch->index].state = ch->enabled;
666 setup_changed = TRUE;
668 case SR_CHANNEL_LOGIC:
670 * A digital POD needs to be enabled for every group of
674 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
676 if (ch->enabled == state->digital_channels[ch->index])
678 g_snprintf(command, sizeof(command),
679 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
680 ch->index, ch->enabled);
682 if (sr_scpi_send(scpi, command) != SR_OK)
685 state->digital_channels[ch->index] = ch->enabled;
686 setup_changed = TRUE;
693 for (i = 1; i <= model->digital_pods; i++) {
694 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
696 g_snprintf(command, sizeof(command),
697 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
698 i, pod_enabled[i - 1]);
699 if (sr_scpi_send(scpi, command) != SR_OK)
701 state->digital_pods[i - 1] = pod_enabled[i - 1];
702 setup_changed = TRUE;
707 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
713 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
716 gboolean digital_added;
717 struct sr_channel *ch;
718 struct dev_context *devc;
719 struct sr_scpi_dev_inst *scpi;
721 if (sdi->status != SR_ST_ACTIVE)
722 return SR_ERR_DEV_CLOSED;
726 digital_added = FALSE;
728 g_slist_free(devc->enabled_channels);
729 devc->enabled_channels = NULL;
731 for (l = sdi->channels; l; l = l->next) {
735 /* Only add a single digital channel. */
736 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
737 devc->enabled_channels = g_slist_append(
738 devc->enabled_channels, ch);
739 if (ch->type == SR_CHANNEL_LOGIC)
740 digital_added = TRUE;
744 if (!devc->enabled_channels)
747 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
748 sr_err("Invalid channel configuration specified!");
752 if (hmo_setup_channels(sdi) != SR_OK) {
753 sr_err("Failed to setup channel configuration!");
757 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
758 hmo_receive_data, (void *)sdi);
760 /* Send header packet to the session bus. */
761 std_session_send_df_header(cb_data, LOG_PREFIX);
763 devc->current_channel = devc->enabled_channels;
765 return hmo_request_data(sdi);
768 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
770 struct dev_context *devc;
771 struct sr_scpi_dev_inst *scpi;
772 struct sr_datafeed_packet packet;
776 packet.type = SR_DF_END;
777 packet.payload = NULL;
778 sr_session_send(sdi, &packet);
780 if (sdi->status != SR_ST_ACTIVE)
781 return SR_ERR_DEV_CLOSED;
785 devc->num_frames = 0;
786 g_slist_free(devc->enabled_channels);
787 devc->enabled_channels = NULL;
789 sr_scpi_source_remove(sdi->session, scpi);
794 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
796 .longname = "Hameg HMO",
801 .dev_list = dev_list,
802 .dev_clear = dev_clear,
803 .config_get = config_get,
804 .config_set = config_set,
805 .config_list = config_list,
806 .dev_open = dev_open,
807 .dev_close = dev_close,
808 .dev_acquisition_start = dev_acquisition_start,
809 .dev_acquisition_stop = dev_acquisition_stop,