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))
157 if (!(devc = sdi->priv))
160 g_free(devc->device);
161 g_free(devc->coupling[0]);
162 g_free(devc->coupling[1]);
163 g_free(devc->trigger_source);
164 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, ...)
178 struct dev_context *devc;
184 va_start(args, format);
185 vsnprintf(buf, 255, format, args);
187 if (rigol_ds1xx2_send(devc, 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 hw_init(struct sr_context *sr_ctx)
200 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
203 static int probe_device(struct drv_context *drvc, const char *device)
205 const gchar *idn_query = "*IDN?";
207 int len, num_tokens, fd;
208 const gchar *delimiter = ",";
210 const char *manufacturer, *model, *version;
211 gboolean matched = FALSE;
212 struct sr_dev_inst *sdi;
213 struct dev_context *devc;
214 gboolean has_digital = FALSE;
217 struct sr_probe *probe;
219 fd = open(device, O_RDWR);
220 len = write(fd, idn_query, strlen(idn_query));
221 len = read(fd, buf, sizeof(buf));
227 tokens = g_strsplit(buf, delimiter, 0);
229 sr_dbg("response: %s %d [%s]", device, len, buf);
231 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
233 if (num_tokens < 4) {
238 manufacturer = tokens[0];
242 if (strcmp(manufacturer, "Rigol Technologies")) {
247 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
248 if (!strcmp(model, supported_models[i])) {
250 has_digital = g_str_has_suffix(model, "D");
255 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
256 manufacturer, model, version))) {
263 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
264 return SR_ERR_MALLOC;
266 devc->limit_frames = 0;
267 if (!(devc->device = g_strdup(device)))
268 return SR_ERR_MALLOC;
269 devc->has_digital = has_digital;
272 drvc->instances = g_slist_append(drvc->instances, sdi);
274 for (i = 0; i < 2; i++) {
275 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
276 i == 0 ? "CH1" : "CH2")))
277 return SR_ERR_MALLOC;
278 sdi->probes = g_slist_append(sdi->probes, probe);
281 if (devc->has_digital) {
282 for (i = 0; i < 16; i++) {
283 if (!(channel_name = g_strdup_printf("D%d", i)))
284 return SR_ERR_MALLOC;
285 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
286 g_free(channel_name);
288 return SR_ERR_MALLOC;
289 sdi->probes = g_slist_append(sdi->probes, probe);
296 static GSList *hw_scan(GSList *options)
298 struct drv_context *drvc;
300 struct sr_config *src;
302 const gchar *dev_name;
303 const gchar *dev_dir = "/dev/";
304 const gchar *prefix = "usbtmc";
305 gchar *device = NULL;
309 drvc->instances = NULL;
311 for (l = options; l; l = l->next) {
313 if (src->key == SR_CONF_CONN) {
314 device = g_variant_get_string(src->data, NULL);
320 ret = probe_device(drvc, device);
321 if (ret == SR_ERR_MALLOC) {
326 if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
328 while ((dev_name = g_dir_read_name(dir))) {
329 if (strncmp(dev_name, prefix, strlen(prefix)))
331 device = g_strconcat(dev_dir, dev_name, NULL);
332 ret = probe_device(drvc, device);
334 if (ret == SR_ERR_MALLOC) {
344 devices = g_slist_copy(drvc->instances);
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)
355 struct dev_context *devc;
360 if ((fd = open(devc->device, O_RDWR)) == -1)
364 if (rigol_ds1xx2_get_dev_cfg(sdi) != SR_OK)
365 /* TODO: force configuration? */
371 static int hw_dev_close(struct sr_dev_inst *sdi)
373 struct dev_context *devc;
382 static int hw_cleanup(void)
389 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
395 case SR_CONF_NUM_TIMEBASE:
396 *data = g_variant_new_int32(NUM_TIMEBASE);
398 case SR_CONF_NUM_VDIV:
399 *data = g_variant_new_int32(NUM_VDIV);
408 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
410 struct dev_context *devc;
411 uint64_t tmp_u64, p, q;
419 if (sdi->status != SR_ST_ACTIVE) {
420 sr_err("Device inactive, can't set config options.");
426 case SR_CONF_LIMIT_FRAMES:
427 devc->limit_frames = g_variant_get_uint64(data);
429 case SR_CONF_TRIGGER_SLOPE:
430 tmp_u64 = g_variant_get_uint64(data);
431 if (tmp_u64 != 0 && tmp_u64 != 1)
433 g_free(devc->trigger_slope);
434 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
435 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
437 case SR_CONF_HORIZ_TRIGGERPOS:
438 t_dbl = g_variant_get_double(data);
439 if (t_dbl < 0.0 || t_dbl > 1.0)
441 devc->horiz_triggerpos = t_dbl;
442 /* We have the trigger offset as a percentage of the frame, but
443 * need to express this in seconds. */
444 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * NUM_TIMEBASE;
445 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
447 case SR_CONF_TIMEBASE:
448 g_variant_get(data, "(tt)", &p, &q);
449 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
450 if (timebases[i][0] == p && timebases[i][1] == q) {
451 devc->timebase = (float)p / q;
452 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
456 if (i == ARRAY_SIZE(timebases))
459 case SR_CONF_TRIGGER_SOURCE:
460 tmp_str = g_variant_get_string(data, NULL);
461 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
462 if (!strcmp(trigger_sources[i], tmp_str)) {
463 g_free(devc->trigger_source);
464 devc->trigger_source = g_strdup(trigger_sources[i]);
465 if (!strcmp(devc->trigger_source, "AC Line"))
467 else if (!strcmp(devc->trigger_source, "CH1"))
469 else if (!strcmp(devc->trigger_source, "CH2"))
472 tmp_str = (char *)devc->trigger_source;
473 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
477 if (i == ARRAY_SIZE(trigger_sources))
481 g_variant_get(data, "(tt)", &p, &q);
482 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
483 if (vdivs[i][0] != p || vdivs[i][1] != q)
485 devc->vdiv[0] = devc->vdiv[1] = (float)p / q;
486 set_cfg(sdi, ":CHAN1:SCAL %.3f", devc->vdiv[0]);
487 ret = set_cfg(sdi, ":CHAN2:SCAL %.3f", devc->vdiv[1]);
490 if (i == ARRAY_SIZE(vdivs))
493 case SR_CONF_COUPLING:
494 /* TODO: Not supporting coupling per channel yet. */
495 tmp_str = g_variant_get_string(data, NULL);
496 for (i = 0; i < ARRAY_SIZE(coupling); i++) {
497 if (!strcmp(tmp_str, coupling[i])) {
498 g_free(devc->coupling[0]);
499 g_free(devc->coupling[1]);
500 devc->coupling[0] = g_strdup(coupling[i]);
501 devc->coupling[1] = g_strdup(coupling[i]);
502 set_cfg(sdi, ":CHAN1:COUP %s", devc->coupling[0]);
503 ret = set_cfg(sdi, ":CHAN2:COUP %s", devc->coupling[1]);
507 if (i == ARRAY_SIZE(coupling))
518 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
520 GVariant *tuple, *rational[2];
523 struct dev_context *devc;
526 case SR_CONF_SCAN_OPTIONS:
527 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
528 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
530 case SR_CONF_DEVICE_OPTIONS:
531 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
532 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
534 case SR_CONF_COUPLING:
535 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
538 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
539 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
540 rational[0] = g_variant_new_uint64(vdivs[i][0]);
541 rational[1] = g_variant_new_uint64(vdivs[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_TIMEBASE:
548 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
549 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
550 rational[0] = g_variant_new_uint64(timebases[i][0]);
551 rational[1] = g_variant_new_uint64(timebases[i][1]);
552 tuple = g_variant_new_tuple(rational, 2);
553 g_variant_builder_add_value(&gvb, tuple);
555 *data = g_variant_builder_end(&gvb);
557 case SR_CONF_TRIGGER_SOURCE:
558 if (!sdi || !sdi->priv)
559 /* Can't know this until we have the exact model. */
562 *data = g_variant_new_strv(trigger_sources,
563 devc->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
572 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
574 struct dev_context *devc;
575 struct sr_probe *probe;
583 for (l = sdi->probes; l; l = l->next) {
585 sr_dbg("handling probe %s", probe->name);
586 if (probe->type == SR_PROBE_ANALOG) {
588 devc->enabled_analog_probes = g_slist_append(
589 devc->enabled_analog_probes, probe);
590 if (probe->enabled != devc->analog_channels[probe->index]) {
591 /* Enabled channel is currently disabled, or vice versa. */
592 sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
593 probe->enabled ? "ON" : "OFF");
594 if (rigol_ds1xx2_send(devc, cmd) != SR_OK)
597 } else if (probe->type == SR_PROBE_LOGIC) {
599 devc->enabled_digital_probes = g_slist_append(
600 devc->enabled_digital_probes, probe);
601 if (probe->enabled != devc->digital_channels[probe->index]) {
602 /* Enabled channel is currently disabled, or vice versa. */
603 sprintf(cmd, ":DIG%d:TURN %s", probe->index,
604 probe->enabled ? "ON" : "OFF");
605 if (rigol_ds1xx2_send(devc, cmd) != SR_OK)
610 if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
613 sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive, (void *)sdi);
615 /* Send header packet to the session bus. */
616 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
618 /* Fetch the first frame. */
619 if (devc->enabled_analog_probes) {
620 devc->channel_frame = devc->enabled_analog_probes->data;
621 if (rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%d",
622 devc->channel_frame->index + 1) != SR_OK)
625 devc->channel_frame = devc->enabled_digital_probes->data;
626 if (rigol_ds1xx2_send(devc, ":WAV:DATA? DIG") != SR_OK)
630 devc->num_frame_bytes = 0;
635 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
637 struct dev_context *devc;
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;
652 sr_source_remove(devc->fd);
657 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
658 .name = "rigol-ds1xx2",
659 .longname = "Rigol DS1xx2",
662 .cleanup = hw_cleanup,
664 .dev_list = hw_dev_list,
665 .dev_clear = clear_instances,
666 .config_get = config_get,
667 .config_set = config_set,
668 .config_list = config_list,
669 .dev_open = hw_dev_open,
670 .dev_close = hw_dev_close,
671 .dev_acquisition_start = dev_acquisition_start,
672 .dev_acquisition_stop = dev_acquisition_stop,