]> sigrok.org Git - libsigrok.git/blob - hardware/rigol-ds1xx2/api.c
rigol-ds1xx2: First working version.
[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 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                 close(devc->fd);
141
142                 sr_dev_inst_free(sdi);
143         }
144
145         g_slist_free(drvc->instances);
146         drvc->instances = NULL;
147
148         return SR_OK;
149 }
150
151 static int hw_init(struct sr_context *sr_ctx)
152 {
153         struct drv_context *drvc;
154         (void)sr_ctx;
155
156         if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
157                 sr_err("Driver context malloc failed.");
158                 return SR_ERR_MALLOC;
159         }
160
161         di->priv = drvc;
162
163         return SR_OK;
164 }
165
166 static GSList *hw_scan(GSList *options)
167 {
168         struct drv_context *drvc;
169         struct sr_dev_inst *sdi;
170         struct dev_context *devc;
171         struct sr_probe *probe;
172         GSList *devices;
173         int i;
174
175         (void)options;
176
177         devices = NULL;
178         drvc = di->priv;
179         drvc->instances = NULL;
180
181         if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, "Rigol", "DS1xx2", NULL)))
182                 return NULL;
183         if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
184                 return NULL;
185
186         sdi->priv = devc;
187         sdi->driver = di;
188
189         for (i = 0; i < 2; i++)
190         {
191                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, i == 0 ? "CH1" : "CH2")))
192                         return NULL;
193                 sdi->probes = g_slist_append(sdi->probes, probe);
194         }
195         drvc->instances = g_slist_append(drvc->instances, sdi);
196         devices = g_slist_append(devices, sdi);
197
198         return devices;
199 }
200
201 static GSList *hw_dev_list(void)
202 {
203         struct drv_context *drvc;
204
205         drvc = di->priv;
206
207         return drvc->instances;
208 }
209
210 static int hw_dev_open(struct sr_dev_inst *sdi)
211 {
212         int fd = open("/dev/usbtmc1", O_RDWR);
213
214         if (fd == -1)
215                 return SR_ERR;
216
217         struct dev_context *devc = sdi->priv;
218         devc->fd = fd;
219
220         devc->scale = 1;
221
222         return SR_OK;
223 }
224
225 static int hw_dev_close(struct sr_dev_inst *sdi)
226 {
227         struct dev_context *devc = sdi->priv;
228
229         close(devc->fd);
230
231         return SR_OK;
232 }
233
234 static int hw_cleanup(void)
235 {
236         clear_instances();
237
238         return SR_OK;
239 }
240
241 static int hw_info_get(int info_id, const void **data,
242                        const struct sr_dev_inst *sdi)
243 {
244         (void)sdi;
245
246         switch (info_id) {
247         case SR_DI_HWCAPS:
248                 *data = hwcaps;
249                 break;
250         case SR_DI_NUM_PROBES:
251                 *data = GINT_TO_POINTER(NUM_PROBES);
252                 break;
253         case SR_DI_PROBE_NAMES:
254                 *data = probe_names;
255                 break;
256         case SR_DI_TIMEBASES:
257                 *data = timebases;
258                 break;
259         case SR_DI_TRIGGER_SOURCES:
260                 *data = trigger_sources;
261                 break;
262         case SR_DI_VDIVS:
263                 *data = vdivs;
264                 break;
265         case SR_DI_COUPLING:
266                 *data = coupling;
267                 break;
268         default:
269                 sr_err("Unknown info_id: %d.", info_id);
270                 return SR_ERR_ARG;
271         }
272
273         return SR_OK;
274 }
275
276 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
277                              const void *value)
278 {
279         struct dev_context *devc = sdi->priv;
280         uint64_t tmp_u64;
281         float tmp_float;
282         struct sr_rational tmp_rat;
283         int ret, i, j;
284         char *channel;
285
286         if (sdi->status != SR_ST_ACTIVE) {
287                 sr_err("Device inactive, can't set config options.");
288                 return SR_ERR;
289         }
290
291         ret = SR_OK;
292         switch (hwcap) {
293     case SR_HWCAP_LIMIT_FRAMES:
294                 devc->limit_frames = *(const uint64_t *)value;
295                 break;
296         case SR_HWCAP_TRIGGER_SLOPE:
297                 tmp_u64 = *(const int *)value;
298                 rigol_ds1xx2_send_data(devc->fd, "TRIG:EDGE:%s\n", tmp_u64 ? "POS" : "NEG");
299                 break;
300         case SR_HWCAP_HORIZ_TRIGGERPOS:
301                 tmp_float = *(const float *)value;
302                 rigol_ds1xx2_send_data(devc->fd, "TIM:OFFS %f\n", tmp_float);
303                 break;
304         case SR_HWCAP_TIMEBASE:
305                 tmp_rat = *(const struct sr_rational *)value;
306                 rigol_ds1xx2_send_data(devc->fd, "TIM:SCAL %f\n", (float) tmp_rat.p / tmp_rat.q);
307                 break;
308         case SR_HWCAP_TRIGGER_SOURCE:
309                 if (!strcmp(value, "CH1"))
310                         channel = "CHAN1";
311                 else if (!strcmp(value, "CH2"))
312                         channel = "CHAN2";
313                 else if (!strcmp(value, "EXT"))
314                         channel = "EXT";
315                 else if (!strcmp(value, "AC Line"))
316                         channel = "ACL";
317                 else
318                         ret = SR_ERR_ARG;
319                         break;
320                 rigol_ds1xx2_send_data(devc->fd, "TRIG:SOUR %s\n", channel);
321                 break;
322         case SR_HWCAP_VDIV:
323                 /* TODO: Not supporting vdiv per channel yet. */
324                 tmp_rat = *(const struct sr_rational *)value;
325                 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
326                         if (vdivs[i].p == tmp_rat.p
327                                         && vdivs[i].q == tmp_rat.q) {
328                                 devc->scale = (float) tmp_rat.p / tmp_rat.q;
329                                 for (j = 0; j < 2; j++)
330                                         rigol_ds1xx2_send_data(devc->fd, "CHAN%d:SCAL %f\n", j, devc->scale);
331                                 break;
332                         }
333                 }
334                 if (vdivs[i].p == 0 && vdivs[i].q == 0)
335                         ret = SR_ERR_ARG;
336                 break;
337         case SR_HWCAP_COUPLING:
338                 /* TODO: Not supporting coupling per channel yet. */
339                 for (i = 0; coupling[i]; i++) {
340                         if (!strcmp(value, coupling[i])) {
341                                 for (j = 0; j < 2; j++)
342                                         rigol_ds1xx2_send_data(devc->fd, "CHAN%d:COUP %s\n", j, coupling[i]);
343                                 break;
344                         }
345                 }
346                 if (coupling[i] == 0)
347                         ret = SR_ERR_ARG;
348                 break;
349         default:
350                 sr_err("Unknown hardware capability: %d.", hwcap);
351                 ret = SR_ERR_ARG;
352         }
353
354         return ret;
355 }
356
357 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
358                                     void *cb_data)
359 {
360         struct dev_context *devc = sdi->priv;
361         struct sr_datafeed_packet packet;
362         struct sr_datafeed_header header;
363         struct sr_datafeed_meta_analog meta;
364         char buf[256];
365         int len;
366         (void)cb_data;
367
368         devc->num_frames = 0;
369
370         sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi);
371
372         /* Send header packet to the session bus. */
373         packet.type = SR_DF_HEADER;
374         packet.payload = (unsigned char *)&header;
375         header.feed_version = 1;
376         gettimeofday(&header.starttime, NULL);
377         sr_session_send(cb_data, &packet);
378
379         /* Send metadata about the SR_DF_ANALOG packets to come. */
380         packet.type = SR_DF_META_ANALOG;
381         packet.payload = &meta;
382         meta.num_probes = NUM_PROBES;
383         sr_session_send(cb_data, &packet);
384
385         rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?\n");
386         len = read(devc->fd, buf, sizeof(buf));
387         buf[len] = 0;
388         devc->scale = atof(buf);
389         sr_dbg("scale is %f", devc->scale);
390         rigol_ds1xx2_send_data(devc->fd, ":CHAN1:OFFS?\n");
391         len = read(devc->fd, buf, sizeof(buf));
392         buf[len] = 0;
393         devc->offset = atof(buf);
394         sr_dbg("offset is %f", devc->offset);
395         rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n");
396
397         return SR_OK;
398 }
399
400 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
401 {
402         struct dev_context *devc = sdi->priv;
403         (void)cb_data;
404
405         if (sdi->status != SR_ST_ACTIVE) {
406                 sr_err("Device inactive, can't stop acquisition.");
407                 return SR_ERR;
408         }
409
410         sr_source_remove(devc->fd);
411
412         return SR_OK;
413 }
414
415 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
416         .name = "rigol-ds1xx2",
417         .longname = "Rigol DS1xx2",
418         .api_version = 1,
419         .init = hw_init,
420         .cleanup = hw_cleanup,
421         .scan = hw_scan,
422         .dev_list = hw_dev_list,
423         .dev_clear = clear_instances,
424         .dev_open = hw_dev_open,
425         .dev_close = hw_dev_close,
426         .info_get = hw_info_get,
427         .dev_config_set = hw_dev_config_set,
428         .dev_acquisition_start = hw_dev_acquisition_start,
429         .dev_acquisition_stop = hw_dev_acquisition_stop,
430         .priv = NULL,
431 };