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 struct sr_rational timebases[] = {
82 static const struct sr_rational vdivs[] = {
100 static const char *trigger_sources[] = {
108 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);
145 sr_dev_inst_free(sdi);
148 g_slist_free(drvc->instances);
149 drvc->instances = NULL;
154 static int hw_init(struct sr_context *sr_ctx)
156 struct drv_context *drvc;
159 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
160 sr_err("Driver context malloc failed.");
161 return SR_ERR_MALLOC;
169 static GSList *hw_scan(GSList *options)
171 struct drv_context *drvc;
172 struct sr_dev_inst *sdi;
173 struct dev_context *devc;
174 struct sr_probe *probe;
177 const gchar *dev_name;
178 const gchar *dev_dir = "/dev/";
179 const gchar *prefix = "usbtmc";
181 const gchar *idn_query = "*IDN?";
182 int len, num_tokens, fd, i;
183 const gchar *delimiter = ",";
185 const char *manufacturer, *model, *version;
187 gboolean matched = FALSE;
194 drvc->instances = NULL;
196 dir = g_dir_open("/sys/class/usb/", 0, NULL);
201 while ((dev_name = g_dir_read_name(dir)) != NULL) {
202 if (strncmp(dev_name, prefix, strlen(prefix)))
205 device = g_strconcat(dev_dir, dev_name, NULL);
207 fd = open(device, O_RDWR);
208 len = write(fd, idn_query, strlen(idn_query));
209 len = read(fd, buf, sizeof(buf));
217 tokens = g_strsplit(buf, delimiter, 0);
219 sr_dbg("response: %s %d [%s]", device, len, buf);
221 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
223 if (num_tokens < 4) {
229 manufacturer = tokens[0];
233 if (strcmp(manufacturer, "Rigol Technologies")) {
239 num_models = sizeof(supported_models) / sizeof(supported_models[0]);
241 for (i = 0; i < num_models; i++) {
242 if (!strcmp(model, supported_models[i])) {
248 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
249 manufacturer, model, version))) {
257 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
258 sr_err("Device context malloc failed.");
263 devc->device = device;
268 for (i = 0; i < 2; i++) {
269 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
270 i == 0 ? "CH1" : "CH2")))
272 sdi->probes = g_slist_append(sdi->probes, probe);
275 drvc->instances = g_slist_append(drvc->instances, sdi);
276 devices = g_slist_append(devices, sdi);
284 static GSList *hw_dev_list(void)
286 struct drv_context *drvc;
290 return drvc->instances;
293 static int hw_dev_open(struct sr_dev_inst *sdi)
295 struct dev_context *devc;
300 if ((fd = open(devc->device, O_RDWR)) == -1)
310 static int hw_dev_close(struct sr_dev_inst *sdi)
312 struct dev_context *devc;
321 static int hw_cleanup(void)
328 static int hw_info_get(int info_id, const void **data,
329 const struct sr_dev_inst *sdi)
337 case SR_DI_TIMEBASES:
340 case SR_DI_TRIGGER_SOURCES:
341 *data = trigger_sources;
350 sr_err("Unknown info_id: %d.", info_id);
357 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
360 struct dev_context *devc;
363 struct sr_rational tmp_rat;
369 if (sdi->status != SR_ST_ACTIVE) {
370 sr_err("Device inactive, can't set config options.");
376 case SR_HWCAP_LIMIT_FRAMES:
377 devc->limit_frames = *(const uint64_t *)value;
379 case SR_HWCAP_TRIGGER_SLOPE:
380 tmp_u64 = *(const int *)value;
381 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s\n",
382 tmp_u64 ? "POS" : "NEG");
384 case SR_HWCAP_HORIZ_TRIGGERPOS:
385 tmp_float = *(const float *)value;
386 rigol_ds1xx2_send_data(devc->fd, ":TIM:OFFS %.9f\n", tmp_float);
388 case SR_HWCAP_TIMEBASE:
389 tmp_rat = *(const struct sr_rational *)value;
390 rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f\n",
391 (float)tmp_rat.p / tmp_rat.q);
393 case SR_HWCAP_TRIGGER_SOURCE:
394 if (!strcmp(value, "CH1"))
396 else if (!strcmp(value, "CH2"))
398 else if (!strcmp(value, "EXT"))
400 else if (!strcmp(value, "AC Line"))
406 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s\n", channel);
409 /* TODO: Not supporting vdiv per channel yet. */
410 tmp_rat = *(const struct sr_rational *)value;
411 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
412 if (vdivs[i].p == tmp_rat.p
413 && vdivs[i].q == tmp_rat.q) {
414 devc->scale = (float)tmp_rat.p / tmp_rat.q;
415 for (j = 0; j < 2; j++)
416 rigol_ds1xx2_send_data(devc->fd,
417 ":CHAN%d:SCAL %.3f\n", j, devc->scale);
421 if (vdivs[i].p == 0 && vdivs[i].q == 0)
424 case SR_HWCAP_COUPLING:
425 /* TODO: Not supporting coupling per channel yet. */
426 for (i = 0; coupling[i]; i++) {
427 if (!strcmp(value, coupling[i])) {
428 for (j = 0; j < 2; j++)
429 rigol_ds1xx2_send_data(devc->fd,
430 ":CHAN%d:COUP %s\n", j, coupling[i]);
434 if (coupling[i] == 0)
438 sr_err("Unknown hardware capability: %d.", hwcap);
446 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
449 struct dev_context *devc;
450 struct sr_datafeed_packet packet;
451 struct sr_datafeed_header header;
452 struct sr_datafeed_meta_analog meta;
460 devc->num_frames = 0;
462 sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi);
464 /* Send header packet to the session bus. */
465 packet.type = SR_DF_HEADER;
466 packet.payload = (unsigned char *)&header;
467 header.feed_version = 1;
468 gettimeofday(&header.starttime, NULL);
469 sr_session_send(cb_data, &packet);
471 /* Send metadata about the SR_DF_ANALOG packets to come. */
472 packet.type = SR_DF_META_ANALOG;
473 packet.payload = &meta;
474 meta.num_probes = NUM_PROBES;
475 sr_session_send(cb_data, &packet);
477 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?\n");
478 len = read(devc->fd, buf, sizeof(buf));
480 devc->scale = atof(buf);
481 sr_dbg("Scale is %.3f.", devc->scale);
482 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:OFFS?\n");
483 len = read(devc->fd, buf, sizeof(buf));
485 devc->offset = atof(buf);
486 sr_dbg("Offset is %.6f.", devc->offset);
487 rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n");
492 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
494 struct dev_context *devc;
500 if (sdi->status != SR_ST_ACTIVE) {
501 sr_err("Device inactive, can't stop acquisition.");
505 sr_source_remove(devc->fd);
510 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
511 .name = "rigol-ds1xx2",
512 .longname = "Rigol DS1xx2",
515 .cleanup = hw_cleanup,
517 .dev_list = hw_dev_list,
518 .dev_clear = clear_instances,
519 .dev_open = hw_dev_open,
520 .dev_close = hw_dev_close,
521 .info_get = hw_info_get,
522 .dev_config_set = hw_dev_config_set,
523 .dev_acquisition_start = hw_dev_acquisition_start,
524 .dev_acquisition_stop = hw_dev_acquisition_stop,