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_with_callback(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 return sr_scpi_close(sdi->conn);
148 static int check_channel_group(struct dev_context *devc,
149 const struct sr_channel_group *cg)
152 const struct scope_config *model;
154 model = devc->model_config;
159 for (i = 0; i < model->analog_channels; i++)
160 if (cg == devc->analog_groups[i])
163 for (i = 0; i < model->digital_pods; i++)
164 if (cg == devc->digital_groups[i])
167 sr_err("Invalid channel group specified.");
172 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
173 const struct sr_channel_group *cg)
177 struct dev_context *devc;
178 const struct scope_config *model;
179 struct scope_state *state;
186 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
190 model = devc->model_config;
191 state = devc->model_state;
194 case SR_CONF_NUM_HDIV:
195 *data = g_variant_new_int32(model->num_xdivs);
198 case SR_CONF_TIMEBASE:
199 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
200 (*model->timebases)[state->timebase][1]);
203 case SR_CONF_NUM_VDIV:
204 if (cg_type == CG_NONE) {
205 sr_err("No channel group specified.");
206 return SR_ERR_CHANNEL_GROUP;
207 } else if (cg_type == CG_ANALOG) {
208 for (i = 0; i < model->analog_channels; i++) {
209 if (cg != devc->analog_groups[i])
211 *data = g_variant_new_int32(model->num_ydivs);
221 if (cg_type == CG_NONE) {
222 sr_err("No channel group specified.");
223 return SR_ERR_CHANNEL_GROUP;
224 } else if (cg_type == CG_ANALOG) {
225 for (i = 0; i < model->analog_channels; i++) {
226 if (cg != devc->analog_groups[i])
228 *data = g_variant_new("(tt)",
229 (*model->vdivs)[state->analog_channels[i].vdiv][0],
230 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
239 case SR_CONF_TRIGGER_SOURCE:
240 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
243 case SR_CONF_TRIGGER_SLOPE:
244 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
247 case SR_CONF_HORIZ_TRIGGERPOS:
248 *data = g_variant_new_double(state->horiz_triggerpos);
251 case SR_CONF_COUPLING:
252 if (cg_type == CG_NONE) {
253 sr_err("No channel group specified.");
254 return SR_ERR_CHANNEL_GROUP;
255 } else if (cg_type == CG_ANALOG) {
256 for (i = 0; i < model->analog_channels; i++) {
257 if (cg != devc->analog_groups[i])
259 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
268 case SR_CONF_SAMPLERATE:
269 *data = g_variant_new_uint64(state->sample_rate);
279 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
282 GVariant *rational[2];
285 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
287 for (i = 0; i < n; i++) {
288 rational[0] = g_variant_new_uint64((*array)[i][0]);
289 rational[1] = g_variant_new_uint64((*array)[i][1]);
291 /* FIXME: Valgrind reports a memory leak here. */
292 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
295 return g_variant_builder_end(&gvb);
298 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
299 const struct sr_channel_group *cg)
303 char command[MAX_COMMAND_SIZE], float_str[30];
304 struct dev_context *devc;
305 const struct scope_config *model;
306 struct scope_state *state;
310 gboolean update_sample_rate;
317 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
320 model = devc->model_config;
321 state = devc->model_state;
322 update_sample_rate = FALSE;
327 case SR_CONF_LIMIT_FRAMES:
328 devc->frame_limit = g_variant_get_uint64(data);
331 case SR_CONF_TRIGGER_SOURCE:
332 tmp = g_variant_get_string(data, NULL);
333 for (i = 0; (*model->trigger_sources)[i]; i++) {
334 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
336 state->trigger_source = i;
337 g_snprintf(command, sizeof(command),
338 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
339 (*model->trigger_sources)[i]);
341 ret = sr_scpi_send(sdi->conn, command);
346 if (cg_type == CG_NONE) {
347 sr_err("No channel group specified.");
348 return SR_ERR_CHANNEL_GROUP;
351 g_variant_get(data, "(tt)", &p, &q);
353 for (i = 0; i < model->num_vdivs; i++) {
354 if (p != (*model->vdivs)[i][0] ||
355 q != (*model->vdivs)[i][1])
357 for (j = 1; j <= model->analog_channels; j++) {
358 if (cg != devc->analog_groups[j - 1])
360 state->analog_channels[j - 1].vdiv = i;
361 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
362 g_snprintf(command, sizeof(command),
363 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
366 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
367 sr_scpi_get_opc(sdi->conn) != SR_OK)
377 case SR_CONF_TIMEBASE:
378 g_variant_get(data, "(tt)", &p, &q);
380 for (i = 0; i < model->num_timebases; i++) {
381 if (p != (*model->timebases)[i][0] ||
382 q != (*model->timebases)[i][1])
385 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
386 g_snprintf(command, sizeof(command),
387 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
390 ret = sr_scpi_send(sdi->conn, command);
391 update_sample_rate = TRUE;
395 case SR_CONF_HORIZ_TRIGGERPOS:
396 tmp_d = g_variant_get_double(data);
398 if (tmp_d < 0.0 || tmp_d > 1.0)
401 state->horiz_triggerpos = tmp_d;
402 tmp_d = -(tmp_d - 0.5) *
403 ((double) (*model->timebases)[state->timebase][0] /
404 (*model->timebases)[state->timebase][1])
407 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
408 g_snprintf(command, sizeof(command),
409 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
412 ret = sr_scpi_send(sdi->conn, command);
414 case SR_CONF_TRIGGER_SLOPE:
415 tmp = g_variant_get_string(data, NULL);
416 for (i = 0; (*model->trigger_slopes)[i]; i++) {
417 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
419 state->trigger_slope = i;
420 g_snprintf(command, sizeof(command),
421 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
422 (*model->trigger_slopes)[i]);
424 ret = sr_scpi_send(sdi->conn, command);
428 case SR_CONF_COUPLING:
429 if (cg_type == CG_NONE) {
430 sr_err("No channel group specified.");
431 return SR_ERR_CHANNEL_GROUP;
434 tmp = g_variant_get_string(data, NULL);
436 for (i = 0; (*model->coupling_options)[i]; i++) {
437 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
439 for (j = 1; j <= model->analog_channels; j++) {
440 if (cg != devc->analog_groups[j - 1])
442 state->analog_channels[j-1].coupling = i;
444 g_snprintf(command, sizeof(command),
445 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
448 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
449 sr_scpi_get_opc(sdi->conn) != SR_OK)
464 ret = sr_scpi_get_opc(sdi->conn);
466 if (ret == SR_OK && update_sample_rate)
467 ret = hmo_update_sample_rate(sdi);
472 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
473 const struct sr_channel_group *cg)
475 int cg_type = CG_NONE;
476 struct dev_context *devc = NULL;
477 const struct scope_config *model = NULL;
481 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
484 model = devc->model_config;
488 case SR_CONF_SCAN_OPTIONS:
489 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
490 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
492 case SR_CONF_DEVICE_OPTIONS:
493 if (cg_type == CG_NONE) {
495 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
496 model->devopts, model->num_devopts, sizeof(uint32_t));
498 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
499 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
500 } else if (cg_type == CG_ANALOG) {
501 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
502 model->analog_devopts, model->num_analog_devopts,
505 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
506 NULL, 0, sizeof(uint32_t));
509 case SR_CONF_COUPLING:
510 if (cg_type == CG_NONE)
511 return SR_ERR_CHANNEL_GROUP;
512 *data = g_variant_new_strv(*model->coupling_options,
513 g_strv_length((char **)*model->coupling_options));
515 case SR_CONF_TRIGGER_SOURCE:
518 *data = g_variant_new_strv(*model->trigger_sources,
519 g_strv_length((char **)*model->trigger_sources));
521 case SR_CONF_TRIGGER_SLOPE:
524 *data = g_variant_new_strv(*model->trigger_slopes,
525 g_strv_length((char **)*model->trigger_slopes));
527 case SR_CONF_TIMEBASE:
530 *data = build_tuples(model->timebases, model->num_timebases);
533 if (cg_type == CG_NONE)
534 return SR_ERR_CHANNEL_GROUP;
535 *data = build_tuples(model->vdivs, model->num_vdivs);
544 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
546 char command[MAX_COMMAND_SIZE];
547 struct sr_channel *ch;
548 struct dev_context *devc;
549 const struct scope_config *model;
552 model = devc->model_config;
554 ch = devc->current_channel->data;
557 case SR_CHANNEL_ANALOG:
558 g_snprintf(command, sizeof(command),
559 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
560 #ifdef WORDS_BIGENDIAN
567 case SR_CHANNEL_LOGIC:
568 g_snprintf(command, sizeof(command),
569 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
570 ch->index < 8 ? 1 : 2);
573 sr_err("Invalid channel type.");
577 return sr_scpi_send(sdi->conn, command);
580 static int hmo_check_channels(GSList *channels)
583 struct sr_channel *ch;
584 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
585 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
588 /* Preset "not enabled" for all channels / pods. */
589 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
590 enabled_chan[idx] = FALSE;
591 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
592 enabled_pod[idx] = FALSE;
595 * Determine which channels / pods are required for the caller's
596 * specified configuration.
598 for (l = channels; l; l = l->next) {
601 case SR_CHANNEL_ANALOG:
603 if (idx < ARRAY_SIZE(enabled_chan))
604 enabled_chan[idx] = TRUE;
606 case SR_CHANNEL_LOGIC:
608 if (idx < ARRAY_SIZE(enabled_pod))
609 enabled_pod[idx] = TRUE;
617 * Check for resource conflicts. Some channels can be either
618 * analog or digital, but never both at the same time.
620 * Note that the constraints might depend on the specific model.
621 * These tests might need some adjustment when support for more
622 * models gets added to the driver.
624 if (enabled_pod[0] && enabled_chan[2])
626 if (enabled_pod[1] && enabled_chan[3])
631 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
635 gboolean *pod_enabled, setup_changed;
636 char command[MAX_COMMAND_SIZE];
637 struct scope_state *state;
638 const struct scope_config *model;
639 struct sr_channel *ch;
640 struct dev_context *devc;
641 struct sr_scpi_dev_inst *scpi;
645 state = devc->model_state;
646 model = devc->model_config;
647 setup_changed = FALSE;
649 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
651 for (l = sdi->channels; l; l = l->next) {
654 case SR_CHANNEL_ANALOG:
655 if (ch->enabled == state->analog_channels[ch->index].state)
657 g_snprintf(command, sizeof(command),
658 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
659 ch->index + 1, ch->enabled);
661 if (sr_scpi_send(scpi, command) != SR_OK)
663 state->analog_channels[ch->index].state = ch->enabled;
664 setup_changed = TRUE;
666 case SR_CHANNEL_LOGIC:
668 * A digital POD needs to be enabled for every group of
672 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
674 if (ch->enabled == state->digital_channels[ch->index])
676 g_snprintf(command, sizeof(command),
677 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
678 ch->index, ch->enabled);
680 if (sr_scpi_send(scpi, command) != SR_OK)
683 state->digital_channels[ch->index] = ch->enabled;
684 setup_changed = TRUE;
691 for (i = 1; i <= model->digital_pods; i++) {
692 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
694 g_snprintf(command, sizeof(command),
695 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
696 i, pod_enabled[i - 1]);
697 if (sr_scpi_send(scpi, command) != SR_OK)
699 state->digital_pods[i - 1] = pod_enabled[i - 1];
700 setup_changed = TRUE;
705 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
711 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
714 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
715 size_t group, pod_count;
716 struct sr_channel *ch;
717 struct dev_context *devc;
718 struct sr_scpi_dev_inst *scpi;
724 /* Preset empty results. */
725 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
726 digital_added[group] = FALSE;
727 g_slist_free(devc->enabled_channels);
728 devc->enabled_channels = NULL;
731 * Contruct the list of enabled channels. Determine the highest
732 * number of digital pods involved in the acquisition.
735 for (l = sdi->channels; l; l = l->next) {
739 /* Only add a single digital channel per group (pod). */
740 group = ch->index / 8;
741 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
742 devc->enabled_channels = g_slist_append(
743 devc->enabled_channels, ch);
744 if (ch->type == SR_CHANNEL_LOGIC) {
745 digital_added[group] = TRUE;
746 if (pod_count < group + 1)
747 pod_count = group + 1;
751 if (!devc->enabled_channels)
753 devc->pod_count = pod_count;
754 devc->logic_data = NULL;
757 * Check constraints. Some channels can be either analog or
758 * digital, but not both at the same time.
760 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
761 sr_err("Invalid channel configuration specified!");
767 * Configure the analog and digital channels and the
768 * corresponding digital pods.
770 if (hmo_setup_channels(sdi) != SR_OK) {
771 sr_err("Failed to setup channel configuration!");
777 * Start acquisition on the first enabled channel. The
778 * receive routine will continue driving the acquisition.
780 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
781 hmo_receive_data, (void *)sdi);
783 std_session_send_df_header(sdi);
785 devc->current_channel = devc->enabled_channels;
787 return hmo_request_data(sdi);
790 g_slist_free(devc->enabled_channels);
791 devc->enabled_channels = NULL;
795 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
797 struct dev_context *devc;
798 struct sr_scpi_dev_inst *scpi;
800 std_session_send_df_end(sdi);
804 devc->num_frames = 0;
805 g_slist_free(devc->enabled_channels);
806 devc->enabled_channels = NULL;
808 sr_scpi_source_remove(sdi->session, scpi);
813 static struct sr_dev_driver hameg_hmo_driver_info = {
815 .longname = "Hameg HMO",
818 .cleanup = std_cleanup,
820 .dev_list = std_dev_list,
821 .dev_clear = dev_clear,
822 .config_get = config_get,
823 .config_set = config_set,
824 .config_list = config_list,
825 .dev_open = dev_open,
826 .dev_close = dev_close,
827 .dev_acquisition_start = dev_acquisition_start,
828 .dev_acquisition_stop = dev_acquisition_stop,
831 SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);