]> sigrok.org Git - libsigrok.git/blame - hardware/rigol-ds1xx2/api.c
rigol-ds1xx2: More selective Rigol DS1xx2 scan
[libsigrok.git] / hardware / rigol-ds1xx2 / api.c
CommitLineData
f4816ac6
ML
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2012 Martin Ling <martin-git@earth.li>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
e0b7d23c
ML
20#include <fcntl.h>
21#include <unistd.h>
22#include <stdlib.h>
23#include <string.h>
f4816ac6
ML
24#include <glib.h>
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
27#include "protocol.h"
28
e0b7d23c
ML
29#define NUM_PROBES 2
30
31static const int hwcaps[] = {
32 SR_HWCAP_OSCILLOSCOPE,
33 SR_HWCAP_LIMIT_SAMPLES,
34 SR_HWCAP_TIMEBASE,
35 SR_HWCAP_TRIGGER_SOURCE,
36 SR_HWCAP_TRIGGER_SLOPE,
37 SR_HWCAP_HORIZ_TRIGGERPOS,
38 SR_HWCAP_VDIV,
39 SR_HWCAP_COUPLING,
40 0,
41};
42
43static const char *probe_names[] = {
44 "CH1", "CH2",
45 NULL,
46};
47
48static const struct sr_rational timebases[] = {
49 /* nanoseconds */
50 { 2, 1000000000 },
51 { 5, 1000000000 },
52 { 10, 1000000000 },
53 { 20, 1000000000 },
54 { 50, 1000000000 },
55 { 100, 1000000000 },
56 { 500, 1000000000 },
57 /* microseconds */
58 { 1, 1000000 },
59 { 2, 1000000 },
60 { 5, 1000000 },
61 { 10, 1000000 },
62 { 20, 1000000 },
63 { 50, 1000000 },
64 { 100, 1000000 },
65 { 200, 1000000 },
66 { 500, 1000000 },
67 /* milliseconds */
68 { 1, 1000 },
69 { 2, 1000 },
70 { 5, 1000 },
71 { 10, 1000 },
72 { 20, 1000 },
73 { 50, 1000 },
74 { 100, 1000 },
75 { 200, 1000 },
76 { 500, 1000 },
77 /* seconds */
78 { 1, 1 },
79 { 2, 1 },
80 { 5, 1 },
81 { 10, 1 },
82 { 20, 1 },
83 { 50, 1 },
84 { 0, 0},
85};
86
87static const struct sr_rational vdivs[] = {
88 /* millivolts */
89 { 2, 1000 },
90 { 5, 1000 },
91 { 10, 1000 },
92 { 20, 1000 },
93 { 50, 1000 },
94 { 100, 1000 },
95 { 200, 1000 },
96 { 500, 1000 },
97 /* volts */
98 { 1, 1 },
99 { 2, 1 },
100 { 5, 1 },
101 { 10, 1 },
102 { 0, 0 },
103};
104
105static const char *trigger_sources[] = {
106 "CH1",
107 "CH2",
108 "EXT",
109 "AC Line",
110 NULL,
111};
112
113static const char *coupling[] = {
114 "AC",
115 "DC",
116 "GND",
117 NULL,
118};
119
512bb890
BV
120static const char *supported_models[] = {
121 "DS1052E",
122 "DS1102E",
123 "DS1052D",
124 "DS1102D"
125};
126
f4816ac6
ML
127SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info;
128static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info;
129
130/* Properly close and free all devices. */
131static int clear_instances(void)
132{
133 struct sr_dev_inst *sdi;
134 struct drv_context *drvc;
135 struct dev_context *devc;
136 GSList *l;
137
138 if (!(drvc = di->priv))
139 return SR_OK;
140
141 for (l = drvc->instances; l; l = l->next) {
142 if (!(sdi = l->data))
143 continue;
144 if (!(devc = sdi->priv))
145 continue;
146
fb6e5ba8 147 g_free(devc->device);
e0b7d23c 148 close(devc->fd);
f4816ac6
ML
149
150 sr_dev_inst_free(sdi);
151 }
152
153 g_slist_free(drvc->instances);
154 drvc->instances = NULL;
155
156 return SR_OK;
157}
158
e0b7d23c 159static int hw_init(struct sr_context *sr_ctx)
f4816ac6
ML
160{
161 struct drv_context *drvc;
e0b7d23c 162 (void)sr_ctx;
f4816ac6
ML
163
164 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
165 sr_err("Driver context malloc failed.");
166 return SR_ERR_MALLOC;
167 }
168
f4816ac6
ML
169 di->priv = drvc;
170
171 return SR_OK;
172}
173
174static GSList *hw_scan(GSList *options)
175{
176 struct drv_context *drvc;
e0b7d23c
ML
177 struct sr_dev_inst *sdi;
178 struct dev_context *devc;
179 struct sr_probe *probe;
f4816ac6 180 GSList *devices;
fb6e5ba8
ML
181 GDir *dir;
182 const gchar *dev_name;
183 const gchar *dev_dir = "/dev/";
184 const gchar *prefix = "usbtmc";
185 gchar *device;
186 const gchar *idn_query = "*IDN?";
29d957ce 187 int len, num_tokens, fd, i;
fb6e5ba8
ML
188 const gchar *delimiter = ",";
189 gchar **tokens;
512bb890
BV
190 const char *manufacturer, *model, *version;
191 int num_models;
192 gboolean matched = FALSE;
fb6e5ba8
ML
193 char buf[256];
194
f4816ac6
ML
195 (void)options;
196
197 devices = NULL;
198 drvc = di->priv;
199 drvc->instances = NULL;
200
fb6e5ba8 201 dir = g_dir_open("/sys/class/usb/", 0, NULL);
e0b7d23c 202
fb6e5ba8
ML
203 if (dir == NULL)
204 return NULL;
e0b7d23c 205
29d957ce 206 while ((dev_name = g_dir_read_name(dir)) != NULL) {
fb6e5ba8
ML
207 if (strncmp(dev_name, prefix, strlen(prefix)))
208 continue;
209
210 device = g_strconcat(dev_dir, dev_name, NULL);
211
212 fd = open(device, O_RDWR);
213 len = write(fd, idn_query, strlen(idn_query));
214 len = read(fd, buf, sizeof(buf));
215 close(fd);
29d957ce 216 if (len == 0) {
fb6e5ba8
ML
217 g_free(device);
218 return NULL;
219 }
220
221 buf[len] = 0;
222 tokens = g_strsplit(buf, delimiter, 0);
223 close(fd);
512bb890 224 sr_dbg("response: %s %d [%s]", device, len, buf);
fb6e5ba8
ML
225
226 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
227
512bb890 228 if (num_tokens < 4) {
fb6e5ba8
ML
229 g_strfreev(tokens);
230 g_free(device);
e0b7d23c 231 return NULL;
fb6e5ba8 232 }
512bb890
BV
233
234 manufacturer = tokens[0];
235 model = tokens[1];
236 version = tokens[3];
237
238 if (strcmp(manufacturer, "Rigol Technologies")) {
239 g_strfreev(tokens);
240 g_free(device);
241 return NULL;
242 }
243
244 num_models = sizeof(supported_models) / sizeof(supported_models[0]);
245
246 for (i = 0; i < num_models; i++) {
247 if (!strcmp(model, supported_models[i])) {
248 matched = 1;
249 break;
250 }
251 }
252
253 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
254 manufacturer, model, version))) {
255 g_strfreev(tokens);
256 g_free(device);
257 return NULL;
258 }
259
fb6e5ba8
ML
260 g_strfreev(tokens);
261
29d957ce
UH
262 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
263 sr_err("Device context malloc failed.");
fb6e5ba8
ML
264 g_free(device);
265 return NULL;
266 }
267
268 devc->device = device;
269
270 sdi->priv = devc;
271 sdi->driver = di;
272
29d957ce
UH
273 for (i = 0; i < 2; i++) {
274 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
275 i == 0 ? "CH1" : "CH2")))
fb6e5ba8
ML
276 return NULL;
277 sdi->probes = g_slist_append(sdi->probes, probe);
278 }
279
280 drvc->instances = g_slist_append(drvc->instances, sdi);
281 devices = g_slist_append(devices, sdi);
e0b7d23c 282 }
fb6e5ba8
ML
283
284 g_dir_close(dir);
f4816ac6
ML
285
286 return devices;
287}
288
289static GSList *hw_dev_list(void)
290{
291 struct drv_context *drvc;
292
293 drvc = di->priv;
294
295 return drvc->instances;
296}
297
298static int hw_dev_open(struct sr_dev_inst *sdi)
299{
29d957ce
UH
300 struct dev_context *devc;
301 int fd;
fb6e5ba8 302
29d957ce 303 devc = sdi->priv;
e0b7d23c 304
29d957ce 305 if ((fd = open(devc->device, O_RDWR)) == -1)
e0b7d23c
ML
306 return SR_ERR;
307
e0b7d23c
ML
308 devc->fd = fd;
309
310 devc->scale = 1;
f4816ac6
ML
311
312 return SR_OK;
313}
314
315static int hw_dev_close(struct sr_dev_inst *sdi)
316{
29d957ce
UH
317 struct dev_context *devc;
318
319 devc = sdi->priv;
e0b7d23c
ML
320
321 close(devc->fd);
f4816ac6
ML
322
323 return SR_OK;
324}
325
326static int hw_cleanup(void)
327{
328 clear_instances();
329
f4816ac6
ML
330 return SR_OK;
331}
332
333static int hw_info_get(int info_id, const void **data,
334 const struct sr_dev_inst *sdi)
335{
e0b7d23c
ML
336 (void)sdi;
337
f4816ac6 338 switch (info_id) {
e0b7d23c
ML
339 case SR_DI_HWCAPS:
340 *data = hwcaps;
341 break;
342 case SR_DI_NUM_PROBES:
343 *data = GINT_TO_POINTER(NUM_PROBES);
344 break;
345 case SR_DI_PROBE_NAMES:
346 *data = probe_names;
347 break;
348 case SR_DI_TIMEBASES:
349 *data = timebases;
350 break;
351 case SR_DI_TRIGGER_SOURCES:
352 *data = trigger_sources;
353 break;
354 case SR_DI_VDIVS:
355 *data = vdivs;
356 break;
357 case SR_DI_COUPLING:
358 *data = coupling;
359 break;
f4816ac6
ML
360 default:
361 sr_err("Unknown info_id: %d.", info_id);
362 return SR_ERR_ARG;
363 }
364
365 return SR_OK;
366}
367
368static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
369 const void *value)
370{
29d957ce 371 struct dev_context *devc;
e0b7d23c
ML
372 uint64_t tmp_u64;
373 float tmp_float;
374 struct sr_rational tmp_rat;
375 int ret, i, j;
376 char *channel;
f4816ac6 377
29d957ce
UH
378 devc = sdi->priv;
379
f4816ac6
ML
380 if (sdi->status != SR_ST_ACTIVE) {
381 sr_err("Device inactive, can't set config options.");
382 return SR_ERR;
383 }
384
385 ret = SR_OK;
386 switch (hwcap) {
29d957ce 387 case SR_HWCAP_LIMIT_FRAMES:
e0b7d23c
ML
388 devc->limit_frames = *(const uint64_t *)value;
389 break;
390 case SR_HWCAP_TRIGGER_SLOPE:
391 tmp_u64 = *(const int *)value;
29d957ce
UH
392 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s\n",
393 tmp_u64 ? "POS" : "NEG");
e0b7d23c
ML
394 break;
395 case SR_HWCAP_HORIZ_TRIGGERPOS:
396 tmp_float = *(const float *)value;
542843f7 397 rigol_ds1xx2_send_data(devc->fd, ":TIM:OFFS %.9f\n", tmp_float);
e0b7d23c
ML
398 break;
399 case SR_HWCAP_TIMEBASE:
400 tmp_rat = *(const struct sr_rational *)value;
29d957ce
UH
401 rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f\n",
402 (float)tmp_rat.p / tmp_rat.q);
e0b7d23c
ML
403 break;
404 case SR_HWCAP_TRIGGER_SOURCE:
405 if (!strcmp(value, "CH1"))
406 channel = "CHAN1";
407 else if (!strcmp(value, "CH2"))
408 channel = "CHAN2";
409 else if (!strcmp(value, "EXT"))
410 channel = "EXT";
411 else if (!strcmp(value, "AC Line"))
412 channel = "ACL";
29d957ce 413 else {
e0b7d23c
ML
414 ret = SR_ERR_ARG;
415 break;
4e108ace
ML
416 }
417 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s\n", channel);
e0b7d23c
ML
418 break;
419 case SR_HWCAP_VDIV:
420 /* TODO: Not supporting vdiv per channel yet. */
421 tmp_rat = *(const struct sr_rational *)value;
422 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
423 if (vdivs[i].p == tmp_rat.p
424 && vdivs[i].q == tmp_rat.q) {
29d957ce 425 devc->scale = (float)tmp_rat.p / tmp_rat.q;
e0b7d23c 426 for (j = 0; j < 2; j++)
29d957ce
UH
427 rigol_ds1xx2_send_data(devc->fd,
428 ":CHAN%d:SCAL %.3f\n", j, devc->scale);
e0b7d23c
ML
429 break;
430 }
431 }
432 if (vdivs[i].p == 0 && vdivs[i].q == 0)
433 ret = SR_ERR_ARG;
434 break;
435 case SR_HWCAP_COUPLING:
436 /* TODO: Not supporting coupling per channel yet. */
437 for (i = 0; coupling[i]; i++) {
438 if (!strcmp(value, coupling[i])) {
439 for (j = 0; j < 2; j++)
29d957ce
UH
440 rigol_ds1xx2_send_data(devc->fd,
441 ":CHAN%d:COUP %s\n", j, coupling[i]);
e0b7d23c
ML
442 break;
443 }
444 }
445 if (coupling[i] == 0)
446 ret = SR_ERR_ARG;
447 break;
f4816ac6
ML
448 default:
449 sr_err("Unknown hardware capability: %d.", hwcap);
450 ret = SR_ERR_ARG;
29d957ce 451 break;
f4816ac6
ML
452 }
453
454 return ret;
455}
456
457static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
458 void *cb_data)
459{
29d957ce 460 struct dev_context *devc;
e0b7d23c
ML
461 struct sr_datafeed_packet packet;
462 struct sr_datafeed_header header;
463 struct sr_datafeed_meta_analog meta;
464 char buf[256];
465 int len;
29d957ce 466
e0b7d23c
ML
467 (void)cb_data;
468
29d957ce
UH
469 devc = sdi->priv;
470
e0b7d23c
ML
471 devc->num_frames = 0;
472
473 sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi);
474
475 /* Send header packet to the session bus. */
476 packet.type = SR_DF_HEADER;
477 packet.payload = (unsigned char *)&header;
478 header.feed_version = 1;
479 gettimeofday(&header.starttime, NULL);
480 sr_session_send(cb_data, &packet);
481
482 /* Send metadata about the SR_DF_ANALOG packets to come. */
483 packet.type = SR_DF_META_ANALOG;
484 packet.payload = &meta;
485 meta.num_probes = NUM_PROBES;
486 sr_session_send(cb_data, &packet);
487
488 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?\n");
489 len = read(devc->fd, buf, sizeof(buf));
490 buf[len] = 0;
491 devc->scale = atof(buf);
29d957ce 492 sr_dbg("Scale is %.3f.", devc->scale);
e0b7d23c
ML
493 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:OFFS?\n");
494 len = read(devc->fd, buf, sizeof(buf));
495 buf[len] = 0;
496 devc->offset = atof(buf);
29d957ce 497 sr_dbg("Offset is %.6f.", devc->offset);
e0b7d23c 498 rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n");
f4816ac6
ML
499
500 return SR_OK;
501}
502
503static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
504{
29d957ce
UH
505 struct dev_context *devc;
506
f4816ac6
ML
507 (void)cb_data;
508
29d957ce
UH
509 devc = sdi->priv;
510
f4816ac6
ML
511 if (sdi->status != SR_ST_ACTIVE) {
512 sr_err("Device inactive, can't stop acquisition.");
513 return SR_ERR;
514 }
515
e0b7d23c 516 sr_source_remove(devc->fd);
f4816ac6
ML
517
518 return SR_OK;
519}
520
521SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
522 .name = "rigol-ds1xx2",
523 .longname = "Rigol DS1xx2",
524 .api_version = 1,
525 .init = hw_init,
526 .cleanup = hw_cleanup,
527 .scan = hw_scan,
528 .dev_list = hw_dev_list,
529 .dev_clear = clear_instances,
530 .dev_open = hw_dev_open,
531 .dev_close = hw_dev_close,
532 .info_get = hw_info_get,
533 .dev_config_set = hw_dev_config_set,
534 .dev_acquisition_start = hw_dev_acquisition_start,
535 .dev_acquisition_stop = hw_dev_acquisition_stop,
536 .priv = NULL,
537};