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;
151 g_free(devc->coupling[0]);
152 g_free(devc->coupling[1]);
153 g_free(devc->trigger_source);
154 g_free(devc->trigger_slope);
157 static int dev_clear(void)
159 return std_dev_clear(di, clear_helper);
162 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
167 va_start(args, format);
168 vsnprintf(buf, 255, format, args);
170 if (rigol_ds1xx2_send(sdi, buf) != SR_OK)
173 /* When setting a bunch of parameters in a row, the DS1052E scrambles
174 * some of them unless there is at least 100ms delay in between. */
175 sr_spew("delay %dms", 100);
181 static int init(struct sr_context *sr_ctx)
183 return std_init(sr_ctx, di, LOG_PREFIX);
186 static int probe_port(const char *port, GSList **devices)
188 struct dev_context *devc;
189 struct sr_dev_inst *sdi;
190 struct sr_serial_dev_inst *serial;
191 struct sr_probe *probe;
194 gboolean matched, has_digital;
195 const char *manufacturer, *model, *version;
197 gchar **tokens, *channel_name;
200 if (!(serial = sr_serial_dev_inst_new(port, NULL)))
201 return SR_ERR_MALLOC;
203 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
205 len = serial_write(serial, "*IDN?", 5);
206 len = serial_read(serial, buf, sizeof(buf));
207 if (serial_close(serial) != SR_OK)
210 sr_serial_dev_inst_free(serial);
216 tokens = g_strsplit(buf, ",", 0);
217 sr_dbg("response: %s [%s]", port, buf);
219 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
221 if (num_tokens < 4) {
226 manufacturer = tokens[0];
230 if (strcmp(manufacturer, "Rigol Technologies")) {
235 matched = has_digital = FALSE;
236 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
237 if (!strcmp(model, supported_models[i])) {
239 has_digital = g_str_has_suffix(model, "D");
244 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
245 manufacturer, model, version))) {
252 if (!(sdi->conn = sr_serial_dev_inst_new(port, NULL)))
253 return SR_ERR_MALLOC;
255 sdi->inst_type = SR_INST_SERIAL;
257 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
258 return SR_ERR_MALLOC;
259 devc->limit_frames = 0;
260 devc->has_digital = has_digital;
262 for (i = 0; i < 2; i++) {
263 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
264 i == 0 ? "CH1" : "CH2")))
265 return SR_ERR_MALLOC;
266 sdi->probes = g_slist_append(sdi->probes, probe);
269 if (devc->has_digital) {
270 for (i = 0; i < 16; i++) {
271 if (!(channel_name = g_strdup_printf("D%d", i)))
272 return SR_ERR_MALLOC;
273 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
274 g_free(channel_name);
276 return SR_ERR_MALLOC;
277 sdi->probes = g_slist_append(sdi->probes, probe);
282 *devices = g_slist_append(NULL, sdi);
287 static GSList *scan(GSList *options)
289 struct drv_context *drvc;
290 struct sr_config *src;
294 const gchar *dev_name;
299 for (l = options; l; l = l->next) {
301 if (src->key == SR_CONF_CONN) {
302 port = (char *)g_variant_get_string(src->data, NULL);
309 if (probe_port(port, &devices) == SR_ERR_MALLOC)
312 if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
313 if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
315 while ((dev_name = g_dir_read_name(dir))) {
316 if (strncmp(dev_name, "usbtmc", 6))
318 port = g_strconcat("/dev/", dev_name, NULL);
319 ret = probe_port(port, &devices);
321 if (ret == SR_ERR_MALLOC) {
329 /* Tack a copy of the newly found devices onto the driver list. */
330 l = g_slist_copy(devices);
331 drvc->instances = g_slist_concat(drvc->instances, l);
336 static GSList *dev_list(void)
338 return ((struct drv_context *)(di->priv))->instances;
341 static int dev_open(struct sr_dev_inst *sdi)
344 if (serial_open(sdi->conn, SERIAL_RDWR) != SR_OK)
347 if (rigol_ds1xx2_get_dev_cfg(sdi) != SR_OK)
350 sdi->status = SR_ST_ACTIVE;
355 static int dev_close(struct sr_dev_inst *sdi)
357 struct sr_serial_dev_inst *serial;
360 if (serial && serial->fd != -1) {
361 serial_close(serial);
362 sdi->status = SR_ST_INACTIVE;
368 static int cleanup(void)
373 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
374 const struct sr_probe_group *probe_group)
381 case SR_CONF_NUM_TIMEBASE:
382 *data = g_variant_new_int32(NUM_TIMEBASE);
384 case SR_CONF_NUM_VDIV:
385 *data = g_variant_new_int32(NUM_VDIV);
394 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
395 const struct sr_probe_group *probe_group)
397 struct dev_context *devc;
398 uint64_t tmp_u64, p, q;
408 if (sdi->status != SR_ST_ACTIVE)
409 return SR_ERR_DEV_CLOSED;
413 case SR_CONF_LIMIT_FRAMES:
414 devc->limit_frames = g_variant_get_uint64(data);
416 case SR_CONF_TRIGGER_SLOPE:
417 tmp_u64 = g_variant_get_uint64(data);
418 if (tmp_u64 != 0 && tmp_u64 != 1)
420 g_free(devc->trigger_slope);
421 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
422 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
424 case SR_CONF_HORIZ_TRIGGERPOS:
425 t_dbl = g_variant_get_double(data);
426 if (t_dbl < 0.0 || t_dbl > 1.0)
428 devc->horiz_triggerpos = t_dbl;
429 /* We have the trigger offset as a percentage of the frame, but
430 * need to express this in seconds. */
431 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * NUM_TIMEBASE;
432 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
434 case SR_CONF_TIMEBASE:
435 g_variant_get(data, "(tt)", &p, &q);
436 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
437 if (timebases[i][0] == p && timebases[i][1] == q) {
438 devc->timebase = (float)p / q;
439 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
443 if (i == ARRAY_SIZE(timebases))
446 case SR_CONF_TRIGGER_SOURCE:
447 tmp_str = g_variant_get_string(data, NULL);
448 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
449 if (!strcmp(trigger_sources[i], tmp_str)) {
450 g_free(devc->trigger_source);
451 devc->trigger_source = g_strdup(trigger_sources[i]);
452 if (!strcmp(devc->trigger_source, "AC Line"))
454 else if (!strcmp(devc->trigger_source, "CH1"))
456 else if (!strcmp(devc->trigger_source, "CH2"))
459 tmp_str = (char *)devc->trigger_source;
460 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
464 if (i == ARRAY_SIZE(trigger_sources))
468 g_variant_get(data, "(tt)", &p, &q);
469 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
470 if (vdivs[i][0] != p || vdivs[i][1] != q)
472 devc->vdiv[0] = devc->vdiv[1] = (float)p / q;
473 set_cfg(sdi, ":CHAN1:SCAL %.3f", devc->vdiv[0]);
474 ret = set_cfg(sdi, ":CHAN2:SCAL %.3f", devc->vdiv[1]);
477 if (i == ARRAY_SIZE(vdivs))
480 case SR_CONF_COUPLING:
481 /* TODO: Not supporting coupling per channel yet. */
482 tmp_str = g_variant_get_string(data, NULL);
483 for (i = 0; i < ARRAY_SIZE(coupling); i++) {
484 if (!strcmp(tmp_str, coupling[i])) {
485 g_free(devc->coupling[0]);
486 g_free(devc->coupling[1]);
487 devc->coupling[0] = g_strdup(coupling[i]);
488 devc->coupling[1] = g_strdup(coupling[i]);
489 set_cfg(sdi, ":CHAN1:COUP %s", devc->coupling[0]);
490 ret = set_cfg(sdi, ":CHAN2:COUP %s", devc->coupling[1]);
494 if (i == ARRAY_SIZE(coupling))
505 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
506 const struct sr_probe_group *probe_group)
508 GVariant *tuple, *rational[2];
511 struct dev_context *devc;
516 case SR_CONF_SCAN_OPTIONS:
517 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
518 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
520 case SR_CONF_DEVICE_OPTIONS:
521 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
522 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
524 case SR_CONF_COUPLING:
525 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
528 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
529 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
530 rational[0] = g_variant_new_uint64(vdivs[i][0]);
531 rational[1] = g_variant_new_uint64(vdivs[i][1]);
532 tuple = g_variant_new_tuple(rational, 2);
533 g_variant_builder_add_value(&gvb, tuple);
535 *data = g_variant_builder_end(&gvb);
537 case SR_CONF_TIMEBASE:
538 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
539 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
540 rational[0] = g_variant_new_uint64(timebases[i][0]);
541 rational[1] = g_variant_new_uint64(timebases[i][1]);
542 tuple = g_variant_new_tuple(rational, 2);
543 g_variant_builder_add_value(&gvb, tuple);
545 *data = g_variant_builder_end(&gvb);
547 case SR_CONF_TRIGGER_SOURCE:
548 if (!sdi || !sdi->priv)
549 /* Can't know this until we have the exact model. */
552 *data = g_variant_new_strv(trigger_sources,
553 devc->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
562 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
564 struct sr_serial_dev_inst *serial;
565 struct dev_context *devc;
566 struct sr_probe *probe;
570 if (sdi->status != SR_ST_ACTIVE)
571 return SR_ERR_DEV_CLOSED;
576 for (l = sdi->probes; l; l = l->next) {
578 sr_dbg("handling probe %s", probe->name);
579 if (probe->type == SR_PROBE_ANALOG) {
581 devc->enabled_analog_probes = g_slist_append(
582 devc->enabled_analog_probes, probe);
583 if (probe->enabled != devc->analog_channels[probe->index]) {
584 /* Enabled channel is currently disabled, or vice versa. */
585 sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
586 probe->enabled ? "ON" : "OFF");
587 if (rigol_ds1xx2_send(sdi, cmd) != SR_OK)
590 } else if (probe->type == SR_PROBE_LOGIC) {
592 devc->enabled_digital_probes = g_slist_append(
593 devc->enabled_digital_probes, probe);
594 if (probe->enabled != devc->digital_channels[probe->index]) {
595 /* Enabled channel is currently disabled, or vice versa. */
596 sprintf(cmd, ":DIG%d:TURN %s", probe->index,
597 probe->enabled ? "ON" : "OFF");
598 if (rigol_ds1xx2_send(sdi, cmd) != SR_OK)
603 if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
606 sr_source_add(serial->fd, G_IO_IN, 50, rigol_ds1xx2_receive, (void *)sdi);
608 /* Send header packet to the session bus. */
609 std_session_send_df_header(cb_data, LOG_PREFIX);
611 /* Fetch the first frame. */
612 if (devc->enabled_analog_probes) {
613 devc->channel_frame = devc->enabled_analog_probes->data;
614 if (rigol_ds1xx2_send(sdi, ":WAV:DATA? CHAN%d",
615 devc->channel_frame->index + 1) != SR_OK)
618 devc->channel_frame = devc->enabled_digital_probes->data;
619 if (rigol_ds1xx2_send(sdi, ":WAV:DATA? DIG") != SR_OK)
623 devc->num_frame_bytes = 0;
628 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
630 struct dev_context *devc;
631 struct sr_serial_dev_inst *serial;
637 if (sdi->status != SR_ST_ACTIVE) {
638 sr_err("Device inactive, can't stop acquisition.");
642 g_slist_free(devc->enabled_analog_probes);
643 g_slist_free(devc->enabled_digital_probes);
644 devc->enabled_analog_probes = NULL;
645 devc->enabled_digital_probes = NULL;
647 sr_source_remove(serial->fd);
652 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
653 .name = "rigol-ds1xx2",
654 .longname = "Rigol DS1xx2",
659 .dev_list = dev_list,
660 .dev_clear = dev_clear,
661 .config_get = config_get,
662 .config_set = config_set,
663 .config_list = config_list,
664 .dev_open = dev_open,
665 .dev_close = dev_close,
666 .dev_acquisition_start = dev_acquisition_start,
667 .dev_acquisition_stop = dev_acquisition_stop,