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[] = {
49 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
51 struct sr_dev_inst *sdi;
52 struct dev_context *devc;
53 struct sr_scpi_hw_info *hw_info;
59 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
60 sr_info("Couldn't get IDN response.");
64 if (std_str_idx_s(hw_info->manufacturer, ARRAY_AND_SIZE(manufacturers)) < 0)
67 sdi = g_malloc0(sizeof(struct sr_dev_inst));
68 sdi->vendor = g_strdup(hw_info->manufacturer);
69 sdi->model = g_strdup(hw_info->model);
70 sdi->version = g_strdup(hw_info->firmware_version);
71 sdi->serial_num = g_strdup(hw_info->serial_number);
72 sdi->driver = &hameg_hmo_driver_info;
73 sdi->inst_type = SR_INST_SCPI;
76 sr_scpi_hw_info_free(hw_info);
79 devc = g_malloc0(sizeof(struct dev_context));
83 if (hmo_init_device(sdi) != SR_OK)
89 sr_scpi_hw_info_free(hw_info);
90 sr_dev_inst_free(sdi);
96 static GSList *scan(struct sr_dev_driver *di, GSList *options)
98 return sr_scpi_scan(di->context, options, probe_device);
101 static void clear_helper(struct dev_context *devc)
103 hmo_scope_state_free(devc->model_state);
104 g_free(devc->analog_groups);
105 g_free(devc->digital_groups);
108 static int dev_clear(const struct sr_dev_driver *di)
110 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
113 static int dev_open(struct sr_dev_inst *sdi)
115 if (sr_scpi_open(sdi->conn) != SR_OK)
118 if (hmo_scope_state_get(sdi) != SR_OK)
124 static int dev_close(struct sr_dev_inst *sdi)
126 return sr_scpi_close(sdi->conn);
129 static int check_channel_group(struct dev_context *devc,
130 const struct sr_channel_group *cg)
132 const struct scope_config *model;
134 model = devc->model_config;
139 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) >= 0)
142 if (std_cg_idx(cg, devc->digital_groups, model->digital_pods) >= 0)
145 sr_err("Invalid channel group specified.");
150 static int config_get(uint32_t key, GVariant **data,
151 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
154 struct dev_context *devc;
155 const struct scope_config *model;
156 struct scope_state *state;
163 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
166 model = devc->model_config;
167 state = devc->model_state;
170 case SR_CONF_NUM_HDIV:
171 *data = g_variant_new_int32(model->num_xdivs);
173 case SR_CONF_TIMEBASE:
174 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
175 (*model->timebases)[state->timebase][1]);
177 case SR_CONF_NUM_VDIV:
179 return SR_ERR_CHANNEL_GROUP;
180 if (cg_type != CG_ANALOG)
182 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) < 0)
184 *data = g_variant_new_int32(model->num_ydivs);
188 return SR_ERR_CHANNEL_GROUP;
189 if (cg_type != CG_ANALOG)
191 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
193 *data = g_variant_new("(tt)",
194 (*model->vdivs)[state->analog_channels[idx].vdiv][0],
195 (*model->vdivs)[state->analog_channels[idx].vdiv][1]);
197 case SR_CONF_TRIGGER_SOURCE:
198 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
200 case SR_CONF_TRIGGER_SLOPE:
201 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
203 case SR_CONF_TRIGGER_PATTERN:
204 *data = g_variant_new_string(state->trigger_pattern);
206 case SR_CONF_HORIZ_TRIGGERPOS:
207 *data = g_variant_new_double(state->horiz_triggerpos);
209 case SR_CONF_COUPLING:
211 return SR_ERR_CHANNEL_GROUP;
212 if (cg_type != CG_ANALOG)
214 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
216 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]);
218 case SR_CONF_SAMPLERATE:
219 *data = g_variant_new_uint64(state->sample_rate);
221 case SR_CONF_LOGIC_THRESHOLD:
223 return SR_ERR_CHANNEL_GROUP;
224 if (cg_type != CG_DIGITAL)
228 if ((idx = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
230 *data = g_variant_new_string((*model->logic_threshold)[state->digital_pods[idx].threshold]);
232 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
234 return SR_ERR_CHANNEL_GROUP;
235 if (cg_type != CG_DIGITAL)
239 if ((idx = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
241 if (strcmp("USER2", (*model->logic_threshold)[state->digital_pods[idx].threshold]))
243 *data = g_variant_new_double(state->digital_pods[idx].user_threshold);
252 static int config_set(uint32_t key, GVariant *data,
253 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
255 int ret, cg_type, idx, j;
256 char command[MAX_COMMAND_SIZE], float_str[30], *tmp_str;
257 struct dev_context *devc;
258 const struct scope_config *model;
259 struct scope_state *state;
260 double tmp_d, tmp_d2;
261 gboolean update_sample_rate;
268 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
271 model = devc->model_config;
272 state = devc->model_state;
273 update_sample_rate = FALSE;
276 case SR_CONF_LIMIT_SAMPLES:
277 devc->samples_limit = g_variant_get_uint64(data);
280 case SR_CONF_LIMIT_FRAMES:
281 devc->frame_limit = g_variant_get_uint64(data);
284 case SR_CONF_TRIGGER_SOURCE:
285 if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
287 g_snprintf(command, sizeof(command),
288 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
289 (*model->trigger_sources)[idx]);
290 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
291 sr_scpi_get_opc(sdi->conn) != SR_OK)
293 state->trigger_source = idx;
298 return SR_ERR_CHANNEL_GROUP;
299 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
301 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
303 g_ascii_formatd(float_str, sizeof(float_str), "%E",
304 (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
305 g_snprintf(command, sizeof(command),
306 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
308 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
309 sr_scpi_get_opc(sdi->conn) != SR_OK)
311 state->analog_channels[j].vdiv = idx;
314 case SR_CONF_TIMEBASE:
315 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
317 g_ascii_formatd(float_str, sizeof(float_str), "%E",
318 (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
319 g_snprintf(command, sizeof(command),
320 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
322 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
323 sr_scpi_get_opc(sdi->conn) != SR_OK)
325 state->timebase = idx;
327 update_sample_rate = TRUE;
329 case SR_CONF_HORIZ_TRIGGERPOS:
330 tmp_d = g_variant_get_double(data);
331 if (tmp_d < 0.0 || tmp_d > 1.0)
333 tmp_d2 = -(tmp_d - 0.5) *
334 ((double) (*model->timebases)[state->timebase][0] /
335 (*model->timebases)[state->timebase][1])
337 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d2);
338 g_snprintf(command, sizeof(command),
339 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
341 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
342 sr_scpi_get_opc(sdi->conn) != SR_OK)
344 state->horiz_triggerpos = tmp_d;
347 case SR_CONF_TRIGGER_SLOPE:
348 if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0)
350 g_snprintf(command, sizeof(command),
351 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
352 (*model->trigger_slopes)[idx]);
353 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
354 sr_scpi_get_opc(sdi->conn) != SR_OK)
356 state->trigger_slope = idx;
359 case SR_CONF_TRIGGER_PATTERN:
360 tmp_str = (char *)g_variant_get_string(data, 0);
361 idx = strlen(tmp_str);
362 if (idx == 0 || idx > model->analog_channels + model->digital_channels)
364 g_snprintf(command, sizeof(command),
365 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_PATTERN],
367 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
368 sr_scpi_get_opc(sdi->conn) != SR_OK)
370 g_free(state->trigger_pattern);
371 state->trigger_pattern = g_strdup(tmp_str);
374 case SR_CONF_COUPLING:
376 return SR_ERR_CHANNEL_GROUP;
377 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
379 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
381 g_snprintf(command, sizeof(command),
382 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
383 j + 1, (*model->coupling_options)[idx]);
384 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
385 sr_scpi_get_opc(sdi->conn) != SR_OK)
387 state->analog_channels[j].coupling = idx;
390 case SR_CONF_LOGIC_THRESHOLD:
392 return SR_ERR_CHANNEL_GROUP;
393 if (cg_type != CG_DIGITAL)
397 if ((idx = std_str_idx(data, *model->logic_threshold, model->num_logic_threshold)) < 0)
399 if ((j = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
401 g_snprintf(command, sizeof(command),
402 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
403 j + 1, (*model->logic_threshold)[idx]);
404 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
405 sr_scpi_get_opc(sdi->conn) != SR_OK)
407 state->digital_pods[j].threshold = idx;
410 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
412 return SR_ERR_CHANNEL_GROUP;
413 if (cg_type != CG_DIGITAL)
417 if ((j = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
419 tmp_d = g_variant_get_double(data);
420 if (tmp_d < -2.0 || tmp_d > 8.0)
422 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
423 g_snprintf(command, sizeof(command),
424 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_USER_THRESHOLD],
425 j + 1, 2, float_str); // USER2 for custom logic_threshold setting
426 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
427 sr_scpi_get_opc(sdi->conn) != SR_OK)
429 g_snprintf(command, sizeof(command),
430 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
432 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
433 sr_scpi_get_opc(sdi->conn) != SR_OK)
435 state->digital_pods[j].user_threshold = tmp_d;
443 if (ret == SR_OK && update_sample_rate)
444 ret = hmo_update_sample_rate(sdi);
449 static int config_list(uint32_t key, GVariant **data,
450 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
452 int cg_type = CG_NONE;
453 struct dev_context *devc = NULL;
454 const struct scope_config *model = NULL;
458 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
461 model = devc->model_config;
465 case SR_CONF_SCAN_OPTIONS:
466 *data = std_gvar_array_u32(ARRAY_AND_SIZE(scanopts));
468 case SR_CONF_DEVICE_OPTIONS:
471 *data = std_gvar_array_u32(*model->devopts, model->num_devopts);
473 *data = std_gvar_array_u32(ARRAY_AND_SIZE(drvopts));
474 } else if (cg_type == CG_ANALOG) {
475 *data = std_gvar_array_u32(*model->devopts_cg_analog, model->num_devopts_cg_analog);
476 } else if (cg_type == CG_DIGITAL) {
477 *data = std_gvar_array_u32(*model->devopts_cg_digital, model->num_devopts_cg_digital);
479 *data = std_gvar_array_u32(NULL, 0);
482 case SR_CONF_COUPLING:
484 return SR_ERR_CHANNEL_GROUP;
487 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
489 case SR_CONF_TRIGGER_SOURCE:
492 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
494 case SR_CONF_TRIGGER_SLOPE:
497 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
499 case SR_CONF_TIMEBASE:
502 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
506 return SR_ERR_CHANNEL_GROUP;
509 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
511 case SR_CONF_LOGIC_THRESHOLD:
513 return SR_ERR_CHANNEL_GROUP;
516 *data = g_variant_new_strv(*model->logic_threshold, model->num_logic_threshold);
525 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
527 char command[MAX_COMMAND_SIZE];
528 struct sr_channel *ch;
529 struct dev_context *devc;
530 const struct scope_config *model;
533 model = devc->model_config;
535 ch = devc->current_channel->data;
538 case SR_CHANNEL_ANALOG:
539 g_snprintf(command, sizeof(command),
540 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
541 #ifdef WORDS_BIGENDIAN
548 case SR_CHANNEL_LOGIC:
549 g_snprintf(command, sizeof(command),
550 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
551 ch->index < 8 ? 1 : 2);
554 sr_err("Invalid channel type.");
558 return sr_scpi_send(sdi->conn, command);
561 static int hmo_check_channels(GSList *channels)
564 struct sr_channel *ch;
565 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
566 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
569 /* Preset "not enabled" for all channels / pods. */
570 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
571 enabled_chan[idx] = FALSE;
572 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
573 enabled_pod[idx] = FALSE;
576 * Determine which channels / pods are required for the caller's
577 * specified configuration.
579 for (l = channels; l; l = l->next) {
582 case SR_CHANNEL_ANALOG:
584 if (idx < ARRAY_SIZE(enabled_chan))
585 enabled_chan[idx] = TRUE;
587 case SR_CHANNEL_LOGIC:
589 if (idx < ARRAY_SIZE(enabled_pod))
590 enabled_pod[idx] = TRUE;
598 * Check for resource conflicts. Some channels can be either
599 * analog or digital, but never both at the same time.
601 * Note that the constraints might depend on the specific model.
602 * These tests might need some adjustment when support for more
603 * models gets added to the driver.
605 if (enabled_pod[0] && enabled_chan[2])
607 if (enabled_pod[1] && enabled_chan[3])
612 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
616 gboolean *pod_enabled, setup_changed;
617 char command[MAX_COMMAND_SIZE];
618 struct scope_state *state;
619 const struct scope_config *model;
620 struct sr_channel *ch;
621 struct dev_context *devc;
622 struct sr_scpi_dev_inst *scpi;
627 state = devc->model_state;
628 model = devc->model_config;
629 setup_changed = FALSE;
631 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
633 for (l = sdi->channels; l; l = l->next) {
636 case SR_CHANNEL_ANALOG:
637 if (ch->enabled == state->analog_channels[ch->index].state)
639 g_snprintf(command, sizeof(command),
640 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
641 ch->index + 1, ch->enabled);
643 if (sr_scpi_send(scpi, command) != SR_OK) {
647 state->analog_channels[ch->index].state = ch->enabled;
648 setup_changed = TRUE;
650 case SR_CHANNEL_LOGIC:
652 * A digital POD needs to be enabled for every group of
656 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
658 if (ch->enabled == state->digital_channels[ch->index])
660 g_snprintf(command, sizeof(command),
661 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
662 ch->index, ch->enabled);
664 if (sr_scpi_send(scpi, command) != SR_OK) {
669 state->digital_channels[ch->index] = ch->enabled;
670 setup_changed = TRUE;
679 for (i = 0; i < model->digital_pods; i++) {
680 if (state->digital_pods[i].state == pod_enabled[i])
682 g_snprintf(command, sizeof(command),
683 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
684 i + 1, pod_enabled[i]);
685 if (sr_scpi_send(scpi, command) != SR_OK) {
689 state->digital_pods[i].state = pod_enabled[i];
690 setup_changed = TRUE;
696 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
702 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
705 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
706 size_t group, pod_count;
707 struct sr_channel *ch;
708 struct dev_context *devc;
709 struct sr_scpi_dev_inst *scpi;
715 devc->num_samples = 0;
716 devc->num_frames = 0;
718 /* Preset empty results. */
719 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
720 digital_added[group] = FALSE;
721 g_slist_free(devc->enabled_channels);
722 devc->enabled_channels = NULL;
725 * Contruct the list of enabled channels. Determine the highest
726 * number of digital pods involved in the acquisition.
729 for (l = sdi->channels; l; l = l->next) {
733 /* Only add a single digital channel per group (pod). */
734 group = ch->index / 8;
735 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
736 devc->enabled_channels = g_slist_append(
737 devc->enabled_channels, ch);
738 if (ch->type == SR_CHANNEL_LOGIC) {
739 digital_added[group] = TRUE;
740 if (pod_count < group + 1)
741 pod_count = group + 1;
745 if (!devc->enabled_channels)
747 devc->pod_count = pod_count;
748 devc->logic_data = NULL;
751 * Check constraints. Some channels can be either analog or
752 * digital, but not both at the same time.
754 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
755 sr_err("Invalid channel configuration specified!");
761 * Configure the analog and digital channels and the
762 * corresponding digital pods.
764 if (hmo_setup_channels(sdi) != SR_OK) {
765 sr_err("Failed to setup channel configuration!");
771 * Start acquisition on the first enabled channel. The
772 * receive routine will continue driving the acquisition.
774 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
775 hmo_receive_data, (void *)sdi);
777 std_session_send_df_header(sdi);
779 devc->current_channel = devc->enabled_channels;
781 return hmo_request_data(sdi);
784 g_slist_free(devc->enabled_channels);
785 devc->enabled_channels = NULL;
789 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
791 struct dev_context *devc;
792 struct sr_scpi_dev_inst *scpi;
794 std_session_send_df_end(sdi);
798 devc->num_samples = 0;
799 devc->num_frames = 0;
800 g_slist_free(devc->enabled_channels);
801 devc->enabled_channels = NULL;
803 sr_scpi_source_remove(sdi->session, scpi);
808 static struct sr_dev_driver hameg_hmo_driver_info = {
810 .longname = "Hameg HMO",
813 .cleanup = std_cleanup,
815 .dev_list = std_dev_list,
816 .dev_clear = dev_clear,
817 .config_get = config_get,
818 .config_set = config_set,
819 .config_list = config_list,
820 .dev_open = dev_open,
821 .dev_close = dev_close,
822 .dev_acquisition_start = dev_acquisition_start,
823 .dev_acquisition_stop = dev_acquisition_stop,
826 SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);