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 check_manufacturer(const char *manufacturer)
54 for (i = 0; i < ARRAY_SIZE(manufacturers); i++)
55 if (!strcmp(manufacturer, manufacturers[i]))
61 static struct sr_dev_inst *hmo_probe_serial_device(struct sr_scpi_dev_inst *scpi)
63 struct sr_dev_inst *sdi;
64 struct dev_context *devc;
65 struct sr_scpi_hw_info *hw_info;
71 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
72 sr_info("Couldn't get IDN response.");
76 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
79 sdi = g_malloc0(sizeof(struct sr_dev_inst));
80 sdi->vendor = g_strdup(hw_info->manufacturer);
81 sdi->model = g_strdup(hw_info->model);
82 sdi->version = g_strdup(hw_info->firmware_version);
83 sdi->serial_num = g_strdup(hw_info->serial_number);
84 sdi->driver = &hameg_hmo_driver_info;
85 sdi->inst_type = SR_INST_SCPI;
88 sr_scpi_hw_info_free(hw_info);
91 devc = g_malloc0(sizeof(struct dev_context));
95 if (hmo_init_device(sdi) != SR_OK)
102 sr_scpi_hw_info_free(hw_info);
104 sr_dev_inst_free(sdi);
110 static GSList *scan(struct sr_dev_driver *di, GSList *options)
112 return sr_scpi_scan(di->context, options, hmo_probe_serial_device);
115 static void clear_helper(void *priv)
117 struct dev_context *devc;
121 hmo_scope_state_free(devc->model_state);
123 g_free(devc->analog_groups);
124 g_free(devc->digital_groups);
129 static int dev_clear(const struct sr_dev_driver *di)
131 return std_dev_clear(di, clear_helper);
134 static int dev_open(struct sr_dev_inst *sdi)
136 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
139 if (hmo_scope_state_get(sdi) != SR_OK)
142 sdi->status = SR_ST_ACTIVE;
147 static int dev_close(struct sr_dev_inst *sdi)
149 if (sdi->status == SR_ST_INACTIVE)
152 sr_scpi_close(sdi->conn);
154 sdi->status = SR_ST_INACTIVE;
159 static int check_channel_group(struct dev_context *devc,
160 const struct sr_channel_group *cg)
163 const struct scope_config *model;
165 model = devc->model_config;
170 for (i = 0; i < model->analog_channels; i++)
171 if (cg == devc->analog_groups[i])
174 for (i = 0; i < model->digital_pods; i++)
175 if (cg == devc->digital_groups[i])
178 sr_err("Invalid channel group specified.");
183 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
184 const struct sr_channel_group *cg)
188 struct dev_context *devc;
189 const struct scope_config *model;
190 struct scope_state *state;
192 if (!sdi || !(devc = sdi->priv))
195 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
199 model = devc->model_config;
200 state = devc->model_state;
203 case SR_CONF_NUM_HDIV:
204 *data = g_variant_new_int32(model->num_xdivs);
207 case SR_CONF_TIMEBASE:
208 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
209 (*model->timebases)[state->timebase][1]);
212 case SR_CONF_NUM_VDIV:
213 if (cg_type == CG_NONE) {
214 sr_err("No channel group specified.");
215 return SR_ERR_CHANNEL_GROUP;
216 } else if (cg_type == CG_ANALOG) {
217 for (i = 0; i < model->analog_channels; i++) {
218 if (cg != devc->analog_groups[i])
220 *data = g_variant_new_int32(model->num_ydivs);
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("(tt)",
238 (*model->vdivs)[state->analog_channels[i].vdiv][0],
239 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
248 case SR_CONF_TRIGGER_SOURCE:
249 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
252 case SR_CONF_TRIGGER_SLOPE:
253 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
256 case SR_CONF_HORIZ_TRIGGERPOS:
257 *data = g_variant_new_double(state->horiz_triggerpos);
260 case SR_CONF_COUPLING:
261 if (cg_type == CG_NONE) {
262 sr_err("No channel group specified.");
263 return SR_ERR_CHANNEL_GROUP;
264 } else if (cg_type == CG_ANALOG) {
265 for (i = 0; i < model->analog_channels; i++) {
266 if (cg != devc->analog_groups[i])
268 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
277 case SR_CONF_SAMPLERATE:
278 *data = g_variant_new_uint64(state->sample_rate);
288 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
291 GVariant *rational[2];
294 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
296 for (i = 0; i < n; i++) {
297 rational[0] = g_variant_new_uint64((*array)[i][0]);
298 rational[1] = g_variant_new_uint64((*array)[i][1]);
300 /* FIXME: Valgrind reports a memory leak here. */
301 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
304 return g_variant_builder_end(&gvb);
307 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
308 const struct sr_channel_group *cg)
312 char command[MAX_COMMAND_SIZE], float_str[30];
313 struct dev_context *devc;
314 const struct scope_config *model;
315 struct scope_state *state;
319 gboolean update_sample_rate;
321 if (!sdi || !(devc = sdi->priv))
324 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
327 model = devc->model_config;
328 state = devc->model_state;
329 update_sample_rate = FALSE;
334 case SR_CONF_LIMIT_FRAMES:
335 devc->frame_limit = g_variant_get_uint64(data);
338 case SR_CONF_TRIGGER_SOURCE:
339 tmp = g_variant_get_string(data, NULL);
340 for (i = 0; (*model->trigger_sources)[i]; i++) {
341 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
343 state->trigger_source = i;
344 g_snprintf(command, sizeof(command),
345 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
346 (*model->trigger_sources)[i]);
348 ret = sr_scpi_send(sdi->conn, command);
353 if (cg_type == CG_NONE) {
354 sr_err("No channel group specified.");
355 return SR_ERR_CHANNEL_GROUP;
358 g_variant_get(data, "(tt)", &p, &q);
360 for (i = 0; i < model->num_vdivs; i++) {
361 if (p != (*model->vdivs)[i][0] ||
362 q != (*model->vdivs)[i][1])
364 for (j = 1; j <= model->analog_channels; j++) {
365 if (cg != devc->analog_groups[j - 1])
367 state->analog_channels[j - 1].vdiv = i;
368 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
369 g_snprintf(command, sizeof(command),
370 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
373 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
374 sr_scpi_get_opc(sdi->conn) != SR_OK)
384 case SR_CONF_TIMEBASE:
385 g_variant_get(data, "(tt)", &p, &q);
387 for (i = 0; i < model->num_timebases; i++) {
388 if (p != (*model->timebases)[i][0] ||
389 q != (*model->timebases)[i][1])
392 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
393 g_snprintf(command, sizeof(command),
394 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
397 ret = sr_scpi_send(sdi->conn, command);
398 update_sample_rate = TRUE;
402 case SR_CONF_HORIZ_TRIGGERPOS:
403 tmp_d = g_variant_get_double(data);
405 if (tmp_d < 0.0 || tmp_d > 1.0)
408 state->horiz_triggerpos = tmp_d;
409 tmp_d = -(tmp_d - 0.5) *
410 ((double) (*model->timebases)[state->timebase][0] /
411 (*model->timebases)[state->timebase][1])
414 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
415 g_snprintf(command, sizeof(command),
416 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
419 ret = sr_scpi_send(sdi->conn, command);
421 case SR_CONF_TRIGGER_SLOPE:
422 tmp = g_variant_get_string(data, NULL);
423 for (i = 0; (*model->trigger_slopes)[i]; i++) {
424 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
426 state->trigger_slope = i;
427 g_snprintf(command, sizeof(command),
428 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
429 (*model->trigger_slopes)[i]);
431 ret = sr_scpi_send(sdi->conn, command);
435 case SR_CONF_COUPLING:
436 if (cg_type == CG_NONE) {
437 sr_err("No channel group specified.");
438 return SR_ERR_CHANNEL_GROUP;
441 tmp = g_variant_get_string(data, NULL);
443 for (i = 0; (*model->coupling_options)[i]; i++) {
444 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
446 for (j = 1; j <= model->analog_channels; j++) {
447 if (cg != devc->analog_groups[j - 1])
449 state->analog_channels[j-1].coupling = i;
451 g_snprintf(command, sizeof(command),
452 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
455 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
456 sr_scpi_get_opc(sdi->conn) != SR_OK)
471 ret = sr_scpi_get_opc(sdi->conn);
473 if (ret == SR_OK && update_sample_rate)
474 ret = hmo_update_sample_rate(sdi);
479 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
480 const struct sr_channel_group *cg)
482 int cg_type = CG_NONE;
483 struct dev_context *devc = NULL;
484 const struct scope_config *model = NULL;
486 if (sdi && (devc = sdi->priv)) {
487 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
490 model = devc->model_config;
494 case SR_CONF_SCAN_OPTIONS:
495 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
496 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
498 case SR_CONF_DEVICE_OPTIONS:
499 if (cg_type == CG_NONE) {
501 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
502 model->devopts, model->num_devopts, sizeof(uint32_t));
504 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
505 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
506 } else if (cg_type == CG_ANALOG) {
507 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
508 model->analog_devopts, model->num_analog_devopts,
511 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
512 NULL, 0, sizeof(uint32_t));
515 case SR_CONF_COUPLING:
516 if (cg_type == CG_NONE)
517 return SR_ERR_CHANNEL_GROUP;
518 *data = g_variant_new_strv(*model->coupling_options,
519 g_strv_length((char **)*model->coupling_options));
521 case SR_CONF_TRIGGER_SOURCE:
524 *data = g_variant_new_strv(*model->trigger_sources,
525 g_strv_length((char **)*model->trigger_sources));
527 case SR_CONF_TRIGGER_SLOPE:
530 *data = g_variant_new_strv(*model->trigger_slopes,
531 g_strv_length((char **)*model->trigger_slopes));
533 case SR_CONF_TIMEBASE:
536 *data = build_tuples(model->timebases, model->num_timebases);
539 if (cg_type == CG_NONE)
540 return SR_ERR_CHANNEL_GROUP;
541 *data = build_tuples(model->vdivs, model->num_vdivs);
550 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
552 char command[MAX_COMMAND_SIZE];
553 struct sr_channel *ch;
554 struct dev_context *devc;
555 const struct scope_config *model;
558 model = devc->model_config;
560 ch = devc->current_channel->data;
563 case SR_CHANNEL_ANALOG:
564 g_snprintf(command, sizeof(command),
565 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
568 case SR_CHANNEL_LOGIC:
569 g_snprintf(command, sizeof(command),
570 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
571 ch->index < 8 ? 1 : 2);
574 sr_err("Invalid channel type.");
578 return sr_scpi_send(sdi->conn, command);
581 static int hmo_check_channels(GSList *channels)
584 struct sr_channel *ch;
585 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
587 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
589 for (l = channels; l; l = l->next) {
592 case SR_CHANNEL_ANALOG:
594 enabled_chan3 = TRUE;
595 else if (ch->index == 3)
596 enabled_chan4 = TRUE;
598 case SR_CHANNEL_LOGIC:
609 if ((enabled_pod1 && enabled_chan3) ||
610 (enabled_pod2 && enabled_chan4))
616 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
620 gboolean *pod_enabled, setup_changed;
621 char command[MAX_COMMAND_SIZE];
622 struct scope_state *state;
623 const struct scope_config *model;
624 struct sr_channel *ch;
625 struct dev_context *devc;
626 struct sr_scpi_dev_inst *scpi;
630 state = devc->model_state;
631 model = devc->model_config;
632 setup_changed = FALSE;
634 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
636 for (l = sdi->channels; l; l = l->next) {
639 case SR_CHANNEL_ANALOG:
640 if (ch->enabled == state->analog_channels[ch->index].state)
642 g_snprintf(command, sizeof(command),
643 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
644 ch->index + 1, ch->enabled);
646 if (sr_scpi_send(scpi, command) != SR_OK)
648 state->analog_channels[ch->index].state = ch->enabled;
649 setup_changed = TRUE;
651 case SR_CHANNEL_LOGIC:
653 * A digital POD needs to be enabled for every group of
657 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
659 if (ch->enabled == state->digital_channels[ch->index])
661 g_snprintf(command, sizeof(command),
662 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
663 ch->index, ch->enabled);
665 if (sr_scpi_send(scpi, command) != SR_OK)
668 state->digital_channels[ch->index] = ch->enabled;
669 setup_changed = TRUE;
676 for (i = 1; i <= model->digital_pods; i++) {
677 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
679 g_snprintf(command, sizeof(command),
680 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
681 i, pod_enabled[i - 1]);
682 if (sr_scpi_send(scpi, command) != SR_OK)
684 state->digital_pods[i - 1] = pod_enabled[i - 1];
685 setup_changed = TRUE;
690 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
696 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
699 gboolean digital_added;
700 struct sr_channel *ch;
701 struct dev_context *devc;
702 struct sr_scpi_dev_inst *scpi;
704 if (sdi->status != SR_ST_ACTIVE)
705 return SR_ERR_DEV_CLOSED;
709 digital_added = FALSE;
711 g_slist_free(devc->enabled_channels);
712 devc->enabled_channels = NULL;
714 for (l = sdi->channels; l; l = l->next) {
718 /* Only add a single digital channel. */
719 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
720 devc->enabled_channels = g_slist_append(
721 devc->enabled_channels, ch);
722 if (ch->type == SR_CHANNEL_LOGIC)
723 digital_added = TRUE;
727 if (!devc->enabled_channels)
730 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
731 sr_err("Invalid channel configuration specified!");
735 if (hmo_setup_channels(sdi) != SR_OK) {
736 sr_err("Failed to setup channel configuration!");
740 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
741 hmo_receive_data, (void *)sdi);
743 std_session_send_df_header(sdi, LOG_PREFIX);
745 devc->current_channel = devc->enabled_channels;
747 return hmo_request_data(sdi);
750 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
752 struct dev_context *devc;
753 struct sr_scpi_dev_inst *scpi;
755 std_session_send_df_end(sdi, LOG_PREFIX);
757 if (sdi->status != SR_ST_ACTIVE)
758 return SR_ERR_DEV_CLOSED;
762 devc->num_frames = 0;
763 g_slist_free(devc->enabled_channels);
764 devc->enabled_channels = NULL;
766 sr_scpi_source_remove(sdi->session, scpi);
771 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
773 .longname = "Hameg HMO",
776 .cleanup = std_cleanup,
778 .dev_list = std_dev_list,
779 .dev_clear = dev_clear,
780 .config_get = config_get,
781 .config_set = config_set,
782 .config_list = config_list,
783 .dev_open = dev_open,
784 .dev_close = dev_close,
785 .dev_acquisition_start = dev_acquisition_start,
786 .dev_acquisition_stop = dev_acquisition_stop,