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";
30 static const uint32_t scanopts[] = {
34 static const uint32_t drvopts[] = {
35 SR_CONF_LOGIC_ANALYZER,
39 static const uint32_t devopts[] = {
40 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
41 SR_CONF_SAMPLERATE | SR_CONF_GET,
42 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43 SR_CONF_NUM_HDIV | SR_CONF_GET,
44 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
45 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
49 static const uint32_t devopts_cg_analog[] = {
50 SR_CONF_NUM_VDIV | SR_CONF_GET,
51 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
52 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
55 static const uint32_t devopts_cg_digital[] = {
65 static struct sr_dev_inst *probe_usbtmc_device(struct sr_scpi_dev_inst *scpi)
67 struct sr_dev_inst *sdi;
68 struct dev_context *devc;
69 struct sr_scpi_hw_info *hw_info;
77 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
78 sr_info("Couldn't get IDN response.");
82 if (strcmp(hw_info->manufacturer, MANUFACTURER_ID) != 0)
85 if (dlm_model_get(hw_info->model, &model_name, &model_index) != SR_OK)
88 sdi = g_malloc0(sizeof(struct sr_dev_inst));
89 sdi->vendor = g_strdup("Yokogawa");
90 sdi->model = g_strdup(model_name);
91 sdi->version = g_strdup(hw_info->firmware_version);
93 sdi->serial_num = g_strdup(hw_info->serial_number);
95 sr_scpi_hw_info_free(hw_info);
98 devc = g_malloc0(sizeof(struct dev_context));
100 sdi->driver = &yokogawa_dlm_driver_info;
102 sdi->inst_type = SR_INST_SCPI;
105 if (dlm_device_init(sdi, model_index) != SR_OK)
111 sr_scpi_hw_info_free(hw_info);
112 sr_dev_inst_free(sdi);
118 static GSList *scan(struct sr_dev_driver *di, GSList *options)
120 return sr_scpi_scan(di->context, options, probe_usbtmc_device);
123 static void clear_helper(struct dev_context *devc)
125 dlm_scope_state_destroy(devc->model_state);
126 g_free(devc->analog_groups);
127 g_free(devc->digital_groups);
130 static int dev_clear(const struct sr_dev_driver *di)
132 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
135 static int dev_open(struct sr_dev_inst *sdi)
137 if (sr_scpi_open(sdi->conn) != SR_OK)
140 if (dlm_scope_state_query(sdi) != SR_OK)
146 static int dev_close(struct sr_dev_inst *sdi)
148 return sr_scpi_close(sdi->conn);
152 * Check which category a given channel group belongs to.
154 * @param devc Our internal device context.
155 * @param cg The channel group to check.
157 * @retval CG_NONE cg is NULL
158 * @retval CG_ANALOG cg is an analog group
159 * @retval CG_DIGITAL cg is a digital group
160 * @retval CG_INVALID cg is something else
162 static int check_channel_group(struct dev_context *devc,
163 const struct sr_channel_group *cg)
166 const struct scope_config *model;
168 model = devc->model_config;
173 for (i = 0; i < model->analog_channels; i++)
174 if (cg == devc->analog_groups[i])
177 for (i = 0; i < model->pods; i++)
178 if (cg == devc->digital_groups[i])
181 sr_err("Invalid channel group specified.");
185 static int config_get(uint32_t key, GVariant **data,
186 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
190 struct dev_context *devc;
191 const struct scope_config *model;
192 struct scope_state *state;
199 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
202 model = devc->model_config;
203 state = devc->model_state;
206 case SR_CONF_NUM_HDIV:
207 *data = g_variant_new_int32(model->num_xdivs);
210 case SR_CONF_TIMEBASE:
211 *data = g_variant_new("(tt)",
212 dlm_timebases[state->timebase][0],
213 dlm_timebases[state->timebase][1]);
216 case SR_CONF_NUM_VDIV:
217 if (cg_type == CG_NONE) {
218 return SR_ERR_CHANNEL_GROUP;
219 } else if (cg_type == CG_ANALOG) {
220 *data = g_variant_new_int32(model->num_ydivs);
229 if (cg_type == CG_NONE) {
230 return SR_ERR_CHANNEL_GROUP;
231 } else if (cg_type != CG_ANALOG)
234 for (i = 0; i < model->analog_channels; i++) {
235 if (cg != devc->analog_groups[i])
237 *data = g_variant_new("(tt)",
238 dlm_vdivs[state->analog_states[i].vdiv][0],
239 dlm_vdivs[state->analog_states[i].vdiv][1]);
244 case SR_CONF_TRIGGER_SOURCE:
245 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
248 case SR_CONF_TRIGGER_SLOPE:
249 *data = g_variant_new_string(dlm_trigger_slopes[state->trigger_slope]);
252 case SR_CONF_HORIZ_TRIGGERPOS:
253 *data = g_variant_new_double(state->horiz_triggerpos);
256 case SR_CONF_COUPLING:
258 if (cg_type == CG_NONE) {
259 return SR_ERR_CHANNEL_GROUP;
260 } else if (cg_type != CG_ANALOG)
263 for (i = 0; i < model->analog_channels; i++) {
264 if (cg != devc->analog_groups[i])
266 *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
271 case SR_CONF_SAMPLERATE:
272 *data = g_variant_new_uint64(state->sample_rate);
282 static int config_set(uint32_t key, GVariant *data,
283 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
288 struct dev_context *devc;
289 const struct scope_config *model;
290 struct scope_state *state;
294 gboolean update_sample_rate;
296 if (!sdi || !(devc = sdi->priv))
299 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
302 model = devc->model_config;
303 state = devc->model_state;
304 update_sample_rate = FALSE;
309 case SR_CONF_LIMIT_FRAMES:
310 devc->frame_limit = g_variant_get_uint64(data);
313 case SR_CONF_TRIGGER_SOURCE:
314 tmp = g_variant_get_string(data, NULL);
315 for (i = 0; (*model->trigger_sources)[i]; i++) {
316 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
318 state->trigger_source = i;
319 /* TODO: A and B trigger support possible? */
320 ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
325 if (cg_type == CG_NONE)
326 return SR_ERR_CHANNEL_GROUP;
328 g_variant_get(data, "(tt)", &p, &q);
330 for (i = 0; i < ARRAY_SIZE(dlm_vdivs); i++) {
331 if (p != dlm_vdivs[i][0] ||
332 q != dlm_vdivs[i][1])
334 for (j = 1; j <= model->analog_channels; j++) {
335 if (cg != devc->analog_groups[j - 1])
337 state->analog_states[j - 1].vdiv = i;
338 g_ascii_formatd(float_str, sizeof(float_str),
339 "%E", (float) p / q);
340 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
341 sr_scpi_get_opc(sdi->conn) != SR_OK)
351 case SR_CONF_TIMEBASE:
352 g_variant_get(data, "(tt)", &p, &q);
354 for (i = 0; i < ARRAY_SIZE(dlm_timebases); i++) {
355 if (p != dlm_timebases[i][0] ||
356 q != dlm_timebases[i][1])
359 g_ascii_formatd(float_str, sizeof(float_str),
360 "%E", (float) p / q);
361 ret = dlm_timebase_set(sdi->conn, float_str);
362 update_sample_rate = TRUE;
366 case SR_CONF_HORIZ_TRIGGERPOS:
367 tmp_d = g_variant_get_double(data);
369 /* TODO: Check if the calculation makes sense for the DLM. */
370 if (tmp_d < 0.0 || tmp_d > 1.0)
373 state->horiz_triggerpos = tmp_d;
374 tmp_d = -(tmp_d - 0.5) *
375 ((double) dlm_timebases[state->timebase][0] /
376 dlm_timebases[state->timebase][1])
379 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
380 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
382 case SR_CONF_TRIGGER_SLOPE:
383 tmp = g_variant_get_string(data, NULL);
385 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
388 /* Note: See dlm_trigger_slopes[] in protocol.c. */
389 state->trigger_slope = (tmp[0] == 'r') ?
390 SLOPE_POSITIVE : SLOPE_NEGATIVE;
392 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
394 case SR_CONF_COUPLING:
395 if (cg_type == CG_NONE)
396 return SR_ERR_CHANNEL_GROUP;
398 tmp = g_variant_get_string(data, NULL);
400 for (i = 0; (*model->coupling_options)[i]; i++) {
401 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
403 for (j = 1; j <= model->analog_channels; j++) {
404 if (cg != devc->analog_groups[j - 1])
406 state->analog_states[j-1].coupling = i;
408 if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
409 sr_scpi_get_opc(sdi->conn) != SR_OK)
424 ret = sr_scpi_get_opc(sdi->conn);
426 if (ret == SR_OK && update_sample_rate)
427 ret = dlm_sample_rate_query(sdi);
432 static int config_channel_set(const struct sr_dev_inst *sdi,
433 struct sr_channel *ch, unsigned int changes)
435 /* Currently we only handle SR_CHANNEL_SET_ENABLED. */
436 if (changes != SR_CHANNEL_SET_ENABLED)
439 return dlm_channel_state_set(sdi, ch->index, ch->enabled);
442 static int config_list(uint32_t key, GVariant **data,
443 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
445 int cg_type = CG_NONE;
446 struct dev_context *devc;
447 const struct scope_config *model;
449 devc = (sdi) ? sdi->priv : NULL;
450 model = (devc) ? devc->model_config : NULL;
454 case SR_CONF_SCAN_OPTIONS:
455 case SR_CONF_DEVICE_OPTIONS:
456 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
457 case SR_CONF_TIMEBASE:
458 *data = std_gvar_tuple_array(ARRAY_AND_SIZE(dlm_timebases));
460 case SR_CONF_TRIGGER_SOURCE:
463 *data = g_variant_new_strv(*model->trigger_sources,
464 g_strv_length((char **)*model->trigger_sources));
466 case SR_CONF_TRIGGER_SLOPE:
467 *data = g_variant_new_strv(dlm_trigger_slopes,
468 g_strv_length((char **)dlm_trigger_slopes));
470 case SR_CONF_NUM_HDIV:
471 *data = g_variant_new_uint32(model->num_xdivs);
478 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
482 case SR_CONF_DEVICE_OPTIONS:
483 if (cg_type == CG_ANALOG)
484 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog));
485 else if (cg_type == CG_DIGITAL)
486 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_digital));
488 *data = std_gvar_array_u32(NULL, 0);
490 case SR_CONF_COUPLING:
491 if (cg_type == CG_NONE)
492 return SR_ERR_CHANNEL_GROUP;
493 *data = g_variant_new_strv(*model->coupling_options,
494 g_strv_length((char **)*model->coupling_options));
497 if (cg_type == CG_NONE)
498 return SR_ERR_CHANNEL_GROUP;
499 *data = std_gvar_tuple_array(ARRAY_AND_SIZE(dlm_vdivs));
508 static int dlm_check_channels(GSList *channels)
511 struct sr_channel *ch;
512 gboolean enabled_pod1, enabled_chan4;
514 enabled_pod1 = enabled_chan4 = FALSE;
516 /* Note: On the DLM2000, CH4 and Logic are shared. */
517 /* TODO Handle non-DLM2000 models. */
518 for (l = channels; l; l = l->next) {
521 case SR_CHANNEL_ANALOG:
523 enabled_chan4 = TRUE;
525 case SR_CHANNEL_LOGIC:
533 if (enabled_pod1 && enabled_chan4)
539 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
542 gboolean digital_added;
543 struct sr_channel *ch;
544 struct dev_context *devc;
545 struct sr_scpi_dev_inst *scpi;
549 digital_added = FALSE;
551 g_slist_free(devc->enabled_channels);
552 devc->enabled_channels = NULL;
554 for (l = sdi->channels; l; l = l->next) {
558 /* Only add a single digital channel. */
559 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
560 devc->enabled_channels = g_slist_append(
561 devc->enabled_channels, ch);
562 if (ch->type == SR_CHANNEL_LOGIC)
563 digital_added = TRUE;
567 if (!devc->enabled_channels)
570 if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
571 sr_err("Invalid channel configuration specified!");
575 /* Request data for the first enabled channel. */
576 devc->current_channel = devc->enabled_channels;
577 dlm_channel_data_request(sdi);
579 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5,
580 dlm_data_receive, (void *)sdi);
585 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
587 struct dev_context *devc;
589 std_session_send_df_end(sdi);
593 devc->num_frames = 0;
594 g_slist_free(devc->enabled_channels);
595 devc->enabled_channels = NULL;
597 sr_scpi_source_remove(sdi->session, sdi->conn);
602 static struct sr_dev_driver yokogawa_dlm_driver_info = {
603 .name = "yokogawa-dlm",
604 .longname = "Yokogawa DL/DLM",
607 .cleanup = std_cleanup,
609 .dev_list = std_dev_list,
610 .dev_clear = dev_clear,
611 .config_get = config_get,
612 .config_set = config_set,
613 .config_channel_set = config_channel_set,
614 .config_list = config_list,
615 .dev_open = dev_open,
616 .dev_close = dev_close,
617 .dev_acquisition_start = dev_acquisition_start,
618 .dev_acquisition_stop = dev_acquisition_stop,
621 SR_REGISTER_DEV_DRIVER(yokogawa_dlm_driver_info);