2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Martin Ling <martin-git@earth.li>
5 * Copyright (C) 2013 Bert Vermeulen <bert@biot.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 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
30 #define NUM_TIMEBASE 12
33 static const int32_t hwopts[] = {
37 static const int32_t hwcaps[] = {
40 SR_CONF_TRIGGER_SOURCE,
41 SR_CONF_TRIGGER_SLOPE,
42 SR_CONF_HORIZ_TRIGGERPOS,
46 static const int32_t analog_hwcaps[] = {
52 static const uint64_t timebases[][2] = {
90 static const uint64_t vdivs[][2] = {
107 static const char *trigger_sources[] = {
130 static const char *coupling[] = {
136 static const char *supported_models[] = {
145 SR_PRIV struct sr_dev_driver rigol_ds_driver_info;
146 static struct sr_dev_driver *di = &rigol_ds_driver_info;
148 static void clear_helper(void *priv)
150 struct dev_context *devc;
153 g_free(devc->coupling[0]);
154 g_free(devc->coupling[1]);
155 g_free(devc->trigger_source);
156 g_free(devc->trigger_slope);
157 g_slist_free(devc->analog_groups[0].probes);
158 g_slist_free(devc->analog_groups[1].probes);
159 g_slist_free(devc->digital_group.probes);
162 static int dev_clear(void)
164 return std_dev_clear(di, clear_helper);
167 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
172 va_start(args, format);
173 vsnprintf(buf, 255, format, args);
175 if (rigol_ds_send(sdi, buf) != SR_OK)
178 /* When setting a bunch of parameters in a row, the DS1052E scrambles
179 * some of them unless there is at least 100ms delay in between. */
180 sr_spew("delay %dms", 100);
186 static int init(struct sr_context *sr_ctx)
188 return std_init(sr_ctx, di, LOG_PREFIX);
191 static int probe_port(const char *port, GSList **devices)
193 struct dev_context *devc;
194 struct sr_dev_inst *sdi;
195 struct sr_serial_dev_inst *serial;
196 struct sr_probe *probe;
199 gboolean matched, has_digital;
200 const char *manufacturer, *model, *version;
202 gchar **tokens, *channel_name;
205 if (!(serial = sr_serial_dev_inst_new(port, NULL)))
206 return SR_ERR_MALLOC;
208 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
210 len = serial_write(serial, "*IDN?", 5);
211 len = serial_read(serial, buf, sizeof(buf));
212 if (serial_close(serial) != SR_OK)
215 sr_serial_dev_inst_free(serial);
221 tokens = g_strsplit(buf, ",", 0);
222 sr_dbg("response: %s [%s]", port, buf);
224 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
226 if (num_tokens < 4) {
231 manufacturer = tokens[0];
235 if (strcmp(manufacturer, "Rigol Technologies")) {
240 matched = has_digital = FALSE;
241 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
242 if (!strcmp(model, supported_models[i])) {
244 has_digital = g_str_has_suffix(model, "D");
249 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
250 manufacturer, model, version))) {
257 if (!(sdi->conn = sr_serial_dev_inst_new(port, NULL)))
258 return SR_ERR_MALLOC;
260 sdi->inst_type = SR_INST_SERIAL;
262 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
263 return SR_ERR_MALLOC;
264 devc->limit_frames = 0;
265 devc->has_digital = has_digital;
267 for (i = 0; i < 2; i++) {
268 channel_name = (i == 0 ? "CH1" : "CH2");
269 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name)))
270 return SR_ERR_MALLOC;
271 sdi->probes = g_slist_append(sdi->probes, probe);
272 devc->analog_groups[i].name = channel_name;
273 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
274 sdi->probe_groups = g_slist_append(sdi->probe_groups,
275 &devc->analog_groups[i]);
278 if (devc->has_digital) {
279 for (i = 0; i < 16; i++) {
280 if (!(channel_name = g_strdup_printf("D%d", i)))
281 return SR_ERR_MALLOC;
282 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
283 g_free(channel_name);
285 return SR_ERR_MALLOC;
286 sdi->probes = g_slist_append(sdi->probes, probe);
287 devc->digital_group.probes = g_slist_append(
288 devc->digital_group.probes, probe);
289 devc->digital_group.name = "LA";
290 sdi->probe_groups = g_slist_append(sdi->probe_groups,
291 &devc->digital_group);
296 *devices = g_slist_append(NULL, sdi);
301 static GSList *scan(GSList *options)
303 struct drv_context *drvc;
304 struct sr_config *src;
308 const gchar *dev_name;
313 for (l = options; l; l = l->next) {
315 if (src->key == SR_CONF_CONN) {
316 port = (char *)g_variant_get_string(src->data, NULL);
323 if (probe_port(port, &devices) == SR_ERR_MALLOC)
326 if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
327 if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
329 while ((dev_name = g_dir_read_name(dir))) {
330 if (strncmp(dev_name, "usbtmc", 6))
332 port = g_strconcat("/dev/", dev_name, NULL);
333 ret = probe_port(port, &devices);
335 if (ret == SR_ERR_MALLOC) {
343 /* Tack a copy of the newly found devices onto the driver list. */
344 l = g_slist_copy(devices);
345 drvc->instances = g_slist_concat(drvc->instances, l);
350 static GSList *dev_list(void)
352 return ((struct drv_context *)(di->priv))->instances;
355 static int dev_open(struct sr_dev_inst *sdi)
358 if (serial_open(sdi->conn, SERIAL_RDWR) != SR_OK)
361 if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
364 sdi->status = SR_ST_ACTIVE;
369 static int dev_close(struct sr_dev_inst *sdi)
371 struct sr_serial_dev_inst *serial;
374 if (serial && serial->fd != -1) {
375 serial_close(serial);
376 sdi->status = SR_ST_INACTIVE;
382 static int cleanup(void)
387 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
388 const struct sr_probe_group *probe_group)
390 struct dev_context *devc;
393 if (!sdi || !(devc = sdi->priv))
396 /* If a probe group is specified, it must be a valid one. */
398 if (probe_group != &devc->analog_groups[0]
399 && probe_group != &devc->analog_groups[1]) {
400 sr_err("Invalid probe group specified.");
406 case SR_CONF_NUM_TIMEBASE:
407 *data = g_variant_new_int32(NUM_TIMEBASE);
409 case SR_CONF_NUM_VDIV:
411 sr_err("No probe group specified.");
412 return SR_ERR_PROBE_GROUP;
414 for (i = 0; i < 2; i++) {
415 if (probe_group == &devc->analog_groups[i]) {
416 *data = g_variant_new_int32(NUM_VDIV);
428 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
429 const struct sr_probe_group *probe_group)
431 struct dev_context *devc;
432 uint64_t tmp_u64, p, q;
438 if (!(devc = sdi->priv))
441 if (sdi->status != SR_ST_ACTIVE)
442 return SR_ERR_DEV_CLOSED;
444 /* If a probe group is specified, it must be a valid one. */
446 if (probe_group != &devc->analog_groups[0]
447 && probe_group != &devc->analog_groups[1]) {
448 sr_err("Invalid probe group specified.");
455 case SR_CONF_LIMIT_FRAMES:
456 devc->limit_frames = g_variant_get_uint64(data);
458 case SR_CONF_TRIGGER_SLOPE:
459 tmp_u64 = g_variant_get_uint64(data);
460 if (tmp_u64 != 0 && tmp_u64 != 1)
462 g_free(devc->trigger_slope);
463 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
464 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
466 case SR_CONF_HORIZ_TRIGGERPOS:
467 t_dbl = g_variant_get_double(data);
468 if (t_dbl < 0.0 || t_dbl > 1.0)
470 devc->horiz_triggerpos = t_dbl;
471 /* We have the trigger offset as a percentage of the frame, but
472 * need to express this in seconds. */
473 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * NUM_TIMEBASE;
474 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
476 case SR_CONF_TIMEBASE:
477 g_variant_get(data, "(tt)", &p, &q);
478 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
479 if (timebases[i][0] == p && timebases[i][1] == q) {
480 devc->timebase = (float)p / q;
481 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
485 if (i == ARRAY_SIZE(timebases))
488 case SR_CONF_TRIGGER_SOURCE:
489 tmp_str = g_variant_get_string(data, NULL);
490 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
491 if (!strcmp(trigger_sources[i], tmp_str)) {
492 g_free(devc->trigger_source);
493 devc->trigger_source = g_strdup(trigger_sources[i]);
494 if (!strcmp(devc->trigger_source, "AC Line"))
496 else if (!strcmp(devc->trigger_source, "CH1"))
498 else if (!strcmp(devc->trigger_source, "CH2"))
501 tmp_str = (char *)devc->trigger_source;
502 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
506 if (i == ARRAY_SIZE(trigger_sources))
511 sr_err("No probe group specified.");
512 return SR_ERR_PROBE_GROUP;
514 g_variant_get(data, "(tt)", &p, &q);
515 for (i = 0; i < 2; i++) {
516 if (probe_group == &devc->analog_groups[i]) {
517 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
518 if (vdivs[j][0] != p || vdivs[j][1] != q)
520 devc->vdiv[i] = (float)p / q;
521 return set_cfg(sdi, ":CHAN%d:SCAL %.3f", i + 1,
528 case SR_CONF_COUPLING:
530 sr_err("No probe group specified.");
531 return SR_ERR_PROBE_GROUP;
533 tmp_str = g_variant_get_string(data, NULL);
534 for (i = 0; i < 2; i++) {
535 if (probe_group == &devc->analog_groups[i]) {
536 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
537 if (!strcmp(tmp_str, coupling[j])) {
538 g_free(devc->coupling[i]);
539 devc->coupling[i] = g_strdup(coupling[j]);
540 return set_cfg(sdi, ":CHAN%d:COUP %s", i + 1,
556 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
557 const struct sr_probe_group *probe_group)
559 GVariant *tuple, *rational[2];
562 struct dev_context *devc;
564 if (key == SR_CONF_SCAN_OPTIONS) {
565 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
566 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
568 } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) {
569 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
570 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
574 /* Every other option requires a valid device instance. */
575 if (!sdi || !(devc = sdi->priv))
578 /* If a probe group is specified, it must be a valid one. */
580 if (probe_group != &devc->analog_groups[0]
581 && probe_group != &devc->analog_groups[1]) {
582 sr_err("Invalid probe group specified.");
589 case SR_CONF_DEVICE_OPTIONS:
591 sr_err("No probe group specified.");
592 return SR_ERR_PROBE_GROUP;
594 if (probe_group == &devc->digital_group) {
595 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
596 NULL, 0, sizeof(int32_t));
599 for (i = 0; i < 2; i++) {
600 if (probe_group == &devc->analog_groups[i]) {
601 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
602 analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t));
609 case SR_CONF_COUPLING:
611 sr_err("No probe group specified.");
612 return SR_ERR_PROBE_GROUP;
614 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
618 sr_err("No probe group specified.");
619 return SR_ERR_PROBE_GROUP;
621 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
622 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
623 rational[0] = g_variant_new_uint64(vdivs[i][0]);
624 rational[1] = g_variant_new_uint64(vdivs[i][1]);
625 tuple = g_variant_new_tuple(rational, 2);
626 g_variant_builder_add_value(&gvb, tuple);
628 *data = g_variant_builder_end(&gvb);
630 case SR_CONF_TIMEBASE:
631 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
632 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
633 rational[0] = g_variant_new_uint64(timebases[i][0]);
634 rational[1] = g_variant_new_uint64(timebases[i][1]);
635 tuple = g_variant_new_tuple(rational, 2);
636 g_variant_builder_add_value(&gvb, tuple);
638 *data = g_variant_builder_end(&gvb);
640 case SR_CONF_TRIGGER_SOURCE:
641 *data = g_variant_new_strv(trigger_sources,
642 devc->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
651 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
653 struct sr_serial_dev_inst *serial;
654 struct dev_context *devc;
655 struct sr_probe *probe;
659 if (sdi->status != SR_ST_ACTIVE)
660 return SR_ERR_DEV_CLOSED;
665 for (l = sdi->probes; l; l = l->next) {
667 sr_dbg("handling probe %s", probe->name);
668 if (probe->type == SR_PROBE_ANALOG) {
670 devc->enabled_analog_probes = g_slist_append(
671 devc->enabled_analog_probes, probe);
672 if (probe->enabled != devc->analog_channels[probe->index]) {
673 /* Enabled channel is currently disabled, or vice versa. */
674 sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
675 probe->enabled ? "ON" : "OFF");
676 if (rigol_ds_send(sdi, cmd) != SR_OK)
679 } else if (probe->type == SR_PROBE_LOGIC) {
681 devc->enabled_digital_probes = g_slist_append(
682 devc->enabled_digital_probes, probe);
683 if (probe->enabled != devc->digital_channels[probe->index]) {
684 /* Enabled channel is currently disabled, or vice versa. */
685 sprintf(cmd, ":DIG%d:TURN %s", probe->index,
686 probe->enabled ? "ON" : "OFF");
687 if (rigol_ds_send(sdi, cmd) != SR_OK)
692 if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
695 sr_source_add(serial->fd, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
697 /* Send header packet to the session bus. */
698 std_session_send_df_header(cb_data, LOG_PREFIX);
700 /* Fetch the first frame. */
701 if (devc->enabled_analog_probes) {
702 devc->channel_frame = devc->enabled_analog_probes->data;
703 if (rigol_ds_send(sdi, ":WAV:DATA? CHAN%d",
704 devc->channel_frame->index + 1) != SR_OK)
707 devc->channel_frame = devc->enabled_digital_probes->data;
708 if (rigol_ds_send(sdi, ":WAV:DATA? DIG") != SR_OK)
712 devc->num_frame_bytes = 0;
717 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
719 struct dev_context *devc;
720 struct sr_serial_dev_inst *serial;
726 if (sdi->status != SR_ST_ACTIVE) {
727 sr_err("Device inactive, can't stop acquisition.");
731 g_slist_free(devc->enabled_analog_probes);
732 g_slist_free(devc->enabled_digital_probes);
733 devc->enabled_analog_probes = NULL;
734 devc->enabled_digital_probes = NULL;
736 sr_source_remove(serial->fd);
741 SR_PRIV struct sr_dev_driver rigol_ds_driver_info = {
743 .longname = "Rigol DS",
748 .dev_list = dev_list,
749 .dev_clear = dev_clear,
750 .config_get = config_get,
751 .config_set = config_set,
752 .config_list = config_list,
753 .dev_open = dev_open,
754 .dev_close = dev_close,
755 .dev_acquisition_start = dev_acquisition_start,
756 .dev_acquisition_stop = dev_acquisition_stop,