2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Martin Ling <martin-git@earth.li>
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.
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.
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/>.
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
31 static const int hwcaps[] = {
32 SR_HWCAP_OSCILLOSCOPE,
33 SR_HWCAP_LIMIT_SAMPLES,
35 SR_HWCAP_TRIGGER_SOURCE,
36 SR_HWCAP_TRIGGER_SLOPE,
37 SR_HWCAP_HORIZ_TRIGGERPOS,
43 static const char *probe_names[] = {
48 static const struct sr_rational timebases[] = {
87 static const struct sr_rational vdivs[] = {
105 static const char *trigger_sources[] = {
113 static const char *coupling[] = {
120 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info;
121 static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info;
123 /* Properly close and free all devices. */
124 static int clear_instances(void)
126 struct sr_dev_inst *sdi;
127 struct drv_context *drvc;
128 struct dev_context *devc;
131 if (!(drvc = di->priv))
134 for (l = drvc->instances; l; l = l->next) {
135 if (!(sdi = l->data))
137 if (!(devc = sdi->priv))
142 sr_dev_inst_free(sdi);
145 g_slist_free(drvc->instances);
146 drvc->instances = NULL;
151 static int hw_init(struct sr_context *sr_ctx)
153 struct drv_context *drvc;
156 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
157 sr_err("Driver context malloc failed.");
158 return SR_ERR_MALLOC;
166 static GSList *hw_scan(GSList *options)
168 struct drv_context *drvc;
169 struct sr_dev_inst *sdi;
170 struct dev_context *devc;
171 struct sr_probe *probe;
179 drvc->instances = NULL;
181 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, "Rigol", "DS1xx2", NULL)))
183 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
189 for (i = 0; i < 2; i++)
191 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, i == 0 ? "CH1" : "CH2")))
193 sdi->probes = g_slist_append(sdi->probes, probe);
195 drvc->instances = g_slist_append(drvc->instances, sdi);
196 devices = g_slist_append(devices, sdi);
201 static GSList *hw_dev_list(void)
203 struct drv_context *drvc;
207 return drvc->instances;
210 static int hw_dev_open(struct sr_dev_inst *sdi)
212 int fd = open("/dev/usbtmc1", O_RDWR);
217 struct dev_context *devc = sdi->priv;
225 static int hw_dev_close(struct sr_dev_inst *sdi)
227 struct dev_context *devc = sdi->priv;
234 static int hw_cleanup(void)
241 static int hw_info_get(int info_id, const void **data,
242 const struct sr_dev_inst *sdi)
250 case SR_DI_NUM_PROBES:
251 *data = GINT_TO_POINTER(NUM_PROBES);
253 case SR_DI_PROBE_NAMES:
256 case SR_DI_TIMEBASES:
259 case SR_DI_TRIGGER_SOURCES:
260 *data = trigger_sources;
269 sr_err("Unknown info_id: %d.", info_id);
276 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
279 struct dev_context *devc = sdi->priv;
282 struct sr_rational tmp_rat;
286 if (sdi->status != SR_ST_ACTIVE) {
287 sr_err("Device inactive, can't set config options.");
293 case SR_HWCAP_LIMIT_FRAMES:
294 devc->limit_frames = *(const uint64_t *)value;
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");
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);
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);
308 case SR_HWCAP_TRIGGER_SOURCE:
309 if (!strcmp(value, "CH1"))
311 else if (!strcmp(value, "CH2"))
313 else if (!strcmp(value, "EXT"))
315 else if (!strcmp(value, "AC Line"))
322 rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s\n", channel);
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);
336 if (vdivs[i].p == 0 && vdivs[i].q == 0)
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]);
348 if (coupling[i] == 0)
352 sr_err("Unknown hardware capability: %d.", hwcap);
359 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
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;
370 devc->num_frames = 0;
372 sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi);
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);
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);
387 rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?\n");
388 len = read(devc->fd, buf, sizeof(buf));
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));
395 devc->offset = atof(buf);
396 sr_dbg("offset is %.6f", devc->offset);
397 rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n");
402 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
404 struct dev_context *devc = sdi->priv;
407 if (sdi->status != SR_ST_ACTIVE) {
408 sr_err("Device inactive, can't stop acquisition.");
412 sr_source_remove(devc->fd);
417 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
418 .name = "rigol-ds1xx2",
419 .longname = "Rigol DS1xx2",
422 .cleanup = hw_cleanup,
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,