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