]>
Commit | Line | Data |
---|---|---|
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 | 29 | static 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 |
41 | static 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 | ||
80 | static 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 | ||
98 | static const char *trigger_sources[] = { | |
99 | "CH1", | |
100 | "CH2", | |
101 | "EXT", | |
102 | "AC Line", | |
103 | NULL, | |
104 | }; | |
105 | ||
106 | static const char *coupling[] = { | |
107 | "AC", | |
108 | "DC", | |
109 | "GND", | |
110 | NULL, | |
111 | }; | |
112 | ||
512bb890 BV |
113 | static const char *supported_models[] = { |
114 | "DS1052E", | |
115 | "DS1102E", | |
116 | "DS1052D", | |
117 | "DS1102D" | |
118 | }; | |
119 | ||
f4816ac6 ML |
120 | SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info; |
121 | static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info; | |
122 | ||
123 | /* Properly close and free all devices. */ | |
124 | static 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 | 153 | static int hw_init(struct sr_context *sr_ctx) |
f4816ac6 ML |
154 | { |
155 | struct drv_context *drvc; | |
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 | ||
c0eea11c | 162 | drvc->sr_ctx = sr_ctx; |
f4816ac6 ML |
163 | di->priv = drvc; |
164 | ||
165 | return SR_OK; | |
166 | } | |
167 | ||
168 | static GSList *hw_scan(GSList *options) | |
169 | { | |
170 | struct drv_context *drvc; | |
e0b7d23c ML |
171 | struct sr_dev_inst *sdi; |
172 | struct dev_context *devc; | |
173 | struct sr_probe *probe; | |
f4816ac6 | 174 | GSList *devices; |
fb6e5ba8 ML |
175 | GDir *dir; |
176 | const gchar *dev_name; | |
177 | const gchar *dev_dir = "/dev/"; | |
178 | const gchar *prefix = "usbtmc"; | |
179 | gchar *device; | |
180 | const gchar *idn_query = "*IDN?"; | |
29d957ce | 181 | int len, num_tokens, fd, i; |
fb6e5ba8 ML |
182 | const gchar *delimiter = ","; |
183 | gchar **tokens; | |
512bb890 BV |
184 | const char *manufacturer, *model, *version; |
185 | int num_models; | |
186 | gboolean matched = FALSE; | |
fb6e5ba8 ML |
187 | char buf[256]; |
188 | ||
f4816ac6 ML |
189 | (void)options; |
190 | ||
f4816ac6 ML |
191 | drvc = di->priv; |
192 | drvc->instances = NULL; | |
193 | ||
4b97c74e UH |
194 | devices = 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 | ||
284 | static GSList *hw_dev_list(void) | |
285 | { | |
286 | struct drv_context *drvc; | |
287 | ||
288 | drvc = di->priv; | |
289 | ||
290 | return drvc->instances; | |
291 | } | |
292 | ||
293 | static 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 | ||
310 | static 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 | ||
321 | static int hw_cleanup(void) | |
322 | { | |
323 | clear_instances(); | |
324 | ||
f4816ac6 ML |
325 | return SR_OK; |
326 | } | |
327 | ||
035a1078 | 328 | static int config_set(int id, const void *value, const struct sr_dev_inst *sdi) |
f4816ac6 | 329 | { |
29d957ce | 330 | struct dev_context *devc; |
e0b7d23c ML |
331 | uint64_t tmp_u64; |
332 | float tmp_float; | |
333 | struct sr_rational tmp_rat; | |
334 | int ret, i, j; | |
335 | char *channel; | |
f4816ac6 | 336 | |
29d957ce UH |
337 | devc = sdi->priv; |
338 | ||
f4816ac6 ML |
339 | if (sdi->status != SR_ST_ACTIVE) { |
340 | sr_err("Device inactive, can't set config options."); | |
341 | return SR_ERR; | |
342 | } | |
343 | ||
344 | ret = SR_OK; | |
035a1078 | 345 | switch (id) { |
1953564a | 346 | case SR_CONF_LIMIT_FRAMES: |
e0b7d23c ML |
347 | devc->limit_frames = *(const uint64_t *)value; |
348 | break; | |
1953564a | 349 | case SR_CONF_TRIGGER_SLOPE: |
e0b7d23c | 350 | tmp_u64 = *(const int *)value; |
29d957ce UH |
351 | rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s\n", |
352 | tmp_u64 ? "POS" : "NEG"); | |
e0b7d23c | 353 | break; |
1953564a | 354 | case SR_CONF_HORIZ_TRIGGERPOS: |
e0b7d23c | 355 | tmp_float = *(const float *)value; |
542843f7 | 356 | rigol_ds1xx2_send_data(devc->fd, ":TIM:OFFS %.9f\n", tmp_float); |
e0b7d23c | 357 | break; |
1953564a | 358 | case SR_CONF_TIMEBASE: |
e0b7d23c | 359 | tmp_rat = *(const struct sr_rational *)value; |
29d957ce UH |
360 | rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f\n", |
361 | (float)tmp_rat.p / tmp_rat.q); | |
e0b7d23c | 362 | break; |
1953564a | 363 | case SR_CONF_TRIGGER_SOURCE: |
e0b7d23c ML |
364 | if (!strcmp(value, "CH1")) |
365 | channel = "CHAN1"; | |
366 | else if (!strcmp(value, "CH2")) | |
367 | channel = "CHAN2"; | |
368 | else if (!strcmp(value, "EXT")) | |
369 | channel = "EXT"; | |
370 | else if (!strcmp(value, "AC Line")) | |
371 | channel = "ACL"; | |
29d957ce | 372 | else { |
e0b7d23c ML |
373 | ret = SR_ERR_ARG; |
374 | break; | |
4e108ace ML |
375 | } |
376 | rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s\n", channel); | |
e0b7d23c | 377 | break; |
1953564a | 378 | case SR_CONF_VDIV: |
e0b7d23c ML |
379 | /* TODO: Not supporting vdiv per channel yet. */ |
380 | tmp_rat = *(const struct sr_rational *)value; | |
381 | for (i = 0; vdivs[i].p && vdivs[i].q; i++) { | |
382 | if (vdivs[i].p == tmp_rat.p | |
383 | && vdivs[i].q == tmp_rat.q) { | |
29d957ce | 384 | devc->scale = (float)tmp_rat.p / tmp_rat.q; |
e0b7d23c | 385 | for (j = 0; j < 2; j++) |
29d957ce UH |
386 | rigol_ds1xx2_send_data(devc->fd, |
387 | ":CHAN%d:SCAL %.3f\n", j, devc->scale); | |
e0b7d23c ML |
388 | break; |
389 | } | |
390 | } | |
391 | if (vdivs[i].p == 0 && vdivs[i].q == 0) | |
392 | ret = SR_ERR_ARG; | |
393 | break; | |
1953564a | 394 | case SR_CONF_COUPLING: |
e0b7d23c ML |
395 | /* TODO: Not supporting coupling per channel yet. */ |
396 | for (i = 0; coupling[i]; i++) { | |
397 | if (!strcmp(value, coupling[i])) { | |
398 | for (j = 0; j < 2; j++) | |
29d957ce UH |
399 | rigol_ds1xx2_send_data(devc->fd, |
400 | ":CHAN%d:COUP %s\n", j, coupling[i]); | |
e0b7d23c ML |
401 | break; |
402 | } | |
403 | } | |
404 | if (coupling[i] == 0) | |
405 | ret = SR_ERR_ARG; | |
406 | break; | |
f4816ac6 | 407 | default: |
035a1078 | 408 | sr_err("Unknown hardware capability: %d.", id); |
f4816ac6 | 409 | ret = SR_ERR_ARG; |
29d957ce | 410 | break; |
f4816ac6 ML |
411 | } |
412 | ||
413 | return ret; | |
414 | } | |
415 | ||
a1c743fc BV |
416 | static int config_list(int key, const void **data, const struct sr_dev_inst *sdi) |
417 | { | |
418 | ||
419 | (void)sdi; | |
420 | ||
421 | switch (key) { | |
9a6517d1 BV |
422 | case SR_CONF_DEVICE_OPTIONS: |
423 | *data = hwcaps; | |
424 | break; | |
2a7b113d BV |
425 | case SR_CONF_COUPLING: |
426 | *data = coupling; | |
427 | break; | |
e4f2b2ad BV |
428 | case SR_CONF_VDIV: |
429 | *data = vdivs; | |
430 | break; | |
41f5bd09 BV |
431 | case SR_CONF_TIMEBASE: |
432 | *data = timebases; | |
433 | break; | |
328bafab BV |
434 | case SR_CONF_TRIGGER_SOURCE: |
435 | *data = trigger_sources; | |
436 | break; | |
a1c743fc BV |
437 | default: |
438 | return SR_ERR_ARG; | |
439 | } | |
440 | ||
441 | return SR_OK; | |
442 | } | |
443 | ||
f4816ac6 ML |
444 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
445 | void *cb_data) | |
446 | { | |
29d957ce | 447 | struct dev_context *devc; |
e0b7d23c ML |
448 | struct sr_datafeed_packet packet; |
449 | struct sr_datafeed_header header; | |
e0b7d23c ML |
450 | char buf[256]; |
451 | int len; | |
29d957ce | 452 | |
e0b7d23c ML |
453 | (void)cb_data; |
454 | ||
29d957ce UH |
455 | devc = sdi->priv; |
456 | ||
e0b7d23c ML |
457 | devc->num_frames = 0; |
458 | ||
459 | sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi); | |
460 | ||
461 | /* Send header packet to the session bus. */ | |
462 | packet.type = SR_DF_HEADER; | |
463 | packet.payload = (unsigned char *)&header; | |
464 | header.feed_version = 1; | |
465 | gettimeofday(&header.starttime, NULL); | |
466 | sr_session_send(cb_data, &packet); | |
467 | ||
69e19dd7 BV |
468 | /* Hardcoded to CH1 only. */ |
469 | devc->enabled_probes = g_slist_append(NULL, sdi->probes->data); | |
e0b7d23c ML |
470 | rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?\n"); |
471 | len = read(devc->fd, buf, sizeof(buf)); | |
472 | buf[len] = 0; | |
473 | devc->scale = atof(buf); | |
29d957ce | 474 | sr_dbg("Scale is %.3f.", devc->scale); |
e0b7d23c ML |
475 | rigol_ds1xx2_send_data(devc->fd, ":CHAN1:OFFS?\n"); |
476 | len = read(devc->fd, buf, sizeof(buf)); | |
477 | buf[len] = 0; | |
478 | devc->offset = atof(buf); | |
29d957ce | 479 | sr_dbg("Offset is %.6f.", devc->offset); |
e0b7d23c | 480 | rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n"); |
f4816ac6 ML |
481 | |
482 | return SR_OK; | |
483 | } | |
484 | ||
485 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
486 | { | |
29d957ce UH |
487 | struct dev_context *devc; |
488 | ||
f4816ac6 ML |
489 | (void)cb_data; |
490 | ||
29d957ce UH |
491 | devc = sdi->priv; |
492 | ||
f4816ac6 ML |
493 | if (sdi->status != SR_ST_ACTIVE) { |
494 | sr_err("Device inactive, can't stop acquisition."); | |
495 | return SR_ERR; | |
496 | } | |
497 | ||
e0b7d23c | 498 | sr_source_remove(devc->fd); |
f4816ac6 ML |
499 | |
500 | return SR_OK; | |
501 | } | |
502 | ||
503 | SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = { | |
504 | .name = "rigol-ds1xx2", | |
505 | .longname = "Rigol DS1xx2", | |
506 | .api_version = 1, | |
507 | .init = hw_init, | |
508 | .cleanup = hw_cleanup, | |
509 | .scan = hw_scan, | |
510 | .dev_list = hw_dev_list, | |
511 | .dev_clear = clear_instances, | |
035a1078 | 512 | .config_set = config_set, |
a1c743fc | 513 | .config_list = config_list, |
f4816ac6 ML |
514 | .dev_open = hw_dev_open, |
515 | .dev_close = hw_dev_close, | |
f4816ac6 ML |
516 | .dev_acquisition_start = hw_dev_acquisition_start, |
517 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
518 | .priv = NULL, | |
519 | }; |