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(di, sr_ctx, 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 void clear_helper(void *priv)
133 struct dev_context *devc;
137 dlm_scope_state_destroy(devc->model_state);
139 g_free(devc->analog_groups);
140 g_free(devc->digital_groups);
144 static int dev_clear(const struct sr_dev_driver *di)
146 return std_dev_clear(di, clear_helper);
149 static int dev_open(struct sr_dev_inst *sdi)
151 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
154 if (dlm_scope_state_query(sdi) != SR_OK)
157 sdi->status = SR_ST_ACTIVE;
162 static int dev_close(struct sr_dev_inst *sdi)
164 if (sdi->status == SR_ST_INACTIVE)
167 sr_scpi_close(sdi->conn);
169 sdi->status = SR_ST_INACTIVE;
175 * Check which category a given channel group belongs to.
177 * @param devc Our internal device context.
178 * @param cg The channel group to check.
180 * @retval CG_NONE cg is NULL
181 * @retval CG_ANALOG cg is an analog group
182 * @retval CG_DIGITAL cg is a digital group
183 * @retval CG_INVALID cg is something else
185 static int check_channel_group(struct dev_context *devc,
186 const struct sr_channel_group *cg)
189 const struct scope_config *model;
191 model = devc->model_config;
196 for (i = 0; i < model->analog_channels; i++)
197 if (cg == devc->analog_groups[i])
200 for (i = 0; i < model->pods; i++)
201 if (cg == devc->digital_groups[i])
204 sr_err("Invalid channel group specified.");
208 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
209 const struct sr_channel_group *cg)
213 struct dev_context *devc;
214 const struct scope_config *model;
215 struct scope_state *state;
217 if (!sdi || !(devc = sdi->priv))
220 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
223 model = devc->model_config;
224 state = devc->model_state;
227 case SR_CONF_NUM_HDIV:
228 *data = g_variant_new_int32(model->num_xdivs);
231 case SR_CONF_TIMEBASE:
232 *data = g_variant_new("(tt)",
233 dlm_timebases[state->timebase][0],
234 dlm_timebases[state->timebase][1]);
237 case SR_CONF_NUM_VDIV:
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) {
242 *data = g_variant_new_int32(model->num_ydivs);
251 if (cg_type == CG_NONE) {
252 sr_err("No channel group specified.");
253 return SR_ERR_CHANNEL_GROUP;
254 } else if (cg_type != CG_ANALOG)
257 for (i = 0; i < model->analog_channels; i++) {
258 if (cg != devc->analog_groups[i])
260 *data = g_variant_new("(tt)",
261 dlm_vdivs[state->analog_states[i].vdiv][0],
262 dlm_vdivs[state->analog_states[i].vdiv][1]);
267 case SR_CONF_TRIGGER_SOURCE:
268 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
271 case SR_CONF_TRIGGER_SLOPE:
272 *data = g_variant_new_string(dlm_trigger_slopes[state->trigger_slope]);
275 case SR_CONF_HORIZ_TRIGGERPOS:
276 *data = g_variant_new_double(state->horiz_triggerpos);
279 case SR_CONF_COUPLING:
281 if (cg_type == CG_NONE) {
282 sr_err("No channel group specified.");
283 return SR_ERR_CHANNEL_GROUP;
284 } else if (cg_type != CG_ANALOG)
287 for (i = 0; i < model->analog_channels; i++) {
288 if (cg != devc->analog_groups[i])
290 *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
295 case SR_CONF_SAMPLERATE:
296 *data = g_variant_new_uint64(state->sample_rate);
306 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
309 GVariant *rational[2];
312 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
314 for (i = 0; i < n; i++) {
315 rational[0] = g_variant_new_uint64((*array)[i][0]);
316 rational[1] = g_variant_new_uint64((*array)[i][1]);
318 /* FIXME: Valgrind reports a memory leak here. */
319 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
322 return g_variant_builder_end(&gvb);
325 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
326 const struct sr_channel_group *cg)
331 struct dev_context *devc;
332 const struct scope_config *model;
333 struct scope_state *state;
337 gboolean update_sample_rate;
339 if (!sdi || !(devc = sdi->priv))
342 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
345 model = devc->model_config;
346 state = devc->model_state;
347 update_sample_rate = FALSE;
352 case SR_CONF_LIMIT_FRAMES:
353 devc->frame_limit = g_variant_get_uint64(data);
356 case SR_CONF_TRIGGER_SOURCE:
357 tmp = g_variant_get_string(data, NULL);
358 for (i = 0; (*model->trigger_sources)[i]; i++) {
359 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
361 state->trigger_source = i;
362 /* TODO: A and B trigger support possible? */
363 ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
368 if (cg_type == CG_NONE) {
369 sr_err("No channel group specified.");
370 return SR_ERR_CHANNEL_GROUP;
373 g_variant_get(data, "(tt)", &p, &q);
375 for (i = 0; i < ARRAY_SIZE(dlm_vdivs); i++) {
376 if (p != dlm_vdivs[i][0] ||
377 q != dlm_vdivs[i][1])
379 for (j = 1; j <= model->analog_channels; j++) {
380 if (cg != devc->analog_groups[j - 1])
382 state->analog_states[j - 1].vdiv = i;
383 g_ascii_formatd(float_str, sizeof(float_str),
384 "%E", (float) p / q);
385 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
386 sr_scpi_get_opc(sdi->conn) != SR_OK)
396 case SR_CONF_TIMEBASE:
397 g_variant_get(data, "(tt)", &p, &q);
399 for (i = 0; i < ARRAY_SIZE(dlm_timebases); i++) {
400 if (p != dlm_timebases[i][0] ||
401 q != dlm_timebases[i][1])
404 g_ascii_formatd(float_str, sizeof(float_str),
405 "%E", (float) p / q);
406 ret = dlm_timebase_set(sdi->conn, float_str);
407 update_sample_rate = TRUE;
411 case SR_CONF_HORIZ_TRIGGERPOS:
412 tmp_d = g_variant_get_double(data);
414 /* TODO: Check if the calculation makes sense for the DLM. */
415 if (tmp_d < 0.0 || tmp_d > 1.0)
418 state->horiz_triggerpos = tmp_d;
419 tmp_d = -(tmp_d - 0.5) *
420 ((double) dlm_timebases[state->timebase][0] /
421 dlm_timebases[state->timebase][1])
424 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
425 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
427 case SR_CONF_TRIGGER_SLOPE:
428 tmp = g_variant_get_string(data, NULL);
430 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
433 /* Note: See dlm_trigger_slopes[] in protocol.c. */
434 state->trigger_slope = (tmp[0] == 'r') ?
435 SLOPE_POSITIVE : SLOPE_NEGATIVE;
437 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
439 case SR_CONF_COUPLING:
440 if (cg_type == CG_NONE) {
441 sr_err("No channel group specified.");
442 return SR_ERR_CHANNEL_GROUP;
445 tmp = g_variant_get_string(data, NULL);
447 for (i = 0; (*model->coupling_options)[i]; i++) {
448 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
450 for (j = 1; j <= model->analog_channels; j++) {
451 if (cg != devc->analog_groups[j - 1])
453 state->analog_states[j-1].coupling = i;
455 if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
456 sr_scpi_get_opc(sdi->conn) != SR_OK)
471 ret = sr_scpi_get_opc(sdi->conn);
473 if (ret == SR_OK && update_sample_rate)
474 ret = dlm_sample_rate_query(sdi);
479 static int config_channel_set(const struct sr_dev_inst *sdi,
480 struct sr_channel *ch, unsigned int changes)
482 /* Currently we only handle SR_CHANNEL_SET_ENABLED. */
483 if (changes != SR_CHANNEL_SET_ENABLED)
486 return dlm_channel_state_set(sdi, ch->index, ch->enabled);
489 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
490 const struct sr_channel_group *cg)
492 int cg_type = CG_NONE;
493 struct dev_context *devc = NULL;
494 const struct scope_config *model = NULL;
496 /* SR_CONF_SCAN_OPTIONS is always valid, regardless of sdi or probe group. */
497 if (key == SR_CONF_SCAN_OPTIONS) {
498 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
499 dlm_scanopts, ARRAY_SIZE(dlm_scanopts), sizeof(uint32_t));
503 /* If sdi is NULL, nothing except SR_CONF_DEVICE_OPTIONS can be provided. */
504 if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
505 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
506 dlm_drvopts, ARRAY_SIZE(dlm_drvopts), sizeof(uint32_t));
514 model = devc->model_config;
517 * If cg is NULL, only the SR_CONF_DEVICE_OPTIONS that are not
518 * specific to a probe group must be returned.
522 case SR_CONF_DEVICE_OPTIONS:
523 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
524 dlm_devopts, ARRAY_SIZE(dlm_devopts), sizeof(uint32_t));
526 case SR_CONF_TIMEBASE:
527 *data = build_tuples(&dlm_timebases, ARRAY_SIZE(dlm_timebases));
529 case SR_CONF_TRIGGER_SOURCE:
532 *data = g_variant_new_strv(*model->trigger_sources,
533 g_strv_length((char **)*model->trigger_sources));
535 case SR_CONF_TRIGGER_SLOPE:
536 *data = g_variant_new_strv(dlm_trigger_slopes,
537 g_strv_length((char **)dlm_trigger_slopes));
539 case SR_CONF_NUM_HDIV:
540 *data = g_variant_new_uint32(model->num_xdivs);
547 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
551 case SR_CONF_DEVICE_OPTIONS:
552 if (cg_type == CG_ANALOG) {
553 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
554 dlm_analog_devopts, ARRAY_SIZE(dlm_analog_devopts), sizeof(uint32_t));
555 } else if (cg_type == CG_DIGITAL) {
556 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
557 dlm_digital_devopts, ARRAY_SIZE(dlm_digital_devopts), sizeof(uint32_t));
559 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
560 NULL, 0, sizeof(uint32_t));
563 case SR_CONF_COUPLING:
564 if (cg_type == CG_NONE)
565 return SR_ERR_CHANNEL_GROUP;
566 *data = g_variant_new_strv(*model->coupling_options,
567 g_strv_length((char **)*model->coupling_options));
570 if (cg_type == CG_NONE)
571 return SR_ERR_CHANNEL_GROUP;
572 *data = build_tuples(&dlm_vdivs, ARRAY_SIZE(dlm_vdivs));
581 static int dlm_check_channels(GSList *channels)
584 struct sr_channel *ch;
585 gboolean enabled_pod1, enabled_chan4;
587 enabled_pod1 = enabled_chan4 = FALSE;
589 /* Note: On the DLM2000, CH4 and Logic are shared. */
590 /* TODO Handle non-DLM2000 models. */
591 for (l = channels; l; l = l->next) {
594 case SR_CHANNEL_ANALOG:
596 enabled_chan4 = TRUE;
598 case SR_CHANNEL_LOGIC:
606 if (enabled_pod1 && enabled_chan4)
612 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
615 gboolean digital_added;
616 struct sr_channel *ch;
617 struct dev_context *devc;
618 struct sr_scpi_dev_inst *scpi;
620 if (sdi->status != SR_ST_ACTIVE)
621 return SR_ERR_DEV_CLOSED;
625 digital_added = FALSE;
627 g_slist_free(devc->enabled_channels);
628 devc->enabled_channels = NULL;
630 for (l = sdi->channels; l; l = l->next) {
634 /* Only add a single digital channel. */
635 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
636 devc->enabled_channels = g_slist_append(
637 devc->enabled_channels, ch);
638 if (ch->type == SR_CHANNEL_LOGIC)
639 digital_added = TRUE;
643 if (!devc->enabled_channels)
646 if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
647 sr_err("Invalid channel configuration specified!");
651 /* Request data for the first enabled channel. */
652 devc->current_channel = devc->enabled_channels;
653 dlm_channel_data_request(sdi);
655 /* Call our callback when data comes in or after 5ms. */
656 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5,
657 dlm_data_receive, (void *)sdi);
662 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
664 struct dev_context *devc;
666 std_session_send_df_end(sdi, LOG_PREFIX);
668 if (sdi->status != SR_ST_ACTIVE)
669 return SR_ERR_DEV_CLOSED;
673 devc->num_frames = 0;
674 g_slist_free(devc->enabled_channels);
675 devc->enabled_channels = NULL;
677 sr_scpi_source_remove(sdi->session, sdi->conn);
682 SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info = {
683 .name = "yokogawa-dlm",
684 .longname = "Yokogawa DL/DLM",
687 .cleanup = std_cleanup,
689 .dev_list = std_dev_list,
690 .dev_clear = dev_clear,
691 .config_get = config_get,
692 .config_set = config_set,
693 .config_channel_set = config_channel_set,
694 .config_list = config_list,
695 .dev_open = dev_open,
696 .dev_close = dev_close,
697 .dev_acquisition_start = dev_acquisition_start,
698 .dev_acquisition_stop = dev_acquisition_stop,