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;
197 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
201 model = devc->model_config;
202 state = devc->model_state;
205 case SR_CONF_NUM_HDIV:
206 *data = g_variant_new_int32(model->num_xdivs);
209 case SR_CONF_TIMEBASE:
210 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
211 (*model->timebases)[state->timebase][1]);
214 case SR_CONF_NUM_VDIV:
215 if (cg_type == CG_NONE) {
216 sr_err("No channel group specified.");
217 return SR_ERR_CHANNEL_GROUP;
218 } else if (cg_type == CG_ANALOG) {
219 for (i = 0; i < model->analog_channels; i++) {
220 if (cg != devc->analog_groups[i])
222 *data = g_variant_new_int32(model->num_ydivs);
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("(tt)",
240 (*model->vdivs)[state->analog_channels[i].vdiv][0],
241 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
250 case SR_CONF_TRIGGER_SOURCE:
251 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
254 case SR_CONF_TRIGGER_SLOPE:
255 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
258 case SR_CONF_HORIZ_TRIGGERPOS:
259 *data = g_variant_new_double(state->horiz_triggerpos);
262 case SR_CONF_COUPLING:
263 if (cg_type == CG_NONE) {
264 sr_err("No channel group specified.");
265 return SR_ERR_CHANNEL_GROUP;
266 } else if (cg_type == CG_ANALOG) {
267 for (i = 0; i < model->analog_channels; i++) {
268 if (cg != devc->analog_groups[i])
270 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
279 case SR_CONF_SAMPLERATE:
280 *data = g_variant_new_uint64(state->sample_rate);
290 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
293 GVariant *rational[2];
296 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
298 for (i = 0; i < n; i++) {
299 rational[0] = g_variant_new_uint64((*array)[i][0]);
300 rational[1] = g_variant_new_uint64((*array)[i][1]);
302 /* FIXME: Valgrind reports a memory leak here. */
303 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
306 return g_variant_builder_end(&gvb);
309 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
310 const struct sr_channel_group *cg)
314 char command[MAX_COMMAND_SIZE], float_str[30];
315 struct dev_context *devc;
316 const struct scope_config *model;
317 struct scope_state *state;
321 gboolean update_sample_rate;
328 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
331 model = devc->model_config;
332 state = devc->model_state;
333 update_sample_rate = FALSE;
338 case SR_CONF_LIMIT_FRAMES:
339 devc->frame_limit = g_variant_get_uint64(data);
342 case SR_CONF_TRIGGER_SOURCE:
343 tmp = g_variant_get_string(data, NULL);
344 for (i = 0; (*model->trigger_sources)[i]; i++) {
345 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
347 state->trigger_source = i;
348 g_snprintf(command, sizeof(command),
349 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
350 (*model->trigger_sources)[i]);
352 ret = sr_scpi_send(sdi->conn, command);
357 if (cg_type == CG_NONE) {
358 sr_err("No channel group specified.");
359 return SR_ERR_CHANNEL_GROUP;
362 g_variant_get(data, "(tt)", &p, &q);
364 for (i = 0; i < model->num_vdivs; i++) {
365 if (p != (*model->vdivs)[i][0] ||
366 q != (*model->vdivs)[i][1])
368 for (j = 1; j <= model->analog_channels; j++) {
369 if (cg != devc->analog_groups[j - 1])
371 state->analog_channels[j - 1].vdiv = i;
372 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
373 g_snprintf(command, sizeof(command),
374 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
377 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
378 sr_scpi_get_opc(sdi->conn) != SR_OK)
388 case SR_CONF_TIMEBASE:
389 g_variant_get(data, "(tt)", &p, &q);
391 for (i = 0; i < model->num_timebases; i++) {
392 if (p != (*model->timebases)[i][0] ||
393 q != (*model->timebases)[i][1])
396 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
397 g_snprintf(command, sizeof(command),
398 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
401 ret = sr_scpi_send(sdi->conn, command);
402 update_sample_rate = TRUE;
406 case SR_CONF_HORIZ_TRIGGERPOS:
407 tmp_d = g_variant_get_double(data);
409 if (tmp_d < 0.0 || tmp_d > 1.0)
412 state->horiz_triggerpos = tmp_d;
413 tmp_d = -(tmp_d - 0.5) *
414 ((double) (*model->timebases)[state->timebase][0] /
415 (*model->timebases)[state->timebase][1])
418 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
419 g_snprintf(command, sizeof(command),
420 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
423 ret = sr_scpi_send(sdi->conn, command);
425 case SR_CONF_TRIGGER_SLOPE:
426 tmp = g_variant_get_string(data, NULL);
427 for (i = 0; (*model->trigger_slopes)[i]; i++) {
428 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
430 state->trigger_slope = i;
431 g_snprintf(command, sizeof(command),
432 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
433 (*model->trigger_slopes)[i]);
435 ret = sr_scpi_send(sdi->conn, command);
439 case SR_CONF_COUPLING:
440 if (cg_type == CG_NONE) {
441 sr_err("No channel group specified.");
442 return SR_ERR_CHANNEL_GROUP;
445 tmp = g_variant_get_string(data, NULL);
447 for (i = 0; (*model->coupling_options)[i]; i++) {
448 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
450 for (j = 1; j <= model->analog_channels; j++) {
451 if (cg != devc->analog_groups[j - 1])
453 state->analog_channels[j-1].coupling = i;
455 g_snprintf(command, sizeof(command),
456 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
459 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
460 sr_scpi_get_opc(sdi->conn) != SR_OK)
475 ret = sr_scpi_get_opc(sdi->conn);
477 if (ret == SR_OK && update_sample_rate)
478 ret = hmo_update_sample_rate(sdi);
483 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
484 const struct sr_channel_group *cg)
486 int cg_type = CG_NONE;
487 struct dev_context *devc = NULL;
488 const struct scope_config *model = NULL;
492 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
495 model = devc->model_config;
499 case SR_CONF_SCAN_OPTIONS:
500 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
501 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
503 case SR_CONF_DEVICE_OPTIONS:
504 if (cg_type == CG_NONE) {
506 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
507 model->devopts, model->num_devopts, sizeof(uint32_t));
509 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
510 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
511 } else if (cg_type == CG_ANALOG) {
512 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
513 model->analog_devopts, model->num_analog_devopts,
516 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
517 NULL, 0, sizeof(uint32_t));
520 case SR_CONF_COUPLING:
521 if (cg_type == CG_NONE)
522 return SR_ERR_CHANNEL_GROUP;
523 *data = g_variant_new_strv(*model->coupling_options,
524 g_strv_length((char **)*model->coupling_options));
526 case SR_CONF_TRIGGER_SOURCE:
529 *data = g_variant_new_strv(*model->trigger_sources,
530 g_strv_length((char **)*model->trigger_sources));
532 case SR_CONF_TRIGGER_SLOPE:
535 *data = g_variant_new_strv(*model->trigger_slopes,
536 g_strv_length((char **)*model->trigger_slopes));
538 case SR_CONF_TIMEBASE:
541 *data = build_tuples(model->timebases, model->num_timebases);
544 if (cg_type == CG_NONE)
545 return SR_ERR_CHANNEL_GROUP;
546 *data = build_tuples(model->vdivs, model->num_vdivs);
555 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
557 char command[MAX_COMMAND_SIZE];
558 struct sr_channel *ch;
559 struct dev_context *devc;
560 const struct scope_config *model;
563 model = devc->model_config;
565 ch = devc->current_channel->data;
568 case SR_CHANNEL_ANALOG:
569 g_snprintf(command, sizeof(command),
570 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
573 case SR_CHANNEL_LOGIC:
574 g_snprintf(command, sizeof(command),
575 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
576 ch->index < 8 ? 1 : 2);
579 sr_err("Invalid channel type.");
583 return sr_scpi_send(sdi->conn, command);
586 static int hmo_check_channels(GSList *channels)
589 struct sr_channel *ch;
590 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
592 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
594 for (l = channels; l; l = l->next) {
597 case SR_CHANNEL_ANALOG:
599 enabled_chan3 = TRUE;
600 else if (ch->index == 3)
601 enabled_chan4 = TRUE;
603 case SR_CHANNEL_LOGIC:
614 if ((enabled_pod1 && enabled_chan3) ||
615 (enabled_pod2 && enabled_chan4))
621 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
625 gboolean *pod_enabled, setup_changed;
626 char command[MAX_COMMAND_SIZE];
627 struct scope_state *state;
628 const struct scope_config *model;
629 struct sr_channel *ch;
630 struct dev_context *devc;
631 struct sr_scpi_dev_inst *scpi;
635 state = devc->model_state;
636 model = devc->model_config;
637 setup_changed = FALSE;
639 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
641 for (l = sdi->channels; l; l = l->next) {
644 case SR_CHANNEL_ANALOG:
645 if (ch->enabled == state->analog_channels[ch->index].state)
647 g_snprintf(command, sizeof(command),
648 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
649 ch->index + 1, ch->enabled);
651 if (sr_scpi_send(scpi, command) != SR_OK)
653 state->analog_channels[ch->index].state = ch->enabled;
654 setup_changed = TRUE;
656 case SR_CHANNEL_LOGIC:
658 * A digital POD needs to be enabled for every group of
662 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
664 if (ch->enabled == state->digital_channels[ch->index])
666 g_snprintf(command, sizeof(command),
667 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
668 ch->index, ch->enabled);
670 if (sr_scpi_send(scpi, command) != SR_OK)
673 state->digital_channels[ch->index] = ch->enabled;
674 setup_changed = TRUE;
681 for (i = 1; i <= model->digital_pods; i++) {
682 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
684 g_snprintf(command, sizeof(command),
685 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
686 i, pod_enabled[i - 1]);
687 if (sr_scpi_send(scpi, command) != SR_OK)
689 state->digital_pods[i - 1] = pod_enabled[i - 1];
690 setup_changed = TRUE;
695 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
701 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
704 gboolean digital_added;
705 struct sr_channel *ch;
706 struct dev_context *devc;
707 struct sr_scpi_dev_inst *scpi;
709 if (sdi->status != SR_ST_ACTIVE)
710 return SR_ERR_DEV_CLOSED;
714 digital_added = FALSE;
716 g_slist_free(devc->enabled_channels);
717 devc->enabled_channels = NULL;
719 for (l = sdi->channels; l; l = l->next) {
723 /* Only add a single digital channel. */
724 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
725 devc->enabled_channels = g_slist_append(
726 devc->enabled_channels, ch);
727 if (ch->type == SR_CHANNEL_LOGIC)
728 digital_added = TRUE;
732 if (!devc->enabled_channels)
735 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
736 sr_err("Invalid channel configuration specified!");
740 if (hmo_setup_channels(sdi) != SR_OK) {
741 sr_err("Failed to setup channel configuration!");
745 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
746 hmo_receive_data, (void *)sdi);
748 std_session_send_df_header(sdi, LOG_PREFIX);
750 devc->current_channel = devc->enabled_channels;
752 return hmo_request_data(sdi);
755 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
757 struct dev_context *devc;
758 struct sr_scpi_dev_inst *scpi;
760 std_session_send_df_end(sdi, LOG_PREFIX);
762 if (sdi->status != SR_ST_ACTIVE)
763 return SR_ERR_DEV_CLOSED;
767 devc->num_frames = 0;
768 g_slist_free(devc->enabled_channels);
769 devc->enabled_channels = NULL;
771 sr_scpi_source_remove(sdi->session, scpi);
776 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
778 .longname = "Hameg HMO",
781 .cleanup = std_cleanup,
783 .dev_list = std_dev_list,
784 .dev_clear = dev_clear,
785 .config_get = config_get,
786 .config_set = config_set,
787 .config_list = config_list,
788 .dev_open = dev_open,
789 .dev_close = dev_close,
790 .dev_acquisition_start = dev_acquisition_start,
791 .dev_acquisition_stop = dev_acquisition_stop,