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_ds1xx2_driver_info;
146 static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info;
148 static void clear_helper(void *priv)
150 struct dev_context *devc;
152 for (l = drvc->instances; l; l = l->next) {
153 if (!(sdi = l->data))
157 sr_serial_dev_inst_free(sdi->conn);
159 g_slist_free(sdi->probe_groups);
161 if (!(devc = sdi->priv))
164 g_free(devc->coupling[0]);
165 g_free(devc->coupling[1]);
166 g_free(devc->trigger_source);
167 g_free(devc->trigger_slope);
168 g_slist_free(devc->analog_groups[0].probes);
169 g_slist_free(devc->analog_groups[1].probes);
170 g_slist_free(devc->digital_group.probes);
172 sr_dev_inst_free(sdi);
175 g_free(devc->coupling[0]);
176 g_free(devc->coupling[1]);
177 g_free(devc->trigger_source);
178 g_free(devc->trigger_slope);
181 static int dev_clear(void)
183 return std_dev_clear(di, clear_helper);
186 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
191 va_start(args, format);
192 vsnprintf(buf, 255, format, args);
194 if (rigol_ds1xx2_send(sdi, buf) != SR_OK)
197 /* When setting a bunch of parameters in a row, the DS1052E scrambles
198 * some of them unless there is at least 100ms delay in between. */
199 sr_spew("delay %dms", 100);
205 static int init(struct sr_context *sr_ctx)
207 return std_init(sr_ctx, di, LOG_PREFIX);
210 static int probe_port(const char *port, GSList **devices)
212 struct dev_context *devc;
213 struct sr_dev_inst *sdi;
214 struct sr_serial_dev_inst *serial;
215 struct sr_probe *probe;
218 gboolean matched, has_digital;
219 const char *manufacturer, *model, *version;
221 gchar **tokens, *channel_name;
224 if (!(serial = sr_serial_dev_inst_new(port, NULL)))
225 return SR_ERR_MALLOC;
227 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
229 len = serial_write(serial, "*IDN?", 5);
230 len = serial_read(serial, buf, sizeof(buf));
231 if (serial_close(serial) != SR_OK)
234 sr_serial_dev_inst_free(serial);
240 tokens = g_strsplit(buf, ",", 0);
241 sr_dbg("response: %s [%s]", port, buf);
243 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
245 if (num_tokens < 4) {
250 manufacturer = tokens[0];
254 if (strcmp(manufacturer, "Rigol Technologies")) {
259 matched = has_digital = FALSE;
260 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
261 if (!strcmp(model, supported_models[i])) {
263 has_digital = g_str_has_suffix(model, "D");
268 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
269 manufacturer, model, version))) {
276 if (!(sdi->conn = sr_serial_dev_inst_new(port, NULL)))
277 return SR_ERR_MALLOC;
279 sdi->inst_type = SR_INST_SERIAL;
281 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
282 return SR_ERR_MALLOC;
283 devc->limit_frames = 0;
284 devc->has_digital = has_digital;
286 for (i = 0; i < 2; i++) {
287 channel_name = (i == 0 ? "CH1" : "CH2");
288 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name)))
289 return SR_ERR_MALLOC;
290 sdi->probes = g_slist_append(sdi->probes, probe);
291 devc->analog_groups[i].name = channel_name;
292 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
293 sdi->probe_groups = g_slist_append(sdi->probe_groups,
294 &devc->analog_groups[i]);
297 if (devc->has_digital) {
298 for (i = 0; i < 16; i++) {
299 if (!(channel_name = g_strdup_printf("D%d", i)))
300 return SR_ERR_MALLOC;
301 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
302 g_free(channel_name);
304 return SR_ERR_MALLOC;
305 sdi->probes = g_slist_append(sdi->probes, probe);
306 devc->digital_group.probes = g_slist_append(
307 devc->digital_group.probes, probe);
308 devc->digital_group.name = "LA";
309 sdi->probe_groups = g_slist_append(sdi->probe_groups,
310 &devc->digital_group);
315 *devices = g_slist_append(NULL, sdi);
320 static GSList *scan(GSList *options)
322 struct drv_context *drvc;
323 struct sr_config *src;
327 const gchar *dev_name;
332 for (l = options; l; l = l->next) {
334 if (src->key == SR_CONF_CONN) {
335 port = (char *)g_variant_get_string(src->data, NULL);
342 if (probe_port(port, &devices) == SR_ERR_MALLOC)
345 if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
346 if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
348 while ((dev_name = g_dir_read_name(dir))) {
349 if (strncmp(dev_name, "usbtmc", 6))
351 port = g_strconcat("/dev/", dev_name, NULL);
352 ret = probe_port(port, &devices);
354 if (ret == SR_ERR_MALLOC) {
362 /* Tack a copy of the newly found devices onto the driver list. */
363 l = g_slist_copy(devices);
364 drvc->instances = g_slist_concat(drvc->instances, l);
369 static GSList *dev_list(void)
371 return ((struct drv_context *)(di->priv))->instances;
374 static int dev_open(struct sr_dev_inst *sdi)
377 if (serial_open(sdi->conn, SERIAL_RDWR) != SR_OK)
380 if (rigol_ds1xx2_get_dev_cfg(sdi) != SR_OK)
383 sdi->status = SR_ST_ACTIVE;
388 static int dev_close(struct sr_dev_inst *sdi)
390 struct sr_serial_dev_inst *serial;
393 if (serial && serial->fd != -1) {
394 serial_close(serial);
395 sdi->status = SR_ST_INACTIVE;
401 static int cleanup(void)
406 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
407 const struct sr_probe_group *probe_group)
409 struct dev_context *devc;
412 if (!sdi || !(devc = sdi->priv))
416 case SR_CONF_NUM_TIMEBASE:
417 *data = g_variant_new_int32(NUM_TIMEBASE);
419 case SR_CONF_NUM_VDIV:
420 for (i = 0; i < 2; i++) {
421 if (probe_group == &devc->analog_groups[i]) {
422 *data = g_variant_new_int32(NUM_VDIV);
434 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
435 const struct sr_probe_group *probe_group)
437 struct dev_context *devc;
438 uint64_t tmp_u64, p, q;
444 if (!(devc = sdi->priv))
447 if (sdi->status != SR_ST_ACTIVE)
448 return SR_ERR_DEV_CLOSED;
452 case SR_CONF_LIMIT_FRAMES:
453 devc->limit_frames = g_variant_get_uint64(data);
455 case SR_CONF_TRIGGER_SLOPE:
456 tmp_u64 = g_variant_get_uint64(data);
457 if (tmp_u64 != 0 && tmp_u64 != 1)
459 g_free(devc->trigger_slope);
460 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
461 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
463 case SR_CONF_HORIZ_TRIGGERPOS:
464 t_dbl = g_variant_get_double(data);
465 if (t_dbl < 0.0 || t_dbl > 1.0)
467 devc->horiz_triggerpos = t_dbl;
468 /* We have the trigger offset as a percentage of the frame, but
469 * need to express this in seconds. */
470 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * NUM_TIMEBASE;
471 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
473 case SR_CONF_TIMEBASE:
474 g_variant_get(data, "(tt)", &p, &q);
475 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
476 if (timebases[i][0] == p && timebases[i][1] == q) {
477 devc->timebase = (float)p / q;
478 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
482 if (i == ARRAY_SIZE(timebases))
485 case SR_CONF_TRIGGER_SOURCE:
486 tmp_str = g_variant_get_string(data, NULL);
487 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
488 if (!strcmp(trigger_sources[i], tmp_str)) {
489 g_free(devc->trigger_source);
490 devc->trigger_source = g_strdup(trigger_sources[i]);
491 if (!strcmp(devc->trigger_source, "AC Line"))
493 else if (!strcmp(devc->trigger_source, "CH1"))
495 else if (!strcmp(devc->trigger_source, "CH2"))
498 tmp_str = (char *)devc->trigger_source;
499 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
503 if (i == ARRAY_SIZE(trigger_sources))
507 g_variant_get(data, "(tt)", &p, &q);
508 for (i = 0; i < 2; i++) {
509 if (probe_group == &devc->analog_groups[i])
511 for (j = 0; j < ARRAY_SIZE(vdivs); j++)
513 if (vdivs[j][0] != p || vdivs[j][1] != q)
515 devc->vdiv[i] = (float)p / q;
516 return set_cfg(sdi, ":CHAN%d:SCAL %.3f", i + 1,
523 case SR_CONF_COUPLING:
524 tmp_str = g_variant_get_string(data, NULL);
525 for (i = 0; i < 2; i++) {
526 if (probe_group == &devc->analog_groups[i])
528 for (j = 0; j < ARRAY_SIZE(coupling); j++)
530 if (!strcmp(tmp_str, coupling[j]))
532 g_free(devc->coupling[i]);
533 devc->coupling[i] = g_strdup(coupling[j]);
534 return set_cfg(sdi, ":CHAN%d:COUP %s", i + 1,
550 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
551 const struct sr_probe_group *probe_group)
553 GVariant *tuple, *rational[2];
556 struct dev_context *devc;
558 if (key == SR_CONF_SCAN_OPTIONS) {
559 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
560 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
562 } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) {
563 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
564 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
568 /* Every other option requires a valid device instance. */
569 if (!sdi || !(devc = sdi->priv))
574 case SR_CONF_DEVICE_OPTIONS:
575 if (probe_group == &devc->digital_group) {
576 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
577 NULL, 0, sizeof(int32_t));
580 for (i = 0; i < 2; i++) {
581 if (probe_group == &devc->analog_groups[i]) {
582 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
583 analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t));
589 case SR_CONF_COUPLING:
590 for (i = 0; i < 2; i++) {
591 if (probe_group == &devc->analog_groups[i]) {
592 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
598 for (i = 0; i < 2; i++) {
599 if (probe_group == &devc->analog_groups[i]) {
600 rational[0] = g_variant_new_uint64(vdivs[i][0]);
601 rational[1] = g_variant_new_uint64(vdivs[i][1]);
602 *data = g_variant_new_tuple(rational, 2);
607 case SR_CONF_TIMEBASE:
608 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
609 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
610 rational[0] = g_variant_new_uint64(timebases[i][0]);
611 rational[1] = g_variant_new_uint64(timebases[i][1]);
612 tuple = g_variant_new_tuple(rational, 2);
613 g_variant_builder_add_value(&gvb, tuple);
615 *data = g_variant_builder_end(&gvb);
617 case SR_CONF_TRIGGER_SOURCE:
618 *data = g_variant_new_strv(trigger_sources,
619 devc->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
628 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
630 struct sr_serial_dev_inst *serial;
631 struct dev_context *devc;
632 struct sr_probe *probe;
636 if (sdi->status != SR_ST_ACTIVE)
637 return SR_ERR_DEV_CLOSED;
642 for (l = sdi->probes; l; l = l->next) {
644 sr_dbg("handling probe %s", probe->name);
645 if (probe->type == SR_PROBE_ANALOG) {
647 devc->enabled_analog_probes = g_slist_append(
648 devc->enabled_analog_probes, probe);
649 if (probe->enabled != devc->analog_channels[probe->index]) {
650 /* Enabled channel is currently disabled, or vice versa. */
651 sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
652 probe->enabled ? "ON" : "OFF");
653 if (rigol_ds1xx2_send(sdi, cmd) != SR_OK)
656 } else if (probe->type == SR_PROBE_LOGIC) {
658 devc->enabled_digital_probes = g_slist_append(
659 devc->enabled_digital_probes, probe);
660 if (probe->enabled != devc->digital_channels[probe->index]) {
661 /* Enabled channel is currently disabled, or vice versa. */
662 sprintf(cmd, ":DIG%d:TURN %s", probe->index,
663 probe->enabled ? "ON" : "OFF");
664 if (rigol_ds1xx2_send(sdi, cmd) != SR_OK)
669 if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
672 sr_source_add(serial->fd, G_IO_IN, 50, rigol_ds1xx2_receive, (void *)sdi);
674 /* Send header packet to the session bus. */
675 std_session_send_df_header(cb_data, LOG_PREFIX);
677 /* Fetch the first frame. */
678 if (devc->enabled_analog_probes) {
679 devc->channel_frame = devc->enabled_analog_probes->data;
680 if (rigol_ds1xx2_send(sdi, ":WAV:DATA? CHAN%d",
681 devc->channel_frame->index + 1) != SR_OK)
684 devc->channel_frame = devc->enabled_digital_probes->data;
685 if (rigol_ds1xx2_send(sdi, ":WAV:DATA? DIG") != SR_OK)
689 devc->num_frame_bytes = 0;
694 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
696 struct dev_context *devc;
697 struct sr_serial_dev_inst *serial;
703 if (sdi->status != SR_ST_ACTIVE) {
704 sr_err("Device inactive, can't stop acquisition.");
708 g_slist_free(devc->enabled_analog_probes);
709 g_slist_free(devc->enabled_digital_probes);
710 devc->enabled_analog_probes = NULL;
711 devc->enabled_digital_probes = NULL;
713 sr_source_remove(serial->fd);
718 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
719 .name = "rigol-ds1xx2",
720 .longname = "Rigol DS1xx2",
725 .dev_list = dev_list,
726 .dev_clear = dev_clear,
727 .config_get = config_get,
728 .config_set = config_set,
729 .config_list = config_list,
730 .dev_open = dev_open,
731 .dev_close = dev_close,
732 .dev_acquisition_start = dev_acquisition_start,
733 .dev_acquisition_stop = dev_acquisition_stop,