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