]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/rigol-ds1xx2/api.c
rigol-ds1xx2: fix channel numbers
[libsigrok.git] / hardware / rigol-ds1xx2 / api.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2012 Martin Ling <martin-git@earth.li>
5 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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
21#include <fcntl.h>
22#include <unistd.h>
23#include <stdlib.h>
24#include <string.h>
25#include <glib.h>
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
28#include "protocol.h"
29
30#define NUM_TIMEBASE 12
31#define NUM_VDIV 8
32
33static const int32_t hwcaps[] = {
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,
42 SR_CONF_NUM_TIMEBASE,
43 SR_CONF_NUM_VDIV,
44};
45
46static const uint64_t timebases[][2] = {
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 },
82};
83
84static const uint64_t vdivs[][2] = {
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 },
99};
100
101static const char *trigger_sources[] = {
102 "CH1",
103 "CH2",
104 "EXT",
105 "AC Line",
106};
107
108static const char *coupling[] = {
109 "AC",
110 "DC",
111 "GND",
112};
113
114static const char *supported_models[] = {
115 "DS1052E",
116 "DS1102E",
117 "DS1052D",
118 "DS1102D",
119};
120
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
141 g_free(devc->device);
142 g_slist_free(devc->enabled_probes);
143 close(devc->fd);
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
154static int hw_init(struct sr_context *sr_ctx)
155{
156 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
157}
158
159static GSList *hw_scan(GSList *options)
160{
161 struct drv_context *drvc;
162 struct sr_dev_inst *sdi;
163 struct dev_context *devc;
164 struct sr_probe *probe;
165 GSList *devices;
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?";
172 unsigned int i;
173 int len, num_tokens, fd;
174 const gchar *delimiter = ",";
175 gchar **tokens;
176 const char *manufacturer, *model, *version;
177 gboolean matched = FALSE;
178 char buf[256];
179
180 (void)options;
181
182 drvc = di->priv;
183 drvc->instances = NULL;
184
185 devices = NULL;
186
187 dir = g_dir_open("/sys/class/usb/", 0, NULL);
188
189 if (dir == NULL)
190 return NULL;
191
192 while ((dev_name = g_dir_read_name(dir)) != NULL) {
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);
202 if (len == 0) {
203 g_free(device);
204 return NULL;
205 }
206
207 buf[len] = 0;
208 tokens = g_strsplit(buf, delimiter, 0);
209 close(fd);
210 sr_dbg("response: %s %d [%s]", device, len, buf);
211
212 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
213
214 if (num_tokens < 4) {
215 g_strfreev(tokens);
216 g_free(device);
217 return NULL;
218 }
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
230 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
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
244 g_strfreev(tokens);
245
246 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
247 sr_err("Device context malloc failed.");
248 g_free(device);
249 return NULL;
250 }
251
252 devc->device = device;
253
254 sdi->priv = devc;
255 sdi->driver = di;
256
257 for (i = 0; i < 2; i++) {
258 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
259 i == 0 ? "CH1" : "CH2")))
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);
266 }
267
268 g_dir_close(dir);
269
270 return devices;
271}
272
273static GSList *hw_dev_list(void)
274{
275 return ((struct drv_context *)(di->priv))->instances;
276}
277
278static int hw_dev_open(struct sr_dev_inst *sdi)
279{
280 struct dev_context *devc;
281 int fd;
282
283 devc = sdi->priv;
284
285 if ((fd = open(devc->device, O_RDWR)) == -1)
286 return SR_ERR;
287
288 devc->fd = fd;
289
290 devc->scale = 1;
291
292 return SR_OK;
293}
294
295static int hw_dev_close(struct sr_dev_inst *sdi)
296{
297 struct dev_context *devc;
298
299 devc = sdi->priv;
300
301 close(devc->fd);
302
303 return SR_OK;
304}
305
306static int hw_cleanup(void)
307{
308 clear_instances();
309
310 return SR_OK;
311}
312
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
332static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
333{
334 struct dev_context *devc;
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;
340
341 devc = sdi->priv;
342
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;
349 switch (id) {
350 case SR_CONF_LIMIT_FRAMES:
351 devc->limit_frames = g_variant_get_uint64(data);
352 break;
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");
357 break;
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);
361 break;
362 case SR_CONF_TIMEBASE:
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)
372 rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f",
373 (float)timebases[i][0] / timebases[i][1]);
374 break;
375 case SR_CONF_TRIGGER_SOURCE:
376 tmp_str = g_variant_get_string(data, NULL);
377 if (!strcmp(tmp_str, "CH1"))
378 channel = "CHAN1";
379 else if (!strcmp(tmp_str, "CH2"))
380 channel = "CHAN2";
381 else if (!strcmp(tmp_str, "EXT"))
382 channel = "EXT";
383 else if (!strcmp(tmp_str, "AC Line"))
384 channel = "ACL";
385 else {
386 ret = SR_ERR_ARG;
387 break;
388 }
389 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s", channel);
390 break;
391 case SR_CONF_VDIV:
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];
398 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL %.3f",
399 devc->scale);
400 break;
401 }
402 if (i == ARRAY_SIZE(vdivs))
403 ret = SR_ERR_ARG;
404 break;
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",
411 coupling[i]);
412 rigol_ds1xx2_send_data(devc->fd, ":CHAN2:COUP %s",
413 coupling[i]);
414 break;
415 }
416 }
417 if (i == ARRAY_SIZE(coupling))
418 ret = SR_ERR_ARG;
419 break;
420 default:
421 sr_err("Unknown hardware capability: %d.", id);
422 ret = SR_ERR_ARG;
423 break;
424 }
425
426 return ret;
427}
428
429static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
430{
431 GVariant *tuple, *rational[2];
432 GVariantBuilder gvb;
433 unsigned int i;
434
435 (void)sdi;
436
437 switch (key) {
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));
441 break;
442 case SR_CONF_COUPLING:
443 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
444 break;
445 case SR_CONF_VDIV:
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);
454 break;
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);
462 }
463 *data = g_variant_builder_end(&gvb);
464 break;
465 case SR_CONF_TRIGGER_SOURCE:
466 *data = g_variant_new_strv(trigger_sources,
467 ARRAY_SIZE(trigger_sources));
468 break;
469 default:
470 return SR_ERR_ARG;
471 }
472
473 return SR_OK;
474}
475
476static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
477 void *cb_data)
478{
479 struct dev_context *devc;
480 char buf[256];
481 int len;
482
483 (void)cb_data;
484
485 devc = sdi->priv;
486
487 devc->num_frames = devc->num_frame_samples = 0;
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. */
492 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
493
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));
498 buf[len] = 0;
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));
503 buf[len] = 0;
504 devc->offset = atof(buf);
505 sr_dbg("Offset is %.6f.", devc->offset);
506 rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?");
507
508 return SR_OK;
509}
510
511static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
512{
513 struct dev_context *devc;
514
515 (void)cb_data;
516
517 devc = sdi->priv;
518
519 if (sdi->status != SR_ST_ACTIVE) {
520 sr_err("Device inactive, can't stop acquisition.");
521 return SR_ERR;
522 }
523
524 sr_source_remove(devc->fd);
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,
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,
545 .priv = NULL,
546};