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 scanopts[] = {
35 static const uint32_t drvopts[] = {
36 SR_CONF_LOGIC_ANALYZER,
40 static const uint32_t 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 devopts_cg_analog[] = {
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 devopts_cg_digital[] = {
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 int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
287 const struct sr_channel_group *cg)
292 struct dev_context *devc;
293 const struct scope_config *model;
294 struct scope_state *state;
298 gboolean update_sample_rate;
300 if (!sdi || !(devc = sdi->priv))
303 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
306 model = devc->model_config;
307 state = devc->model_state;
308 update_sample_rate = FALSE;
313 case SR_CONF_LIMIT_FRAMES:
314 devc->frame_limit = g_variant_get_uint64(data);
317 case SR_CONF_TRIGGER_SOURCE:
318 tmp = g_variant_get_string(data, NULL);
319 for (i = 0; (*model->trigger_sources)[i]; i++) {
320 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
322 state->trigger_source = i;
323 /* TODO: A and B trigger support possible? */
324 ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
329 if (cg_type == CG_NONE) {
330 sr_err("No channel group specified.");
331 return SR_ERR_CHANNEL_GROUP;
334 g_variant_get(data, "(tt)", &p, &q);
336 for (i = 0; i < ARRAY_SIZE(dlm_vdivs); i++) {
337 if (p != dlm_vdivs[i][0] ||
338 q != dlm_vdivs[i][1])
340 for (j = 1; j <= model->analog_channels; j++) {
341 if (cg != devc->analog_groups[j - 1])
343 state->analog_states[j - 1].vdiv = i;
344 g_ascii_formatd(float_str, sizeof(float_str),
345 "%E", (float) p / q);
346 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
347 sr_scpi_get_opc(sdi->conn) != SR_OK)
357 case SR_CONF_TIMEBASE:
358 g_variant_get(data, "(tt)", &p, &q);
360 for (i = 0; i < ARRAY_SIZE(dlm_timebases); i++) {
361 if (p != dlm_timebases[i][0] ||
362 q != dlm_timebases[i][1])
365 g_ascii_formatd(float_str, sizeof(float_str),
366 "%E", (float) p / q);
367 ret = dlm_timebase_set(sdi->conn, float_str);
368 update_sample_rate = TRUE;
372 case SR_CONF_HORIZ_TRIGGERPOS:
373 tmp_d = g_variant_get_double(data);
375 /* TODO: Check if the calculation makes sense for the DLM. */
376 if (tmp_d < 0.0 || tmp_d > 1.0)
379 state->horiz_triggerpos = tmp_d;
380 tmp_d = -(tmp_d - 0.5) *
381 ((double) dlm_timebases[state->timebase][0] /
382 dlm_timebases[state->timebase][1])
385 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
386 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
388 case SR_CONF_TRIGGER_SLOPE:
389 tmp = g_variant_get_string(data, NULL);
391 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
394 /* Note: See dlm_trigger_slopes[] in protocol.c. */
395 state->trigger_slope = (tmp[0] == 'r') ?
396 SLOPE_POSITIVE : SLOPE_NEGATIVE;
398 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
400 case SR_CONF_COUPLING:
401 if (cg_type == CG_NONE) {
402 sr_err("No channel group specified.");
403 return SR_ERR_CHANNEL_GROUP;
406 tmp = g_variant_get_string(data, NULL);
408 for (i = 0; (*model->coupling_options)[i]; i++) {
409 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
411 for (j = 1; j <= model->analog_channels; j++) {
412 if (cg != devc->analog_groups[j - 1])
414 state->analog_states[j-1].coupling = i;
416 if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
417 sr_scpi_get_opc(sdi->conn) != SR_OK)
432 ret = sr_scpi_get_opc(sdi->conn);
434 if (ret == SR_OK && update_sample_rate)
435 ret = dlm_sample_rate_query(sdi);
440 static int config_channel_set(const struct sr_dev_inst *sdi,
441 struct sr_channel *ch, unsigned int changes)
443 /* Currently we only handle SR_CHANNEL_SET_ENABLED. */
444 if (changes != SR_CHANNEL_SET_ENABLED)
447 return dlm_channel_state_set(sdi, ch->index, ch->enabled);
450 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
451 const struct sr_channel_group *cg)
453 int cg_type = CG_NONE;
454 struct dev_context *devc;
455 const struct scope_config *model;
457 devc = (sdi) ? sdi->priv : NULL;
458 model = (devc) ? devc->model_config : NULL;
462 case SR_CONF_SCAN_OPTIONS:
463 case SR_CONF_DEVICE_OPTIONS:
464 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
465 case SR_CONF_TIMEBASE:
466 *data = std_gvar_tuple_array(&dlm_timebases, ARRAY_SIZE(dlm_timebases));
468 case SR_CONF_TRIGGER_SOURCE:
471 *data = g_variant_new_strv(*model->trigger_sources,
472 g_strv_length((char **)*model->trigger_sources));
474 case SR_CONF_TRIGGER_SLOPE:
475 *data = g_variant_new_strv(dlm_trigger_slopes,
476 g_strv_length((char **)dlm_trigger_slopes));
478 case SR_CONF_NUM_HDIV:
479 *data = g_variant_new_uint32(model->num_xdivs);
486 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
490 case SR_CONF_DEVICE_OPTIONS:
491 if (cg_type == CG_ANALOG)
492 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog));
493 else if (cg_type == CG_DIGITAL)
494 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_digital));
496 *data = std_gvar_array_u32(NULL, 0);
498 case SR_CONF_COUPLING:
499 if (cg_type == CG_NONE)
500 return SR_ERR_CHANNEL_GROUP;
501 *data = g_variant_new_strv(*model->coupling_options,
502 g_strv_length((char **)*model->coupling_options));
505 if (cg_type == CG_NONE)
506 return SR_ERR_CHANNEL_GROUP;
507 *data = std_gvar_tuple_array(&dlm_vdivs, ARRAY_SIZE(dlm_vdivs));
516 static int dlm_check_channels(GSList *channels)
519 struct sr_channel *ch;
520 gboolean enabled_pod1, enabled_chan4;
522 enabled_pod1 = enabled_chan4 = FALSE;
524 /* Note: On the DLM2000, CH4 and Logic are shared. */
525 /* TODO Handle non-DLM2000 models. */
526 for (l = channels; l; l = l->next) {
529 case SR_CHANNEL_ANALOG:
531 enabled_chan4 = TRUE;
533 case SR_CHANNEL_LOGIC:
541 if (enabled_pod1 && enabled_chan4)
547 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
550 gboolean digital_added;
551 struct sr_channel *ch;
552 struct dev_context *devc;
553 struct sr_scpi_dev_inst *scpi;
557 digital_added = FALSE;
559 g_slist_free(devc->enabled_channels);
560 devc->enabled_channels = NULL;
562 for (l = sdi->channels; l; l = l->next) {
566 /* Only add a single digital channel. */
567 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
568 devc->enabled_channels = g_slist_append(
569 devc->enabled_channels, ch);
570 if (ch->type == SR_CHANNEL_LOGIC)
571 digital_added = TRUE;
575 if (!devc->enabled_channels)
578 if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
579 sr_err("Invalid channel configuration specified!");
583 /* Request data for the first enabled channel. */
584 devc->current_channel = devc->enabled_channels;
585 dlm_channel_data_request(sdi);
587 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5,
588 dlm_data_receive, (void *)sdi);
593 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
595 struct dev_context *devc;
597 std_session_send_df_end(sdi);
601 devc->num_frames = 0;
602 g_slist_free(devc->enabled_channels);
603 devc->enabled_channels = NULL;
605 sr_scpi_source_remove(sdi->session, sdi->conn);
610 static struct sr_dev_driver yokogawa_dlm_driver_info = {
611 .name = "yokogawa-dlm",
612 .longname = "Yokogawa DL/DLM",
615 .cleanup = std_cleanup,
617 .dev_list = std_dev_list,
618 .dev_clear = dev_clear,
619 .config_get = config_get,
620 .config_set = config_set,
621 .config_channel_set = config_channel_set,
622 .config_list = config_list,
623 .dev_open = dev_open,
624 .dev_close = dev_close,
625 .dev_acquisition_start = dev_acquisition_start,
626 .dev_acquisition_stop = dev_acquisition_stop,
629 SR_REGISTER_DEV_DRIVER(yokogawa_dlm_driver_info);