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 int32_t hwcaps[] = {
31 SR_CONF_LIMIT_SAMPLES,
33 SR_CONF_TRIGGER_SOURCE,
34 SR_CONF_TRIGGER_SLOPE,
35 SR_CONF_HORIZ_TRIGGERPOS,
40 static const uint64_t timebases[][2] = {
78 static const uint64_t vdivs[][2] = {
95 static const char *trigger_sources[] = {
102 static const char *coupling[] = {
108 static const char *supported_models[] = {
115 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info;
116 static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info;
118 /* Properly close and free all devices. */
119 static int clear_instances(void)
121 struct sr_dev_inst *sdi;
122 struct drv_context *drvc;
123 struct dev_context *devc;
126 if (!(drvc = di->priv))
129 for (l = drvc->instances; l; l = l->next) {
130 if (!(sdi = l->data))
132 if (!(devc = sdi->priv))
135 g_free(devc->device);
136 g_slist_free(devc->enabled_probes);
139 sr_dev_inst_free(sdi);
142 g_slist_free(drvc->instances);
143 drvc->instances = NULL;
148 static int hw_init(struct sr_context *sr_ctx)
150 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
153 static GSList *hw_scan(GSList *options)
155 struct drv_context *drvc;
156 struct sr_dev_inst *sdi;
157 struct dev_context *devc;
158 struct sr_probe *probe;
161 const gchar *dev_name;
162 const gchar *dev_dir = "/dev/";
163 const gchar *prefix = "usbtmc";
165 const gchar *idn_query = "*IDN?";
167 int len, num_tokens, fd;
168 const gchar *delimiter = ",";
170 const char *manufacturer, *model, *version;
171 gboolean matched = FALSE;
177 drvc->instances = NULL;
181 dir = g_dir_open("/sys/class/usb/", 0, NULL);
186 while ((dev_name = g_dir_read_name(dir)) != NULL) {
187 if (strncmp(dev_name, prefix, strlen(prefix)))
190 device = g_strconcat(dev_dir, dev_name, NULL);
192 fd = open(device, O_RDWR);
193 len = write(fd, idn_query, strlen(idn_query));
194 len = read(fd, buf, sizeof(buf));
202 tokens = g_strsplit(buf, delimiter, 0);
204 sr_dbg("response: %s %d [%s]", device, len, buf);
206 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
208 if (num_tokens < 4) {
214 manufacturer = tokens[0];
218 if (strcmp(manufacturer, "Rigol Technologies")) {
224 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
225 if (!strcmp(model, supported_models[i])) {
231 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
232 manufacturer, model, version))) {
240 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
241 sr_err("Device context malloc failed.");
246 devc->device = device;
251 for (i = 0; i < 2; i++) {
252 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
253 i == 0 ? "CH1" : "CH2")))
255 sdi->probes = g_slist_append(sdi->probes, probe);
258 drvc->instances = g_slist_append(drvc->instances, sdi);
259 devices = g_slist_append(devices, sdi);
267 static GSList *hw_dev_list(void)
269 return ((struct drv_context *)(di->priv))->instances;
272 static int hw_dev_open(struct sr_dev_inst *sdi)
274 struct dev_context *devc;
279 if ((fd = open(devc->device, O_RDWR)) == -1)
289 static int hw_dev_close(struct sr_dev_inst *sdi)
291 struct dev_context *devc;
300 static int hw_cleanup(void)
307 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
309 struct dev_context *devc;
310 uint64_t tmp_u64, p, q;
314 const char *tmp_str, *channel;
318 if (sdi->status != SR_ST_ACTIVE) {
319 sr_err("Device inactive, can't set config options.");
325 case SR_CONF_LIMIT_FRAMES:
326 devc->limit_frames = g_variant_get_uint64(data);
328 case SR_CONF_TRIGGER_SLOPE:
329 tmp_u64 = g_variant_get_uint64(data);
330 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s\n",
331 tmp_u64 ? "POS" : "NEG");
333 case SR_CONF_HORIZ_TRIGGERPOS:
334 tmp_double = g_variant_get_double(data);
335 rigol_ds1xx2_send_data(devc->fd, ":TIM:OFFS %.9f\n", tmp_double);
337 case SR_CONF_TIMEBASE:
338 g_variant_get(data, "(tt)", &p, &q);
340 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
341 if (timebases[i][0] == p && timebases[i][1] == q) {
347 rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f\n",
348 (float)timebases[i][0] / timebases[i][1]);
350 case SR_CONF_TRIGGER_SOURCE:
351 tmp_str = g_variant_get_string(data, NULL);
352 if (!strcmp(tmp_str, "CH1"))
354 else if (!strcmp(tmp_str, "CH2"))
356 else if (!strcmp(tmp_str, "EXT"))
358 else if (!strcmp(tmp_str, "AC Line"))
364 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s\n", channel);
367 g_variant_get(data, "(tt)", &p, &q);
369 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
370 if (vdivs[i][0] != p || vdivs[i][1] != q)
372 devc->scale = (float)vdivs[i][0] / vdivs[i][1];
373 rigol_ds1xx2_send_data(devc->fd, ":CHAN0:SCAL %.3f\n",
375 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL %.3f\n",
379 if (i == ARRAY_SIZE(vdivs))
382 case SR_CONF_COUPLING:
383 /* TODO: Not supporting coupling per channel yet. */
384 tmp_str = g_variant_get_string(data, NULL);
385 for (i = 0; i < ARRAY_SIZE(coupling); i++) {
386 if (!strcmp(tmp_str, coupling[i])) {
387 rigol_ds1xx2_send_data(devc->fd, ":CHAN0:COUP %s\n",
389 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:COUP %s\n",
394 if (i == ARRAY_SIZE(coupling))
398 sr_err("Unknown hardware capability: %d.", id);
406 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
408 GVariant *tuple, *rational[2];
415 case SR_CONF_DEVICE_OPTIONS:
416 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
417 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
419 case SR_CONF_COUPLING:
420 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
423 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
424 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
425 rational[0] = g_variant_new_uint64(vdivs[i][0]);
426 rational[1] = g_variant_new_uint64(vdivs[i][1]);
427 tuple = g_variant_new_tuple(rational, 2);
428 g_variant_builder_add_value(&gvb, tuple);
430 *data = g_variant_builder_end(&gvb);
432 case SR_CONF_TIMEBASE:
433 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
434 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
435 rational[0] = g_variant_new_uint64(timebases[i][0]);
436 rational[1] = g_variant_new_uint64(timebases[i][1]);
437 tuple = g_variant_new_tuple(rational, 2);
438 g_variant_builder_add_value(&gvb, tuple);
440 *data = g_variant_builder_end(&gvb);
442 case SR_CONF_TRIGGER_SOURCE:
443 *data = g_variant_new_strv(trigger_sources,
444 ARRAY_SIZE(trigger_sources));
453 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
456 struct dev_context *devc;
464 devc->num_frames = 0;
466 sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi);
468 /* Send header packet to the session bus. */
469 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
471 /* Hardcoded to CH1 only. */
472 devc->enabled_probes = g_slist_append(NULL, sdi->probes->data);
473 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?\n");
474 len = read(devc->fd, buf, sizeof(buf));
476 devc->scale = atof(buf);
477 sr_dbg("Scale is %.3f.", devc->scale);
478 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:OFFS?\n");
479 len = read(devc->fd, buf, sizeof(buf));
481 devc->offset = atof(buf);
482 sr_dbg("Offset is %.6f.", devc->offset);
483 rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n");
488 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
490 struct dev_context *devc;
496 if (sdi->status != SR_ST_ACTIVE) {
497 sr_err("Device inactive, can't stop acquisition.");
501 sr_source_remove(devc->fd);
506 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
507 .name = "rigol-ds1xx2",
508 .longname = "Rigol DS1xx2",
511 .cleanup = hw_cleanup,
513 .dev_list = hw_dev_list,
514 .dev_clear = clear_instances,
516 .config_set = config_set,
517 .config_list = config_list,
518 .dev_open = hw_dev_open,
519 .dev_close = hw_dev_close,
520 .dev_acquisition_start = hw_dev_acquisition_start,
521 .dev_acquisition_stop = hw_dev_acquisition_stop,