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->vendor = g_strdup(MANUFACTURER_NAME);
96 sdi->model = g_strdup(model_name);
97 sdi->version = g_strdup(hw_info->firmware_version);
99 sdi->serial_num = g_strdup(hw_info->serial_number);
101 sr_scpi_hw_info_free(hw_info);
104 devc = g_malloc0(sizeof(struct dev_context));
106 sdi->driver = &yokogawa_dlm_driver_info;
108 sdi->inst_type = SR_INST_SCPI;
111 if (dlm_device_init(sdi, model_index) != SR_OK)
118 sr_scpi_hw_info_free(hw_info);
120 sr_dev_inst_free(sdi);
126 static GSList *scan(struct sr_dev_driver *di, GSList *options)
128 return sr_scpi_scan(di->context, options, probe_usbtmc_device);
131 static GSList *dev_list(const struct sr_dev_driver *di)
133 return ((struct drv_context *)(di->context))->instances;
136 static void clear_helper(void *priv)
138 struct dev_context *devc;
142 dlm_scope_state_destroy(devc->model_state);
144 g_free(devc->analog_groups);
145 g_free(devc->digital_groups);
149 static int dev_clear(const struct sr_dev_driver *di)
151 return std_dev_clear(di, clear_helper);
154 static int dev_open(struct sr_dev_inst *sdi)
156 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
159 if (dlm_scope_state_query(sdi) != SR_OK)
162 sdi->status = SR_ST_ACTIVE;
167 static int dev_close(struct sr_dev_inst *sdi)
169 if (sdi->status == SR_ST_INACTIVE)
172 sr_scpi_close(sdi->conn);
174 sdi->status = SR_ST_INACTIVE;
179 static int cleanup(const struct sr_dev_driver *di)
187 * Check which category a given channel group belongs to.
189 * @param devc Our internal device context.
190 * @param cg The channel group to check.
192 * @retval CG_NONE cg is NULL
193 * @retval CG_ANALOG cg is an analog group
194 * @retval CG_DIGITAL cg is a digital group
195 * @retval CG_INVALID cg is something else
197 static int check_channel_group(struct dev_context *devc,
198 const struct sr_channel_group *cg)
201 const struct scope_config *model;
203 model = devc->model_config;
208 for (i = 0; i < model->analog_channels; i++)
209 if (cg == devc->analog_groups[i])
212 for (i = 0; i < model->pods; i++)
213 if (cg == devc->digital_groups[i])
216 sr_err("Invalid channel group specified.");
220 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
221 const struct sr_channel_group *cg)
225 struct dev_context *devc;
226 const struct scope_config *model;
227 struct scope_state *state;
229 if (!sdi || !(devc = sdi->priv))
232 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
235 model = devc->model_config;
236 state = devc->model_state;
239 case SR_CONF_NUM_HDIV:
240 *data = g_variant_new_int32(model->num_xdivs);
243 case SR_CONF_TIMEBASE:
244 *data = g_variant_new("(tt)",
245 dlm_timebases[state->timebase][0],
246 dlm_timebases[state->timebase][1]);
249 case SR_CONF_NUM_VDIV:
250 if (cg_type == CG_NONE) {
251 sr_err("No channel group specified.");
252 return SR_ERR_CHANNEL_GROUP;
253 } else if (cg_type == CG_ANALOG) {
254 *data = g_variant_new_int32(model->num_ydivs);
263 if (cg_type == CG_NONE) {
264 sr_err("No channel group specified.");
265 return SR_ERR_CHANNEL_GROUP;
266 } else if (cg_type != CG_ANALOG)
269 for (i = 0; i < model->analog_channels; i++) {
270 if (cg != devc->analog_groups[i])
272 *data = g_variant_new("(tt)",
273 dlm_vdivs[state->analog_states[i].vdiv][0],
274 dlm_vdivs[state->analog_states[i].vdiv][1]);
279 case SR_CONF_TRIGGER_SOURCE:
280 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
283 case SR_CONF_TRIGGER_SLOPE:
284 *data = g_variant_new_string(dlm_trigger_slopes[state->trigger_slope]);
287 case SR_CONF_HORIZ_TRIGGERPOS:
288 *data = g_variant_new_double(state->horiz_triggerpos);
291 case SR_CONF_COUPLING:
293 if (cg_type == CG_NONE) {
294 sr_err("No channel group specified.");
295 return SR_ERR_CHANNEL_GROUP;
296 } else if (cg_type != CG_ANALOG)
299 for (i = 0; i < model->analog_channels; i++) {
300 if (cg != devc->analog_groups[i])
302 *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
307 case SR_CONF_SAMPLERATE:
308 *data = g_variant_new_uint64(state->sample_rate);
318 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
321 GVariant *rational[2];
324 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
326 for (i = 0; i < n; i++) {
327 rational[0] = g_variant_new_uint64((*array)[i][0]);
328 rational[1] = g_variant_new_uint64((*array)[i][1]);
330 /* FIXME: Valgrind reports a memory leak here. */
331 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
334 return g_variant_builder_end(&gvb);
337 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
338 const struct sr_channel_group *cg)
343 struct dev_context *devc;
344 const struct scope_config *model;
345 struct scope_state *state;
349 gboolean update_sample_rate;
351 if (!sdi || !(devc = sdi->priv))
354 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
357 model = devc->model_config;
358 state = devc->model_state;
359 update_sample_rate = FALSE;
364 case SR_CONF_LIMIT_FRAMES:
365 devc->frame_limit = g_variant_get_uint64(data);
368 case SR_CONF_TRIGGER_SOURCE:
369 tmp = g_variant_get_string(data, NULL);
370 for (i = 0; (*model->trigger_sources)[i]; i++) {
371 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
373 state->trigger_source = i;
374 /* TODO: A and B trigger support possible? */
375 ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
380 if (cg_type == CG_NONE) {
381 sr_err("No channel group specified.");
382 return SR_ERR_CHANNEL_GROUP;
385 g_variant_get(data, "(tt)", &p, &q);
387 for (i = 0; i < ARRAY_SIZE(dlm_vdivs); i++) {
388 if (p != dlm_vdivs[i][0] ||
389 q != dlm_vdivs[i][1])
391 for (j = 1; j <= model->analog_channels; j++) {
392 if (cg != devc->analog_groups[j - 1])
394 state->analog_states[j - 1].vdiv = i;
395 g_ascii_formatd(float_str, sizeof(float_str),
396 "%E", (float) p / q);
397 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
398 sr_scpi_get_opc(sdi->conn) != SR_OK)
408 case SR_CONF_TIMEBASE:
409 g_variant_get(data, "(tt)", &p, &q);
411 for (i = 0; i < ARRAY_SIZE(dlm_timebases); i++) {
412 if (p != dlm_timebases[i][0] ||
413 q != dlm_timebases[i][1])
416 g_ascii_formatd(float_str, sizeof(float_str),
417 "%E", (float) p / q);
418 ret = dlm_timebase_set(sdi->conn, float_str);
419 update_sample_rate = TRUE;
423 case SR_CONF_HORIZ_TRIGGERPOS:
424 tmp_d = g_variant_get_double(data);
426 /* TODO: Check if the calculation makes sense for the DLM. */
427 if (tmp_d < 0.0 || tmp_d > 1.0)
430 state->horiz_triggerpos = tmp_d;
431 tmp_d = -(tmp_d - 0.5) *
432 ((double) dlm_timebases[state->timebase][0] /
433 dlm_timebases[state->timebase][1])
436 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
437 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
439 case SR_CONF_TRIGGER_SLOPE:
440 tmp = g_variant_get_string(data, NULL);
442 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
445 /* Note: See dlm_trigger_slopes[] in protocol.c. */
446 state->trigger_slope = (tmp[0] == 'r') ?
447 SLOPE_POSITIVE : SLOPE_NEGATIVE;
449 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
451 case SR_CONF_COUPLING:
452 if (cg_type == CG_NONE) {
453 sr_err("No channel group specified.");
454 return SR_ERR_CHANNEL_GROUP;
457 tmp = g_variant_get_string(data, NULL);
459 for (i = 0; (*model->coupling_options)[i]; i++) {
460 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
462 for (j = 1; j <= model->analog_channels; j++) {
463 if (cg != devc->analog_groups[j - 1])
465 state->analog_states[j-1].coupling = i;
467 if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
468 sr_scpi_get_opc(sdi->conn) != SR_OK)
483 ret = sr_scpi_get_opc(sdi->conn);
485 if (ret == SR_OK && update_sample_rate)
486 ret = dlm_sample_rate_query(sdi);
491 static int config_channel_set(const struct sr_dev_inst *sdi,
492 struct sr_channel *ch, unsigned int changes)
494 /* Currently we only handle SR_CHANNEL_SET_ENABLED. */
495 if (changes != SR_CHANNEL_SET_ENABLED)
498 return dlm_channel_state_set(sdi, ch->index, ch->enabled);
501 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
502 const struct sr_channel_group *cg)
504 int cg_type = CG_NONE;
505 struct dev_context *devc = NULL;
506 const struct scope_config *model = NULL;
508 /* SR_CONF_SCAN_OPTIONS is always valid, regardless of sdi or probe group. */
509 if (key == SR_CONF_SCAN_OPTIONS) {
510 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
511 dlm_scanopts, ARRAY_SIZE(dlm_scanopts), sizeof(uint32_t));
515 /* If sdi is NULL, nothing except SR_CONF_DEVICE_OPTIONS can be provided. */
516 if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
517 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
518 dlm_drvopts, ARRAY_SIZE(dlm_drvopts), sizeof(uint32_t));
526 model = devc->model_config;
529 * If cg is NULL, only the SR_CONF_DEVICE_OPTIONS that are not
530 * specific to a probe group must be returned.
534 case SR_CONF_DEVICE_OPTIONS:
535 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
536 dlm_devopts, ARRAY_SIZE(dlm_devopts), sizeof(uint32_t));
538 case SR_CONF_TIMEBASE:
539 *data = build_tuples(&dlm_timebases, ARRAY_SIZE(dlm_timebases));
541 case SR_CONF_TRIGGER_SOURCE:
544 *data = g_variant_new_strv(*model->trigger_sources,
545 g_strv_length((char **)*model->trigger_sources));
547 case SR_CONF_TRIGGER_SLOPE:
548 *data = g_variant_new_strv(dlm_trigger_slopes,
549 g_strv_length((char **)dlm_trigger_slopes));
551 case SR_CONF_NUM_HDIV:
552 *data = g_variant_new_uint32(model->num_xdivs);
559 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
563 case SR_CONF_DEVICE_OPTIONS:
564 if (cg_type == CG_ANALOG) {
565 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
566 dlm_analog_devopts, ARRAY_SIZE(dlm_analog_devopts), sizeof(uint32_t));
567 } else if (cg_type == CG_DIGITAL) {
568 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
569 dlm_digital_devopts, ARRAY_SIZE(dlm_digital_devopts), sizeof(uint32_t));
571 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
572 NULL, 0, sizeof(uint32_t));
575 case SR_CONF_COUPLING:
576 if (cg_type == CG_NONE)
577 return SR_ERR_CHANNEL_GROUP;
578 *data = g_variant_new_strv(*model->coupling_options,
579 g_strv_length((char **)*model->coupling_options));
582 if (cg_type == CG_NONE)
583 return SR_ERR_CHANNEL_GROUP;
584 *data = build_tuples(&dlm_vdivs, ARRAY_SIZE(dlm_vdivs));
593 static int dlm_check_channels(GSList *channels)
596 struct sr_channel *ch;
597 gboolean enabled_pod1, enabled_chan4;
599 enabled_pod1 = enabled_chan4 = FALSE;
601 /* Note: On the DLM2000, CH4 and Logic are shared. */
602 /* TODO Handle non-DLM2000 models. */
603 for (l = channels; l; l = l->next) {
606 case SR_CHANNEL_ANALOG:
608 enabled_chan4 = TRUE;
610 case SR_CHANNEL_LOGIC:
618 if (enabled_pod1 && enabled_chan4)
624 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
627 gboolean digital_added;
628 struct sr_channel *ch;
629 struct dev_context *devc;
630 struct sr_scpi_dev_inst *scpi;
634 if (sdi->status != SR_ST_ACTIVE)
635 return SR_ERR_DEV_CLOSED;
639 digital_added = FALSE;
641 g_slist_free(devc->enabled_channels);
642 devc->enabled_channels = NULL;
644 for (l = sdi->channels; l; l = l->next) {
648 /* Only add a single digital channel. */
649 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
650 devc->enabled_channels = g_slist_append(
651 devc->enabled_channels, ch);
652 if (ch->type == SR_CHANNEL_LOGIC)
653 digital_added = TRUE;
657 if (!devc->enabled_channels)
660 if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
661 sr_err("Invalid channel configuration specified!");
665 /* Request data for the first enabled channel. */
666 devc->current_channel = devc->enabled_channels;
667 dlm_channel_data_request(sdi);
669 /* Call our callback when data comes in or after 5ms. */
670 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5,
671 dlm_data_receive, (void *)sdi);
676 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
678 struct dev_context *devc;
679 struct sr_datafeed_packet packet;
683 packet.type = SR_DF_END;
684 packet.payload = NULL;
685 sr_session_send(sdi, &packet);
687 if (sdi->status != SR_ST_ACTIVE)
688 return SR_ERR_DEV_CLOSED;
692 devc->num_frames = 0;
693 g_slist_free(devc->enabled_channels);
694 devc->enabled_channels = NULL;
696 sr_scpi_source_remove(sdi->session, sdi->conn);
701 SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info = {
702 .name = "yokogawa-dlm",
703 .longname = "Yokogawa DL/DLM",
708 .dev_list = dev_list,
709 .dev_clear = dev_clear,
710 .config_get = config_get,
711 .config_set = config_set,
712 .config_channel_set = config_channel_set,
713 .config_list = config_list,
714 .dev_open = dev_open,
715 .dev_close = dev_close,
716 .dev_acquisition_start = dev_acquisition_start,
717 .dev_acquisition_stop = dev_acquisition_stop,