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,
49 static const uint64_t timebases[][2] = {
87 static const uint64_t vdivs[][2] = {
104 static const char *trigger_sources[] = {
127 static const char *coupling[] = {
133 static const char *supported_models[] = {
142 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info;
143 static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info;
145 static void clear_helper(void *priv)
147 struct dev_context *devc;
149 for (l = drvc->instances; l; l = l->next) {
150 if (!(sdi = l->data))
154 sr_serial_dev_inst_free(sdi->conn);
156 g_slist_free(sdi->probe_groups);
158 if (!(devc = sdi->priv))
161 g_free(devc->device);
162 g_free(devc->coupling[0]);
163 g_free(devc->coupling[1]);
164 g_free(devc->trigger_source);
165 g_free(devc->trigger_slope);
166 g_slist_free(devc->analog_groups[0].probes);
167 g_slist_free(devc->analog_groups[1].probes);
168 g_slist_free(devc->digital_group.probes);
171 sr_dev_inst_free(sdi);
174 g_free(devc->coupling[0]);
175 g_free(devc->coupling[1]);
176 g_free(devc->trigger_source);
177 g_free(devc->trigger_slope);
180 static int dev_clear(void)
182 return std_dev_clear(di, clear_helper);
185 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
190 va_start(args, format);
191 vsnprintf(buf, 255, format, args);
193 if (rigol_ds1xx2_send(sdi, buf) != SR_OK)
196 /* When setting a bunch of parameters in a row, the DS1052E scrambles
197 * some of them unless there is at least 100ms delay in between. */
198 sr_spew("delay %dms", 100);
204 static int init(struct sr_context *sr_ctx)
206 return std_init(sr_ctx, di, LOG_PREFIX);
209 static int probe_port(const char *port, GSList **devices)
211 struct dev_context *devc;
212 struct sr_dev_inst *sdi;
213 struct sr_serial_dev_inst *serial;
214 struct sr_probe *probe;
217 gboolean matched, has_digital;
218 const char *manufacturer, *model, *version;
220 gchar **tokens, *channel_name;
223 if (!(serial = sr_serial_dev_inst_new(port, NULL)))
224 return SR_ERR_MALLOC;
226 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
228 len = serial_write(serial, "*IDN?", 5);
229 len = serial_read(serial, buf, sizeof(buf));
230 if (serial_close(serial) != SR_OK)
233 sr_serial_dev_inst_free(serial);
239 tokens = g_strsplit(buf, ",", 0);
240 sr_dbg("response: %s [%s]", port, buf);
242 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
244 if (num_tokens < 4) {
249 manufacturer = tokens[0];
253 if (strcmp(manufacturer, "Rigol Technologies")) {
258 matched = has_digital = FALSE;
259 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
260 if (!strcmp(model, supported_models[i])) {
262 has_digital = g_str_has_suffix(model, "D");
267 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
268 manufacturer, model, version))) {
275 if (!(sdi->conn = sr_serial_dev_inst_new(port, NULL)))
276 return SR_ERR_MALLOC;
278 sdi->inst_type = SR_INST_SERIAL;
280 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
281 return SR_ERR_MALLOC;
282 devc->limit_frames = 0;
283 devc->has_digital = has_digital;
285 for (i = 0; i < 2; i++) {
286 channel_name = (i == 0 ? "CH1" : "CH2");
287 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name)))
288 return SR_ERR_MALLOC;
289 sdi->probes = g_slist_append(sdi->probes, probe);
290 devc->analog_groups[i].name = channel_name;
291 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
292 sdi->probe_groups = g_slist_append(sdi->probe_groups,
293 &devc->analog_groups[i]);
296 if (devc->has_digital) {
297 for (i = 0; i < 16; i++) {
298 if (!(channel_name = g_strdup_printf("D%d", i)))
299 return SR_ERR_MALLOC;
300 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
301 g_free(channel_name);
303 return SR_ERR_MALLOC;
304 sdi->probes = g_slist_append(sdi->probes, probe);
305 devc->digital_group.probes = g_slist_append(
306 devc->digital_group.probes, probe);
307 devc->digital_group.name = "LA";
308 sdi->probe_groups = g_slist_append(sdi->probe_groups,
309 &devc->digital_group);
314 *devices = g_slist_append(NULL, sdi);
319 static GSList *scan(GSList *options)
321 struct drv_context *drvc;
322 struct sr_config *src;
326 const gchar *dev_name;
331 for (l = options; l; l = l->next) {
333 if (src->key == SR_CONF_CONN) {
334 port = (char *)g_variant_get_string(src->data, NULL);
341 if (probe_port(port, &devices) == SR_ERR_MALLOC)
344 if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
345 if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
347 while ((dev_name = g_dir_read_name(dir))) {
348 if (strncmp(dev_name, "usbtmc", 6))
350 port = g_strconcat("/dev/", dev_name, NULL);
351 ret = probe_port(port, &devices);
353 if (ret == SR_ERR_MALLOC) {
361 /* Tack a copy of the newly found devices onto the driver list. */
362 l = g_slist_copy(devices);
363 drvc->instances = g_slist_concat(drvc->instances, l);
368 static GSList *dev_list(void)
370 return ((struct drv_context *)(di->priv))->instances;
373 static int dev_open(struct sr_dev_inst *sdi)
376 if (serial_open(sdi->conn, SERIAL_RDWR) != SR_OK)
379 if (rigol_ds1xx2_get_dev_cfg(sdi) != SR_OK)
382 sdi->status = SR_ST_ACTIVE;
387 static int dev_close(struct sr_dev_inst *sdi)
389 struct sr_serial_dev_inst *serial;
392 if (serial && serial->fd != -1) {
393 serial_close(serial);
394 sdi->status = SR_ST_INACTIVE;
400 static int cleanup(void)
405 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
406 const struct sr_probe_group *probe_group)
413 case SR_CONF_NUM_TIMEBASE:
414 *data = g_variant_new_int32(NUM_TIMEBASE);
416 case SR_CONF_NUM_VDIV:
417 *data = g_variant_new_int32(NUM_VDIV);
426 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
427 const struct sr_probe_group *probe_group)
429 struct dev_context *devc;
430 uint64_t tmp_u64, p, q;
440 if (sdi->status != SR_ST_ACTIVE)
441 return SR_ERR_DEV_CLOSED;
445 case SR_CONF_LIMIT_FRAMES:
446 devc->limit_frames = g_variant_get_uint64(data);
448 case SR_CONF_TRIGGER_SLOPE:
449 tmp_u64 = g_variant_get_uint64(data);
450 if (tmp_u64 != 0 && tmp_u64 != 1)
452 g_free(devc->trigger_slope);
453 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
454 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
456 case SR_CONF_HORIZ_TRIGGERPOS:
457 t_dbl = g_variant_get_double(data);
458 if (t_dbl < 0.0 || t_dbl > 1.0)
460 devc->horiz_triggerpos = t_dbl;
461 /* We have the trigger offset as a percentage of the frame, but
462 * need to express this in seconds. */
463 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * NUM_TIMEBASE;
464 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
466 case SR_CONF_TIMEBASE:
467 g_variant_get(data, "(tt)", &p, &q);
468 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
469 if (timebases[i][0] == p && timebases[i][1] == q) {
470 devc->timebase = (float)p / q;
471 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
475 if (i == ARRAY_SIZE(timebases))
478 case SR_CONF_TRIGGER_SOURCE:
479 tmp_str = g_variant_get_string(data, NULL);
480 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
481 if (!strcmp(trigger_sources[i], tmp_str)) {
482 g_free(devc->trigger_source);
483 devc->trigger_source = g_strdup(trigger_sources[i]);
484 if (!strcmp(devc->trigger_source, "AC Line"))
486 else if (!strcmp(devc->trigger_source, "CH1"))
488 else if (!strcmp(devc->trigger_source, "CH2"))
491 tmp_str = (char *)devc->trigger_source;
492 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
496 if (i == ARRAY_SIZE(trigger_sources))
500 g_variant_get(data, "(tt)", &p, &q);
501 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
502 if (vdivs[i][0] != p || vdivs[i][1] != q)
504 devc->vdiv[0] = devc->vdiv[1] = (float)p / q;
505 set_cfg(sdi, ":CHAN1:SCAL %.3f", devc->vdiv[0]);
506 ret = set_cfg(sdi, ":CHAN2:SCAL %.3f", devc->vdiv[1]);
509 if (i == ARRAY_SIZE(vdivs))
512 case SR_CONF_COUPLING:
513 /* TODO: Not supporting coupling per channel yet. */
514 tmp_str = g_variant_get_string(data, NULL);
515 for (i = 0; i < ARRAY_SIZE(coupling); i++) {
516 if (!strcmp(tmp_str, coupling[i])) {
517 g_free(devc->coupling[0]);
518 g_free(devc->coupling[1]);
519 devc->coupling[0] = g_strdup(coupling[i]);
520 devc->coupling[1] = g_strdup(coupling[i]);
521 set_cfg(sdi, ":CHAN1:COUP %s", devc->coupling[0]);
522 ret = set_cfg(sdi, ":CHAN2:COUP %s", devc->coupling[1]);
526 if (i == ARRAY_SIZE(coupling))
537 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
538 const struct sr_probe_group *probe_group)
540 GVariant *tuple, *rational[2];
543 struct dev_context *devc;
548 case SR_CONF_SCAN_OPTIONS:
549 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
550 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
552 case SR_CONF_DEVICE_OPTIONS:
553 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
554 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
556 case SR_CONF_COUPLING:
557 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
560 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
561 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
562 rational[0] = g_variant_new_uint64(vdivs[i][0]);
563 rational[1] = g_variant_new_uint64(vdivs[i][1]);
564 tuple = g_variant_new_tuple(rational, 2);
565 g_variant_builder_add_value(&gvb, tuple);
567 *data = g_variant_builder_end(&gvb);
569 case SR_CONF_TIMEBASE:
570 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
571 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
572 rational[0] = g_variant_new_uint64(timebases[i][0]);
573 rational[1] = g_variant_new_uint64(timebases[i][1]);
574 tuple = g_variant_new_tuple(rational, 2);
575 g_variant_builder_add_value(&gvb, tuple);
577 *data = g_variant_builder_end(&gvb);
579 case SR_CONF_TRIGGER_SOURCE:
580 if (!sdi || !sdi->priv)
581 /* Can't know this until we have the exact model. */
584 *data = g_variant_new_strv(trigger_sources,
585 devc->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
594 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
596 struct sr_serial_dev_inst *serial;
597 struct dev_context *devc;
598 struct sr_probe *probe;
602 if (sdi->status != SR_ST_ACTIVE)
603 return SR_ERR_DEV_CLOSED;
608 for (l = sdi->probes; l; l = l->next) {
610 sr_dbg("handling probe %s", probe->name);
611 if (probe->type == SR_PROBE_ANALOG) {
613 devc->enabled_analog_probes = g_slist_append(
614 devc->enabled_analog_probes, probe);
615 if (probe->enabled != devc->analog_channels[probe->index]) {
616 /* Enabled channel is currently disabled, or vice versa. */
617 sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
618 probe->enabled ? "ON" : "OFF");
619 if (rigol_ds1xx2_send(sdi, cmd) != SR_OK)
622 } else if (probe->type == SR_PROBE_LOGIC) {
624 devc->enabled_digital_probes = g_slist_append(
625 devc->enabled_digital_probes, probe);
626 if (probe->enabled != devc->digital_channels[probe->index]) {
627 /* Enabled channel is currently disabled, or vice versa. */
628 sprintf(cmd, ":DIG%d:TURN %s", probe->index,
629 probe->enabled ? "ON" : "OFF");
630 if (rigol_ds1xx2_send(sdi, cmd) != SR_OK)
635 if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
638 sr_source_add(serial->fd, G_IO_IN, 50, rigol_ds1xx2_receive, (void *)sdi);
640 /* Send header packet to the session bus. */
641 std_session_send_df_header(cb_data, LOG_PREFIX);
643 /* Fetch the first frame. */
644 if (devc->enabled_analog_probes) {
645 devc->channel_frame = devc->enabled_analog_probes->data;
646 if (rigol_ds1xx2_send(sdi, ":WAV:DATA? CHAN%d",
647 devc->channel_frame->index + 1) != SR_OK)
650 devc->channel_frame = devc->enabled_digital_probes->data;
651 if (rigol_ds1xx2_send(sdi, ":WAV:DATA? DIG") != SR_OK)
655 devc->num_frame_bytes = 0;
660 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
662 struct dev_context *devc;
663 struct sr_serial_dev_inst *serial;
669 if (sdi->status != SR_ST_ACTIVE) {
670 sr_err("Device inactive, can't stop acquisition.");
674 g_slist_free(devc->enabled_analog_probes);
675 g_slist_free(devc->enabled_digital_probes);
676 devc->enabled_analog_probes = NULL;
677 devc->enabled_digital_probes = NULL;
679 sr_source_remove(serial->fd);
684 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
685 .name = "rigol-ds1xx2",
686 .longname = "Rigol DS1xx2",
691 .dev_list = dev_list,
692 .dev_clear = dev_clear,
693 .config_get = config_get,
694 .config_set = config_set,
695 .config_list = config_list,
696 .dev_open = dev_open,
697 .dev_close = dev_close,
698 .dev_acquisition_start = dev_acquisition_start,
699 .dev_acquisition_stop = dev_acquisition_stop,