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 static 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_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
47 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
50 static const uint32_t dlm_analog_devopts[] = {
51 SR_CONF_NUM_VDIV | SR_CONF_GET,
52 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
53 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
56 static const uint32_t dlm_digital_devopts[] = {
66 static struct sr_dev_inst *probe_usbtmc_device(struct sr_scpi_dev_inst *scpi)
68 struct sr_dev_inst *sdi;
69 struct dev_context *devc;
70 struct sr_scpi_hw_info *hw_info;
78 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
79 sr_info("Couldn't get IDN response.");
83 if (strcmp(hw_info->manufacturer, MANUFACTURER_ID) != 0)
86 if (dlm_model_get(hw_info->model, &model_name, &model_index) != SR_OK)
89 sdi = g_malloc0(sizeof(struct sr_dev_inst));
90 sdi->vendor = g_strdup(MANUFACTURER_NAME);
91 sdi->model = g_strdup(model_name);
92 sdi->version = g_strdup(hw_info->firmware_version);
94 sdi->serial_num = g_strdup(hw_info->serial_number);
96 sr_scpi_hw_info_free(hw_info);
99 devc = g_malloc0(sizeof(struct dev_context));
101 sdi->driver = &yokogawa_dlm_driver_info;
103 sdi->inst_type = SR_INST_SCPI;
106 if (dlm_device_init(sdi, model_index) != SR_OK)
112 sr_scpi_hw_info_free(hw_info);
113 sr_dev_inst_free(sdi);
119 static GSList *scan(struct sr_dev_driver *di, GSList *options)
121 return sr_scpi_scan(di->context, options, probe_usbtmc_device);
124 static void clear_helper(struct dev_context *devc)
126 dlm_scope_state_destroy(devc->model_state);
127 g_free(devc->analog_groups);
128 g_free(devc->digital_groups);
131 static int dev_clear(const struct sr_dev_driver *di)
133 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
136 static int dev_open(struct sr_dev_inst *sdi)
138 if (sr_scpi_open(sdi->conn) != SR_OK)
141 if (dlm_scope_state_query(sdi) != SR_OK)
147 static int dev_close(struct sr_dev_inst *sdi)
149 return sr_scpi_close(sdi->conn);
153 * Check which category a given channel group belongs to.
155 * @param devc Our internal device context.
156 * @param cg The channel group to check.
158 * @retval CG_NONE cg is NULL
159 * @retval CG_ANALOG cg is an analog group
160 * @retval CG_DIGITAL cg is a digital group
161 * @retval CG_INVALID cg is something else
163 static int check_channel_group(struct dev_context *devc,
164 const struct sr_channel_group *cg)
167 const struct scope_config *model;
169 model = devc->model_config;
174 for (i = 0; i < model->analog_channels; i++)
175 if (cg == devc->analog_groups[i])
178 for (i = 0; i < model->pods; i++)
179 if (cg == devc->digital_groups[i])
182 sr_err("Invalid channel group specified.");
186 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
187 const struct sr_channel_group *cg)
191 struct dev_context *devc;
192 const struct scope_config *model;
193 struct scope_state *state;
200 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
203 model = devc->model_config;
204 state = devc->model_state;
207 case SR_CONF_NUM_HDIV:
208 *data = g_variant_new_int32(model->num_xdivs);
211 case SR_CONF_TIMEBASE:
212 *data = g_variant_new("(tt)",
213 dlm_timebases[state->timebase][0],
214 dlm_timebases[state->timebase][1]);
217 case SR_CONF_NUM_VDIV:
218 if (cg_type == CG_NONE) {
219 sr_err("No channel group specified.");
220 return SR_ERR_CHANNEL_GROUP;
221 } else if (cg_type == CG_ANALOG) {
222 *data = g_variant_new_int32(model->num_ydivs);
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)
237 for (i = 0; i < model->analog_channels; i++) {
238 if (cg != devc->analog_groups[i])
240 *data = g_variant_new("(tt)",
241 dlm_vdivs[state->analog_states[i].vdiv][0],
242 dlm_vdivs[state->analog_states[i].vdiv][1]);
247 case SR_CONF_TRIGGER_SOURCE:
248 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
251 case SR_CONF_TRIGGER_SLOPE:
252 *data = g_variant_new_string(dlm_trigger_slopes[state->trigger_slope]);
255 case SR_CONF_HORIZ_TRIGGERPOS:
256 *data = g_variant_new_double(state->horiz_triggerpos);
259 case SR_CONF_COUPLING:
261 if (cg_type == CG_NONE) {
262 sr_err("No channel group specified.");
263 return SR_ERR_CHANNEL_GROUP;
264 } else if (cg_type != CG_ANALOG)
267 for (i = 0; i < model->analog_channels; i++) {
268 if (cg != devc->analog_groups[i])
270 *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
275 case SR_CONF_SAMPLERATE:
276 *data = g_variant_new_uint64(state->sample_rate);
286 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
289 GVariant *rational[2];
292 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
294 for (i = 0; i < n; i++) {
295 rational[0] = g_variant_new_uint64((*array)[i][0]);
296 rational[1] = g_variant_new_uint64((*array)[i][1]);
298 /* FIXME: Valgrind reports a memory leak here. */
299 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
302 return g_variant_builder_end(&gvb);
305 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
306 const struct sr_channel_group *cg)
311 struct dev_context *devc;
312 const struct scope_config *model;
313 struct scope_state *state;
317 gboolean update_sample_rate;
319 if (!sdi || !(devc = sdi->priv))
322 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
325 model = devc->model_config;
326 state = devc->model_state;
327 update_sample_rate = FALSE;
332 case SR_CONF_LIMIT_FRAMES:
333 devc->frame_limit = g_variant_get_uint64(data);
336 case SR_CONF_TRIGGER_SOURCE:
337 tmp = g_variant_get_string(data, NULL);
338 for (i = 0; (*model->trigger_sources)[i]; i++) {
339 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
341 state->trigger_source = i;
342 /* TODO: A and B trigger support possible? */
343 ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
348 if (cg_type == CG_NONE) {
349 sr_err("No channel group specified.");
350 return SR_ERR_CHANNEL_GROUP;
353 g_variant_get(data, "(tt)", &p, &q);
355 for (i = 0; i < ARRAY_SIZE(dlm_vdivs); i++) {
356 if (p != dlm_vdivs[i][0] ||
357 q != dlm_vdivs[i][1])
359 for (j = 1; j <= model->analog_channels; j++) {
360 if (cg != devc->analog_groups[j - 1])
362 state->analog_states[j - 1].vdiv = i;
363 g_ascii_formatd(float_str, sizeof(float_str),
364 "%E", (float) p / q);
365 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
366 sr_scpi_get_opc(sdi->conn) != SR_OK)
376 case SR_CONF_TIMEBASE:
377 g_variant_get(data, "(tt)", &p, &q);
379 for (i = 0; i < ARRAY_SIZE(dlm_timebases); i++) {
380 if (p != dlm_timebases[i][0] ||
381 q != dlm_timebases[i][1])
384 g_ascii_formatd(float_str, sizeof(float_str),
385 "%E", (float) p / q);
386 ret = dlm_timebase_set(sdi->conn, float_str);
387 update_sample_rate = TRUE;
391 case SR_CONF_HORIZ_TRIGGERPOS:
392 tmp_d = g_variant_get_double(data);
394 /* TODO: Check if the calculation makes sense for the DLM. */
395 if (tmp_d < 0.0 || tmp_d > 1.0)
398 state->horiz_triggerpos = tmp_d;
399 tmp_d = -(tmp_d - 0.5) *
400 ((double) dlm_timebases[state->timebase][0] /
401 dlm_timebases[state->timebase][1])
404 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
405 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
407 case SR_CONF_TRIGGER_SLOPE:
408 tmp = g_variant_get_string(data, NULL);
410 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
413 /* Note: See dlm_trigger_slopes[] in protocol.c. */
414 state->trigger_slope = (tmp[0] == 'r') ?
415 SLOPE_POSITIVE : SLOPE_NEGATIVE;
417 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
419 case SR_CONF_COUPLING:
420 if (cg_type == CG_NONE) {
421 sr_err("No channel group specified.");
422 return SR_ERR_CHANNEL_GROUP;
425 tmp = g_variant_get_string(data, NULL);
427 for (i = 0; (*model->coupling_options)[i]; i++) {
428 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
430 for (j = 1; j <= model->analog_channels; j++) {
431 if (cg != devc->analog_groups[j - 1])
433 state->analog_states[j-1].coupling = i;
435 if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
436 sr_scpi_get_opc(sdi->conn) != SR_OK)
451 ret = sr_scpi_get_opc(sdi->conn);
453 if (ret == SR_OK && update_sample_rate)
454 ret = dlm_sample_rate_query(sdi);
459 static int config_channel_set(const struct sr_dev_inst *sdi,
460 struct sr_channel *ch, unsigned int changes)
462 /* Currently we only handle SR_CHANNEL_SET_ENABLED. */
463 if (changes != SR_CHANNEL_SET_ENABLED)
466 return dlm_channel_state_set(sdi, ch->index, ch->enabled);
469 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
470 const struct sr_channel_group *cg)
472 int cg_type = CG_NONE;
473 struct dev_context *devc;
474 const struct scope_config *model;
476 devc = (sdi) ? sdi->priv : NULL;
477 model = (devc) ? devc->model_config : NULL;
481 case SR_CONF_SCAN_OPTIONS:
482 case SR_CONF_DEVICE_OPTIONS:
483 return STD_CONFIG_LIST(key, data, sdi, cg, dlm_scanopts, dlm_drvopts, dlm_devopts);
484 case SR_CONF_TIMEBASE:
485 *data = build_tuples(&dlm_timebases, ARRAY_SIZE(dlm_timebases));
487 case SR_CONF_TRIGGER_SOURCE:
490 *data = g_variant_new_strv(*model->trigger_sources,
491 g_strv_length((char **)*model->trigger_sources));
493 case SR_CONF_TRIGGER_SLOPE:
494 *data = g_variant_new_strv(dlm_trigger_slopes,
495 g_strv_length((char **)dlm_trigger_slopes));
497 case SR_CONF_NUM_HDIV:
498 *data = g_variant_new_uint32(model->num_xdivs);
505 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
509 case SR_CONF_DEVICE_OPTIONS:
510 if (cg_type == CG_ANALOG) {
511 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
512 dlm_analog_devopts, ARRAY_SIZE(dlm_analog_devopts), sizeof(uint32_t));
513 } else if (cg_type == CG_DIGITAL) {
514 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
515 dlm_digital_devopts, ARRAY_SIZE(dlm_digital_devopts), sizeof(uint32_t));
517 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
518 NULL, 0, sizeof(uint32_t));
521 case SR_CONF_COUPLING:
522 if (cg_type == CG_NONE)
523 return SR_ERR_CHANNEL_GROUP;
524 *data = g_variant_new_strv(*model->coupling_options,
525 g_strv_length((char **)*model->coupling_options));
528 if (cg_type == CG_NONE)
529 return SR_ERR_CHANNEL_GROUP;
530 *data = build_tuples(&dlm_vdivs, ARRAY_SIZE(dlm_vdivs));
539 static int dlm_check_channels(GSList *channels)
542 struct sr_channel *ch;
543 gboolean enabled_pod1, enabled_chan4;
545 enabled_pod1 = enabled_chan4 = FALSE;
547 /* Note: On the DLM2000, CH4 and Logic are shared. */
548 /* TODO Handle non-DLM2000 models. */
549 for (l = channels; l; l = l->next) {
552 case SR_CHANNEL_ANALOG:
554 enabled_chan4 = TRUE;
556 case SR_CHANNEL_LOGIC:
564 if (enabled_pod1 && enabled_chan4)
570 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
573 gboolean digital_added;
574 struct sr_channel *ch;
575 struct dev_context *devc;
576 struct sr_scpi_dev_inst *scpi;
580 digital_added = FALSE;
582 g_slist_free(devc->enabled_channels);
583 devc->enabled_channels = NULL;
585 for (l = sdi->channels; l; l = l->next) {
589 /* Only add a single digital channel. */
590 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
591 devc->enabled_channels = g_slist_append(
592 devc->enabled_channels, ch);
593 if (ch->type == SR_CHANNEL_LOGIC)
594 digital_added = TRUE;
598 if (!devc->enabled_channels)
601 if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
602 sr_err("Invalid channel configuration specified!");
606 /* Request data for the first enabled channel. */
607 devc->current_channel = devc->enabled_channels;
608 dlm_channel_data_request(sdi);
610 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5,
611 dlm_data_receive, (void *)sdi);
616 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
618 struct dev_context *devc;
620 std_session_send_df_end(sdi);
624 devc->num_frames = 0;
625 g_slist_free(devc->enabled_channels);
626 devc->enabled_channels = NULL;
628 sr_scpi_source_remove(sdi->session, sdi->conn);
633 static struct sr_dev_driver yokogawa_dlm_driver_info = {
634 .name = "yokogawa-dlm",
635 .longname = "Yokogawa DL/DLM",
638 .cleanup = std_cleanup,
640 .dev_list = std_dev_list,
641 .dev_clear = dev_clear,
642 .config_get = config_get,
643 .config_set = config_set,
644 .config_channel_set = config_channel_set,
645 .config_list = config_list,
646 .dev_open = dev_open,
647 .dev_close = dev_close,
648 .dev_acquisition_start = dev_acquisition_start,
649 .dev_acquisition_stop = dev_acquisition_stop,
652 SR_REGISTER_DEV_DRIVER(yokogawa_dlm_driver_info);