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[] = {
107 static const char *coupling[] = {
113 static const char *supported_models[] = {
120 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info;
121 static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info;
123 /* Properly close and free all devices. */
124 static int clear_instances(void)
126 struct sr_dev_inst *sdi;
127 struct drv_context *drvc;
128 struct dev_context *devc;
131 if (!(drvc = di->priv))
134 for (l = drvc->instances; l; l = l->next) {
135 if (!(sdi = l->data))
137 if (!(devc = sdi->priv))
140 g_free(devc->device);
141 g_free(devc->coupling[0]);
142 g_free(devc->coupling[1]);
143 g_free(devc->trigger_source);
144 g_free(devc->trigger_slope);
147 sr_dev_inst_free(sdi);
150 g_slist_free(drvc->instances);
151 drvc->instances = NULL;
156 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
158 struct dev_context *devc;
164 va_start(args, format);
165 vsnprintf(buf, 255, format, args);
167 if (rigol_ds1xx2_send(devc, buf) != SR_OK)
170 /* When setting a bunch of parameters in a row, the DS1052E scrambles
171 * some of them unless there is at least 100ms delay in between. */
172 sr_spew("delay %dms", 100);
178 static int hw_init(struct sr_context *sr_ctx)
180 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
183 static GSList *hw_scan(GSList *options)
185 struct drv_context *drvc;
186 struct sr_dev_inst *sdi;
187 struct dev_context *devc;
188 struct sr_probe *probe;
191 const gchar *dev_name;
192 const gchar *dev_dir = "/dev/";
193 const gchar *prefix = "usbtmc";
195 const gchar *idn_query = "*IDN?";
197 int len, num_tokens, fd;
198 const gchar *delimiter = ",";
200 const char *manufacturer, *model, *version;
201 gboolean matched = FALSE;
207 drvc->instances = NULL;
211 dir = g_dir_open("/sys/class/usb/", 0, NULL);
216 while ((dev_name = g_dir_read_name(dir)) != NULL) {
217 if (strncmp(dev_name, prefix, strlen(prefix)))
220 device = g_strconcat(dev_dir, dev_name, NULL);
222 fd = open(device, O_RDWR);
223 len = write(fd, idn_query, strlen(idn_query));
224 len = read(fd, buf, sizeof(buf));
232 tokens = g_strsplit(buf, delimiter, 0);
234 sr_dbg("response: %s %d [%s]", device, len, buf);
236 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
238 if (num_tokens < 4) {
244 manufacturer = tokens[0];
248 if (strcmp(manufacturer, "Rigol Technologies")) {
254 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
255 if (!strcmp(model, supported_models[i])) {
261 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
262 manufacturer, model, version))) {
270 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
271 sr_err("Device context malloc failed.");
275 devc->limit_frames = 0;
276 devc->device = device;
280 for (i = 0; i < 2; i++) {
281 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
282 i == 0 ? "CH1" : "CH2")))
284 sdi->probes = g_slist_append(sdi->probes, probe);
287 drvc->instances = g_slist_append(drvc->instances, sdi);
288 devices = g_slist_append(devices, sdi);
296 static GSList *hw_dev_list(void)
298 return ((struct drv_context *)(di->priv))->instances;
301 static int hw_dev_open(struct sr_dev_inst *sdi)
303 struct dev_context *devc;
308 if ((fd = open(devc->device, O_RDWR)) == -1)
312 if (rigol_ds1xx2_get_dev_cfg(sdi) != SR_OK)
313 /* TODO: force configuration? */
319 static int hw_dev_close(struct sr_dev_inst *sdi)
321 struct dev_context *devc;
330 static int hw_cleanup(void)
337 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
343 case SR_CONF_NUM_TIMEBASE:
344 *data = g_variant_new_int32(NUM_TIMEBASE);
346 case SR_CONF_NUM_VDIV:
347 *data = g_variant_new_int32(NUM_VDIV);
356 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
358 struct dev_context *devc;
359 uint64_t tmp_u64, p, q;
367 if (sdi->status != SR_ST_ACTIVE) {
368 sr_err("Device inactive, can't set config options.");
374 case SR_CONF_LIMIT_FRAMES:
375 devc->limit_frames = g_variant_get_uint64(data);
377 case SR_CONF_TRIGGER_SLOPE:
378 tmp_u64 = g_variant_get_uint64(data);
379 if (tmp_u64 != 0 && tmp_u64 != 1)
381 g_free(devc->trigger_slope);
382 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
383 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
385 case SR_CONF_HORIZ_TRIGGERPOS:
386 t_dbl = g_variant_get_double(data);
387 if (t_dbl < 0.0 || t_dbl > 1.0)
389 devc->horiz_triggerpos = t_dbl;
390 /* We have the trigger offset as a percentage of the frame, but
391 * need to express this in seconds. */
392 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * NUM_TIMEBASE;
393 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
395 case SR_CONF_TIMEBASE:
396 g_variant_get(data, "(tt)", &p, &q);
397 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
398 if (timebases[i][0] == p && timebases[i][1] == q) {
399 devc->timebase = (float)p / q;
400 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
404 if (i == ARRAY_SIZE(timebases))
407 case SR_CONF_TRIGGER_SOURCE:
408 tmp_str = g_variant_get_string(data, NULL);
409 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
410 if (!strcmp(trigger_sources[i], tmp_str)) {
411 g_free(devc->trigger_source);
412 devc->trigger_source = g_strdup(trigger_sources[i]);
413 if (!strcmp(devc->trigger_source, "AC Line"))
415 else if (!strcmp(devc->trigger_source, "CH1"))
417 else if (!strcmp(devc->trigger_source, "CH2"))
420 tmp_str = (char *)devc->trigger_source;
421 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
425 if (i == ARRAY_SIZE(trigger_sources))
429 g_variant_get(data, "(tt)", &p, &q);
430 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
431 if (vdivs[i][0] != p || vdivs[i][1] != q)
433 devc->vdiv[0] = devc->vdiv[1] = (float)p / q;
434 set_cfg(sdi, ":CHAN1:SCAL %.3f", devc->vdiv[0]);
435 ret = set_cfg(sdi, ":CHAN2:SCAL %.3f", devc->vdiv[1]);
438 if (i == ARRAY_SIZE(vdivs))
441 case SR_CONF_COUPLING:
442 /* TODO: Not supporting coupling per channel yet. */
443 tmp_str = g_variant_get_string(data, NULL);
444 for (i = 0; i < ARRAY_SIZE(coupling); i++) {
445 if (!strcmp(tmp_str, coupling[i])) {
446 g_free(devc->coupling[0]);
447 g_free(devc->coupling[1]);
448 devc->coupling[0] = g_strdup(coupling[i]);
449 devc->coupling[1] = g_strdup(coupling[i]);
450 set_cfg(sdi, ":CHAN1:COUP %s", devc->coupling[0]);
451 ret = set_cfg(sdi, ":CHAN2:COUP %s", devc->coupling[1]);
455 if (i == ARRAY_SIZE(coupling))
459 sr_err("Unknown hardware capability: %d.", id);
467 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
469 GVariant *tuple, *rational[2];
476 case SR_CONF_DEVICE_OPTIONS:
477 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
478 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
480 case SR_CONF_COUPLING:
481 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
484 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
485 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
486 rational[0] = g_variant_new_uint64(vdivs[i][0]);
487 rational[1] = g_variant_new_uint64(vdivs[i][1]);
488 tuple = g_variant_new_tuple(rational, 2);
489 g_variant_builder_add_value(&gvb, tuple);
491 *data = g_variant_builder_end(&gvb);
493 case SR_CONF_TIMEBASE:
494 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
495 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
496 rational[0] = g_variant_new_uint64(timebases[i][0]);
497 rational[1] = g_variant_new_uint64(timebases[i][1]);
498 tuple = g_variant_new_tuple(rational, 2);
499 g_variant_builder_add_value(&gvb, tuple);
501 *data = g_variant_builder_end(&gvb);
503 case SR_CONF_TRIGGER_SOURCE:
504 *data = g_variant_new_strv(trigger_sources,
505 ARRAY_SIZE(trigger_sources));
514 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
516 struct dev_context *devc;
517 struct sr_probe *probe;
526 for (l = sdi->probes; l; l = l->next) {
528 probenum = probe->name[2] == '1' ? 0 : 1;
530 devc->enabled_probes = g_slist_append(devc->enabled_probes, probe);
532 if (probe->enabled != devc->channels[probenum]) {
533 /* Enabled channel is currently disabled, or vice versa. */
534 sprintf(cmd, ":CHAN%d:DISP %s", probenum + 1,
535 probe->enabled ? "ON" : "OFF");
536 if (rigol_ds1xx2_send(devc, cmd) != SR_OK)
540 if (!devc->enabled_probes)
543 sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive, (void *)sdi);
545 /* Send header packet to the session bus. */
546 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
548 /* Fetch the first frame. */
549 devc->channel_frame = devc->enabled_probes->data;
550 if (rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%c",
551 devc->channel_frame->name[2]) != SR_OK)
554 devc->num_frame_bytes = 0;
559 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
561 struct dev_context *devc;
567 if (sdi->status != SR_ST_ACTIVE) {
568 sr_err("Device inactive, can't stop acquisition.");
572 g_slist_free(devc->enabled_probes);
573 devc->enabled_probes = NULL;
574 sr_source_remove(devc->fd);
579 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
580 .name = "rigol-ds1xx2",
581 .longname = "Rigol DS1xx2",
584 .cleanup = hw_cleanup,
586 .dev_list = hw_dev_list,
587 .dev_clear = clear_instances,
588 .config_get = config_get,
589 .config_set = config_set,
590 .config_list = config_list,
591 .dev_open = hw_dev_open,
592 .dev_close = hw_dev_close,
593 .dev_acquisition_start = dev_acquisition_start,
594 .dev_acquisition_stop = dev_acquisition_stop,