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