2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
5 * Copyright (C) 2018 Guido Trentalancia <guido@trentalancia.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 static struct sr_dev_driver hameg_hmo_driver_info;
28 static const char *manufacturers[] = {
33 static const uint32_t scanopts[] = {
38 static const uint32_t drvopts[] = {
40 SR_CONF_LOGIC_ANALYZER,
50 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
52 struct sr_dev_inst *sdi;
53 struct dev_context *devc;
54 struct sr_scpi_hw_info *hw_info;
60 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
61 sr_info("Couldn't get IDN response.");
65 if (std_str_idx_s(hw_info->manufacturer, ARRAY_AND_SIZE(manufacturers)) < 0)
68 sdi = g_malloc0(sizeof(struct sr_dev_inst));
69 sdi->vendor = g_strdup(hw_info->manufacturer);
70 sdi->model = g_strdup(hw_info->model);
71 sdi->version = g_strdup(hw_info->firmware_version);
72 sdi->serial_num = g_strdup(hw_info->serial_number);
73 sdi->driver = &hameg_hmo_driver_info;
74 sdi->inst_type = SR_INST_SCPI;
77 sr_scpi_hw_info_free(hw_info);
80 devc = g_malloc0(sizeof(struct dev_context));
84 if (hmo_init_device(sdi) != SR_OK)
90 sr_scpi_hw_info_free(hw_info);
91 sr_dev_inst_free(sdi);
97 static GSList *scan(struct sr_dev_driver *di, GSList *options)
99 return sr_scpi_scan(di->context, options, probe_device);
102 static void clear_helper(struct dev_context *devc)
104 hmo_scope_state_free(devc->model_state);
105 g_free(devc->analog_groups);
106 g_free(devc->digital_groups);
109 static int dev_clear(const struct sr_dev_driver *di)
111 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
114 static int dev_open(struct sr_dev_inst *sdi)
116 if (sr_scpi_open(sdi->conn) != SR_OK)
119 if (hmo_scope_state_get(sdi) != SR_OK)
125 static int dev_close(struct sr_dev_inst *sdi)
127 return sr_scpi_close(sdi->conn);
130 static int check_channel_group(struct dev_context *devc,
131 const struct sr_channel_group *cg)
133 const struct scope_config *model;
135 model = devc->model_config;
140 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) >= 0)
143 if (std_cg_idx(cg, devc->digital_groups, model->digital_pods) >= 0)
146 sr_err("Invalid channel group specified.");
151 static int config_get(uint32_t key, GVariant **data,
152 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
155 struct dev_context *devc;
156 const struct scope_config *model;
157 struct scope_state *state;
164 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
167 model = devc->model_config;
168 state = devc->model_state;
171 case SR_CONF_NUM_HDIV:
172 *data = g_variant_new_int32(model->num_xdivs);
174 case SR_CONF_TIMEBASE:
175 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
176 (*model->timebases)[state->timebase][1]);
178 case SR_CONF_NUM_VDIV:
180 return SR_ERR_CHANNEL_GROUP;
181 if (cg_type != CG_ANALOG)
183 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) < 0)
185 *data = g_variant_new_int32(model->num_ydivs);
189 return SR_ERR_CHANNEL_GROUP;
190 if (cg_type != CG_ANALOG)
192 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
194 *data = g_variant_new("(tt)",
195 (*model->vdivs)[state->analog_channels[idx].vdiv][0],
196 (*model->vdivs)[state->analog_channels[idx].vdiv][1]);
198 case SR_CONF_TRIGGER_SOURCE:
199 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
201 case SR_CONF_TRIGGER_SLOPE:
202 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
204 case SR_CONF_TRIGGER_PATTERN:
205 *data = g_variant_new_string(state->trigger_pattern);
207 case SR_CONF_HORIZ_TRIGGERPOS:
208 *data = g_variant_new_double(state->horiz_triggerpos);
210 case SR_CONF_COUPLING:
212 return SR_ERR_CHANNEL_GROUP;
213 if (cg_type != CG_ANALOG)
215 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
217 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]);
219 case SR_CONF_SAMPLERATE:
220 *data = g_variant_new_uint64(state->sample_rate);
222 case SR_CONF_LOGIC_THRESHOLD:
224 return SR_ERR_CHANNEL_GROUP;
225 if (cg_type != CG_DIGITAL)
229 if ((idx = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
231 *data = g_variant_new_string((*model->logic_threshold)[state->digital_pods[idx].threshold]);
233 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
235 return SR_ERR_CHANNEL_GROUP;
236 if (cg_type != CG_DIGITAL)
240 if ((idx = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
242 if (strcmp("USER2", (*model->logic_threshold)[state->digital_pods[idx].threshold]))
244 *data = g_variant_new_double(state->digital_pods[idx].user_threshold);
253 static int config_set(uint32_t key, GVariant *data,
254 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
256 int ret, cg_type, idx, j;
257 char command[MAX_COMMAND_SIZE], float_str[30], *tmp_str;
258 struct dev_context *devc;
259 const struct scope_config *model;
260 struct scope_state *state;
261 double tmp_d, tmp_d2;
262 gboolean update_sample_rate;
269 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
272 model = devc->model_config;
273 state = devc->model_state;
274 update_sample_rate = FALSE;
277 case SR_CONF_LIMIT_SAMPLES:
278 devc->samples_limit = g_variant_get_uint64(data);
281 case SR_CONF_LIMIT_FRAMES:
282 devc->frame_limit = g_variant_get_uint64(data);
285 case SR_CONF_TRIGGER_SOURCE:
286 if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
288 g_snprintf(command, sizeof(command),
289 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
290 (*model->trigger_sources)[idx]);
291 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
292 sr_scpi_get_opc(sdi->conn) != SR_OK)
294 state->trigger_source = idx;
299 return SR_ERR_CHANNEL_GROUP;
300 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
302 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
304 g_ascii_formatd(float_str, sizeof(float_str), "%E",
305 (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
306 g_snprintf(command, sizeof(command),
307 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
309 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
310 sr_scpi_get_opc(sdi->conn) != SR_OK)
312 state->analog_channels[j].vdiv = idx;
315 case SR_CONF_TIMEBASE:
316 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
318 g_ascii_formatd(float_str, sizeof(float_str), "%E",
319 (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
320 g_snprintf(command, sizeof(command),
321 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
323 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
324 sr_scpi_get_opc(sdi->conn) != SR_OK)
326 state->timebase = idx;
328 update_sample_rate = TRUE;
330 case SR_CONF_HORIZ_TRIGGERPOS:
331 tmp_d = g_variant_get_double(data);
332 if (tmp_d < 0.0 || tmp_d > 1.0)
334 tmp_d2 = -(tmp_d - 0.5) *
335 ((double) (*model->timebases)[state->timebase][0] /
336 (*model->timebases)[state->timebase][1])
338 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d2);
339 g_snprintf(command, sizeof(command),
340 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
342 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
343 sr_scpi_get_opc(sdi->conn) != SR_OK)
345 state->horiz_triggerpos = tmp_d;
348 case SR_CONF_TRIGGER_SLOPE:
349 if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0)
351 g_snprintf(command, sizeof(command),
352 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
353 (*model->trigger_slopes)[idx]);
354 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
355 sr_scpi_get_opc(sdi->conn) != SR_OK)
357 state->trigger_slope = idx;
360 case SR_CONF_TRIGGER_PATTERN:
361 tmp_str = (char *)g_variant_get_string(data, 0);
362 idx = strlen(tmp_str);
363 if (idx == 0 || idx > model->analog_channels + model->digital_channels)
365 g_snprintf(command, sizeof(command),
366 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_PATTERN],
368 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
369 sr_scpi_get_opc(sdi->conn) != SR_OK)
371 g_free(state->trigger_pattern);
372 state->trigger_pattern = g_strdup(tmp_str);
375 case SR_CONF_COUPLING:
377 return SR_ERR_CHANNEL_GROUP;
378 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
380 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
382 g_snprintf(command, sizeof(command),
383 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
384 j + 1, (*model->coupling_options)[idx]);
385 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
386 sr_scpi_get_opc(sdi->conn) != SR_OK)
388 state->analog_channels[j].coupling = idx;
391 case SR_CONF_LOGIC_THRESHOLD:
393 return SR_ERR_CHANNEL_GROUP;
394 if (cg_type != CG_DIGITAL)
398 if ((idx = std_str_idx(data, *model->logic_threshold, model->num_logic_threshold)) < 0)
400 if ((j = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
402 g_snprintf(command, sizeof(command),
403 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
404 j + 1, (*model->logic_threshold)[idx]);
405 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
406 sr_scpi_get_opc(sdi->conn) != SR_OK)
408 state->digital_pods[j].threshold = idx;
411 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
413 return SR_ERR_CHANNEL_GROUP;
414 if (cg_type != CG_DIGITAL)
418 if ((j = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
420 tmp_d = g_variant_get_double(data);
421 if (tmp_d < -2.0 || tmp_d > 8.0)
423 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
424 g_snprintf(command, sizeof(command),
425 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_USER_THRESHOLD],
426 j + 1, 2, float_str); // USER2 for custom logic_threshold setting
427 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
428 sr_scpi_get_opc(sdi->conn) != SR_OK)
430 g_snprintf(command, sizeof(command),
431 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
433 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
434 sr_scpi_get_opc(sdi->conn) != SR_OK)
436 state->digital_pods[j].user_threshold = tmp_d;
444 if (ret == SR_OK && update_sample_rate)
445 ret = hmo_update_sample_rate(sdi);
450 static int config_list(uint32_t key, GVariant **data,
451 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
453 int cg_type = CG_NONE;
454 struct dev_context *devc = NULL;
455 const struct scope_config *model = NULL;
459 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
462 model = devc->model_config;
466 case SR_CONF_SCAN_OPTIONS:
467 *data = std_gvar_array_u32(ARRAY_AND_SIZE(scanopts));
469 case SR_CONF_DEVICE_OPTIONS:
472 *data = std_gvar_array_u32(*model->devopts, model->num_devopts);
474 *data = std_gvar_array_u32(ARRAY_AND_SIZE(drvopts));
475 } else if (cg_type == CG_ANALOG) {
476 *data = std_gvar_array_u32(*model->devopts_cg_analog, model->num_devopts_cg_analog);
477 } else if (cg_type == CG_DIGITAL) {
478 *data = std_gvar_array_u32(*model->devopts_cg_digital, model->num_devopts_cg_digital);
480 *data = std_gvar_array_u32(NULL, 0);
483 case SR_CONF_COUPLING:
485 return SR_ERR_CHANNEL_GROUP;
488 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
490 case SR_CONF_TRIGGER_SOURCE:
493 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
495 case SR_CONF_TRIGGER_SLOPE:
498 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
500 case SR_CONF_TIMEBASE:
503 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
507 return SR_ERR_CHANNEL_GROUP;
510 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
512 case SR_CONF_LOGIC_THRESHOLD:
514 return SR_ERR_CHANNEL_GROUP;
517 *data = g_variant_new_strv(*model->logic_threshold, model->num_logic_threshold);
526 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
528 char command[MAX_COMMAND_SIZE];
529 struct sr_channel *ch;
530 struct dev_context *devc;
531 const struct scope_config *model;
534 model = devc->model_config;
536 ch = devc->current_channel->data;
539 case SR_CHANNEL_ANALOG:
540 g_snprintf(command, sizeof(command),
541 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
542 #ifdef WORDS_BIGENDIAN
549 case SR_CHANNEL_LOGIC:
550 g_snprintf(command, sizeof(command),
551 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
552 ch->index < 8 ? 1 : 2);
555 sr_err("Invalid channel type.");
559 return sr_scpi_send(sdi->conn, command);
562 static int hmo_check_channels(GSList *channels)
565 struct sr_channel *ch;
566 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
567 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
570 /* Preset "not enabled" for all channels / pods. */
571 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
572 enabled_chan[idx] = FALSE;
573 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
574 enabled_pod[idx] = FALSE;
577 * Determine which channels / pods are required for the caller's
578 * specified configuration.
580 for (l = channels; l; l = l->next) {
583 case SR_CHANNEL_ANALOG:
585 if (idx < ARRAY_SIZE(enabled_chan))
586 enabled_chan[idx] = TRUE;
588 case SR_CHANNEL_LOGIC:
590 if (idx < ARRAY_SIZE(enabled_pod))
591 enabled_pod[idx] = TRUE;
599 * Check for resource conflicts. Some channels can be either
600 * analog or digital, but never both at the same time.
602 * Note that the constraints might depend on the specific model.
603 * These tests might need some adjustment when support for more
604 * models gets added to the driver.
606 if (enabled_pod[0] && enabled_chan[2])
608 if (enabled_pod[1] && enabled_chan[3])
613 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
617 gboolean *pod_enabled, setup_changed;
618 char command[MAX_COMMAND_SIZE];
619 struct scope_state *state;
620 const struct scope_config *model;
621 struct sr_channel *ch;
622 struct dev_context *devc;
623 struct sr_scpi_dev_inst *scpi;
628 state = devc->model_state;
629 model = devc->model_config;
630 setup_changed = FALSE;
632 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
634 for (l = sdi->channels; l; l = l->next) {
637 case SR_CHANNEL_ANALOG:
638 if (ch->enabled == state->analog_channels[ch->index].state)
640 g_snprintf(command, sizeof(command),
641 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
642 ch->index + 1, ch->enabled);
644 if (sr_scpi_send(scpi, command) != SR_OK) {
648 state->analog_channels[ch->index].state = ch->enabled;
649 setup_changed = TRUE;
651 case SR_CHANNEL_LOGIC:
653 * A digital POD needs to be enabled for every group of
657 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
659 if (ch->enabled == state->digital_channels[ch->index])
661 g_snprintf(command, sizeof(command),
662 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
663 ch->index, ch->enabled);
665 if (sr_scpi_send(scpi, command) != SR_OK) {
670 state->digital_channels[ch->index] = ch->enabled;
671 setup_changed = TRUE;
680 for (i = 0; i < model->digital_pods; i++) {
681 if (state->digital_pods[i].state == pod_enabled[i])
683 g_snprintf(command, sizeof(command),
684 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
685 i + 1, pod_enabled[i]);
686 if (sr_scpi_send(scpi, command) != SR_OK) {
690 state->digital_pods[i].state = pod_enabled[i];
691 setup_changed = TRUE;
697 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
703 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
706 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
707 size_t group, pod_count;
708 struct sr_channel *ch;
709 struct dev_context *devc;
710 struct sr_scpi_dev_inst *scpi;
716 devc->num_samples = 0;
717 devc->num_frames = 0;
719 /* Preset empty results. */
720 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
721 digital_added[group] = FALSE;
722 g_slist_free(devc->enabled_channels);
723 devc->enabled_channels = NULL;
726 * Contruct the list of enabled channels. Determine the highest
727 * number of digital pods involved in the acquisition.
730 for (l = sdi->channels; l; l = l->next) {
734 /* Only add a single digital channel per group (pod). */
735 group = ch->index / 8;
736 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
737 devc->enabled_channels = g_slist_append(
738 devc->enabled_channels, ch);
739 if (ch->type == SR_CHANNEL_LOGIC) {
740 digital_added[group] = TRUE;
741 if (pod_count < group + 1)
742 pod_count = group + 1;
746 if (!devc->enabled_channels)
748 devc->pod_count = pod_count;
749 devc->logic_data = NULL;
752 * Check constraints. Some channels can be either analog or
753 * digital, but not both at the same time.
755 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
756 sr_err("Invalid channel configuration specified!");
762 * Configure the analog and digital channels and the
763 * corresponding digital pods.
765 if (hmo_setup_channels(sdi) != SR_OK) {
766 sr_err("Failed to setup channel configuration!");
772 * Start acquisition on the first enabled channel. The
773 * receive routine will continue driving the acquisition.
775 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
776 hmo_receive_data, (void *)sdi);
778 std_session_send_df_header(sdi);
780 devc->current_channel = devc->enabled_channels;
782 return hmo_request_data(sdi);
785 g_slist_free(devc->enabled_channels);
786 devc->enabled_channels = NULL;
790 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
792 struct dev_context *devc;
793 struct sr_scpi_dev_inst *scpi;
795 std_session_send_df_end(sdi);
799 devc->num_samples = 0;
800 devc->num_frames = 0;
801 g_slist_free(devc->enabled_channels);
802 devc->enabled_channels = NULL;
804 sr_scpi_source_remove(sdi->session, scpi);
809 static struct sr_dev_driver hameg_hmo_driver_info = {
811 .longname = "Hameg HMO",
814 .cleanup = std_cleanup,
816 .dev_list = std_dev_list,
817 .dev_clear = dev_clear,
818 .config_get = config_get,
819 .config_set = config_set,
820 .config_list = config_list,
821 .dev_open = dev_open,
822 .dev_close = dev_close,
823 .dev_acquisition_start = dev_acquisition_start,
824 .dev_acquisition_stop = dev_acquisition_stop,
827 SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);