]> sigrok.org Git - libsigrok.git/blame - hardware/rigol-ds1xx2/api.c
rigol-ds1xx2: Assorted parameter setting fixes.
[libsigrok.git] / hardware / rigol-ds1xx2 / api.c
CommitLineData
f4816ac6
ML
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
e0b7d23c
ML
20#include <fcntl.h>
21#include <unistd.h>
22#include <stdlib.h>
23#include <string.h>
f4816ac6
ML
24#include <glib.h>
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
27#include "protocol.h"
28
e0b7d23c
ML
29#define NUM_PROBES 2
30
31static 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
43static const char *probe_names[] = {
44 "CH1", "CH2",
45 NULL,
46};
47
48static 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
87static 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
105static const char *trigger_sources[] = {
106 "CH1",
107 "CH2",
108 "EXT",
109 "AC Line",
110 NULL,
111};
112
113static const char *coupling[] = {
114 "AC",
115 "DC",
116 "GND",
117 NULL,
118};
119
f4816ac6
ML
120SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info;
121static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info;
122
123/* Properly close and free all devices. */
124static 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
e0b7d23c 140 close(devc->fd);
f4816ac6
ML
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
e0b7d23c 151static int hw_init(struct sr_context *sr_ctx)
f4816ac6
ML
152{
153 struct drv_context *drvc;
e0b7d23c 154 (void)sr_ctx;
f4816ac6
ML
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
f4816ac6
ML
161 di->priv = drvc;
162
163 return SR_OK;
164}
165
166static GSList *hw_scan(GSList *options)
167{
168 struct drv_context *drvc;
e0b7d23c
ML
169 struct sr_dev_inst *sdi;
170 struct dev_context *devc;
171 struct sr_probe *probe;
f4816ac6 172 GSList *devices;
e0b7d23c 173 int i;
f4816ac6
ML
174
175 (void)options;
176
177 devices = NULL;
178 drvc = di->priv;
179 drvc->instances = NULL;
180
e0b7d23c
ML
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);
f4816ac6
ML
197
198 return devices;
199}
200
201static GSList *hw_dev_list(void)
202{
203 struct drv_context *drvc;
204
205 drvc = di->priv;
206
207 return drvc->instances;
208}
209
210static int hw_dev_open(struct sr_dev_inst *sdi)
211{
e0b7d23c
ML
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;
f4816ac6
ML
221
222 return SR_OK;
223}
224
225static int hw_dev_close(struct sr_dev_inst *sdi)
226{
e0b7d23c
ML
227 struct dev_context *devc = sdi->priv;
228
229 close(devc->fd);
f4816ac6
ML
230
231 return SR_OK;
232}
233
234static int hw_cleanup(void)
235{
236 clear_instances();
237
f4816ac6
ML
238 return SR_OK;
239}
240
241static int hw_info_get(int info_id, const void **data,
242 const struct sr_dev_inst *sdi)
243{
e0b7d23c
ML
244 (void)sdi;
245
f4816ac6 246 switch (info_id) {
e0b7d23c
ML
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;
f4816ac6
ML
268 default:
269 sr_err("Unknown info_id: %d.", info_id);
270 return SR_ERR_ARG;
271 }
272
273 return SR_OK;
274}
275
276static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
277 const void *value)
278{
e0b7d23c
ML
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;
f4816ac6
ML
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) {
e0b7d23c
ML
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;
542843f7 298 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:%s\n", tmp_u64 ? "POS" : "NEG");
e0b7d23c
ML
299 break;
300 case SR_HWCAP_HORIZ_TRIGGERPOS:
301 tmp_float = *(const float *)value;
542843f7 302 rigol_ds1xx2_send_data(devc->fd, ":TIM:OFFS %.9f\n", tmp_float);
e0b7d23c
ML
303 break;
304 case SR_HWCAP_TIMEBASE:
305 tmp_rat = *(const struct sr_rational *)value;
542843f7 306 rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f\n", (float) tmp_rat.p / tmp_rat.q);
e0b7d23c
ML
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;
542843f7 320 rigol_ds1xx2_send_data(devc->fd, ":TRIG:SOUR %s\n", channel);
e0b7d23c
ML
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++)
542843f7 330 rigol_ds1xx2_send_data(devc->fd, ":CHAN%d:SCAL %.3f\n", j, devc->scale);
e0b7d23c
ML
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++)
542843f7 342 rigol_ds1xx2_send_data(devc->fd, ":CHAN%d:COUP %s\n", j, coupling[i]);
e0b7d23c
ML
343 break;
344 }
345 }
346 if (coupling[i] == 0)
347 ret = SR_ERR_ARG;
348 break;
f4816ac6
ML
349 default:
350 sr_err("Unknown hardware capability: %d.", hwcap);
351 ret = SR_ERR_ARG;
352 }
353
354 return ret;
355}
356
357static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
358 void *cb_data)
359{
e0b7d23c
ML
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);
542843f7 389 sr_dbg("scale is %.3f", devc->scale);
e0b7d23c
ML
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);
542843f7 394 sr_dbg("offset is %.6f", devc->offset);
e0b7d23c 395 rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n");
f4816ac6
ML
396
397 return SR_OK;
398}
399
400static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
401{
e0b7d23c 402 struct dev_context *devc = sdi->priv;
f4816ac6
ML
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
e0b7d23c 410 sr_source_remove(devc->fd);
f4816ac6
ML
411
412 return SR_OK;
413}
414
415SR_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};