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