2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Martin Ling <martin-git@earth.li>
5 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
6 * Copyright (C) 2013 Mathias Grimmberger <mgri@zaphod.sax.de>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "libsigrok.h"
28 #include "libsigrok-internal.h"
31 static const int32_t hwopts[] = {
35 static const int32_t hwcaps[] = {
38 SR_CONF_TRIGGER_SOURCE,
39 SR_CONF_TRIGGER_SLOPE,
40 SR_CONF_HORIZ_TRIGGERPOS,
44 static const int32_t analog_hwcaps[] = {
51 static const uint64_t timebases[][2] = {
90 /* { 1000, 1 }, Confuses other code? */
93 static const uint64_t vdivs[][2] = {
113 #define NUM_TIMEBASE ARRAY_SIZE(timebases)
114 #define NUM_VDIV ARRAY_SIZE(vdivs)
116 static const char *trigger_sources[] = {
139 static const char *coupling[] = {
145 /* Do not change the order of entries */
146 static const char *data_sources[] = {
153 * name, series, protocol flavor, min timebase, max timebase, min vdiv,
154 * digital channels, number of horizontal divs
156 static const struct rigol_ds_model supported_models[] = {
157 {"DS1052E", RIGOL_DS1000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, false, 12},
158 {"DS1102E", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, false, 12},
159 {"DS1152E", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, false, 12},
160 {"DS1052D", RIGOL_DS1000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, true, 12},
161 {"DS1102D", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, true, 12},
162 {"DS1152D", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, true, 12},
163 {"DS2072", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {500, 1}, {500, 1000000}, false, 14},
164 {"DS2102", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {500, 1}, {500, 1000000}, false, 14},
165 {"DS2202", RIGOL_DS2000, PROTOCOL_IEEE488_2, {2, 1000000000}, {500, 1}, {500, 1000000}, false, 14},
168 SR_PRIV struct sr_dev_driver rigol_ds_driver_info;
169 static struct sr_dev_driver *di = &rigol_ds_driver_info;
171 static void clear_helper(void *priv)
173 struct dev_context *devc;
177 g_free(devc->buffer);
178 g_free(devc->coupling[0]);
179 g_free(devc->coupling[1]);
180 g_free(devc->trigger_source);
181 g_free(devc->trigger_slope);
182 g_slist_free(devc->analog_groups[0].probes);
183 g_slist_free(devc->analog_groups[1].probes);
184 g_slist_free(devc->digital_group.probes);
187 static int dev_clear(void)
189 return std_dev_clear(di, clear_helper);
192 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
197 va_start(args, format);
198 vsnprintf(buf, 255, format, args);
200 if (rigol_ds_send(sdi, buf) != SR_OK)
203 /* When setting a bunch of parameters in a row, the DS1052E scrambles
204 * some of them unless there is at least 100ms delay in between. */
205 sr_spew("delay %dms", 100);
211 static int init(struct sr_context *sr_ctx)
213 return std_init(sr_ctx, di, LOG_PREFIX);
216 static int probe_port(const char *port, GSList **devices)
218 struct dev_context *devc;
219 struct sr_dev_inst *sdi;
220 struct sr_usbtmc_dev_inst *usbtmc;
221 struct sr_probe *probe;
224 const char *manufacturer, *model_name, *version;
225 const struct rigol_ds_model *model = NULL;
227 gchar **tokens, *channel_name;
230 if (!(usbtmc = sr_usbtmc_dev_inst_new(port)))
231 return SR_ERR_MALLOC;
233 if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
235 len = write(usbtmc->fd, "*IDN?", 5);
236 len = read(usbtmc->fd, buf, sizeof(buf));
237 if (close(usbtmc->fd) < 0)
240 sr_usbtmc_dev_inst_free(usbtmc);
246 tokens = g_strsplit(buf, ",", 0);
247 sr_dbg("response: %s [%s]", port, buf);
249 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
251 if (num_tokens < 4) {
256 manufacturer = tokens[0];
257 model_name = tokens[1];
260 if (strcasecmp(manufacturer, "Rigol Technologies")) {
265 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
266 if (!strcmp(model_name, supported_models[i].name)) {
267 model = &supported_models[i];
272 if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
273 manufacturer, model_name, version))) {
280 if (!(sdi->conn = sr_usbtmc_dev_inst_new(port)))
281 return SR_ERR_MALLOC;
283 sdi->inst_type = SR_INST_USBTMC;
285 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
286 return SR_ERR_MALLOC;
287 devc->limit_frames = 0;
290 for (i = 0; i < 2; i++) {
291 channel_name = (i == 0 ? "CH1" : "CH2");
292 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name)))
293 return SR_ERR_MALLOC;
294 sdi->probes = g_slist_append(sdi->probes, probe);
295 devc->analog_groups[i].name = channel_name;
296 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
297 sdi->probe_groups = g_slist_append(sdi->probe_groups,
298 &devc->analog_groups[i]);
301 if (devc->model->has_digital) {
302 for (i = 0; i < 16; i++) {
303 if (!(channel_name = g_strdup_printf("D%d", i)))
304 return SR_ERR_MALLOC;
305 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
306 g_free(channel_name);
308 return SR_ERR_MALLOC;
309 sdi->probes = g_slist_append(sdi->probes, probe);
310 devc->digital_group.probes = g_slist_append(
311 devc->digital_group.probes, probe);
312 devc->digital_group.name = "LA";
313 sdi->probe_groups = g_slist_append(sdi->probe_groups,
314 &devc->digital_group);
318 for (i = 0; i < NUM_TIMEBASE; i++) {
319 if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
320 devc->timebases = &timebases[i];
321 if (!memcmp(&devc->model->max_timebase, &timebases[i], sizeof(uint64_t[2])))
322 devc->num_timebases = &timebases[i] - devc->timebases + 1;
325 for (i = 0; i < NUM_VDIV; i++) {
326 if (!memcmp(&devc->model->min_vdiv, &vdivs[i], sizeof(uint64_t[2]))) {
327 devc->vdivs = &vdivs[i];
328 devc->num_vdivs = NUM_VDIV - (&vdivs[i] - &vdivs[0]);
332 if (!(devc->buffer = g_try_malloc(ACQ_BUFFER_SIZE)))
333 return SR_ERR_MALLOC;
334 if (!(devc->data = g_try_malloc(ACQ_BUFFER_SIZE * sizeof(float))))
335 return SR_ERR_MALLOC;
337 devc->data_source = DATA_SOURCE_LIVE;
341 *devices = g_slist_append(NULL, sdi);
346 static GSList *scan(GSList *options)
348 struct drv_context *drvc;
349 struct sr_config *src;
353 const gchar *dev_name;
358 for (l = options; l; l = l->next) {
360 if (src->key == SR_CONF_CONN) {
361 port = (char *)g_variant_get_string(src->data, NULL);
368 if (probe_port(port, &devices) == SR_ERR_MALLOC)
371 if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
372 if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
374 while ((dev_name = g_dir_read_name(dir))) {
375 if (strncmp(dev_name, "usbtmc", 6))
377 port = g_strconcat("/dev/", dev_name, NULL);
378 ret = probe_port(port, &devices);
380 if (ret == SR_ERR_MALLOC) {
388 /* Tack a copy of the newly found devices onto the driver list. */
389 l = g_slist_copy(devices);
390 drvc->instances = g_slist_concat(drvc->instances, l);
395 static GSList *dev_list(void)
397 return ((struct drv_context *)(di->priv))->instances;
400 static int dev_open(struct sr_dev_inst *sdi)
402 struct sr_usbtmc_dev_inst *usbtmc = sdi->conn;
404 if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
407 if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
410 sdi->status = SR_ST_ACTIVE;
415 static int dev_close(struct sr_dev_inst *sdi)
417 struct sr_usbtmc_dev_inst *usbtmc;
420 if (usbtmc && usbtmc->fd != -1) {
423 sdi->status = SR_ST_INACTIVE;
429 static int cleanup(void)
434 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
435 const struct sr_probe_group *probe_group)
437 struct dev_context *devc;
440 if (!sdi || !(devc = sdi->priv))
443 /* If a probe group is specified, it must be a valid one. */
445 if (probe_group != &devc->analog_groups[0]
446 && probe_group != &devc->analog_groups[1]) {
447 sr_err("Invalid probe group specified.");
453 case SR_CONF_NUM_TIMEBASE:
454 *data = g_variant_new_int32(devc->num_timebases);
456 case SR_CONF_NUM_VDIV:
458 sr_err("No probe group specified.");
459 return SR_ERR_PROBE_GROUP;
461 for (i = 0; i < 2; i++) {
462 if (probe_group == &devc->analog_groups[i]) {
463 *data = g_variant_new_int32(devc->num_vdivs);
468 case SR_CONF_DATA_SOURCE:
469 if (devc->data_source == DATA_SOURCE_LIVE)
470 *data = g_variant_new_string("Live");
471 else if (devc->data_source == DATA_SOURCE_MEMORY)
472 *data = g_variant_new_string("Memory");
474 *data = g_variant_new_string("Segmented");
483 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
484 const struct sr_probe_group *probe_group)
486 struct dev_context *devc;
487 uint64_t tmp_u64, p, q;
493 if (!(devc = sdi->priv))
496 if (sdi->status != SR_ST_ACTIVE)
497 return SR_ERR_DEV_CLOSED;
499 /* If a probe group is specified, it must be a valid one. */
501 if (probe_group != &devc->analog_groups[0]
502 && probe_group != &devc->analog_groups[1]) {
503 sr_err("Invalid probe group specified.");
510 case SR_CONF_LIMIT_FRAMES:
511 devc->limit_frames = g_variant_get_uint64(data);
513 case SR_CONF_TRIGGER_SLOPE:
514 tmp_u64 = g_variant_get_uint64(data);
515 if (tmp_u64 != 0 && tmp_u64 != 1)
517 g_free(devc->trigger_slope);
518 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
519 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
521 case SR_CONF_HORIZ_TRIGGERPOS:
522 t_dbl = g_variant_get_double(data);
523 if (t_dbl < 0.0 || t_dbl > 1.0)
525 devc->horiz_triggerpos = t_dbl;
526 /* We have the trigger offset as a percentage of the frame, but
527 * need to express this in seconds. */
528 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
529 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
531 case SR_CONF_TIMEBASE:
532 g_variant_get(data, "(tt)", &p, &q);
533 for (i = 0; i < devc->num_timebases; i++) {
534 if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
535 devc->timebase = (float)p / q;
536 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
540 if (i == devc->num_timebases)
543 case SR_CONF_TRIGGER_SOURCE:
544 tmp_str = g_variant_get_string(data, NULL);
545 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
546 if (!strcmp(trigger_sources[i], tmp_str)) {
547 g_free(devc->trigger_source);
548 devc->trigger_source = g_strdup(trigger_sources[i]);
549 if (!strcmp(devc->trigger_source, "AC Line"))
551 else if (!strcmp(devc->trigger_source, "CH1"))
553 else if (!strcmp(devc->trigger_source, "CH2"))
556 tmp_str = (char *)devc->trigger_source;
557 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
561 if (i == ARRAY_SIZE(trigger_sources))
566 sr_err("No probe group specified.");
567 return SR_ERR_PROBE_GROUP;
569 g_variant_get(data, "(tt)", &p, &q);
570 for (i = 0; i < 2; i++) {
571 if (probe_group == &devc->analog_groups[i]) {
572 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
573 if (vdivs[j][0] != p || vdivs[j][1] != q)
575 devc->vdiv[i] = (float)p / q;
576 return set_cfg(sdi, ":CHAN%d:SCAL %.3f", i + 1,
583 case SR_CONF_COUPLING:
585 sr_err("No probe group specified.");
586 return SR_ERR_PROBE_GROUP;
588 tmp_str = g_variant_get_string(data, NULL);
589 for (i = 0; i < 2; i++) {
590 if (probe_group == &devc->analog_groups[i]) {
591 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
592 if (!strcmp(tmp_str, coupling[j])) {
593 g_free(devc->coupling[i]);
594 devc->coupling[i] = g_strdup(coupling[j]);
595 return set_cfg(sdi, ":CHAN%d:COUP %s", i + 1,
603 case SR_CONF_DATA_SOURCE:
604 tmp_str = g_variant_get_string(data, NULL);
605 if (!strcmp(tmp_str, "Live"))
606 devc->data_source = DATA_SOURCE_LIVE;
607 else if (!strcmp(tmp_str, "Memory"))
608 devc->data_source = DATA_SOURCE_MEMORY;
609 else if (devc->model->protocol == PROTOCOL_IEEE488_2
610 && !strcmp(tmp_str, "Segmented"))
611 devc->data_source = DATA_SOURCE_SEGMENTED;
623 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
624 const struct sr_probe_group *probe_group)
626 GVariant *tuple, *rational[2];
629 struct dev_context *devc = NULL;
634 if (key == SR_CONF_SCAN_OPTIONS) {
635 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
636 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
638 } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) {
639 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
640 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
644 /* Every other option requires a valid device instance. */
645 if (!sdi || !(devc = sdi->priv))
648 /* If a probe group is specified, it must be a valid one. */
650 if (probe_group != &devc->analog_groups[0]
651 && probe_group != &devc->analog_groups[1]) {
652 sr_err("Invalid probe group specified.");
659 case SR_CONF_DEVICE_OPTIONS:
661 sr_err("No probe group specified.");
662 return SR_ERR_PROBE_GROUP;
664 if (probe_group == &devc->digital_group) {
665 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
666 NULL, 0, sizeof(int32_t));
669 for (i = 0; i < 2; i++) {
670 if (probe_group == &devc->analog_groups[i]) {
671 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
672 analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t));
679 case SR_CONF_COUPLING:
681 sr_err("No probe group specified.");
682 return SR_ERR_PROBE_GROUP;
684 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
688 /* Can't know this until we have the exact model. */
691 sr_err("No probe group specified.");
692 return SR_ERR_PROBE_GROUP;
694 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
695 for (i = 0; i < devc->num_vdivs; i++) {
696 rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
697 rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
698 tuple = g_variant_new_tuple(rational, 2);
699 g_variant_builder_add_value(&gvb, tuple);
701 *data = g_variant_builder_end(&gvb);
703 case SR_CONF_TIMEBASE:
705 /* Can't know this until we have the exact model. */
707 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
708 for (i = 0; i < devc->num_timebases; i++) {
709 rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
710 rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
711 tuple = g_variant_new_tuple(rational, 2);
712 g_variant_builder_add_value(&gvb, tuple);
714 *data = g_variant_builder_end(&gvb);
716 case SR_CONF_TRIGGER_SOURCE:
718 /* Can't know this until we have the exact model. */
720 *data = g_variant_new_strv(trigger_sources,
721 devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
723 case SR_CONF_DATA_SOURCE:
725 /* Can't know this until we have the exact model. */
727 /* This needs tweaking by series/model! */
728 if (devc->model->series == RIGOL_DS2000)
729 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
731 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
740 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
742 struct sr_usbtmc_dev_inst *usbtmc;
743 struct dev_context *devc;
744 struct sr_probe *probe;
748 if (sdi->status != SR_ST_ACTIVE)
749 return SR_ERR_DEV_CLOSED;
754 if (devc->data_source == DATA_SOURCE_LIVE) {
755 if (rigol_ds_send(sdi, ":RUN") != SR_OK)
757 } else if (devc->data_source == DATA_SOURCE_MEMORY) {
758 if (devc->model->series != RIGOL_DS2000) {
759 sr_err("Data source 'Memory' not supported for this device");
762 } else if (devc->data_source == DATA_SOURCE_SEGMENTED) {
763 sr_err("Data source 'Segmented' not yet supported");
767 for (l = sdi->probes; l; l = l->next) {
769 sr_dbg("handling probe %s", probe->name);
770 if (probe->type == SR_PROBE_ANALOG) {
772 devc->enabled_analog_probes = g_slist_append(
773 devc->enabled_analog_probes, probe);
774 if (probe->enabled != devc->analog_channels[probe->index]) {
775 /* Enabled channel is currently disabled, or vice versa. */
776 sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
777 probe->enabled ? "ON" : "OFF");
778 if (rigol_ds_send(sdi, cmd) != SR_OK)
781 } else if (probe->type == SR_PROBE_LOGIC) {
783 devc->enabled_digital_probes = g_slist_append(
784 devc->enabled_digital_probes, probe);
785 if (probe->enabled != devc->digital_channels[probe->index]) {
786 /* Enabled channel is currently disabled, or vice versa. */
787 sprintf(cmd, ":DIG%d:TURN %s", probe->index,
788 probe->enabled ? "ON" : "OFF");
789 if (rigol_ds_send(sdi, cmd) != SR_OK)
794 if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
797 sr_source_add(usbtmc->fd, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
799 /* Send header packet to the session bus. */
800 std_session_send_df_header(cb_data, LOG_PREFIX);
802 if (devc->model->protocol == PROTOCOL_LEGACY) {
803 /* Fetch the first frame. */
804 if (devc->enabled_analog_probes) {
805 devc->analog_frame_size = DS1000_ANALOG_LIVE_WAVEFORM_SIZE;
806 devc->channel_frame = devc->enabled_analog_probes->data;
807 if (rigol_ds_send(sdi, ":WAV:DATA? CHAN%d",
808 devc->channel_frame->index + 1) != SR_OK)
811 devc->channel_frame = devc->enabled_digital_probes->data;
812 if (rigol_ds_send(sdi, ":WAV:DATA? DIG") != SR_OK)
816 devc->num_frame_bytes = 0;
818 if (devc->enabled_analog_probes) {
819 if (devc->data_source == DATA_SOURCE_MEMORY)
821 if (g_slist_length(devc->enabled_analog_probes) == 1)
822 devc->analog_frame_size = DS2000_ANALOG_MEM_WAVEFORM_SIZE_1C;
824 devc->analog_frame_size = DS2000_ANALOG_MEM_WAVEFORM_SIZE_2C;
825 /* Apparently for the DS2000 the memory
826 * depth can only be set in Running state -
827 * this matches the behaviour of the UI. */
828 if (rigol_ds_send(sdi, ":RUN") != SR_OK)
830 if (rigol_ds_send(sdi, "ACQ:MDEP %d", devc->analog_frame_size) != SR_OK)
832 if (rigol_ds_send(sdi, ":STOP") != SR_OK)
835 devc->analog_frame_size = DS2000_ANALOG_LIVE_WAVEFORM_SIZE;
836 devc->channel_frame = devc->enabled_analog_probes->data;
837 if (rigol_ds_capture_start(sdi) != SR_OK)
845 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
847 struct dev_context *devc;
848 struct sr_usbtmc_dev_inst *usbtmc;
854 if (sdi->status != SR_ST_ACTIVE) {
855 sr_err("Device inactive, can't stop acquisition.");
859 g_slist_free(devc->enabled_analog_probes);
860 g_slist_free(devc->enabled_digital_probes);
861 devc->enabled_analog_probes = NULL;
862 devc->enabled_digital_probes = NULL;
864 sr_source_remove(usbtmc->fd);
869 SR_PRIV struct sr_dev_driver rigol_ds_driver_info = {
871 .longname = "Rigol DS",
876 .dev_list = dev_list,
877 .dev_clear = dev_clear,
878 .config_get = config_get,
879 .config_set = config_set,
880 .config_list = config_list,
881 .dev_open = dev_open,
882 .dev_close = dev_close,
883 .dev_acquisition_start = dev_acquisition_start,
884 .dev_acquisition_stop = dev_acquisition_stop,