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/>.
23 #define SERIALCOMM "115200/8n1/flow=1"
25 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info;
26 static struct sr_dev_driver *di = &hameg_hmo_driver_info;
28 static const char *manufacturers[] = {
32 static const uint32_t drvopts[] = {
36 static const uint32_t scanopts[] = {
48 static int init(struct sr_context *sr_ctx)
50 return std_init(sr_ctx, di, LOG_PREFIX);
53 static int check_manufacturer(const char *manufacturer)
57 for (i = 0; i < ARRAY_SIZE(manufacturers); ++i)
58 if (!strcmp(manufacturer, manufacturers[i]))
64 static struct sr_dev_inst *hmo_probe_serial_device(struct sr_scpi_dev_inst *scpi)
66 struct sr_dev_inst *sdi;
67 struct dev_context *devc;
68 struct sr_scpi_hw_info *hw_info;
74 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
75 sr_info("Couldn't get IDN response.");
79 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
82 sdi = g_malloc0(sizeof(struct sr_dev_inst));
83 sdi->status = SR_ST_ACTIVE;
84 sdi->vendor = g_strdup(hw_info->manufacturer);
85 sdi->model = g_strdup(hw_info->model);
86 sdi->version = g_strdup(hw_info->firmware_version);
87 sdi->serial_num = g_strdup(hw_info->serial_number);
89 sdi->inst_type = SR_INST_SCPI;
92 sr_scpi_hw_info_free(hw_info);
95 devc = g_malloc0(sizeof(struct dev_context));
99 if (hmo_init_device(sdi) != SR_OK)
102 sr_scpi_close(sdi->conn);
104 sdi->status = SR_ST_INACTIVE;
110 sr_scpi_hw_info_free(hw_info);
112 sr_dev_inst_free(sdi);
119 static GSList *scan(GSList *options)
121 return sr_scpi_scan(di->priv, options, hmo_probe_serial_device);
124 static GSList *dev_list(void)
126 return ((struct drv_context *)(di->priv))->instances;
129 static void clear_helper(void *priv)
131 struct dev_context *devc;
135 hmo_scope_state_free(devc->model_state);
137 g_free(devc->analog_groups);
138 g_free(devc->digital_groups);
143 static int dev_clear(void)
145 return std_dev_clear(di, clear_helper);
148 static int dev_open(struct sr_dev_inst *sdi)
150 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
153 if (hmo_scope_state_get(sdi) != SR_OK)
156 sdi->status = SR_ST_ACTIVE;
161 static int dev_close(struct sr_dev_inst *sdi)
163 if (sdi->status == SR_ST_INACTIVE)
166 sr_scpi_close(sdi->conn);
168 sdi->status = SR_ST_INACTIVE;
173 static int cleanup(void)
180 static int check_channel_group(struct dev_context *devc,
181 const struct sr_channel_group *cg)
184 struct scope_config *model;
186 model = devc->model_config;
191 for (i = 0; i < model->analog_channels; ++i)
192 if (cg == devc->analog_groups[i])
195 for (i = 0; i < model->digital_pods; ++i)
196 if (cg == devc->digital_groups[i])
199 sr_err("Invalid channel group specified.");
204 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
205 const struct sr_channel_group *cg)
209 struct dev_context *devc;
210 struct scope_config *model;
211 struct scope_state *state;
213 if (!sdi || !(devc = sdi->priv))
216 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
220 model = devc->model_config;
221 state = devc->model_state;
224 case SR_CONF_NUM_HDIV:
225 *data = g_variant_new_int32(model->num_xdivs);
228 case SR_CONF_TIMEBASE:
229 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
230 (*model->timebases)[state->timebase][1]);
233 case SR_CONF_NUM_VDIV:
234 if (cg_type == CG_NONE) {
235 sr_err("No channel group specified.");
236 return SR_ERR_CHANNEL_GROUP;
237 } else if (cg_type == CG_ANALOG) {
238 for (i = 0; i < model->analog_channels; ++i) {
239 if (cg != devc->analog_groups[i])
241 *data = g_variant_new_int32(model->num_ydivs);
251 if (cg_type == CG_NONE) {
252 sr_err("No channel group specified.");
253 return SR_ERR_CHANNEL_GROUP;
254 } else if (cg_type == CG_ANALOG) {
255 for (i = 0; i < model->analog_channels; ++i) {
256 if (cg != devc->analog_groups[i])
258 *data = g_variant_new("(tt)",
259 (*model->vdivs)[state->analog_channels[i].vdiv][0],
260 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
269 case SR_CONF_TRIGGER_SOURCE:
270 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
273 case SR_CONF_TRIGGER_SLOPE:
274 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
277 case SR_CONF_HORIZ_TRIGGERPOS:
278 *data = g_variant_new_double(state->horiz_triggerpos);
281 case SR_CONF_COUPLING:
282 if (cg_type == CG_NONE) {
283 sr_err("No channel group specified.");
284 return SR_ERR_CHANNEL_GROUP;
285 } else if (cg_type == CG_ANALOG) {
286 for (i = 0; i < model->analog_channels; ++i) {
287 if (cg != devc->analog_groups[i])
289 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
298 case SR_CONF_SAMPLERATE:
299 *data = g_variant_new_uint64(state->sample_rate);
309 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
312 GVariant *rational[2];
315 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
317 for (i = 0; i < n; i++) {
318 rational[0] = g_variant_new_uint64((*array)[i][0]);
319 rational[1] = g_variant_new_uint64((*array)[i][1]);
321 /* FIXME: Valgrind reports a memory leak here. */
322 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
325 return g_variant_builder_end(&gvb);
328 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
329 const struct sr_channel_group *cg)
333 char command[MAX_COMMAND_SIZE], float_str[30];
334 struct dev_context *devc;
335 struct scope_config *model;
336 struct scope_state *state;
340 gboolean update_sample_rate;
342 if (!sdi || !(devc = sdi->priv))
345 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
348 model = devc->model_config;
349 state = devc->model_state;
350 update_sample_rate = FALSE;
355 case SR_CONF_LIMIT_FRAMES:
356 devc->frame_limit = g_variant_get_uint64(data);
359 case SR_CONF_TRIGGER_SOURCE:
360 tmp = g_variant_get_string(data, NULL);
361 for (i = 0; (*model->trigger_sources)[i]; i++) {
362 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
364 state->trigger_source = i;
365 g_snprintf(command, sizeof(command),
366 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
367 (*model->trigger_sources)[i]);
369 ret = sr_scpi_send(sdi->conn, command);
374 if (cg_type == CG_NONE) {
375 sr_err("No channel group specified.");
376 return SR_ERR_CHANNEL_GROUP;
379 g_variant_get(data, "(tt)", &p, &q);
381 for (i = 0; i < model->num_vdivs; i++) {
382 if (p != (*model->vdivs)[i][0] ||
383 q != (*model->vdivs)[i][1])
385 for (j = 1; j <= model->analog_channels; ++j) {
386 if (cg != devc->analog_groups[j - 1])
388 state->analog_channels[j - 1].vdiv = i;
389 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
390 g_snprintf(command, sizeof(command),
391 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
394 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
395 sr_scpi_get_opc(sdi->conn) != SR_OK)
405 case SR_CONF_TIMEBASE:
406 g_variant_get(data, "(tt)", &p, &q);
408 for (i = 0; i < model->num_timebases; i++) {
409 if (p != (*model->timebases)[i][0] ||
410 q != (*model->timebases)[i][1])
413 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
414 g_snprintf(command, sizeof(command),
415 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
418 ret = sr_scpi_send(sdi->conn, command);
419 update_sample_rate = TRUE;
423 case SR_CONF_HORIZ_TRIGGERPOS:
424 tmp_d = g_variant_get_double(data);
426 if (tmp_d < 0.0 || tmp_d > 1.0)
429 state->horiz_triggerpos = tmp_d;
430 tmp_d = -(tmp_d - 0.5) *
431 ((double) (*model->timebases)[state->timebase][0] /
432 (*model->timebases)[state->timebase][1])
435 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
436 g_snprintf(command, sizeof(command),
437 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
440 ret = sr_scpi_send(sdi->conn, command);
442 case SR_CONF_TRIGGER_SLOPE:
443 tmp = g_variant_get_string(data, NULL);
445 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
448 state->trigger_slope = (tmp[0] == 'r') ? 0 : 1;
450 g_snprintf(command, sizeof(command),
451 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
452 (state->trigger_slope == 0) ? "POS" : "NEG");
454 ret = sr_scpi_send(sdi->conn, command);
456 case SR_CONF_COUPLING:
457 if (cg_type == CG_NONE) {
458 sr_err("No channel group specified.");
459 return SR_ERR_CHANNEL_GROUP;
462 tmp = g_variant_get_string(data, NULL);
464 for (i = 0; (*model->coupling_options)[i]; i++) {
465 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
467 for (j = 1; j <= model->analog_channels; ++j) {
468 if (cg != devc->analog_groups[j - 1])
470 state->analog_channels[j-1].coupling = i;
472 g_snprintf(command, sizeof(command),
473 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
476 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
477 sr_scpi_get_opc(sdi->conn) != SR_OK)
492 ret = sr_scpi_get_opc(sdi->conn);
494 if (ret == SR_OK && update_sample_rate)
495 ret = hmo_update_sample_rate(sdi);
500 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
501 const struct sr_channel_group *cg)
503 int cg_type = CG_NONE;
504 struct dev_context *devc = NULL;
505 struct scope_config *model = NULL;
507 if (sdi && (devc = sdi->priv)) {
508 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
511 model = devc->model_config;
515 case SR_CONF_SCAN_OPTIONS:
516 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
517 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
519 case SR_CONF_DEVICE_OPTIONS:
520 if (cg_type == CG_NONE) {
522 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
523 model->devopts, model->num_devopts, sizeof(uint32_t));
525 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
526 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
527 } else if (cg_type == CG_ANALOG) {
528 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
529 model->analog_devopts, model->num_analog_devopts,
532 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
533 NULL, 0, sizeof(uint32_t));
536 case SR_CONF_COUPLING:
537 if (cg_type == CG_NONE)
538 return SR_ERR_CHANNEL_GROUP;
539 *data = g_variant_new_strv(*model->coupling_options,
540 g_strv_length((char **)*model->coupling_options));
542 case SR_CONF_TRIGGER_SOURCE:
545 *data = g_variant_new_strv(*model->trigger_sources,
546 g_strv_length((char **)*model->trigger_sources));
548 case SR_CONF_TRIGGER_SLOPE:
551 *data = g_variant_new_strv(*model->trigger_slopes,
552 g_strv_length((char **)*model->trigger_slopes));
554 case SR_CONF_TIMEBASE:
557 *data = build_tuples(model->timebases, model->num_timebases);
560 if (cg_type == CG_NONE)
561 return SR_ERR_CHANNEL_GROUP;
562 *data = build_tuples(model->vdivs, model->num_vdivs);
571 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
573 char command[MAX_COMMAND_SIZE];
574 struct sr_channel *ch;
575 struct dev_context *devc;
576 struct scope_config *model;
579 model = devc->model_config;
581 ch = devc->current_channel->data;
584 case SR_CHANNEL_ANALOG:
585 g_snprintf(command, sizeof(command),
586 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
589 case SR_CHANNEL_LOGIC:
590 g_snprintf(command, sizeof(command),
591 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
592 ch->index < 8 ? 1 : 2);
595 sr_err("Invalid channel type.");
599 return sr_scpi_send(sdi->conn, command);
602 static int hmo_check_channels(GSList *channels)
605 struct sr_channel *ch;
606 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
608 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
610 for (l = channels; l; l = l->next) {
613 case SR_CHANNEL_ANALOG:
615 enabled_chan3 = TRUE;
616 else if (ch->index == 3)
617 enabled_chan4 = TRUE;
619 case SR_CHANNEL_LOGIC:
630 if ((enabled_pod1 && enabled_chan3) ||
631 (enabled_pod2 && enabled_chan4))
637 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
641 gboolean *pod_enabled, setup_changed;
642 char command[MAX_COMMAND_SIZE];
643 struct scope_state *state;
644 struct scope_config *model;
645 struct sr_channel *ch;
646 struct dev_context *devc;
647 struct sr_scpi_dev_inst *scpi;
651 state = devc->model_state;
652 model = devc->model_config;
653 setup_changed = FALSE;
655 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
657 for (l = sdi->channels; l; l = l->next) {
660 case SR_CHANNEL_ANALOG:
661 if (ch->enabled == state->analog_channels[ch->index].state)
663 g_snprintf(command, sizeof(command),
664 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
665 ch->index + 1, ch->enabled);
667 if (sr_scpi_send(scpi, command) != SR_OK)
669 state->analog_channels[ch->index].state = ch->enabled;
670 setup_changed = TRUE;
672 case SR_CHANNEL_LOGIC:
674 * A digital POD needs to be enabled for every group of
678 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
680 if (ch->enabled == state->digital_channels[ch->index])
682 g_snprintf(command, sizeof(command),
683 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
684 ch->index, ch->enabled);
686 if (sr_scpi_send(scpi, command) != SR_OK)
689 state->digital_channels[ch->index] = ch->enabled;
690 setup_changed = TRUE;
697 for (i = 1; i <= model->digital_pods; ++i) {
698 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
700 g_snprintf(command, sizeof(command),
701 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
702 i, pod_enabled[i - 1]);
703 if (sr_scpi_send(scpi, command) != SR_OK)
705 state->digital_pods[i - 1] = pod_enabled[i - 1];
706 setup_changed = TRUE;
711 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
717 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
720 gboolean digital_added;
721 struct sr_channel *ch;
722 struct dev_context *devc;
723 struct sr_scpi_dev_inst *scpi;
725 if (sdi->status != SR_ST_ACTIVE)
726 return SR_ERR_DEV_CLOSED;
730 digital_added = FALSE;
732 g_slist_free(devc->enabled_channels);
733 devc->enabled_channels = NULL;
735 for (l = sdi->channels; l; l = l->next) {
739 /* Only add a single digital channel. */
740 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
741 devc->enabled_channels = g_slist_append(
742 devc->enabled_channels, ch);
743 if (ch->type == SR_CHANNEL_LOGIC)
744 digital_added = TRUE;
748 if (!devc->enabled_channels)
751 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
752 sr_err("Invalid channel configuration specified!");
756 if (hmo_setup_channels(sdi) != SR_OK) {
757 sr_err("Failed to setup channel configuration!");
761 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
762 hmo_receive_data, (void *)sdi);
764 /* Send header packet to the session bus. */
765 std_session_send_df_header(cb_data, LOG_PREFIX);
767 devc->current_channel = devc->enabled_channels;
769 return hmo_request_data(sdi);
772 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
774 struct dev_context *devc;
775 struct sr_scpi_dev_inst *scpi;
776 struct sr_datafeed_packet packet;
780 packet.type = SR_DF_END;
781 packet.payload = NULL;
782 sr_session_send(sdi, &packet);
784 if (sdi->status != SR_ST_ACTIVE)
785 return SR_ERR_DEV_CLOSED;
789 devc->num_frames = 0;
790 g_slist_free(devc->enabled_channels);
791 devc->enabled_channels = NULL;
793 sr_scpi_source_remove(sdi->session, scpi);
798 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
800 .longname = "Hameg HMO",
805 .dev_list = dev_list,
806 .dev_clear = dev_clear,
807 .config_get = config_get,
808 .config_set = config_set,
809 .config_list = config_list,
810 .dev_open = dev_open,
811 .dev_close = dev_close,
812 .dev_acquisition_start = dev_acquisition_start,
813 .dev_acquisition_stop = dev_acquisition_stop,