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"
31 static const int hwcaps[] = {
32 SR_HWCAP_OSCILLOSCOPE,
33 SR_HWCAP_LIMIT_SAMPLES,
35 SR_HWCAP_TRIGGER_SOURCE,
36 SR_HWCAP_TRIGGER_SLOPE,
37 SR_HWCAP_HORIZ_TRIGGERPOS,
43 static const char *probe_names[] = {
48 static const struct sr_rational timebases[] = {
87 static const struct sr_rational vdivs[] = {
105 static const char *trigger_sources[] = {
113 static const char *coupling[] = {
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);
143 sr_dev_inst_free(sdi);
146 g_slist_free(drvc->instances);
147 drvc->instances = NULL;
152 static int hw_init(struct sr_context *sr_ctx)
154 struct drv_context *drvc;
157 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
158 sr_err("Driver context malloc failed.");
159 return SR_ERR_MALLOC;
167 static GSList *hw_scan(GSList *options)
169 struct drv_context *drvc;
170 struct sr_dev_inst *sdi;
171 struct dev_context *devc;
172 struct sr_probe *probe;
175 const gchar *dev_name;
176 const gchar *dev_dir = "/dev/";
177 const gchar *prefix = "usbtmc";
179 const gchar *idn_query = "*IDN?";
180 int len, num_tokens, fd, i;
181 const gchar *delimiter = ",";
189 drvc->instances = NULL;
191 dir = g_dir_open("/sys/class/usb/", 0, NULL);
196 while ((dev_name = g_dir_read_name(dir)) != NULL) {
197 if (strncmp(dev_name, prefix, strlen(prefix)))
200 device = g_strconcat(dev_dir, dev_name, NULL);
202 fd = open(device, O_RDWR);
203 len = write(fd, idn_query, strlen(idn_query));
204 len = read(fd, buf, sizeof(buf));
212 tokens = g_strsplit(buf, delimiter, 0);
215 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
217 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, tokens[0],
218 num_tokens > 1 ? tokens[1] : NULL,
219 num_tokens > 3 ? tokens[3] : NULL))) {
226 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
227 sr_err("Device context malloc failed.");
232 devc->device = device;
237 for (i = 0; i < 2; i++) {
238 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
239 i == 0 ? "CH1" : "CH2")))
241 sdi->probes = g_slist_append(sdi->probes, probe);
244 drvc->instances = g_slist_append(drvc->instances, sdi);
245 devices = g_slist_append(devices, sdi);
253 static GSList *hw_dev_list(void)
255 struct drv_context *drvc;
259 return drvc->instances;
262 static int hw_dev_open(struct sr_dev_inst *sdi)
264 struct dev_context *devc;
269 if ((fd = open(devc->device, O_RDWR)) == -1)
279 static int hw_dev_close(struct sr_dev_inst *sdi)
281 struct dev_context *devc;
290 static int hw_cleanup(void)
297 static int hw_info_get(int info_id, const void **data,
298 const struct sr_dev_inst *sdi)
306 case SR_DI_NUM_PROBES:
307 *data = GINT_TO_POINTER(NUM_PROBES);
309 case SR_DI_PROBE_NAMES:
312 case SR_DI_TIMEBASES:
315 case SR_DI_TRIGGER_SOURCES:
316 *data = trigger_sources;
325 sr_err("Unknown info_id: %d.", info_id);
332 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
335 struct dev_context *devc;
338 struct sr_rational tmp_rat;
344 if (sdi->status != SR_ST_ACTIVE) {
345 sr_err("Device inactive, can't set config options.");
351 case SR_HWCAP_LIMIT_FRAMES:
352 devc->limit_frames = *(const uint64_t *)value;
354 case SR_HWCAP_TRIGGER_SLOPE:
355 tmp_u64 = *(const int *)value;
356 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s\n",
357 tmp_u64 ? "POS" : "NEG");
359 case SR_HWCAP_HORIZ_TRIGGERPOS:
360 tmp_float = *(const float *)value;
361 rigol_ds1xx2_send_data(devc->fd, ":TIM:OFFS %.9f\n", tmp_float);
363 case SR_HWCAP_TIMEBASE:
364 tmp_rat = *(const struct sr_rational *)value;
365 rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f\n",
366 (float)tmp_rat.p / tmp_rat.q);
368 case SR_HWCAP_TRIGGER_SOURCE:
369 if (!strcmp(value, "CH1"))
371 else if (!strcmp(value, "CH2"))
373 else if (!strcmp(value, "EXT"))
375 else if (!strcmp(value, "AC Line"))
381 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s\n", channel);
384 /* TODO: Not supporting vdiv per channel yet. */
385 tmp_rat = *(const struct sr_rational *)value;
386 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
387 if (vdivs[i].p == tmp_rat.p
388 && vdivs[i].q == tmp_rat.q) {
389 devc->scale = (float)tmp_rat.p / tmp_rat.q;
390 for (j = 0; j < 2; j++)
391 rigol_ds1xx2_send_data(devc->fd,
392 ":CHAN%d:SCAL %.3f\n", j, devc->scale);
396 if (vdivs[i].p == 0 && vdivs[i].q == 0)
399 case SR_HWCAP_COUPLING:
400 /* TODO: Not supporting coupling per channel yet. */
401 for (i = 0; coupling[i]; i++) {
402 if (!strcmp(value, coupling[i])) {
403 for (j = 0; j < 2; j++)
404 rigol_ds1xx2_send_data(devc->fd,
405 ":CHAN%d:COUP %s\n", j, coupling[i]);
409 if (coupling[i] == 0)
413 sr_err("Unknown hardware capability: %d.", hwcap);
421 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
424 struct dev_context *devc;
425 struct sr_datafeed_packet packet;
426 struct sr_datafeed_header header;
427 struct sr_datafeed_meta_analog meta;
435 devc->num_frames = 0;
437 sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi);
439 /* Send header packet to the session bus. */
440 packet.type = SR_DF_HEADER;
441 packet.payload = (unsigned char *)&header;
442 header.feed_version = 1;
443 gettimeofday(&header.starttime, NULL);
444 sr_session_send(cb_data, &packet);
446 /* Send metadata about the SR_DF_ANALOG packets to come. */
447 packet.type = SR_DF_META_ANALOG;
448 packet.payload = &meta;
449 meta.num_probes = NUM_PROBES;
450 sr_session_send(cb_data, &packet);
452 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?\n");
453 len = read(devc->fd, buf, sizeof(buf));
455 devc->scale = atof(buf);
456 sr_dbg("Scale is %.3f.", devc->scale);
457 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:OFFS?\n");
458 len = read(devc->fd, buf, sizeof(buf));
460 devc->offset = atof(buf);
461 sr_dbg("Offset is %.6f.", devc->offset);
462 rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n");
467 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
469 struct dev_context *devc;
475 if (sdi->status != SR_ST_ACTIVE) {
476 sr_err("Device inactive, can't stop acquisition.");
480 sr_source_remove(devc->fd);
485 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
486 .name = "rigol-ds1xx2",
487 .longname = "Rigol DS1xx2",
490 .cleanup = hw_cleanup,
492 .dev_list = hw_dev_list,
493 .dev_clear = clear_instances,
494 .dev_open = hw_dev_open,
495 .dev_close = hw_dev_close,
496 .info_get = hw_info_get,
497 .dev_config_set = hw_dev_config_set,
498 .dev_acquisition_start = hw_dev_acquisition_start,
499 .dev_acquisition_stop = hw_dev_acquisition_stop,