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