]> sigrok.org Git - libsigrok.git/blob - hardware/rigol-ds1xx2/api.c
rigol-ds1xx2: Fix setting trigger parameters.
[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:SLOP %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 %.9f\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 %.9f\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                 {
319                         ret = SR_ERR_ARG;
320                         break;
321                 }
322                 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s\n", channel);
323                 break;
324         case SR_HWCAP_VDIV:
325                 /* TODO: Not supporting vdiv per channel yet. */
326                 tmp_rat = *(const struct sr_rational *)value;
327                 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
328                         if (vdivs[i].p == tmp_rat.p
329                                         && vdivs[i].q == tmp_rat.q) {
330                                 devc->scale = (float) tmp_rat.p / tmp_rat.q;
331                                 for (j = 0; j < 2; j++)
332                                         rigol_ds1xx2_send_data(devc->fd, ":CHAN%d:SCAL %.3f\n", j, devc->scale);
333                                 break;
334                         }
335                 }
336                 if (vdivs[i].p == 0 && vdivs[i].q == 0)
337                         ret = SR_ERR_ARG;
338                 break;
339         case SR_HWCAP_COUPLING:
340                 /* TODO: Not supporting coupling per channel yet. */
341                 for (i = 0; coupling[i]; i++) {
342                         if (!strcmp(value, coupling[i])) {
343                                 for (j = 0; j < 2; j++)
344                                         rigol_ds1xx2_send_data(devc->fd, ":CHAN%d:COUP %s\n", j, coupling[i]);
345                                 break;
346                         }
347                 }
348                 if (coupling[i] == 0)
349                         ret = SR_ERR_ARG;
350                 break;
351         default:
352                 sr_err("Unknown hardware capability: %d.", hwcap);
353                 ret = SR_ERR_ARG;
354         }
355
356         return ret;
357 }
358
359 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
360                                     void *cb_data)
361 {
362         struct dev_context *devc = sdi->priv;
363         struct sr_datafeed_packet packet;
364         struct sr_datafeed_header header;
365         struct sr_datafeed_meta_analog meta;
366         char buf[256];
367         int len;
368         (void)cb_data;
369
370         devc->num_frames = 0;
371
372         sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi);
373
374         /* Send header packet to the session bus. */
375         packet.type = SR_DF_HEADER;
376         packet.payload = (unsigned char *)&header;
377         header.feed_version = 1;
378         gettimeofday(&header.starttime, NULL);
379         sr_session_send(cb_data, &packet);
380
381         /* Send metadata about the SR_DF_ANALOG packets to come. */
382         packet.type = SR_DF_META_ANALOG;
383         packet.payload = &meta;
384         meta.num_probes = NUM_PROBES;
385         sr_session_send(cb_data, &packet);
386
387         rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?\n");
388         len = read(devc->fd, buf, sizeof(buf));
389         buf[len] = 0;
390         devc->scale = atof(buf);
391         sr_dbg("scale is %.3f", devc->scale);
392         rigol_ds1xx2_send_data(devc->fd, ":CHAN1:OFFS?\n");
393         len = read(devc->fd, buf, sizeof(buf));
394         buf[len] = 0;
395         devc->offset = atof(buf);
396         sr_dbg("offset is %.6f", devc->offset);
397         rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n");
398
399         return SR_OK;
400 }
401
402 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
403 {
404         struct dev_context *devc = sdi->priv;
405         (void)cb_data;
406
407         if (sdi->status != SR_ST_ACTIVE) {
408                 sr_err("Device inactive, can't stop acquisition.");
409                 return SR_ERR;
410         }
411
412         sr_source_remove(devc->fd);
413
414         return SR_OK;
415 }
416
417 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
418         .name = "rigol-ds1xx2",
419         .longname = "Rigol DS1xx2",
420         .api_version = 1,
421         .init = hw_init,
422         .cleanup = hw_cleanup,
423         .scan = hw_scan,
424         .dev_list = hw_dev_list,
425         .dev_clear = clear_instances,
426         .dev_open = hw_dev_open,
427         .dev_close = hw_dev_close,
428         .info_get = hw_info_get,
429         .dev_config_set = hw_dev_config_set,
430         .dev_acquisition_start = hw_dev_acquisition_start,
431         .dev_acquisition_stop = hw_dev_acquisition_stop,
432         .priv = NULL,
433 };