]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Martin Ling <martin-git@earth.li> | |
5 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
6 | * Copyright (C) 2013 Mathias Grimmberger <mgri@zaphod.sax.de> | |
7 | * | |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, either version 3 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | #include <fcntl.h> | |
23 | #include <unistd.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <glib.h> | |
27 | #include "libsigrok.h" | |
28 | #include "libsigrok-internal.h" | |
29 | #include "protocol.h" | |
30 | ||
31 | static const int32_t hwopts[] = { | |
32 | SR_CONF_CONN, | |
33 | }; | |
34 | ||
35 | static const int32_t hwcaps[] = { | |
36 | SR_CONF_OSCILLOSCOPE, | |
37 | SR_CONF_TIMEBASE, | |
38 | SR_CONF_TRIGGER_SOURCE, | |
39 | SR_CONF_TRIGGER_SLOPE, | |
40 | SR_CONF_HORIZ_TRIGGERPOS, | |
41 | SR_CONF_NUM_TIMEBASE, | |
42 | }; | |
43 | ||
44 | static const int32_t analog_hwcaps[] = { | |
45 | SR_CONF_NUM_VDIV, | |
46 | SR_CONF_VDIV, | |
47 | SR_CONF_COUPLING, | |
48 | }; | |
49 | ||
50 | static const uint64_t timebases[][2] = { | |
51 | /* nanoseconds */ | |
52 | { 2, 1000000000 }, | |
53 | { 5, 1000000000 }, | |
54 | { 10, 1000000000 }, | |
55 | { 20, 1000000000 }, | |
56 | { 50, 1000000000 }, | |
57 | { 100, 1000000000 }, | |
58 | { 500, 1000000000 }, | |
59 | /* microseconds */ | |
60 | { 1, 1000000 }, | |
61 | { 2, 1000000 }, | |
62 | { 5, 1000000 }, | |
63 | { 10, 1000000 }, | |
64 | { 20, 1000000 }, | |
65 | { 50, 1000000 }, | |
66 | { 100, 1000000 }, | |
67 | { 200, 1000000 }, | |
68 | { 500, 1000000 }, | |
69 | /* milliseconds */ | |
70 | { 1, 1000 }, | |
71 | { 2, 1000 }, | |
72 | { 5, 1000 }, | |
73 | { 10, 1000 }, | |
74 | { 20, 1000 }, | |
75 | { 50, 1000 }, | |
76 | { 100, 1000 }, | |
77 | { 200, 1000 }, | |
78 | { 500, 1000 }, | |
79 | /* seconds */ | |
80 | { 1, 1 }, | |
81 | { 2, 1 }, | |
82 | { 5, 1 }, | |
83 | { 10, 1 }, | |
84 | { 20, 1 }, | |
85 | { 50, 1 }, | |
86 | { 100, 1 }, | |
87 | { 200, 1 }, | |
88 | { 500, 1 }, | |
89 | /* { 1000, 1 }, Confuses other code? */ | |
90 | }; | |
91 | ||
92 | static const uint64_t vdivs[][2] = { | |
93 | /* microvolts */ | |
94 | { 500, 1000000 }, | |
95 | /* millivolts */ | |
96 | { 1, 1000 }, | |
97 | { 2, 1000 }, | |
98 | { 5, 1000 }, | |
99 | { 10, 1000 }, | |
100 | { 20, 1000 }, | |
101 | { 50, 1000 }, | |
102 | { 100, 1000 }, | |
103 | { 200, 1000 }, | |
104 | { 500, 1000 }, | |
105 | /* volts */ | |
106 | { 1, 1 }, | |
107 | { 2, 1 }, | |
108 | { 5, 1 }, | |
109 | { 10, 1 }, | |
110 | }; | |
111 | ||
112 | #define NUM_TIMEBASE ARRAY_SIZE(timebases) | |
113 | #define NUM_VDIV ARRAY_SIZE(vdivs) | |
114 | ||
115 | static const char *trigger_sources[] = { | |
116 | "CH1", | |
117 | "CH2", | |
118 | "EXT", | |
119 | "AC Line", | |
120 | "D0", | |
121 | "D1", | |
122 | "D2", | |
123 | "D3", | |
124 | "D4", | |
125 | "D5", | |
126 | "D6", | |
127 | "D7", | |
128 | "D8", | |
129 | "D9", | |
130 | "D10", | |
131 | "D11", | |
132 | "D12", | |
133 | "D13", | |
134 | "D14", | |
135 | "D15", | |
136 | }; | |
137 | ||
138 | static const char *coupling[] = { | |
139 | "AC", | |
140 | "DC", | |
141 | "GND", | |
142 | }; | |
143 | ||
144 | /* name, series, min timebase, max timebase, min vdiv, digital channels */ | |
145 | static const struct rigol_ds_model supported_models[] = { | |
146 | {"DS1052E", 1, {5, 1000000000}, {50, 1}, {2, 1000}, false}, | |
147 | {"DS1102E", 1, {2, 1000000000}, {50, 1}, {2, 1000}, false}, | |
148 | {"DS1152E", 1, {2, 1000000000}, {50, 1}, {2, 1000}, false}, | |
149 | {"DS1052D", 1, {5, 1000000000}, {50, 1}, {2, 1000}, true}, | |
150 | {"DS1102D", 1, {2, 1000000000}, {50, 1}, {2, 1000}, true}, | |
151 | {"DS1152D", 1, {2, 1000000000}, {50, 1}, {2, 1000}, true}, | |
152 | {"DS2072", 2, {5, 1000000000}, {500, 1}, {500, 1000000}, false}, | |
153 | {"DS2102", 2, {5, 1000000000}, {500, 1}, {500, 1000000}, false}, | |
154 | {"DS2202", 2, {2, 1000000000}, {500, 1}, {500, 1000000}, false}, | |
155 | }; | |
156 | ||
157 | SR_PRIV struct sr_dev_driver rigol_ds_driver_info; | |
158 | static struct sr_dev_driver *di = &rigol_ds_driver_info; | |
159 | ||
160 | static void clear_helper(void *priv) | |
161 | { | |
162 | struct dev_context *devc; | |
163 | ||
164 | devc = priv; | |
165 | g_free(devc->coupling[0]); | |
166 | g_free(devc->coupling[1]); | |
167 | g_free(devc->trigger_source); | |
168 | g_free(devc->trigger_slope); | |
169 | g_slist_free(devc->analog_groups[0].probes); | |
170 | g_slist_free(devc->analog_groups[1].probes); | |
171 | g_slist_free(devc->digital_group.probes); | |
172 | } | |
173 | ||
174 | static int dev_clear(void) | |
175 | { | |
176 | return std_dev_clear(di, clear_helper); | |
177 | } | |
178 | ||
179 | static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...) | |
180 | { | |
181 | va_list args; | |
182 | char buf[256]; | |
183 | ||
184 | va_start(args, format); | |
185 | vsnprintf(buf, 255, format, args); | |
186 | va_end(args); | |
187 | if (rigol_ds_send(sdi, buf) != SR_OK) | |
188 | return SR_ERR; | |
189 | ||
190 | /* When setting a bunch of parameters in a row, the DS1052E scrambles | |
191 | * some of them unless there is at least 100ms delay in between. */ | |
192 | sr_spew("delay %dms", 100); | |
193 | g_usleep(100000); | |
194 | ||
195 | return SR_OK; | |
196 | } | |
197 | ||
198 | static int init(struct sr_context *sr_ctx) | |
199 | { | |
200 | return std_init(sr_ctx, di, LOG_PREFIX); | |
201 | } | |
202 | ||
203 | static int probe_port(const char *port, GSList **devices) | |
204 | { | |
205 | struct dev_context *devc; | |
206 | struct sr_dev_inst *sdi; | |
207 | struct sr_serial_dev_inst *serial; | |
208 | struct sr_probe *probe; | |
209 | unsigned int i; | |
210 | int len, num_tokens; | |
211 | const char *manufacturer, *model_name, *version; | |
212 | const struct rigol_ds_model *model = NULL; | |
213 | char buf[256]; | |
214 | gchar **tokens, *channel_name; | |
215 | ||
216 | *devices = NULL; | |
217 | if (!(serial = sr_serial_dev_inst_new(port, NULL))) | |
218 | return SR_ERR_MALLOC; | |
219 | ||
220 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
221 | return SR_ERR; | |
222 | len = serial_write(serial, "*IDN?", 5); | |
223 | len = serial_read(serial, buf, sizeof(buf)); | |
224 | if (serial_close(serial) != SR_OK) | |
225 | return SR_ERR; | |
226 | ||
227 | sr_serial_dev_inst_free(serial); | |
228 | ||
229 | if (len == 0) | |
230 | return SR_ERR_NA; | |
231 | ||
232 | buf[len] = 0; | |
233 | tokens = g_strsplit(buf, ",", 0); | |
234 | sr_dbg("response: %s [%s]", port, buf); | |
235 | ||
236 | for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++); | |
237 | ||
238 | if (num_tokens < 4) { | |
239 | g_strfreev(tokens); | |
240 | return SR_ERR_NA; | |
241 | } | |
242 | ||
243 | manufacturer = tokens[0]; | |
244 | model_name = tokens[1]; | |
245 | version = tokens[3]; | |
246 | ||
247 | if (strcasecmp(manufacturer, "Rigol Technologies")) { | |
248 | g_strfreev(tokens); | |
249 | return SR_ERR_NA; | |
250 | } | |
251 | ||
252 | for (i = 0; i < ARRAY_SIZE(supported_models); i++) { | |
253 | if (!strcmp(model_name, supported_models[i].name)) { | |
254 | model = &supported_models[i]; | |
255 | break; | |
256 | } | |
257 | } | |
258 | ||
259 | if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, | |
260 | manufacturer, model_name, version))) { | |
261 | g_strfreev(tokens); | |
262 | return SR_ERR_NA; | |
263 | } | |
264 | ||
265 | g_strfreev(tokens); | |
266 | ||
267 | if (!(sdi->conn = sr_serial_dev_inst_new(port, NULL))) | |
268 | return SR_ERR_MALLOC; | |
269 | sdi->driver = di; | |
270 | sdi->inst_type = SR_INST_SERIAL; | |
271 | ||
272 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) | |
273 | return SR_ERR_MALLOC; | |
274 | devc->limit_frames = 0; | |
275 | devc->model = model; | |
276 | ||
277 | for (i = 0; i < 2; i++) { | |
278 | channel_name = (i == 0 ? "CH1" : "CH2"); | |
279 | if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name))) | |
280 | return SR_ERR_MALLOC; | |
281 | sdi->probes = g_slist_append(sdi->probes, probe); | |
282 | devc->analog_groups[i].name = channel_name; | |
283 | devc->analog_groups[i].probes = g_slist_append(NULL, probe); | |
284 | sdi->probe_groups = g_slist_append(sdi->probe_groups, | |
285 | &devc->analog_groups[i]); | |
286 | } | |
287 | ||
288 | if (devc->model->has_digital) { | |
289 | for (i = 0; i < 16; i++) { | |
290 | if (!(channel_name = g_strdup_printf("D%d", i))) | |
291 | return SR_ERR_MALLOC; | |
292 | probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name); | |
293 | g_free(channel_name); | |
294 | if (!probe) | |
295 | return SR_ERR_MALLOC; | |
296 | sdi->probes = g_slist_append(sdi->probes, probe); | |
297 | devc->digital_group.probes = g_slist_append( | |
298 | devc->digital_group.probes, probe); | |
299 | devc->digital_group.name = "LA"; | |
300 | sdi->probe_groups = g_slist_append(sdi->probe_groups, | |
301 | &devc->digital_group); | |
302 | } | |
303 | } | |
304 | ||
305 | for (i = 0; i < NUM_TIMEBASE; i++) { | |
306 | if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2]))) | |
307 | devc->timebases = &timebases[i]; | |
308 | if (!memcmp(&devc->model->max_timebase, &timebases[i], sizeof(uint64_t[2]))) | |
309 | devc->num_timebases = &timebases[i] - devc->timebases + 1; | |
310 | } | |
311 | ||
312 | for (i = 0; i < NUM_VDIV; i++) { | |
313 | if (!memcmp(&devc->model->min_vdiv, &vdivs[i], sizeof(uint64_t[2]))) { | |
314 | devc->vdivs = &timebases[i]; | |
315 | devc->num_vdivs = NUM_VDIV - (&timebases[i] - &timebases[0]); | |
316 | } | |
317 | } | |
318 | ||
319 | sdi->priv = devc; | |
320 | ||
321 | *devices = g_slist_append(NULL, sdi); | |
322 | ||
323 | return SR_OK; | |
324 | } | |
325 | ||
326 | static GSList *scan(GSList *options) | |
327 | { | |
328 | struct drv_context *drvc; | |
329 | struct sr_config *src; | |
330 | GSList *l, *devices; | |
331 | GDir *dir; | |
332 | int ret; | |
333 | const gchar *dev_name; | |
334 | gchar *port = NULL; | |
335 | ||
336 | drvc = di->priv; | |
337 | ||
338 | for (l = options; l; l = l->next) { | |
339 | src = l->data; | |
340 | if (src->key == SR_CONF_CONN) { | |
341 | port = (char *)g_variant_get_string(src->data, NULL); | |
342 | break; | |
343 | } | |
344 | } | |
345 | ||
346 | devices = NULL; | |
347 | if (port) { | |
348 | if (probe_port(port, &devices) == SR_ERR_MALLOC) | |
349 | return NULL; | |
350 | } else { | |
351 | if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL))) | |
352 | if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL))) | |
353 | return NULL; | |
354 | while ((dev_name = g_dir_read_name(dir))) { | |
355 | if (strncmp(dev_name, "usbtmc", 6)) | |
356 | continue; | |
357 | port = g_strconcat("/dev/", dev_name, NULL); | |
358 | ret = probe_port(port, &devices); | |
359 | g_free(port); | |
360 | if (ret == SR_ERR_MALLOC) { | |
361 | g_dir_close(dir); | |
362 | return NULL; | |
363 | } | |
364 | } | |
365 | g_dir_close(dir); | |
366 | } | |
367 | ||
368 | /* Tack a copy of the newly found devices onto the driver list. */ | |
369 | l = g_slist_copy(devices); | |
370 | drvc->instances = g_slist_concat(drvc->instances, l); | |
371 | ||
372 | return devices; | |
373 | } | |
374 | ||
375 | static GSList *dev_list(void) | |
376 | { | |
377 | return ((struct drv_context *)(di->priv))->instances; | |
378 | } | |
379 | ||
380 | static int dev_open(struct sr_dev_inst *sdi) | |
381 | { | |
382 | ||
383 | if (serial_open(sdi->conn, SERIAL_RDWR) != SR_OK) | |
384 | return SR_ERR; | |
385 | ||
386 | if (rigol_ds_get_dev_cfg(sdi) != SR_OK) | |
387 | return SR_ERR; | |
388 | ||
389 | sdi->status = SR_ST_ACTIVE; | |
390 | ||
391 | return SR_OK; | |
392 | } | |
393 | ||
394 | static int dev_close(struct sr_dev_inst *sdi) | |
395 | { | |
396 | struct sr_serial_dev_inst *serial; | |
397 | ||
398 | serial = sdi->conn; | |
399 | if (serial && serial->fd != -1) { | |
400 | serial_close(serial); | |
401 | sdi->status = SR_ST_INACTIVE; | |
402 | } | |
403 | ||
404 | return SR_OK; | |
405 | } | |
406 | ||
407 | static int cleanup(void) | |
408 | { | |
409 | return dev_clear(); | |
410 | } | |
411 | ||
412 | static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi, | |
413 | const struct sr_probe_group *probe_group) | |
414 | { | |
415 | struct dev_context *devc; | |
416 | unsigned int i; | |
417 | ||
418 | if (!sdi || !(devc = sdi->priv)) | |
419 | return SR_ERR_ARG; | |
420 | ||
421 | /* If a probe group is specified, it must be a valid one. */ | |
422 | if (probe_group) { | |
423 | if (probe_group != &devc->analog_groups[0] | |
424 | && probe_group != &devc->analog_groups[1]) { | |
425 | sr_err("Invalid probe group specified."); | |
426 | return SR_ERR; | |
427 | } | |
428 | } | |
429 | ||
430 | switch (id) { | |
431 | case SR_CONF_NUM_TIMEBASE: | |
432 | *data = g_variant_new_int32(devc->num_timebases); | |
433 | break; | |
434 | case SR_CONF_NUM_VDIV: | |
435 | if (!probe_group) { | |
436 | sr_err("No probe group specified."); | |
437 | return SR_ERR_PROBE_GROUP; | |
438 | } | |
439 | for (i = 0; i < 2; i++) { | |
440 | if (probe_group == &devc->analog_groups[i]) { | |
441 | *data = g_variant_new_int32(devc->num_vdivs); | |
442 | return SR_OK; | |
443 | } | |
444 | } | |
445 | return SR_ERR_NA; | |
446 | default: | |
447 | return SR_ERR_NA; | |
448 | } | |
449 | ||
450 | return SR_OK; | |
451 | } | |
452 | ||
453 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi, | |
454 | const struct sr_probe_group *probe_group) | |
455 | { | |
456 | struct dev_context *devc; | |
457 | uint64_t tmp_u64, p, q; | |
458 | double t_dbl; | |
459 | unsigned int i, j; | |
460 | int ret; | |
461 | const char *tmp_str; | |
462 | ||
463 | if (!(devc = sdi->priv)) | |
464 | return SR_ERR_ARG; | |
465 | ||
466 | if (sdi->status != SR_ST_ACTIVE) | |
467 | return SR_ERR_DEV_CLOSED; | |
468 | ||
469 | /* If a probe group is specified, it must be a valid one. */ | |
470 | if (probe_group) { | |
471 | if (probe_group != &devc->analog_groups[0] | |
472 | && probe_group != &devc->analog_groups[1]) { | |
473 | sr_err("Invalid probe group specified."); | |
474 | return SR_ERR; | |
475 | } | |
476 | } | |
477 | ||
478 | ret = SR_OK; | |
479 | switch (id) { | |
480 | case SR_CONF_LIMIT_FRAMES: | |
481 | devc->limit_frames = g_variant_get_uint64(data); | |
482 | break; | |
483 | case SR_CONF_TRIGGER_SLOPE: | |
484 | tmp_u64 = g_variant_get_uint64(data); | |
485 | if (tmp_u64 != 0 && tmp_u64 != 1) | |
486 | return SR_ERR; | |
487 | g_free(devc->trigger_slope); | |
488 | devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG"); | |
489 | ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope); | |
490 | break; | |
491 | case SR_CONF_HORIZ_TRIGGERPOS: | |
492 | t_dbl = g_variant_get_double(data); | |
493 | if (t_dbl < 0.0 || t_dbl > 1.0) | |
494 | return SR_ERR; | |
495 | devc->horiz_triggerpos = t_dbl; | |
496 | /* We have the trigger offset as a percentage of the frame, but | |
497 | * need to express this in seconds. */ | |
498 | t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases; | |
499 | ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl); | |
500 | break; | |
501 | case SR_CONF_TIMEBASE: | |
502 | g_variant_get(data, "(tt)", &p, &q); | |
503 | for (i = 0; i < devc->num_timebases; i++) { | |
504 | if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) { | |
505 | devc->timebase = (float)p / q; | |
506 | ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase); | |
507 | break; | |
508 | } | |
509 | } | |
510 | if (i == devc->num_timebases) | |
511 | ret = SR_ERR_ARG; | |
512 | break; | |
513 | case SR_CONF_TRIGGER_SOURCE: | |
514 | tmp_str = g_variant_get_string(data, NULL); | |
515 | for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) { | |
516 | if (!strcmp(trigger_sources[i], tmp_str)) { | |
517 | g_free(devc->trigger_source); | |
518 | devc->trigger_source = g_strdup(trigger_sources[i]); | |
519 | if (!strcmp(devc->trigger_source, "AC Line")) | |
520 | tmp_str = "ACL"; | |
521 | else if (!strcmp(devc->trigger_source, "CH1")) | |
522 | tmp_str = "CHAN1"; | |
523 | else if (!strcmp(devc->trigger_source, "CH2")) | |
524 | tmp_str = "CHAN2"; | |
525 | else | |
526 | tmp_str = (char *)devc->trigger_source; | |
527 | ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str); | |
528 | break; | |
529 | } | |
530 | } | |
531 | if (i == ARRAY_SIZE(trigger_sources)) | |
532 | ret = SR_ERR_ARG; | |
533 | break; | |
534 | case SR_CONF_VDIV: | |
535 | if (!probe_group) { | |
536 | sr_err("No probe group specified."); | |
537 | return SR_ERR_PROBE_GROUP; | |
538 | } | |
539 | g_variant_get(data, "(tt)", &p, &q); | |
540 | for (i = 0; i < 2; i++) { | |
541 | if (probe_group == &devc->analog_groups[i]) { | |
542 | for (j = 0; j < ARRAY_SIZE(vdivs); j++) { | |
543 | if (vdivs[j][0] != p || vdivs[j][1] != q) | |
544 | continue; | |
545 | devc->vdiv[i] = (float)p / q; | |
546 | return set_cfg(sdi, ":CHAN%d:SCAL %.3f", i + 1, | |
547 | devc->vdiv[i]); | |
548 | } | |
549 | return SR_ERR_ARG; | |
550 | } | |
551 | } | |
552 | return SR_ERR_NA; | |
553 | case SR_CONF_COUPLING: | |
554 | if (!probe_group) { | |
555 | sr_err("No probe group specified."); | |
556 | return SR_ERR_PROBE_GROUP; | |
557 | } | |
558 | tmp_str = g_variant_get_string(data, NULL); | |
559 | for (i = 0; i < 2; i++) { | |
560 | if (probe_group == &devc->analog_groups[i]) { | |
561 | for (j = 0; j < ARRAY_SIZE(coupling); j++) { | |
562 | if (!strcmp(tmp_str, coupling[j])) { | |
563 | g_free(devc->coupling[i]); | |
564 | devc->coupling[i] = g_strdup(coupling[j]); | |
565 | return set_cfg(sdi, ":CHAN%d:COUP %s", i + 1, | |
566 | devc->coupling[i]); | |
567 | } | |
568 | } | |
569 | return SR_ERR_ARG; | |
570 | } | |
571 | } | |
572 | return SR_ERR_NA; | |
573 | default: | |
574 | ret = SR_ERR_NA; | |
575 | break; | |
576 | } | |
577 | ||
578 | return ret; | |
579 | } | |
580 | ||
581 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
582 | const struct sr_probe_group *probe_group) | |
583 | { | |
584 | GVariant *tuple, *rational[2]; | |
585 | GVariantBuilder gvb; | |
586 | unsigned int i; | |
587 | struct dev_context *devc = sdi->priv; | |
588 | ||
589 | if (key == SR_CONF_SCAN_OPTIONS) { | |
590 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
591 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
592 | return SR_OK; | |
593 | } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) { | |
594 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
595 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
596 | return SR_OK; | |
597 | } | |
598 | ||
599 | /* Every other option requires a valid device instance. */ | |
600 | if (!sdi || !(devc = sdi->priv)) | |
601 | return SR_ERR_ARG; | |
602 | ||
603 | /* If a probe group is specified, it must be a valid one. */ | |
604 | if (probe_group) { | |
605 | if (probe_group != &devc->analog_groups[0] | |
606 | && probe_group != &devc->analog_groups[1]) { | |
607 | sr_err("Invalid probe group specified."); | |
608 | return SR_ERR; | |
609 | } | |
610 | } | |
611 | ||
612 | switch (key) { | |
613 | break; | |
614 | case SR_CONF_DEVICE_OPTIONS: | |
615 | if (!probe_group) { | |
616 | sr_err("No probe group specified."); | |
617 | return SR_ERR_PROBE_GROUP; | |
618 | } | |
619 | if (probe_group == &devc->digital_group) { | |
620 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
621 | NULL, 0, sizeof(int32_t)); | |
622 | return SR_OK; | |
623 | } else { | |
624 | for (i = 0; i < 2; i++) { | |
625 | if (probe_group == &devc->analog_groups[i]) { | |
626 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
627 | analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t)); | |
628 | return SR_OK; | |
629 | } | |
630 | } | |
631 | return SR_ERR_NA; | |
632 | } | |
633 | break; | |
634 | case SR_CONF_COUPLING: | |
635 | if (!probe_group) { | |
636 | sr_err("No probe group specified."); | |
637 | return SR_ERR_PROBE_GROUP; | |
638 | } | |
639 | *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling)); | |
640 | break; | |
641 | case SR_CONF_VDIV: | |
642 | if (!probe_group) { | |
643 | sr_err("No probe group specified."); | |
644 | return SR_ERR_PROBE_GROUP; | |
645 | } | |
646 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
647 | for (i = 0; i < devc->num_vdivs; i++) { | |
648 | rational[0] = g_variant_new_uint64(devc->vdivs[i][0]); | |
649 | rational[1] = g_variant_new_uint64(devc->vdivs[i][1]); | |
650 | tuple = g_variant_new_tuple(rational, 2); | |
651 | g_variant_builder_add_value(&gvb, tuple); | |
652 | } | |
653 | *data = g_variant_builder_end(&gvb); | |
654 | break; | |
655 | case SR_CONF_TIMEBASE: | |
656 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
657 | for (i = 0; i < devc->num_timebases; i++) { | |
658 | rational[0] = g_variant_new_uint64(devc->timebases[i][0]); | |
659 | rational[1] = g_variant_new_uint64(devc->timebases[i][1]); | |
660 | tuple = g_variant_new_tuple(rational, 2); | |
661 | g_variant_builder_add_value(&gvb, tuple); | |
662 | } | |
663 | *data = g_variant_builder_end(&gvb); | |
664 | break; | |
665 | case SR_CONF_TRIGGER_SOURCE: | |
666 | *data = g_variant_new_strv(trigger_sources, | |
667 | devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4); | |
668 | break; | |
669 | default: | |
670 | return SR_ERR_NA; | |
671 | } | |
672 | ||
673 | return SR_OK; | |
674 | } | |
675 | ||
676 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
677 | { | |
678 | struct sr_serial_dev_inst *serial; | |
679 | struct dev_context *devc; | |
680 | struct sr_probe *probe; | |
681 | GSList *l; | |
682 | char cmd[256]; | |
683 | ||
684 | if (sdi->status != SR_ST_ACTIVE) | |
685 | return SR_ERR_DEV_CLOSED; | |
686 | ||
687 | serial = sdi->conn; | |
688 | devc = sdi->priv; | |
689 | ||
690 | for (l = sdi->probes; l; l = l->next) { | |
691 | probe = l->data; | |
692 | sr_dbg("handling probe %s", probe->name); | |
693 | if (probe->type == SR_PROBE_ANALOG) { | |
694 | if (probe->enabled) | |
695 | devc->enabled_analog_probes = g_slist_append( | |
696 | devc->enabled_analog_probes, probe); | |
697 | if (probe->enabled != devc->analog_channels[probe->index]) { | |
698 | /* Enabled channel is currently disabled, or vice versa. */ | |
699 | sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1, | |
700 | probe->enabled ? "ON" : "OFF"); | |
701 | if (rigol_ds_send(sdi, cmd) != SR_OK) | |
702 | return SR_ERR; | |
703 | } | |
704 | } else if (probe->type == SR_PROBE_LOGIC) { | |
705 | if (probe->enabled) | |
706 | devc->enabled_digital_probes = g_slist_append( | |
707 | devc->enabled_digital_probes, probe); | |
708 | if (probe->enabled != devc->digital_channels[probe->index]) { | |
709 | /* Enabled channel is currently disabled, or vice versa. */ | |
710 | sprintf(cmd, ":DIG%d:TURN %s", probe->index, | |
711 | probe->enabled ? "ON" : "OFF"); | |
712 | if (rigol_ds_send(sdi, cmd) != SR_OK) | |
713 | return SR_ERR; | |
714 | } | |
715 | } | |
716 | } | |
717 | if (!devc->enabled_analog_probes && !devc->enabled_digital_probes) | |
718 | return SR_ERR; | |
719 | ||
720 | sr_source_add(serial->fd, G_IO_IN, 50, rigol_ds_receive, (void *)sdi); | |
721 | ||
722 | /* Send header packet to the session bus. */ | |
723 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
724 | ||
725 | if (devc->model->series == 1) { | |
726 | /* Fetch the first frame. */ | |
727 | if (devc->enabled_analog_probes) { | |
728 | devc->channel_frame = devc->enabled_analog_probes->data; | |
729 | if (rigol_ds_send(sdi, ":WAV:DATA? CHAN%d", | |
730 | devc->channel_frame->index + 1) != SR_OK) | |
731 | return SR_ERR; | |
732 | } else { | |
733 | devc->channel_frame = devc->enabled_digital_probes->data; | |
734 | if (rigol_ds_send(sdi, ":WAV:DATA? DIG") != SR_OK) | |
735 | return SR_ERR; | |
736 | } | |
737 | ||
738 | devc->num_frame_bytes = 0; | |
739 | } else { | |
740 | if (devc->enabled_analog_probes) { | |
741 | /* Assume there already was a trigger event - don't wait */ | |
742 | if (rigol_ds2xx2_acquisition_start(sdi, FALSE) != SR_OK) | |
743 | return SR_ERR; | |
744 | } | |
745 | } | |
746 | ||
747 | return SR_OK; | |
748 | } | |
749 | ||
750 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
751 | { | |
752 | struct dev_context *devc; | |
753 | struct sr_serial_dev_inst *serial; | |
754 | ||
755 | (void)cb_data; | |
756 | ||
757 | devc = sdi->priv; | |
758 | ||
759 | if (sdi->status != SR_ST_ACTIVE) { | |
760 | sr_err("Device inactive, can't stop acquisition."); | |
761 | return SR_ERR; | |
762 | } | |
763 | ||
764 | g_slist_free(devc->enabled_analog_probes); | |
765 | g_slist_free(devc->enabled_digital_probes); | |
766 | devc->enabled_analog_probes = NULL; | |
767 | devc->enabled_digital_probes = NULL; | |
768 | serial = sdi->conn; | |
769 | sr_source_remove(serial->fd); | |
770 | ||
771 | return SR_OK; | |
772 | } | |
773 | ||
774 | SR_PRIV struct sr_dev_driver rigol_ds_driver_info = { | |
775 | .name = "rigol-ds", | |
776 | .longname = "Rigol DS", | |
777 | .api_version = 1, | |
778 | .init = init, | |
779 | .cleanup = cleanup, | |
780 | .scan = scan, | |
781 | .dev_list = dev_list, | |
782 | .dev_clear = dev_clear, | |
783 | .config_get = config_get, | |
784 | .config_set = config_set, | |
785 | .config_list = config_list, | |
786 | .dev_open = dev_open, | |
787 | .dev_close = dev_close, | |
788 | .dev_acquisition_start = dev_acquisition_start, | |
789 | .dev_acquisition_stop = dev_acquisition_stop, | |
790 | .priv = NULL, | |
791 | }; |