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 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(struct dev_context *devc)
115 hmo_scope_state_free(devc->model_state);
116 g_free(devc->analog_groups);
117 g_free(devc->digital_groups);
120 static int dev_clear(const struct sr_dev_driver *di)
122 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
125 static int dev_open(struct sr_dev_inst *sdi)
127 if (sr_scpi_open(sdi->conn) != SR_OK)
130 if (hmo_scope_state_get(sdi) != SR_OK)
136 static int dev_close(struct sr_dev_inst *sdi)
138 return sr_scpi_close(sdi->conn);
141 static int check_channel_group(struct dev_context *devc,
142 const struct sr_channel_group *cg)
145 const struct scope_config *model;
147 model = devc->model_config;
152 for (i = 0; i < model->analog_channels; i++)
153 if (cg == devc->analog_groups[i])
156 for (i = 0; i < model->digital_pods; i++)
157 if (cg == devc->digital_groups[i])
160 sr_err("Invalid channel group specified.");
165 static int config_get(uint32_t key, GVariant **data,
166 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
170 struct dev_context *devc;
171 const struct scope_config *model;
172 struct scope_state *state;
179 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
183 model = devc->model_config;
184 state = devc->model_state;
187 case SR_CONF_NUM_HDIV:
188 *data = g_variant_new_int32(model->num_xdivs);
191 case SR_CONF_TIMEBASE:
192 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
193 (*model->timebases)[state->timebase][1]);
196 case SR_CONF_NUM_VDIV:
197 if (cg_type == CG_NONE) {
198 return SR_ERR_CHANNEL_GROUP;
199 } else if (cg_type == CG_ANALOG) {
200 for (i = 0; i < model->analog_channels; i++) {
201 if (cg != devc->analog_groups[i])
203 *data = g_variant_new_int32(model->num_ydivs);
213 if (cg_type == CG_NONE) {
214 return SR_ERR_CHANNEL_GROUP;
215 } else if (cg_type == CG_ANALOG) {
216 for (i = 0; i < model->analog_channels; i++) {
217 if (cg != devc->analog_groups[i])
219 *data = g_variant_new("(tt)",
220 (*model->vdivs)[state->analog_channels[i].vdiv][0],
221 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
230 case SR_CONF_TRIGGER_SOURCE:
231 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
234 case SR_CONF_TRIGGER_SLOPE:
235 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
238 case SR_CONF_HORIZ_TRIGGERPOS:
239 *data = g_variant_new_double(state->horiz_triggerpos);
242 case SR_CONF_COUPLING:
243 if (cg_type == CG_NONE) {
244 return SR_ERR_CHANNEL_GROUP;
245 } else if (cg_type == CG_ANALOG) {
246 for (i = 0; i < model->analog_channels; i++) {
247 if (cg != devc->analog_groups[i])
249 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
258 case SR_CONF_SAMPLERATE:
259 *data = g_variant_new_uint64(state->sample_rate);
269 static int config_set(uint32_t key, GVariant *data,
270 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
274 char command[MAX_COMMAND_SIZE], float_str[30];
275 struct dev_context *devc;
276 const struct scope_config *model;
277 struct scope_state *state;
281 gboolean update_sample_rate;
288 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
291 model = devc->model_config;
292 state = devc->model_state;
293 update_sample_rate = FALSE;
298 case SR_CONF_LIMIT_FRAMES:
299 devc->frame_limit = g_variant_get_uint64(data);
302 case SR_CONF_TRIGGER_SOURCE:
303 tmp = g_variant_get_string(data, NULL);
304 for (i = 0; (*model->trigger_sources)[i]; i++) {
305 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
307 state->trigger_source = i;
308 g_snprintf(command, sizeof(command),
309 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
310 (*model->trigger_sources)[i]);
312 ret = sr_scpi_send(sdi->conn, command);
317 if (cg_type == CG_NONE)
318 return SR_ERR_CHANNEL_GROUP;
320 g_variant_get(data, "(tt)", &p, &q);
322 for (i = 0; i < model->num_vdivs; i++) {
323 if (p != (*model->vdivs)[i][0] ||
324 q != (*model->vdivs)[i][1])
326 for (j = 1; j <= model->analog_channels; j++) {
327 if (cg != devc->analog_groups[j - 1])
329 state->analog_channels[j - 1].vdiv = i;
330 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
331 g_snprintf(command, sizeof(command),
332 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
335 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
336 sr_scpi_get_opc(sdi->conn) != SR_OK)
346 case SR_CONF_TIMEBASE:
347 g_variant_get(data, "(tt)", &p, &q);
349 for (i = 0; i < model->num_timebases; i++) {
350 if (p != (*model->timebases)[i][0] ||
351 q != (*model->timebases)[i][1])
354 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
355 g_snprintf(command, sizeof(command),
356 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
359 ret = sr_scpi_send(sdi->conn, command);
360 update_sample_rate = TRUE;
364 case SR_CONF_HORIZ_TRIGGERPOS:
365 tmp_d = g_variant_get_double(data);
367 if (tmp_d < 0.0 || tmp_d > 1.0)
370 state->horiz_triggerpos = tmp_d;
371 tmp_d = -(tmp_d - 0.5) *
372 ((double) (*model->timebases)[state->timebase][0] /
373 (*model->timebases)[state->timebase][1])
376 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
377 g_snprintf(command, sizeof(command),
378 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
381 ret = sr_scpi_send(sdi->conn, command);
383 case SR_CONF_TRIGGER_SLOPE:
384 tmp = g_variant_get_string(data, NULL);
385 for (i = 0; (*model->trigger_slopes)[i]; i++) {
386 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
388 state->trigger_slope = i;
389 g_snprintf(command, sizeof(command),
390 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
391 (*model->trigger_slopes)[i]);
393 ret = sr_scpi_send(sdi->conn, command);
397 case SR_CONF_COUPLING:
398 if (cg_type == CG_NONE)
399 return SR_ERR_CHANNEL_GROUP;
401 tmp = g_variant_get_string(data, NULL);
403 for (i = 0; (*model->coupling_options)[i]; i++) {
404 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
406 for (j = 1; j <= model->analog_channels; j++) {
407 if (cg != devc->analog_groups[j - 1])
409 state->analog_channels[j-1].coupling = i;
411 g_snprintf(command, sizeof(command),
412 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
415 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
416 sr_scpi_get_opc(sdi->conn) != SR_OK)
431 ret = sr_scpi_get_opc(sdi->conn);
433 if (ret == SR_OK && update_sample_rate)
434 ret = hmo_update_sample_rate(sdi);
439 static int config_list(uint32_t key, GVariant **data,
440 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
442 int cg_type = CG_NONE;
443 struct dev_context *devc = NULL;
444 const struct scope_config *model = NULL;
448 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
451 model = devc->model_config;
455 case SR_CONF_SCAN_OPTIONS:
456 *data = std_gvar_array_u32(ARRAY_AND_SIZE(scanopts));
458 case SR_CONF_DEVICE_OPTIONS:
459 if (cg_type == CG_NONE) {
461 *data = std_gvar_array_u32((const uint32_t *)model->devopts, model->num_devopts);
463 *data = std_gvar_array_u32(ARRAY_AND_SIZE(drvopts));
464 } else if (cg_type == CG_ANALOG) {
465 *data = std_gvar_array_u32((const uint32_t *)model->devopts_cg_analog, model->num_devopts_cg_analog);
467 *data = std_gvar_array_u32(NULL, 0);
470 case SR_CONF_COUPLING:
471 if (cg_type == CG_NONE)
472 return SR_ERR_CHANNEL_GROUP;
473 *data = g_variant_new_strv(*model->coupling_options,
474 g_strv_length((char **)*model->coupling_options));
476 case SR_CONF_TRIGGER_SOURCE:
479 *data = g_variant_new_strv(*model->trigger_sources,
480 g_strv_length((char **)*model->trigger_sources));
482 case SR_CONF_TRIGGER_SLOPE:
485 *data = g_variant_new_strv(*model->trigger_slopes,
486 g_strv_length((char **)*model->trigger_slopes));
488 case SR_CONF_TIMEBASE:
491 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
494 if (cg_type == CG_NONE)
495 return SR_ERR_CHANNEL_GROUP;
496 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
505 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
507 char command[MAX_COMMAND_SIZE];
508 struct sr_channel *ch;
509 struct dev_context *devc;
510 const struct scope_config *model;
513 model = devc->model_config;
515 ch = devc->current_channel->data;
518 case SR_CHANNEL_ANALOG:
519 g_snprintf(command, sizeof(command),
520 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
521 #ifdef WORDS_BIGENDIAN
528 case SR_CHANNEL_LOGIC:
529 g_snprintf(command, sizeof(command),
530 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
531 ch->index < 8 ? 1 : 2);
534 sr_err("Invalid channel type.");
538 return sr_scpi_send(sdi->conn, command);
541 static int hmo_check_channels(GSList *channels)
544 struct sr_channel *ch;
545 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
546 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
549 /* Preset "not enabled" for all channels / pods. */
550 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
551 enabled_chan[idx] = FALSE;
552 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
553 enabled_pod[idx] = FALSE;
556 * Determine which channels / pods are required for the caller's
557 * specified configuration.
559 for (l = channels; l; l = l->next) {
562 case SR_CHANNEL_ANALOG:
564 if (idx < ARRAY_SIZE(enabled_chan))
565 enabled_chan[idx] = TRUE;
567 case SR_CHANNEL_LOGIC:
569 if (idx < ARRAY_SIZE(enabled_pod))
570 enabled_pod[idx] = TRUE;
578 * Check for resource conflicts. Some channels can be either
579 * analog or digital, but never both at the same time.
581 * Note that the constraints might depend on the specific model.
582 * These tests might need some adjustment when support for more
583 * models gets added to the driver.
585 if (enabled_pod[0] && enabled_chan[2])
587 if (enabled_pod[1] && enabled_chan[3])
592 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
596 gboolean *pod_enabled, setup_changed;
597 char command[MAX_COMMAND_SIZE];
598 struct scope_state *state;
599 const struct scope_config *model;
600 struct sr_channel *ch;
601 struct dev_context *devc;
602 struct sr_scpi_dev_inst *scpi;
606 state = devc->model_state;
607 model = devc->model_config;
608 setup_changed = FALSE;
610 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
612 for (l = sdi->channels; l; l = l->next) {
615 case SR_CHANNEL_ANALOG:
616 if (ch->enabled == state->analog_channels[ch->index].state)
618 g_snprintf(command, sizeof(command),
619 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
620 ch->index + 1, ch->enabled);
622 if (sr_scpi_send(scpi, command) != SR_OK)
624 state->analog_channels[ch->index].state = ch->enabled;
625 setup_changed = TRUE;
627 case SR_CHANNEL_LOGIC:
629 * A digital POD needs to be enabled for every group of
633 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
635 if (ch->enabled == state->digital_channels[ch->index])
637 g_snprintf(command, sizeof(command),
638 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
639 ch->index, ch->enabled);
641 if (sr_scpi_send(scpi, command) != SR_OK)
644 state->digital_channels[ch->index] = ch->enabled;
645 setup_changed = TRUE;
652 for (i = 1; i <= model->digital_pods; i++) {
653 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
655 g_snprintf(command, sizeof(command),
656 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
657 i, pod_enabled[i - 1]);
658 if (sr_scpi_send(scpi, command) != SR_OK)
660 state->digital_pods[i - 1] = pod_enabled[i - 1];
661 setup_changed = TRUE;
666 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
672 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
675 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
676 size_t group, pod_count;
677 struct sr_channel *ch;
678 struct dev_context *devc;
679 struct sr_scpi_dev_inst *scpi;
685 /* Preset empty results. */
686 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
687 digital_added[group] = FALSE;
688 g_slist_free(devc->enabled_channels);
689 devc->enabled_channels = NULL;
692 * Contruct the list of enabled channels. Determine the highest
693 * number of digital pods involved in the acquisition.
696 for (l = sdi->channels; l; l = l->next) {
700 /* Only add a single digital channel per group (pod). */
701 group = ch->index / 8;
702 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
703 devc->enabled_channels = g_slist_append(
704 devc->enabled_channels, ch);
705 if (ch->type == SR_CHANNEL_LOGIC) {
706 digital_added[group] = TRUE;
707 if (pod_count < group + 1)
708 pod_count = group + 1;
712 if (!devc->enabled_channels)
714 devc->pod_count = pod_count;
715 devc->logic_data = NULL;
718 * Check constraints. Some channels can be either analog or
719 * digital, but not both at the same time.
721 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
722 sr_err("Invalid channel configuration specified!");
728 * Configure the analog and digital channels and the
729 * corresponding digital pods.
731 if (hmo_setup_channels(sdi) != SR_OK) {
732 sr_err("Failed to setup channel configuration!");
738 * Start acquisition on the first enabled channel. The
739 * receive routine will continue driving the acquisition.
741 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
742 hmo_receive_data, (void *)sdi);
744 std_session_send_df_header(sdi);
746 devc->current_channel = devc->enabled_channels;
748 return hmo_request_data(sdi);
751 g_slist_free(devc->enabled_channels);
752 devc->enabled_channels = NULL;
756 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
758 struct dev_context *devc;
759 struct sr_scpi_dev_inst *scpi;
761 std_session_send_df_end(sdi);
765 devc->num_frames = 0;
766 g_slist_free(devc->enabled_channels);
767 devc->enabled_channels = NULL;
769 sr_scpi_source_remove(sdi->session, scpi);
774 static struct sr_dev_driver hameg_hmo_driver_info = {
776 .longname = "Hameg HMO",
779 .cleanup = std_cleanup,
781 .dev_list = std_dev_list,
782 .dev_clear = dev_clear,
783 .config_get = config_get,
784 .config_set = config_set,
785 .config_list = config_list,
786 .dev_open = dev_open,
787 .dev_close = dev_close,
788 .dev_acquisition_start = dev_acquisition_start,
789 .dev_acquisition_stop = dev_acquisition_stop,
792 SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);