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_HORIZ_TRIGGERPOS:
204 *data = g_variant_new_double(state->horiz_triggerpos);
206 case SR_CONF_COUPLING:
208 return SR_ERR_CHANNEL_GROUP;
209 if (cg_type != CG_ANALOG)
211 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
213 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]);
215 case SR_CONF_SAMPLERATE:
216 *data = g_variant_new_uint64(state->sample_rate);
218 case SR_CONF_LOGIC_THRESHOLD:
220 return SR_ERR_CHANNEL_GROUP;
221 if (cg_type != CG_DIGITAL)
225 if ((idx = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
227 *data = g_variant_new_string((*model->logic_threshold)[state->digital_pods[idx].threshold]);
229 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
231 return SR_ERR_CHANNEL_GROUP;
232 if (cg_type != CG_DIGITAL)
236 if ((idx = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
238 if (strcmp("USER2", (*model->logic_threshold)[state->digital_pods[idx].threshold]))
240 *data = g_variant_new_double(state->digital_pods[idx].user_threshold);
249 static int config_set(uint32_t key, GVariant *data,
250 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
252 int ret, cg_type, idx, j;
253 char command[MAX_COMMAND_SIZE], float_str[30];
254 struct dev_context *devc;
255 const struct scope_config *model;
256 struct scope_state *state;
258 gboolean update_sample_rate;
265 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
268 model = devc->model_config;
269 state = devc->model_state;
270 update_sample_rate = FALSE;
273 case SR_CONF_LIMIT_SAMPLES:
274 devc->samples_limit = g_variant_get_uint64(data);
277 case SR_CONF_LIMIT_FRAMES:
278 devc->frame_limit = g_variant_get_uint64(data);
281 case SR_CONF_TRIGGER_SOURCE:
282 if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
284 state->trigger_source = idx;
285 g_snprintf(command, sizeof(command),
286 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
287 (*model->trigger_sources)[idx]);
288 ret = sr_scpi_send(sdi->conn, command);
292 return SR_ERR_CHANNEL_GROUP;
293 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
295 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
297 state->analog_channels[j].vdiv = idx;
298 g_ascii_formatd(float_str, sizeof(float_str), "%E",
299 (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
300 g_snprintf(command, sizeof(command),
301 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
303 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
304 sr_scpi_get_opc(sdi->conn) != SR_OK)
308 case SR_CONF_TIMEBASE:
309 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
311 state->timebase = idx;
312 g_ascii_formatd(float_str, sizeof(float_str), "%E",
313 (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
314 g_snprintf(command, sizeof(command),
315 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
317 ret = sr_scpi_send(sdi->conn, command);
318 update_sample_rate = TRUE;
320 case SR_CONF_HORIZ_TRIGGERPOS:
321 tmp_d = g_variant_get_double(data);
322 if (tmp_d < 0.0 || tmp_d > 1.0)
324 state->horiz_triggerpos = tmp_d;
325 tmp_d = -(tmp_d - 0.5) *
326 ((double) (*model->timebases)[state->timebase][0] /
327 (*model->timebases)[state->timebase][1])
329 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
330 g_snprintf(command, sizeof(command),
331 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
333 ret = sr_scpi_send(sdi->conn, command);
335 case SR_CONF_TRIGGER_SLOPE:
336 if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0)
338 state->trigger_slope = idx;
339 g_snprintf(command, sizeof(command),
340 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
341 (*model->trigger_slopes)[idx]);
342 ret = sr_scpi_send(sdi->conn, command);
344 case SR_CONF_COUPLING:
346 return SR_ERR_CHANNEL_GROUP;
347 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
349 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
351 state->analog_channels[j].coupling = idx;
352 g_snprintf(command, sizeof(command),
353 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
354 j + 1, (*model->coupling_options)[idx]);
355 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
356 sr_scpi_get_opc(sdi->conn) != SR_OK)
360 case SR_CONF_LOGIC_THRESHOLD:
362 return SR_ERR_CHANNEL_GROUP;
363 if (cg_type != CG_DIGITAL)
367 if ((idx = std_str_idx(data, *model->logic_threshold, model->num_logic_threshold)) < 0)
369 if ((j = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
371 g_snprintf(command, sizeof(command),
372 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
373 j + 1, (*model->logic_threshold)[idx]);
374 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
375 sr_scpi_get_opc(sdi->conn) != SR_OK)
377 state->digital_pods[j].threshold = idx;
380 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
382 return SR_ERR_CHANNEL_GROUP;
383 if (cg_type != CG_DIGITAL)
387 if ((j = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
389 tmp_d = g_variant_get_double(data);
390 if (tmp_d < -2.0 || tmp_d > 8.0)
392 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
393 g_snprintf(command, sizeof(command),
394 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_USER_THRESHOLD],
395 j + 1, 2, float_str); // USER2 for custom logic_threshold setting
396 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
397 sr_scpi_get_opc(sdi->conn) != SR_OK)
399 g_snprintf(command, sizeof(command),
400 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
402 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
403 sr_scpi_get_opc(sdi->conn) != SR_OK)
405 state->digital_pods[j].user_threshold = tmp_d;
414 ret = sr_scpi_get_opc(sdi->conn);
416 if (ret == SR_OK && update_sample_rate)
417 ret = hmo_update_sample_rate(sdi);
422 static int config_list(uint32_t key, GVariant **data,
423 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
425 int cg_type = CG_NONE;
426 struct dev_context *devc = NULL;
427 const struct scope_config *model = NULL;
431 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
434 model = devc->model_config;
438 case SR_CONF_SCAN_OPTIONS:
439 *data = std_gvar_array_u32(ARRAY_AND_SIZE(scanopts));
441 case SR_CONF_DEVICE_OPTIONS:
444 *data = std_gvar_array_u32(*model->devopts, model->num_devopts);
446 *data = std_gvar_array_u32(ARRAY_AND_SIZE(drvopts));
447 } else if (cg_type == CG_ANALOG) {
448 *data = std_gvar_array_u32(*model->devopts_cg_analog, model->num_devopts_cg_analog);
449 } else if (cg_type == CG_DIGITAL) {
450 *data = std_gvar_array_u32(*model->devopts_cg_digital, model->num_devopts_cg_digital);
452 *data = std_gvar_array_u32(NULL, 0);
455 case SR_CONF_COUPLING:
457 return SR_ERR_CHANNEL_GROUP;
460 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
462 case SR_CONF_TRIGGER_SOURCE:
465 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
467 case SR_CONF_TRIGGER_SLOPE:
470 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
472 case SR_CONF_TIMEBASE:
475 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
479 return SR_ERR_CHANNEL_GROUP;
482 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
484 case SR_CONF_LOGIC_THRESHOLD:
486 return SR_ERR_CHANNEL_GROUP;
489 *data = g_variant_new_strv(*model->logic_threshold, model->num_logic_threshold);
498 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
500 char command[MAX_COMMAND_SIZE];
501 struct sr_channel *ch;
502 struct dev_context *devc;
503 const struct scope_config *model;
506 model = devc->model_config;
508 ch = devc->current_channel->data;
511 case SR_CHANNEL_ANALOG:
512 g_snprintf(command, sizeof(command),
513 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
514 #ifdef WORDS_BIGENDIAN
521 case SR_CHANNEL_LOGIC:
522 g_snprintf(command, sizeof(command),
523 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
524 ch->index < 8 ? 1 : 2);
527 sr_err("Invalid channel type.");
531 return sr_scpi_send(sdi->conn, command);
534 static int hmo_check_channels(GSList *channels)
537 struct sr_channel *ch;
538 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
539 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
542 /* Preset "not enabled" for all channels / pods. */
543 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
544 enabled_chan[idx] = FALSE;
545 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
546 enabled_pod[idx] = FALSE;
549 * Determine which channels / pods are required for the caller's
550 * specified configuration.
552 for (l = channels; l; l = l->next) {
555 case SR_CHANNEL_ANALOG:
557 if (idx < ARRAY_SIZE(enabled_chan))
558 enabled_chan[idx] = TRUE;
560 case SR_CHANNEL_LOGIC:
562 if (idx < ARRAY_SIZE(enabled_pod))
563 enabled_pod[idx] = TRUE;
571 * Check for resource conflicts. Some channels can be either
572 * analog or digital, but never both at the same time.
574 * Note that the constraints might depend on the specific model.
575 * These tests might need some adjustment when support for more
576 * models gets added to the driver.
578 if (enabled_pod[0] && enabled_chan[2])
580 if (enabled_pod[1] && enabled_chan[3])
585 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
589 gboolean *pod_enabled, setup_changed;
590 char command[MAX_COMMAND_SIZE];
591 struct scope_state *state;
592 const struct scope_config *model;
593 struct sr_channel *ch;
594 struct dev_context *devc;
595 struct sr_scpi_dev_inst *scpi;
600 state = devc->model_state;
601 model = devc->model_config;
602 setup_changed = FALSE;
604 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
606 for (l = sdi->channels; l; l = l->next) {
609 case SR_CHANNEL_ANALOG:
610 if (ch->enabled == state->analog_channels[ch->index].state)
612 g_snprintf(command, sizeof(command),
613 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
614 ch->index + 1, ch->enabled);
616 if (sr_scpi_send(scpi, command) != SR_OK) {
620 state->analog_channels[ch->index].state = ch->enabled;
621 setup_changed = TRUE;
623 case SR_CHANNEL_LOGIC:
625 * A digital POD needs to be enabled for every group of
629 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
631 if (ch->enabled == state->digital_channels[ch->index])
633 g_snprintf(command, sizeof(command),
634 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
635 ch->index, ch->enabled);
637 if (sr_scpi_send(scpi, command) != SR_OK) {
642 state->digital_channels[ch->index] = ch->enabled;
643 setup_changed = TRUE;
652 for (i = 0; i < model->digital_pods; i++) {
653 if (state->digital_pods[i].state == pod_enabled[i])
655 g_snprintf(command, sizeof(command),
656 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
657 i + 1, pod_enabled[i]);
658 if (sr_scpi_send(scpi, command) != SR_OK) {
662 state->digital_pods[i].state = pod_enabled[i];
663 setup_changed = TRUE;
669 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
675 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
678 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
679 size_t group, pod_count;
680 struct sr_channel *ch;
681 struct dev_context *devc;
682 struct sr_scpi_dev_inst *scpi;
688 devc->num_samples = 0;
689 devc->num_frames = 0;
691 /* Preset empty results. */
692 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
693 digital_added[group] = FALSE;
694 g_slist_free(devc->enabled_channels);
695 devc->enabled_channels = NULL;
698 * Contruct the list of enabled channels. Determine the highest
699 * number of digital pods involved in the acquisition.
702 for (l = sdi->channels; l; l = l->next) {
706 /* Only add a single digital channel per group (pod). */
707 group = ch->index / 8;
708 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
709 devc->enabled_channels = g_slist_append(
710 devc->enabled_channels, ch);
711 if (ch->type == SR_CHANNEL_LOGIC) {
712 digital_added[group] = TRUE;
713 if (pod_count < group + 1)
714 pod_count = group + 1;
718 if (!devc->enabled_channels)
720 devc->pod_count = pod_count;
721 devc->logic_data = NULL;
724 * Check constraints. Some channels can be either analog or
725 * digital, but not both at the same time.
727 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
728 sr_err("Invalid channel configuration specified!");
734 * Configure the analog and digital channels and the
735 * corresponding digital pods.
737 if (hmo_setup_channels(sdi) != SR_OK) {
738 sr_err("Failed to setup channel configuration!");
744 * Start acquisition on the first enabled channel. The
745 * receive routine will continue driving the acquisition.
747 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
748 hmo_receive_data, (void *)sdi);
750 std_session_send_df_header(sdi);
752 devc->current_channel = devc->enabled_channels;
754 return hmo_request_data(sdi);
757 g_slist_free(devc->enabled_channels);
758 devc->enabled_channels = NULL;
762 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
764 struct dev_context *devc;
765 struct sr_scpi_dev_inst *scpi;
767 std_session_send_df_end(sdi);
771 devc->num_samples = 0;
772 devc->num_frames = 0;
773 g_slist_free(devc->enabled_channels);
774 devc->enabled_channels = NULL;
776 sr_scpi_source_remove(sdi->session, scpi);
781 static struct sr_dev_driver hameg_hmo_driver_info = {
783 .longname = "Hameg HMO",
786 .cleanup = std_cleanup,
788 .dev_list = std_dev_list,
789 .dev_clear = dev_clear,
790 .config_get = config_get,
791 .config_set = config_set,
792 .config_list = config_list,
793 .dev_open = dev_open,
794 .dev_close = dev_close,
795 .dev_acquisition_start = dev_acquisition_start,
796 .dev_acquisition_stop = dev_acquisition_stop,
799 SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);