]>
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 | SR_CONF_DATA_SOURCE, | |
49 | }; | |
50 | ||
51 | static const uint64_t timebases[][2] = { | |
52 | /* nanoseconds */ | |
53 | { 2, 1000000000 }, | |
54 | { 5, 1000000000 }, | |
55 | { 10, 1000000000 }, | |
56 | { 20, 1000000000 }, | |
57 | { 50, 1000000000 }, | |
58 | { 100, 1000000000 }, | |
59 | { 500, 1000000000 }, | |
60 | /* microseconds */ | |
61 | { 1, 1000000 }, | |
62 | { 2, 1000000 }, | |
63 | { 5, 1000000 }, | |
64 | { 10, 1000000 }, | |
65 | { 20, 1000000 }, | |
66 | { 50, 1000000 }, | |
67 | { 100, 1000000 }, | |
68 | { 200, 1000000 }, | |
69 | { 500, 1000000 }, | |
70 | /* milliseconds */ | |
71 | { 1, 1000 }, | |
72 | { 2, 1000 }, | |
73 | { 5, 1000 }, | |
74 | { 10, 1000 }, | |
75 | { 20, 1000 }, | |
76 | { 50, 1000 }, | |
77 | { 100, 1000 }, | |
78 | { 200, 1000 }, | |
79 | { 500, 1000 }, | |
80 | /* seconds */ | |
81 | { 1, 1 }, | |
82 | { 2, 1 }, | |
83 | { 5, 1 }, | |
84 | { 10, 1 }, | |
85 | { 20, 1 }, | |
86 | { 50, 1 }, | |
87 | { 100, 1 }, | |
88 | { 200, 1 }, | |
89 | { 500, 1 }, | |
90 | /* { 1000, 1 }, Confuses other code? */ | |
91 | }; | |
92 | ||
93 | static const uint64_t vdivs[][2] = { | |
94 | /* microvolts */ | |
95 | { 500, 1000000 }, | |
96 | /* millivolts */ | |
97 | { 1, 1000 }, | |
98 | { 2, 1000 }, | |
99 | { 5, 1000 }, | |
100 | { 10, 1000 }, | |
101 | { 20, 1000 }, | |
102 | { 50, 1000 }, | |
103 | { 100, 1000 }, | |
104 | { 200, 1000 }, | |
105 | { 500, 1000 }, | |
106 | /* volts */ | |
107 | { 1, 1 }, | |
108 | { 2, 1 }, | |
109 | { 5, 1 }, | |
110 | { 10, 1 }, | |
111 | }; | |
112 | ||
113 | #define NUM_TIMEBASE ARRAY_SIZE(timebases) | |
114 | #define NUM_VDIV ARRAY_SIZE(vdivs) | |
115 | ||
116 | static const char *trigger_sources[] = { | |
117 | "CH1", | |
118 | "CH2", | |
119 | "EXT", | |
120 | "AC Line", | |
121 | "D0", | |
122 | "D1", | |
123 | "D2", | |
124 | "D3", | |
125 | "D4", | |
126 | "D5", | |
127 | "D6", | |
128 | "D7", | |
129 | "D8", | |
130 | "D9", | |
131 | "D10", | |
132 | "D11", | |
133 | "D12", | |
134 | "D13", | |
135 | "D14", | |
136 | "D15", | |
137 | }; | |
138 | ||
139 | static const char *coupling[] = { | |
140 | "AC", | |
141 | "DC", | |
142 | "GND", | |
143 | }; | |
144 | ||
145 | /* Do not change the order of entries */ | |
146 | static const char *data_sources[] = { | |
147 | "Live", | |
148 | "Memory", | |
149 | "Segmented", | |
150 | }; | |
151 | ||
152 | /* | |
153 | * name, series, protocol flavor, min timebase, max timebase, min vdiv, | |
154 | * digital channels, number of horizontal divs | |
155 | */ | |
156 | static const struct rigol_ds_model supported_models[] = { | |
157 | {"DS1052E", RIGOL_DS1000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, false, 12}, | |
158 | {"DS1102E", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, false, 12}, | |
159 | {"DS1152E", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, false, 12}, | |
160 | {"DS1052D", RIGOL_DS1000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, true, 12}, | |
161 | {"DS1102D", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, true, 12}, | |
162 | {"DS1152D", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, true, 12}, | |
163 | {"DS2072", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {500, 1}, {500, 1000000}, false, 14}, | |
164 | {"DS2102", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {500, 1}, {500, 1000000}, false, 14}, | |
165 | {"DS2202", RIGOL_DS2000, PROTOCOL_IEEE488_2, {2, 1000000000}, {500, 1}, {500, 1000000}, false, 14}, | |
166 | }; | |
167 | ||
168 | SR_PRIV struct sr_dev_driver rigol_ds_driver_info; | |
169 | static struct sr_dev_driver *di = &rigol_ds_driver_info; | |
170 | ||
171 | static void clear_helper(void *priv) | |
172 | { | |
173 | struct dev_context *devc; | |
174 | ||
175 | devc = priv; | |
176 | g_free(devc->data); | |
177 | g_free(devc->buffer); | |
178 | g_free(devc->coupling[0]); | |
179 | g_free(devc->coupling[1]); | |
180 | g_free(devc->trigger_source); | |
181 | g_free(devc->trigger_slope); | |
182 | g_slist_free(devc->analog_groups[0].probes); | |
183 | g_slist_free(devc->analog_groups[1].probes); | |
184 | g_slist_free(devc->digital_group.probes); | |
185 | } | |
186 | ||
187 | static int dev_clear(void) | |
188 | { | |
189 | return std_dev_clear(di, clear_helper); | |
190 | } | |
191 | ||
192 | static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...) | |
193 | { | |
194 | va_list args; | |
195 | char buf[256]; | |
196 | ||
197 | va_start(args, format); | |
198 | vsnprintf(buf, 255, format, args); | |
199 | va_end(args); | |
200 | if (rigol_ds_send(sdi, buf) != SR_OK) | |
201 | return SR_ERR; | |
202 | ||
203 | /* When setting a bunch of parameters in a row, the DS1052E scrambles | |
204 | * some of them unless there is at least 100ms delay in between. */ | |
205 | sr_spew("delay %dms", 100); | |
206 | g_usleep(100000); | |
207 | ||
208 | return SR_OK; | |
209 | } | |
210 | ||
211 | static int init(struct sr_context *sr_ctx) | |
212 | { | |
213 | return std_init(sr_ctx, di, LOG_PREFIX); | |
214 | } | |
215 | ||
216 | static int probe_port(const char *port, GSList **devices) | |
217 | { | |
218 | struct dev_context *devc; | |
219 | struct sr_dev_inst *sdi; | |
220 | struct sr_usbtmc_dev_inst *usbtmc; | |
221 | struct sr_probe *probe; | |
222 | unsigned int i; | |
223 | int len, num_tokens; | |
224 | const char *manufacturer, *model_name, *version; | |
225 | const struct rigol_ds_model *model = NULL; | |
226 | char buf[256]; | |
227 | gchar **tokens, *channel_name; | |
228 | ||
229 | *devices = NULL; | |
230 | if (!(usbtmc = sr_usbtmc_dev_inst_new(port))) | |
231 | return SR_ERR_MALLOC; | |
232 | ||
233 | if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0) | |
234 | return SR_ERR; | |
235 | len = write(usbtmc->fd, "*IDN?", 5); | |
236 | len = read(usbtmc->fd, buf, sizeof(buf)); | |
237 | if (close(usbtmc->fd) < 0) | |
238 | return SR_ERR; | |
239 | ||
240 | sr_usbtmc_dev_inst_free(usbtmc); | |
241 | ||
242 | if (len == 0) | |
243 | return SR_ERR_NA; | |
244 | ||
245 | buf[len] = 0; | |
246 | tokens = g_strsplit(buf, ",", 0); | |
247 | sr_dbg("response: %s [%s]", port, buf); | |
248 | ||
249 | for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++); | |
250 | ||
251 | if (num_tokens < 4) { | |
252 | g_strfreev(tokens); | |
253 | return SR_ERR_NA; | |
254 | } | |
255 | ||
256 | manufacturer = tokens[0]; | |
257 | model_name = tokens[1]; | |
258 | version = tokens[3]; | |
259 | ||
260 | if (strcasecmp(manufacturer, "Rigol Technologies")) { | |
261 | g_strfreev(tokens); | |
262 | return SR_ERR_NA; | |
263 | } | |
264 | ||
265 | for (i = 0; i < ARRAY_SIZE(supported_models); i++) { | |
266 | if (!strcmp(model_name, supported_models[i].name)) { | |
267 | model = &supported_models[i]; | |
268 | break; | |
269 | } | |
270 | } | |
271 | ||
272 | if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, | |
273 | manufacturer, model_name, version))) { | |
274 | g_strfreev(tokens); | |
275 | return SR_ERR_NA; | |
276 | } | |
277 | ||
278 | g_strfreev(tokens); | |
279 | ||
280 | if (!(sdi->conn = sr_usbtmc_dev_inst_new(port))) | |
281 | return SR_ERR_MALLOC; | |
282 | sdi->driver = di; | |
283 | sdi->inst_type = SR_INST_USBTMC; | |
284 | ||
285 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) | |
286 | return SR_ERR_MALLOC; | |
287 | devc->limit_frames = 0; | |
288 | devc->model = model; | |
289 | ||
290 | for (i = 0; i < 2; i++) { | |
291 | channel_name = (i == 0 ? "CH1" : "CH2"); | |
292 | if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name))) | |
293 | return SR_ERR_MALLOC; | |
294 | sdi->probes = g_slist_append(sdi->probes, probe); | |
295 | devc->analog_groups[i].name = channel_name; | |
296 | devc->analog_groups[i].probes = g_slist_append(NULL, probe); | |
297 | sdi->probe_groups = g_slist_append(sdi->probe_groups, | |
298 | &devc->analog_groups[i]); | |
299 | } | |
300 | ||
301 | if (devc->model->has_digital) { | |
302 | for (i = 0; i < 16; i++) { | |
303 | if (!(channel_name = g_strdup_printf("D%d", i))) | |
304 | return SR_ERR_MALLOC; | |
305 | probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name); | |
306 | g_free(channel_name); | |
307 | if (!probe) | |
308 | return SR_ERR_MALLOC; | |
309 | sdi->probes = g_slist_append(sdi->probes, probe); | |
310 | devc->digital_group.probes = g_slist_append( | |
311 | devc->digital_group.probes, probe); | |
312 | devc->digital_group.name = "LA"; | |
313 | sdi->probe_groups = g_slist_append(sdi->probe_groups, | |
314 | &devc->digital_group); | |
315 | } | |
316 | } | |
317 | ||
318 | for (i = 0; i < NUM_TIMEBASE; i++) { | |
319 | if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2]))) | |
320 | devc->timebases = &timebases[i]; | |
321 | if (!memcmp(&devc->model->max_timebase, &timebases[i], sizeof(uint64_t[2]))) | |
322 | devc->num_timebases = &timebases[i] - devc->timebases + 1; | |
323 | } | |
324 | ||
325 | for (i = 0; i < NUM_VDIV; i++) { | |
326 | if (!memcmp(&devc->model->min_vdiv, &vdivs[i], sizeof(uint64_t[2]))) { | |
327 | devc->vdivs = &vdivs[i]; | |
328 | devc->num_vdivs = NUM_VDIV - (&vdivs[i] - &vdivs[0]); | |
329 | } | |
330 | } | |
331 | ||
332 | if (!(devc->buffer = g_try_malloc(ACQ_BUFFER_SIZE))) | |
333 | return SR_ERR_MALLOC; | |
334 | if (!(devc->data = g_try_malloc(ACQ_BUFFER_SIZE * sizeof(float)))) | |
335 | return SR_ERR_MALLOC; | |
336 | ||
337 | devc->data_source = DATA_SOURCE_LIVE; | |
338 | ||
339 | sdi->priv = devc; | |
340 | ||
341 | *devices = g_slist_append(NULL, sdi); | |
342 | ||
343 | return SR_OK; | |
344 | } | |
345 | ||
346 | static GSList *scan(GSList *options) | |
347 | { | |
348 | struct drv_context *drvc; | |
349 | struct sr_config *src; | |
350 | GSList *l, *devices; | |
351 | GDir *dir; | |
352 | int ret; | |
353 | const gchar *dev_name; | |
354 | gchar *port = NULL; | |
355 | ||
356 | drvc = di->priv; | |
357 | ||
358 | for (l = options; l; l = l->next) { | |
359 | src = l->data; | |
360 | if (src->key == SR_CONF_CONN) { | |
361 | port = (char *)g_variant_get_string(src->data, NULL); | |
362 | break; | |
363 | } | |
364 | } | |
365 | ||
366 | devices = NULL; | |
367 | if (port) { | |
368 | if (probe_port(port, &devices) == SR_ERR_MALLOC) | |
369 | return NULL; | |
370 | } else { | |
371 | if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL))) | |
372 | if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL))) | |
373 | return NULL; | |
374 | while ((dev_name = g_dir_read_name(dir))) { | |
375 | if (strncmp(dev_name, "usbtmc", 6)) | |
376 | continue; | |
377 | port = g_strconcat("/dev/", dev_name, NULL); | |
378 | ret = probe_port(port, &devices); | |
379 | g_free(port); | |
380 | if (ret == SR_ERR_MALLOC) { | |
381 | g_dir_close(dir); | |
382 | return NULL; | |
383 | } | |
384 | } | |
385 | g_dir_close(dir); | |
386 | } | |
387 | ||
388 | /* Tack a copy of the newly found devices onto the driver list. */ | |
389 | l = g_slist_copy(devices); | |
390 | drvc->instances = g_slist_concat(drvc->instances, l); | |
391 | ||
392 | return devices; | |
393 | } | |
394 | ||
395 | static GSList *dev_list(void) | |
396 | { | |
397 | return ((struct drv_context *)(di->priv))->instances; | |
398 | } | |
399 | ||
400 | static int dev_open(struct sr_dev_inst *sdi) | |
401 | { | |
402 | struct sr_usbtmc_dev_inst *usbtmc = sdi->conn; | |
403 | ||
404 | if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0) | |
405 | return SR_ERR; | |
406 | ||
407 | if (rigol_ds_get_dev_cfg(sdi) != SR_OK) | |
408 | return SR_ERR; | |
409 | ||
410 | sdi->status = SR_ST_ACTIVE; | |
411 | ||
412 | return SR_OK; | |
413 | } | |
414 | ||
415 | static int dev_close(struct sr_dev_inst *sdi) | |
416 | { | |
417 | struct sr_usbtmc_dev_inst *usbtmc; | |
418 | ||
419 | usbtmc = sdi->conn; | |
420 | if (usbtmc && usbtmc->fd != -1) { | |
421 | close(usbtmc->fd); | |
422 | usbtmc->fd = -1; | |
423 | sdi->status = SR_ST_INACTIVE; | |
424 | } | |
425 | ||
426 | return SR_OK; | |
427 | } | |
428 | ||
429 | static int cleanup(void) | |
430 | { | |
431 | return dev_clear(); | |
432 | } | |
433 | ||
434 | static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi, | |
435 | const struct sr_probe_group *probe_group) | |
436 | { | |
437 | struct dev_context *devc; | |
438 | unsigned int i; | |
439 | ||
440 | if (!sdi || !(devc = sdi->priv)) | |
441 | return SR_ERR_ARG; | |
442 | ||
443 | /* If a probe group is specified, it must be a valid one. */ | |
444 | if (probe_group) { | |
445 | if (probe_group != &devc->analog_groups[0] | |
446 | && probe_group != &devc->analog_groups[1]) { | |
447 | sr_err("Invalid probe group specified."); | |
448 | return SR_ERR; | |
449 | } | |
450 | } | |
451 | ||
452 | switch (id) { | |
453 | case SR_CONF_NUM_TIMEBASE: | |
454 | *data = g_variant_new_int32(devc->num_timebases); | |
455 | break; | |
456 | case SR_CONF_NUM_VDIV: | |
457 | if (!probe_group) { | |
458 | sr_err("No probe group specified."); | |
459 | return SR_ERR_PROBE_GROUP; | |
460 | } | |
461 | for (i = 0; i < 2; i++) { | |
462 | if (probe_group == &devc->analog_groups[i]) { | |
463 | *data = g_variant_new_int32(devc->num_vdivs); | |
464 | return SR_OK; | |
465 | } | |
466 | } | |
467 | return SR_ERR_NA; | |
468 | case SR_CONF_DATA_SOURCE: | |
469 | if (devc->data_source == DATA_SOURCE_LIVE) | |
470 | *data = g_variant_new_string("Live"); | |
471 | else if (devc->data_source == DATA_SOURCE_MEMORY) | |
472 | *data = g_variant_new_string("Memory"); | |
473 | else | |
474 | *data = g_variant_new_string("Segmented"); | |
475 | break; | |
476 | default: | |
477 | return SR_ERR_NA; | |
478 | } | |
479 | ||
480 | return SR_OK; | |
481 | } | |
482 | ||
483 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi, | |
484 | const struct sr_probe_group *probe_group) | |
485 | { | |
486 | struct dev_context *devc; | |
487 | uint64_t tmp_u64, p, q; | |
488 | double t_dbl; | |
489 | unsigned int i, j; | |
490 | int ret; | |
491 | const char *tmp_str; | |
492 | ||
493 | if (!(devc = sdi->priv)) | |
494 | return SR_ERR_ARG; | |
495 | ||
496 | if (sdi->status != SR_ST_ACTIVE) | |
497 | return SR_ERR_DEV_CLOSED; | |
498 | ||
499 | /* If a probe group is specified, it must be a valid one. */ | |
500 | if (probe_group) { | |
501 | if (probe_group != &devc->analog_groups[0] | |
502 | && probe_group != &devc->analog_groups[1]) { | |
503 | sr_err("Invalid probe group specified."); | |
504 | return SR_ERR; | |
505 | } | |
506 | } | |
507 | ||
508 | ret = SR_OK; | |
509 | switch (id) { | |
510 | case SR_CONF_LIMIT_FRAMES: | |
511 | devc->limit_frames = g_variant_get_uint64(data); | |
512 | break; | |
513 | case SR_CONF_TRIGGER_SLOPE: | |
514 | tmp_u64 = g_variant_get_uint64(data); | |
515 | if (tmp_u64 != 0 && tmp_u64 != 1) | |
516 | return SR_ERR; | |
517 | g_free(devc->trigger_slope); | |
518 | devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG"); | |
519 | ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope); | |
520 | break; | |
521 | case SR_CONF_HORIZ_TRIGGERPOS: | |
522 | t_dbl = g_variant_get_double(data); | |
523 | if (t_dbl < 0.0 || t_dbl > 1.0) | |
524 | return SR_ERR; | |
525 | devc->horiz_triggerpos = t_dbl; | |
526 | /* We have the trigger offset as a percentage of the frame, but | |
527 | * need to express this in seconds. */ | |
528 | t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases; | |
529 | ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl); | |
530 | break; | |
531 | case SR_CONF_TIMEBASE: | |
532 | g_variant_get(data, "(tt)", &p, &q); | |
533 | for (i = 0; i < devc->num_timebases; i++) { | |
534 | if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) { | |
535 | devc->timebase = (float)p / q; | |
536 | ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase); | |
537 | break; | |
538 | } | |
539 | } | |
540 | if (i == devc->num_timebases) | |
541 | ret = SR_ERR_ARG; | |
542 | break; | |
543 | case SR_CONF_TRIGGER_SOURCE: | |
544 | tmp_str = g_variant_get_string(data, NULL); | |
545 | for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) { | |
546 | if (!strcmp(trigger_sources[i], tmp_str)) { | |
547 | g_free(devc->trigger_source); | |
548 | devc->trigger_source = g_strdup(trigger_sources[i]); | |
549 | if (!strcmp(devc->trigger_source, "AC Line")) | |
550 | tmp_str = "ACL"; | |
551 | else if (!strcmp(devc->trigger_source, "CH1")) | |
552 | tmp_str = "CHAN1"; | |
553 | else if (!strcmp(devc->trigger_source, "CH2")) | |
554 | tmp_str = "CHAN2"; | |
555 | else | |
556 | tmp_str = (char *)devc->trigger_source; | |
557 | ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str); | |
558 | break; | |
559 | } | |
560 | } | |
561 | if (i == ARRAY_SIZE(trigger_sources)) | |
562 | ret = SR_ERR_ARG; | |
563 | break; | |
564 | case SR_CONF_VDIV: | |
565 | if (!probe_group) { | |
566 | sr_err("No probe group specified."); | |
567 | return SR_ERR_PROBE_GROUP; | |
568 | } | |
569 | g_variant_get(data, "(tt)", &p, &q); | |
570 | for (i = 0; i < 2; i++) { | |
571 | if (probe_group == &devc->analog_groups[i]) { | |
572 | for (j = 0; j < ARRAY_SIZE(vdivs); j++) { | |
573 | if (vdivs[j][0] != p || vdivs[j][1] != q) | |
574 | continue; | |
575 | devc->vdiv[i] = (float)p / q; | |
576 | return set_cfg(sdi, ":CHAN%d:SCAL %.3f", i + 1, | |
577 | devc->vdiv[i]); | |
578 | } | |
579 | return SR_ERR_ARG; | |
580 | } | |
581 | } | |
582 | return SR_ERR_NA; | |
583 | case SR_CONF_COUPLING: | |
584 | if (!probe_group) { | |
585 | sr_err("No probe group specified."); | |
586 | return SR_ERR_PROBE_GROUP; | |
587 | } | |
588 | tmp_str = g_variant_get_string(data, NULL); | |
589 | for (i = 0; i < 2; i++) { | |
590 | if (probe_group == &devc->analog_groups[i]) { | |
591 | for (j = 0; j < ARRAY_SIZE(coupling); j++) { | |
592 | if (!strcmp(tmp_str, coupling[j])) { | |
593 | g_free(devc->coupling[i]); | |
594 | devc->coupling[i] = g_strdup(coupling[j]); | |
595 | return set_cfg(sdi, ":CHAN%d:COUP %s", i + 1, | |
596 | devc->coupling[i]); | |
597 | } | |
598 | } | |
599 | return SR_ERR_ARG; | |
600 | } | |
601 | } | |
602 | return SR_ERR_NA; | |
603 | case SR_CONF_DATA_SOURCE: | |
604 | tmp_str = g_variant_get_string(data, NULL); | |
605 | if (!strcmp(tmp_str, "Live")) | |
606 | devc->data_source = DATA_SOURCE_LIVE; | |
607 | else if (!strcmp(tmp_str, "Memory")) | |
608 | devc->data_source = DATA_SOURCE_MEMORY; | |
609 | else if (devc->model->protocol == PROTOCOL_IEEE488_2 | |
610 | && !strcmp(tmp_str, "Segmented")) | |
611 | devc->data_source = DATA_SOURCE_SEGMENTED; | |
612 | else | |
613 | return SR_ERR; | |
614 | break; | |
615 | default: | |
616 | ret = SR_ERR_NA; | |
617 | break; | |
618 | } | |
619 | ||
620 | return ret; | |
621 | } | |
622 | ||
623 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
624 | const struct sr_probe_group *probe_group) | |
625 | { | |
626 | GVariant *tuple, *rational[2]; | |
627 | GVariantBuilder gvb; | |
628 | unsigned int i; | |
629 | struct dev_context *devc = NULL; | |
630 | ||
631 | if (sdi) | |
632 | devc = sdi->priv; | |
633 | ||
634 | if (key == SR_CONF_SCAN_OPTIONS) { | |
635 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
636 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
637 | return SR_OK; | |
638 | } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) { | |
639 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
640 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
641 | return SR_OK; | |
642 | } | |
643 | ||
644 | /* Every other option requires a valid device instance. */ | |
645 | if (!sdi || !(devc = sdi->priv)) | |
646 | return SR_ERR_ARG; | |
647 | ||
648 | /* If a probe group is specified, it must be a valid one. */ | |
649 | if (probe_group) { | |
650 | if (probe_group != &devc->analog_groups[0] | |
651 | && probe_group != &devc->analog_groups[1]) { | |
652 | sr_err("Invalid probe group specified."); | |
653 | return SR_ERR; | |
654 | } | |
655 | } | |
656 | ||
657 | switch (key) { | |
658 | break; | |
659 | case SR_CONF_DEVICE_OPTIONS: | |
660 | if (!probe_group) { | |
661 | sr_err("No probe group specified."); | |
662 | return SR_ERR_PROBE_GROUP; | |
663 | } | |
664 | if (probe_group == &devc->digital_group) { | |
665 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
666 | NULL, 0, sizeof(int32_t)); | |
667 | return SR_OK; | |
668 | } else { | |
669 | for (i = 0; i < 2; i++) { | |
670 | if (probe_group == &devc->analog_groups[i]) { | |
671 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
672 | analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t)); | |
673 | return SR_OK; | |
674 | } | |
675 | } | |
676 | return SR_ERR_NA; | |
677 | } | |
678 | break; | |
679 | case SR_CONF_COUPLING: | |
680 | if (!probe_group) { | |
681 | sr_err("No probe group specified."); | |
682 | return SR_ERR_PROBE_GROUP; | |
683 | } | |
684 | *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling)); | |
685 | break; | |
686 | case SR_CONF_VDIV: | |
687 | if (!devc) | |
688 | /* Can't know this until we have the exact model. */ | |
689 | return SR_ERR_ARG; | |
690 | if (!probe_group) { | |
691 | sr_err("No probe group specified."); | |
692 | return SR_ERR_PROBE_GROUP; | |
693 | } | |
694 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
695 | for (i = 0; i < devc->num_vdivs; i++) { | |
696 | rational[0] = g_variant_new_uint64(devc->vdivs[i][0]); | |
697 | rational[1] = g_variant_new_uint64(devc->vdivs[i][1]); | |
698 | tuple = g_variant_new_tuple(rational, 2); | |
699 | g_variant_builder_add_value(&gvb, tuple); | |
700 | } | |
701 | *data = g_variant_builder_end(&gvb); | |
702 | break; | |
703 | case SR_CONF_TIMEBASE: | |
704 | if (!devc) | |
705 | /* Can't know this until we have the exact model. */ | |
706 | return SR_ERR_ARG; | |
707 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
708 | for (i = 0; i < devc->num_timebases; i++) { | |
709 | rational[0] = g_variant_new_uint64(devc->timebases[i][0]); | |
710 | rational[1] = g_variant_new_uint64(devc->timebases[i][1]); | |
711 | tuple = g_variant_new_tuple(rational, 2); | |
712 | g_variant_builder_add_value(&gvb, tuple); | |
713 | } | |
714 | *data = g_variant_builder_end(&gvb); | |
715 | break; | |
716 | case SR_CONF_TRIGGER_SOURCE: | |
717 | if (!devc) | |
718 | /* Can't know this until we have the exact model. */ | |
719 | return SR_ERR_ARG; | |
720 | *data = g_variant_new_strv(trigger_sources, | |
721 | devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4); | |
722 | break; | |
723 | case SR_CONF_DATA_SOURCE: | |
724 | if (!devc) | |
725 | /* Can't know this until we have the exact model. */ | |
726 | return SR_ERR_ARG; | |
727 | /* This needs tweaking by series/model! */ | |
728 | if (devc->model->series == RIGOL_DS2000) | |
729 | *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources)); | |
730 | else | |
731 | *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1); | |
732 | break; | |
733 | default: | |
734 | return SR_ERR_NA; | |
735 | } | |
736 | ||
737 | return SR_OK; | |
738 | } | |
739 | ||
740 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
741 | { | |
742 | struct sr_usbtmc_dev_inst *usbtmc; | |
743 | struct dev_context *devc; | |
744 | struct sr_probe *probe; | |
745 | GSList *l; | |
746 | char cmd[256]; | |
747 | ||
748 | if (sdi->status != SR_ST_ACTIVE) | |
749 | return SR_ERR_DEV_CLOSED; | |
750 | ||
751 | usbtmc = sdi->conn; | |
752 | devc = sdi->priv; | |
753 | ||
754 | if (devc->data_source == DATA_SOURCE_LIVE) { | |
755 | if (rigol_ds_send(sdi, ":RUN") != SR_OK) | |
756 | return SR_ERR; | |
757 | } else if (devc->data_source == DATA_SOURCE_MEMORY) { | |
758 | if (devc->model->series != RIGOL_DS2000) { | |
759 | sr_err("Data source 'Memory' not supported for this device"); | |
760 | return SR_ERR; | |
761 | } | |
762 | } else if (devc->data_source == DATA_SOURCE_SEGMENTED) { | |
763 | sr_err("Data source 'Segmented' not yet supported"); | |
764 | return SR_ERR; | |
765 | } | |
766 | ||
767 | for (l = sdi->probes; l; l = l->next) { | |
768 | probe = l->data; | |
769 | sr_dbg("handling probe %s", probe->name); | |
770 | if (probe->type == SR_PROBE_ANALOG) { | |
771 | if (probe->enabled) | |
772 | devc->enabled_analog_probes = g_slist_append( | |
773 | devc->enabled_analog_probes, probe); | |
774 | if (probe->enabled != devc->analog_channels[probe->index]) { | |
775 | /* Enabled channel is currently disabled, or vice versa. */ | |
776 | sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1, | |
777 | probe->enabled ? "ON" : "OFF"); | |
778 | if (rigol_ds_send(sdi, cmd) != SR_OK) | |
779 | return SR_ERR; | |
780 | } | |
781 | } else if (probe->type == SR_PROBE_LOGIC) { | |
782 | if (probe->enabled) | |
783 | devc->enabled_digital_probes = g_slist_append( | |
784 | devc->enabled_digital_probes, probe); | |
785 | if (probe->enabled != devc->digital_channels[probe->index]) { | |
786 | /* Enabled channel is currently disabled, or vice versa. */ | |
787 | sprintf(cmd, ":DIG%d:TURN %s", probe->index, | |
788 | probe->enabled ? "ON" : "OFF"); | |
789 | if (rigol_ds_send(sdi, cmd) != SR_OK) | |
790 | return SR_ERR; | |
791 | } | |
792 | } | |
793 | } | |
794 | if (!devc->enabled_analog_probes && !devc->enabled_digital_probes) | |
795 | return SR_ERR; | |
796 | ||
797 | sr_source_add(usbtmc->fd, G_IO_IN, 50, rigol_ds_receive, (void *)sdi); | |
798 | ||
799 | /* Send header packet to the session bus. */ | |
800 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
801 | ||
802 | if (devc->model->protocol == PROTOCOL_LEGACY) { | |
803 | /* Fetch the first frame. */ | |
804 | if (devc->enabled_analog_probes) { | |
805 | devc->analog_frame_size = DS1000_ANALOG_LIVE_WAVEFORM_SIZE; | |
806 | devc->channel_frame = devc->enabled_analog_probes->data; | |
807 | if (rigol_ds_send(sdi, ":WAV:DATA? CHAN%d", | |
808 | devc->channel_frame->index + 1) != SR_OK) | |
809 | return SR_ERR; | |
810 | } else { | |
811 | devc->channel_frame = devc->enabled_digital_probes->data; | |
812 | if (rigol_ds_send(sdi, ":WAV:DATA? DIG") != SR_OK) | |
813 | return SR_ERR; | |
814 | } | |
815 | ||
816 | devc->num_frame_bytes = 0; | |
817 | } else { | |
818 | if (devc->enabled_analog_probes) { | |
819 | if (devc->data_source == DATA_SOURCE_MEMORY) | |
820 | { | |
821 | if (g_slist_length(devc->enabled_analog_probes) == 1) | |
822 | devc->analog_frame_size = DS2000_ANALOG_MEM_WAVEFORM_SIZE_1C; | |
823 | else | |
824 | devc->analog_frame_size = DS2000_ANALOG_MEM_WAVEFORM_SIZE_2C; | |
825 | /* Apparently for the DS2000 the memory | |
826 | * depth can only be set in Running state - | |
827 | * this matches the behaviour of the UI. */ | |
828 | if (rigol_ds_send(sdi, ":RUN") != SR_OK) | |
829 | return SR_ERR; | |
830 | if (rigol_ds_send(sdi, "ACQ:MDEP %d", devc->analog_frame_size) != SR_OK) | |
831 | return SR_ERR; | |
832 | if (rigol_ds_send(sdi, ":STOP") != SR_OK) | |
833 | return SR_ERR; | |
834 | } else | |
835 | devc->analog_frame_size = DS2000_ANALOG_LIVE_WAVEFORM_SIZE; | |
836 | devc->channel_frame = devc->enabled_analog_probes->data; | |
837 | if (rigol_ds_capture_start(sdi) != SR_OK) | |
838 | return SR_ERR; | |
839 | } | |
840 | } | |
841 | ||
842 | return SR_OK; | |
843 | } | |
844 | ||
845 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
846 | { | |
847 | struct dev_context *devc; | |
848 | struct sr_usbtmc_dev_inst *usbtmc; | |
849 | ||
850 | (void)cb_data; | |
851 | ||
852 | devc = sdi->priv; | |
853 | ||
854 | if (sdi->status != SR_ST_ACTIVE) { | |
855 | sr_err("Device inactive, can't stop acquisition."); | |
856 | return SR_ERR; | |
857 | } | |
858 | ||
859 | g_slist_free(devc->enabled_analog_probes); | |
860 | g_slist_free(devc->enabled_digital_probes); | |
861 | devc->enabled_analog_probes = NULL; | |
862 | devc->enabled_digital_probes = NULL; | |
863 | usbtmc = sdi->conn; | |
864 | sr_source_remove(usbtmc->fd); | |
865 | ||
866 | return SR_OK; | |
867 | } | |
868 | ||
869 | SR_PRIV struct sr_dev_driver rigol_ds_driver_info = { | |
870 | .name = "rigol-ds", | |
871 | .longname = "Rigol DS", | |
872 | .api_version = 1, | |
873 | .init = init, | |
874 | .cleanup = cleanup, | |
875 | .scan = scan, | |
876 | .dev_list = dev_list, | |
877 | .dev_clear = dev_clear, | |
878 | .config_get = config_get, | |
879 | .config_set = config_set, | |
880 | .config_list = config_list, | |
881 | .dev_open = dev_open, | |
882 | .dev_close = dev_close, | |
883 | .dev_acquisition_start = dev_acquisition_start, | |
884 | .dev_acquisition_stop = dev_acquisition_stop, | |
885 | .priv = NULL, | |
886 | }; |