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[] = {
36 static const int32_t hwcaps[] = {
39 SR_CONF_TRIGGER_SOURCE,
40 SR_CONF_TRIGGER_SLOPE,
41 SR_CONF_HORIZ_TRIGGERPOS,
45 static const int32_t analog_hwcaps[] = {
52 static const uint64_t timebases[][2] = {
91 /* { 1000, 1 }, Confuses other code? */
94 static const uint64_t vdivs[][2] = {
114 #define NUM_TIMEBASE ARRAY_SIZE(timebases)
115 #define NUM_VDIV ARRAY_SIZE(vdivs)
117 static const char *trigger_sources[] = {
140 static const char *coupling[] = {
146 /* Do not change the order of entries */
147 static const char *data_sources[] = {
154 * name, series, protocol flavor, min timebase, max timebase, min vdiv,
155 * digital channels, number of horizontal divs
157 static const struct rigol_ds_model supported_models[] = {
158 {"DS1052E", RIGOL_DS1000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, false, 12},
159 {"DS1102E", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, false, 12},
160 {"DS1152E", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, false, 12},
161 {"DS1052D", RIGOL_DS1000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, true, 12},
162 {"DS1102D", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, true, 12},
163 {"DS1152D", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, true, 12},
164 {"DS2072", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {500, 1}, {500, 1000000}, false, 14},
165 {"DS2102", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {500, 1}, {500, 1000000}, false, 14},
166 {"DS2202", RIGOL_DS2000, PROTOCOL_IEEE488_2, {2, 1000000000}, {500, 1}, {500, 1000000}, false, 14},
169 SR_PRIV struct sr_dev_driver rigol_ds_driver_info;
170 static struct sr_dev_driver *di = &rigol_ds_driver_info;
172 static void clear_helper(void *priv)
174 struct dev_context *devc;
178 g_free(devc->buffer);
179 g_free(devc->coupling[0]);
180 g_free(devc->coupling[1]);
181 g_free(devc->trigger_source);
182 g_free(devc->trigger_slope);
183 g_slist_free(devc->analog_groups[0].probes);
184 g_slist_free(devc->analog_groups[1].probes);
185 g_slist_free(devc->digital_group.probes);
188 static int dev_clear(void)
190 return std_dev_clear(di, clear_helper);
193 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
198 va_start(args, format);
199 ret = sr_scpi_send_variadic(sdi->conn, format, args);
205 /* When setting a bunch of parameters in a row, the DS1052E scrambles
206 * some of them unless there is at least 100ms delay in between. */
207 sr_spew("delay %dms", 100);
213 static int init(struct sr_context *sr_ctx)
215 return std_init(sr_ctx, di, LOG_PREFIX);
218 static int probe_port(const char *port, const char *serialcomm, GSList **devices)
220 struct dev_context *devc;
221 struct sr_dev_inst *sdi;
222 const char *usbtmc_prefix = "/dev/usbtmc";
223 struct sr_scpi_dev_inst *scpi;
224 struct sr_scpi_hw_info *hw_info;
225 struct sr_probe *probe;
227 const struct rigol_ds_model *model = NULL;
233 if (strncmp(port, usbtmc_prefix, strlen(usbtmc_prefix)) == 0) {
234 if (!(scpi = scpi_usbtmc_dev_inst_new(port)))
235 return SR_ERR_MALLOC;
237 if (!(scpi = scpi_serial_dev_inst_new(port, serialcomm)))
238 return SR_ERR_MALLOC;
241 if (sr_scpi_open(scpi) != SR_OK) {
246 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
247 sr_info("Couldn't get IDN response.");
253 if (strcasecmp(hw_info->manufacturer, "Rigol Technologies")) {
254 sr_scpi_hw_info_free(hw_info);
259 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
260 if (!strcmp(hw_info->model, supported_models[i].name)) {
261 model = &supported_models[i];
266 if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
267 hw_info->manufacturer, hw_info->model,
268 hw_info->firmware_version))) {
269 sr_scpi_hw_info_free(hw_info);
274 sr_scpi_hw_info_free(hw_info);
279 sdi->inst_type = SR_INST_SCPI;
281 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
282 return SR_ERR_MALLOC;
284 devc->limit_frames = 0;
287 for (i = 0; i < 2; i++) {
288 channel_name = (i == 0 ? "CH1" : "CH2");
289 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name)))
290 return SR_ERR_MALLOC;
291 sdi->probes = g_slist_append(sdi->probes, probe);
292 devc->analog_groups[i].name = channel_name;
293 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
294 sdi->probe_groups = g_slist_append(sdi->probe_groups,
295 &devc->analog_groups[i]);
298 if (devc->model->has_digital) {
299 for (i = 0; i < 16; i++) {
300 if (!(channel_name = g_strdup_printf("D%d", i)))
301 return SR_ERR_MALLOC;
302 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
303 g_free(channel_name);
305 return SR_ERR_MALLOC;
306 sdi->probes = g_slist_append(sdi->probes, probe);
307 devc->digital_group.probes = g_slist_append(
308 devc->digital_group.probes, probe);
310 devc->digital_group.name = "LA";
311 sdi->probe_groups = g_slist_append(sdi->probe_groups,
312 &devc->digital_group);
315 for (i = 0; i < NUM_TIMEBASE; i++) {
316 if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
317 devc->timebases = &timebases[i];
318 if (!memcmp(&devc->model->max_timebase, &timebases[i], sizeof(uint64_t[2])))
319 devc->num_timebases = &timebases[i] - devc->timebases + 1;
322 for (i = 0; i < NUM_VDIV; i++) {
323 if (!memcmp(&devc->model->min_vdiv, &vdivs[i], sizeof(uint64_t[2]))) {
324 devc->vdivs = &vdivs[i];
325 devc->num_vdivs = NUM_VDIV - (&vdivs[i] - &vdivs[0]);
329 if (!(devc->buffer = g_try_malloc(ACQ_BUFFER_SIZE)))
330 return SR_ERR_MALLOC;
331 if (!(devc->data = g_try_malloc(ACQ_BUFFER_SIZE * sizeof(float))))
332 return SR_ERR_MALLOC;
334 devc->data_source = DATA_SOURCE_LIVE;
338 *devices = g_slist_append(NULL, sdi);
343 static GSList *scan(GSList *options)
345 struct drv_context *drvc;
346 struct sr_config *src;
350 const gchar *dev_name;
352 gchar *serialcomm = NULL;
356 for (l = options; l; l = l->next) {
360 port = (char *)g_variant_get_string(src->data, NULL);
362 case SR_CONF_SERIALCOMM:
363 serialcomm = (char *)g_variant_get_string(src->data, NULL);
370 if (probe_port(port, serialcomm, &devices) == SR_ERR_MALLOC) {
377 if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
378 if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
380 while ((dev_name = g_dir_read_name(dir))) {
381 if (strncmp(dev_name, "usbtmc", 6))
383 port = g_strconcat("/dev/", dev_name, NULL);
384 ret = probe_port(port, serialcomm, &devices);
388 if (ret == SR_ERR_MALLOC) {
396 /* Tack a copy of the newly found devices onto the driver list. */
397 l = g_slist_copy(devices);
398 drvc->instances = g_slist_concat(drvc->instances, l);
403 static GSList *dev_list(void)
405 return ((struct drv_context *)(di->priv))->instances;
408 static int dev_open(struct sr_dev_inst *sdi)
410 struct sr_scpi_dev_inst *scpi = sdi->conn;
412 if (sr_scpi_open(scpi) < 0)
415 if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
418 sdi->status = SR_ST_ACTIVE;
423 static int dev_close(struct sr_dev_inst *sdi)
425 struct sr_scpi_dev_inst *scpi;
430 if (sr_scpi_close(scpi) < 0)
432 sdi->status = SR_ST_INACTIVE;
438 static int cleanup(void)
443 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
444 const struct sr_probe_group *probe_group)
446 struct dev_context *devc;
449 if (!sdi || !(devc = sdi->priv))
452 /* If a probe group is specified, it must be a valid one. */
454 if (probe_group != &devc->analog_groups[0]
455 && probe_group != &devc->analog_groups[1]) {
456 sr_err("Invalid probe group specified.");
462 case SR_CONF_NUM_TIMEBASE:
463 *data = g_variant_new_int32(devc->num_timebases);
465 case SR_CONF_NUM_VDIV:
467 sr_err("No probe group specified.");
468 return SR_ERR_PROBE_GROUP;
470 for (i = 0; i < 2; i++) {
471 if (probe_group == &devc->analog_groups[i]) {
472 *data = g_variant_new_int32(devc->num_vdivs);
477 case SR_CONF_DATA_SOURCE:
478 if (devc->data_source == DATA_SOURCE_LIVE)
479 *data = g_variant_new_string("Live");
480 else if (devc->data_source == DATA_SOURCE_MEMORY)
481 *data = g_variant_new_string("Memory");
483 *data = g_variant_new_string("Segmented");
492 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
493 const struct sr_probe_group *probe_group)
495 struct dev_context *devc;
496 uint64_t tmp_u64, p, q;
502 if (!(devc = sdi->priv))
505 if (sdi->status != SR_ST_ACTIVE)
506 return SR_ERR_DEV_CLOSED;
508 /* If a probe group is specified, it must be a valid one. */
510 if (probe_group != &devc->analog_groups[0]
511 && probe_group != &devc->analog_groups[1]) {
512 sr_err("Invalid probe group specified.");
519 case SR_CONF_LIMIT_FRAMES:
520 devc->limit_frames = g_variant_get_uint64(data);
522 case SR_CONF_TRIGGER_SLOPE:
523 tmp_u64 = g_variant_get_uint64(data);
524 if (tmp_u64 != 0 && tmp_u64 != 1)
526 g_free(devc->trigger_slope);
527 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
528 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
530 case SR_CONF_HORIZ_TRIGGERPOS:
531 t_dbl = g_variant_get_double(data);
532 if (t_dbl < 0.0 || t_dbl > 1.0)
534 devc->horiz_triggerpos = t_dbl;
535 /* We have the trigger offset as a percentage of the frame, but
536 * need to express this in seconds. */
537 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
538 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
540 case SR_CONF_TIMEBASE:
541 g_variant_get(data, "(tt)", &p, &q);
542 for (i = 0; i < devc->num_timebases; i++) {
543 if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
544 devc->timebase = (float)p / q;
545 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
549 if (i == devc->num_timebases)
552 case SR_CONF_TRIGGER_SOURCE:
553 tmp_str = g_variant_get_string(data, NULL);
554 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
555 if (!strcmp(trigger_sources[i], tmp_str)) {
556 g_free(devc->trigger_source);
557 devc->trigger_source = g_strdup(trigger_sources[i]);
558 if (!strcmp(devc->trigger_source, "AC Line"))
560 else if (!strcmp(devc->trigger_source, "CH1"))
562 else if (!strcmp(devc->trigger_source, "CH2"))
565 tmp_str = (char *)devc->trigger_source;
566 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
570 if (i == ARRAY_SIZE(trigger_sources))
575 sr_err("No probe group specified.");
576 return SR_ERR_PROBE_GROUP;
578 g_variant_get(data, "(tt)", &p, &q);
579 for (i = 0; i < 2; i++) {
580 if (probe_group == &devc->analog_groups[i]) {
581 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
582 if (vdivs[j][0] != p || vdivs[j][1] != q)
584 devc->vdiv[i] = (float)p / q;
585 return set_cfg(sdi, ":CHAN%d:SCAL %.3f", i + 1,
592 case SR_CONF_COUPLING:
594 sr_err("No probe group specified.");
595 return SR_ERR_PROBE_GROUP;
597 tmp_str = g_variant_get_string(data, NULL);
598 for (i = 0; i < 2; i++) {
599 if (probe_group == &devc->analog_groups[i]) {
600 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
601 if (!strcmp(tmp_str, coupling[j])) {
602 g_free(devc->coupling[i]);
603 devc->coupling[i] = g_strdup(coupling[j]);
604 return set_cfg(sdi, ":CHAN%d:COUP %s", i + 1,
612 case SR_CONF_DATA_SOURCE:
613 tmp_str = g_variant_get_string(data, NULL);
614 if (!strcmp(tmp_str, "Live"))
615 devc->data_source = DATA_SOURCE_LIVE;
616 else if (!strcmp(tmp_str, "Memory"))
617 devc->data_source = DATA_SOURCE_MEMORY;
618 else if (devc->model->protocol == PROTOCOL_IEEE488_2
619 && !strcmp(tmp_str, "Segmented"))
620 devc->data_source = DATA_SOURCE_SEGMENTED;
632 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
633 const struct sr_probe_group *probe_group)
635 GVariant *tuple, *rational[2];
638 struct dev_context *devc = NULL;
643 if (key == SR_CONF_SCAN_OPTIONS) {
644 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
645 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
647 } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) {
648 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
649 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
653 /* Every other option requires a valid device instance. */
654 if (!sdi || !(devc = sdi->priv))
657 /* If a probe group is specified, it must be a valid one. */
659 if (probe_group != &devc->analog_groups[0]
660 && probe_group != &devc->analog_groups[1]) {
661 sr_err("Invalid probe group specified.");
668 case SR_CONF_DEVICE_OPTIONS:
670 sr_err("No probe group specified.");
671 return SR_ERR_PROBE_GROUP;
673 if (probe_group == &devc->digital_group) {
674 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
675 NULL, 0, sizeof(int32_t));
678 for (i = 0; i < 2; i++) {
679 if (probe_group == &devc->analog_groups[i]) {
680 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
681 analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t));
688 case SR_CONF_COUPLING:
690 sr_err("No probe group specified.");
691 return SR_ERR_PROBE_GROUP;
693 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
697 /* Can't know this until we have the exact model. */
700 sr_err("No probe group specified.");
701 return SR_ERR_PROBE_GROUP;
703 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
704 for (i = 0; i < devc->num_vdivs; i++) {
705 rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
706 rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
707 tuple = g_variant_new_tuple(rational, 2);
708 g_variant_builder_add_value(&gvb, tuple);
710 *data = g_variant_builder_end(&gvb);
712 case SR_CONF_TIMEBASE:
714 /* Can't know this until we have the exact model. */
716 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
717 for (i = 0; i < devc->num_timebases; i++) {
718 rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
719 rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
720 tuple = g_variant_new_tuple(rational, 2);
721 g_variant_builder_add_value(&gvb, tuple);
723 *data = g_variant_builder_end(&gvb);
725 case SR_CONF_TRIGGER_SOURCE:
727 /* Can't know this until we have the exact model. */
729 *data = g_variant_new_strv(trigger_sources,
730 devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
732 case SR_CONF_DATA_SOURCE:
734 /* Can't know this until we have the exact model. */
736 /* This needs tweaking by series/model! */
737 if (devc->model->series == RIGOL_DS2000)
738 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
740 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
749 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
751 struct sr_scpi_dev_inst *scpi;
752 struct dev_context *devc;
753 struct sr_probe *probe;
757 if (sdi->status != SR_ST_ACTIVE)
758 return SR_ERR_DEV_CLOSED;
763 if (devc->data_source == DATA_SOURCE_LIVE) {
764 if (sr_scpi_send(sdi->conn, ":RUN") != SR_OK)
766 } else if (devc->data_source == DATA_SOURCE_MEMORY) {
767 if (devc->model->series != RIGOL_DS2000) {
768 sr_err("Data source 'Memory' not supported for this device");
771 } else if (devc->data_source == DATA_SOURCE_SEGMENTED) {
772 sr_err("Data source 'Segmented' not yet supported");
776 for (l = sdi->probes; l; l = l->next) {
778 sr_dbg("handling probe %s", probe->name);
779 if (probe->type == SR_PROBE_ANALOG) {
781 devc->enabled_analog_probes = g_slist_append(
782 devc->enabled_analog_probes, probe);
783 if (probe->enabled != devc->analog_channels[probe->index]) {
784 /* Enabled channel is currently disabled, or vice versa. */
785 sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
786 probe->enabled ? "ON" : "OFF");
787 if (sr_scpi_send(sdi->conn, cmd) != SR_OK)
790 } else if (probe->type == SR_PROBE_LOGIC) {
792 devc->enabled_digital_probes = g_slist_append(
793 devc->enabled_digital_probes, probe);
794 if (probe->enabled != devc->digital_channels[probe->index]) {
795 /* Enabled channel is currently disabled, or vice versa. */
796 sprintf(cmd, ":DIG%d:TURN %s", probe->index,
797 probe->enabled ? "ON" : "OFF");
798 if (sr_scpi_send(sdi->conn, cmd) != SR_OK)
803 if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
806 sr_scpi_source_add(scpi, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
808 /* Send header packet to the session bus. */
809 std_session_send_df_header(cb_data, LOG_PREFIX);
811 if (devc->model->protocol == PROTOCOL_LEGACY) {
812 /* Fetch the first frame. */
813 if (devc->enabled_analog_probes) {
814 devc->analog_frame_size = DS1000_ANALOG_LIVE_WAVEFORM_SIZE;
815 devc->channel_frame = devc->enabled_analog_probes->data;
816 if (sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%d",
817 devc->channel_frame->index + 1) != SR_OK)
820 devc->channel_frame = devc->enabled_digital_probes->data;
821 if (sr_scpi_send(sdi->conn, ":WAV:DATA? DIG") != SR_OK)
825 devc->num_frame_bytes = 0;
827 if (devc->enabled_analog_probes) {
828 if (devc->data_source == DATA_SOURCE_MEMORY)
830 if (g_slist_length(devc->enabled_analog_probes) == 1)
831 devc->analog_frame_size = DS2000_ANALOG_MEM_WAVEFORM_SIZE_1C;
833 devc->analog_frame_size = DS2000_ANALOG_MEM_WAVEFORM_SIZE_2C;
834 /* Apparently for the DS2000 the memory
835 * depth can only be set in Running state -
836 * this matches the behaviour of the UI. */
837 if (sr_scpi_send(sdi->conn, ":RUN") != SR_OK)
839 if (sr_scpi_send(sdi->conn, "ACQ:MDEP %d", devc->analog_frame_size) != SR_OK)
841 if (sr_scpi_send(sdi->conn, ":STOP") != SR_OK)
844 devc->analog_frame_size = DS2000_ANALOG_LIVE_WAVEFORM_SIZE;
845 devc->channel_frame = devc->enabled_analog_probes->data;
846 if (rigol_ds_capture_start(sdi) != SR_OK)
854 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
856 struct dev_context *devc;
857 struct sr_scpi_dev_inst *scpi;
863 if (sdi->status != SR_ST_ACTIVE) {
864 sr_err("Device inactive, can't stop acquisition.");
868 g_slist_free(devc->enabled_analog_probes);
869 g_slist_free(devc->enabled_digital_probes);
870 devc->enabled_analog_probes = NULL;
871 devc->enabled_digital_probes = NULL;
873 sr_scpi_source_remove(scpi);
878 SR_PRIV struct sr_dev_driver rigol_ds_driver_info = {
880 .longname = "Rigol DS",
885 .dev_list = dev_list,
886 .dev_clear = dev_clear,
887 .config_get = config_get,
888 .config_set = config_set,
889 .config_list = config_list,
890 .dev_open = dev_open,
891 .dev_close = dev_close,
892 .dev_acquisition_start = dev_acquisition_start,
893 .dev_acquisition_stop = dev_acquisition_stop,