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[] = {
30 SR_HWCAP_OSCILLOSCOPE,
31 SR_HWCAP_LIMIT_SAMPLES,
33 SR_HWCAP_TRIGGER_SOURCE,
34 SR_HWCAP_TRIGGER_SLOPE,
35 SR_HWCAP_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);
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 = ",";
183 const char *manufacturer, *model, *version;
185 gboolean matched = FALSE;
192 drvc->instances = NULL;
194 dir = g_dir_open("/sys/class/usb/", 0, NULL);
199 while ((dev_name = g_dir_read_name(dir)) != NULL) {
200 if (strncmp(dev_name, prefix, strlen(prefix)))
203 device = g_strconcat(dev_dir, dev_name, NULL);
205 fd = open(device, O_RDWR);
206 len = write(fd, idn_query, strlen(idn_query));
207 len = read(fd, buf, sizeof(buf));
215 tokens = g_strsplit(buf, delimiter, 0);
217 sr_dbg("response: %s %d [%s]", device, len, buf);
219 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
221 if (num_tokens < 4) {
227 manufacturer = tokens[0];
231 if (strcmp(manufacturer, "Rigol Technologies")) {
237 num_models = sizeof(supported_models) / sizeof(supported_models[0]);
239 for (i = 0; i < num_models; i++) {
240 if (!strcmp(model, supported_models[i])) {
246 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
247 manufacturer, model, version))) {
255 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
256 sr_err("Device context malloc failed.");
261 devc->device = device;
266 for (i = 0; i < 2; i++) {
267 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
268 i == 0 ? "CH1" : "CH2")))
270 sdi->probes = g_slist_append(sdi->probes, probe);
273 drvc->instances = g_slist_append(drvc->instances, sdi);
274 devices = g_slist_append(devices, sdi);
282 static GSList *hw_dev_list(void)
284 struct drv_context *drvc;
288 return drvc->instances;
291 static int hw_dev_open(struct sr_dev_inst *sdi)
293 struct dev_context *devc;
298 if ((fd = open(devc->device, O_RDWR)) == -1)
308 static int hw_dev_close(struct sr_dev_inst *sdi)
310 struct dev_context *devc;
319 static int hw_cleanup(void)
326 static int hw_info_get(int info_id, const void **data,
327 const struct sr_dev_inst *sdi)
335 case SR_DI_TIMEBASES:
338 case SR_DI_TRIGGER_SOURCES:
339 *data = trigger_sources;
354 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
357 struct dev_context *devc;
360 struct sr_rational tmp_rat;
366 if (sdi->status != SR_ST_ACTIVE) {
367 sr_err("Device inactive, can't set config options.");
373 case SR_HWCAP_LIMIT_FRAMES:
374 devc->limit_frames = *(const uint64_t *)value;
376 case SR_HWCAP_TRIGGER_SLOPE:
377 tmp_u64 = *(const int *)value;
378 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s\n",
379 tmp_u64 ? "POS" : "NEG");
381 case SR_HWCAP_HORIZ_TRIGGERPOS:
382 tmp_float = *(const float *)value;
383 rigol_ds1xx2_send_data(devc->fd, ":TIM:OFFS %.9f\n", tmp_float);
385 case SR_HWCAP_TIMEBASE:
386 tmp_rat = *(const struct sr_rational *)value;
387 rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f\n",
388 (float)tmp_rat.p / tmp_rat.q);
390 case SR_HWCAP_TRIGGER_SOURCE:
391 if (!strcmp(value, "CH1"))
393 else if (!strcmp(value, "CH2"))
395 else if (!strcmp(value, "EXT"))
397 else if (!strcmp(value, "AC Line"))
403 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s\n", channel);
406 /* TODO: Not supporting vdiv per channel yet. */
407 tmp_rat = *(const struct sr_rational *)value;
408 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
409 if (vdivs[i].p == tmp_rat.p
410 && vdivs[i].q == tmp_rat.q) {
411 devc->scale = (float)tmp_rat.p / tmp_rat.q;
412 for (j = 0; j < 2; j++)
413 rigol_ds1xx2_send_data(devc->fd,
414 ":CHAN%d:SCAL %.3f\n", j, devc->scale);
418 if (vdivs[i].p == 0 && vdivs[i].q == 0)
421 case SR_HWCAP_COUPLING:
422 /* TODO: Not supporting coupling per channel yet. */
423 for (i = 0; coupling[i]; i++) {
424 if (!strcmp(value, coupling[i])) {
425 for (j = 0; j < 2; j++)
426 rigol_ds1xx2_send_data(devc->fd,
427 ":CHAN%d:COUP %s\n", j, coupling[i]);
431 if (coupling[i] == 0)
435 sr_err("Unknown hardware capability: %d.", hwcap);
443 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
446 struct dev_context *devc;
447 struct sr_datafeed_packet packet;
448 struct sr_datafeed_header header;
456 devc->num_frames = 0;
458 sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi);
460 /* Send header packet to the session bus. */
461 packet.type = SR_DF_HEADER;
462 packet.payload = (unsigned char *)&header;
463 header.feed_version = 1;
464 gettimeofday(&header.starttime, NULL);
465 sr_session_send(cb_data, &packet);
467 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?\n");
468 len = read(devc->fd, buf, sizeof(buf));
470 devc->scale = atof(buf);
471 sr_dbg("Scale is %.3f.", devc->scale);
472 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:OFFS?\n");
473 len = read(devc->fd, buf, sizeof(buf));
475 devc->offset = atof(buf);
476 sr_dbg("Offset is %.6f.", devc->offset);
477 rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n");
482 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
484 struct dev_context *devc;
490 if (sdi->status != SR_ST_ACTIVE) {
491 sr_err("Device inactive, can't stop acquisition.");
495 sr_source_remove(devc->fd);
500 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
501 .name = "rigol-ds1xx2",
502 .longname = "Rigol DS1xx2",
505 .cleanup = hw_cleanup,
507 .dev_list = hw_dev_list,
508 .dev_clear = clear_instances,
509 .dev_open = hw_dev_open,
510 .dev_close = hw_dev_close,
511 .info_get = hw_info_get,
512 .dev_config_set = hw_dev_config_set,
513 .dev_acquisition_start = hw_dev_acquisition_start,
514 .dev_acquisition_stop = hw_dev_acquisition_stop,