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 hwcaps[] = {
36 SR_CONF_TRIGGER_SOURCE,
37 SR_CONF_TRIGGER_SLOPE,
38 SR_CONF_HORIZ_TRIGGERPOS,
45 static const uint64_t timebases[][2] = {
83 static const uint64_t vdivs[][2] = {
100 static const char *trigger_sources[] = {
123 static const char *coupling[] = {
129 static const char *supported_models[] = {
136 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info;
137 static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info;
139 /* Properly close and free all devices. */
140 static int clear_instances(void)
142 struct sr_dev_inst *sdi;
143 struct drv_context *drvc;
144 struct dev_context *devc;
147 if (!(drvc = di->priv))
150 for (l = drvc->instances; l; l = l->next) {
151 if (!(sdi = l->data))
153 if (!(devc = sdi->priv))
156 g_free(devc->device);
157 g_free(devc->coupling[0]);
158 g_free(devc->coupling[1]);
159 g_free(devc->trigger_source);
160 g_free(devc->trigger_slope);
163 sr_dev_inst_free(sdi);
166 g_slist_free(drvc->instances);
167 drvc->instances = NULL;
172 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
174 struct dev_context *devc;
180 va_start(args, format);
181 vsnprintf(buf, 255, format, args);
183 if (rigol_ds1xx2_send(devc, buf) != SR_OK)
186 /* When setting a bunch of parameters in a row, the DS1052E scrambles
187 * some of them unless there is at least 100ms delay in between. */
188 sr_spew("delay %dms", 100);
194 static int hw_init(struct sr_context *sr_ctx)
196 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
199 static GSList *hw_scan(GSList *options)
201 struct drv_context *drvc;
202 struct sr_dev_inst *sdi;
203 struct dev_context *devc;
204 struct sr_probe *probe;
207 const gchar *dev_name;
208 const gchar *dev_dir = "/dev/";
209 const gchar *prefix = "usbtmc";
211 const gchar *idn_query = "*IDN?";
213 int len, num_tokens, fd;
214 const gchar *delimiter = ",";
216 const char *manufacturer, *model, *version;
217 gboolean matched = FALSE;
218 gboolean has_digital = FALSE;
225 drvc->instances = NULL;
229 dir = g_dir_open("/sys/class/usb/", 0, NULL);
234 while ((dev_name = g_dir_read_name(dir)) != NULL) {
235 if (strncmp(dev_name, prefix, strlen(prefix)))
238 device = g_strconcat(dev_dir, dev_name, NULL);
240 fd = open(device, O_RDWR);
241 len = write(fd, idn_query, strlen(idn_query));
242 len = read(fd, buf, sizeof(buf));
250 tokens = g_strsplit(buf, delimiter, 0);
252 sr_dbg("response: %s %d [%s]", device, len, buf);
254 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
256 if (num_tokens < 4) {
262 manufacturer = tokens[0];
266 if (strcmp(manufacturer, "Rigol Technologies")) {
272 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
273 if (!strcmp(model, supported_models[i])) {
275 has_digital = g_str_has_suffix(model, "D");
280 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
281 manufacturer, model, version))) {
289 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
290 sr_err("Device context malloc failed.");
294 devc->limit_frames = 0;
295 devc->device = device;
296 devc->has_digital = has_digital;
300 for (i = 0; i < 2; i++) {
301 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
302 i == 0 ? "CH1" : "CH2")))
304 sdi->probes = g_slist_append(sdi->probes, probe);
307 if (devc->has_digital) {
308 for (i = 0; i < 16; i++) {
309 if (!(channel_name = g_strdup_printf("D%d", i)))
311 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
312 g_free(channel_name);
315 sdi->probes = g_slist_append(sdi->probes, probe);
319 drvc->instances = g_slist_append(drvc->instances, sdi);
320 devices = g_slist_append(devices, sdi);
328 static GSList *hw_dev_list(void)
330 return ((struct drv_context *)(di->priv))->instances;
333 static int hw_dev_open(struct sr_dev_inst *sdi)
335 struct dev_context *devc;
340 if ((fd = open(devc->device, O_RDWR)) == -1)
344 if (rigol_ds1xx2_get_dev_cfg(sdi) != SR_OK)
345 /* TODO: force configuration? */
351 static int hw_dev_close(struct sr_dev_inst *sdi)
353 struct dev_context *devc;
362 static int hw_cleanup(void)
369 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
375 case SR_CONF_NUM_TIMEBASE:
376 *data = g_variant_new_int32(NUM_TIMEBASE);
378 case SR_CONF_NUM_VDIV:
379 *data = g_variant_new_int32(NUM_VDIV);
388 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
390 struct dev_context *devc;
391 uint64_t tmp_u64, p, q;
399 if (sdi->status != SR_ST_ACTIVE) {
400 sr_err("Device inactive, can't set config options.");
406 case SR_CONF_LIMIT_FRAMES:
407 devc->limit_frames = g_variant_get_uint64(data);
409 case SR_CONF_TRIGGER_SLOPE:
410 tmp_u64 = g_variant_get_uint64(data);
411 if (tmp_u64 != 0 && tmp_u64 != 1)
413 g_free(devc->trigger_slope);
414 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
415 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
417 case SR_CONF_HORIZ_TRIGGERPOS:
418 t_dbl = g_variant_get_double(data);
419 if (t_dbl < 0.0 || t_dbl > 1.0)
421 devc->horiz_triggerpos = t_dbl;
422 /* We have the trigger offset as a percentage of the frame, but
423 * need to express this in seconds. */
424 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * NUM_TIMEBASE;
425 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
427 case SR_CONF_TIMEBASE:
428 g_variant_get(data, "(tt)", &p, &q);
429 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
430 if (timebases[i][0] == p && timebases[i][1] == q) {
431 devc->timebase = (float)p / q;
432 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
436 if (i == ARRAY_SIZE(timebases))
439 case SR_CONF_TRIGGER_SOURCE:
440 tmp_str = g_variant_get_string(data, NULL);
441 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
442 if (!strcmp(trigger_sources[i], tmp_str)) {
443 g_free(devc->trigger_source);
444 devc->trigger_source = g_strdup(trigger_sources[i]);
445 if (!strcmp(devc->trigger_source, "AC Line"))
447 else if (!strcmp(devc->trigger_source, "CH1"))
449 else if (!strcmp(devc->trigger_source, "CH2"))
452 tmp_str = (char *)devc->trigger_source;
453 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
457 if (i == ARRAY_SIZE(trigger_sources))
461 g_variant_get(data, "(tt)", &p, &q);
462 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
463 if (vdivs[i][0] != p || vdivs[i][1] != q)
465 devc->vdiv[0] = devc->vdiv[1] = (float)p / q;
466 set_cfg(sdi, ":CHAN1:SCAL %.3f", devc->vdiv[0]);
467 ret = set_cfg(sdi, ":CHAN2:SCAL %.3f", devc->vdiv[1]);
470 if (i == ARRAY_SIZE(vdivs))
473 case SR_CONF_COUPLING:
474 /* TODO: Not supporting coupling per channel yet. */
475 tmp_str = g_variant_get_string(data, NULL);
476 for (i = 0; i < ARRAY_SIZE(coupling); i++) {
477 if (!strcmp(tmp_str, coupling[i])) {
478 g_free(devc->coupling[0]);
479 g_free(devc->coupling[1]);
480 devc->coupling[0] = g_strdup(coupling[i]);
481 devc->coupling[1] = g_strdup(coupling[i]);
482 set_cfg(sdi, ":CHAN1:COUP %s", devc->coupling[0]);
483 ret = set_cfg(sdi, ":CHAN2:COUP %s", devc->coupling[1]);
487 if (i == ARRAY_SIZE(coupling))
491 sr_err("Unknown hardware capability: %d.", id);
499 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
501 GVariant *tuple, *rational[2];
504 struct dev_context *devc;
507 case SR_CONF_DEVICE_OPTIONS:
508 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
509 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
511 case SR_CONF_COUPLING:
512 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
515 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
516 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
517 rational[0] = g_variant_new_uint64(vdivs[i][0]);
518 rational[1] = g_variant_new_uint64(vdivs[i][1]);
519 tuple = g_variant_new_tuple(rational, 2);
520 g_variant_builder_add_value(&gvb, tuple);
522 *data = g_variant_builder_end(&gvb);
524 case SR_CONF_TIMEBASE:
525 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
526 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
527 rational[0] = g_variant_new_uint64(timebases[i][0]);
528 rational[1] = g_variant_new_uint64(timebases[i][1]);
529 tuple = g_variant_new_tuple(rational, 2);
530 g_variant_builder_add_value(&gvb, tuple);
532 *data = g_variant_builder_end(&gvb);
534 case SR_CONF_TRIGGER_SOURCE:
535 if (!sdi || !sdi->priv)
536 /* Can't know this until we have the exact model. */
539 *data = g_variant_new_strv(trigger_sources,
540 devc->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
549 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
551 struct dev_context *devc;
552 struct sr_probe *probe;
560 for (l = sdi->probes; l; l = l->next) {
562 sr_dbg("handling probe %s", probe->name);
563 if (probe->type == SR_PROBE_ANALOG) {
565 devc->enabled_analog_probes = g_slist_append(
566 devc->enabled_analog_probes, probe);
567 if (probe->enabled != devc->analog_channels[probe->index]) {
568 /* Enabled channel is currently disabled, or vice versa. */
569 sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
570 probe->enabled ? "ON" : "OFF");
571 if (rigol_ds1xx2_send(devc, cmd) != SR_OK)
574 } else if (probe->type == SR_PROBE_LOGIC) {
576 devc->enabled_digital_probes = g_slist_append(
577 devc->enabled_digital_probes, probe);
578 if (probe->enabled != devc->digital_channels[probe->index]) {
579 /* Enabled channel is currently disabled, or vice versa. */
580 sprintf(cmd, ":DIG%d:TURN %s", probe->index,
581 probe->enabled ? "ON" : "OFF");
582 if (rigol_ds1xx2_send(devc, cmd) != SR_OK)
587 if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
590 sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive, (void *)sdi);
592 /* Send header packet to the session bus. */
593 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
595 /* Fetch the first frame. */
596 if (devc->enabled_analog_probes) {
597 devc->channel_frame = devc->enabled_analog_probes->data;
598 if (rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%d",
599 devc->channel_frame->index + 1) != SR_OK)
602 devc->channel_frame = devc->enabled_digital_probes->data;
603 if (rigol_ds1xx2_send(devc, ":WAV:DATA? DIG") != SR_OK)
607 devc->num_frame_bytes = 0;
612 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
614 struct dev_context *devc;
620 if (sdi->status != SR_ST_ACTIVE) {
621 sr_err("Device inactive, can't stop acquisition.");
625 g_slist_free(devc->enabled_analog_probes);
626 g_slist_free(devc->enabled_digital_probes);
627 devc->enabled_analog_probes = NULL;
628 devc->enabled_digital_probes = NULL;
629 sr_source_remove(devc->fd);
634 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
635 .name = "rigol-ds1xx2",
636 .longname = "Rigol DS1xx2",
639 .cleanup = hw_cleanup,
641 .dev_list = hw_dev_list,
642 .dev_clear = clear_instances,
643 .config_get = config_get,
644 .config_set = config_set,
645 .config_list = config_list,
646 .dev_open = hw_dev_open,
647 .dev_close = hw_dev_close,
648 .dev_acquisition_start = dev_acquisition_start,
649 .dev_acquisition_stop = dev_acquisition_stop,