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