2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Martin Ling <martin-git@earth.li>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
29 static const int hwcaps[] = {
31 SR_CONF_LIMIT_SAMPLES,
33 SR_CONF_TRIGGER_SOURCE,
34 SR_CONF_TRIGGER_SLOPE,
35 SR_CONF_HORIZ_TRIGGERPOS,
41 static const struct sr_rational timebases[] = {
80 static const struct sr_rational vdivs[] = {
98 static const char *trigger_sources[] = {
106 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_slist_free(devc->enabled_probes);
144 sr_dev_inst_free(sdi);
147 g_slist_free(drvc->instances);
148 drvc->instances = NULL;
153 static int hw_init(struct sr_context *sr_ctx)
155 struct drv_context *drvc;
158 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
159 sr_err("Driver context malloc failed.");
160 return SR_ERR_MALLOC;
168 static GSList *hw_scan(GSList *options)
170 struct drv_context *drvc;
171 struct sr_dev_inst *sdi;
172 struct dev_context *devc;
173 struct sr_probe *probe;
176 const gchar *dev_name;
177 const gchar *dev_dir = "/dev/";
178 const gchar *prefix = "usbtmc";
180 const gchar *idn_query = "*IDN?";
181 int len, num_tokens, fd, i;
182 const gchar *delimiter = ",";
184 const char *manufacturer, *model, *version;
186 gboolean matched = FALSE;
193 drvc->instances = NULL;
195 dir = g_dir_open("/sys/class/usb/", 0, NULL);
200 while ((dev_name = g_dir_read_name(dir)) != NULL) {
201 if (strncmp(dev_name, prefix, strlen(prefix)))
204 device = g_strconcat(dev_dir, dev_name, NULL);
206 fd = open(device, O_RDWR);
207 len = write(fd, idn_query, strlen(idn_query));
208 len = read(fd, buf, sizeof(buf));
216 tokens = g_strsplit(buf, delimiter, 0);
218 sr_dbg("response: %s %d [%s]", device, len, buf);
220 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
222 if (num_tokens < 4) {
228 manufacturer = tokens[0];
232 if (strcmp(manufacturer, "Rigol Technologies")) {
238 num_models = sizeof(supported_models) / sizeof(supported_models[0]);
240 for (i = 0; i < num_models; i++) {
241 if (!strcmp(model, supported_models[i])) {
247 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
248 manufacturer, model, version))) {
256 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
257 sr_err("Device context malloc failed.");
262 devc->device = device;
267 for (i = 0; i < 2; i++) {
268 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
269 i == 0 ? "CH1" : "CH2")))
271 sdi->probes = g_slist_append(sdi->probes, probe);
274 drvc->instances = g_slist_append(drvc->instances, sdi);
275 devices = g_slist_append(devices, sdi);
283 static GSList *hw_dev_list(void)
285 struct drv_context *drvc;
289 return drvc->instances;
292 static int hw_dev_open(struct sr_dev_inst *sdi)
294 struct dev_context *devc;
299 if ((fd = open(devc->device, O_RDWR)) == -1)
309 static int hw_dev_close(struct sr_dev_inst *sdi)
311 struct dev_context *devc;
320 static int hw_cleanup(void)
327 static int config_get(int id, const void **data, const struct sr_dev_inst *sdi)
335 case SR_DI_TRIGGER_SOURCES:
336 *data = trigger_sources;
345 static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
347 struct dev_context *devc;
350 struct sr_rational tmp_rat;
356 if (sdi->status != SR_ST_ACTIVE) {
357 sr_err("Device inactive, can't set config options.");
363 case SR_CONF_LIMIT_FRAMES:
364 devc->limit_frames = *(const uint64_t *)value;
366 case SR_CONF_TRIGGER_SLOPE:
367 tmp_u64 = *(const int *)value;
368 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s\n",
369 tmp_u64 ? "POS" : "NEG");
371 case SR_CONF_HORIZ_TRIGGERPOS:
372 tmp_float = *(const float *)value;
373 rigol_ds1xx2_send_data(devc->fd, ":TIM:OFFS %.9f\n", tmp_float);
375 case SR_CONF_TIMEBASE:
376 tmp_rat = *(const struct sr_rational *)value;
377 rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f\n",
378 (float)tmp_rat.p / tmp_rat.q);
380 case SR_CONF_TRIGGER_SOURCE:
381 if (!strcmp(value, "CH1"))
383 else if (!strcmp(value, "CH2"))
385 else if (!strcmp(value, "EXT"))
387 else if (!strcmp(value, "AC Line"))
393 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s\n", channel);
396 /* TODO: Not supporting vdiv per channel yet. */
397 tmp_rat = *(const struct sr_rational *)value;
398 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
399 if (vdivs[i].p == tmp_rat.p
400 && vdivs[i].q == tmp_rat.q) {
401 devc->scale = (float)tmp_rat.p / tmp_rat.q;
402 for (j = 0; j < 2; j++)
403 rigol_ds1xx2_send_data(devc->fd,
404 ":CHAN%d:SCAL %.3f\n", j, devc->scale);
408 if (vdivs[i].p == 0 && vdivs[i].q == 0)
411 case SR_CONF_COUPLING:
412 /* TODO: Not supporting coupling per channel yet. */
413 for (i = 0; coupling[i]; i++) {
414 if (!strcmp(value, coupling[i])) {
415 for (j = 0; j < 2; j++)
416 rigol_ds1xx2_send_data(devc->fd,
417 ":CHAN%d:COUP %s\n", j, coupling[i]);
421 if (coupling[i] == 0)
425 sr_err("Unknown hardware capability: %d.", id);
433 static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
439 case SR_CONF_COUPLING:
445 case SR_CONF_TIMEBASE:
455 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
458 struct dev_context *devc;
459 struct sr_datafeed_packet packet;
460 struct sr_datafeed_header header;
468 devc->num_frames = 0;
470 sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi);
472 /* Send header packet to the session bus. */
473 packet.type = SR_DF_HEADER;
474 packet.payload = (unsigned char *)&header;
475 header.feed_version = 1;
476 gettimeofday(&header.starttime, NULL);
477 sr_session_send(cb_data, &packet);
479 /* Hardcoded to CH1 only. */
480 devc->enabled_probes = g_slist_append(NULL, sdi->probes->data);
481 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?\n");
482 len = read(devc->fd, buf, sizeof(buf));
484 devc->scale = atof(buf);
485 sr_dbg("Scale is %.3f.", devc->scale);
486 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:OFFS?\n");
487 len = read(devc->fd, buf, sizeof(buf));
489 devc->offset = atof(buf);
490 sr_dbg("Offset is %.6f.", devc->offset);
491 rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n");
496 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
498 struct dev_context *devc;
504 if (sdi->status != SR_ST_ACTIVE) {
505 sr_err("Device inactive, can't stop acquisition.");
509 sr_source_remove(devc->fd);
514 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
515 .name = "rigol-ds1xx2",
516 .longname = "Rigol DS1xx2",
519 .cleanup = hw_cleanup,
521 .dev_list = hw_dev_list,
522 .dev_clear = clear_instances,
523 .config_get = config_get,
524 .config_set = config_set,
525 .config_list = config_list,
526 .dev_open = hw_dev_open,
527 .dev_close = hw_dev_close,
528 .dev_acquisition_start = hw_dev_acquisition_start,
529 .dev_acquisition_stop = hw_dev_acquisition_stop,