2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 abraxa (Soeren Apel) <soeren@apelpie.net>
5 * Based on the Hameg HMO driver by poljar (Damir Jelić) <poljarinho@gmail.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/>.
24 SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info;
26 static char *MANUFACTURER_ID = "YOKOGAWA";
27 static char *MANUFACTURER_NAME = "Yokogawa";
29 static const uint32_t drvopts[] = {
30 SR_CONF_LOGIC_ANALYZER,
41 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
43 return std_init(sr_ctx, di, LOG_PREFIX);
46 static struct sr_dev_inst *probe_usbtmc_device(struct sr_scpi_dev_inst *scpi)
48 struct sr_dev_inst *sdi;
49 struct dev_context *devc;
50 struct sr_scpi_hw_info *hw_info;
58 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
59 sr_info("Couldn't get IDN response.");
63 if (strcmp(hw_info->manufacturer, MANUFACTURER_ID) != 0)
66 if (dlm_model_get(hw_info->model, &model_name, &model_index) != SR_OK)
69 sdi = g_malloc0(sizeof(struct sr_dev_inst));
70 sdi->status = SR_ST_ACTIVE;
71 sdi->vendor = g_strdup(MANUFACTURER_NAME);
72 sdi->model = g_strdup(model_name);
73 sdi->version = g_strdup(hw_info->firmware_version);
75 sdi->serial_num = g_strdup(hw_info->serial_number);
77 sr_scpi_hw_info_free(hw_info);
80 devc = g_malloc0(sizeof(struct dev_context));
82 sdi->driver = &yokogawa_dlm_driver_info;
84 sdi->inst_type = SR_INST_SCPI;
87 if (dlm_device_init(sdi, model_index) != SR_OK)
90 sr_scpi_close(sdi->conn);
92 sdi->status = SR_ST_INACTIVE;
97 sr_scpi_hw_info_free(hw_info);
99 sr_dev_inst_free(sdi);
106 static GSList *scan(struct sr_dev_driver *di, GSList *options)
108 return sr_scpi_scan(di->priv, options, probe_usbtmc_device);
111 static GSList *dev_list(const struct sr_dev_driver *di)
113 return ((struct drv_context *)(di->priv))->instances;
116 static void clear_helper(void *priv)
118 struct dev_context *devc;
122 dlm_scope_state_destroy(devc->model_state);
124 g_free(devc->analog_groups);
125 g_free(devc->digital_groups);
129 static int dev_clear(const struct sr_dev_driver *di)
131 return std_dev_clear(di, clear_helper);
134 static int dev_open(struct sr_dev_inst *sdi)
136 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
139 if (dlm_scope_state_query(sdi) != SR_OK)
142 sdi->status = SR_ST_ACTIVE;
147 static int dev_close(struct sr_dev_inst *sdi)
149 if (sdi->status == SR_ST_INACTIVE)
152 sr_scpi_close(sdi->conn);
154 sdi->status = SR_ST_INACTIVE;
159 static int cleanup(const struct sr_dev_driver *di)
167 * Check which category a given channel group belongs to.
169 * @param devc Our internal device context.
170 * @param cg The channel group to check.
172 * @retval CG_NONE cg is NULL
173 * @retval CG_ANALOG cg is an analog group
174 * @retval CG_DIGITAL cg is a digital group
175 * @retval CG_INVALID cg is something else
177 static int check_channel_group(struct dev_context *devc,
178 const struct sr_channel_group *cg)
181 struct scope_config *model;
183 model = devc->model_config;
188 for (i = 0; i < model->analog_channels; ++i)
189 if (cg == devc->analog_groups[i])
192 for (i = 0; i < model->pods; ++i)
193 if (cg == devc->digital_groups[i])
196 sr_err("Invalid channel group specified.");
200 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
201 const struct sr_channel_group *cg)
205 struct dev_context *devc;
206 struct scope_config *model;
207 struct scope_state *state;
209 if (!sdi || !(devc = sdi->priv))
212 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
216 model = devc->model_config;
217 state = devc->model_state;
220 case SR_CONF_NUM_HDIV:
221 *data = g_variant_new_int32(model->num_xdivs);
224 case SR_CONF_TIMEBASE:
225 *data = g_variant_new("(tt)",
226 (*model->timebases)[state->timebase][0],
227 (*model->timebases)[state->timebase][1]);
230 case SR_CONF_NUM_VDIV:
231 if (cg_type == CG_NONE) {
232 sr_err("No channel group specified.");
233 return SR_ERR_CHANNEL_GROUP;
234 } else if (cg_type == CG_ANALOG) {
235 *data = g_variant_new_int32(model->num_ydivs);
244 if (cg_type == CG_NONE) {
245 sr_err("No channel group specified.");
246 return SR_ERR_CHANNEL_GROUP;
247 } else if (cg_type != CG_ANALOG)
250 for (i = 0; i < model->analog_channels; ++i) {
251 if (cg != devc->analog_groups[i])
253 *data = g_variant_new("(tt)",
254 (*model->vdivs)[state->analog_states[i].vdiv][0],
255 (*model->vdivs)[state->analog_states[i].vdiv][1]);
260 case SR_CONF_TRIGGER_SOURCE:
261 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
264 case SR_CONF_TRIGGER_SLOPE:
265 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
268 case SR_CONF_HORIZ_TRIGGERPOS:
269 *data = g_variant_new_double(state->horiz_triggerpos);
272 case SR_CONF_COUPLING:
274 if (cg_type == CG_NONE) {
275 sr_err("No channel group specified.");
276 return SR_ERR_CHANNEL_GROUP;
277 } else if (cg_type != CG_ANALOG)
280 for (i = 0; i < model->analog_channels; ++i) {
281 if (cg != devc->analog_groups[i])
283 *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
288 case SR_CONF_SAMPLERATE:
289 *data = g_variant_new_uint64(state->sample_rate);
299 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
302 GVariant *rational[2];
305 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
307 for (i = 0; i < n; i++) {
308 rational[0] = g_variant_new_uint64((*array)[i][0]);
309 rational[1] = g_variant_new_uint64((*array)[i][1]);
311 /* FIXME: Valgrind reports a memory leak here. */
312 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
315 return g_variant_builder_end(&gvb);
318 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
319 const struct sr_channel_group *cg)
324 struct dev_context *devc;
325 struct scope_config *model;
326 struct scope_state *state;
330 gboolean update_sample_rate;
332 if (!sdi || !(devc = sdi->priv))
335 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
338 model = devc->model_config;
339 state = devc->model_state;
340 update_sample_rate = FALSE;
345 case SR_CONF_LIMIT_FRAMES:
346 devc->frame_limit = g_variant_get_uint64(data);
349 case SR_CONF_TRIGGER_SOURCE:
350 tmp = g_variant_get_string(data, NULL);
351 for (i = 0; (*model->trigger_sources)[i]; i++) {
352 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
354 state->trigger_source = i;
355 /* TODO: A and B trigger support possible? */
356 ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
361 if (cg_type == CG_NONE) {
362 sr_err("No channel group specified.");
363 return SR_ERR_CHANNEL_GROUP;
366 g_variant_get(data, "(tt)", &p, &q);
368 for (i = 0; i < model->num_vdivs; i++) {
369 if (p != (*model->vdivs)[i][0] ||
370 q != (*model->vdivs)[i][1])
372 for (j = 1; j <= model->analog_channels; ++j) {
373 if (cg != devc->analog_groups[j - 1])
375 state->analog_states[j - 1].vdiv = i;
376 g_ascii_formatd(float_str, sizeof(float_str),
377 "%E", (float) p / q);
378 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
379 sr_scpi_get_opc(sdi->conn) != SR_OK)
389 case SR_CONF_TIMEBASE:
390 g_variant_get(data, "(tt)", &p, &q);
392 for (i = 0; i < model->num_timebases; i++) {
393 if (p != (*model->timebases)[i][0] ||
394 q != (*model->timebases)[i][1])
397 g_ascii_formatd(float_str, sizeof(float_str),
398 "%E", (float) p / q);
399 ret = dlm_timebase_set(sdi->conn, float_str);
400 update_sample_rate = TRUE;
404 case SR_CONF_HORIZ_TRIGGERPOS:
405 tmp_d = g_variant_get_double(data);
407 /* TODO: Check if the calculation makes sense for the DLM. */
408 if (tmp_d < 0.0 || tmp_d > 1.0)
411 state->horiz_triggerpos = tmp_d;
412 tmp_d = -(tmp_d - 0.5) *
413 ((double) (*model->timebases)[state->timebase][0] /
414 (*model->timebases)[state->timebase][1])
417 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
418 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
420 case SR_CONF_TRIGGER_SLOPE:
421 tmp = g_variant_get_string(data, NULL);
423 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
426 /* Note: See dlm_trigger_slopes[] in protocol.c. */
427 state->trigger_slope = (tmp[0] == 'r') ?
428 SLOPE_POSITIVE : SLOPE_NEGATIVE;
430 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
432 case SR_CONF_COUPLING:
433 if (cg_type == CG_NONE) {
434 sr_err("No channel group specified.");
435 return SR_ERR_CHANNEL_GROUP;
438 tmp = g_variant_get_string(data, NULL);
440 for (i = 0; (*model->coupling_options)[i]; i++) {
441 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
443 for (j = 1; j <= model->analog_channels; ++j) {
444 if (cg != devc->analog_groups[j - 1])
446 state->analog_states[j-1].coupling = i;
448 if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
449 sr_scpi_get_opc(sdi->conn) != SR_OK)
464 ret = sr_scpi_get_opc(sdi->conn);
466 if (ret == SR_OK && update_sample_rate)
467 ret = dlm_sample_rate_query(sdi);
472 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
473 const struct sr_channel_group *cg)
475 int cg_type = CG_NONE;
476 struct dev_context *devc = NULL;
477 struct scope_config *model = NULL;
479 if (sdi && (devc = sdi->priv)) {
480 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
483 model = devc->model_config;
487 case SR_CONF_DEVICE_OPTIONS:
488 if (cg_type == CG_NONE) {
490 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
491 model->devopts, model->num_devopts, sizeof(uint32_t));
493 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
494 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
495 } else if (cg_type == CG_ANALOG) {
496 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
497 model->analog_devopts, model->num_analog_devopts, sizeof(uint32_t));
499 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
500 NULL, 0, sizeof(uint32_t));
503 case SR_CONF_COUPLING:
504 if (cg_type == CG_NONE)
505 return SR_ERR_CHANNEL_GROUP;
506 *data = g_variant_new_strv(*model->coupling_options,
507 g_strv_length((char **)*model->coupling_options));
509 case SR_CONF_TRIGGER_SOURCE:
512 *data = g_variant_new_strv(*model->trigger_sources,
513 g_strv_length((char **)*model->trigger_sources));
515 case SR_CONF_TRIGGER_SLOPE:
518 *data = g_variant_new_strv(*model->trigger_slopes,
519 g_strv_length((char **)*model->trigger_slopes));
521 case SR_CONF_TIMEBASE:
524 *data = build_tuples(model->timebases, model->num_timebases);
527 if (cg_type == CG_NONE)
528 return SR_ERR_CHANNEL_GROUP;
529 *data = build_tuples(model->vdivs, model->num_vdivs);
538 static int dlm_check_channels(GSList *channels)
541 struct sr_channel *ch;
542 gboolean enabled_pod1, enabled_chan4;
544 enabled_pod1 = enabled_chan4 = FALSE;
546 /* Note: On the DLM2000, CH4 and Logic are shared. */
547 /* TODO Handle non-DLM2000 models. */
548 for (l = channels; l; l = l->next) {
551 case SR_CHANNEL_ANALOG:
553 enabled_chan4 = TRUE;
555 case SR_CHANNEL_LOGIC:
563 if (enabled_pod1 && enabled_chan4)
569 static int dlm_setup_channels(const struct sr_dev_inst *sdi)
573 gboolean *pod_enabled, setup_changed;
574 struct scope_state *state;
575 struct scope_config *model;
576 struct sr_channel *ch;
577 struct dev_context *devc;
578 struct sr_scpi_dev_inst *scpi;
582 state = devc->model_state;
583 model = devc->model_config;
584 setup_changed = FALSE;
586 pod_enabled = g_malloc0(sizeof(gboolean) * model->pods);
588 for (l = sdi->channels; l; l = l->next) {
591 case SR_CHANNEL_ANALOG:
592 if (ch->enabled == state->analog_states[ch->index].state)
595 if (dlm_analog_chan_state_set(scpi, ch->index + 1,
596 ch->enabled) != SR_OK)
599 state->analog_states[ch->index].state = ch->enabled;
600 setup_changed = TRUE;
602 case SR_CHANNEL_LOGIC:
604 pod_enabled[ch->index / 8] = TRUE;
606 if (ch->enabled == state->digital_states[ch->index])
609 if (dlm_digital_chan_state_set(scpi, ch->index + 1,
610 ch->enabled) != SR_OK)
613 state->digital_states[ch->index] = ch->enabled;
614 setup_changed = TRUE;
621 for (i = 1; i <= model->pods; ++i) {
622 if (state->pod_states[i - 1] == pod_enabled[i - 1])
625 if (dlm_digital_pod_state_set(scpi, i,
626 pod_enabled[i - 1]) != SR_OK)
629 state->pod_states[i - 1] = pod_enabled[i - 1];
630 setup_changed = TRUE;
635 if (setup_changed && dlm_sample_rate_query(sdi) != SR_OK)
641 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
644 gboolean digital_added;
645 struct sr_channel *ch;
646 struct dev_context *devc;
647 struct sr_scpi_dev_inst *scpi;
651 if (sdi->status != SR_ST_ACTIVE)
652 return SR_ERR_DEV_CLOSED;
656 digital_added = FALSE;
658 g_slist_free(devc->enabled_channels);
659 devc->enabled_channels = NULL;
661 for (l = sdi->channels; l; l = l->next) {
665 /* Only add a single digital channel. */
666 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
667 devc->enabled_channels = g_slist_append(
668 devc->enabled_channels, ch);
669 if (ch->type == SR_CHANNEL_LOGIC)
670 digital_added = TRUE;
674 if (!devc->enabled_channels)
677 if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
678 sr_err("Invalid channel configuration specified!");
682 if (dlm_setup_channels(sdi) != SR_OK) {
683 sr_err("Failed to setup channel configuration!");
687 /* Request data for the first enabled channel. */
688 devc->current_channel = devc->enabled_channels;
689 dlm_channel_data_request(sdi);
691 /* Call our callback when data comes in or after 5ms. */
692 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5,
693 dlm_data_receive, (void *)sdi);
698 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
700 struct dev_context *devc;
701 struct sr_datafeed_packet packet;
705 packet.type = SR_DF_END;
706 packet.payload = NULL;
707 sr_session_send(sdi, &packet);
709 if (sdi->status != SR_ST_ACTIVE)
710 return SR_ERR_DEV_CLOSED;
714 devc->num_frames = 0;
715 g_slist_free(devc->enabled_channels);
716 devc->enabled_channels = NULL;
718 sr_scpi_source_remove(sdi->session, sdi->conn);
723 SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info = {
724 .name = "yokogawa-dlm",
725 .longname = "Yokogawa DL/DLM",
730 .dev_list = dev_list,
731 .dev_clear = dev_clear,
732 .config_get = config_get,
733 .config_set = config_set,
734 .config_list = config_list,
735 .dev_open = dev_open,
736 .dev_close = dev_close,
737 .dev_acquisition_start = dev_acquisition_start,
738 .dev_acquisition_stop = dev_acquisition_stop,