]> sigrok.org Git - libsigrok.git/blob - hardware/rigol-ds1xx2/api.c
4023ecca1ef4d2a4bd705d442f5af799cf87736c
[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 #define NUM_PROBES 2
30
31 static const int hwcaps[] = {
32         SR_HWCAP_OSCILLOSCOPE,
33         SR_HWCAP_LIMIT_SAMPLES,
34         SR_HWCAP_TIMEBASE,
35         SR_HWCAP_TRIGGER_SOURCE,
36         SR_HWCAP_TRIGGER_SLOPE,
37         SR_HWCAP_HORIZ_TRIGGERPOS,
38         SR_HWCAP_VDIV,
39         SR_HWCAP_COUPLING,
40         0,
41 };
42
43 static const char *probe_names[] = {
44         "CH1", "CH2",
45         NULL,
46 };
47
48 static const struct sr_rational timebases[] = {
49         /* nanoseconds */
50         { 2, 1000000000 },
51         { 5, 1000000000 },
52         { 10, 1000000000 },
53         { 20, 1000000000 },
54         { 50, 1000000000 },
55         { 100, 1000000000 },
56         { 500, 1000000000 },
57         /* microseconds */
58         { 1, 1000000 },
59         { 2, 1000000 },
60         { 5, 1000000 },
61         { 10, 1000000 },
62         { 20, 1000000 },
63         { 50, 1000000 },
64         { 100, 1000000 },
65         { 200, 1000000 },
66         { 500, 1000000 },
67         /* milliseconds */
68         { 1, 1000 },
69         { 2, 1000 },
70         { 5, 1000 },
71         { 10, 1000 },
72         { 20, 1000 },
73         { 50, 1000 },
74         { 100, 1000 },
75         { 200, 1000 },
76         { 500, 1000 },
77         /* seconds */
78         { 1, 1 },
79         { 2, 1 },
80         { 5, 1 },
81         { 10, 1 },
82         { 20, 1 },
83         { 50, 1 },
84         { 0, 0},
85 };
86
87 static const struct sr_rational vdivs[] = {
88         /* millivolts */
89         { 2, 1000 },
90         { 5, 1000 },
91         { 10, 1000 },
92         { 20, 1000 },
93         { 50, 1000 },
94         { 100, 1000 },
95         { 200, 1000 },
96         { 500, 1000 },
97         /* volts */
98         { 1, 1 },
99         { 2, 1 },
100         { 5, 1 },
101         { 10, 1 },
102         { 0, 0 },
103 };
104
105 static const char *trigger_sources[] = {
106         "CH1",
107         "CH2",
108         "EXT",
109         "AC Line",
110         NULL,
111 };
112
113 static const char *coupling[] = {
114         "AC",
115         "DC",
116         "GND",
117         NULL,
118 };
119
120 static const char *supported_models[] = {
121         "DS1052E",
122         "DS1102E",
123         "DS1052D",
124         "DS1102D"
125 };
126
127 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info;
128 static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info;
129
130 /* Properly close and free all devices. */
131 static int clear_instances(void)
132 {
133         struct sr_dev_inst *sdi;
134         struct drv_context *drvc;
135         struct dev_context *devc;
136         GSList *l;
137
138         if (!(drvc = di->priv))
139                 return SR_OK;
140
141         for (l = drvc->instances; l; l = l->next) {
142                 if (!(sdi = l->data))
143                         continue;
144                 if (!(devc = sdi->priv))
145                         continue;
146
147                 g_free(devc->device);
148                 close(devc->fd);
149
150                 sr_dev_inst_free(sdi);
151         }
152
153         g_slist_free(drvc->instances);
154         drvc->instances = NULL;
155
156         return SR_OK;
157 }
158
159 static int hw_init(struct sr_context *sr_ctx)
160 {
161         struct drv_context *drvc;
162         (void)sr_ctx;
163
164         if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
165                 sr_err("Driver context malloc failed.");
166                 return SR_ERR_MALLOC;
167         }
168
169         di->priv = drvc;
170
171         return SR_OK;
172 }
173
174 static GSList *hw_scan(GSList *options)
175 {
176         struct drv_context *drvc;
177         struct sr_dev_inst *sdi;
178         struct dev_context *devc;
179         struct sr_probe *probe;
180         GSList *devices;
181         GDir *dir;
182         const gchar *dev_name;
183         const gchar *dev_dir = "/dev/";
184         const gchar *prefix = "usbtmc";
185         gchar *device;
186         const gchar *idn_query = "*IDN?";
187         int len, num_tokens, fd, i;
188         const gchar *delimiter = ",";
189         gchar **tokens;
190         const char *manufacturer, *model, *version;
191         int num_models;
192         gboolean matched = FALSE;
193         char buf[256];
194
195         (void)options;
196
197         devices = NULL;
198         drvc = di->priv;
199         drvc->instances = NULL;
200
201         dir = g_dir_open("/sys/class/usb/", 0, NULL);
202
203         if (dir == NULL)
204                 return NULL;
205
206         while ((dev_name = g_dir_read_name(dir)) != NULL) {
207                 if (strncmp(dev_name, prefix, strlen(prefix))) 
208                         continue;
209
210                 device = g_strconcat(dev_dir, dev_name, NULL);
211
212                 fd = open(device, O_RDWR);
213                 len = write(fd, idn_query, strlen(idn_query));
214                 len = read(fd, buf, sizeof(buf));
215                 close(fd);
216                 if (len == 0) {
217                         g_free(device);
218                         return NULL;
219                 }
220
221                 buf[len] = 0;
222                 tokens = g_strsplit(buf, delimiter, 0);
223                 close(fd);
224                 sr_dbg("response: %s %d [%s]", device, len, buf);
225
226                 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
227
228                 if (num_tokens < 4) {
229                         g_strfreev(tokens);
230                         g_free(device);
231                         return NULL;
232                 }
233
234                 manufacturer = tokens[0];
235                 model = tokens[1];
236                 version = tokens[3];
237
238                 if (strcmp(manufacturer, "Rigol Technologies")) {
239                         g_strfreev(tokens);
240                         g_free(device);
241                         return NULL;
242                 }
243
244                 num_models = sizeof(supported_models) / sizeof(supported_models[0]);
245
246                 for (i = 0; i < num_models; i++) {
247                         if (!strcmp(model, supported_models[i])) {
248                                 matched = 1;
249                                 break;
250                         }
251                 }
252
253                 if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
254                         manufacturer, model, version))) {
255                         g_strfreev(tokens);
256                         g_free(device);
257                         return NULL;
258                 }
259
260                 g_strfreev(tokens);
261
262                 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
263                         sr_err("Device context malloc failed.");
264                         g_free(device);
265                         return NULL;
266                 }
267
268                 devc->device = device;
269
270                 sdi->priv = devc;
271                 sdi->driver = di;
272
273                 for (i = 0; i < 2; i++) {
274                         if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
275                             i == 0 ? "CH1" : "CH2")))
276                                 return NULL;
277                         sdi->probes = g_slist_append(sdi->probes, probe);
278                 }
279
280                 drvc->instances = g_slist_append(drvc->instances, sdi);
281                 devices = g_slist_append(devices, sdi);
282         }
283
284         g_dir_close(dir);
285
286         return devices;
287 }
288
289 static GSList *hw_dev_list(void)
290 {
291         struct drv_context *drvc;
292
293         drvc = di->priv;
294
295         return drvc->instances;
296 }
297
298 static int hw_dev_open(struct sr_dev_inst *sdi)
299 {
300         struct dev_context *devc;
301         int fd;
302
303         devc = sdi->priv;
304
305         if ((fd = open(devc->device, O_RDWR)) == -1)
306                 return SR_ERR;
307
308         devc->fd = fd;
309
310         devc->scale = 1;
311
312         return SR_OK;
313 }
314
315 static int hw_dev_close(struct sr_dev_inst *sdi)
316 {
317         struct dev_context *devc;
318
319         devc = sdi->priv;
320
321         close(devc->fd);
322
323         return SR_OK;
324 }
325
326 static int hw_cleanup(void)
327 {
328         clear_instances();
329
330         return SR_OK;
331 }
332
333 static int hw_info_get(int info_id, const void **data,
334                        const struct sr_dev_inst *sdi)
335 {
336         (void)sdi;
337
338         switch (info_id) {
339         case SR_DI_HWCAPS:
340                 *data = hwcaps;
341                 break;
342         case SR_DI_NUM_PROBES:
343                 *data = GINT_TO_POINTER(NUM_PROBES);
344                 break;
345         case SR_DI_PROBE_NAMES:
346                 *data = probe_names;
347                 break;
348         case SR_DI_TIMEBASES:
349                 *data = timebases;
350                 break;
351         case SR_DI_TRIGGER_SOURCES:
352                 *data = trigger_sources;
353                 break;
354         case SR_DI_VDIVS:
355                 *data = vdivs;
356                 break;
357         case SR_DI_COUPLING:
358                 *data = coupling;
359                 break;
360         default:
361                 sr_err("Unknown info_id: %d.", info_id);
362                 return SR_ERR_ARG;
363         }
364
365         return SR_OK;
366 }
367
368 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
369                              const void *value)
370 {
371         struct dev_context *devc;
372         uint64_t tmp_u64;
373         float tmp_float;
374         struct sr_rational tmp_rat;
375         int ret, i, j;
376         char *channel;
377
378         devc = sdi->priv;
379
380         if (sdi->status != SR_ST_ACTIVE) {
381                 sr_err("Device inactive, can't set config options.");
382                 return SR_ERR;
383         }
384
385         ret = SR_OK;
386         switch (hwcap) {
387         case SR_HWCAP_LIMIT_FRAMES:
388                 devc->limit_frames = *(const uint64_t *)value;
389                 break;
390         case SR_HWCAP_TRIGGER_SLOPE:
391                 tmp_u64 = *(const int *)value;
392                 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s\n",
393                                        tmp_u64 ? "POS" : "NEG");
394                 break;
395         case SR_HWCAP_HORIZ_TRIGGERPOS:
396                 tmp_float = *(const float *)value;
397                 rigol_ds1xx2_send_data(devc->fd, ":TIM:OFFS %.9f\n", tmp_float);
398                 break;
399         case SR_HWCAP_TIMEBASE:
400                 tmp_rat = *(const struct sr_rational *)value;
401                 rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f\n",
402                                        (float)tmp_rat.p / tmp_rat.q);
403                 break;
404         case SR_HWCAP_TRIGGER_SOURCE:
405                 if (!strcmp(value, "CH1"))
406                         channel = "CHAN1";
407                 else if (!strcmp(value, "CH2"))
408                         channel = "CHAN2";
409                 else if (!strcmp(value, "EXT"))
410                         channel = "EXT";
411                 else if (!strcmp(value, "AC Line"))
412                         channel = "ACL";
413                 else {
414                         ret = SR_ERR_ARG;
415                         break;
416                 }
417                 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s\n", channel);
418                 break;
419         case SR_HWCAP_VDIV:
420                 /* TODO: Not supporting vdiv per channel yet. */
421                 tmp_rat = *(const struct sr_rational *)value;
422                 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
423                         if (vdivs[i].p == tmp_rat.p
424                                         && vdivs[i].q == tmp_rat.q) {
425                                 devc->scale = (float)tmp_rat.p / tmp_rat.q;
426                                 for (j = 0; j < 2; j++)
427                                         rigol_ds1xx2_send_data(devc->fd,
428                                          ":CHAN%d:SCAL %.3f\n", j, devc->scale);
429                                 break;
430                         }
431                 }
432                 if (vdivs[i].p == 0 && vdivs[i].q == 0)
433                         ret = SR_ERR_ARG;
434                 break;
435         case SR_HWCAP_COUPLING:
436                 /* TODO: Not supporting coupling per channel yet. */
437                 for (i = 0; coupling[i]; i++) {
438                         if (!strcmp(value, coupling[i])) {
439                                 for (j = 0; j < 2; j++)
440                                         rigol_ds1xx2_send_data(devc->fd,
441                                           ":CHAN%d:COUP %s\n", j, coupling[i]);
442                                 break;
443                         }
444                 }
445                 if (coupling[i] == 0)
446                         ret = SR_ERR_ARG;
447                 break;
448         default:
449                 sr_err("Unknown hardware capability: %d.", hwcap);
450                 ret = SR_ERR_ARG;
451                 break;
452         }
453
454         return ret;
455 }
456
457 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
458                                     void *cb_data)
459 {
460         struct dev_context *devc;
461         struct sr_datafeed_packet packet;
462         struct sr_datafeed_header header;
463         struct sr_datafeed_meta_analog meta;
464         char buf[256];
465         int len;
466
467         (void)cb_data;
468
469         devc = sdi->priv;
470
471         devc->num_frames = 0;
472
473         sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi);
474
475         /* Send header packet to the session bus. */
476         packet.type = SR_DF_HEADER;
477         packet.payload = (unsigned char *)&header;
478         header.feed_version = 1;
479         gettimeofday(&header.starttime, NULL);
480         sr_session_send(cb_data, &packet);
481
482         /* Send metadata about the SR_DF_ANALOG packets to come. */
483         packet.type = SR_DF_META_ANALOG;
484         packet.payload = &meta;
485         meta.num_probes = NUM_PROBES;
486         sr_session_send(cb_data, &packet);
487
488         rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?\n");
489         len = read(devc->fd, buf, sizeof(buf));
490         buf[len] = 0;
491         devc->scale = atof(buf);
492         sr_dbg("Scale is %.3f.", devc->scale);
493         rigol_ds1xx2_send_data(devc->fd, ":CHAN1:OFFS?\n");
494         len = read(devc->fd, buf, sizeof(buf));
495         buf[len] = 0;
496         devc->offset = atof(buf);
497         sr_dbg("Offset is %.6f.", devc->offset);
498         rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n");
499
500         return SR_OK;
501 }
502
503 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
504 {
505         struct dev_context *devc;
506
507         (void)cb_data;
508
509         devc = sdi->priv;
510
511         if (sdi->status != SR_ST_ACTIVE) {
512                 sr_err("Device inactive, can't stop acquisition.");
513                 return SR_ERR;
514         }
515
516         sr_source_remove(devc->fd);
517
518         return SR_OK;
519 }
520
521 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
522         .name = "rigol-ds1xx2",
523         .longname = "Rigol DS1xx2",
524         .api_version = 1,
525         .init = hw_init,
526         .cleanup = hw_cleanup,
527         .scan = hw_scan,
528         .dev_list = hw_dev_list,
529         .dev_clear = clear_instances,
530         .dev_open = hw_dev_open,
531         .dev_close = hw_dev_close,
532         .info_get = hw_info_get,
533         .dev_config_set = hw_dev_config_set,
534         .dev_acquisition_start = hw_dev_acquisition_start,
535         .dev_acquisition_stop = hw_dev_acquisition_stop,
536         .priv = NULL,
537 };