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>
6 * Copyright (C) 2013 Mathias Grimmberger <mgri@zaphod.sax.de>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "libsigrok.h"
28 #include "libsigrok-internal.h"
31 static const int32_t hwopts[] = {
35 static const int32_t hwcaps[] = {
38 SR_CONF_TRIGGER_SOURCE,
39 SR_CONF_TRIGGER_SLOPE,
40 SR_CONF_HORIZ_TRIGGERPOS,
44 static const int32_t analog_hwcaps[] = {
50 static const uint64_t timebases[][2] = {
89 /* { 1000, 1 }, Confuses other code? */
92 static const uint64_t vdivs[][2] = {
112 #define NUM_TIMEBASE ARRAY_SIZE(timebases)
113 #define NUM_VDIV ARRAY_SIZE(vdivs)
115 static const char *trigger_sources[] = {
138 static const char *coupling[] = {
144 /* name, series, min timebase, max timebase, min vdiv, digital channels */
145 static const struct rigol_ds_model supported_models[] = {
146 {"DS1052E", 1, {5, 1000000000}, {50, 1}, {2, 1000}, false},
147 {"DS1102E", 1, {2, 1000000000}, {50, 1}, {2, 1000}, false},
148 {"DS1152E", 1, {2, 1000000000}, {50, 1}, {2, 1000}, false},
149 {"DS1052D", 1, {5, 1000000000}, {50, 1}, {2, 1000}, true},
150 {"DS1102D", 1, {2, 1000000000}, {50, 1}, {2, 1000}, true},
151 {"DS1152D", 1, {2, 1000000000}, {50, 1}, {2, 1000}, true},
152 {"DS2072", 2, {5, 1000000000}, {500, 1}, {500, 1000000}, false},
153 {"DS2102", 2, {5, 1000000000}, {500, 1}, {500, 1000000}, false},
154 {"DS2202", 2, {2, 1000000000}, {500, 1}, {500, 1000000}, false},
157 SR_PRIV struct sr_dev_driver rigol_ds_driver_info;
158 static struct sr_dev_driver *di = &rigol_ds_driver_info;
160 static void clear_helper(void *priv)
162 struct dev_context *devc;
165 g_free(devc->coupling[0]);
166 g_free(devc->coupling[1]);
167 g_free(devc->trigger_source);
168 g_free(devc->trigger_slope);
169 g_slist_free(devc->analog_groups[0].probes);
170 g_slist_free(devc->analog_groups[1].probes);
171 g_slist_free(devc->digital_group.probes);
174 static int dev_clear(void)
176 return std_dev_clear(di, clear_helper);
179 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
184 va_start(args, format);
185 vsnprintf(buf, 255, format, args);
187 if (rigol_ds_send(sdi, buf) != SR_OK)
190 /* When setting a bunch of parameters in a row, the DS1052E scrambles
191 * some of them unless there is at least 100ms delay in between. */
192 sr_spew("delay %dms", 100);
198 static int init(struct sr_context *sr_ctx)
200 return std_init(sr_ctx, di, LOG_PREFIX);
203 static int probe_port(const char *port, GSList **devices)
205 struct dev_context *devc;
206 struct sr_dev_inst *sdi;
207 struct sr_serial_dev_inst *serial;
208 struct sr_probe *probe;
211 const char *manufacturer, *model_name, *version;
212 const struct rigol_ds_model *model = NULL;
214 gchar **tokens, *channel_name;
217 if (!(serial = sr_serial_dev_inst_new(port, NULL)))
218 return SR_ERR_MALLOC;
220 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
222 len = serial_write(serial, "*IDN?", 5);
223 len = serial_read(serial, buf, sizeof(buf));
224 if (serial_close(serial) != SR_OK)
227 sr_serial_dev_inst_free(serial);
233 tokens = g_strsplit(buf, ",", 0);
234 sr_dbg("response: %s [%s]", port, buf);
236 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
238 if (num_tokens < 4) {
243 manufacturer = tokens[0];
244 model_name = tokens[1];
247 if (strcasecmp(manufacturer, "Rigol Technologies")) {
252 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
253 if (!strcmp(model_name, supported_models[i].name)) {
254 model = &supported_models[i];
259 if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
260 manufacturer, model_name, version))) {
267 if (!(sdi->conn = sr_serial_dev_inst_new(port, NULL)))
268 return SR_ERR_MALLOC;
270 sdi->inst_type = SR_INST_SERIAL;
272 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
273 return SR_ERR_MALLOC;
274 devc->limit_frames = 0;
277 for (i = 0; i < 2; i++) {
278 channel_name = (i == 0 ? "CH1" : "CH2");
279 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name)))
280 return SR_ERR_MALLOC;
281 sdi->probes = g_slist_append(sdi->probes, probe);
282 devc->analog_groups[i].name = channel_name;
283 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
284 sdi->probe_groups = g_slist_append(sdi->probe_groups,
285 &devc->analog_groups[i]);
288 if (devc->model->has_digital) {
289 for (i = 0; i < 16; i++) {
290 if (!(channel_name = g_strdup_printf("D%d", i)))
291 return SR_ERR_MALLOC;
292 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
293 g_free(channel_name);
295 return SR_ERR_MALLOC;
296 sdi->probes = g_slist_append(sdi->probes, probe);
297 devc->digital_group.probes = g_slist_append(
298 devc->digital_group.probes, probe);
299 devc->digital_group.name = "LA";
300 sdi->probe_groups = g_slist_append(sdi->probe_groups,
301 &devc->digital_group);
305 for (i = 0; i < NUM_TIMEBASE; i++) {
306 if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
307 devc->timebases = &timebases[i];
308 if (!memcmp(&devc->model->max_timebase, &timebases[i], sizeof(uint64_t[2])))
309 devc->num_timebases = &timebases[i] - devc->timebases + 1;
312 for (i = 0; i < NUM_VDIV; i++) {
313 if (!memcmp(&devc->model->min_vdiv, &vdivs[i], sizeof(uint64_t[2]))) {
314 devc->vdivs = &vdivs[i];
315 devc->num_vdivs = NUM_VDIV - (&vdivs[i] - &vdivs[0]);
321 *devices = g_slist_append(NULL, sdi);
326 static GSList *scan(GSList *options)
328 struct drv_context *drvc;
329 struct sr_config *src;
333 const gchar *dev_name;
338 for (l = options; l; l = l->next) {
340 if (src->key == SR_CONF_CONN) {
341 port = (char *)g_variant_get_string(src->data, NULL);
348 if (probe_port(port, &devices) == SR_ERR_MALLOC)
351 if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
352 if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
354 while ((dev_name = g_dir_read_name(dir))) {
355 if (strncmp(dev_name, "usbtmc", 6))
357 port = g_strconcat("/dev/", dev_name, NULL);
358 ret = probe_port(port, &devices);
360 if (ret == SR_ERR_MALLOC) {
368 /* Tack a copy of the newly found devices onto the driver list. */
369 l = g_slist_copy(devices);
370 drvc->instances = g_slist_concat(drvc->instances, l);
375 static GSList *dev_list(void)
377 return ((struct drv_context *)(di->priv))->instances;
380 static int dev_open(struct sr_dev_inst *sdi)
383 if (serial_open(sdi->conn, SERIAL_RDWR) != SR_OK)
386 if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
389 sdi->status = SR_ST_ACTIVE;
394 static int dev_close(struct sr_dev_inst *sdi)
396 struct sr_serial_dev_inst *serial;
399 if (serial && serial->fd != -1) {
400 serial_close(serial);
401 sdi->status = SR_ST_INACTIVE;
407 static int cleanup(void)
412 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
413 const struct sr_probe_group *probe_group)
415 struct dev_context *devc;
418 if (!sdi || !(devc = sdi->priv))
421 /* If a probe group is specified, it must be a valid one. */
423 if (probe_group != &devc->analog_groups[0]
424 && probe_group != &devc->analog_groups[1]) {
425 sr_err("Invalid probe group specified.");
431 case SR_CONF_NUM_TIMEBASE:
432 *data = g_variant_new_int32(devc->num_timebases);
434 case SR_CONF_NUM_VDIV:
436 sr_err("No probe group specified.");
437 return SR_ERR_PROBE_GROUP;
439 for (i = 0; i < 2; i++) {
440 if (probe_group == &devc->analog_groups[i]) {
441 *data = g_variant_new_int32(devc->num_vdivs);
453 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
454 const struct sr_probe_group *probe_group)
456 struct dev_context *devc;
457 uint64_t tmp_u64, p, q;
463 if (!(devc = sdi->priv))
466 if (sdi->status != SR_ST_ACTIVE)
467 return SR_ERR_DEV_CLOSED;
469 /* If a probe group is specified, it must be a valid one. */
471 if (probe_group != &devc->analog_groups[0]
472 && probe_group != &devc->analog_groups[1]) {
473 sr_err("Invalid probe group specified.");
480 case SR_CONF_LIMIT_FRAMES:
481 devc->limit_frames = g_variant_get_uint64(data);
483 case SR_CONF_TRIGGER_SLOPE:
484 tmp_u64 = g_variant_get_uint64(data);
485 if (tmp_u64 != 0 && tmp_u64 != 1)
487 g_free(devc->trigger_slope);
488 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
489 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
491 case SR_CONF_HORIZ_TRIGGERPOS:
492 t_dbl = g_variant_get_double(data);
493 if (t_dbl < 0.0 || t_dbl > 1.0)
495 devc->horiz_triggerpos = t_dbl;
496 /* We have the trigger offset as a percentage of the frame, but
497 * need to express this in seconds. */
498 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
499 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
501 case SR_CONF_TIMEBASE:
502 g_variant_get(data, "(tt)", &p, &q);
503 for (i = 0; i < devc->num_timebases; i++) {
504 if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
505 devc->timebase = (float)p / q;
506 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
510 if (i == devc->num_timebases)
513 case SR_CONF_TRIGGER_SOURCE:
514 tmp_str = g_variant_get_string(data, NULL);
515 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
516 if (!strcmp(trigger_sources[i], tmp_str)) {
517 g_free(devc->trigger_source);
518 devc->trigger_source = g_strdup(trigger_sources[i]);
519 if (!strcmp(devc->trigger_source, "AC Line"))
521 else if (!strcmp(devc->trigger_source, "CH1"))
523 else if (!strcmp(devc->trigger_source, "CH2"))
526 tmp_str = (char *)devc->trigger_source;
527 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
531 if (i == ARRAY_SIZE(trigger_sources))
536 sr_err("No probe group specified.");
537 return SR_ERR_PROBE_GROUP;
539 g_variant_get(data, "(tt)", &p, &q);
540 for (i = 0; i < 2; i++) {
541 if (probe_group == &devc->analog_groups[i]) {
542 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
543 if (vdivs[j][0] != p || vdivs[j][1] != q)
545 devc->vdiv[i] = (float)p / q;
546 return set_cfg(sdi, ":CHAN%d:SCAL %.3f", i + 1,
553 case SR_CONF_COUPLING:
555 sr_err("No probe group specified.");
556 return SR_ERR_PROBE_GROUP;
558 tmp_str = g_variant_get_string(data, NULL);
559 for (i = 0; i < 2; i++) {
560 if (probe_group == &devc->analog_groups[i]) {
561 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
562 if (!strcmp(tmp_str, coupling[j])) {
563 g_free(devc->coupling[i]);
564 devc->coupling[i] = g_strdup(coupling[j]);
565 return set_cfg(sdi, ":CHAN%d:COUP %s", i + 1,
581 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
582 const struct sr_probe_group *probe_group)
584 GVariant *tuple, *rational[2];
587 struct dev_context *devc = NULL;
592 if (key == SR_CONF_SCAN_OPTIONS) {
593 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
594 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
596 } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) {
597 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
598 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
602 /* Every other option requires a valid device instance. */
603 if (!sdi || !(devc = sdi->priv))
606 /* If a probe group is specified, it must be a valid one. */
608 if (probe_group != &devc->analog_groups[0]
609 && probe_group != &devc->analog_groups[1]) {
610 sr_err("Invalid probe group specified.");
617 case SR_CONF_DEVICE_OPTIONS:
619 sr_err("No probe group specified.");
620 return SR_ERR_PROBE_GROUP;
622 if (probe_group == &devc->digital_group) {
623 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
624 NULL, 0, sizeof(int32_t));
627 for (i = 0; i < 2; i++) {
628 if (probe_group == &devc->analog_groups[i]) {
629 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
630 analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t));
637 case SR_CONF_COUPLING:
639 sr_err("No probe group specified.");
640 return SR_ERR_PROBE_GROUP;
642 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
646 /* Can't know this until we have the exact model. */
649 sr_err("No probe group specified.");
650 return SR_ERR_PROBE_GROUP;
652 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
653 for (i = 0; i < devc->num_vdivs; i++) {
654 rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
655 rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
656 tuple = g_variant_new_tuple(rational, 2);
657 g_variant_builder_add_value(&gvb, tuple);
659 *data = g_variant_builder_end(&gvb);
661 case SR_CONF_TIMEBASE:
663 /* Can't know this until we have the exact model. */
665 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
666 for (i = 0; i < devc->num_timebases; i++) {
667 rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
668 rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
669 tuple = g_variant_new_tuple(rational, 2);
670 g_variant_builder_add_value(&gvb, tuple);
672 *data = g_variant_builder_end(&gvb);
674 case SR_CONF_TRIGGER_SOURCE:
676 /* Can't know this until we have the exact model. */
678 *data = g_variant_new_strv(trigger_sources,
679 devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
688 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
690 struct sr_serial_dev_inst *serial;
691 struct dev_context *devc;
692 struct sr_probe *probe;
696 if (sdi->status != SR_ST_ACTIVE)
697 return SR_ERR_DEV_CLOSED;
702 for (l = sdi->probes; l; l = l->next) {
704 sr_dbg("handling probe %s", probe->name);
705 if (probe->type == SR_PROBE_ANALOG) {
707 devc->enabled_analog_probes = g_slist_append(
708 devc->enabled_analog_probes, probe);
709 if (probe->enabled != devc->analog_channels[probe->index]) {
710 /* Enabled channel is currently disabled, or vice versa. */
711 sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
712 probe->enabled ? "ON" : "OFF");
713 if (rigol_ds_send(sdi, cmd) != SR_OK)
716 } else if (probe->type == SR_PROBE_LOGIC) {
718 devc->enabled_digital_probes = g_slist_append(
719 devc->enabled_digital_probes, probe);
720 if (probe->enabled != devc->digital_channels[probe->index]) {
721 /* Enabled channel is currently disabled, or vice versa. */
722 sprintf(cmd, ":DIG%d:TURN %s", probe->index,
723 probe->enabled ? "ON" : "OFF");
724 if (rigol_ds_send(sdi, cmd) != SR_OK)
729 if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
732 sr_source_add(serial->fd, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
734 /* Send header packet to the session bus. */
735 std_session_send_df_header(cb_data, LOG_PREFIX);
737 if (devc->model->series == 1) {
738 /* Fetch the first frame. */
739 if (devc->enabled_analog_probes) {
740 devc->channel_frame = devc->enabled_analog_probes->data;
741 if (rigol_ds_send(sdi, ":WAV:DATA? CHAN%d",
742 devc->channel_frame->index + 1) != SR_OK)
745 devc->channel_frame = devc->enabled_digital_probes->data;
746 if (rigol_ds_send(sdi, ":WAV:DATA? DIG") != SR_OK)
750 devc->num_frame_bytes = 0;
752 if (devc->enabled_analog_probes) {
753 /* Assume there already was a trigger event - don't wait */
754 if (rigol_ds2xx2_acquisition_start(sdi, FALSE) != SR_OK)
762 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
764 struct dev_context *devc;
765 struct sr_serial_dev_inst *serial;
771 if (sdi->status != SR_ST_ACTIVE) {
772 sr_err("Device inactive, can't stop acquisition.");
776 g_slist_free(devc->enabled_analog_probes);
777 g_slist_free(devc->enabled_digital_probes);
778 devc->enabled_analog_probes = NULL;
779 devc->enabled_digital_probes = NULL;
781 sr_source_remove(serial->fd);
786 SR_PRIV struct sr_dev_driver rigol_ds_driver_info = {
788 .longname = "Rigol DS",
793 .dev_list = dev_list,
794 .dev_clear = dev_clear,
795 .config_get = config_get,
796 .config_set = config_set,
797 .config_list = config_list,
798 .dev_open = dev_open,
799 .dev_close = dev_close,
800 .dev_acquisition_start = dev_acquisition_start,
801 .dev_acquisition_stop = dev_acquisition_stop,