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[] = {
35 SR_CONF_LIMIT_SAMPLES,
37 SR_CONF_TRIGGER_SOURCE,
38 SR_CONF_TRIGGER_SLOPE,
39 SR_CONF_HORIZ_TRIGGERPOS,
46 static const uint64_t timebases[][2] = {
84 static const uint64_t vdivs[][2] = {
101 static const char *trigger_sources[] = {
108 static const char *coupling[] = {
114 static const char *supported_models[] = {
121 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info;
122 static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info;
124 /* Properly close and free all devices. */
125 static int clear_instances(void)
127 struct sr_dev_inst *sdi;
128 struct drv_context *drvc;
129 struct dev_context *devc;
132 if (!(drvc = di->priv))
135 for (l = drvc->instances; l; l = l->next) {
136 if (!(sdi = l->data))
138 if (!(devc = sdi->priv))
141 g_free(devc->device);
142 g_free(devc->coupling[0]);
143 g_free(devc->coupling[1]);
144 g_free(devc->trigger_source);
145 g_free(devc->trigger_slope);
148 sr_dev_inst_free(sdi);
151 g_slist_free(drvc->instances);
152 drvc->instances = NULL;
157 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
159 struct dev_context *devc;
165 va_start(args, format);
166 vsnprintf(buf, 255, format, args);
168 if (rigol_ds1xx2_send(devc, buf) != SR_OK)
171 /* When setting a bunch of parameters in a row, the DS1052E scrambles
172 * some of them unless there is at least 100ms delay in between. */
173 sr_spew("delay %dms", 100);
179 static int hw_init(struct sr_context *sr_ctx)
181 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
184 static GSList *hw_scan(GSList *options)
186 struct drv_context *drvc;
187 struct sr_dev_inst *sdi;
188 struct dev_context *devc;
189 struct sr_probe *probe;
192 const gchar *dev_name;
193 const gchar *dev_dir = "/dev/";
194 const gchar *prefix = "usbtmc";
196 const gchar *idn_query = "*IDN?";
198 int len, num_tokens, fd;
199 const gchar *delimiter = ",";
201 const char *manufacturer, *model, *version;
202 gboolean matched = FALSE;
208 drvc->instances = NULL;
212 dir = g_dir_open("/sys/class/usb/", 0, NULL);
217 while ((dev_name = g_dir_read_name(dir)) != NULL) {
218 if (strncmp(dev_name, prefix, strlen(prefix)))
221 device = g_strconcat(dev_dir, dev_name, NULL);
223 fd = open(device, O_RDWR);
224 len = write(fd, idn_query, strlen(idn_query));
225 len = read(fd, buf, sizeof(buf));
233 tokens = g_strsplit(buf, delimiter, 0);
235 sr_dbg("response: %s %d [%s]", device, len, buf);
237 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
239 if (num_tokens < 4) {
245 manufacturer = tokens[0];
249 if (strcmp(manufacturer, "Rigol Technologies")) {
255 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
256 if (!strcmp(model, supported_models[i])) {
262 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
263 manufacturer, model, version))) {
271 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
272 sr_err("Device context malloc failed.");
276 devc->limit_frames = 0;
277 devc->device = device;
281 for (i = 0; i < 2; i++) {
282 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
283 i == 0 ? "CH1" : "CH2")))
285 sdi->probes = g_slist_append(sdi->probes, probe);
288 drvc->instances = g_slist_append(drvc->instances, sdi);
289 devices = g_slist_append(devices, sdi);
297 static GSList *hw_dev_list(void)
299 return ((struct drv_context *)(di->priv))->instances;
302 static int hw_dev_open(struct sr_dev_inst *sdi)
304 struct dev_context *devc;
309 if ((fd = open(devc->device, O_RDWR)) == -1)
313 if (rigol_ds1xx2_get_dev_cfg(sdi) != SR_OK)
314 /* TODO: force configuration? */
320 static int hw_dev_close(struct sr_dev_inst *sdi)
322 struct dev_context *devc;
331 static int hw_cleanup(void)
338 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
344 case SR_CONF_NUM_TIMEBASE:
345 *data = g_variant_new_int32(NUM_TIMEBASE);
347 case SR_CONF_NUM_VDIV:
348 *data = g_variant_new_int32(NUM_VDIV);
357 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
359 struct dev_context *devc;
360 uint64_t tmp_u64, p, q;
368 if (sdi->status != SR_ST_ACTIVE) {
369 sr_err("Device inactive, can't set config options.");
375 case SR_CONF_LIMIT_FRAMES:
376 devc->limit_frames = g_variant_get_uint64(data);
378 case SR_CONF_TRIGGER_SLOPE:
379 tmp_u64 = g_variant_get_uint64(data);
380 if (tmp_u64 != 0 && tmp_u64 != 1)
382 g_free(devc->trigger_slope);
383 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
384 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
386 case SR_CONF_HORIZ_TRIGGERPOS:
387 t_dbl = g_variant_get_double(data);
388 if (t_dbl < 0.0 || t_dbl > 1.0)
390 devc->horiz_triggerpos = t_dbl;
391 /* We have the trigger offset as a percentage of the frame, but
392 * need to express this in seconds. */
393 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * NUM_TIMEBASE;
394 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
396 case SR_CONF_TIMEBASE:
397 g_variant_get(data, "(tt)", &p, &q);
398 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
399 if (timebases[i][0] == p && timebases[i][1] == q) {
400 devc->timebase = (float)p / q;
401 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
405 if (i == ARRAY_SIZE(timebases))
408 case SR_CONF_TRIGGER_SOURCE:
409 tmp_str = g_variant_get_string(data, NULL);
410 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
411 if (!strcmp(trigger_sources[i], tmp_str)) {
412 g_free(devc->trigger_source);
413 devc->trigger_source = g_strdup(trigger_sources[i]);
414 if (!strcmp(devc->trigger_source, "AC Line"))
416 else if (!strcmp(devc->trigger_source, "CH1"))
418 else if (!strcmp(devc->trigger_source, "CH2"))
421 tmp_str = (char *)devc->trigger_source;
422 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
426 if (i == ARRAY_SIZE(trigger_sources))
430 g_variant_get(data, "(tt)", &p, &q);
431 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
432 if (vdivs[i][0] != p || vdivs[i][1] != q)
434 devc->vdiv[0] = devc->vdiv[1] = (float)p / q;
435 set_cfg(sdi, ":CHAN1:SCAL %.3f", devc->vdiv[0]);
436 ret = set_cfg(sdi, ":CHAN2:SCAL %.3f", devc->vdiv[1]);
439 if (i == ARRAY_SIZE(vdivs))
442 case SR_CONF_COUPLING:
443 /* TODO: Not supporting coupling per channel yet. */
444 tmp_str = g_variant_get_string(data, NULL);
445 for (i = 0; i < ARRAY_SIZE(coupling); i++) {
446 if (!strcmp(tmp_str, coupling[i])) {
447 g_free(devc->coupling[0]);
448 g_free(devc->coupling[1]);
449 devc->coupling[0] = g_strdup(coupling[i]);
450 devc->coupling[1] = g_strdup(coupling[i]);
451 set_cfg(sdi, ":CHAN1:COUP %s", devc->coupling[0]);
452 ret = set_cfg(sdi, ":CHAN2:COUP %s", devc->coupling[1]);
456 if (i == ARRAY_SIZE(coupling))
460 sr_err("Unknown hardware capability: %d.", id);
468 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
470 GVariant *tuple, *rational[2];
477 case SR_CONF_DEVICE_OPTIONS:
478 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
479 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
481 case SR_CONF_COUPLING:
482 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
485 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
486 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
487 rational[0] = g_variant_new_uint64(vdivs[i][0]);
488 rational[1] = g_variant_new_uint64(vdivs[i][1]);
489 tuple = g_variant_new_tuple(rational, 2);
490 g_variant_builder_add_value(&gvb, tuple);
492 *data = g_variant_builder_end(&gvb);
494 case SR_CONF_TIMEBASE:
495 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
496 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
497 rational[0] = g_variant_new_uint64(timebases[i][0]);
498 rational[1] = g_variant_new_uint64(timebases[i][1]);
499 tuple = g_variant_new_tuple(rational, 2);
500 g_variant_builder_add_value(&gvb, tuple);
502 *data = g_variant_builder_end(&gvb);
504 case SR_CONF_TRIGGER_SOURCE:
505 *data = g_variant_new_strv(trigger_sources,
506 ARRAY_SIZE(trigger_sources));
515 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
517 struct dev_context *devc;
518 struct sr_probe *probe;
527 for (l = sdi->probes; l; l = l->next) {
529 probenum = probe->name[2] == '1' ? 0 : 1;
531 devc->enabled_probes = g_slist_append(devc->enabled_probes, probe);
533 if (probe->enabled != devc->channels[probenum]) {
534 /* Enabled channel is currently disabled, or vice versa. */
535 sprintf(cmd, ":CHAN%d:DISP %s", probenum + 1,
536 probe->enabled ? "ON" : "OFF");
537 if (rigol_ds1xx2_send(devc, cmd) != SR_OK)
541 if (!devc->enabled_probes)
544 sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive, (void *)sdi);
546 /* Send header packet to the session bus. */
547 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
549 /* Fetch the first frame. */
550 devc->channel_frame = devc->enabled_probes->data;
551 if (rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%c",
552 devc->channel_frame->name[2]) != SR_OK)
555 devc->num_frame_bytes = 0;
560 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
562 struct dev_context *devc;
568 if (sdi->status != SR_ST_ACTIVE) {
569 sr_err("Device inactive, can't stop acquisition.");
573 g_slist_free(devc->enabled_probes);
574 devc->enabled_probes = NULL;
575 sr_source_remove(devc->fd);
580 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
581 .name = "rigol-ds1xx2",
582 .longname = "Rigol DS1xx2",
585 .cleanup = hw_cleanup,
587 .dev_list = hw_dev_list,
588 .dev_clear = clear_instances,
589 .config_get = config_get,
590 .config_set = config_set,
591 .config_list = config_list,
592 .dev_open = hw_dev_open,
593 .dev_close = hw_dev_close,
594 .dev_acquisition_start = dev_acquisition_start,
595 .dev_acquisition_stop = dev_acquisition_stop,