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