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