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";
30 static const uint32_t drvopts[] = {
31 SR_CONF_LOGIC_ANALYZER,
42 static int init(struct sr_context *sr_ctx)
44 return std_init(sr_ctx, di, LOG_PREFIX);
47 static struct sr_dev_inst *probe_usbtmc_device(struct sr_scpi_dev_inst *scpi)
49 struct sr_dev_inst *sdi;
50 struct dev_context *devc;
51 struct sr_scpi_hw_info *hw_info;
59 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
60 sr_info("Couldn't get IDN response.");
64 if (strcmp(hw_info->manufacturer, MANUFACTURER_ID) != 0)
67 if (dlm_model_get(hw_info->model, &model_name, &model_index) != SR_OK)
70 sdi = g_malloc0(sizeof(struct sr_dev_inst));
71 sdi->status = SR_ST_ACTIVE;
72 sdi->vendor = g_strdup(MANUFACTURER_NAME);
73 sdi->model = g_strdup(model_name);
74 sdi->version = g_strdup(hw_info->firmware_version);
76 sdi->serial_num = g_strdup(hw_info->serial_number);
78 sr_scpi_hw_info_free(hw_info);
81 devc = g_malloc0(sizeof(struct dev_context));
85 sdi->inst_type = SR_INST_SCPI;
88 if (dlm_device_init(sdi, model_index) != SR_OK)
91 sr_scpi_close(sdi->conn);
93 sdi->status = SR_ST_INACTIVE;
98 sr_scpi_hw_info_free(hw_info);
100 sr_dev_inst_free(sdi);
107 static GSList *scan(GSList *options)
109 return sr_scpi_scan(di->priv, options, probe_usbtmc_device);
112 static GSList *dev_list(void)
114 return ((struct drv_context *)(di->priv))->instances;
117 static void clear_helper(void *priv)
119 struct dev_context *devc;
123 dlm_scope_state_destroy(devc->model_state);
125 g_free(devc->analog_groups);
126 g_free(devc->digital_groups);
130 static int dev_clear(void)
132 return std_dev_clear(di, clear_helper);
135 static int dev_open(struct sr_dev_inst *sdi)
137 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
140 if (dlm_scope_state_query(sdi) != SR_OK)
143 sdi->status = SR_ST_ACTIVE;
148 static int dev_close(struct sr_dev_inst *sdi)
150 if (sdi->status == SR_ST_INACTIVE)
153 sr_scpi_close(sdi->conn);
155 sdi->status = SR_ST_INACTIVE;
160 static int cleanup(void)
168 * Check which category a given channel group belongs to.
170 * @param devc Our internal device context.
171 * @param cg The channel group to check.
173 * @retval CG_NONE cg is NULL
174 * @retval CG_ANALOG cg is an analog group
175 * @retval CG_DIGITAL cg is a digital group
176 * @retval CG_INVALID cg is something else
178 static int check_channel_group(struct dev_context *devc,
179 const struct sr_channel_group *cg)
182 struct scope_config *model;
184 model = devc->model_config;
189 for (i = 0; i < model->analog_channels; ++i)
190 if (cg == devc->analog_groups[i])
193 for (i = 0; i < model->pods; ++i)
194 if (cg == devc->digital_groups[i])
197 sr_err("Invalid channel group specified.");
201 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
202 const struct sr_channel_group *cg)
206 struct dev_context *devc;
207 struct scope_config *model;
208 struct scope_state *state;
210 if (!sdi || !(devc = sdi->priv))
213 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
217 model = devc->model_config;
218 state = devc->model_state;
221 case SR_CONF_NUM_HDIV:
222 *data = g_variant_new_int32(model->num_xdivs);
225 case SR_CONF_TIMEBASE:
226 *data = g_variant_new("(tt)",
227 (*model->timebases)[state->timebase][0],
228 (*model->timebases)[state->timebase][1]);
231 case SR_CONF_NUM_VDIV:
232 if (cg_type == CG_NONE) {
233 sr_err("No channel group specified.");
234 return SR_ERR_CHANNEL_GROUP;
235 } else if (cg_type == CG_ANALOG) {
236 *data = g_variant_new_int32(model->num_ydivs);
245 if (cg_type == CG_NONE) {
246 sr_err("No channel group specified.");
247 return SR_ERR_CHANNEL_GROUP;
248 } else if (cg_type != CG_ANALOG)
251 for (i = 0; i < model->analog_channels; ++i) {
252 if (cg != devc->analog_groups[i])
254 *data = g_variant_new("(tt)",
255 (*model->vdivs)[state->analog_states[i].vdiv][0],
256 (*model->vdivs)[state->analog_states[i].vdiv][1]);
261 case SR_CONF_TRIGGER_SOURCE:
262 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
265 case SR_CONF_TRIGGER_SLOPE:
266 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
269 case SR_CONF_HORIZ_TRIGGERPOS:
270 *data = g_variant_new_double(state->horiz_triggerpos);
273 case SR_CONF_COUPLING:
275 if (cg_type == CG_NONE) {
276 sr_err("No channel group specified.");
277 return SR_ERR_CHANNEL_GROUP;
278 } else if (cg_type != CG_ANALOG)
281 for (i = 0; i < model->analog_channels; ++i) {
282 if (cg != devc->analog_groups[i])
284 *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
289 case SR_CONF_SAMPLERATE:
290 *data = g_variant_new_uint64(state->sample_rate);
300 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
303 GVariant *rational[2];
306 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
308 for (i = 0; i < n; i++) {
309 rational[0] = g_variant_new_uint64((*array)[i][0]);
310 rational[1] = g_variant_new_uint64((*array)[i][1]);
312 /* FIXME: Valgrind reports a memory leak here. */
313 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
316 return g_variant_builder_end(&gvb);
319 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
320 const struct sr_channel_group *cg)
325 struct dev_context *devc;
326 struct scope_config *model;
327 struct scope_state *state;
331 gboolean update_sample_rate;
333 if (!sdi || !(devc = sdi->priv))
336 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
339 model = devc->model_config;
340 state = devc->model_state;
341 update_sample_rate = FALSE;
346 case SR_CONF_LIMIT_FRAMES:
347 devc->frame_limit = g_variant_get_uint64(data);
350 case SR_CONF_TRIGGER_SOURCE:
351 tmp = g_variant_get_string(data, NULL);
352 for (i = 0; (*model->trigger_sources)[i]; i++) {
353 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
355 state->trigger_source = i;
356 /* TODO: A and B trigger support possible? */
357 ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
362 if (cg_type == CG_NONE) {
363 sr_err("No channel group specified.");
364 return SR_ERR_CHANNEL_GROUP;
367 g_variant_get(data, "(tt)", &p, &q);
369 for (i = 0; i < model->num_vdivs; i++) {
370 if (p != (*model->vdivs)[i][0] ||
371 q != (*model->vdivs)[i][1])
373 for (j = 1; j <= model->analog_channels; ++j) {
374 if (cg != devc->analog_groups[j - 1])
376 state->analog_states[j - 1].vdiv = i;
377 g_ascii_formatd(float_str, sizeof(float_str),
378 "%E", (float) p / q);
379 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
380 sr_scpi_get_opc(sdi->conn) != SR_OK)
390 case SR_CONF_TIMEBASE:
391 g_variant_get(data, "(tt)", &p, &q);
393 for (i = 0; i < model->num_timebases; i++) {
394 if (p != (*model->timebases)[i][0] ||
395 q != (*model->timebases)[i][1])
398 g_ascii_formatd(float_str, sizeof(float_str),
399 "%E", (float) p / q);
400 ret = dlm_timebase_set(sdi->conn, float_str);
401 update_sample_rate = TRUE;
405 case SR_CONF_HORIZ_TRIGGERPOS:
406 tmp_d = g_variant_get_double(data);
408 /* TODO: Check if the calculation makes sense for the DLM. */
409 if (tmp_d < 0.0 || tmp_d > 1.0)
412 state->horiz_triggerpos = tmp_d;
413 tmp_d = -(tmp_d - 0.5) *
414 ((double) (*model->timebases)[state->timebase][0] /
415 (*model->timebases)[state->timebase][1])
418 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
419 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
421 case SR_CONF_TRIGGER_SLOPE:
422 tmp = g_variant_get_string(data, NULL);
424 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
427 /* Note: See dlm_trigger_slopes[] in protocol.c. */
428 state->trigger_slope = (tmp[0] == 'r') ?
429 SLOPE_POSITIVE : SLOPE_NEGATIVE;
431 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
433 case SR_CONF_COUPLING:
434 if (cg_type == CG_NONE) {
435 sr_err("No channel group specified.");
436 return SR_ERR_CHANNEL_GROUP;
439 tmp = g_variant_get_string(data, NULL);
441 for (i = 0; (*model->coupling_options)[i]; i++) {
442 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
444 for (j = 1; j <= model->analog_channels; ++j) {
445 if (cg != devc->analog_groups[j - 1])
447 state->analog_states[j-1].coupling = i;
449 if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
450 sr_scpi_get_opc(sdi->conn) != SR_OK)
465 ret = sr_scpi_get_opc(sdi->conn);
467 if (ret == SR_OK && update_sample_rate)
468 ret = dlm_sample_rate_query(sdi);
473 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
474 const struct sr_channel_group *cg)
476 int cg_type = CG_NONE;
477 struct dev_context *devc = NULL;
478 struct scope_config *model = NULL;
480 if (sdi && (devc = sdi->priv)) {
481 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
484 model = devc->model_config;
488 case SR_CONF_DEVICE_OPTIONS:
489 if (cg_type == CG_NONE) {
491 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
492 model->devopts, model->num_devopts, sizeof(uint32_t));
494 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
495 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
496 } else if (cg_type == CG_ANALOG) {
497 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
498 model->analog_devopts, model->num_analog_devopts, sizeof(uint32_t));
500 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
501 NULL, 0, sizeof(uint32_t));
504 case SR_CONF_COUPLING:
505 if (cg_type == CG_NONE)
506 return SR_ERR_CHANNEL_GROUP;
507 *data = g_variant_new_strv(*model->coupling_options,
508 g_strv_length((char **)*model->coupling_options));
510 case SR_CONF_TRIGGER_SOURCE:
513 *data = g_variant_new_strv(*model->trigger_sources,
514 g_strv_length((char **)*model->trigger_sources));
516 case SR_CONF_TRIGGER_SLOPE:
519 *data = g_variant_new_strv(*model->trigger_slopes,
520 g_strv_length((char **)*model->trigger_slopes));
522 case SR_CONF_TIMEBASE:
525 *data = build_tuples(model->timebases, model->num_timebases);
528 if (cg_type == CG_NONE)
529 return SR_ERR_CHANNEL_GROUP;
530 *data = build_tuples(model->vdivs, model->num_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 dlm_setup_channels(const struct sr_dev_inst *sdi)
574 gboolean *pod_enabled, setup_changed;
575 struct scope_state *state;
576 struct scope_config *model;
577 struct sr_channel *ch;
578 struct dev_context *devc;
579 struct sr_scpi_dev_inst *scpi;
583 state = devc->model_state;
584 model = devc->model_config;
585 setup_changed = FALSE;
587 pod_enabled = g_malloc0(sizeof(gboolean) * model->pods);
589 for (l = sdi->channels; l; l = l->next) {
592 case SR_CHANNEL_ANALOG:
593 if (ch->enabled == state->analog_states[ch->index].state)
596 if (dlm_analog_chan_state_set(scpi, ch->index + 1,
597 ch->enabled) != SR_OK)
600 state->analog_states[ch->index].state = ch->enabled;
601 setup_changed = TRUE;
603 case SR_CHANNEL_LOGIC:
605 pod_enabled[ch->index / 8] = TRUE;
607 if (ch->enabled == state->digital_states[ch->index])
610 if (dlm_digital_chan_state_set(scpi, ch->index + 1,
611 ch->enabled) != SR_OK)
614 state->digital_states[ch->index] = ch->enabled;
615 setup_changed = TRUE;
622 for (i = 1; i <= model->pods; ++i) {
623 if (state->pod_states[i - 1] == pod_enabled[i - 1])
626 if (dlm_digital_pod_state_set(scpi, i,
627 pod_enabled[i - 1]) != SR_OK)
630 state->pod_states[i - 1] = pod_enabled[i - 1];
631 setup_changed = TRUE;
636 if (setup_changed && dlm_sample_rate_query(sdi) != SR_OK)
642 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
645 gboolean digital_added;
646 struct sr_channel *ch;
647 struct dev_context *devc;
648 struct sr_scpi_dev_inst *scpi;
652 if (sdi->status != SR_ST_ACTIVE)
653 return SR_ERR_DEV_CLOSED;
657 digital_added = FALSE;
659 g_slist_free(devc->enabled_channels);
660 devc->enabled_channels = NULL;
662 for (l = sdi->channels; l; l = l->next) {
666 /* Only add a single digital channel. */
667 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
668 devc->enabled_channels = g_slist_append(
669 devc->enabled_channels, ch);
670 if (ch->type == SR_CHANNEL_LOGIC)
671 digital_added = TRUE;
675 if (!devc->enabled_channels)
678 if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
679 sr_err("Invalid channel configuration specified!");
683 if (dlm_setup_channels(sdi) != SR_OK) {
684 sr_err("Failed to setup channel configuration!");
688 /* Request data for the first enabled channel. */
689 devc->current_channel = devc->enabled_channels;
690 dlm_channel_data_request(sdi);
692 /* Call our callback when data comes in or after 5ms. */
693 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5,
694 dlm_data_receive, (void *)sdi);
699 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
701 struct dev_context *devc;
702 struct sr_datafeed_packet packet;
706 packet.type = SR_DF_END;
707 packet.payload = NULL;
708 sr_session_send(sdi, &packet);
710 if (sdi->status != SR_ST_ACTIVE)
711 return SR_ERR_DEV_CLOSED;
715 devc->num_frames = 0;
716 g_slist_free(devc->enabled_channels);
717 devc->enabled_channels = NULL;
719 sr_scpi_source_remove(sdi->session, sdi->conn);
724 SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info = {
725 .name = "yokogawa-dlm",
726 .longname = "Yokogawa DL/DLM",
731 .dev_list = dev_list,
732 .dev_clear = dev_clear,
733 .config_get = config_get,
734 .config_set = config_set,
735 .config_list = config_list,
736 .dev_open = dev_open,
737 .dev_close = dev_close,
738 .dev_acquisition_start = dev_acquisition_start,
739 .dev_acquisition_stop = dev_acquisition_stop,