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 const char *MANUFACTURER_ID = "YOKOGAWA";
27 static const 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);
105 static GSList *scan(struct sr_dev_driver *di, GSList *options)
107 return sr_scpi_scan(di->priv, options, probe_usbtmc_device);
110 static GSList *dev_list(const struct sr_dev_driver *di)
112 return ((struct drv_context *)(di->priv))->instances;
115 static void clear_helper(void *priv)
117 struct dev_context *devc;
121 dlm_scope_state_destroy(devc->model_state);
123 g_free(devc->analog_groups);
124 g_free(devc->digital_groups);
128 static int dev_clear(const struct sr_dev_driver *di)
130 return std_dev_clear(di, clear_helper);
133 static int dev_open(struct sr_dev_inst *sdi)
135 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
138 if (dlm_scope_state_query(sdi) != SR_OK)
141 sdi->status = SR_ST_ACTIVE;
146 static int dev_close(struct sr_dev_inst *sdi)
148 if (sdi->status == SR_ST_INACTIVE)
151 sr_scpi_close(sdi->conn);
153 sdi->status = SR_ST_INACTIVE;
158 static int cleanup(const struct sr_dev_driver *di)
166 * Check which category a given channel group belongs to.
168 * @param devc Our internal device context.
169 * @param cg The channel group to check.
171 * @retval CG_NONE cg is NULL
172 * @retval CG_ANALOG cg is an analog group
173 * @retval CG_DIGITAL cg is a digital group
174 * @retval CG_INVALID cg is something else
176 static int check_channel_group(struct dev_context *devc,
177 const struct sr_channel_group *cg)
180 const struct scope_config *model;
182 model = devc->model_config;
187 for (i = 0; i < model->analog_channels; ++i)
188 if (cg == devc->analog_groups[i])
191 for (i = 0; i < model->pods; ++i)
192 if (cg == devc->digital_groups[i])
195 sr_err("Invalid channel group specified.");
199 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
200 const struct sr_channel_group *cg)
204 struct dev_context *devc;
205 const struct scope_config *model;
206 struct scope_state *state;
208 if (!sdi || !(devc = sdi->priv))
211 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
215 model = devc->model_config;
216 state = devc->model_state;
219 case SR_CONF_NUM_HDIV:
220 *data = g_variant_new_int32(model->num_xdivs);
223 case SR_CONF_TIMEBASE:
224 *data = g_variant_new("(tt)",
225 (*model->timebases)[state->timebase][0],
226 (*model->timebases)[state->timebase][1]);
229 case SR_CONF_NUM_VDIV:
230 if (cg_type == CG_NONE) {
231 sr_err("No channel group specified.");
232 return SR_ERR_CHANNEL_GROUP;
233 } else if (cg_type == CG_ANALOG) {
234 *data = g_variant_new_int32(model->num_ydivs);
243 if (cg_type == CG_NONE) {
244 sr_err("No channel group specified.");
245 return SR_ERR_CHANNEL_GROUP;
246 } else if (cg_type != CG_ANALOG)
249 for (i = 0; i < model->analog_channels; ++i) {
250 if (cg != devc->analog_groups[i])
252 *data = g_variant_new("(tt)",
253 (*model->vdivs)[state->analog_states[i].vdiv][0],
254 (*model->vdivs)[state->analog_states[i].vdiv][1]);
259 case SR_CONF_TRIGGER_SOURCE:
260 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
263 case SR_CONF_TRIGGER_SLOPE:
264 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
267 case SR_CONF_HORIZ_TRIGGERPOS:
268 *data = g_variant_new_double(state->horiz_triggerpos);
271 case SR_CONF_COUPLING:
273 if (cg_type == CG_NONE) {
274 sr_err("No channel group specified.");
275 return SR_ERR_CHANNEL_GROUP;
276 } else if (cg_type != CG_ANALOG)
279 for (i = 0; i < model->analog_channels; ++i) {
280 if (cg != devc->analog_groups[i])
282 *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
287 case SR_CONF_SAMPLERATE:
288 *data = g_variant_new_uint64(state->sample_rate);
298 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
301 GVariant *rational[2];
304 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
306 for (i = 0; i < n; i++) {
307 rational[0] = g_variant_new_uint64((*array)[i][0]);
308 rational[1] = g_variant_new_uint64((*array)[i][1]);
310 /* FIXME: Valgrind reports a memory leak here. */
311 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
314 return g_variant_builder_end(&gvb);
317 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
318 const struct sr_channel_group *cg)
323 struct dev_context *devc;
324 const struct scope_config *model;
325 struct scope_state *state;
329 gboolean update_sample_rate;
331 if (!sdi || !(devc = sdi->priv))
334 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
337 model = devc->model_config;
338 state = devc->model_state;
339 update_sample_rate = FALSE;
344 case SR_CONF_LIMIT_FRAMES:
345 devc->frame_limit = g_variant_get_uint64(data);
348 case SR_CONF_TRIGGER_SOURCE:
349 tmp = g_variant_get_string(data, NULL);
350 for (i = 0; (*model->trigger_sources)[i]; i++) {
351 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
353 state->trigger_source = i;
354 /* TODO: A and B trigger support possible? */
355 ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
360 if (cg_type == CG_NONE) {
361 sr_err("No channel group specified.");
362 return SR_ERR_CHANNEL_GROUP;
365 g_variant_get(data, "(tt)", &p, &q);
367 for (i = 0; i < model->num_vdivs; i++) {
368 if (p != (*model->vdivs)[i][0] ||
369 q != (*model->vdivs)[i][1])
371 for (j = 1; j <= model->analog_channels; ++j) {
372 if (cg != devc->analog_groups[j - 1])
374 state->analog_states[j - 1].vdiv = i;
375 g_ascii_formatd(float_str, sizeof(float_str),
376 "%E", (float) p / q);
377 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
378 sr_scpi_get_opc(sdi->conn) != SR_OK)
388 case SR_CONF_TIMEBASE:
389 g_variant_get(data, "(tt)", &p, &q);
391 for (i = 0; i < model->num_timebases; i++) {
392 if (p != (*model->timebases)[i][0] ||
393 q != (*model->timebases)[i][1])
396 g_ascii_formatd(float_str, sizeof(float_str),
397 "%E", (float) p / q);
398 ret = dlm_timebase_set(sdi->conn, float_str);
399 update_sample_rate = TRUE;
403 case SR_CONF_HORIZ_TRIGGERPOS:
404 tmp_d = g_variant_get_double(data);
406 /* TODO: Check if the calculation makes sense for the DLM. */
407 if (tmp_d < 0.0 || tmp_d > 1.0)
410 state->horiz_triggerpos = tmp_d;
411 tmp_d = -(tmp_d - 0.5) *
412 ((double) (*model->timebases)[state->timebase][0] /
413 (*model->timebases)[state->timebase][1])
416 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
417 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
419 case SR_CONF_TRIGGER_SLOPE:
420 tmp = g_variant_get_string(data, NULL);
422 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
425 /* Note: See dlm_trigger_slopes[] in protocol.c. */
426 state->trigger_slope = (tmp[0] == 'r') ?
427 SLOPE_POSITIVE : SLOPE_NEGATIVE;
429 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
431 case SR_CONF_COUPLING:
432 if (cg_type == CG_NONE) {
433 sr_err("No channel group specified.");
434 return SR_ERR_CHANNEL_GROUP;
437 tmp = g_variant_get_string(data, NULL);
439 for (i = 0; (*model->coupling_options)[i]; i++) {
440 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
442 for (j = 1; j <= model->analog_channels; ++j) {
443 if (cg != devc->analog_groups[j - 1])
445 state->analog_states[j-1].coupling = i;
447 if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
448 sr_scpi_get_opc(sdi->conn) != SR_OK)
463 ret = sr_scpi_get_opc(sdi->conn);
465 if (ret == SR_OK && update_sample_rate)
466 ret = dlm_sample_rate_query(sdi);
471 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
472 const struct sr_channel_group *cg)
474 int cg_type = CG_NONE;
475 struct dev_context *devc = NULL;
476 const struct scope_config *model = NULL;
478 if (sdi && (devc = sdi->priv)) {
479 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
482 model = devc->model_config;
486 case SR_CONF_DEVICE_OPTIONS:
487 if (cg_type == CG_NONE) {
489 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
490 model->devopts, model->num_devopts, sizeof(uint32_t));
492 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
493 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
494 } else if (cg_type == CG_ANALOG) {
495 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
496 model->analog_devopts, model->num_analog_devopts, sizeof(uint32_t));
498 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
499 NULL, 0, sizeof(uint32_t));
502 case SR_CONF_COUPLING:
503 if (cg_type == CG_NONE)
504 return SR_ERR_CHANNEL_GROUP;
505 *data = g_variant_new_strv(*model->coupling_options,
506 g_strv_length((char **)*model->coupling_options));
508 case SR_CONF_TRIGGER_SOURCE:
511 *data = g_variant_new_strv(*model->trigger_sources,
512 g_strv_length((char **)*model->trigger_sources));
514 case SR_CONF_TRIGGER_SLOPE:
517 *data = g_variant_new_strv(*model->trigger_slopes,
518 g_strv_length((char **)*model->trigger_slopes));
520 case SR_CONF_TIMEBASE:
523 *data = build_tuples(model->timebases, model->num_timebases);
526 if (cg_type == CG_NONE)
527 return SR_ERR_CHANNEL_GROUP;
528 *data = build_tuples(model->vdivs, model->num_vdivs);
537 static int dlm_check_channels(GSList *channels)
540 struct sr_channel *ch;
541 gboolean enabled_pod1, enabled_chan4;
543 enabled_pod1 = enabled_chan4 = FALSE;
545 /* Note: On the DLM2000, CH4 and Logic are shared. */
546 /* TODO Handle non-DLM2000 models. */
547 for (l = channels; l; l = l->next) {
550 case SR_CHANNEL_ANALOG:
552 enabled_chan4 = TRUE;
554 case SR_CHANNEL_LOGIC:
562 if (enabled_pod1 && enabled_chan4)
568 static int dlm_setup_channels(const struct sr_dev_inst *sdi)
572 gboolean *pod_enabled, setup_changed;
573 struct scope_state *state;
574 const struct scope_config *model;
575 struct sr_channel *ch;
576 struct dev_context *devc;
577 struct sr_scpi_dev_inst *scpi;
581 state = devc->model_state;
582 model = devc->model_config;
583 setup_changed = FALSE;
585 pod_enabled = g_malloc0(sizeof(gboolean) * model->pods);
587 for (l = sdi->channels; l; l = l->next) {
590 case SR_CHANNEL_ANALOG:
591 if (ch->enabled == state->analog_states[ch->index].state)
594 if (dlm_analog_chan_state_set(scpi, ch->index + 1,
595 ch->enabled) != SR_OK)
598 state->analog_states[ch->index].state = ch->enabled;
599 setup_changed = TRUE;
601 case SR_CHANNEL_LOGIC:
602 i = ch->index - DLM_DIG_CHAN_INDEX_OFFS;
604 pod_enabled[i / 8] = TRUE;
606 if (ch->enabled == state->digital_states[i])
609 if (dlm_digital_chan_state_set(scpi, i + 1,
610 ch->enabled) != SR_OK)
613 state->digital_states[i] = 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,