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 static 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);
103 sr_dev_inst_free(sdi);
109 static GSList *scan(struct sr_dev_driver *di, GSList *options)
111 return sr_scpi_scan(di->context, options, hmo_probe_serial_device);
114 static void clear_helper(void *priv)
116 struct dev_context *devc;
120 hmo_scope_state_free(devc->model_state);
122 g_free(devc->analog_groups);
123 g_free(devc->digital_groups);
128 static int dev_clear(const struct sr_dev_driver *di)
130 return std_dev_clear(di, clear_helper);
133 static int dev_open(struct sr_dev_inst *sdi)
135 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
138 if (hmo_scope_state_get(sdi) != SR_OK)
141 sdi->status = SR_ST_ACTIVE;
146 static int dev_close(struct sr_dev_inst *sdi)
148 if (sdi->status == SR_ST_INACTIVE)
151 sr_scpi_close(sdi->conn);
153 sdi->status = SR_ST_INACTIVE;
158 static int check_channel_group(struct dev_context *devc,
159 const struct sr_channel_group *cg)
162 const struct scope_config *model;
164 model = devc->model_config;
169 for (i = 0; i < model->analog_channels; i++)
170 if (cg == devc->analog_groups[i])
173 for (i = 0; i < model->digital_pods; i++)
174 if (cg == devc->digital_groups[i])
177 sr_err("Invalid channel group specified.");
182 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
183 const struct sr_channel_group *cg)
187 struct dev_context *devc;
188 const struct scope_config *model;
189 struct scope_state *state;
196 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
200 model = devc->model_config;
201 state = devc->model_state;
204 case SR_CONF_NUM_HDIV:
205 *data = g_variant_new_int32(model->num_xdivs);
208 case SR_CONF_TIMEBASE:
209 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
210 (*model->timebases)[state->timebase][1]);
213 case SR_CONF_NUM_VDIV:
214 if (cg_type == CG_NONE) {
215 sr_err("No channel group specified.");
216 return SR_ERR_CHANNEL_GROUP;
217 } else if (cg_type == CG_ANALOG) {
218 for (i = 0; i < model->analog_channels; i++) {
219 if (cg != devc->analog_groups[i])
221 *data = g_variant_new_int32(model->num_ydivs);
231 if (cg_type == CG_NONE) {
232 sr_err("No channel group specified.");
233 return SR_ERR_CHANNEL_GROUP;
234 } else if (cg_type == CG_ANALOG) {
235 for (i = 0; i < model->analog_channels; i++) {
236 if (cg != devc->analog_groups[i])
238 *data = g_variant_new("(tt)",
239 (*model->vdivs)[state->analog_channels[i].vdiv][0],
240 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
249 case SR_CONF_TRIGGER_SOURCE:
250 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
253 case SR_CONF_TRIGGER_SLOPE:
254 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
257 case SR_CONF_HORIZ_TRIGGERPOS:
258 *data = g_variant_new_double(state->horiz_triggerpos);
261 case SR_CONF_COUPLING:
262 if (cg_type == CG_NONE) {
263 sr_err("No channel group specified.");
264 return SR_ERR_CHANNEL_GROUP;
265 } else if (cg_type == CG_ANALOG) {
266 for (i = 0; i < model->analog_channels; i++) {
267 if (cg != devc->analog_groups[i])
269 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
278 case SR_CONF_SAMPLERATE:
279 *data = g_variant_new_uint64(state->sample_rate);
289 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
292 GVariant *rational[2];
295 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
297 for (i = 0; i < n; i++) {
298 rational[0] = g_variant_new_uint64((*array)[i][0]);
299 rational[1] = g_variant_new_uint64((*array)[i][1]);
301 /* FIXME: Valgrind reports a memory leak here. */
302 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
305 return g_variant_builder_end(&gvb);
308 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
309 const struct sr_channel_group *cg)
313 char command[MAX_COMMAND_SIZE], float_str[30];
314 struct dev_context *devc;
315 const struct scope_config *model;
316 struct scope_state *state;
320 gboolean update_sample_rate;
327 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
330 model = devc->model_config;
331 state = devc->model_state;
332 update_sample_rate = FALSE;
337 case SR_CONF_LIMIT_FRAMES:
338 devc->frame_limit = g_variant_get_uint64(data);
341 case SR_CONF_TRIGGER_SOURCE:
342 tmp = g_variant_get_string(data, NULL);
343 for (i = 0; (*model->trigger_sources)[i]; i++) {
344 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
346 state->trigger_source = i;
347 g_snprintf(command, sizeof(command),
348 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
349 (*model->trigger_sources)[i]);
351 ret = sr_scpi_send(sdi->conn, command);
356 if (cg_type == CG_NONE) {
357 sr_err("No channel group specified.");
358 return SR_ERR_CHANNEL_GROUP;
361 g_variant_get(data, "(tt)", &p, &q);
363 for (i = 0; i < model->num_vdivs; i++) {
364 if (p != (*model->vdivs)[i][0] ||
365 q != (*model->vdivs)[i][1])
367 for (j = 1; j <= model->analog_channels; j++) {
368 if (cg != devc->analog_groups[j - 1])
370 state->analog_channels[j - 1].vdiv = i;
371 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
372 g_snprintf(command, sizeof(command),
373 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
376 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
377 sr_scpi_get_opc(sdi->conn) != SR_OK)
387 case SR_CONF_TIMEBASE:
388 g_variant_get(data, "(tt)", &p, &q);
390 for (i = 0; i < model->num_timebases; i++) {
391 if (p != (*model->timebases)[i][0] ||
392 q != (*model->timebases)[i][1])
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_TIMEBASE],
400 ret = sr_scpi_send(sdi->conn, command);
401 update_sample_rate = TRUE;
405 case SR_CONF_HORIZ_TRIGGERPOS:
406 tmp_d = g_variant_get_double(data);
408 if (tmp_d < 0.0 || tmp_d > 1.0)
411 state->horiz_triggerpos = tmp_d;
412 tmp_d = -(tmp_d - 0.5) *
413 ((double) (*model->timebases)[state->timebase][0] /
414 (*model->timebases)[state->timebase][1])
417 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
418 g_snprintf(command, sizeof(command),
419 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
422 ret = sr_scpi_send(sdi->conn, command);
424 case SR_CONF_TRIGGER_SLOPE:
425 tmp = g_variant_get_string(data, NULL);
426 for (i = 0; (*model->trigger_slopes)[i]; i++) {
427 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
429 state->trigger_slope = i;
430 g_snprintf(command, sizeof(command),
431 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
432 (*model->trigger_slopes)[i]);
434 ret = sr_scpi_send(sdi->conn, command);
438 case SR_CONF_COUPLING:
439 if (cg_type == CG_NONE) {
440 sr_err("No channel group specified.");
441 return SR_ERR_CHANNEL_GROUP;
444 tmp = g_variant_get_string(data, NULL);
446 for (i = 0; (*model->coupling_options)[i]; i++) {
447 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
449 for (j = 1; j <= model->analog_channels; j++) {
450 if (cg != devc->analog_groups[j - 1])
452 state->analog_channels[j-1].coupling = i;
454 g_snprintf(command, sizeof(command),
455 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
458 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
459 sr_scpi_get_opc(sdi->conn) != SR_OK)
474 ret = sr_scpi_get_opc(sdi->conn);
476 if (ret == SR_OK && update_sample_rate)
477 ret = hmo_update_sample_rate(sdi);
482 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
483 const struct sr_channel_group *cg)
485 int cg_type = CG_NONE;
486 struct dev_context *devc = NULL;
487 const struct scope_config *model = NULL;
491 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
494 model = devc->model_config;
498 case SR_CONF_SCAN_OPTIONS:
499 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
500 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
502 case SR_CONF_DEVICE_OPTIONS:
503 if (cg_type == CG_NONE) {
505 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
506 model->devopts, model->num_devopts, sizeof(uint32_t));
508 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
509 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
510 } else if (cg_type == CG_ANALOG) {
511 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
512 model->analog_devopts, model->num_analog_devopts,
515 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
516 NULL, 0, sizeof(uint32_t));
519 case SR_CONF_COUPLING:
520 if (cg_type == CG_NONE)
521 return SR_ERR_CHANNEL_GROUP;
522 *data = g_variant_new_strv(*model->coupling_options,
523 g_strv_length((char **)*model->coupling_options));
525 case SR_CONF_TRIGGER_SOURCE:
528 *data = g_variant_new_strv(*model->trigger_sources,
529 g_strv_length((char **)*model->trigger_sources));
531 case SR_CONF_TRIGGER_SLOPE:
534 *data = g_variant_new_strv(*model->trigger_slopes,
535 g_strv_length((char **)*model->trigger_slopes));
537 case SR_CONF_TIMEBASE:
540 *data = build_tuples(model->timebases, model->num_timebases);
543 if (cg_type == CG_NONE)
544 return SR_ERR_CHANNEL_GROUP;
545 *data = build_tuples(model->vdivs, model->num_vdivs);
554 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
556 char command[MAX_COMMAND_SIZE];
557 struct sr_channel *ch;
558 struct dev_context *devc;
559 const struct scope_config *model;
562 model = devc->model_config;
564 ch = devc->current_channel->data;
567 case SR_CHANNEL_ANALOG:
568 g_snprintf(command, sizeof(command),
569 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
570 #ifdef WORDS_BIGENDIAN
577 case SR_CHANNEL_LOGIC:
578 g_snprintf(command, sizeof(command),
579 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
580 ch->index < 8 ? 1 : 2);
583 sr_err("Invalid channel type.");
587 return sr_scpi_send(sdi->conn, command);
590 static int hmo_check_channels(GSList *channels)
593 struct sr_channel *ch;
594 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
595 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
598 /* Preset "not enabled" for all channels / pods. */
599 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
600 enabled_chan[idx] = FALSE;
601 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
602 enabled_pod[idx] = FALSE;
605 * Determine which channels / pods are required for the caller's
606 * specified configuration.
608 for (l = channels; l; l = l->next) {
611 case SR_CHANNEL_ANALOG:
613 if (idx < ARRAY_SIZE(enabled_chan))
614 enabled_chan[idx] = TRUE;
616 case SR_CHANNEL_LOGIC:
618 if (idx < ARRAY_SIZE(enabled_pod))
619 enabled_pod[idx] = TRUE;
627 * Check for resource conflicts. Some channels can be either
628 * analog or digital, but never both at the same time.
630 * Note that the constraints might depend on the specific model.
631 * These tests might need some adjustment when support for more
632 * models gets added to the driver.
634 if (enabled_pod[0] && enabled_chan[2])
636 if (enabled_pod[1] && enabled_chan[3])
641 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
645 gboolean *pod_enabled, setup_changed;
646 char command[MAX_COMMAND_SIZE];
647 struct scope_state *state;
648 const struct scope_config *model;
649 struct sr_channel *ch;
650 struct dev_context *devc;
651 struct sr_scpi_dev_inst *scpi;
655 state = devc->model_state;
656 model = devc->model_config;
657 setup_changed = FALSE;
659 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
661 for (l = sdi->channels; l; l = l->next) {
664 case SR_CHANNEL_ANALOG:
665 if (ch->enabled == state->analog_channels[ch->index].state)
667 g_snprintf(command, sizeof(command),
668 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
669 ch->index + 1, ch->enabled);
671 if (sr_scpi_send(scpi, command) != SR_OK)
673 state->analog_channels[ch->index].state = ch->enabled;
674 setup_changed = TRUE;
676 case SR_CHANNEL_LOGIC:
678 * A digital POD needs to be enabled for every group of
682 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
684 if (ch->enabled == state->digital_channels[ch->index])
686 g_snprintf(command, sizeof(command),
687 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
688 ch->index, ch->enabled);
690 if (sr_scpi_send(scpi, command) != SR_OK)
693 state->digital_channels[ch->index] = ch->enabled;
694 setup_changed = TRUE;
701 for (i = 1; i <= model->digital_pods; i++) {
702 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
704 g_snprintf(command, sizeof(command),
705 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
706 i, pod_enabled[i - 1]);
707 if (sr_scpi_send(scpi, command) != SR_OK)
709 state->digital_pods[i - 1] = pod_enabled[i - 1];
710 setup_changed = TRUE;
715 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
721 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
724 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
725 size_t group, pod_count;
726 struct sr_channel *ch;
727 struct dev_context *devc;
728 struct sr_scpi_dev_inst *scpi;
731 if (sdi->status != SR_ST_ACTIVE)
732 return SR_ERR_DEV_CLOSED;
737 /* Preset empty results. */
738 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
739 digital_added[group] = FALSE;
740 g_slist_free(devc->enabled_channels);
741 devc->enabled_channels = NULL;
744 * Contruct the list of enabled channels. Determine the highest
745 * number of digital pods involved in the acquisition.
748 for (l = sdi->channels; l; l = l->next) {
752 /* Only add a single digital channel per group (pod). */
753 group = ch->index / 8;
754 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
755 devc->enabled_channels = g_slist_append(
756 devc->enabled_channels, ch);
757 if (ch->type == SR_CHANNEL_LOGIC) {
758 digital_added[group] = TRUE;
759 if (pod_count < group + 1)
760 pod_count = group + 1;
764 if (!devc->enabled_channels)
766 devc->pod_count = pod_count;
767 devc->logic_data = NULL;
770 * Check constraints. Some channels can be either analog or
771 * digital, but not both at the same time.
773 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
774 sr_err("Invalid channel configuration specified!");
780 * Configure the analog and digital channels and the
781 * corresponding digital pods.
783 if (hmo_setup_channels(sdi) != SR_OK) {
784 sr_err("Failed to setup channel configuration!");
790 * Start acquisition on the first enabled channel. The
791 * receive routine will continue driving the acquisition.
793 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
794 hmo_receive_data, (void *)sdi);
796 std_session_send_df_header(sdi);
798 devc->current_channel = devc->enabled_channels;
800 return hmo_request_data(sdi);
803 g_slist_free(devc->enabled_channels);
804 devc->enabled_channels = NULL;
808 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
810 struct dev_context *devc;
811 struct sr_scpi_dev_inst *scpi;
813 std_session_send_df_end(sdi);
815 if (sdi->status != SR_ST_ACTIVE)
816 return SR_ERR_DEV_CLOSED;
820 devc->num_frames = 0;
821 g_slist_free(devc->enabled_channels);
822 devc->enabled_channels = NULL;
824 sr_scpi_source_remove(sdi->session, scpi);
829 static struct sr_dev_driver hameg_hmo_driver_info = {
831 .longname = "Hameg HMO",
834 .cleanup = std_cleanup,
836 .dev_list = std_dev_list,
837 .dev_clear = dev_clear,
838 .config_get = config_get,
839 .config_set = config_set,
840 .config_list = config_list,
841 .dev_open = dev_open,
842 .dev_close = dev_close,
843 .dev_acquisition_start = dev_acquisition_start,
844 .dev_acquisition_stop = dev_acquisition_stop,
847 SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);