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