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 scanopts[] = {
39 static const uint32_t drvopts[] = {
50 static struct sr_dev_inst *hmo_probe_serial_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, hmo_probe_serial_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)
134 const struct scope_config *model;
136 model = devc->model_config;
141 for (i = 0; i < model->analog_channels; i++)
142 if (cg == devc->analog_groups[i])
145 for (i = 0; i < model->digital_pods; i++)
146 if (cg == devc->digital_groups[i])
149 sr_err("Invalid channel group specified.");
154 static int config_get(uint32_t key, GVariant **data,
155 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
159 struct dev_context *devc;
160 const struct scope_config *model;
161 struct scope_state *state;
168 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
172 model = devc->model_config;
173 state = devc->model_state;
176 case SR_CONF_NUM_HDIV:
177 *data = g_variant_new_int32(model->num_xdivs);
180 case SR_CONF_TIMEBASE:
181 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
182 (*model->timebases)[state->timebase][1]);
185 case SR_CONF_NUM_VDIV:
186 if (cg_type == CG_NONE) {
187 return SR_ERR_CHANNEL_GROUP;
188 } else if (cg_type == CG_ANALOG) {
189 for (i = 0; i < model->analog_channels; i++) {
190 if (cg != devc->analog_groups[i])
192 *data = g_variant_new_int32(model->num_ydivs);
202 if (cg_type == CG_NONE) {
203 return SR_ERR_CHANNEL_GROUP;
204 } else if (cg_type == CG_ANALOG) {
205 for (i = 0; i < model->analog_channels; i++) {
206 if (cg != devc->analog_groups[i])
208 *data = g_variant_new("(tt)",
209 (*model->vdivs)[state->analog_channels[i].vdiv][0],
210 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
219 case SR_CONF_TRIGGER_SOURCE:
220 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
223 case SR_CONF_TRIGGER_SLOPE:
224 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
227 case SR_CONF_HORIZ_TRIGGERPOS:
228 *data = g_variant_new_double(state->horiz_triggerpos);
231 case SR_CONF_COUPLING:
232 if (cg_type == CG_NONE) {
233 return SR_ERR_CHANNEL_GROUP;
234 } else if (cg_type == CG_ANALOG) {
235 for (i = 0; i < model->analog_channels; i++) {
236 if (cg != devc->analog_groups[i])
238 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
247 case SR_CONF_SAMPLERATE:
248 *data = g_variant_new_uint64(state->sample_rate);
258 static int config_set(uint32_t key, GVariant *data,
259 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
261 int ret, cg_type, idx;
263 char command[MAX_COMMAND_SIZE], float_str[30];
264 struct dev_context *devc;
265 const struct scope_config *model;
266 struct scope_state *state;
269 gboolean update_sample_rate;
276 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
279 model = devc->model_config;
280 state = devc->model_state;
281 update_sample_rate = FALSE;
286 case SR_CONF_LIMIT_FRAMES:
287 devc->frame_limit = g_variant_get_uint64(data);
290 case SR_CONF_TRIGGER_SOURCE:
291 tmp = g_variant_get_string(data, NULL);
292 for (i = 0; (*model->trigger_sources)[i]; i++) {
293 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
295 state->trigger_source = i;
296 g_snprintf(command, sizeof(command),
297 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
298 (*model->trigger_sources)[i]);
300 ret = sr_scpi_send(sdi->conn, command);
305 if (cg_type == CG_NONE)
306 return SR_ERR_CHANNEL_GROUP;
307 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
309 for (j = 1; j <= model->analog_channels; j++) {
310 if (cg != devc->analog_groups[j - 1])
312 state->analog_channels[j - 1].vdiv = idx;
313 g_ascii_formatd(float_str, sizeof(float_str), "%E",
314 (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
315 g_snprintf(command, sizeof(command),
316 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
318 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
319 sr_scpi_get_opc(sdi->conn) != SR_OK)
325 case SR_CONF_TIMEBASE:
326 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
328 state->timebase = idx;
329 g_ascii_formatd(float_str, sizeof(float_str), "%E",
330 (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
331 g_snprintf(command, sizeof(command),
332 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
334 ret = sr_scpi_send(sdi->conn, command);
335 update_sample_rate = TRUE;
337 case SR_CONF_HORIZ_TRIGGERPOS:
338 tmp_d = g_variant_get_double(data);
340 if (tmp_d < 0.0 || tmp_d > 1.0)
343 state->horiz_triggerpos = tmp_d;
344 tmp_d = -(tmp_d - 0.5) *
345 ((double) (*model->timebases)[state->timebase][0] /
346 (*model->timebases)[state->timebase][1])
349 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
350 g_snprintf(command, sizeof(command),
351 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
354 ret = sr_scpi_send(sdi->conn, command);
356 case SR_CONF_TRIGGER_SLOPE:
357 tmp = g_variant_get_string(data, NULL);
358 for (i = 0; (*model->trigger_slopes)[i]; i++) {
359 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
361 state->trigger_slope = i;
362 g_snprintf(command, sizeof(command),
363 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
364 (*model->trigger_slopes)[i]);
366 ret = sr_scpi_send(sdi->conn, command);
370 case SR_CONF_COUPLING:
371 if (cg_type == CG_NONE)
372 return SR_ERR_CHANNEL_GROUP;
374 tmp = g_variant_get_string(data, NULL);
376 for (i = 0; (*model->coupling_options)[i]; i++) {
377 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
379 for (j = 1; j <= model->analog_channels; j++) {
380 if (cg != devc->analog_groups[j - 1])
382 state->analog_channels[j-1].coupling = i;
384 g_snprintf(command, sizeof(command),
385 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
388 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
389 sr_scpi_get_opc(sdi->conn) != SR_OK)
404 ret = sr_scpi_get_opc(sdi->conn);
406 if (ret == SR_OK && update_sample_rate)
407 ret = hmo_update_sample_rate(sdi);
412 static int config_list(uint32_t key, GVariant **data,
413 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
415 int cg_type = CG_NONE;
416 struct dev_context *devc = NULL;
417 const struct scope_config *model = NULL;
421 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
424 model = devc->model_config;
428 case SR_CONF_SCAN_OPTIONS:
429 *data = std_gvar_array_u32(ARRAY_AND_SIZE(scanopts));
431 case SR_CONF_DEVICE_OPTIONS:
432 if (cg_type == CG_NONE) {
434 *data = std_gvar_array_u32((const uint32_t *)model->devopts, model->num_devopts);
436 *data = std_gvar_array_u32(ARRAY_AND_SIZE(drvopts));
437 } else if (cg_type == CG_ANALOG) {
438 *data = std_gvar_array_u32((const uint32_t *)model->devopts_cg_analog, model->num_devopts_cg_analog);
440 *data = std_gvar_array_u32(NULL, 0);
443 case SR_CONF_COUPLING:
444 if (cg_type == CG_NONE)
445 return SR_ERR_CHANNEL_GROUP;
446 *data = g_variant_new_strv(*model->coupling_options,
447 g_strv_length((char **)*model->coupling_options));
449 case SR_CONF_TRIGGER_SOURCE:
452 *data = g_variant_new_strv(*model->trigger_sources,
453 g_strv_length((char **)*model->trigger_sources));
455 case SR_CONF_TRIGGER_SLOPE:
458 *data = g_variant_new_strv(*model->trigger_slopes,
459 g_strv_length((char **)*model->trigger_slopes));
461 case SR_CONF_TIMEBASE:
464 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
467 if (cg_type == CG_NONE)
468 return SR_ERR_CHANNEL_GROUP;
469 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
478 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
480 char command[MAX_COMMAND_SIZE];
481 struct sr_channel *ch;
482 struct dev_context *devc;
483 const struct scope_config *model;
486 model = devc->model_config;
488 ch = devc->current_channel->data;
491 case SR_CHANNEL_ANALOG:
492 g_snprintf(command, sizeof(command),
493 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
494 #ifdef WORDS_BIGENDIAN
501 case SR_CHANNEL_LOGIC:
502 g_snprintf(command, sizeof(command),
503 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
504 ch->index < 8 ? 1 : 2);
507 sr_err("Invalid channel type.");
511 return sr_scpi_send(sdi->conn, command);
514 static int hmo_check_channels(GSList *channels)
517 struct sr_channel *ch;
518 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
519 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
522 /* Preset "not enabled" for all channels / pods. */
523 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
524 enabled_chan[idx] = FALSE;
525 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
526 enabled_pod[idx] = FALSE;
529 * Determine which channels / pods are required for the caller's
530 * specified configuration.
532 for (l = channels; l; l = l->next) {
535 case SR_CHANNEL_ANALOG:
537 if (idx < ARRAY_SIZE(enabled_chan))
538 enabled_chan[idx] = TRUE;
540 case SR_CHANNEL_LOGIC:
542 if (idx < ARRAY_SIZE(enabled_pod))
543 enabled_pod[idx] = TRUE;
551 * Check for resource conflicts. Some channels can be either
552 * analog or digital, but never both at the same time.
554 * Note that the constraints might depend on the specific model.
555 * These tests might need some adjustment when support for more
556 * models gets added to the driver.
558 if (enabled_pod[0] && enabled_chan[2])
560 if (enabled_pod[1] && enabled_chan[3])
565 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
569 gboolean *pod_enabled, setup_changed;
570 char command[MAX_COMMAND_SIZE];
571 struct scope_state *state;
572 const struct scope_config *model;
573 struct sr_channel *ch;
574 struct dev_context *devc;
575 struct sr_scpi_dev_inst *scpi;
579 state = devc->model_state;
580 model = devc->model_config;
581 setup_changed = FALSE;
583 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
585 for (l = sdi->channels; l; l = l->next) {
588 case SR_CHANNEL_ANALOG:
589 if (ch->enabled == state->analog_channels[ch->index].state)
591 g_snprintf(command, sizeof(command),
592 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
593 ch->index + 1, ch->enabled);
595 if (sr_scpi_send(scpi, command) != SR_OK)
597 state->analog_channels[ch->index].state = ch->enabled;
598 setup_changed = TRUE;
600 case SR_CHANNEL_LOGIC:
602 * A digital POD needs to be enabled for every group of
606 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
608 if (ch->enabled == state->digital_channels[ch->index])
610 g_snprintf(command, sizeof(command),
611 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
612 ch->index, ch->enabled);
614 if (sr_scpi_send(scpi, command) != SR_OK)
617 state->digital_channels[ch->index] = ch->enabled;
618 setup_changed = TRUE;
625 for (i = 1; i <= model->digital_pods; i++) {
626 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
628 g_snprintf(command, sizeof(command),
629 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
630 i, pod_enabled[i - 1]);
631 if (sr_scpi_send(scpi, command) != SR_OK)
633 state->digital_pods[i - 1] = pod_enabled[i - 1];
634 setup_changed = TRUE;
639 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
645 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
648 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
649 size_t group, pod_count;
650 struct sr_channel *ch;
651 struct dev_context *devc;
652 struct sr_scpi_dev_inst *scpi;
658 /* Preset empty results. */
659 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
660 digital_added[group] = FALSE;
661 g_slist_free(devc->enabled_channels);
662 devc->enabled_channels = NULL;
665 * Contruct the list of enabled channels. Determine the highest
666 * number of digital pods involved in the acquisition.
669 for (l = sdi->channels; l; l = l->next) {
673 /* Only add a single digital channel per group (pod). */
674 group = ch->index / 8;
675 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
676 devc->enabled_channels = g_slist_append(
677 devc->enabled_channels, ch);
678 if (ch->type == SR_CHANNEL_LOGIC) {
679 digital_added[group] = TRUE;
680 if (pod_count < group + 1)
681 pod_count = group + 1;
685 if (!devc->enabled_channels)
687 devc->pod_count = pod_count;
688 devc->logic_data = NULL;
691 * Check constraints. Some channels can be either analog or
692 * digital, but not both at the same time.
694 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
695 sr_err("Invalid channel configuration specified!");
701 * Configure the analog and digital channels and the
702 * corresponding digital pods.
704 if (hmo_setup_channels(sdi) != SR_OK) {
705 sr_err("Failed to setup channel configuration!");
711 * Start acquisition on the first enabled channel. The
712 * receive routine will continue driving the acquisition.
714 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
715 hmo_receive_data, (void *)sdi);
717 std_session_send_df_header(sdi);
719 devc->current_channel = devc->enabled_channels;
721 return hmo_request_data(sdi);
724 g_slist_free(devc->enabled_channels);
725 devc->enabled_channels = NULL;
729 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
731 struct dev_context *devc;
732 struct sr_scpi_dev_inst *scpi;
734 std_session_send_df_end(sdi);
738 devc->num_frames = 0;
739 g_slist_free(devc->enabled_channels);
740 devc->enabled_channels = NULL;
742 sr_scpi_source_remove(sdi->session, scpi);
747 static struct sr_dev_driver hameg_hmo_driver_info = {
749 .longname = "Hameg HMO",
752 .cleanup = std_cleanup,
754 .dev_list = std_dev_list,
755 .dev_clear = dev_clear,
756 .config_get = config_get,
757 .config_set = config_set,
758 .config_list = config_list,
759 .dev_open = dev_open,
760 .dev_close = dev_close,
761 .dev_acquisition_start = dev_acquisition_start,
762 .dev_acquisition_stop = dev_acquisition_stop,
765 SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);