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)
101 sr_scpi_hw_info_free(hw_info);
102 sr_dev_inst_free(sdi);
108 static GSList *scan(struct sr_dev_driver *di, GSList *options)
110 return sr_scpi_scan(di->context, options, hmo_probe_serial_device);
113 static void clear_helper(void *priv)
115 struct dev_context *devc;
119 hmo_scope_state_free(devc->model_state);
121 g_free(devc->analog_groups);
122 g_free(devc->digital_groups);
127 static int dev_clear(const struct sr_dev_driver *di)
129 return std_dev_clear(di, clear_helper);
132 static int dev_open(struct sr_dev_inst *sdi)
134 if (sr_scpi_open(sdi->conn) != SR_OK)
137 if (hmo_scope_state_get(sdi) != SR_OK)
140 sdi->status = SR_ST_ACTIVE;
145 static int dev_close(struct sr_dev_inst *sdi)
147 sr_scpi_close(sdi->conn);
149 sdi->status = SR_ST_INACTIVE;
154 static int check_channel_group(struct dev_context *devc,
155 const struct sr_channel_group *cg)
158 const struct scope_config *model;
160 model = devc->model_config;
165 for (i = 0; i < model->analog_channels; i++)
166 if (cg == devc->analog_groups[i])
169 for (i = 0; i < model->digital_pods; i++)
170 if (cg == devc->digital_groups[i])
173 sr_err("Invalid channel group specified.");
178 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
179 const struct sr_channel_group *cg)
183 struct dev_context *devc;
184 const struct scope_config *model;
185 struct scope_state *state;
192 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
196 model = devc->model_config;
197 state = devc->model_state;
200 case SR_CONF_NUM_HDIV:
201 *data = g_variant_new_int32(model->num_xdivs);
204 case SR_CONF_TIMEBASE:
205 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
206 (*model->timebases)[state->timebase][1]);
209 case SR_CONF_NUM_VDIV:
210 if (cg_type == CG_NONE) {
211 sr_err("No channel group specified.");
212 return SR_ERR_CHANNEL_GROUP;
213 } else if (cg_type == CG_ANALOG) {
214 for (i = 0; i < model->analog_channels; i++) {
215 if (cg != devc->analog_groups[i])
217 *data = g_variant_new_int32(model->num_ydivs);
227 if (cg_type == CG_NONE) {
228 sr_err("No channel group specified.");
229 return SR_ERR_CHANNEL_GROUP;
230 } else if (cg_type == CG_ANALOG) {
231 for (i = 0; i < model->analog_channels; i++) {
232 if (cg != devc->analog_groups[i])
234 *data = g_variant_new("(tt)",
235 (*model->vdivs)[state->analog_channels[i].vdiv][0],
236 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
245 case SR_CONF_TRIGGER_SOURCE:
246 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
249 case SR_CONF_TRIGGER_SLOPE:
250 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
253 case SR_CONF_HORIZ_TRIGGERPOS:
254 *data = g_variant_new_double(state->horiz_triggerpos);
257 case SR_CONF_COUPLING:
258 if (cg_type == CG_NONE) {
259 sr_err("No channel group specified.");
260 return SR_ERR_CHANNEL_GROUP;
261 } else if (cg_type == CG_ANALOG) {
262 for (i = 0; i < model->analog_channels; i++) {
263 if (cg != devc->analog_groups[i])
265 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
274 case SR_CONF_SAMPLERATE:
275 *data = g_variant_new_uint64(state->sample_rate);
285 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
288 GVariant *rational[2];
291 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
293 for (i = 0; i < n; i++) {
294 rational[0] = g_variant_new_uint64((*array)[i][0]);
295 rational[1] = g_variant_new_uint64((*array)[i][1]);
297 /* FIXME: Valgrind reports a memory leak here. */
298 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
301 return g_variant_builder_end(&gvb);
304 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
305 const struct sr_channel_group *cg)
309 char command[MAX_COMMAND_SIZE], float_str[30];
310 struct dev_context *devc;
311 const struct scope_config *model;
312 struct scope_state *state;
316 gboolean update_sample_rate;
323 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
326 model = devc->model_config;
327 state = devc->model_state;
328 update_sample_rate = FALSE;
333 case SR_CONF_LIMIT_FRAMES:
334 devc->frame_limit = g_variant_get_uint64(data);
337 case SR_CONF_TRIGGER_SOURCE:
338 tmp = g_variant_get_string(data, NULL);
339 for (i = 0; (*model->trigger_sources)[i]; i++) {
340 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
342 state->trigger_source = i;
343 g_snprintf(command, sizeof(command),
344 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
345 (*model->trigger_sources)[i]);
347 ret = sr_scpi_send(sdi->conn, command);
352 if (cg_type == CG_NONE) {
353 sr_err("No channel group specified.");
354 return SR_ERR_CHANNEL_GROUP;
357 g_variant_get(data, "(tt)", &p, &q);
359 for (i = 0; i < model->num_vdivs; i++) {
360 if (p != (*model->vdivs)[i][0] ||
361 q != (*model->vdivs)[i][1])
363 for (j = 1; j <= model->analog_channels; j++) {
364 if (cg != devc->analog_groups[j - 1])
366 state->analog_channels[j - 1].vdiv = i;
367 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
368 g_snprintf(command, sizeof(command),
369 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
372 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
373 sr_scpi_get_opc(sdi->conn) != SR_OK)
383 case SR_CONF_TIMEBASE:
384 g_variant_get(data, "(tt)", &p, &q);
386 for (i = 0; i < model->num_timebases; i++) {
387 if (p != (*model->timebases)[i][0] ||
388 q != (*model->timebases)[i][1])
391 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
392 g_snprintf(command, sizeof(command),
393 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
396 ret = sr_scpi_send(sdi->conn, command);
397 update_sample_rate = TRUE;
401 case SR_CONF_HORIZ_TRIGGERPOS:
402 tmp_d = g_variant_get_double(data);
404 if (tmp_d < 0.0 || tmp_d > 1.0)
407 state->horiz_triggerpos = tmp_d;
408 tmp_d = -(tmp_d - 0.5) *
409 ((double) (*model->timebases)[state->timebase][0] /
410 (*model->timebases)[state->timebase][1])
413 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
414 g_snprintf(command, sizeof(command),
415 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
418 ret = sr_scpi_send(sdi->conn, command);
420 case SR_CONF_TRIGGER_SLOPE:
421 tmp = g_variant_get_string(data, NULL);
422 for (i = 0; (*model->trigger_slopes)[i]; i++) {
423 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
425 state->trigger_slope = i;
426 g_snprintf(command, sizeof(command),
427 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
428 (*model->trigger_slopes)[i]);
430 ret = sr_scpi_send(sdi->conn, command);
434 case SR_CONF_COUPLING:
435 if (cg_type == CG_NONE) {
436 sr_err("No channel group specified.");
437 return SR_ERR_CHANNEL_GROUP;
440 tmp = g_variant_get_string(data, NULL);
442 for (i = 0; (*model->coupling_options)[i]; i++) {
443 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
445 for (j = 1; j <= model->analog_channels; j++) {
446 if (cg != devc->analog_groups[j - 1])
448 state->analog_channels[j-1].coupling = i;
450 g_snprintf(command, sizeof(command),
451 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
454 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
455 sr_scpi_get_opc(sdi->conn) != SR_OK)
470 ret = sr_scpi_get_opc(sdi->conn);
472 if (ret == SR_OK && update_sample_rate)
473 ret = hmo_update_sample_rate(sdi);
478 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
479 const struct sr_channel_group *cg)
481 int cg_type = CG_NONE;
482 struct dev_context *devc = NULL;
483 const struct scope_config *model = NULL;
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],
566 #ifdef WORDS_BIGENDIAN
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_chan[MAX_ANALOG_CHANNEL_COUNT];
591 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
594 /* Preset "not enabled" for all channels / pods. */
595 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
596 enabled_chan[idx] = FALSE;
597 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
598 enabled_pod[idx] = FALSE;
601 * Determine which channels / pods are required for the caller's
602 * specified configuration.
604 for (l = channels; l; l = l->next) {
607 case SR_CHANNEL_ANALOG:
609 if (idx < ARRAY_SIZE(enabled_chan))
610 enabled_chan[idx] = TRUE;
612 case SR_CHANNEL_LOGIC:
614 if (idx < ARRAY_SIZE(enabled_pod))
615 enabled_pod[idx] = TRUE;
623 * Check for resource conflicts. Some channels can be either
624 * analog or digital, but never both at the same time.
626 * Note that the constraints might depend on the specific model.
627 * These tests might need some adjustment when support for more
628 * models gets added to the driver.
630 if (enabled_pod[0] && enabled_chan[2])
632 if (enabled_pod[1] && enabled_chan[3])
637 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
641 gboolean *pod_enabled, setup_changed;
642 char command[MAX_COMMAND_SIZE];
643 struct scope_state *state;
644 const struct scope_config *model;
645 struct sr_channel *ch;
646 struct dev_context *devc;
647 struct sr_scpi_dev_inst *scpi;
651 state = devc->model_state;
652 model = devc->model_config;
653 setup_changed = FALSE;
655 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
657 for (l = sdi->channels; l; l = l->next) {
660 case SR_CHANNEL_ANALOG:
661 if (ch->enabled == state->analog_channels[ch->index].state)
663 g_snprintf(command, sizeof(command),
664 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
665 ch->index + 1, ch->enabled);
667 if (sr_scpi_send(scpi, command) != SR_OK)
669 state->analog_channels[ch->index].state = ch->enabled;
670 setup_changed = TRUE;
672 case SR_CHANNEL_LOGIC:
674 * A digital POD needs to be enabled for every group of
678 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
680 if (ch->enabled == state->digital_channels[ch->index])
682 g_snprintf(command, sizeof(command),
683 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
684 ch->index, ch->enabled);
686 if (sr_scpi_send(scpi, command) != SR_OK)
689 state->digital_channels[ch->index] = ch->enabled;
690 setup_changed = TRUE;
697 for (i = 1; i <= model->digital_pods; i++) {
698 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
700 g_snprintf(command, sizeof(command),
701 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
702 i, pod_enabled[i - 1]);
703 if (sr_scpi_send(scpi, command) != SR_OK)
705 state->digital_pods[i - 1] = pod_enabled[i - 1];
706 setup_changed = TRUE;
711 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
717 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
720 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
721 size_t group, pod_count;
722 struct sr_channel *ch;
723 struct dev_context *devc;
724 struct sr_scpi_dev_inst *scpi;
730 /* Preset empty results. */
731 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
732 digital_added[group] = FALSE;
733 g_slist_free(devc->enabled_channels);
734 devc->enabled_channels = NULL;
737 * Contruct the list of enabled channels. Determine the highest
738 * number of digital pods involved in the acquisition.
741 for (l = sdi->channels; l; l = l->next) {
745 /* Only add a single digital channel per group (pod). */
746 group = ch->index / 8;
747 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
748 devc->enabled_channels = g_slist_append(
749 devc->enabled_channels, ch);
750 if (ch->type == SR_CHANNEL_LOGIC) {
751 digital_added[group] = TRUE;
752 if (pod_count < group + 1)
753 pod_count = group + 1;
757 if (!devc->enabled_channels)
759 devc->pod_count = pod_count;
760 devc->logic_data = NULL;
763 * Check constraints. Some channels can be either analog or
764 * digital, but not both at the same time.
766 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
767 sr_err("Invalid channel configuration specified!");
773 * Configure the analog and digital channels and the
774 * corresponding digital pods.
776 if (hmo_setup_channels(sdi) != SR_OK) {
777 sr_err("Failed to setup channel configuration!");
783 * Start acquisition on the first enabled channel. The
784 * receive routine will continue driving the acquisition.
786 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
787 hmo_receive_data, (void *)sdi);
789 std_session_send_df_header(sdi);
791 devc->current_channel = devc->enabled_channels;
793 return hmo_request_data(sdi);
796 g_slist_free(devc->enabled_channels);
797 devc->enabled_channels = NULL;
801 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
803 struct dev_context *devc;
804 struct sr_scpi_dev_inst *scpi;
806 std_session_send_df_end(sdi);
810 devc->num_frames = 0;
811 g_slist_free(devc->enabled_channels);
812 devc->enabled_channels = NULL;
814 sr_scpi_source_remove(sdi->session, scpi);
819 static struct sr_dev_driver hameg_hmo_driver_info = {
821 .longname = "Hameg HMO",
824 .cleanup = std_cleanup,
826 .dev_list = std_dev_list,
827 .dev_clear = dev_clear,
828 .config_get = config_get,
829 .config_set = config_set,
830 .config_list = config_list,
831 .dev_open = dev_open,
832 .dev_close = dev_close,
833 .dev_acquisition_start = dev_acquisition_start,
834 .dev_acquisition_stop = dev_acquisition_stop,
837 SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);