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";
37 static int init(struct sr_context *sr_ctx)
39 return std_init(sr_ctx, di, LOG_PREFIX);
42 static struct sr_dev_inst *probe_usbtmc_device(struct sr_scpi_dev_inst *scpi)
44 struct sr_dev_inst *sdi;
45 struct dev_context *devc;
46 struct sr_scpi_hw_info *hw_info;
54 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
55 sr_info("Couldn't get IDN response.");
59 if (strcmp(hw_info->manufacturer, MANUFACTURER_ID) != 0)
62 if (dlm_model_get(hw_info->model, &model_name, &model_index) != SR_OK)
65 sdi = sr_dev_inst_new();
66 sdi->status = SR_ST_ACTIVE;
67 sdi->vendor = g_strdup(MANUFACTURER_NAME);
68 sdi->model = g_strdup(model_name);
69 sdi->version = g_strdup(hw_info->firmware_version);
71 sdi->serial_num = g_strdup(hw_info->serial_number);
73 sr_scpi_hw_info_free(hw_info);
76 devc = g_malloc0(sizeof(struct dev_context));
80 sdi->inst_type = SR_INST_SCPI;
83 if (dlm_device_init(sdi, model_index) != SR_OK)
86 sr_scpi_close(sdi->conn);
88 sdi->status = SR_ST_INACTIVE;
93 sr_scpi_hw_info_free(hw_info);
95 sr_dev_inst_free(sdi);
102 static GSList *scan(GSList *options)
104 return sr_scpi_scan(di->priv, options, probe_usbtmc_device);
107 static GSList *dev_list(void)
109 return ((struct drv_context *)(di->priv))->instances;
112 static void clear_helper(void *priv)
114 struct dev_context *devc;
118 dlm_scope_state_destroy(devc->model_state);
120 g_free(devc->analog_groups);
121 g_free(devc->digital_groups);
125 static int dev_clear(void)
127 return std_dev_clear(di, clear_helper);
130 static int dev_open(struct sr_dev_inst *sdi)
132 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
135 if (dlm_scope_state_query(sdi) != SR_OK)
138 sdi->status = SR_ST_ACTIVE;
143 static int dev_close(struct sr_dev_inst *sdi)
145 if (sdi->status == SR_ST_INACTIVE)
148 sr_scpi_close(sdi->conn);
150 sdi->status = SR_ST_INACTIVE;
155 static int cleanup(void)
163 * Check which category a given channel group belongs to.
165 * @param devc Our internal device context.
166 * @param cg The channel group to check.
168 * @retval CG_NONE cg is NULL
169 * @retval CG_ANALOG cg is an analog group
170 * @retval CG_DIGITAL cg is a digital group
171 * @retval CG_INVALID cg is something else
173 static int check_channel_group(struct dev_context *devc,
174 const struct sr_channel_group *cg)
177 struct scope_config *model;
179 model = devc->model_config;
184 for (i = 0; i < model->analog_channels; ++i)
185 if (cg == devc->analog_groups[i])
188 for (i = 0; i < model->pods; ++i)
189 if (cg == devc->digital_groups[i])
192 sr_err("Invalid channel group specified.");
196 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
197 const struct sr_channel_group *cg)
201 struct dev_context *devc;
202 struct scope_config *model;
203 struct scope_state *state;
205 if (!sdi || !(devc = sdi->priv))
208 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
212 model = devc->model_config;
213 state = devc->model_state;
216 case SR_CONF_NUM_TIMEBASE:
217 *data = g_variant_new_int32(model->num_xdivs);
220 case SR_CONF_TIMEBASE:
221 *data = g_variant_new("(tt)",
222 (*model->timebases)[state->timebase][0],
223 (*model->timebases)[state->timebase][1]);
226 case SR_CONF_NUM_VDIV:
227 if (cg_type == CG_NONE) {
228 sr_err("No channel group specified.");
229 return SR_ERR_CHANNEL_GROUP;
230 } else if (cg_type == CG_ANALOG) {
231 *data = g_variant_new_int32(model->num_ydivs);
240 if (cg_type == CG_NONE) {
241 sr_err("No channel group specified.");
242 return SR_ERR_CHANNEL_GROUP;
243 } else if (cg_type != CG_ANALOG)
246 for (i = 0; i < model->analog_channels; ++i) {
247 if (cg != devc->analog_groups[i])
249 *data = g_variant_new("(tt)",
250 (*model->vdivs)[state->analog_states[i].vdiv][0],
251 (*model->vdivs)[state->analog_states[i].vdiv][1]);
256 case SR_CONF_TRIGGER_SOURCE:
257 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
260 case SR_CONF_TRIGGER_SLOPE:
261 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
264 case SR_CONF_HORIZ_TRIGGERPOS:
265 *data = g_variant_new_double(state->horiz_triggerpos);
268 case SR_CONF_COUPLING:
270 if (cg_type == CG_NONE) {
271 sr_err("No channel group specified.");
272 return SR_ERR_CHANNEL_GROUP;
273 } else if (cg_type != CG_ANALOG)
276 for (i = 0; i < model->analog_channels; ++i) {
277 if (cg != devc->analog_groups[i])
279 *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
284 case SR_CONF_SAMPLERATE:
285 *data = g_variant_new_uint64(state->sample_rate);
295 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
298 GVariant *rational[2];
301 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
303 for (i = 0; i < n; i++) {
304 rational[0] = g_variant_new_uint64((*array)[i][0]);
305 rational[1] = g_variant_new_uint64((*array)[i][1]);
307 /* FIXME: Valgrind reports a memory leak here. */
308 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
311 return g_variant_builder_end(&gvb);
314 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
315 const struct sr_channel_group *cg)
320 struct dev_context *devc;
321 struct scope_config *model;
322 struct scope_state *state;
326 gboolean update_sample_rate;
328 if (!sdi || !(devc = sdi->priv))
331 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
334 model = devc->model_config;
335 state = devc->model_state;
336 update_sample_rate = FALSE;
341 case SR_CONF_LIMIT_FRAMES:
342 devc->frame_limit = g_variant_get_uint64(data);
345 case SR_CONF_TRIGGER_SOURCE:
346 tmp = g_variant_get_string(data, NULL);
347 for (i = 0; (*model->trigger_sources)[i]; i++) {
348 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
350 state->trigger_source = i;
351 /* TODO: A and B trigger support possible? */
352 ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
357 if (cg_type == CG_NONE) {
358 sr_err("No channel group specified.");
359 return SR_ERR_CHANNEL_GROUP;
362 g_variant_get(data, "(tt)", &p, &q);
364 for (i = 0; i < model->num_vdivs; i++) {
365 if (p != (*model->vdivs)[i][0] ||
366 q != (*model->vdivs)[i][1])
368 for (j = 1; j <= model->analog_channels; ++j) {
369 if (cg != devc->analog_groups[j - 1])
371 state->analog_states[j - 1].vdiv = i;
372 g_ascii_formatd(float_str, sizeof(float_str),
373 "%E", (float) p / q);
374 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
375 sr_scpi_get_opc(sdi->conn) != SR_OK)
385 case SR_CONF_TIMEBASE:
386 g_variant_get(data, "(tt)", &p, &q);
388 for (i = 0; i < model->num_timebases; i++) {
389 if (p != (*model->timebases)[i][0] ||
390 q != (*model->timebases)[i][1])
393 g_ascii_formatd(float_str, sizeof(float_str),
394 "%E", (float) p / q);
395 ret = dlm_timebase_set(sdi->conn, float_str);
396 update_sample_rate = TRUE;
400 case SR_CONF_HORIZ_TRIGGERPOS:
401 tmp_d = g_variant_get_double(data);
403 /* TODO: Check if the calculation makes sense for the DLM. */
404 if (tmp_d < 0.0 || tmp_d > 1.0)
407 state->horiz_triggerpos = tmp_d;
408 tmp_d = -(tmp_d - 0.5) *
409 ((double) (*model->timebases)[state->timebase][0] /
410 (*model->timebases)[state->timebase][1])
413 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
414 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
416 case SR_CONF_TRIGGER_SLOPE:
417 tmp = g_variant_get_string(data, NULL);
419 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
422 /* Note: See dlm_trigger_slopes[] in protocol.c. */
423 state->trigger_slope = (tmp[0] == 'r') ?
424 SLOPE_POSITIVE : SLOPE_NEGATIVE;
426 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
428 case SR_CONF_COUPLING:
429 if (cg_type == CG_NONE) {
430 sr_err("No channel group specified.");
431 return SR_ERR_CHANNEL_GROUP;
434 tmp = g_variant_get_string(data, NULL);
436 for (i = 0; (*model->coupling_options)[i]; i++) {
437 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
439 for (j = 1; j <= model->analog_channels; ++j) {
440 if (cg != devc->analog_groups[j - 1])
442 state->analog_states[j-1].coupling = i;
444 if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
445 sr_scpi_get_opc(sdi->conn) != SR_OK)
460 ret = sr_scpi_get_opc(sdi->conn);
462 if (ret == SR_OK && update_sample_rate)
463 ret = dlm_sample_rate_query(sdi);
468 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
469 const struct sr_channel_group *cg)
472 struct dev_context *devc;
473 struct scope_config *model;
475 if (!sdi || !(devc = sdi->priv))
478 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
481 model = devc->model_config;
484 case SR_CONF_DEVICE_OPTIONS:
485 if (cg_type == CG_NONE) {
486 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
487 model->devopts, model->num_devopts, sizeof(uint32_t));
488 } else if (cg_type == CG_ANALOG) {
489 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
490 model->analog_devopts, model->num_analog_devopts, sizeof(uint32_t));
492 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
493 NULL, 0, sizeof(uint32_t));
496 case SR_CONF_COUPLING:
497 if (cg_type == CG_NONE)
498 return SR_ERR_CHANNEL_GROUP;
499 *data = g_variant_new_strv(*model->coupling_options,
500 g_strv_length((char **)*model->coupling_options));
502 case SR_CONF_TRIGGER_SOURCE:
503 *data = g_variant_new_strv(*model->trigger_sources,
504 g_strv_length((char **)*model->trigger_sources));
506 case SR_CONF_TRIGGER_SLOPE:
507 *data = g_variant_new_strv(*model->trigger_slopes,
508 g_strv_length((char **)*model->trigger_slopes));
510 case SR_CONF_TIMEBASE:
511 *data = build_tuples(model->timebases, model->num_timebases);
514 if (cg_type == CG_NONE)
515 return SR_ERR_CHANNEL_GROUP;
516 *data = build_tuples(model->vdivs, model->num_vdivs);
525 static int dlm_check_channels(GSList *channels)
528 struct sr_channel *ch;
529 gboolean enabled_pod1, enabled_chan4;
531 enabled_pod1 = enabled_chan4 = FALSE;
533 /* Note: On the DLM2000, CH4 and Logic are shared. */
534 /* TODO Handle non-DLM2000 models. */
535 for (l = channels; l; l = l->next) {
538 case SR_CHANNEL_ANALOG:
540 enabled_chan4 = TRUE;
542 case SR_CHANNEL_LOGIC:
550 if (enabled_pod1 && enabled_chan4)
556 static int dlm_setup_channels(const struct sr_dev_inst *sdi)
560 gboolean *pod_enabled, setup_changed;
561 struct scope_state *state;
562 struct scope_config *model;
563 struct sr_channel *ch;
564 struct dev_context *devc;
565 struct sr_scpi_dev_inst *scpi;
569 state = devc->model_state;
570 model = devc->model_config;
571 setup_changed = FALSE;
573 pod_enabled = g_malloc0(sizeof(gboolean) * model->pods);
575 for (l = sdi->channels; l; l = l->next) {
578 case SR_CHANNEL_ANALOG:
579 if (ch->enabled == state->analog_states[ch->index].state)
582 if (dlm_analog_chan_state_set(scpi, ch->index + 1,
583 ch->enabled) != SR_OK)
586 state->analog_states[ch->index].state = ch->enabled;
587 setup_changed = TRUE;
589 case SR_CHANNEL_LOGIC:
591 pod_enabled[ch->index / 8] = TRUE;
593 if (ch->enabled == state->digital_states[ch->index])
596 if (dlm_digital_chan_state_set(scpi, ch->index + 1,
597 ch->enabled) != SR_OK)
600 state->digital_states[ch->index] = ch->enabled;
601 setup_changed = TRUE;
608 for (i = 1; i <= model->pods; ++i) {
609 if (state->pod_states[i - 1] == pod_enabled[i - 1])
612 if (dlm_digital_pod_state_set(scpi, i,
613 pod_enabled[i - 1]) != SR_OK)
616 state->pod_states[i - 1] = pod_enabled[i - 1];
617 setup_changed = TRUE;
622 if (setup_changed && dlm_sample_rate_query(sdi) != SR_OK)
628 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
631 gboolean digital_added;
632 struct sr_channel *ch;
633 struct dev_context *devc;
634 struct sr_scpi_dev_inst *scpi;
638 if (sdi->status != SR_ST_ACTIVE)
639 return SR_ERR_DEV_CLOSED;
643 digital_added = FALSE;
645 g_slist_free(devc->enabled_channels);
646 devc->enabled_channels = NULL;
648 for (l = sdi->channels; l; l = l->next) {
652 /* Only add a single digital channel. */
653 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
654 devc->enabled_channels = g_slist_append(
655 devc->enabled_channels, ch);
656 if (ch->type == SR_CHANNEL_LOGIC)
657 digital_added = TRUE;
661 if (!devc->enabled_channels)
664 if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
665 sr_err("Invalid channel configuration specified!");
669 if (dlm_setup_channels(sdi) != SR_OK) {
670 sr_err("Failed to setup channel configuration!");
674 /* Request data for the first enabled channel. */
675 devc->current_channel = devc->enabled_channels;
676 dlm_channel_data_request(sdi);
678 /* Call our callback when data comes in or after 5ms. */
679 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5,
680 dlm_data_receive, (void *)sdi);
685 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
687 struct dev_context *devc;
688 struct sr_datafeed_packet packet;
692 packet.type = SR_DF_END;
693 packet.payload = NULL;
694 sr_session_send(sdi, &packet);
696 if (sdi->status != SR_ST_ACTIVE)
697 return SR_ERR_DEV_CLOSED;
701 devc->num_frames = 0;
702 g_slist_free(devc->enabled_channels);
703 devc->enabled_channels = NULL;
705 sr_scpi_source_remove(sdi->session, sdi->conn);
710 SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info = {
711 .name = "yokogawa-dlm",
712 .longname = "Yokogawa DL/DLM",
717 .dev_list = dev_list,
718 .dev_clear = dev_clear,
719 .config_get = config_get,
720 .config_set = config_set,
721 .config_list = config_list,
722 .dev_open = dev_open,
723 .dev_close = dev_close,
724 .dev_acquisition_start = dev_acquisition_start,
725 .dev_acquisition_stop = dev_acquisition_stop,