]> sigrok.org Git - libsigrok.git/blame - hardware/rigol-ds1xx2/api.c
rigol-ds1xx2: Autoprobe for usbtmc devices on Linux.
[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
f4816ac6
ML
120SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info;
121static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info;
122
123/* Properly close and free all devices. */
124static int clear_instances(void)
125{
126 struct sr_dev_inst *sdi;
127 struct drv_context *drvc;
128 struct dev_context *devc;
129 GSList *l;
130
131 if (!(drvc = di->priv))
132 return SR_OK;
133
134 for (l = drvc->instances; l; l = l->next) {
135 if (!(sdi = l->data))
136 continue;
137 if (!(devc = sdi->priv))
138 continue;
139
fb6e5ba8 140 g_free(devc->device);
e0b7d23c 141 close(devc->fd);
f4816ac6
ML
142
143 sr_dev_inst_free(sdi);
144 }
145
146 g_slist_free(drvc->instances);
147 drvc->instances = NULL;
148
149 return SR_OK;
150}
151
e0b7d23c 152static int hw_init(struct sr_context *sr_ctx)
f4816ac6
ML
153{
154 struct drv_context *drvc;
e0b7d23c 155 (void)sr_ctx;
f4816ac6
ML
156
157 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
158 sr_err("Driver context malloc failed.");
159 return SR_ERR_MALLOC;
160 }
161
f4816ac6
ML
162 di->priv = drvc;
163
164 return SR_OK;
165}
166
167static GSList *hw_scan(GSList *options)
168{
169 struct drv_context *drvc;
e0b7d23c
ML
170 struct sr_dev_inst *sdi;
171 struct dev_context *devc;
172 struct sr_probe *probe;
f4816ac6 173 GSList *devices;
fb6e5ba8
ML
174 GDir *dir;
175 const gchar *dev_name;
176 const gchar *dev_dir = "/dev/";
177 const gchar *prefix = "usbtmc";
178 gchar *device;
179 const gchar *idn_query = "*IDN?";
180 gchar *idn_reply;
181 const gchar *idn_reply_prefix = "*IDN ";
182 int len;
183 const gchar *delimiter = ",";
184 gchar **tokens;
185 int num_tokens;
186 int fd;
187 char buf[256];
188
e0b7d23c 189 int i;
f4816ac6
ML
190
191 (void)options;
192
193 devices = NULL;
194 drvc = di->priv;
195 drvc->instances = NULL;
196
fb6e5ba8 197 dir = g_dir_open("/sys/class/usb/", 0, NULL);
e0b7d23c 198
fb6e5ba8
ML
199 if (dir == NULL)
200 return NULL;
e0b7d23c 201
fb6e5ba8 202 while ((dev_name = g_dir_read_name(dir)) != NULL)
e0b7d23c 203 {
fb6e5ba8
ML
204 if (strncmp(dev_name, prefix, strlen(prefix)))
205 continue;
206
207 device = g_strconcat(dev_dir, dev_name, NULL);
208
209 fd = open(device, O_RDWR);
210 len = write(fd, idn_query, strlen(idn_query));
211 len = read(fd, buf, sizeof(buf));
212 close(fd);
213 if (len == 0)
214 {
215 g_free(device);
216 return NULL;
217 }
218
219 buf[len] = 0;
220 tokens = g_strsplit(buf, delimiter, 0);
221 close(fd);
222
223 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
224
225 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, tokens[0],
226 num_tokens > 1 ? tokens[1] : NULL, num_tokens > 3 ? tokens[3] : NULL)))
227 {
228 g_strfreev(tokens);
229 g_free(device);
e0b7d23c 230 return NULL;
fb6e5ba8
ML
231 }
232 g_strfreev(tokens);
233
234 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
235 {
236 g_free(device);
237 return NULL;
238 }
239
240 devc->device = device;
241
242 sdi->priv = devc;
243 sdi->driver = di;
244
245 for (i = 0; i < 2; i++)
246 {
247 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, i == 0 ? "CH1" : "CH2")))
248 return NULL;
249 sdi->probes = g_slist_append(sdi->probes, probe);
250 }
251
252 drvc->instances = g_slist_append(drvc->instances, sdi);
253 devices = g_slist_append(devices, sdi);
e0b7d23c 254 }
fb6e5ba8
ML
255
256 g_dir_close(dir);
f4816ac6
ML
257
258 return devices;
259}
260
261static GSList *hw_dev_list(void)
262{
263 struct drv_context *drvc;
264
265 drvc = di->priv;
266
267 return drvc->instances;
268}
269
270static int hw_dev_open(struct sr_dev_inst *sdi)
271{
fb6e5ba8
ML
272 struct dev_context *devc = sdi->priv;
273
274 int fd = open(devc->device, O_RDWR);
e0b7d23c
ML
275
276 if (fd == -1)
277 return SR_ERR;
278
e0b7d23c
ML
279 devc->fd = fd;
280
281 devc->scale = 1;
f4816ac6
ML
282
283 return SR_OK;
284}
285
286static int hw_dev_close(struct sr_dev_inst *sdi)
287{
e0b7d23c
ML
288 struct dev_context *devc = sdi->priv;
289
290 close(devc->fd);
f4816ac6
ML
291
292 return SR_OK;
293}
294
295static int hw_cleanup(void)
296{
297 clear_instances();
298
f4816ac6
ML
299 return SR_OK;
300}
301
302static int hw_info_get(int info_id, const void **data,
303 const struct sr_dev_inst *sdi)
304{
e0b7d23c
ML
305 (void)sdi;
306
f4816ac6 307 switch (info_id) {
e0b7d23c
ML
308 case SR_DI_HWCAPS:
309 *data = hwcaps;
310 break;
311 case SR_DI_NUM_PROBES:
312 *data = GINT_TO_POINTER(NUM_PROBES);
313 break;
314 case SR_DI_PROBE_NAMES:
315 *data = probe_names;
316 break;
317 case SR_DI_TIMEBASES:
318 *data = timebases;
319 break;
320 case SR_DI_TRIGGER_SOURCES:
321 *data = trigger_sources;
322 break;
323 case SR_DI_VDIVS:
324 *data = vdivs;
325 break;
326 case SR_DI_COUPLING:
327 *data = coupling;
328 break;
f4816ac6
ML
329 default:
330 sr_err("Unknown info_id: %d.", info_id);
331 return SR_ERR_ARG;
332 }
333
334 return SR_OK;
335}
336
337static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
338 const void *value)
339{
e0b7d23c
ML
340 struct dev_context *devc = sdi->priv;
341 uint64_t tmp_u64;
342 float tmp_float;
343 struct sr_rational tmp_rat;
344 int ret, i, j;
345 char *channel;
f4816ac6
ML
346
347 if (sdi->status != SR_ST_ACTIVE) {
348 sr_err("Device inactive, can't set config options.");
349 return SR_ERR;
350 }
351
352 ret = SR_OK;
353 switch (hwcap) {
e0b7d23c
ML
354 case SR_HWCAP_LIMIT_FRAMES:
355 devc->limit_frames = *(const uint64_t *)value;
356 break;
357 case SR_HWCAP_TRIGGER_SLOPE:
358 tmp_u64 = *(const int *)value;
4e108ace 359 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s\n", tmp_u64 ? "POS" : "NEG");
e0b7d23c
ML
360 break;
361 case SR_HWCAP_HORIZ_TRIGGERPOS:
362 tmp_float = *(const float *)value;
542843f7 363 rigol_ds1xx2_send_data(devc->fd, ":TIM:OFFS %.9f\n", tmp_float);
e0b7d23c
ML
364 break;
365 case SR_HWCAP_TIMEBASE:
366 tmp_rat = *(const struct sr_rational *)value;
542843f7 367 rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f\n", (float) tmp_rat.p / tmp_rat.q);
e0b7d23c
ML
368 break;
369 case SR_HWCAP_TRIGGER_SOURCE:
370 if (!strcmp(value, "CH1"))
371 channel = "CHAN1";
372 else if (!strcmp(value, "CH2"))
373 channel = "CHAN2";
374 else if (!strcmp(value, "EXT"))
375 channel = "EXT";
376 else if (!strcmp(value, "AC Line"))
377 channel = "ACL";
378 else
4e108ace 379 {
e0b7d23c
ML
380 ret = SR_ERR_ARG;
381 break;
4e108ace
ML
382 }
383 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s\n", channel);
e0b7d23c
ML
384 break;
385 case SR_HWCAP_VDIV:
386 /* TODO: Not supporting vdiv per channel yet. */
387 tmp_rat = *(const struct sr_rational *)value;
388 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
389 if (vdivs[i].p == tmp_rat.p
390 && vdivs[i].q == tmp_rat.q) {
391 devc->scale = (float) tmp_rat.p / tmp_rat.q;
392 for (j = 0; j < 2; j++)
542843f7 393 rigol_ds1xx2_send_data(devc->fd, ":CHAN%d:SCAL %.3f\n", j, devc->scale);
e0b7d23c
ML
394 break;
395 }
396 }
397 if (vdivs[i].p == 0 && vdivs[i].q == 0)
398 ret = SR_ERR_ARG;
399 break;
400 case SR_HWCAP_COUPLING:
401 /* TODO: Not supporting coupling per channel yet. */
402 for (i = 0; coupling[i]; i++) {
403 if (!strcmp(value, coupling[i])) {
404 for (j = 0; j < 2; j++)
542843f7 405 rigol_ds1xx2_send_data(devc->fd, ":CHAN%d:COUP %s\n", j, coupling[i]);
e0b7d23c
ML
406 break;
407 }
408 }
409 if (coupling[i] == 0)
410 ret = SR_ERR_ARG;
411 break;
f4816ac6
ML
412 default:
413 sr_err("Unknown hardware capability: %d.", hwcap);
414 ret = SR_ERR_ARG;
415 }
416
417 return ret;
418}
419
420static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
421 void *cb_data)
422{
e0b7d23c
ML
423 struct dev_context *devc = sdi->priv;
424 struct sr_datafeed_packet packet;
425 struct sr_datafeed_header header;
426 struct sr_datafeed_meta_analog meta;
427 char buf[256];
428 int len;
429 (void)cb_data;
430
431 devc->num_frames = 0;
432
433 sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi);
434
435 /* Send header packet to the session bus. */
436 packet.type = SR_DF_HEADER;
437 packet.payload = (unsigned char *)&header;
438 header.feed_version = 1;
439 gettimeofday(&header.starttime, NULL);
440 sr_session_send(cb_data, &packet);
441
442 /* Send metadata about the SR_DF_ANALOG packets to come. */
443 packet.type = SR_DF_META_ANALOG;
444 packet.payload = &meta;
445 meta.num_probes = NUM_PROBES;
446 sr_session_send(cb_data, &packet);
447
448 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?\n");
449 len = read(devc->fd, buf, sizeof(buf));
450 buf[len] = 0;
451 devc->scale = atof(buf);
542843f7 452 sr_dbg("scale is %.3f", devc->scale);
e0b7d23c
ML
453 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:OFFS?\n");
454 len = read(devc->fd, buf, sizeof(buf));
455 buf[len] = 0;
456 devc->offset = atof(buf);
542843f7 457 sr_dbg("offset is %.6f", devc->offset);
e0b7d23c 458 rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n");
f4816ac6
ML
459
460 return SR_OK;
461}
462
463static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
464{
e0b7d23c 465 struct dev_context *devc = sdi->priv;
f4816ac6
ML
466 (void)cb_data;
467
468 if (sdi->status != SR_ST_ACTIVE) {
469 sr_err("Device inactive, can't stop acquisition.");
470 return SR_ERR;
471 }
472
e0b7d23c 473 sr_source_remove(devc->fd);
f4816ac6
ML
474
475 return SR_OK;
476}
477
478SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
479 .name = "rigol-ds1xx2",
480 .longname = "Rigol DS1xx2",
481 .api_version = 1,
482 .init = hw_init,
483 .cleanup = hw_cleanup,
484 .scan = hw_scan,
485 .dev_list = hw_dev_list,
486 .dev_clear = clear_instances,
487 .dev_open = hw_dev_open,
488 .dev_close = hw_dev_close,
489 .info_get = hw_info_get,
490 .dev_config_set = hw_dev_config_set,
491 .dev_acquisition_start = hw_dev_acquisition_start,
492 .dev_acquisition_stop = hw_dev_acquisition_stop,
493 .priv = NULL,
494};