2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Martin Ling <martin-git@earth.li>
5 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
30 #define NUM_TIMEBASE 12
33 static const int32_t hwcaps[] = {
35 SR_CONF_LIMIT_SAMPLES,
37 SR_CONF_TRIGGER_SOURCE,
38 SR_CONF_TRIGGER_SLOPE,
39 SR_CONF_HORIZ_TRIGGERPOS,
46 static const uint64_t timebases[][2] = {
84 static const uint64_t vdivs[][2] = {
101 static const char *trigger_sources[] = {
108 static const char *coupling[] = {
114 static const char *supported_models[] = {
121 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info;
122 static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info;
124 /* Properly close and free all devices. */
125 static int clear_instances(void)
127 struct sr_dev_inst *sdi;
128 struct drv_context *drvc;
129 struct dev_context *devc;
132 if (!(drvc = di->priv))
135 for (l = drvc->instances; l; l = l->next) {
136 if (!(sdi = l->data))
138 if (!(devc = sdi->priv))
141 g_free(devc->device);
142 g_slist_free(devc->enabled_probes);
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 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
159 static GSList *hw_scan(GSList *options)
161 struct drv_context *drvc;
162 struct sr_dev_inst *sdi;
163 struct dev_context *devc;
164 struct sr_probe *probe;
167 const gchar *dev_name;
168 const gchar *dev_dir = "/dev/";
169 const gchar *prefix = "usbtmc";
171 const gchar *idn_query = "*IDN?";
173 int len, num_tokens, fd;
174 const gchar *delimiter = ",";
176 const char *manufacturer, *model, *version;
177 gboolean matched = FALSE;
183 drvc->instances = NULL;
187 dir = g_dir_open("/sys/class/usb/", 0, NULL);
192 while ((dev_name = g_dir_read_name(dir)) != NULL) {
193 if (strncmp(dev_name, prefix, strlen(prefix)))
196 device = g_strconcat(dev_dir, dev_name, NULL);
198 fd = open(device, O_RDWR);
199 len = write(fd, idn_query, strlen(idn_query));
200 len = read(fd, buf, sizeof(buf));
208 tokens = g_strsplit(buf, delimiter, 0);
210 sr_dbg("response: %s %d [%s]", device, len, buf);
212 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
214 if (num_tokens < 4) {
220 manufacturer = tokens[0];
224 if (strcmp(manufacturer, "Rigol Technologies")) {
230 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
231 if (!strcmp(model, supported_models[i])) {
237 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
238 manufacturer, model, version))) {
246 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
247 sr_err("Device context malloc failed.");
252 devc->device = device;
257 for (i = 0; i < 2; i++) {
258 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
259 i == 0 ? "CH1" : "CH2")))
261 sdi->probes = g_slist_append(sdi->probes, probe);
264 drvc->instances = g_slist_append(drvc->instances, sdi);
265 devices = g_slist_append(devices, sdi);
273 static GSList *hw_dev_list(void)
275 return ((struct drv_context *)(di->priv))->instances;
278 static int hw_dev_open(struct sr_dev_inst *sdi)
280 struct dev_context *devc;
285 if ((fd = open(devc->device, O_RDWR)) == -1)
295 static int hw_dev_close(struct sr_dev_inst *sdi)
297 struct dev_context *devc;
306 static int hw_cleanup(void)
313 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
319 case SR_CONF_NUM_TIMEBASE:
320 *data = g_variant_new_int32(NUM_TIMEBASE);
322 case SR_CONF_NUM_VDIV:
323 *data = g_variant_new_int32(NUM_VDIV);
332 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
334 struct dev_context *devc;
335 uint64_t tmp_u64, p, q;
339 const char *tmp_str, *channel;
343 if (sdi->status != SR_ST_ACTIVE) {
344 sr_err("Device inactive, can't set config options.");
350 case SR_CONF_LIMIT_FRAMES:
351 devc->limit_frames = g_variant_get_uint64(data);
353 case SR_CONF_TRIGGER_SLOPE:
354 tmp_u64 = g_variant_get_uint64(data);
355 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s",
356 tmp_u64 ? "POS" : "NEG");
358 case SR_CONF_HORIZ_TRIGGERPOS:
359 tmp_double = g_variant_get_double(data);
360 rigol_ds1xx2_send_data(devc->fd, ":TIM:OFFS %.9f", tmp_double);
362 case SR_CONF_TIMEBASE:
363 g_variant_get(data, "(tt)", &p, &q);
365 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
366 if (timebases[i][0] == p && timebases[i][1] == q) {
372 rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f",
373 (float)timebases[i][0] / timebases[i][1]);
375 case SR_CONF_TRIGGER_SOURCE:
376 tmp_str = g_variant_get_string(data, NULL);
377 if (!strcmp(tmp_str, "CH1"))
379 else if (!strcmp(tmp_str, "CH2"))
381 else if (!strcmp(tmp_str, "EXT"))
383 else if (!strcmp(tmp_str, "AC Line"))
389 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s", channel);
392 g_variant_get(data, "(tt)", &p, &q);
394 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
395 if (vdivs[i][0] != p || vdivs[i][1] != q)
397 devc->scale = (float)vdivs[i][0] / vdivs[i][1];
398 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL %.3f",
402 if (i == ARRAY_SIZE(vdivs))
405 case SR_CONF_COUPLING:
406 /* TODO: Not supporting coupling per channel yet. */
407 tmp_str = g_variant_get_string(data, NULL);
408 for (i = 0; i < ARRAY_SIZE(coupling); i++) {
409 if (!strcmp(tmp_str, coupling[i])) {
410 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:COUP %s",
412 rigol_ds1xx2_send_data(devc->fd, ":CHAN2:COUP %s",
417 if (i == ARRAY_SIZE(coupling))
421 sr_err("Unknown hardware capability: %d.", id);
429 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
431 GVariant *tuple, *rational[2];
438 case SR_CONF_DEVICE_OPTIONS:
439 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
440 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
442 case SR_CONF_COUPLING:
443 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
446 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
447 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
448 rational[0] = g_variant_new_uint64(vdivs[i][0]);
449 rational[1] = g_variant_new_uint64(vdivs[i][1]);
450 tuple = g_variant_new_tuple(rational, 2);
451 g_variant_builder_add_value(&gvb, tuple);
453 *data = g_variant_builder_end(&gvb);
455 case SR_CONF_TIMEBASE:
456 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
457 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
458 rational[0] = g_variant_new_uint64(timebases[i][0]);
459 rational[1] = g_variant_new_uint64(timebases[i][1]);
460 tuple = g_variant_new_tuple(rational, 2);
461 g_variant_builder_add_value(&gvb, tuple);
463 *data = g_variant_builder_end(&gvb);
465 case SR_CONF_TRIGGER_SOURCE:
466 *data = g_variant_new_strv(trigger_sources,
467 ARRAY_SIZE(trigger_sources));
476 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
479 struct dev_context *devc;
487 devc->num_frames = devc->num_frame_samples = 0;
489 sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi);
491 /* Send header packet to the session bus. */
492 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
494 /* Hardcoded to CH1 only. */
495 devc->enabled_probes = g_slist_append(NULL, sdi->probes->data);
496 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?");
497 len = read(devc->fd, buf, sizeof(buf));
499 devc->scale = atof(buf);
500 sr_dbg("Scale is %.3f.", devc->scale);
501 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:OFFS?");
502 len = read(devc->fd, buf, sizeof(buf));
504 devc->offset = atof(buf);
505 sr_dbg("Offset is %.6f.", devc->offset);
506 rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?");
511 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
513 struct dev_context *devc;
519 if (sdi->status != SR_ST_ACTIVE) {
520 sr_err("Device inactive, can't stop acquisition.");
524 sr_source_remove(devc->fd);
529 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
530 .name = "rigol-ds1xx2",
531 .longname = "Rigol DS1xx2",
534 .cleanup = hw_cleanup,
536 .dev_list = hw_dev_list,
537 .dev_clear = clear_instances,
538 .config_get = config_get,
539 .config_set = config_set,
540 .config_list = config_list,
541 .dev_open = hw_dev_open,
542 .dev_close = hw_dev_close,
543 .dev_acquisition_start = hw_dev_acquisition_start,
544 .dev_acquisition_stop = hw_dev_acquisition_stop,