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(SR_ST_ACTIVE, MANUFACTURER_NAME,
66 model_name, hw_info->firmware_version)))
69 sdi->serial_num = g_strdup(hw_info->serial_number);
71 sr_scpi_hw_info_free(hw_info);
74 devc = g_malloc0(sizeof(struct dev_context));
78 sdi->inst_type = SR_INST_SCPI;
81 if (dlm_device_init(sdi, model_index) != SR_OK)
84 sr_scpi_close(sdi->conn);
86 sdi->status = SR_ST_INACTIVE;
91 sr_scpi_hw_info_free(hw_info);
93 sr_dev_inst_free(sdi);
100 static GSList *scan(GSList *options)
102 return sr_scpi_scan(di->priv, options, probe_usbtmc_device);
105 static GSList *dev_list(void)
107 return ((struct drv_context *)(di->priv))->instances;
110 static void clear_helper(void *priv)
112 struct dev_context *devc;
116 dlm_scope_state_destroy(devc->model_state);
118 g_free(devc->analog_groups);
119 g_free(devc->digital_groups);
123 static int dev_clear(void)
125 return std_dev_clear(di, clear_helper);
128 static int dev_open(struct sr_dev_inst *sdi)
130 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
133 if (dlm_scope_state_query(sdi) != SR_OK)
136 sdi->status = SR_ST_ACTIVE;
141 static int dev_close(struct sr_dev_inst *sdi)
143 if (sdi->status == SR_ST_INACTIVE)
146 sr_scpi_close(sdi->conn);
148 sdi->status = SR_ST_INACTIVE;
153 static int cleanup(void)
161 * Check which category a given channel group belongs to.
163 * @param devc Our internal device context.
164 * @param cg The channel group to check.
166 * @retval CG_NONE cg is NULL
167 * @retval CG_ANALOG cg is an analog group
168 * @retval CG_DIGITAL cg is a digital group
169 * @retval CG_INVALID cg is something else
171 static int check_channel_group(struct dev_context *devc,
172 const struct sr_channel_group *cg)
175 struct scope_config *model;
177 model = devc->model_config;
182 for (i = 0; i < model->analog_channels; ++i)
183 if (cg == devc->analog_groups[i])
186 for (i = 0; i < model->pods; ++i)
187 if (cg == devc->digital_groups[i])
190 sr_err("Invalid channel group specified.");
194 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
195 const struct sr_channel_group *cg)
199 struct dev_context *devc;
200 struct scope_config *model;
201 struct scope_state *state;
203 if (!sdi || !(devc = sdi->priv))
206 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
210 model = devc->model_config;
211 state = devc->model_state;
214 case SR_CONF_NUM_TIMEBASE:
215 *data = g_variant_new_int32(model->num_xdivs);
218 case SR_CONF_TIMEBASE:
219 *data = g_variant_new("(tt)",
220 (*model->timebases)[state->timebase][0],
221 (*model->timebases)[state->timebase][1]);
224 case SR_CONF_NUM_VDIV:
225 if (cg_type == CG_NONE) {
226 sr_err("No channel group specified.");
227 return SR_ERR_CHANNEL_GROUP;
228 } else if (cg_type == CG_ANALOG) {
229 *data = g_variant_new_int32(model->num_ydivs);
238 if (cg_type == CG_NONE) {
239 sr_err("No channel group specified.");
240 return SR_ERR_CHANNEL_GROUP;
241 } else if (cg_type != CG_ANALOG)
244 for (i = 0; i < model->analog_channels; ++i) {
245 if (cg != devc->analog_groups[i])
247 *data = g_variant_new("(tt)",
248 (*model->vdivs)[state->analog_states[i].vdiv][0],
249 (*model->vdivs)[state->analog_states[i].vdiv][1]);
254 case SR_CONF_TRIGGER_SOURCE:
255 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
258 case SR_CONF_TRIGGER_SLOPE:
259 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
262 case SR_CONF_HORIZ_TRIGGERPOS:
263 *data = g_variant_new_double(state->horiz_triggerpos);
266 case SR_CONF_COUPLING:
268 if (cg_type == CG_NONE) {
269 sr_err("No channel group specified.");
270 return SR_ERR_CHANNEL_GROUP;
271 } else if (cg_type != CG_ANALOG)
274 for (i = 0; i < model->analog_channels; ++i) {
275 if (cg != devc->analog_groups[i])
277 *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
282 case SR_CONF_SAMPLERATE:
283 *data = g_variant_new_uint64(state->sample_rate);
293 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
296 GVariant *rational[2];
299 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
301 for (i = 0; i < n; i++) {
302 rational[0] = g_variant_new_uint64((*array)[i][0]);
303 rational[1] = g_variant_new_uint64((*array)[i][1]);
305 /* FIXME: Valgrind reports a memory leak here. */
306 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
309 return g_variant_builder_end(&gvb);
312 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
313 const struct sr_channel_group *cg)
318 struct dev_context *devc;
319 struct scope_config *model;
320 struct scope_state *state;
324 gboolean update_sample_rate;
326 if (!sdi || !(devc = sdi->priv))
329 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
332 model = devc->model_config;
333 state = devc->model_state;
334 update_sample_rate = FALSE;
339 case SR_CONF_LIMIT_FRAMES:
340 devc->frame_limit = g_variant_get_uint64(data);
343 case SR_CONF_TRIGGER_SOURCE:
344 tmp = g_variant_get_string(data, NULL);
345 for (i = 0; (*model->trigger_sources)[i]; i++) {
346 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
348 state->trigger_source = i;
349 /* TODO: A and B trigger support possible? */
350 ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
355 if (cg_type == CG_NONE) {
356 sr_err("No channel group specified.");
357 return SR_ERR_CHANNEL_GROUP;
360 g_variant_get(data, "(tt)", &p, &q);
362 for (i = 0; i < model->num_vdivs; i++) {
363 if (p != (*model->vdivs)[i][0] ||
364 q != (*model->vdivs)[i][1])
366 for (j = 1; j <= model->analog_channels; ++j) {
367 if (cg != devc->analog_groups[j - 1])
369 state->analog_states[j - 1].vdiv = i;
370 g_ascii_formatd(float_str, sizeof(float_str),
371 "%E", (float) p / q);
372 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
373 sr_scpi_get_opc(sdi->conn) != SR_OK)
383 case SR_CONF_TIMEBASE:
384 g_variant_get(data, "(tt)", &p, &q);
386 for (i = 0; i < model->num_timebases; i++) {
387 if (p != (*model->timebases)[i][0] ||
388 q != (*model->timebases)[i][1])
391 g_ascii_formatd(float_str, sizeof(float_str),
392 "%E", (float) p / q);
393 ret = dlm_timebase_set(sdi->conn, float_str);
394 update_sample_rate = TRUE;
398 case SR_CONF_HORIZ_TRIGGERPOS:
399 tmp_d = g_variant_get_double(data);
401 /* TODO: Check if the calculation makes sense for the DLM. */
402 if (tmp_d < 0.0 || tmp_d > 1.0)
405 state->horiz_triggerpos = tmp_d;
406 tmp_d = -(tmp_d - 0.5) *
407 ((double) (*model->timebases)[state->timebase][0] /
408 (*model->timebases)[state->timebase][1])
411 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
412 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
414 case SR_CONF_TRIGGER_SLOPE:
415 tmp = g_variant_get_string(data, NULL);
417 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
420 /* Note: See dlm_trigger_slopes[] in protocol.c. */
421 state->trigger_slope = (tmp[0] == 'r') ?
422 SLOPE_POSITIVE : SLOPE_NEGATIVE;
424 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
426 case SR_CONF_COUPLING:
427 if (cg_type == CG_NONE) {
428 sr_err("No channel group specified.");
429 return SR_ERR_CHANNEL_GROUP;
432 tmp = g_variant_get_string(data, NULL);
434 for (i = 0; (*model->coupling_options)[i]; i++) {
435 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
437 for (j = 1; j <= model->analog_channels; ++j) {
438 if (cg != devc->analog_groups[j - 1])
440 state->analog_states[j-1].coupling = i;
442 if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
443 sr_scpi_get_opc(sdi->conn) != SR_OK)
458 ret = sr_scpi_get_opc(sdi->conn);
460 if (ret == SR_OK && update_sample_rate)
461 ret = dlm_sample_rate_query(sdi);
466 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
467 const struct sr_channel_group *cg)
470 struct dev_context *devc;
471 struct scope_config *model;
473 if (!sdi || !(devc = sdi->priv))
476 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
479 model = devc->model_config;
482 case SR_CONF_DEVICE_OPTIONS:
483 if (cg_type == CG_NONE) {
484 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
485 model->devopts, model->num_devopts, sizeof(uint32_t));
486 } else if (cg_type == CG_ANALOG) {
487 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
488 model->analog_devopts, model->num_analog_devopts, sizeof(uint32_t));
490 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
491 NULL, 0, sizeof(uint32_t));
494 case SR_CONF_COUPLING:
495 if (cg_type == CG_NONE)
496 return SR_ERR_CHANNEL_GROUP;
497 *data = g_variant_new_strv(*model->coupling_options,
498 g_strv_length((char **)*model->coupling_options));
500 case SR_CONF_TRIGGER_SOURCE:
501 *data = g_variant_new_strv(*model->trigger_sources,
502 g_strv_length((char **)*model->trigger_sources));
504 case SR_CONF_TRIGGER_SLOPE:
505 *data = g_variant_new_strv(*model->trigger_slopes,
506 g_strv_length((char **)*model->trigger_slopes));
508 case SR_CONF_TIMEBASE:
509 *data = build_tuples(model->timebases, model->num_timebases);
512 if (cg_type == CG_NONE)
513 return SR_ERR_CHANNEL_GROUP;
514 *data = build_tuples(model->vdivs, model->num_vdivs);
523 static int dlm_check_channels(GSList *channels)
526 struct sr_channel *ch;
527 gboolean enabled_pod1, enabled_chan4;
529 enabled_pod1 = enabled_chan4 = FALSE;
531 /* Note: On the DLM2000, CH4 and Logic are shared. */
532 /* TODO Handle non-DLM2000 models. */
533 for (l = channels; l; l = l->next) {
536 case SR_CHANNEL_ANALOG:
538 enabled_chan4 = TRUE;
540 case SR_CHANNEL_LOGIC:
548 if (enabled_pod1 && enabled_chan4)
554 static int dlm_setup_channels(const struct sr_dev_inst *sdi)
558 gboolean *pod_enabled, setup_changed;
559 struct scope_state *state;
560 struct scope_config *model;
561 struct sr_channel *ch;
562 struct dev_context *devc;
563 struct sr_scpi_dev_inst *scpi;
567 state = devc->model_state;
568 model = devc->model_config;
569 setup_changed = FALSE;
571 pod_enabled = g_malloc0(sizeof(gboolean) * model->pods);
573 for (l = sdi->channels; l; l = l->next) {
576 case SR_CHANNEL_ANALOG:
577 if (ch->enabled == state->analog_states[ch->index].state)
580 if (dlm_analog_chan_state_set(scpi, ch->index + 1,
581 ch->enabled) != SR_OK)
584 state->analog_states[ch->index].state = ch->enabled;
585 setup_changed = TRUE;
587 case SR_CHANNEL_LOGIC:
589 pod_enabled[ch->index / 8] = TRUE;
591 if (ch->enabled == state->digital_states[ch->index])
594 if (dlm_digital_chan_state_set(scpi, ch->index + 1,
595 ch->enabled) != SR_OK)
598 state->digital_states[ch->index] = ch->enabled;
599 setup_changed = TRUE;
606 for (i = 1; i <= model->pods; ++i) {
607 if (state->pod_states[i - 1] == pod_enabled[i - 1])
610 if (dlm_digital_pod_state_set(scpi, i,
611 pod_enabled[i - 1]) != SR_OK)
614 state->pod_states[i - 1] = pod_enabled[i - 1];
615 setup_changed = TRUE;
620 if (setup_changed && dlm_sample_rate_query(sdi) != SR_OK)
626 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
629 gboolean digital_added;
630 struct sr_channel *ch;
631 struct dev_context *devc;
632 struct sr_scpi_dev_inst *scpi;
636 if (sdi->status != SR_ST_ACTIVE)
637 return SR_ERR_DEV_CLOSED;
641 digital_added = FALSE;
643 g_slist_free(devc->enabled_channels);
644 devc->enabled_channels = NULL;
646 for (l = sdi->channels; l; l = l->next) {
650 /* Only add a single digital channel. */
651 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
652 devc->enabled_channels = g_slist_append(
653 devc->enabled_channels, ch);
654 if (ch->type == SR_CHANNEL_LOGIC)
655 digital_added = TRUE;
659 if (!devc->enabled_channels)
662 if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
663 sr_err("Invalid channel configuration specified!");
667 if (dlm_setup_channels(sdi) != SR_OK) {
668 sr_err("Failed to setup channel configuration!");
672 /* Request data for the first enabled channel. */
673 devc->current_channel = devc->enabled_channels;
674 dlm_channel_data_request(sdi);
676 /* Call our callback when data comes in or after 5ms. */
677 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5,
678 dlm_data_receive, (void *)sdi);
683 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
685 struct dev_context *devc;
686 struct sr_datafeed_packet packet;
690 packet.type = SR_DF_END;
691 packet.payload = NULL;
692 sr_session_send(sdi, &packet);
694 if (sdi->status != SR_ST_ACTIVE)
695 return SR_ERR_DEV_CLOSED;
699 devc->num_frames = 0;
700 g_slist_free(devc->enabled_channels);
701 devc->enabled_channels = NULL;
703 sr_scpi_source_remove(sdi->session, sdi->conn);
708 SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info = {
709 .name = "yokogawa-dlm",
710 .longname = "Yokogawa DL/DLM",
715 .dev_list = dev_list,
716 .dev_clear = dev_clear,
717 .config_get = config_get,
718 .config_set = config_set,
719 .config_list = config_list,
720 .dev_open = dev_open,
721 .dev_close = dev_close,
722 .dev_acquisition_start = dev_acquisition_start,
723 .dev_acquisition_stop = dev_acquisition_stop,