]> sigrok.org Git - libsigrok.git/blob - hardware/rigol-ds1xx2/api.c
rigol-ds1xx2: Whitespace, minor fix.
[libsigrok.git] / hardware / rigol-ds1xx2 / api.c
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  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <glib.h>
27 #include "libsigrok.h"
28 #include "libsigrok-internal.h"
29 #include "protocol.h"
30
31 #define NUM_TIMEBASE  12
32 #define NUM_VDIV      8
33
34 static const int32_t hwcaps[] = {
35         SR_CONF_OSCILLOSCOPE,
36         SR_CONF_LIMIT_SAMPLES,
37         SR_CONF_TIMEBASE,
38         SR_CONF_TRIGGER_SOURCE,
39         SR_CONF_TRIGGER_SLOPE,
40         SR_CONF_HORIZ_TRIGGERPOS,
41         SR_CONF_VDIV,
42         SR_CONF_COUPLING,
43         SR_CONF_NUM_TIMEBASE,
44         SR_CONF_NUM_VDIV,
45 };
46
47 static const uint64_t timebases[][2] = {
48         /* nanoseconds */
49         { 2, 1000000000 },
50         { 5, 1000000000 },
51         { 10, 1000000000 },
52         { 20, 1000000000 },
53         { 50, 1000000000 },
54         { 100, 1000000000 },
55         { 500, 1000000000 },
56         /* microseconds */
57         { 1, 1000000 },
58         { 2, 1000000 },
59         { 5, 1000000 },
60         { 10, 1000000 },
61         { 20, 1000000 },
62         { 50, 1000000 },
63         { 100, 1000000 },
64         { 200, 1000000 },
65         { 500, 1000000 },
66         /* milliseconds */
67         { 1, 1000 },
68         { 2, 1000 },
69         { 5, 1000 },
70         { 10, 1000 },
71         { 20, 1000 },
72         { 50, 1000 },
73         { 100, 1000 },
74         { 200, 1000 },
75         { 500, 1000 },
76         /* seconds */
77         { 1, 1 },
78         { 2, 1 },
79         { 5, 1 },
80         { 10, 1 },
81         { 20, 1 },
82         { 50, 1 },
83 };
84
85 static const uint64_t vdivs[][2] = {
86         /* millivolts */
87         { 2, 1000 },
88         { 5, 1000 },
89         { 10, 1000 },
90         { 20, 1000 },
91         { 50, 1000 },
92         { 100, 1000 },
93         { 200, 1000 },
94         { 500, 1000 },
95         /* volts */
96         { 1, 1 },
97         { 2, 1 },
98         { 5, 1 },
99         { 10, 1 },
100 };
101
102 static const char *trigger_sources[] = {
103         "CH1",
104         "CH2",
105         "EXT",
106         "AC Line",
107 };
108
109 static const char *coupling[] = {
110         "AC",
111         "DC",
112         "GND",
113 };
114
115 static const char *supported_models[] = {
116         "DS1052E",
117         "DS1102E",
118         "DS1052D",
119         "DS1102D",
120 };
121
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
142                 g_free(devc->device);
143                 g_free(devc->coupling[0]);
144                 g_free(devc->coupling[1]);
145                 g_free(devc->trigger_source);
146                 g_free(devc->trigger_slope);
147                 close(devc->fd);
148
149                 sr_dev_inst_free(sdi);
150         }
151
152         g_slist_free(drvc->instances);
153         drvc->instances = NULL;
154
155         return SR_OK;
156 }
157
158 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
159 {
160         struct dev_context *devc;
161         struct timespec delay;
162         va_list args;
163         char buf[256];
164
165         devc = sdi->priv;
166
167         va_start(args, format);
168         vsnprintf(buf, 255, format, args);
169         va_end(args);
170         if (rigol_ds1xx2_send(devc, buf) != SR_OK)
171                 return SR_ERR;
172
173         /* When setting a bunch of parameters in a row, the DS1052E scrambles
174          * some of them unless there is at least 100ms delay in between. */
175         delay.tv_sec = 0;
176         delay.tv_nsec = 100000000L;
177         sr_spew("delay %dms", delay.tv_nsec / 1000000);
178         nanosleep(&delay, NULL);
179
180         return SR_OK;
181 }
182
183 static int hw_init(struct sr_context *sr_ctx)
184 {
185         return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
186 }
187
188 static GSList *hw_scan(GSList *options)
189 {
190         struct drv_context *drvc;
191         struct sr_dev_inst *sdi;
192         struct dev_context *devc;
193         struct sr_probe *probe;
194         GSList *devices;
195         GDir *dir;
196         const gchar *dev_name;
197         const gchar *dev_dir = "/dev/";
198         const gchar *prefix = "usbtmc";
199         gchar *device;
200         const gchar *idn_query = "*IDN?";
201         unsigned int i;
202         int len, num_tokens, fd;
203         const gchar *delimiter = ",";
204         gchar **tokens;
205         const char *manufacturer, *model, *version;
206         gboolean matched = FALSE;
207         char buf[256];
208
209         (void)options;
210
211         drvc = di->priv;
212         drvc->instances = NULL;
213
214         devices = NULL;
215
216         dir = g_dir_open("/sys/class/usb/", 0, NULL);
217
218         if (dir == NULL)
219                 return NULL;
220
221         while ((dev_name = g_dir_read_name(dir)) != NULL) {
222                 if (strncmp(dev_name, prefix, strlen(prefix))) 
223                         continue;
224
225                 device = g_strconcat(dev_dir, dev_name, NULL);
226
227                 fd = open(device, O_RDWR);
228                 len = write(fd, idn_query, strlen(idn_query));
229                 len = read(fd, buf, sizeof(buf));
230                 close(fd);
231                 if (len == 0) {
232                         g_free(device);
233                         return NULL;
234                 }
235
236                 buf[len] = 0;
237                 tokens = g_strsplit(buf, delimiter, 0);
238                 close(fd);
239                 sr_dbg("response: %s %d [%s]", device, len, buf);
240
241                 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
242
243                 if (num_tokens < 4) {
244                         g_strfreev(tokens);
245                         g_free(device);
246                         return NULL;
247                 }
248
249                 manufacturer = tokens[0];
250                 model = tokens[1];
251                 version = tokens[3];
252
253                 if (strcmp(manufacturer, "Rigol Technologies")) {
254                         g_strfreev(tokens);
255                         g_free(device);
256                         return NULL;
257                 }
258
259                 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
260                         if (!strcmp(model, supported_models[i])) {
261                                 matched = 1;
262                                 break;
263                         }
264                 }
265
266                 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
267                         manufacturer, model, version))) {
268                         g_strfreev(tokens);
269                         g_free(device);
270                         return NULL;
271                 }
272
273                 g_strfreev(tokens);
274
275                 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
276                         sr_err("Device context malloc failed.");
277                         g_free(device);
278                         return NULL;
279                 }
280                 devc->limit_frames = 0;
281                 devc->device = device;
282                 sdi->priv = devc;
283                 sdi->driver = di;
284
285                 for (i = 0; i < 2; i++) {
286                         if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
287                             i == 0 ? "CH1" : "CH2")))
288                                 return NULL;
289                         sdi->probes = g_slist_append(sdi->probes, probe);
290                 }
291
292                 drvc->instances = g_slist_append(drvc->instances, sdi);
293                 devices = g_slist_append(devices, sdi);
294         }
295
296         g_dir_close(dir);
297
298         return devices;
299 }
300
301 static GSList *hw_dev_list(void)
302 {
303         return ((struct drv_context *)(di->priv))->instances;
304 }
305
306 static int hw_dev_open(struct sr_dev_inst *sdi)
307 {
308         struct dev_context *devc;
309         int fd;
310
311         devc = sdi->priv;
312
313         if ((fd = open(devc->device, O_RDWR)) == -1)
314                 return SR_ERR;
315         devc->fd = fd;
316
317         if (rigol_ds1xx2_get_dev_cfg(sdi) != SR_OK)
318                 /* TODO: force configuration? */
319                 return SR_ERR;
320
321         return SR_OK;
322 }
323
324 static int hw_dev_close(struct sr_dev_inst *sdi)
325 {
326         struct dev_context *devc;
327
328         devc = sdi->priv;
329
330         close(devc->fd);
331
332         return SR_OK;
333 }
334
335 static int hw_cleanup(void)
336 {
337         clear_instances();
338
339         return SR_OK;
340 }
341
342 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
343 {
344
345         (void)sdi;
346
347         switch (id) {
348         case SR_CONF_NUM_TIMEBASE:
349                 *data = g_variant_new_int32(NUM_TIMEBASE);
350                 break;
351         case SR_CONF_NUM_VDIV:
352                 *data = g_variant_new_int32(NUM_VDIV);
353                 break;
354         default:
355                 return SR_ERR_ARG;
356         }
357
358         return SR_OK;
359 }
360
361 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
362 {
363         struct dev_context *devc;
364         uint64_t tmp_u64, p, q;
365         double t_dbl;
366         unsigned int i;
367         int ret;
368         const char *tmp_str;
369
370         devc = sdi->priv;
371
372         if (sdi->status != SR_ST_ACTIVE) {
373                 sr_err("Device inactive, can't set config options.");
374                 return SR_ERR;
375         }
376
377         ret = SR_OK;
378         switch (id) {
379         case SR_CONF_LIMIT_FRAMES:
380                 devc->limit_frames = g_variant_get_uint64(data);
381                 break;
382         case SR_CONF_TRIGGER_SLOPE:
383                 tmp_u64 = g_variant_get_uint64(data);
384                 if (tmp_u64 != 0 && tmp_u64 != 1)
385                         return SR_ERR;
386                 g_free(devc->trigger_slope);
387                 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
388                 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
389                 break;
390         case SR_CONF_HORIZ_TRIGGERPOS:
391                 t_dbl = g_variant_get_double(data);
392                 if (t_dbl < 0.0 || t_dbl > 1.0)
393                         return SR_ERR;
394                 devc->horiz_triggerpos = t_dbl;
395                 /* We have the trigger offset as a percentage of the frame, but
396                  * need to express this in seconds. */
397                 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * NUM_TIMEBASE;
398                 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
399                 break;
400         case SR_CONF_TIMEBASE:
401                 g_variant_get(data, "(tt)", &p, &q);
402                 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
403                         if (timebases[i][0] == p && timebases[i][1] == q) {
404                                 devc->timebase = (float)p / q;
405                                 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
406                                 break;
407                         }
408                 }
409                 if (i == ARRAY_SIZE(timebases))
410                         ret = SR_ERR_ARG;
411                 break;
412         case SR_CONF_TRIGGER_SOURCE:
413                 tmp_str = g_variant_get_string(data, NULL);
414                 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
415                         if (!strcmp(trigger_sources[i], tmp_str)) {
416                                 g_free(devc->trigger_source);
417                                 devc->trigger_source = g_strdup(trigger_sources[i]);
418                                 if (!strcmp(devc->trigger_source, "AC Line"))
419                                         tmp_str = "ACL";
420                                 else if (!strcmp(devc->trigger_source, "CH1"))
421                                         tmp_str = "CHAN1";
422                                 else if (!strcmp(devc->trigger_source, "CH2"))
423                                         tmp_str = "CHAN2";
424                                 else
425                                         tmp_str = (char *)devc->trigger_source;
426                                 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
427                                 break;
428                         }
429                 }
430                 if (i == ARRAY_SIZE(trigger_sources))
431                         ret = SR_ERR_ARG;
432                 break;
433         case SR_CONF_VDIV:
434                 g_variant_get(data, "(tt)", &p, &q);
435                 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
436                         if (vdivs[i][0] != p || vdivs[i][1] != q)
437                                 continue;
438                         devc->vdiv[0] = devc->vdiv[1] = (float)p / q;
439                         set_cfg(sdi, ":CHAN1:SCAL %.3f", devc->vdiv[0]);
440                         ret = set_cfg(sdi, ":CHAN2:SCAL %.3f", devc->vdiv[1]);
441                         break;
442                 }
443                 if (i == ARRAY_SIZE(vdivs))
444                         ret = SR_ERR_ARG;
445                 break;
446         case SR_CONF_COUPLING:
447                 /* TODO: Not supporting coupling per channel yet. */
448                 tmp_str = g_variant_get_string(data, NULL);
449                 for (i = 0; i < ARRAY_SIZE(coupling); i++) {
450                         if (!strcmp(tmp_str, coupling[i])) {
451                                 g_free(devc->coupling[0]);
452                                 g_free(devc->coupling[1]);
453                                 devc->coupling[0] = g_strdup(coupling[i]);
454                                 devc->coupling[1] = g_strdup(coupling[i]);
455                                 set_cfg(sdi, ":CHAN1:COUP %s", devc->coupling[0]);
456                                 ret = set_cfg(sdi, ":CHAN2:COUP %s", devc->coupling[1]);
457                                 break;
458                         }
459                 }
460                 if (i == ARRAY_SIZE(coupling))
461                         ret = SR_ERR_ARG;
462                 break;
463         default:
464                 sr_err("Unknown hardware capability: %d.", id);
465                 ret = SR_ERR_ARG;
466                 break;
467         }
468
469         return ret;
470 }
471
472 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
473 {
474         GVariant *tuple, *rational[2];
475         GVariantBuilder gvb;
476         unsigned int i;
477
478         (void)sdi;
479
480         switch (key) {
481         case SR_CONF_DEVICE_OPTIONS:
482                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
483                                 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
484                 break;
485         case SR_CONF_COUPLING:
486                 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
487                 break;
488         case SR_CONF_VDIV:
489                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
490                 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
491                         rational[0] = g_variant_new_uint64(vdivs[i][0]);
492                         rational[1] = g_variant_new_uint64(vdivs[i][1]);
493                         tuple = g_variant_new_tuple(rational, 2);
494                         g_variant_builder_add_value(&gvb, tuple);
495                 }
496                 *data = g_variant_builder_end(&gvb);
497                 break;
498         case SR_CONF_TIMEBASE:
499                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
500                 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
501                         rational[0] = g_variant_new_uint64(timebases[i][0]);
502                         rational[1] = g_variant_new_uint64(timebases[i][1]);
503                         tuple = g_variant_new_tuple(rational, 2);
504                         g_variant_builder_add_value(&gvb, tuple);
505                 }
506                 *data = g_variant_builder_end(&gvb);
507                 break;
508         case SR_CONF_TRIGGER_SOURCE:
509                 *data = g_variant_new_strv(trigger_sources,
510                                 ARRAY_SIZE(trigger_sources));
511                 break;
512         default:
513                 return SR_ERR_ARG;
514         }
515
516         return SR_OK;
517 }
518
519 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
520 {
521         struct dev_context *devc;
522         struct sr_probe *probe;
523         GSList *l;
524         int probenum;
525         char cmd[256];
526
527         (void)cb_data;
528
529         devc = sdi->priv;
530
531         for (l = sdi->probes; l; l = l->next) {
532                 probe = l->data;
533                 probenum = probe->name[2] == '1' ? 0 : 1;
534                 if (probe->enabled)
535                         devc->enabled_probes = g_slist_append(devc->enabled_probes, probe);
536
537                 if (probe->enabled != devc->channels[probenum]) {
538                         /* Enabled channel is currently disabled, or vice versa. */
539                         sprintf(cmd, ":CHAN%d:DISP %s", probenum + 1,
540                                         probe->enabled ? "ON" : "OFF");
541                         if (rigol_ds1xx2_send(devc, cmd) != SR_OK)
542                                 return SR_ERR;
543                 }
544         }
545         if (!devc->enabled_probes)
546                 return SR_ERR;
547
548         sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive, (void *)sdi);
549
550         /* Send header packet to the session bus. */
551         std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
552
553         /* Fetch the first frame. */
554         devc->channel_frame = devc->enabled_probes->data;
555         if (rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%c",
556                         devc->channel_frame->name[2]) != SR_OK)
557                 return SR_ERR;
558
559         return SR_OK;
560 }
561
562 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
563 {
564         struct dev_context *devc;
565
566         (void)cb_data;
567
568         devc = sdi->priv;
569
570         if (sdi->status != SR_ST_ACTIVE) {
571                 sr_err("Device inactive, can't stop acquisition.");
572                 return SR_ERR;
573         }
574
575         g_slist_free(devc->enabled_probes);
576         devc->enabled_probes = NULL;
577         sr_source_remove(devc->fd);
578
579         return SR_OK;
580 }
581
582 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
583         .name = "rigol-ds1xx2",
584         .longname = "Rigol DS1xx2",
585         .api_version = 1,
586         .init = hw_init,
587         .cleanup = hw_cleanup,
588         .scan = hw_scan,
589         .dev_list = hw_dev_list,
590         .dev_clear = clear_instances,
591         .config_get = config_get,
592         .config_set = config_set,
593         .config_list = config_list,
594         .dev_open = hw_dev_open,
595         .dev_close = hw_dev_close,
596         .dev_acquisition_start = dev_acquisition_start,
597         .dev_acquisition_stop = dev_acquisition_stop,
598         .priv = NULL,
599 };