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/>.
26 SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info;
28 static const char *MANUFACTURER_ID = "YOKOGAWA";
29 static const char *MANUFACTURER_NAME = "Yokogawa";
31 static const uint32_t dlm_scanopts[] = {
35 static const uint32_t dlm_drvopts[] = {
36 SR_CONF_LOGIC_ANALYZER,
40 static const uint32_t dlm_devopts[] = {
41 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
42 SR_CONF_SAMPLERATE | SR_CONF_GET,
43 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
44 SR_CONF_NUM_HDIV | SR_CONF_GET,
45 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
46 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
47 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
50 static const uint32_t dlm_analog_devopts[] = {
51 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
52 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
53 SR_CONF_NUM_VDIV | SR_CONF_GET,
56 static const uint32_t dlm_digital_devopts[] = {
66 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
68 return std_init(sr_ctx, di, LOG_PREFIX);
71 static struct sr_dev_inst *probe_usbtmc_device(struct sr_scpi_dev_inst *scpi)
73 struct sr_dev_inst *sdi;
74 struct dev_context *devc;
75 struct sr_scpi_hw_info *hw_info;
83 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
84 sr_info("Couldn't get IDN response.");
88 if (strcmp(hw_info->manufacturer, MANUFACTURER_ID) != 0)
91 if (dlm_model_get(hw_info->model, &model_name, &model_index) != SR_OK)
94 sdi = g_malloc0(sizeof(struct sr_dev_inst));
95 sdi->status = SR_ST_ACTIVE;
96 sdi->vendor = g_strdup(MANUFACTURER_NAME);
97 sdi->model = g_strdup(model_name);
98 sdi->version = g_strdup(hw_info->firmware_version);
100 sdi->serial_num = g_strdup(hw_info->serial_number);
102 sr_scpi_hw_info_free(hw_info);
105 devc = g_malloc0(sizeof(struct dev_context));
107 sdi->driver = &yokogawa_dlm_driver_info;
109 sdi->inst_type = SR_INST_SCPI;
112 if (dlm_device_init(sdi, model_index) != SR_OK)
115 sr_scpi_close(sdi->conn);
117 sdi->status = SR_ST_INACTIVE;
122 sr_scpi_hw_info_free(hw_info);
124 sr_dev_inst_free(sdi);
130 static GSList *scan(struct sr_dev_driver *di, GSList *options)
132 return sr_scpi_scan(di->context, options, probe_usbtmc_device);
135 static GSList *dev_list(const struct sr_dev_driver *di)
137 return ((struct drv_context *)(di->context))->instances;
140 static void clear_helper(void *priv)
142 struct dev_context *devc;
146 dlm_scope_state_destroy(devc->model_state);
148 g_free(devc->analog_groups);
149 g_free(devc->digital_groups);
153 static int dev_clear(const struct sr_dev_driver *di)
155 return std_dev_clear(di, clear_helper);
158 static int dev_open(struct sr_dev_inst *sdi)
160 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
163 if (dlm_scope_state_query(sdi) != SR_OK)
166 sdi->status = SR_ST_ACTIVE;
171 static int dev_close(struct sr_dev_inst *sdi)
173 if (sdi->status == SR_ST_INACTIVE)
176 sr_scpi_close(sdi->conn);
178 sdi->status = SR_ST_INACTIVE;
183 static int cleanup(const struct sr_dev_driver *di)
191 * Check which category a given channel group belongs to.
193 * @param devc Our internal device context.
194 * @param cg The channel group to check.
196 * @retval CG_NONE cg is NULL
197 * @retval CG_ANALOG cg is an analog group
198 * @retval CG_DIGITAL cg is a digital group
199 * @retval CG_INVALID cg is something else
201 static int check_channel_group(struct dev_context *devc,
202 const struct sr_channel_group *cg)
205 const struct scope_config *model;
207 model = devc->model_config;
212 for (i = 0; i < model->analog_channels; i++)
213 if (cg == devc->analog_groups[i])
216 for (i = 0; i < model->pods; i++)
217 if (cg == devc->digital_groups[i])
220 sr_err("Invalid channel group specified.");
224 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
225 const struct sr_channel_group *cg)
229 struct dev_context *devc;
230 const struct scope_config *model;
231 struct scope_state *state;
233 if (!sdi || !(devc = sdi->priv))
236 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
239 model = devc->model_config;
240 state = devc->model_state;
243 case SR_CONF_NUM_HDIV:
244 *data = g_variant_new_int32(model->num_xdivs);
247 case SR_CONF_TIMEBASE:
248 *data = g_variant_new("(tt)",
249 dlm_timebases[state->timebase][0],
250 dlm_timebases[state->timebase][1]);
253 case SR_CONF_NUM_VDIV:
254 if (cg_type == CG_NONE) {
255 sr_err("No channel group specified.");
256 return SR_ERR_CHANNEL_GROUP;
257 } else if (cg_type == CG_ANALOG) {
258 *data = g_variant_new_int32(model->num_ydivs);
267 if (cg_type == CG_NONE) {
268 sr_err("No channel group specified.");
269 return SR_ERR_CHANNEL_GROUP;
270 } else if (cg_type != CG_ANALOG)
273 for (i = 0; i < model->analog_channels; i++) {
274 if (cg != devc->analog_groups[i])
276 *data = g_variant_new("(tt)",
277 dlm_vdivs[state->analog_states[i].vdiv][0],
278 dlm_vdivs[state->analog_states[i].vdiv][1]);
283 case SR_CONF_TRIGGER_SOURCE:
284 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
287 case SR_CONF_TRIGGER_SLOPE:
288 *data = g_variant_new_string(dlm_trigger_slopes[state->trigger_slope]);
291 case SR_CONF_HORIZ_TRIGGERPOS:
292 *data = g_variant_new_double(state->horiz_triggerpos);
295 case SR_CONF_COUPLING:
297 if (cg_type == CG_NONE) {
298 sr_err("No channel group specified.");
299 return SR_ERR_CHANNEL_GROUP;
300 } else if (cg_type != CG_ANALOG)
303 for (i = 0; i < model->analog_channels; i++) {
304 if (cg != devc->analog_groups[i])
306 *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
311 case SR_CONF_SAMPLERATE:
312 *data = g_variant_new_uint64(state->sample_rate);
322 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
325 GVariant *rational[2];
328 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
330 for (i = 0; i < n; i++) {
331 rational[0] = g_variant_new_uint64((*array)[i][0]);
332 rational[1] = g_variant_new_uint64((*array)[i][1]);
334 /* FIXME: Valgrind reports a memory leak here. */
335 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
338 return g_variant_builder_end(&gvb);
341 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
342 const struct sr_channel_group *cg)
347 struct dev_context *devc;
348 const struct scope_config *model;
349 struct scope_state *state;
353 gboolean update_sample_rate;
355 if (!sdi || !(devc = sdi->priv))
358 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
361 model = devc->model_config;
362 state = devc->model_state;
363 update_sample_rate = FALSE;
368 case SR_CONF_LIMIT_FRAMES:
369 devc->frame_limit = g_variant_get_uint64(data);
372 case SR_CONF_TRIGGER_SOURCE:
373 tmp = g_variant_get_string(data, NULL);
374 for (i = 0; (*model->trigger_sources)[i]; i++) {
375 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
377 state->trigger_source = i;
378 /* TODO: A and B trigger support possible? */
379 ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
384 if (cg_type == CG_NONE) {
385 sr_err("No channel group specified.");
386 return SR_ERR_CHANNEL_GROUP;
389 g_variant_get(data, "(tt)", &p, &q);
391 for (i = 0; i < ARRAY_SIZE(dlm_vdivs); i++) {
392 if (p != dlm_vdivs[i][0] ||
393 q != dlm_vdivs[i][1])
395 for (j = 1; j <= model->analog_channels; j++) {
396 if (cg != devc->analog_groups[j - 1])
398 state->analog_states[j - 1].vdiv = i;
399 g_ascii_formatd(float_str, sizeof(float_str),
400 "%E", (float) p / q);
401 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
402 sr_scpi_get_opc(sdi->conn) != SR_OK)
412 case SR_CONF_TIMEBASE:
413 g_variant_get(data, "(tt)", &p, &q);
415 for (i = 0; i < ARRAY_SIZE(dlm_timebases); i++) {
416 if (p != dlm_timebases[i][0] ||
417 q != dlm_timebases[i][1])
420 g_ascii_formatd(float_str, sizeof(float_str),
421 "%E", (float) p / q);
422 ret = dlm_timebase_set(sdi->conn, float_str);
423 update_sample_rate = TRUE;
427 case SR_CONF_HORIZ_TRIGGERPOS:
428 tmp_d = g_variant_get_double(data);
430 /* TODO: Check if the calculation makes sense for the DLM. */
431 if (tmp_d < 0.0 || tmp_d > 1.0)
434 state->horiz_triggerpos = tmp_d;
435 tmp_d = -(tmp_d - 0.5) *
436 ((double) dlm_timebases[state->timebase][0] /
437 dlm_timebases[state->timebase][1])
440 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
441 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
443 case SR_CONF_TRIGGER_SLOPE:
444 tmp = g_variant_get_string(data, NULL);
446 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
449 /* Note: See dlm_trigger_slopes[] in protocol.c. */
450 state->trigger_slope = (tmp[0] == 'r') ?
451 SLOPE_POSITIVE : SLOPE_NEGATIVE;
453 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
455 case SR_CONF_COUPLING:
456 if (cg_type == CG_NONE) {
457 sr_err("No channel group specified.");
458 return SR_ERR_CHANNEL_GROUP;
461 tmp = g_variant_get_string(data, NULL);
463 for (i = 0; (*model->coupling_options)[i]; i++) {
464 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
466 for (j = 1; j <= model->analog_channels; j++) {
467 if (cg != devc->analog_groups[j - 1])
469 state->analog_states[j-1].coupling = i;
471 if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
472 sr_scpi_get_opc(sdi->conn) != SR_OK)
487 ret = sr_scpi_get_opc(sdi->conn);
489 if (ret == SR_OK && update_sample_rate)
490 ret = dlm_sample_rate_query(sdi);
495 static int config_channel_set(const struct sr_dev_inst *sdi,
496 struct sr_channel *ch, unsigned int changes)
498 /* Currently we only handle SR_CHANNEL_SET_ENABLED. */
499 if (changes != SR_CHANNEL_SET_ENABLED)
502 return dlm_channel_state_set(sdi, ch->index, ch->enabled);
505 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
506 const struct sr_channel_group *cg)
508 int cg_type = CG_NONE;
509 struct dev_context *devc = NULL;
510 const struct scope_config *model = NULL;
512 /* SR_CONF_SCAN_OPTIONS is always valid, regardless of sdi or probe group. */
513 if (key == SR_CONF_SCAN_OPTIONS) {
514 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
515 dlm_scanopts, ARRAY_SIZE(dlm_scanopts), sizeof(uint32_t));
519 /* If sdi is NULL, nothing except SR_CONF_DEVICE_OPTIONS can be provided. */
520 if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
521 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
522 dlm_drvopts, ARRAY_SIZE(dlm_drvopts), sizeof(uint32_t));
530 model = devc->model_config;
533 * If cg is NULL, only the SR_CONF_DEVICE_OPTIONS that are not
534 * specific to a probe group must be returned.
538 case SR_CONF_DEVICE_OPTIONS:
539 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
540 dlm_devopts, ARRAY_SIZE(dlm_devopts), sizeof(uint32_t));
542 case SR_CONF_TIMEBASE:
543 *data = build_tuples(&dlm_timebases, ARRAY_SIZE(dlm_timebases));
545 case SR_CONF_TRIGGER_SOURCE:
548 *data = g_variant_new_strv(*model->trigger_sources,
549 g_strv_length((char **)*model->trigger_sources));
551 case SR_CONF_TRIGGER_SLOPE:
552 *data = g_variant_new_strv(dlm_trigger_slopes,
553 g_strv_length((char **)dlm_trigger_slopes));
555 case SR_CONF_NUM_HDIV:
556 *data = g_variant_new_uint32(model->num_xdivs);
563 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
567 case SR_CONF_DEVICE_OPTIONS:
568 if (cg_type == CG_ANALOG) {
569 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
570 dlm_analog_devopts, ARRAY_SIZE(dlm_analog_devopts), sizeof(uint32_t));
571 } else if (cg_type == CG_DIGITAL) {
572 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
573 dlm_digital_devopts, ARRAY_SIZE(dlm_digital_devopts), sizeof(uint32_t));
575 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
576 NULL, 0, sizeof(uint32_t));
579 case SR_CONF_COUPLING:
580 if (cg_type == CG_NONE)
581 return SR_ERR_CHANNEL_GROUP;
582 *data = g_variant_new_strv(*model->coupling_options,
583 g_strv_length((char **)*model->coupling_options));
586 if (cg_type == CG_NONE)
587 return SR_ERR_CHANNEL_GROUP;
588 *data = build_tuples(&dlm_vdivs, ARRAY_SIZE(dlm_vdivs));
597 static int dlm_check_channels(GSList *channels)
600 struct sr_channel *ch;
601 gboolean enabled_pod1, enabled_chan4;
603 enabled_pod1 = enabled_chan4 = FALSE;
605 /* Note: On the DLM2000, CH4 and Logic are shared. */
606 /* TODO Handle non-DLM2000 models. */
607 for (l = channels; l; l = l->next) {
610 case SR_CHANNEL_ANALOG:
612 enabled_chan4 = TRUE;
614 case SR_CHANNEL_LOGIC:
622 if (enabled_pod1 && enabled_chan4)
628 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
631 gboolean digital_added;
632 struct sr_channel *ch;
633 struct dev_context *devc;
634 struct sr_scpi_dev_inst *scpi;
638 if (sdi->status != SR_ST_ACTIVE)
639 return SR_ERR_DEV_CLOSED;
643 digital_added = FALSE;
645 g_slist_free(devc->enabled_channels);
646 devc->enabled_channels = NULL;
648 for (l = sdi->channels; l; l = l->next) {
652 /* Only add a single digital channel. */
653 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
654 devc->enabled_channels = g_slist_append(
655 devc->enabled_channels, ch);
656 if (ch->type == SR_CHANNEL_LOGIC)
657 digital_added = TRUE;
661 if (!devc->enabled_channels)
664 if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
665 sr_err("Invalid channel configuration specified!");
669 /* Request data for the first enabled channel. */
670 devc->current_channel = devc->enabled_channels;
671 dlm_channel_data_request(sdi);
673 /* Call our callback when data comes in or after 5ms. */
674 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5,
675 dlm_data_receive, (void *)sdi);
680 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
682 struct dev_context *devc;
683 struct sr_datafeed_packet packet;
687 packet.type = SR_DF_END;
688 packet.payload = NULL;
689 sr_session_send(sdi, &packet);
691 if (sdi->status != SR_ST_ACTIVE)
692 return SR_ERR_DEV_CLOSED;
696 devc->num_frames = 0;
697 g_slist_free(devc->enabled_channels);
698 devc->enabled_channels = NULL;
700 sr_scpi_source_remove(sdi->session, sdi->conn);
705 SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info = {
706 .name = "yokogawa-dlm",
707 .longname = "Yokogawa DL/DLM",
712 .dev_list = dev_list,
713 .dev_clear = dev_clear,
714 .config_get = config_get,
715 .config_set = config_set,
716 .config_channel_set = config_channel_set,
717 .config_list = config_list,
718 .dev_open = dev_open,
719 .dev_close = dev_close,
720 .dev_acquisition_start = dev_acquisition_start,
721 .dev_acquisition_stop = dev_acquisition_stop,