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/>.
27 #include "libsigrok.h"
28 #include "libsigrok-internal.h"
31 #define NUM_TIMEBASE 12
34 static const int32_t hwcaps[] = {
36 SR_CONF_LIMIT_SAMPLES,
38 SR_CONF_TRIGGER_SOURCE,
39 SR_CONF_TRIGGER_SLOPE,
40 SR_CONF_HORIZ_TRIGGERPOS,
47 static const uint64_t timebases[][2] = {
85 static const uint64_t vdivs[][2] = {
102 static const char *trigger_sources[] = {
109 static const char *coupling[] = {
115 static const char *supported_models[] = {
122 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info;
123 static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info;
125 /* Properly close and free all devices. */
126 static int clear_instances(void)
128 struct sr_dev_inst *sdi;
129 struct drv_context *drvc;
130 struct dev_context *devc;
133 if (!(drvc = di->priv))
136 for (l = drvc->instances; l; l = l->next) {
137 if (!(sdi = l->data))
139 if (!(devc = sdi->priv))
142 g_free(devc->device);
143 g_free(devc->coupling[0]);
144 g_free(devc->coupling[1]);
145 g_free(devc->trigger_source);
146 g_free(devc->trigger_slope);
149 sr_dev_inst_free(sdi);
152 g_slist_free(drvc->instances);
153 drvc->instances = NULL;
158 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
160 struct dev_context *devc;
161 struct timespec delay;
167 va_start(args, format);
168 vsnprintf(buf, 255, format, args);
170 if (rigol_ds1xx2_send(devc, 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. */
176 delay.tv_nsec = 100000000L;
177 sr_spew("delay %dms", delay.tv_nsec / 1000000);
178 nanosleep(&delay, NULL);
183 static int hw_init(struct sr_context *sr_ctx)
185 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
188 static GSList *hw_scan(GSList *options)
190 struct drv_context *drvc;
191 struct sr_dev_inst *sdi;
192 struct dev_context *devc;
193 struct sr_probe *probe;
196 const gchar *dev_name;
197 const gchar *dev_dir = "/dev/";
198 const gchar *prefix = "usbtmc";
200 const gchar *idn_query = "*IDN?";
202 int len, num_tokens, fd;
203 const gchar *delimiter = ",";
205 const char *manufacturer, *model, *version;
206 gboolean matched = FALSE;
212 drvc->instances = NULL;
216 dir = g_dir_open("/sys/class/usb/", 0, NULL);
221 while ((dev_name = g_dir_read_name(dir)) != NULL) {
222 if (strncmp(dev_name, prefix, strlen(prefix)))
225 device = g_strconcat(dev_dir, dev_name, NULL);
227 fd = open(device, O_RDWR);
228 len = write(fd, idn_query, strlen(idn_query));
229 len = read(fd, buf, sizeof(buf));
237 tokens = g_strsplit(buf, delimiter, 0);
239 sr_dbg("response: %s %d [%s]", device, len, buf);
241 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
243 if (num_tokens < 4) {
249 manufacturer = tokens[0];
253 if (strcmp(manufacturer, "Rigol Technologies")) {
259 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
260 if (!strcmp(model, supported_models[i])) {
266 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
267 manufacturer, model, version))) {
275 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
276 sr_err("Device context malloc failed.");
280 devc->limit_frames = 0;
281 devc->device = device;
285 for (i = 0; i < 2; i++) {
286 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
287 i == 0 ? "CH1" : "CH2")))
289 sdi->probes = g_slist_append(sdi->probes, probe);
292 drvc->instances = g_slist_append(drvc->instances, sdi);
293 devices = g_slist_append(devices, sdi);
301 static GSList *hw_dev_list(void)
303 return ((struct drv_context *)(di->priv))->instances;
306 static int hw_dev_open(struct sr_dev_inst *sdi)
308 struct dev_context *devc;
313 if ((fd = open(devc->device, O_RDWR)) == -1)
317 if (rigol_ds1xx2_get_dev_cfg(sdi) != SR_OK)
318 /* TODO: force configuration? */
324 static int hw_dev_close(struct sr_dev_inst *sdi)
326 struct dev_context *devc;
335 static int hw_cleanup(void)
342 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
348 case SR_CONF_NUM_TIMEBASE:
349 *data = g_variant_new_int32(NUM_TIMEBASE);
351 case SR_CONF_NUM_VDIV:
352 *data = g_variant_new_int32(NUM_VDIV);
361 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
363 struct dev_context *devc;
364 uint64_t tmp_u64, p, q;
372 if (sdi->status != SR_ST_ACTIVE) {
373 sr_err("Device inactive, can't set config options.");
379 case SR_CONF_LIMIT_FRAMES:
380 devc->limit_frames = g_variant_get_uint64(data);
382 case SR_CONF_TRIGGER_SLOPE:
383 tmp_u64 = g_variant_get_uint64(data);
384 if (tmp_u64 != 0 && tmp_u64 != 1)
386 g_free(devc->trigger_slope);
387 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
388 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
390 case SR_CONF_HORIZ_TRIGGERPOS:
391 t_dbl = g_variant_get_double(data);
392 if (t_dbl < 0.0 || t_dbl > 1.0)
394 devc->horiz_triggerpos = t_dbl;
395 /* We have the trigger offset as a percentage of the frame, but
396 * need to express this in seconds. */
397 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * NUM_TIMEBASE;
398 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
400 case SR_CONF_TIMEBASE:
401 g_variant_get(data, "(tt)", &p, &q);
402 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
403 if (timebases[i][0] == p && timebases[i][1] == q) {
404 devc->timebase = (float)p / q;
405 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
409 if (i == ARRAY_SIZE(timebases))
412 case SR_CONF_TRIGGER_SOURCE:
413 tmp_str = g_variant_get_string(data, NULL);
414 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
415 if (!strcmp(trigger_sources[i], tmp_str)) {
416 g_free(devc->trigger_source);
417 devc->trigger_source = g_strdup(trigger_sources[i]);
418 if (!strcmp(devc->trigger_source, "AC Line"))
420 else if (!strcmp(devc->trigger_source, "CH1"))
422 else if (!strcmp(devc->trigger_source, "CH2"))
425 tmp_str = (char *)devc->trigger_source;
426 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
430 if (i == ARRAY_SIZE(trigger_sources))
434 g_variant_get(data, "(tt)", &p, &q);
435 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
436 if (vdivs[i][0] != p || vdivs[i][1] != q)
438 devc->vdiv[0] = devc->vdiv[1] = (float)p / q;
439 set_cfg(sdi, ":CHAN1:SCAL %.3f", devc->vdiv[0]);
440 ret = set_cfg(sdi, ":CHAN2:SCAL %.3f", devc->vdiv[1]);
443 if (i == ARRAY_SIZE(vdivs))
446 case SR_CONF_COUPLING:
447 /* TODO: Not supporting coupling per channel yet. */
448 tmp_str = g_variant_get_string(data, NULL);
449 for (i = 0; i < ARRAY_SIZE(coupling); i++) {
450 if (!strcmp(tmp_str, coupling[i])) {
451 g_free(devc->coupling[0]);
452 g_free(devc->coupling[1]);
453 devc->coupling[0] = g_strdup(coupling[i]);
454 devc->coupling[1] = g_strdup(coupling[i]);
455 set_cfg(sdi, ":CHAN1:COUP %s", devc->coupling[0]);
456 ret = set_cfg(sdi, ":CHAN2:COUP %s", devc->coupling[1]);
460 if (i == ARRAY_SIZE(coupling))
464 sr_err("Unknown hardware capability: %d.", id);
472 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
474 GVariant *tuple, *rational[2];
481 case SR_CONF_DEVICE_OPTIONS:
482 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
483 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
485 case SR_CONF_COUPLING:
486 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
489 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
490 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
491 rational[0] = g_variant_new_uint64(vdivs[i][0]);
492 rational[1] = g_variant_new_uint64(vdivs[i][1]);
493 tuple = g_variant_new_tuple(rational, 2);
494 g_variant_builder_add_value(&gvb, tuple);
496 *data = g_variant_builder_end(&gvb);
498 case SR_CONF_TIMEBASE:
499 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
500 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
501 rational[0] = g_variant_new_uint64(timebases[i][0]);
502 rational[1] = g_variant_new_uint64(timebases[i][1]);
503 tuple = g_variant_new_tuple(rational, 2);
504 g_variant_builder_add_value(&gvb, tuple);
506 *data = g_variant_builder_end(&gvb);
508 case SR_CONF_TRIGGER_SOURCE:
509 *data = g_variant_new_strv(trigger_sources,
510 ARRAY_SIZE(trigger_sources));
519 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
521 struct dev_context *devc;
522 struct sr_probe *probe;
531 for (l = sdi->probes; l; l = l->next) {
533 probenum = probe->name[2] == '1' ? 0 : 1;
535 devc->enabled_probes = g_slist_append(devc->enabled_probes, probe);
537 if (probe->enabled != devc->channels[probenum]) {
538 /* Enabled channel is currently disabled, or vice versa. */
539 sprintf(cmd, ":CHAN%d:DISP %s", probenum + 1,
540 probe->enabled ? "ON" : "OFF");
541 if (rigol_ds1xx2_send(devc, cmd) != SR_OK)
545 if (!devc->enabled_probes)
548 sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive, (void *)sdi);
550 /* Send header packet to the session bus. */
551 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
553 /* Fetch the first frame. */
554 devc->channel_frame = devc->enabled_probes->data;
555 if (rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%c",
556 devc->channel_frame->name[2]) != SR_OK)
562 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
564 struct dev_context *devc;
570 if (sdi->status != SR_ST_ACTIVE) {
571 sr_err("Device inactive, can't stop acquisition.");
575 g_slist_free(devc->enabled_probes);
576 devc->enabled_probes = NULL;
577 sr_source_remove(devc->fd);
582 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
583 .name = "rigol-ds1xx2",
584 .longname = "Rigol DS1xx2",
587 .cleanup = hw_cleanup,
589 .dev_list = hw_dev_list,
590 .dev_clear = clear_instances,
591 .config_get = config_get,
592 .config_set = config_set,
593 .config_list = config_list,
594 .dev_open = hw_dev_open,
595 .dev_close = hw_dev_close,
596 .dev_acquisition_start = dev_acquisition_start,
597 .dev_acquisition_stop = dev_acquisition_stop,