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)
143 static int dev_close(struct sr_dev_inst *sdi)
145 sr_scpi_close(sdi->conn);
147 sdi->status = SR_ST_INACTIVE;
152 static int check_channel_group(struct dev_context *devc,
153 const struct sr_channel_group *cg)
156 const struct scope_config *model;
158 model = devc->model_config;
163 for (i = 0; i < model->analog_channels; i++)
164 if (cg == devc->analog_groups[i])
167 for (i = 0; i < model->digital_pods; i++)
168 if (cg == devc->digital_groups[i])
171 sr_err("Invalid channel group specified.");
176 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
177 const struct sr_channel_group *cg)
181 struct dev_context *devc;
182 const struct scope_config *model;
183 struct scope_state *state;
190 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
194 model = devc->model_config;
195 state = devc->model_state;
198 case SR_CONF_NUM_HDIV:
199 *data = g_variant_new_int32(model->num_xdivs);
202 case SR_CONF_TIMEBASE:
203 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
204 (*model->timebases)[state->timebase][1]);
207 case SR_CONF_NUM_VDIV:
208 if (cg_type == CG_NONE) {
209 sr_err("No channel group specified.");
210 return SR_ERR_CHANNEL_GROUP;
211 } else if (cg_type == CG_ANALOG) {
212 for (i = 0; i < model->analog_channels; i++) {
213 if (cg != devc->analog_groups[i])
215 *data = g_variant_new_int32(model->num_ydivs);
225 if (cg_type == CG_NONE) {
226 sr_err("No channel group specified.");
227 return SR_ERR_CHANNEL_GROUP;
228 } else if (cg_type == CG_ANALOG) {
229 for (i = 0; i < model->analog_channels; i++) {
230 if (cg != devc->analog_groups[i])
232 *data = g_variant_new("(tt)",
233 (*model->vdivs)[state->analog_channels[i].vdiv][0],
234 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
243 case SR_CONF_TRIGGER_SOURCE:
244 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
247 case SR_CONF_TRIGGER_SLOPE:
248 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
251 case SR_CONF_HORIZ_TRIGGERPOS:
252 *data = g_variant_new_double(state->horiz_triggerpos);
255 case SR_CONF_COUPLING:
256 if (cg_type == CG_NONE) {
257 sr_err("No channel group specified.");
258 return SR_ERR_CHANNEL_GROUP;
259 } else if (cg_type == CG_ANALOG) {
260 for (i = 0; i < model->analog_channels; i++) {
261 if (cg != devc->analog_groups[i])
263 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
272 case SR_CONF_SAMPLERATE:
273 *data = g_variant_new_uint64(state->sample_rate);
283 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
286 GVariant *rational[2];
289 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
291 for (i = 0; i < n; i++) {
292 rational[0] = g_variant_new_uint64((*array)[i][0]);
293 rational[1] = g_variant_new_uint64((*array)[i][1]);
295 /* FIXME: Valgrind reports a memory leak here. */
296 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
299 return g_variant_builder_end(&gvb);
302 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
303 const struct sr_channel_group *cg)
307 char command[MAX_COMMAND_SIZE], float_str[30];
308 struct dev_context *devc;
309 const struct scope_config *model;
310 struct scope_state *state;
314 gboolean update_sample_rate;
321 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
324 model = devc->model_config;
325 state = devc->model_state;
326 update_sample_rate = FALSE;
331 case SR_CONF_LIMIT_FRAMES:
332 devc->frame_limit = g_variant_get_uint64(data);
335 case SR_CONF_TRIGGER_SOURCE:
336 tmp = g_variant_get_string(data, NULL);
337 for (i = 0; (*model->trigger_sources)[i]; i++) {
338 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
340 state->trigger_source = i;
341 g_snprintf(command, sizeof(command),
342 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
343 (*model->trigger_sources)[i]);
345 ret = sr_scpi_send(sdi->conn, command);
350 if (cg_type == CG_NONE) {
351 sr_err("No channel group specified.");
352 return SR_ERR_CHANNEL_GROUP;
355 g_variant_get(data, "(tt)", &p, &q);
357 for (i = 0; i < model->num_vdivs; i++) {
358 if (p != (*model->vdivs)[i][0] ||
359 q != (*model->vdivs)[i][1])
361 for (j = 1; j <= model->analog_channels; j++) {
362 if (cg != devc->analog_groups[j - 1])
364 state->analog_channels[j - 1].vdiv = i;
365 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
366 g_snprintf(command, sizeof(command),
367 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
370 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
371 sr_scpi_get_opc(sdi->conn) != SR_OK)
381 case SR_CONF_TIMEBASE:
382 g_variant_get(data, "(tt)", &p, &q);
384 for (i = 0; i < model->num_timebases; i++) {
385 if (p != (*model->timebases)[i][0] ||
386 q != (*model->timebases)[i][1])
389 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
390 g_snprintf(command, sizeof(command),
391 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
394 ret = sr_scpi_send(sdi->conn, command);
395 update_sample_rate = TRUE;
399 case SR_CONF_HORIZ_TRIGGERPOS:
400 tmp_d = g_variant_get_double(data);
402 if (tmp_d < 0.0 || tmp_d > 1.0)
405 state->horiz_triggerpos = tmp_d;
406 tmp_d = -(tmp_d - 0.5) *
407 ((double) (*model->timebases)[state->timebase][0] /
408 (*model->timebases)[state->timebase][1])
411 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
412 g_snprintf(command, sizeof(command),
413 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
416 ret = sr_scpi_send(sdi->conn, command);
418 case SR_CONF_TRIGGER_SLOPE:
419 tmp = g_variant_get_string(data, NULL);
420 for (i = 0; (*model->trigger_slopes)[i]; i++) {
421 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
423 state->trigger_slope = i;
424 g_snprintf(command, sizeof(command),
425 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
426 (*model->trigger_slopes)[i]);
428 ret = sr_scpi_send(sdi->conn, command);
432 case SR_CONF_COUPLING:
433 if (cg_type == CG_NONE) {
434 sr_err("No channel group specified.");
435 return SR_ERR_CHANNEL_GROUP;
438 tmp = g_variant_get_string(data, NULL);
440 for (i = 0; (*model->coupling_options)[i]; i++) {
441 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
443 for (j = 1; j <= model->analog_channels; j++) {
444 if (cg != devc->analog_groups[j - 1])
446 state->analog_channels[j-1].coupling = i;
448 g_snprintf(command, sizeof(command),
449 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
452 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
453 sr_scpi_get_opc(sdi->conn) != SR_OK)
468 ret = sr_scpi_get_opc(sdi->conn);
470 if (ret == SR_OK && update_sample_rate)
471 ret = hmo_update_sample_rate(sdi);
476 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
477 const struct sr_channel_group *cg)
479 int cg_type = CG_NONE;
480 struct dev_context *devc = NULL;
481 const struct scope_config *model = NULL;
485 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
488 model = devc->model_config;
492 case SR_CONF_SCAN_OPTIONS:
493 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
494 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
496 case SR_CONF_DEVICE_OPTIONS:
497 if (cg_type == CG_NONE) {
499 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
500 model->devopts, model->num_devopts, sizeof(uint32_t));
502 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
503 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
504 } else if (cg_type == CG_ANALOG) {
505 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
506 model->analog_devopts, model->num_analog_devopts,
509 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
510 NULL, 0, sizeof(uint32_t));
513 case SR_CONF_COUPLING:
514 if (cg_type == CG_NONE)
515 return SR_ERR_CHANNEL_GROUP;
516 *data = g_variant_new_strv(*model->coupling_options,
517 g_strv_length((char **)*model->coupling_options));
519 case SR_CONF_TRIGGER_SOURCE:
522 *data = g_variant_new_strv(*model->trigger_sources,
523 g_strv_length((char **)*model->trigger_sources));
525 case SR_CONF_TRIGGER_SLOPE:
528 *data = g_variant_new_strv(*model->trigger_slopes,
529 g_strv_length((char **)*model->trigger_slopes));
531 case SR_CONF_TIMEBASE:
534 *data = build_tuples(model->timebases, model->num_timebases);
537 if (cg_type == CG_NONE)
538 return SR_ERR_CHANNEL_GROUP;
539 *data = build_tuples(model->vdivs, model->num_vdivs);
548 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
550 char command[MAX_COMMAND_SIZE];
551 struct sr_channel *ch;
552 struct dev_context *devc;
553 const struct scope_config *model;
556 model = devc->model_config;
558 ch = devc->current_channel->data;
561 case SR_CHANNEL_ANALOG:
562 g_snprintf(command, sizeof(command),
563 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
564 #ifdef WORDS_BIGENDIAN
571 case SR_CHANNEL_LOGIC:
572 g_snprintf(command, sizeof(command),
573 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
574 ch->index < 8 ? 1 : 2);
577 sr_err("Invalid channel type.");
581 return sr_scpi_send(sdi->conn, command);
584 static int hmo_check_channels(GSList *channels)
587 struct sr_channel *ch;
588 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
589 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
592 /* Preset "not enabled" for all channels / pods. */
593 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
594 enabled_chan[idx] = FALSE;
595 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
596 enabled_pod[idx] = FALSE;
599 * Determine which channels / pods are required for the caller's
600 * specified configuration.
602 for (l = channels; l; l = l->next) {
605 case SR_CHANNEL_ANALOG:
607 if (idx < ARRAY_SIZE(enabled_chan))
608 enabled_chan[idx] = TRUE;
610 case SR_CHANNEL_LOGIC:
612 if (idx < ARRAY_SIZE(enabled_pod))
613 enabled_pod[idx] = TRUE;
621 * Check for resource conflicts. Some channels can be either
622 * analog or digital, but never both at the same time.
624 * Note that the constraints might depend on the specific model.
625 * These tests might need some adjustment when support for more
626 * models gets added to the driver.
628 if (enabled_pod[0] && enabled_chan[2])
630 if (enabled_pod[1] && enabled_chan[3])
635 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
639 gboolean *pod_enabled, setup_changed;
640 char command[MAX_COMMAND_SIZE];
641 struct scope_state *state;
642 const struct scope_config *model;
643 struct sr_channel *ch;
644 struct dev_context *devc;
645 struct sr_scpi_dev_inst *scpi;
649 state = devc->model_state;
650 model = devc->model_config;
651 setup_changed = FALSE;
653 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
655 for (l = sdi->channels; l; l = l->next) {
658 case SR_CHANNEL_ANALOG:
659 if (ch->enabled == state->analog_channels[ch->index].state)
661 g_snprintf(command, sizeof(command),
662 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
663 ch->index + 1, ch->enabled);
665 if (sr_scpi_send(scpi, command) != SR_OK)
667 state->analog_channels[ch->index].state = ch->enabled;
668 setup_changed = TRUE;
670 case SR_CHANNEL_LOGIC:
672 * A digital POD needs to be enabled for every group of
676 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
678 if (ch->enabled == state->digital_channels[ch->index])
680 g_snprintf(command, sizeof(command),
681 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
682 ch->index, ch->enabled);
684 if (sr_scpi_send(scpi, command) != SR_OK)
687 state->digital_channels[ch->index] = ch->enabled;
688 setup_changed = TRUE;
695 for (i = 1; i <= model->digital_pods; i++) {
696 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
698 g_snprintf(command, sizeof(command),
699 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
700 i, pod_enabled[i - 1]);
701 if (sr_scpi_send(scpi, command) != SR_OK)
703 state->digital_pods[i - 1] = pod_enabled[i - 1];
704 setup_changed = TRUE;
709 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
715 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
718 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
719 size_t group, pod_count;
720 struct sr_channel *ch;
721 struct dev_context *devc;
722 struct sr_scpi_dev_inst *scpi;
728 /* Preset empty results. */
729 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
730 digital_added[group] = FALSE;
731 g_slist_free(devc->enabled_channels);
732 devc->enabled_channels = NULL;
735 * Contruct the list of enabled channels. Determine the highest
736 * number of digital pods involved in the acquisition.
739 for (l = sdi->channels; l; l = l->next) {
743 /* Only add a single digital channel per group (pod). */
744 group = ch->index / 8;
745 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
746 devc->enabled_channels = g_slist_append(
747 devc->enabled_channels, ch);
748 if (ch->type == SR_CHANNEL_LOGIC) {
749 digital_added[group] = TRUE;
750 if (pod_count < group + 1)
751 pod_count = group + 1;
755 if (!devc->enabled_channels)
757 devc->pod_count = pod_count;
758 devc->logic_data = NULL;
761 * Check constraints. Some channels can be either analog or
762 * digital, but not both at the same time.
764 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
765 sr_err("Invalid channel configuration specified!");
771 * Configure the analog and digital channels and the
772 * corresponding digital pods.
774 if (hmo_setup_channels(sdi) != SR_OK) {
775 sr_err("Failed to setup channel configuration!");
781 * Start acquisition on the first enabled channel. The
782 * receive routine will continue driving the acquisition.
784 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
785 hmo_receive_data, (void *)sdi);
787 std_session_send_df_header(sdi);
789 devc->current_channel = devc->enabled_channels;
791 return hmo_request_data(sdi);
794 g_slist_free(devc->enabled_channels);
795 devc->enabled_channels = NULL;
799 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
801 struct dev_context *devc;
802 struct sr_scpi_dev_inst *scpi;
804 std_session_send_df_end(sdi);
808 devc->num_frames = 0;
809 g_slist_free(devc->enabled_channels);
810 devc->enabled_channels = NULL;
812 sr_scpi_source_remove(sdi->session, scpi);
817 static struct sr_dev_driver hameg_hmo_driver_info = {
819 .longname = "Hameg HMO",
822 .cleanup = std_cleanup,
824 .dev_list = std_dev_list,
825 .dev_clear = dev_clear,
826 .config_get = config_get,
827 .config_set = config_set,
828 .config_list = config_list,
829 .dev_open = dev_open,
830 .dev_close = dev_close,
831 .dev_acquisition_start = dev_acquisition_start,
832 .dev_acquisition_stop = dev_acquisition_stop,
835 SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);