]>
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 ML |
29 | #define NUM_PROBES 2 |
30 | ||
31 | static 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 |
43 | static 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 | ||
82 | static 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 | ||
100 | static const char *trigger_sources[] = { | |
101 | "CH1", | |
102 | "CH2", | |
103 | "EXT", | |
104 | "AC Line", | |
105 | NULL, | |
106 | }; | |
107 | ||
108 | static const char *coupling[] = { | |
109 | "AC", | |
110 | "DC", | |
111 | "GND", | |
112 | NULL, | |
113 | }; | |
114 | ||
512bb890 BV |
115 | static const char *supported_models[] = { |
116 | "DS1052E", | |
117 | "DS1102E", | |
118 | "DS1052D", | |
119 | "DS1102D" | |
120 | }; | |
121 | ||
f4816ac6 ML |
122 | SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info; |
123 | static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info; | |
124 | ||
125 | /* Properly close and free all devices. */ | |
126 | static 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 | 154 | static 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 | ||
169 | static 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 | ||
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 | ||
328 | static 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 ML |
349 | default: |
350 | sr_err("Unknown info_id: %d.", info_id); | |
351 | return SR_ERR_ARG; | |
352 | } | |
353 | ||
354 | return SR_OK; | |
355 | } | |
356 | ||
357 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
358 | const void *value) | |
359 | { | |
29d957ce | 360 | struct dev_context *devc; |
e0b7d23c ML |
361 | uint64_t tmp_u64; |
362 | float tmp_float; | |
363 | struct sr_rational tmp_rat; | |
364 | int ret, i, j; | |
365 | char *channel; | |
f4816ac6 | 366 | |
29d957ce UH |
367 | devc = sdi->priv; |
368 | ||
f4816ac6 ML |
369 | if (sdi->status != SR_ST_ACTIVE) { |
370 | sr_err("Device inactive, can't set config options."); | |
371 | return SR_ERR; | |
372 | } | |
373 | ||
374 | ret = SR_OK; | |
375 | switch (hwcap) { | |
29d957ce | 376 | case SR_HWCAP_LIMIT_FRAMES: |
e0b7d23c ML |
377 | devc->limit_frames = *(const uint64_t *)value; |
378 | break; | |
379 | case SR_HWCAP_TRIGGER_SLOPE: | |
380 | tmp_u64 = *(const int *)value; | |
29d957ce UH |
381 | rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s\n", |
382 | tmp_u64 ? "POS" : "NEG"); | |
e0b7d23c ML |
383 | break; |
384 | case SR_HWCAP_HORIZ_TRIGGERPOS: | |
385 | tmp_float = *(const float *)value; | |
542843f7 | 386 | rigol_ds1xx2_send_data(devc->fd, ":TIM:OFFS %.9f\n", tmp_float); |
e0b7d23c ML |
387 | break; |
388 | case SR_HWCAP_TIMEBASE: | |
389 | tmp_rat = *(const struct sr_rational *)value; | |
29d957ce UH |
390 | rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f\n", |
391 | (float)tmp_rat.p / tmp_rat.q); | |
e0b7d23c ML |
392 | break; |
393 | case SR_HWCAP_TRIGGER_SOURCE: | |
394 | if (!strcmp(value, "CH1")) | |
395 | channel = "CHAN1"; | |
396 | else if (!strcmp(value, "CH2")) | |
397 | channel = "CHAN2"; | |
398 | else if (!strcmp(value, "EXT")) | |
399 | channel = "EXT"; | |
400 | else if (!strcmp(value, "AC Line")) | |
401 | channel = "ACL"; | |
29d957ce | 402 | else { |
e0b7d23c ML |
403 | ret = SR_ERR_ARG; |
404 | break; | |
4e108ace ML |
405 | } |
406 | rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s\n", channel); | |
e0b7d23c ML |
407 | break; |
408 | case SR_HWCAP_VDIV: | |
409 | /* TODO: Not supporting vdiv per channel yet. */ | |
410 | tmp_rat = *(const struct sr_rational *)value; | |
411 | for (i = 0; vdivs[i].p && vdivs[i].q; i++) { | |
412 | if (vdivs[i].p == tmp_rat.p | |
413 | && vdivs[i].q == tmp_rat.q) { | |
29d957ce | 414 | devc->scale = (float)tmp_rat.p / tmp_rat.q; |
e0b7d23c | 415 | for (j = 0; j < 2; j++) |
29d957ce UH |
416 | rigol_ds1xx2_send_data(devc->fd, |
417 | ":CHAN%d:SCAL %.3f\n", j, devc->scale); | |
e0b7d23c ML |
418 | break; |
419 | } | |
420 | } | |
421 | if (vdivs[i].p == 0 && vdivs[i].q == 0) | |
422 | ret = SR_ERR_ARG; | |
423 | break; | |
424 | case SR_HWCAP_COUPLING: | |
425 | /* TODO: Not supporting coupling per channel yet. */ | |
426 | for (i = 0; coupling[i]; i++) { | |
427 | if (!strcmp(value, coupling[i])) { | |
428 | for (j = 0; j < 2; j++) | |
29d957ce UH |
429 | rigol_ds1xx2_send_data(devc->fd, |
430 | ":CHAN%d:COUP %s\n", j, coupling[i]); | |
e0b7d23c ML |
431 | break; |
432 | } | |
433 | } | |
434 | if (coupling[i] == 0) | |
435 | ret = SR_ERR_ARG; | |
436 | break; | |
f4816ac6 ML |
437 | default: |
438 | sr_err("Unknown hardware capability: %d.", hwcap); | |
439 | ret = SR_ERR_ARG; | |
29d957ce | 440 | break; |
f4816ac6 ML |
441 | } |
442 | ||
443 | return ret; | |
444 | } | |
445 | ||
446 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
447 | void *cb_data) | |
448 | { | |
29d957ce | 449 | struct dev_context *devc; |
e0b7d23c ML |
450 | struct sr_datafeed_packet packet; |
451 | struct sr_datafeed_header header; | |
452 | struct sr_datafeed_meta_analog meta; | |
453 | char buf[256]; | |
454 | int len; | |
29d957ce | 455 | |
e0b7d23c ML |
456 | (void)cb_data; |
457 | ||
29d957ce UH |
458 | devc = sdi->priv; |
459 | ||
e0b7d23c ML |
460 | devc->num_frames = 0; |
461 | ||
462 | sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi); | |
463 | ||
464 | /* Send header packet to the session bus. */ | |
465 | packet.type = SR_DF_HEADER; | |
466 | packet.payload = (unsigned char *)&header; | |
467 | header.feed_version = 1; | |
468 | gettimeofday(&header.starttime, NULL); | |
469 | sr_session_send(cb_data, &packet); | |
470 | ||
471 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
472 | packet.type = SR_DF_META_ANALOG; | |
473 | packet.payload = &meta; | |
474 | meta.num_probes = NUM_PROBES; | |
475 | sr_session_send(cb_data, &packet); | |
476 | ||
477 | rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?\n"); | |
478 | len = read(devc->fd, buf, sizeof(buf)); | |
479 | buf[len] = 0; | |
480 | devc->scale = atof(buf); | |
29d957ce | 481 | sr_dbg("Scale is %.3f.", devc->scale); |
e0b7d23c ML |
482 | rigol_ds1xx2_send_data(devc->fd, ":CHAN1:OFFS?\n"); |
483 | len = read(devc->fd, buf, sizeof(buf)); | |
484 | buf[len] = 0; | |
485 | devc->offset = atof(buf); | |
29d957ce | 486 | sr_dbg("Offset is %.6f.", devc->offset); |
e0b7d23c | 487 | rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n"); |
f4816ac6 ML |
488 | |
489 | return SR_OK; | |
490 | } | |
491 | ||
492 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
493 | { | |
29d957ce UH |
494 | struct dev_context *devc; |
495 | ||
f4816ac6 ML |
496 | (void)cb_data; |
497 | ||
29d957ce UH |
498 | devc = sdi->priv; |
499 | ||
f4816ac6 ML |
500 | if (sdi->status != SR_ST_ACTIVE) { |
501 | sr_err("Device inactive, can't stop acquisition."); | |
502 | return SR_ERR; | |
503 | } | |
504 | ||
e0b7d23c | 505 | sr_source_remove(devc->fd); |
f4816ac6 ML |
506 | |
507 | return SR_OK; | |
508 | } | |
509 | ||
510 | SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = { | |
511 | .name = "rigol-ds1xx2", | |
512 | .longname = "Rigol DS1xx2", | |
513 | .api_version = 1, | |
514 | .init = hw_init, | |
515 | .cleanup = hw_cleanup, | |
516 | .scan = hw_scan, | |
517 | .dev_list = hw_dev_list, | |
518 | .dev_clear = clear_instances, | |
519 | .dev_open = hw_dev_open, | |
520 | .dev_close = hw_dev_close, | |
521 | .info_get = hw_info_get, | |
522 | .dev_config_set = hw_dev_config_set, | |
523 | .dev_acquisition_start = hw_dev_acquisition_start, | |
524 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
525 | .priv = NULL, | |
526 | }; |