]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/rigol-ds1xx2/api.c
serial: more serial processing flags to turn off
[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 *
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
20#include <fcntl.h>
21#include <unistd.h>
22#include <stdlib.h>
23#include <string.h>
24#include <glib.h>
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
27#include "protocol.h"
28
29static const int hwcaps[] = {
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,
38 0,
39};
40
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
113static const char *supported_models[] = {
114 "DS1052E",
115 "DS1102E",
116 "DS1052D",
117 "DS1102D"
118};
119
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
140 g_free(devc->device);
141 close(devc->fd);
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
152static int hw_init(struct sr_context *sr_ctx)
153{
154 struct drv_context *drvc;
155 (void)sr_ctx;
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
162 di->priv = drvc;
163
164 return SR_OK;
165}
166
167static GSList *hw_scan(GSList *options)
168{
169 struct drv_context *drvc;
170 struct sr_dev_inst *sdi;
171 struct dev_context *devc;
172 struct sr_probe *probe;
173 GSList *devices;
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 int len, num_tokens, fd, i;
181 const gchar *delimiter = ",";
182 gchar **tokens;
183 const char *manufacturer, *model, *version;
184 int num_models;
185 gboolean matched = FALSE;
186 char buf[256];
187
188 (void)options;
189
190 devices = NULL;
191 drvc = di->priv;
192 drvc->instances = NULL;
193
194 dir = g_dir_open("/sys/class/usb/", 0, NULL);
195
196 if (dir == NULL)
197 return NULL;
198
199 while ((dev_name = g_dir_read_name(dir)) != NULL) {
200 if (strncmp(dev_name, prefix, strlen(prefix)))
201 continue;
202
203 device = g_strconcat(dev_dir, dev_name, NULL);
204
205 fd = open(device, O_RDWR);
206 len = write(fd, idn_query, strlen(idn_query));
207 len = read(fd, buf, sizeof(buf));
208 close(fd);
209 if (len == 0) {
210 g_free(device);
211 return NULL;
212 }
213
214 buf[len] = 0;
215 tokens = g_strsplit(buf, delimiter, 0);
216 close(fd);
217 sr_dbg("response: %s %d [%s]", device, len, buf);
218
219 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
220
221 if (num_tokens < 4) {
222 g_strfreev(tokens);
223 g_free(device);
224 return NULL;
225 }
226
227 manufacturer = tokens[0];
228 model = tokens[1];
229 version = tokens[3];
230
231 if (strcmp(manufacturer, "Rigol Technologies")) {
232 g_strfreev(tokens);
233 g_free(device);
234 return NULL;
235 }
236
237 num_models = sizeof(supported_models) / sizeof(supported_models[0]);
238
239 for (i = 0; i < num_models; i++) {
240 if (!strcmp(model, supported_models[i])) {
241 matched = 1;
242 break;
243 }
244 }
245
246 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
247 manufacturer, model, version))) {
248 g_strfreev(tokens);
249 g_free(device);
250 return NULL;
251 }
252
253 g_strfreev(tokens);
254
255 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
256 sr_err("Device context malloc failed.");
257 g_free(device);
258 return NULL;
259 }
260
261 devc->device = device;
262
263 sdi->priv = devc;
264 sdi->driver = di;
265
266 for (i = 0; i < 2; i++) {
267 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
268 i == 0 ? "CH1" : "CH2")))
269 return NULL;
270 sdi->probes = g_slist_append(sdi->probes, probe);
271 }
272
273 drvc->instances = g_slist_append(drvc->instances, sdi);
274 devices = g_slist_append(devices, sdi);
275 }
276
277 g_dir_close(dir);
278
279 return devices;
280}
281
282static GSList *hw_dev_list(void)
283{
284 struct drv_context *drvc;
285
286 drvc = di->priv;
287
288 return drvc->instances;
289}
290
291static int hw_dev_open(struct sr_dev_inst *sdi)
292{
293 struct dev_context *devc;
294 int fd;
295
296 devc = sdi->priv;
297
298 if ((fd = open(devc->device, O_RDWR)) == -1)
299 return SR_ERR;
300
301 devc->fd = fd;
302
303 devc->scale = 1;
304
305 return SR_OK;
306}
307
308static int hw_dev_close(struct sr_dev_inst *sdi)
309{
310 struct dev_context *devc;
311
312 devc = sdi->priv;
313
314 close(devc->fd);
315
316 return SR_OK;
317}
318
319static int hw_cleanup(void)
320{
321 clear_instances();
322
323 return SR_OK;
324}
325
326static int hw_info_get(int info_id, const void **data,
327 const struct sr_dev_inst *sdi)
328{
329 (void)sdi;
330
331 switch (info_id) {
332 case SR_DI_HWCAPS:
333 *data = hwcaps;
334 break;
335 case SR_DI_TIMEBASES:
336 *data = timebases;
337 break;
338 case SR_DI_TRIGGER_SOURCES:
339 *data = trigger_sources;
340 break;
341 case SR_DI_VDIVS:
342 *data = vdivs;
343 break;
344 case SR_DI_COUPLING:
345 *data = coupling;
346 break;
347 default:
348 return SR_ERR_ARG;
349 }
350
351 return SR_OK;
352}
353
354static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
355 const void *value)
356{
357 struct dev_context *devc;
358 uint64_t tmp_u64;
359 float tmp_float;
360 struct sr_rational tmp_rat;
361 int ret, i, j;
362 char *channel;
363
364 devc = sdi->priv;
365
366 if (sdi->status != SR_ST_ACTIVE) {
367 sr_err("Device inactive, can't set config options.");
368 return SR_ERR;
369 }
370
371 ret = SR_OK;
372 switch (hwcap) {
373 case SR_CONF_LIMIT_FRAMES:
374 devc->limit_frames = *(const uint64_t *)value;
375 break;
376 case SR_CONF_TRIGGER_SLOPE:
377 tmp_u64 = *(const int *)value;
378 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s\n",
379 tmp_u64 ? "POS" : "NEG");
380 break;
381 case SR_CONF_HORIZ_TRIGGERPOS:
382 tmp_float = *(const float *)value;
383 rigol_ds1xx2_send_data(devc->fd, ":TIM:OFFS %.9f\n", tmp_float);
384 break;
385 case SR_CONF_TIMEBASE:
386 tmp_rat = *(const struct sr_rational *)value;
387 rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f\n",
388 (float)tmp_rat.p / tmp_rat.q);
389 break;
390 case SR_CONF_TRIGGER_SOURCE:
391 if (!strcmp(value, "CH1"))
392 channel = "CHAN1";
393 else if (!strcmp(value, "CH2"))
394 channel = "CHAN2";
395 else if (!strcmp(value, "EXT"))
396 channel = "EXT";
397 else if (!strcmp(value, "AC Line"))
398 channel = "ACL";
399 else {
400 ret = SR_ERR_ARG;
401 break;
402 }
403 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s\n", channel);
404 break;
405 case SR_CONF_VDIV:
406 /* TODO: Not supporting vdiv per channel yet. */
407 tmp_rat = *(const struct sr_rational *)value;
408 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
409 if (vdivs[i].p == tmp_rat.p
410 && vdivs[i].q == tmp_rat.q) {
411 devc->scale = (float)tmp_rat.p / tmp_rat.q;
412 for (j = 0; j < 2; j++)
413 rigol_ds1xx2_send_data(devc->fd,
414 ":CHAN%d:SCAL %.3f\n", j, devc->scale);
415 break;
416 }
417 }
418 if (vdivs[i].p == 0 && vdivs[i].q == 0)
419 ret = SR_ERR_ARG;
420 break;
421 case SR_CONF_COUPLING:
422 /* TODO: Not supporting coupling per channel yet. */
423 for (i = 0; coupling[i]; i++) {
424 if (!strcmp(value, coupling[i])) {
425 for (j = 0; j < 2; j++)
426 rigol_ds1xx2_send_data(devc->fd,
427 ":CHAN%d:COUP %s\n", j, coupling[i]);
428 break;
429 }
430 }
431 if (coupling[i] == 0)
432 ret = SR_ERR_ARG;
433 break;
434 default:
435 sr_err("Unknown hardware capability: %d.", hwcap);
436 ret = SR_ERR_ARG;
437 break;
438 }
439
440 return ret;
441}
442
443static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
444 void *cb_data)
445{
446 struct dev_context *devc;
447 struct sr_datafeed_packet packet;
448 struct sr_datafeed_header header;
449 char buf[256];
450 int len;
451
452 (void)cb_data;
453
454 devc = sdi->priv;
455
456 devc->num_frames = 0;
457
458 sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi);
459
460 /* Send header packet to the session bus. */
461 packet.type = SR_DF_HEADER;
462 packet.payload = (unsigned char *)&header;
463 header.feed_version = 1;
464 gettimeofday(&header.starttime, NULL);
465 sr_session_send(cb_data, &packet);
466
467 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?\n");
468 len = read(devc->fd, buf, sizeof(buf));
469 buf[len] = 0;
470 devc->scale = atof(buf);
471 sr_dbg("Scale is %.3f.", devc->scale);
472 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:OFFS?\n");
473 len = read(devc->fd, buf, sizeof(buf));
474 buf[len] = 0;
475 devc->offset = atof(buf);
476 sr_dbg("Offset is %.6f.", devc->offset);
477 rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n");
478
479 return SR_OK;
480}
481
482static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
483{
484 struct dev_context *devc;
485
486 (void)cb_data;
487
488 devc = sdi->priv;
489
490 if (sdi->status != SR_ST_ACTIVE) {
491 sr_err("Device inactive, can't stop acquisition.");
492 return SR_ERR;
493 }
494
495 sr_source_remove(devc->fd);
496
497 return SR_OK;
498}
499
500SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
501 .name = "rigol-ds1xx2",
502 .longname = "Rigol DS1xx2",
503 .api_version = 1,
504 .init = hw_init,
505 .cleanup = hw_cleanup,
506 .scan = hw_scan,
507 .dev_list = hw_dev_list,
508 .dev_clear = clear_instances,
509 .dev_open = hw_dev_open,
510 .dev_close = hw_dev_close,
511 .info_get = hw_info_get,
512 .dev_config_set = hw_dev_config_set,
513 .dev_acquisition_start = hw_dev_acquisition_start,
514 .dev_acquisition_stop = hw_dev_acquisition_stop,
515 .priv = NULL,
516};