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;
25 static struct sr_dev_driver *di = &yokogawa_dlm_driver_info;
27 static char *MANUFACTURER_ID = "YOKOGAWA";
28 static char *MANUFACTURER_NAME = "Yokogawa";
37 static int init(struct sr_context *sr_ctx)
39 return std_init(sr_ctx, di, LOG_PREFIX);
42 static struct sr_dev_inst *probe_usbtmc_device(struct sr_scpi_dev_inst *scpi)
44 struct sr_dev_inst *sdi;
45 struct dev_context *devc;
46 struct sr_scpi_hw_info *hw_info;
54 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
55 sr_info("Couldn't get IDN response.");
59 if (strcmp(hw_info->manufacturer, MANUFACTURER_ID) != 0)
62 if (dlm_model_get(hw_info->model, &model_name, &model_index) != SR_OK)
65 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, MANUFACTURER_NAME,
69 sr_scpi_hw_info_free(hw_info);
72 devc = g_malloc0(sizeof(struct dev_context));
76 sdi->inst_type = SR_INST_SCPI;
79 if (dlm_device_init(sdi, model_index) != SR_OK)
82 sr_scpi_close(sdi->conn);
84 sdi->status = SR_ST_INACTIVE;
89 sr_scpi_hw_info_free(hw_info);
91 sr_dev_inst_free(sdi);
98 static GSList *scan(GSList *options)
100 return sr_scpi_scan(di->priv, options, probe_usbtmc_device);
103 static GSList *dev_list(void)
105 return ((struct drv_context *)(di->priv))->instances;
108 static void clear_helper(void *priv)
110 struct dev_context *devc;
114 dlm_scope_state_destroy(devc->model_state);
116 g_free(devc->analog_groups);
117 g_free(devc->digital_groups);
121 static int dev_clear(void)
123 return std_dev_clear(di, clear_helper);
126 static int dev_open(struct sr_dev_inst *sdi)
128 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
131 if (dlm_scope_state_query(sdi) != SR_OK)
134 sdi->status = SR_ST_ACTIVE;
139 static int dev_close(struct sr_dev_inst *sdi)
141 if (sdi->status == SR_ST_INACTIVE)
144 sr_scpi_close(sdi->conn);
146 sdi->status = SR_ST_INACTIVE;
151 static int cleanup(void)
159 * Check which category a given channel group belongs to.
161 * @param devc Our internal device context.
162 * @param cg The channel group to check.
164 * @retval CG_NONE cg is NULL
165 * @retval CG_ANALOG cg is an analog group
166 * @retval CG_DIGITAL cg is a digital group
167 * @retval CG_INVALID cg is something else
169 static int check_channel_group(struct dev_context *devc,
170 const struct sr_channel_group *cg)
173 struct scope_config *model;
175 model = devc->model_config;
180 for (i = 0; i < model->analog_channels; ++i)
181 if (cg == devc->analog_groups[i])
184 for (i = 0; i < model->pods; ++i)
185 if (cg == devc->digital_groups[i])
188 sr_err("Invalid channel group specified.");
192 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
193 const struct sr_channel_group *cg)
197 struct dev_context *devc;
198 struct scope_config *model;
199 struct scope_state *state;
201 if (!sdi || !(devc = sdi->priv))
204 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
208 model = devc->model_config;
209 state = devc->model_state;
212 case SR_CONF_NUM_TIMEBASE:
213 *data = g_variant_new_int32(model->num_xdivs);
216 case SR_CONF_TIMEBASE:
217 *data = g_variant_new("(tt)",
218 (*model->timebases)[state->timebase][0],
219 (*model->timebases)[state->timebase][1]);
222 case SR_CONF_NUM_VDIV:
223 if (cg_type == CG_NONE) {
224 sr_err("No channel group specified.");
225 return SR_ERR_CHANNEL_GROUP;
226 } else if (cg_type == CG_ANALOG) {
227 *data = g_variant_new_int32(model->num_ydivs);
236 if (cg_type == CG_NONE) {
237 sr_err("No channel group specified.");
238 return SR_ERR_CHANNEL_GROUP;
239 } else if (cg_type != CG_ANALOG)
242 for (i = 0; i < model->analog_channels; ++i) {
243 if (cg != devc->analog_groups[i])
245 *data = g_variant_new("(tt)",
246 (*model->vdivs)[state->analog_states[i].vdiv][0],
247 (*model->vdivs)[state->analog_states[i].vdiv][1]);
252 case SR_CONF_TRIGGER_SOURCE:
253 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
256 case SR_CONF_TRIGGER_SLOPE:
257 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
260 case SR_CONF_HORIZ_TRIGGERPOS:
261 *data = g_variant_new_double(state->horiz_triggerpos);
264 case SR_CONF_COUPLING:
266 if (cg_type == CG_NONE) {
267 sr_err("No channel group specified.");
268 return SR_ERR_CHANNEL_GROUP;
269 } else if (cg_type != CG_ANALOG)
272 for (i = 0; i < model->analog_channels; ++i) {
273 if (cg != devc->analog_groups[i])
275 *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
280 case SR_CONF_SAMPLERATE:
281 *data = g_variant_new_uint64(state->sample_rate);
291 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
294 GVariant *rational[2];
297 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
299 for (i = 0; i < n; i++) {
300 rational[0] = g_variant_new_uint64((*array)[i][0]);
301 rational[1] = g_variant_new_uint64((*array)[i][1]);
303 /* FIXME: Valgrind reports a memory leak here. */
304 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
307 return g_variant_builder_end(&gvb);
310 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
311 const struct sr_channel_group *cg)
316 struct dev_context *devc;
317 struct scope_config *model;
318 struct scope_state *state;
322 gboolean update_sample_rate;
324 if (!sdi || !(devc = sdi->priv))
327 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
330 model = devc->model_config;
331 state = devc->model_state;
332 update_sample_rate = FALSE;
337 case SR_CONF_LIMIT_FRAMES:
338 devc->frame_limit = g_variant_get_uint64(data);
341 case SR_CONF_TRIGGER_SOURCE:
342 tmp = g_variant_get_string(data, NULL);
343 for (i = 0; (*model->trigger_sources)[i]; i++) {
344 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
346 state->trigger_source = i;
347 /* TODO: A and B trigger support possible? */
348 ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
353 if (cg_type == CG_NONE) {
354 sr_err("No channel group specified.");
355 return SR_ERR_CHANNEL_GROUP;
358 g_variant_get(data, "(tt)", &p, &q);
360 for (i = 0; i < model->num_vdivs; i++) {
361 if (p != (*model->vdivs)[i][0] ||
362 q != (*model->vdivs)[i][1])
364 for (j = 1; j <= model->analog_channels; ++j) {
365 if (cg != devc->analog_groups[j - 1])
367 state->analog_states[j - 1].vdiv = i;
368 g_ascii_formatd(float_str, sizeof(float_str),
369 "%E", (float) p / q);
370 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
371 sr_scpi_get_opc(sdi->conn) != SR_OK)
381 case SR_CONF_TIMEBASE:
382 g_variant_get(data, "(tt)", &p, &q);
384 for (i = 0; i < model->num_timebases; i++) {
385 if (p != (*model->timebases)[i][0] ||
386 q != (*model->timebases)[i][1])
389 g_ascii_formatd(float_str, sizeof(float_str),
390 "%E", (float) p / q);
391 ret = dlm_timebase_set(sdi->conn, float_str);
392 update_sample_rate = TRUE;
396 case SR_CONF_HORIZ_TRIGGERPOS:
397 tmp_d = g_variant_get_double(data);
399 /* TODO: Check if the calculation makes sense for the DLM. */
400 if (tmp_d < 0.0 || tmp_d > 1.0)
403 state->horiz_triggerpos = tmp_d;
404 tmp_d = -(tmp_d - 0.5) *
405 ((double) (*model->timebases)[state->timebase][0] /
406 (*model->timebases)[state->timebase][1])
409 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
410 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
412 case SR_CONF_TRIGGER_SLOPE:
413 tmp = g_variant_get_string(data, NULL);
415 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
418 /* Note: See dlm_trigger_slopes[] in protocol.c. */
419 state->trigger_slope = (tmp[0] == 'r') ?
420 SLOPE_POSITIVE : SLOPE_NEGATIVE;
422 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
424 case SR_CONF_COUPLING:
425 if (cg_type == CG_NONE) {
426 sr_err("No channel group specified.");
427 return SR_ERR_CHANNEL_GROUP;
430 tmp = g_variant_get_string(data, NULL);
432 for (i = 0; (*model->coupling_options)[i]; i++) {
433 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
435 for (j = 1; j <= model->analog_channels; ++j) {
436 if (cg != devc->analog_groups[j - 1])
438 state->analog_states[j-1].coupling = i;
440 if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
441 sr_scpi_get_opc(sdi->conn) != SR_OK)
456 ret = sr_scpi_get_opc(sdi->conn);
458 if (ret == SR_OK && update_sample_rate)
459 ret = dlm_sample_rate_query(sdi);
464 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
465 const struct sr_channel_group *cg)
468 struct dev_context *devc;
469 struct scope_config *model;
471 if (!sdi || !(devc = sdi->priv))
474 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
477 model = devc->model_config;
480 case SR_CONF_SCAN_OPTIONS:
483 case SR_CONF_DEVICE_OPTIONS:
484 if (cg_type == CG_NONE) {
485 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
486 model->hw_caps, model->num_hwcaps, sizeof(int32_t));
487 } else if (cg_type == CG_ANALOG) {
488 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
489 model->analog_hwcaps, model->num_analog_hwcaps, sizeof(int32_t));
491 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
492 NULL, 0, sizeof(int32_t));
495 case SR_CONF_COUPLING:
496 if (cg_type == CG_NONE)
497 return SR_ERR_CHANNEL_GROUP;
498 *data = g_variant_new_strv(*model->coupling_options,
499 g_strv_length((char **)*model->coupling_options));
501 case SR_CONF_TRIGGER_SOURCE:
502 *data = g_variant_new_strv(*model->trigger_sources,
503 g_strv_length((char **)*model->trigger_sources));
505 case SR_CONF_TRIGGER_SLOPE:
506 *data = g_variant_new_strv(*model->trigger_slopes,
507 g_strv_length((char **)*model->trigger_slopes));
509 case SR_CONF_TIMEBASE:
510 *data = build_tuples(model->timebases, model->num_timebases);
513 if (cg_type == CG_NONE)
514 return SR_ERR_CHANNEL_GROUP;
515 *data = build_tuples(model->vdivs, model->num_vdivs);
524 static int dlm_check_channels(GSList *channels)
527 struct sr_channel *ch;
528 gboolean enabled_pod1, enabled_chan4;
530 enabled_pod1 = enabled_chan4 = FALSE;
532 /* Note: On the DLM2000, CH4 and Logic are shared. */
533 /* TODO Handle non-DLM2000 models. */
534 for (l = channels; l; l = l->next) {
537 case SR_CHANNEL_ANALOG:
539 enabled_chan4 = TRUE;
541 case SR_CHANNEL_LOGIC:
549 if (enabled_pod1 && enabled_chan4)
555 static int dlm_setup_channels(const struct sr_dev_inst *sdi)
559 gboolean *pod_enabled, setup_changed;
560 struct scope_state *state;
561 struct scope_config *model;
562 struct sr_channel *ch;
563 struct dev_context *devc;
564 struct sr_scpi_dev_inst *scpi;
568 state = devc->model_state;
569 model = devc->model_config;
570 setup_changed = FALSE;
572 pod_enabled = g_malloc0(sizeof(gboolean) * model->pods);
574 for (l = sdi->channels; l; l = l->next) {
577 case SR_CHANNEL_ANALOG:
578 if (ch->enabled == state->analog_states[ch->index].state)
581 if (dlm_analog_chan_state_set(scpi, ch->index + 1,
582 ch->enabled) != SR_OK)
585 state->analog_states[ch->index].state = ch->enabled;
586 setup_changed = TRUE;
588 case SR_CHANNEL_LOGIC:
590 pod_enabled[ch->index / 8] = TRUE;
592 if (ch->enabled == state->digital_states[ch->index])
595 if (dlm_digital_chan_state_set(scpi, ch->index + 1,
596 ch->enabled) != SR_OK)
599 state->digital_states[ch->index] = ch->enabled;
600 setup_changed = TRUE;
607 for (i = 1; i <= model->pods; ++i) {
608 if (state->pod_states[i - 1] == pod_enabled[i - 1])
611 if (dlm_digital_pod_state_set(scpi, i,
612 pod_enabled[i - 1]) != SR_OK)
615 state->pod_states[i - 1] = pod_enabled[i - 1];
616 setup_changed = TRUE;
621 if (setup_changed && dlm_sample_rate_query(sdi) != SR_OK)
627 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
630 gboolean digital_added;
631 struct sr_channel *ch;
632 struct dev_context *devc;
633 struct sr_scpi_dev_inst *scpi;
637 if (sdi->status != SR_ST_ACTIVE)
638 return SR_ERR_DEV_CLOSED;
642 digital_added = FALSE;
644 g_slist_free(devc->enabled_channels);
645 devc->enabled_channels = NULL;
647 for (l = sdi->channels; l; l = l->next) {
651 /* Only add a single digital channel. */
652 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
653 devc->enabled_channels = g_slist_append(
654 devc->enabled_channels, ch);
655 if (ch->type == SR_CHANNEL_LOGIC)
656 digital_added = TRUE;
660 if (!devc->enabled_channels)
663 if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
664 sr_err("Invalid channel configuration specified!");
668 if (dlm_setup_channels(sdi) != SR_OK) {
669 sr_err("Failed to setup channel configuration!");
673 /* Request data for the first enabled channel. */
674 devc->current_channel = devc->enabled_channels;
675 dlm_channel_data_request(sdi);
677 /* Call our callback when data comes in or after 5ms. */
678 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5,
679 dlm_data_receive, (void *)sdi);
684 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
686 struct dev_context *devc;
687 struct sr_datafeed_packet packet;
691 packet.type = SR_DF_END;
692 packet.payload = NULL;
693 sr_session_send(sdi, &packet);
695 if (sdi->status != SR_ST_ACTIVE)
696 return SR_ERR_DEV_CLOSED;
700 devc->num_frames = 0;
701 g_slist_free(devc->enabled_channels);
702 devc->enabled_channels = NULL;
704 sr_scpi_source_remove(sdi->session, sdi->conn);
709 SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info = {
710 .name = "yokogawa-dlm",
711 .longname = "Yokogawa DL/DLM",
716 .dev_list = dev_list,
717 .dev_clear = dev_clear,
718 .config_get = config_get,
719 .config_set = config_set,
720 .config_list = config_list,
721 .dev_open = dev_open,
722 .dev_close = dev_close,
723 .dev_acquisition_start = dev_acquisition_start,
724 .dev_acquisition_stop = dev_acquisition_stop,