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);
125 static int dev_clear(const struct sr_dev_driver *di)
127 return std_dev_clear_with_callback(di, clear_helper);
130 static int dev_open(struct sr_dev_inst *sdi)
132 if (sr_scpi_open(sdi->conn) != SR_OK)
135 if (hmo_scope_state_get(sdi) != SR_OK)
141 static int dev_close(struct sr_dev_inst *sdi)
143 return sr_scpi_close(sdi->conn);
146 static int check_channel_group(struct dev_context *devc,
147 const struct sr_channel_group *cg)
150 const struct scope_config *model;
152 model = devc->model_config;
157 for (i = 0; i < model->analog_channels; i++)
158 if (cg == devc->analog_groups[i])
161 for (i = 0; i < model->digital_pods; i++)
162 if (cg == devc->digital_groups[i])
165 sr_err("Invalid channel group specified.");
170 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
171 const struct sr_channel_group *cg)
175 struct dev_context *devc;
176 const struct scope_config *model;
177 struct scope_state *state;
184 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
188 model = devc->model_config;
189 state = devc->model_state;
192 case SR_CONF_NUM_HDIV:
193 *data = g_variant_new_int32(model->num_xdivs);
196 case SR_CONF_TIMEBASE:
197 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
198 (*model->timebases)[state->timebase][1]);
201 case SR_CONF_NUM_VDIV:
202 if (cg_type == CG_NONE) {
203 sr_err("No channel group specified.");
204 return SR_ERR_CHANNEL_GROUP;
205 } else if (cg_type == CG_ANALOG) {
206 for (i = 0; i < model->analog_channels; i++) {
207 if (cg != devc->analog_groups[i])
209 *data = g_variant_new_int32(model->num_ydivs);
219 if (cg_type == CG_NONE) {
220 sr_err("No channel group specified.");
221 return SR_ERR_CHANNEL_GROUP;
222 } else if (cg_type == CG_ANALOG) {
223 for (i = 0; i < model->analog_channels; i++) {
224 if (cg != devc->analog_groups[i])
226 *data = g_variant_new("(tt)",
227 (*model->vdivs)[state->analog_channels[i].vdiv][0],
228 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
237 case SR_CONF_TRIGGER_SOURCE:
238 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
241 case SR_CONF_TRIGGER_SLOPE:
242 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
245 case SR_CONF_HORIZ_TRIGGERPOS:
246 *data = g_variant_new_double(state->horiz_triggerpos);
249 case SR_CONF_COUPLING:
250 if (cg_type == CG_NONE) {
251 sr_err("No channel group specified.");
252 return SR_ERR_CHANNEL_GROUP;
253 } else if (cg_type == CG_ANALOG) {
254 for (i = 0; i < model->analog_channels; i++) {
255 if (cg != devc->analog_groups[i])
257 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
266 case SR_CONF_SAMPLERATE:
267 *data = g_variant_new_uint64(state->sample_rate);
277 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
280 GVariant *rational[2];
283 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
285 for (i = 0; i < n; i++) {
286 rational[0] = g_variant_new_uint64((*array)[i][0]);
287 rational[1] = g_variant_new_uint64((*array)[i][1]);
289 /* FIXME: Valgrind reports a memory leak here. */
290 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
293 return g_variant_builder_end(&gvb);
296 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
297 const struct sr_channel_group *cg)
301 char command[MAX_COMMAND_SIZE], float_str[30];
302 struct dev_context *devc;
303 const struct scope_config *model;
304 struct scope_state *state;
308 gboolean update_sample_rate;
315 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
318 model = devc->model_config;
319 state = devc->model_state;
320 update_sample_rate = FALSE;
325 case SR_CONF_LIMIT_FRAMES:
326 devc->frame_limit = g_variant_get_uint64(data);
329 case SR_CONF_TRIGGER_SOURCE:
330 tmp = g_variant_get_string(data, NULL);
331 for (i = 0; (*model->trigger_sources)[i]; i++) {
332 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
334 state->trigger_source = i;
335 g_snprintf(command, sizeof(command),
336 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
337 (*model->trigger_sources)[i]);
339 ret = sr_scpi_send(sdi->conn, command);
344 if (cg_type == CG_NONE) {
345 sr_err("No channel group specified.");
346 return SR_ERR_CHANNEL_GROUP;
349 g_variant_get(data, "(tt)", &p, &q);
351 for (i = 0; i < model->num_vdivs; i++) {
352 if (p != (*model->vdivs)[i][0] ||
353 q != (*model->vdivs)[i][1])
355 for (j = 1; j <= model->analog_channels; j++) {
356 if (cg != devc->analog_groups[j - 1])
358 state->analog_channels[j - 1].vdiv = i;
359 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
360 g_snprintf(command, sizeof(command),
361 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
364 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
365 sr_scpi_get_opc(sdi->conn) != SR_OK)
375 case SR_CONF_TIMEBASE:
376 g_variant_get(data, "(tt)", &p, &q);
378 for (i = 0; i < model->num_timebases; i++) {
379 if (p != (*model->timebases)[i][0] ||
380 q != (*model->timebases)[i][1])
383 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
384 g_snprintf(command, sizeof(command),
385 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
388 ret = sr_scpi_send(sdi->conn, command);
389 update_sample_rate = TRUE;
393 case SR_CONF_HORIZ_TRIGGERPOS:
394 tmp_d = g_variant_get_double(data);
396 if (tmp_d < 0.0 || tmp_d > 1.0)
399 state->horiz_triggerpos = tmp_d;
400 tmp_d = -(tmp_d - 0.5) *
401 ((double) (*model->timebases)[state->timebase][0] /
402 (*model->timebases)[state->timebase][1])
405 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
406 g_snprintf(command, sizeof(command),
407 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
410 ret = sr_scpi_send(sdi->conn, command);
412 case SR_CONF_TRIGGER_SLOPE:
413 tmp = g_variant_get_string(data, NULL);
414 for (i = 0; (*model->trigger_slopes)[i]; i++) {
415 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
417 state->trigger_slope = i;
418 g_snprintf(command, sizeof(command),
419 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
420 (*model->trigger_slopes)[i]);
422 ret = sr_scpi_send(sdi->conn, command);
426 case SR_CONF_COUPLING:
427 if (cg_type == CG_NONE) {
428 sr_err("No channel group specified.");
429 return SR_ERR_CHANNEL_GROUP;
432 tmp = g_variant_get_string(data, NULL);
434 for (i = 0; (*model->coupling_options)[i]; i++) {
435 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
437 for (j = 1; j <= model->analog_channels; j++) {
438 if (cg != devc->analog_groups[j - 1])
440 state->analog_channels[j-1].coupling = i;
442 g_snprintf(command, sizeof(command),
443 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
446 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
447 sr_scpi_get_opc(sdi->conn) != SR_OK)
462 ret = sr_scpi_get_opc(sdi->conn);
464 if (ret == SR_OK && update_sample_rate)
465 ret = hmo_update_sample_rate(sdi);
470 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
471 const struct sr_channel_group *cg)
473 int cg_type = CG_NONE;
474 struct dev_context *devc = NULL;
475 const struct scope_config *model = NULL;
479 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
482 model = devc->model_config;
486 case SR_CONF_SCAN_OPTIONS:
487 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
488 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
490 case SR_CONF_DEVICE_OPTIONS:
491 if (cg_type == CG_NONE) {
493 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
494 model->devopts, model->num_devopts, sizeof(uint32_t));
496 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
497 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
498 } else if (cg_type == CG_ANALOG) {
499 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
500 model->analog_devopts, model->num_analog_devopts,
503 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
504 NULL, 0, sizeof(uint32_t));
507 case SR_CONF_COUPLING:
508 if (cg_type == CG_NONE)
509 return SR_ERR_CHANNEL_GROUP;
510 *data = g_variant_new_strv(*model->coupling_options,
511 g_strv_length((char **)*model->coupling_options));
513 case SR_CONF_TRIGGER_SOURCE:
516 *data = g_variant_new_strv(*model->trigger_sources,
517 g_strv_length((char **)*model->trigger_sources));
519 case SR_CONF_TRIGGER_SLOPE:
522 *data = g_variant_new_strv(*model->trigger_slopes,
523 g_strv_length((char **)*model->trigger_slopes));
525 case SR_CONF_TIMEBASE:
528 *data = build_tuples(model->timebases, model->num_timebases);
531 if (cg_type == CG_NONE)
532 return SR_ERR_CHANNEL_GROUP;
533 *data = build_tuples(model->vdivs, model->num_vdivs);
542 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
544 char command[MAX_COMMAND_SIZE];
545 struct sr_channel *ch;
546 struct dev_context *devc;
547 const struct scope_config *model;
550 model = devc->model_config;
552 ch = devc->current_channel->data;
555 case SR_CHANNEL_ANALOG:
556 g_snprintf(command, sizeof(command),
557 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
558 #ifdef WORDS_BIGENDIAN
565 case SR_CHANNEL_LOGIC:
566 g_snprintf(command, sizeof(command),
567 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
568 ch->index < 8 ? 1 : 2);
571 sr_err("Invalid channel type.");
575 return sr_scpi_send(sdi->conn, command);
578 static int hmo_check_channels(GSList *channels)
581 struct sr_channel *ch;
582 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
583 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
586 /* Preset "not enabled" for all channels / pods. */
587 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
588 enabled_chan[idx] = FALSE;
589 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
590 enabled_pod[idx] = FALSE;
593 * Determine which channels / pods are required for the caller's
594 * specified configuration.
596 for (l = channels; l; l = l->next) {
599 case SR_CHANNEL_ANALOG:
601 if (idx < ARRAY_SIZE(enabled_chan))
602 enabled_chan[idx] = TRUE;
604 case SR_CHANNEL_LOGIC:
606 if (idx < ARRAY_SIZE(enabled_pod))
607 enabled_pod[idx] = TRUE;
615 * Check for resource conflicts. Some channels can be either
616 * analog or digital, but never both at the same time.
618 * Note that the constraints might depend on the specific model.
619 * These tests might need some adjustment when support for more
620 * models gets added to the driver.
622 if (enabled_pod[0] && enabled_chan[2])
624 if (enabled_pod[1] && enabled_chan[3])
629 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
633 gboolean *pod_enabled, setup_changed;
634 char command[MAX_COMMAND_SIZE];
635 struct scope_state *state;
636 const struct scope_config *model;
637 struct sr_channel *ch;
638 struct dev_context *devc;
639 struct sr_scpi_dev_inst *scpi;
643 state = devc->model_state;
644 model = devc->model_config;
645 setup_changed = FALSE;
647 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
649 for (l = sdi->channels; l; l = l->next) {
652 case SR_CHANNEL_ANALOG:
653 if (ch->enabled == state->analog_channels[ch->index].state)
655 g_snprintf(command, sizeof(command),
656 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
657 ch->index + 1, ch->enabled);
659 if (sr_scpi_send(scpi, command) != SR_OK)
661 state->analog_channels[ch->index].state = ch->enabled;
662 setup_changed = TRUE;
664 case SR_CHANNEL_LOGIC:
666 * A digital POD needs to be enabled for every group of
670 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
672 if (ch->enabled == state->digital_channels[ch->index])
674 g_snprintf(command, sizeof(command),
675 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
676 ch->index, ch->enabled);
678 if (sr_scpi_send(scpi, command) != SR_OK)
681 state->digital_channels[ch->index] = ch->enabled;
682 setup_changed = TRUE;
689 for (i = 1; i <= model->digital_pods; i++) {
690 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
692 g_snprintf(command, sizeof(command),
693 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
694 i, pod_enabled[i - 1]);
695 if (sr_scpi_send(scpi, command) != SR_OK)
697 state->digital_pods[i - 1] = pod_enabled[i - 1];
698 setup_changed = TRUE;
703 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
709 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
712 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
713 size_t group, pod_count;
714 struct sr_channel *ch;
715 struct dev_context *devc;
716 struct sr_scpi_dev_inst *scpi;
722 /* Preset empty results. */
723 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
724 digital_added[group] = FALSE;
725 g_slist_free(devc->enabled_channels);
726 devc->enabled_channels = NULL;
729 * Contruct the list of enabled channels. Determine the highest
730 * number of digital pods involved in the acquisition.
733 for (l = sdi->channels; l; l = l->next) {
737 /* Only add a single digital channel per group (pod). */
738 group = ch->index / 8;
739 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
740 devc->enabled_channels = g_slist_append(
741 devc->enabled_channels, ch);
742 if (ch->type == SR_CHANNEL_LOGIC) {
743 digital_added[group] = TRUE;
744 if (pod_count < group + 1)
745 pod_count = group + 1;
749 if (!devc->enabled_channels)
751 devc->pod_count = pod_count;
752 devc->logic_data = NULL;
755 * Check constraints. Some channels can be either analog or
756 * digital, but not both at the same time.
758 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
759 sr_err("Invalid channel configuration specified!");
765 * Configure the analog and digital channels and the
766 * corresponding digital pods.
768 if (hmo_setup_channels(sdi) != SR_OK) {
769 sr_err("Failed to setup channel configuration!");
775 * Start acquisition on the first enabled channel. The
776 * receive routine will continue driving the acquisition.
778 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
779 hmo_receive_data, (void *)sdi);
781 std_session_send_df_header(sdi);
783 devc->current_channel = devc->enabled_channels;
785 return hmo_request_data(sdi);
788 g_slist_free(devc->enabled_channels);
789 devc->enabled_channels = NULL;
793 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
795 struct dev_context *devc;
796 struct sr_scpi_dev_inst *scpi;
798 std_session_send_df_end(sdi);
802 devc->num_frames = 0;
803 g_slist_free(devc->enabled_channels);
804 devc->enabled_channels = NULL;
806 sr_scpi_source_remove(sdi->session, scpi);
811 static struct sr_dev_driver hameg_hmo_driver_info = {
813 .longname = "Hameg HMO",
816 .cleanup = std_cleanup,
818 .dev_list = std_dev_list,
819 .dev_clear = dev_clear,
820 .config_get = config_get,
821 .config_set = config_set,
822 .config_list = config_list,
823 .dev_open = dev_open,
824 .dev_close = dev_close,
825 .dev_acquisition_start = dev_acquisition_start,
826 .dev_acquisition_stop = dev_acquisition_stop,
829 SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);