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[] = {
140 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info;
141 static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info;
143 /* Properly close and free all devices. */
144 static int clear_instances(void)
146 struct sr_dev_inst *sdi;
147 struct drv_context *drvc;
148 struct dev_context *devc;
151 if (!(drvc = di->priv))
154 for (l = drvc->instances; l; l = l->next) {
155 if (!(sdi = l->data))
159 sr_serial_dev_inst_free(sdi->conn);
161 if ((devc = sdi->priv)) {
162 g_free(devc->coupling[0]);
163 g_free(devc->coupling[1]);
164 g_free(devc->trigger_source);
165 g_free(devc->trigger_slope);
167 sr_dev_inst_free(sdi);
170 g_slist_free(drvc->instances);
171 drvc->instances = NULL;
176 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
181 va_start(args, format);
182 vsnprintf(buf, 255, format, args);
184 if (rigol_ds1xx2_send(sdi, buf) != SR_OK)
187 /* When setting a bunch of parameters in a row, the DS1052E scrambles
188 * some of them unless there is at least 100ms delay in between. */
189 sr_spew("delay %dms", 100);
195 static int hw_init(struct sr_context *sr_ctx)
197 return std_hw_init(sr_ctx, di, LOG_PREFIX);
200 static int probe_port(const char *port, GSList **devices)
202 struct dev_context *devc;
203 struct sr_dev_inst *sdi;
204 struct sr_serial_dev_inst *serial;
205 struct sr_probe *probe;
208 gboolean matched, has_digital;
209 const char *manufacturer, *model, *version;
211 gchar **tokens, *channel_name;
214 if (!(serial = sr_serial_dev_inst_new(port, NULL)))
215 return SR_ERR_MALLOC;
217 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
219 len = serial_write(serial, "*IDN?", 5);
220 len = serial_read(serial, buf, sizeof(buf));
221 if (serial_close(serial) != SR_OK)
224 sr_serial_dev_inst_free(serial);
230 tokens = g_strsplit(buf, ",", 0);
231 sr_dbg("response: %s [%s]", port, buf);
233 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
235 if (num_tokens < 4) {
240 manufacturer = tokens[0];
244 if (strcmp(manufacturer, "Rigol Technologies")) {
249 matched = has_digital = FALSE;
250 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
251 if (!strcmp(model, supported_models[i])) {
253 has_digital = g_str_has_suffix(model, "D");
258 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
259 manufacturer, model, version))) {
266 if (!(sdi->conn = sr_serial_dev_inst_new(port, NULL)))
267 return SR_ERR_MALLOC;
270 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
271 return SR_ERR_MALLOC;
272 devc->limit_frames = 0;
273 devc->has_digital = has_digital;
275 for (i = 0; i < 2; i++) {
276 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
277 i == 0 ? "CH1" : "CH2")))
278 return SR_ERR_MALLOC;
279 sdi->probes = g_slist_append(sdi->probes, probe);
282 if (devc->has_digital) {
283 for (i = 0; i < 16; i++) {
284 if (!(channel_name = g_strdup_printf("D%d", i)))
285 return SR_ERR_MALLOC;
286 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
287 g_free(channel_name);
289 return SR_ERR_MALLOC;
290 sdi->probes = g_slist_append(sdi->probes, probe);
295 *devices = g_slist_append(NULL, sdi);
300 static GSList *hw_scan(GSList *options)
302 struct drv_context *drvc;
303 struct sr_config *src;
307 const gchar *dev_name;
312 for (l = options; l; l = l->next) {
314 if (src->key == SR_CONF_CONN) {
315 port = (char *)g_variant_get_string(src->data, NULL);
322 if (probe_port(port, &devices) == SR_ERR_MALLOC)
325 if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
327 while ((dev_name = g_dir_read_name(dir))) {
328 if (strncmp(dev_name, "usbtmc", 6))
330 port = g_strconcat("/dev/", dev_name, NULL);
331 ret = probe_port(port, &devices);
333 if (ret == SR_ERR_MALLOC) {
341 /* Tack a copy of the newly found devices onto the driver list. */
342 l = g_slist_copy(devices);
343 drvc->instances = g_slist_concat(drvc->instances, l);
348 static GSList *hw_dev_list(void)
350 return ((struct drv_context *)(di->priv))->instances;
353 static int hw_dev_open(struct sr_dev_inst *sdi)
356 if (serial_open(sdi->conn, SERIAL_RDWR) != SR_OK)
359 if (rigol_ds1xx2_get_dev_cfg(sdi) != SR_OK)
362 sdi->status = SR_ST_ACTIVE;
367 static int hw_dev_close(struct sr_dev_inst *sdi)
369 struct sr_serial_dev_inst *serial;
372 if (serial && serial->fd != -1) {
373 serial_close(serial);
374 sdi->status = SR_ST_INACTIVE;
380 static int hw_cleanup(void)
387 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
393 case SR_CONF_NUM_TIMEBASE:
394 *data = g_variant_new_int32(NUM_TIMEBASE);
396 case SR_CONF_NUM_VDIV:
397 *data = g_variant_new_int32(NUM_VDIV);
406 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
408 struct dev_context *devc;
409 uint64_t tmp_u64, p, q;
417 if (sdi->status != SR_ST_ACTIVE)
418 return SR_ERR_DEV_CLOSED;
422 case SR_CONF_LIMIT_FRAMES:
423 devc->limit_frames = g_variant_get_uint64(data);
425 case SR_CONF_TRIGGER_SLOPE:
426 tmp_u64 = g_variant_get_uint64(data);
427 if (tmp_u64 != 0 && tmp_u64 != 1)
429 g_free(devc->trigger_slope);
430 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
431 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
433 case SR_CONF_HORIZ_TRIGGERPOS:
434 t_dbl = g_variant_get_double(data);
435 if (t_dbl < 0.0 || t_dbl > 1.0)
437 devc->horiz_triggerpos = t_dbl;
438 /* We have the trigger offset as a percentage of the frame, but
439 * need to express this in seconds. */
440 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * NUM_TIMEBASE;
441 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
443 case SR_CONF_TIMEBASE:
444 g_variant_get(data, "(tt)", &p, &q);
445 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
446 if (timebases[i][0] == p && timebases[i][1] == q) {
447 devc->timebase = (float)p / q;
448 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
452 if (i == ARRAY_SIZE(timebases))
455 case SR_CONF_TRIGGER_SOURCE:
456 tmp_str = g_variant_get_string(data, NULL);
457 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
458 if (!strcmp(trigger_sources[i], tmp_str)) {
459 g_free(devc->trigger_source);
460 devc->trigger_source = g_strdup(trigger_sources[i]);
461 if (!strcmp(devc->trigger_source, "AC Line"))
463 else if (!strcmp(devc->trigger_source, "CH1"))
465 else if (!strcmp(devc->trigger_source, "CH2"))
468 tmp_str = (char *)devc->trigger_source;
469 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
473 if (i == ARRAY_SIZE(trigger_sources))
477 g_variant_get(data, "(tt)", &p, &q);
478 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
479 if (vdivs[i][0] != p || vdivs[i][1] != q)
481 devc->vdiv[0] = devc->vdiv[1] = (float)p / q;
482 set_cfg(sdi, ":CHAN1:SCAL %.3f", devc->vdiv[0]);
483 ret = set_cfg(sdi, ":CHAN2:SCAL %.3f", devc->vdiv[1]);
486 if (i == ARRAY_SIZE(vdivs))
489 case SR_CONF_COUPLING:
490 /* TODO: Not supporting coupling per channel yet. */
491 tmp_str = g_variant_get_string(data, NULL);
492 for (i = 0; i < ARRAY_SIZE(coupling); i++) {
493 if (!strcmp(tmp_str, coupling[i])) {
494 g_free(devc->coupling[0]);
495 g_free(devc->coupling[1]);
496 devc->coupling[0] = g_strdup(coupling[i]);
497 devc->coupling[1] = g_strdup(coupling[i]);
498 set_cfg(sdi, ":CHAN1:COUP %s", devc->coupling[0]);
499 ret = set_cfg(sdi, ":CHAN2:COUP %s", devc->coupling[1]);
503 if (i == ARRAY_SIZE(coupling))
514 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
516 GVariant *tuple, *rational[2];
519 struct dev_context *devc;
522 case SR_CONF_SCAN_OPTIONS:
523 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
524 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
526 case SR_CONF_DEVICE_OPTIONS:
527 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
528 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
530 case SR_CONF_COUPLING:
531 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
534 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
535 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
536 rational[0] = g_variant_new_uint64(vdivs[i][0]);
537 rational[1] = g_variant_new_uint64(vdivs[i][1]);
538 tuple = g_variant_new_tuple(rational, 2);
539 g_variant_builder_add_value(&gvb, tuple);
541 *data = g_variant_builder_end(&gvb);
543 case SR_CONF_TIMEBASE:
544 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
545 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
546 rational[0] = g_variant_new_uint64(timebases[i][0]);
547 rational[1] = g_variant_new_uint64(timebases[i][1]);
548 tuple = g_variant_new_tuple(rational, 2);
549 g_variant_builder_add_value(&gvb, tuple);
551 *data = g_variant_builder_end(&gvb);
553 case SR_CONF_TRIGGER_SOURCE:
554 if (!sdi || !sdi->priv)
555 /* Can't know this until we have the exact model. */
558 *data = g_variant_new_strv(trigger_sources,
559 devc->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
568 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
570 struct sr_serial_dev_inst *serial;
571 struct dev_context *devc;
572 struct sr_probe *probe;
576 if (sdi->status != SR_ST_ACTIVE)
577 return SR_ERR_DEV_CLOSED;
582 for (l = sdi->probes; l; l = l->next) {
584 sr_dbg("handling probe %s", probe->name);
585 if (probe->type == SR_PROBE_ANALOG) {
587 devc->enabled_analog_probes = g_slist_append(
588 devc->enabled_analog_probes, probe);
589 if (probe->enabled != devc->analog_channels[probe->index]) {
590 /* Enabled channel is currently disabled, or vice versa. */
591 sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
592 probe->enabled ? "ON" : "OFF");
593 if (rigol_ds1xx2_send(sdi, cmd) != SR_OK)
596 } else if (probe->type == SR_PROBE_LOGIC) {
598 devc->enabled_digital_probes = g_slist_append(
599 devc->enabled_digital_probes, probe);
600 if (probe->enabled != devc->digital_channels[probe->index]) {
601 /* Enabled channel is currently disabled, or vice versa. */
602 sprintf(cmd, ":DIG%d:TURN %s", probe->index,
603 probe->enabled ? "ON" : "OFF");
604 if (rigol_ds1xx2_send(sdi, cmd) != SR_OK)
609 if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
612 sr_source_add(serial->fd, G_IO_IN, 50, rigol_ds1xx2_receive, (void *)sdi);
614 /* Send header packet to the session bus. */
615 std_session_send_df_header(cb_data, LOG_PREFIX);
617 /* Fetch the first frame. */
618 if (devc->enabled_analog_probes) {
619 devc->channel_frame = devc->enabled_analog_probes->data;
620 if (rigol_ds1xx2_send(sdi, ":WAV:DATA? CHAN%d",
621 devc->channel_frame->index + 1) != SR_OK)
624 devc->channel_frame = devc->enabled_digital_probes->data;
625 if (rigol_ds1xx2_send(sdi, ":WAV:DATA? DIG") != SR_OK)
629 devc->num_frame_bytes = 0;
634 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
636 struct dev_context *devc;
637 struct sr_serial_dev_inst *serial;
643 if (sdi->status != SR_ST_ACTIVE) {
644 sr_err("Device inactive, can't stop acquisition.");
648 g_slist_free(devc->enabled_analog_probes);
649 g_slist_free(devc->enabled_digital_probes);
650 devc->enabled_analog_probes = NULL;
651 devc->enabled_digital_probes = NULL;
653 sr_source_remove(serial->fd);
658 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
659 .name = "rigol-ds1xx2",
660 .longname = "Rigol DS1xx2",
663 .cleanup = hw_cleanup,
665 .dev_list = hw_dev_list,
666 .dev_clear = clear_instances,
667 .config_get = config_get,
668 .config_set = config_set,
669 .config_list = config_list,
670 .dev_open = hw_dev_open,
671 .dev_close = hw_dev_close,
672 .dev_acquisition_start = dev_acquisition_start,
673 .dev_acquisition_stop = dev_acquisition_stop,