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 (sr_scpi_send(sdi->conn, 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_scpi_dev_inst *scpi;
221 struct sr_scpi_hw_info *hw_info;
222 struct sr_probe *probe;
224 const struct rigol_ds_model *model = NULL;
228 if (!(scpi = scpi_usbtmc_dev_inst_new(port)))
229 return SR_ERR_MALLOC;
231 if (sr_scpi_open(scpi) != SR_OK) {
236 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
237 sr_info("Couldn't get IDN response.");
243 if (strcasecmp(hw_info->manufacturer, "Rigol Technologies")) {
244 sr_scpi_hw_info_free(hw_info);
249 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
250 if (!strcmp(hw_info->model, supported_models[i].name)) {
251 model = &supported_models[i];
256 if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
257 hw_info->manufacturer, hw_info->model,
258 hw_info->firmware_version))) {
259 sr_scpi_hw_info_free(hw_info);
264 sr_scpi_hw_info_free(hw_info);
269 sdi->inst_type = SR_INST_SCPI;
271 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
272 return SR_ERR_MALLOC;
274 devc->limit_frames = 0;
277 for (i = 0; i < 2; i++) {
278 channel_name = (i == 0 ? "CH1" : "CH2");
279 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name)))
280 return SR_ERR_MALLOC;
281 sdi->probes = g_slist_append(sdi->probes, probe);
282 devc->analog_groups[i].name = channel_name;
283 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
284 sdi->probe_groups = g_slist_append(sdi->probe_groups,
285 &devc->analog_groups[i]);
288 if (devc->model->has_digital) {
289 for (i = 0; i < 16; i++) {
290 if (!(channel_name = g_strdup_printf("D%d", i)))
291 return SR_ERR_MALLOC;
292 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
293 g_free(channel_name);
295 return SR_ERR_MALLOC;
296 sdi->probes = g_slist_append(sdi->probes, probe);
297 devc->digital_group.probes = g_slist_append(
298 devc->digital_group.probes, probe);
300 devc->digital_group.name = "LA";
301 sdi->probe_groups = g_slist_append(sdi->probe_groups,
302 &devc->digital_group);
305 for (i = 0; i < NUM_TIMEBASE; i++) {
306 if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
307 devc->timebases = &timebases[i];
308 if (!memcmp(&devc->model->max_timebase, &timebases[i], sizeof(uint64_t[2])))
309 devc->num_timebases = &timebases[i] - devc->timebases + 1;
312 for (i = 0; i < NUM_VDIV; i++) {
313 if (!memcmp(&devc->model->min_vdiv, &vdivs[i], sizeof(uint64_t[2]))) {
314 devc->vdivs = &vdivs[i];
315 devc->num_vdivs = NUM_VDIV - (&vdivs[i] - &vdivs[0]);
319 if (!(devc->buffer = g_try_malloc(ACQ_BUFFER_SIZE)))
320 return SR_ERR_MALLOC;
321 if (!(devc->data = g_try_malloc(ACQ_BUFFER_SIZE * sizeof(float))))
322 return SR_ERR_MALLOC;
324 devc->data_source = DATA_SOURCE_LIVE;
328 *devices = g_slist_append(NULL, sdi);
333 static GSList *scan(GSList *options)
335 struct drv_context *drvc;
336 struct sr_config *src;
340 const gchar *dev_name;
345 for (l = options; l; l = l->next) {
347 if (src->key == SR_CONF_CONN) {
348 port = (char *)g_variant_get_string(src->data, NULL);
355 if (probe_port(port, &devices) == SR_ERR_MALLOC)
358 if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
359 if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
361 while ((dev_name = g_dir_read_name(dir))) {
362 if (strncmp(dev_name, "usbtmc", 6))
364 port = g_strconcat("/dev/", dev_name, NULL);
365 ret = probe_port(port, &devices);
367 if (ret == SR_ERR_MALLOC) {
375 /* Tack a copy of the newly found devices onto the driver list. */
376 l = g_slist_copy(devices);
377 drvc->instances = g_slist_concat(drvc->instances, l);
382 static GSList *dev_list(void)
384 return ((struct drv_context *)(di->priv))->instances;
387 static int dev_open(struct sr_dev_inst *sdi)
389 struct sr_scpi_dev_inst *scpi = sdi->conn;
391 if (sr_scpi_open(scpi) < 0)
394 if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
397 sdi->status = SR_ST_ACTIVE;
402 static int dev_close(struct sr_dev_inst *sdi)
404 struct sr_scpi_dev_inst *scpi;
409 if (sr_scpi_close(scpi) < 0)
411 sdi->status = SR_ST_INACTIVE;
417 static int cleanup(void)
422 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
423 const struct sr_probe_group *probe_group)
425 struct dev_context *devc;
428 if (!sdi || !(devc = sdi->priv))
431 /* If a probe group is specified, it must be a valid one. */
433 if (probe_group != &devc->analog_groups[0]
434 && probe_group != &devc->analog_groups[1]) {
435 sr_err("Invalid probe group specified.");
441 case SR_CONF_NUM_TIMEBASE:
442 *data = g_variant_new_int32(devc->num_timebases);
444 case SR_CONF_NUM_VDIV:
446 sr_err("No probe group specified.");
447 return SR_ERR_PROBE_GROUP;
449 for (i = 0; i < 2; i++) {
450 if (probe_group == &devc->analog_groups[i]) {
451 *data = g_variant_new_int32(devc->num_vdivs);
456 case SR_CONF_DATA_SOURCE:
457 if (devc->data_source == DATA_SOURCE_LIVE)
458 *data = g_variant_new_string("Live");
459 else if (devc->data_source == DATA_SOURCE_MEMORY)
460 *data = g_variant_new_string("Memory");
462 *data = g_variant_new_string("Segmented");
471 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
472 const struct sr_probe_group *probe_group)
474 struct dev_context *devc;
475 uint64_t tmp_u64, p, q;
481 if (!(devc = sdi->priv))
484 if (sdi->status != SR_ST_ACTIVE)
485 return SR_ERR_DEV_CLOSED;
487 /* If a probe group is specified, it must be a valid one. */
489 if (probe_group != &devc->analog_groups[0]
490 && probe_group != &devc->analog_groups[1]) {
491 sr_err("Invalid probe group specified.");
498 case SR_CONF_LIMIT_FRAMES:
499 devc->limit_frames = g_variant_get_uint64(data);
501 case SR_CONF_TRIGGER_SLOPE:
502 tmp_u64 = g_variant_get_uint64(data);
503 if (tmp_u64 != 0 && tmp_u64 != 1)
505 g_free(devc->trigger_slope);
506 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
507 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
509 case SR_CONF_HORIZ_TRIGGERPOS:
510 t_dbl = g_variant_get_double(data);
511 if (t_dbl < 0.0 || t_dbl > 1.0)
513 devc->horiz_triggerpos = t_dbl;
514 /* We have the trigger offset as a percentage of the frame, but
515 * need to express this in seconds. */
516 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
517 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
519 case SR_CONF_TIMEBASE:
520 g_variant_get(data, "(tt)", &p, &q);
521 for (i = 0; i < devc->num_timebases; i++) {
522 if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
523 devc->timebase = (float)p / q;
524 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
528 if (i == devc->num_timebases)
531 case SR_CONF_TRIGGER_SOURCE:
532 tmp_str = g_variant_get_string(data, NULL);
533 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
534 if (!strcmp(trigger_sources[i], tmp_str)) {
535 g_free(devc->trigger_source);
536 devc->trigger_source = g_strdup(trigger_sources[i]);
537 if (!strcmp(devc->trigger_source, "AC Line"))
539 else if (!strcmp(devc->trigger_source, "CH1"))
541 else if (!strcmp(devc->trigger_source, "CH2"))
544 tmp_str = (char *)devc->trigger_source;
545 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
549 if (i == ARRAY_SIZE(trigger_sources))
554 sr_err("No probe group specified.");
555 return SR_ERR_PROBE_GROUP;
557 g_variant_get(data, "(tt)", &p, &q);
558 for (i = 0; i < 2; i++) {
559 if (probe_group == &devc->analog_groups[i]) {
560 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
561 if (vdivs[j][0] != p || vdivs[j][1] != q)
563 devc->vdiv[i] = (float)p / q;
564 return set_cfg(sdi, ":CHAN%d:SCAL %.3f", i + 1,
571 case SR_CONF_COUPLING:
573 sr_err("No probe group specified.");
574 return SR_ERR_PROBE_GROUP;
576 tmp_str = g_variant_get_string(data, NULL);
577 for (i = 0; i < 2; i++) {
578 if (probe_group == &devc->analog_groups[i]) {
579 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
580 if (!strcmp(tmp_str, coupling[j])) {
581 g_free(devc->coupling[i]);
582 devc->coupling[i] = g_strdup(coupling[j]);
583 return set_cfg(sdi, ":CHAN%d:COUP %s", i + 1,
591 case SR_CONF_DATA_SOURCE:
592 tmp_str = g_variant_get_string(data, NULL);
593 if (!strcmp(tmp_str, "Live"))
594 devc->data_source = DATA_SOURCE_LIVE;
595 else if (!strcmp(tmp_str, "Memory"))
596 devc->data_source = DATA_SOURCE_MEMORY;
597 else if (devc->model->protocol == PROTOCOL_IEEE488_2
598 && !strcmp(tmp_str, "Segmented"))
599 devc->data_source = DATA_SOURCE_SEGMENTED;
611 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
612 const struct sr_probe_group *probe_group)
614 GVariant *tuple, *rational[2];
617 struct dev_context *devc = NULL;
622 if (key == SR_CONF_SCAN_OPTIONS) {
623 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
624 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
626 } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) {
627 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
628 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
632 /* Every other option requires a valid device instance. */
633 if (!sdi || !(devc = sdi->priv))
636 /* If a probe group is specified, it must be a valid one. */
638 if (probe_group != &devc->analog_groups[0]
639 && probe_group != &devc->analog_groups[1]) {
640 sr_err("Invalid probe group specified.");
647 case SR_CONF_DEVICE_OPTIONS:
649 sr_err("No probe group specified.");
650 return SR_ERR_PROBE_GROUP;
652 if (probe_group == &devc->digital_group) {
653 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
654 NULL, 0, sizeof(int32_t));
657 for (i = 0; i < 2; i++) {
658 if (probe_group == &devc->analog_groups[i]) {
659 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
660 analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t));
667 case SR_CONF_COUPLING:
669 sr_err("No probe group specified.");
670 return SR_ERR_PROBE_GROUP;
672 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
676 /* Can't know this until we have the exact model. */
679 sr_err("No probe group specified.");
680 return SR_ERR_PROBE_GROUP;
682 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
683 for (i = 0; i < devc->num_vdivs; i++) {
684 rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
685 rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
686 tuple = g_variant_new_tuple(rational, 2);
687 g_variant_builder_add_value(&gvb, tuple);
689 *data = g_variant_builder_end(&gvb);
691 case SR_CONF_TIMEBASE:
693 /* Can't know this until we have the exact model. */
695 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
696 for (i = 0; i < devc->num_timebases; i++) {
697 rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
698 rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
699 tuple = g_variant_new_tuple(rational, 2);
700 g_variant_builder_add_value(&gvb, tuple);
702 *data = g_variant_builder_end(&gvb);
704 case SR_CONF_TRIGGER_SOURCE:
706 /* Can't know this until we have the exact model. */
708 *data = g_variant_new_strv(trigger_sources,
709 devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
711 case SR_CONF_DATA_SOURCE:
713 /* Can't know this until we have the exact model. */
715 /* This needs tweaking by series/model! */
716 if (devc->model->series == RIGOL_DS2000)
717 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
719 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
728 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
730 struct sr_scpi_dev_inst *scpi;
731 struct dev_context *devc;
732 struct sr_probe *probe;
736 if (sdi->status != SR_ST_ACTIVE)
737 return SR_ERR_DEV_CLOSED;
742 if (devc->data_source == DATA_SOURCE_LIVE) {
743 if (sr_scpi_send(sdi->conn, ":RUN") != SR_OK)
745 } else if (devc->data_source == DATA_SOURCE_MEMORY) {
746 if (devc->model->series != RIGOL_DS2000) {
747 sr_err("Data source 'Memory' not supported for this device");
750 } else if (devc->data_source == DATA_SOURCE_SEGMENTED) {
751 sr_err("Data source 'Segmented' not yet supported");
755 for (l = sdi->probes; l; l = l->next) {
757 sr_dbg("handling probe %s", probe->name);
758 if (probe->type == SR_PROBE_ANALOG) {
760 devc->enabled_analog_probes = g_slist_append(
761 devc->enabled_analog_probes, probe);
762 if (probe->enabled != devc->analog_channels[probe->index]) {
763 /* Enabled channel is currently disabled, or vice versa. */
764 sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
765 probe->enabled ? "ON" : "OFF");
766 if (sr_scpi_send(sdi->conn, cmd) != SR_OK)
769 } else if (probe->type == SR_PROBE_LOGIC) {
771 devc->enabled_digital_probes = g_slist_append(
772 devc->enabled_digital_probes, probe);
773 if (probe->enabled != devc->digital_channels[probe->index]) {
774 /* Enabled channel is currently disabled, or vice versa. */
775 sprintf(cmd, ":DIG%d:TURN %s", probe->index,
776 probe->enabled ? "ON" : "OFF");
777 if (sr_scpi_send(sdi->conn, cmd) != SR_OK)
782 if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
785 sr_scpi_source_add(scpi, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
787 /* Send header packet to the session bus. */
788 std_session_send_df_header(cb_data, LOG_PREFIX);
790 if (devc->model->protocol == PROTOCOL_LEGACY) {
791 /* Fetch the first frame. */
792 if (devc->enabled_analog_probes) {
793 devc->analog_frame_size = DS1000_ANALOG_LIVE_WAVEFORM_SIZE;
794 devc->channel_frame = devc->enabled_analog_probes->data;
795 if (sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%d",
796 devc->channel_frame->index + 1) != SR_OK)
799 devc->channel_frame = devc->enabled_digital_probes->data;
800 if (sr_scpi_send(sdi->conn, ":WAV:DATA? DIG") != SR_OK)
804 devc->num_frame_bytes = 0;
806 if (devc->enabled_analog_probes) {
807 if (devc->data_source == DATA_SOURCE_MEMORY)
809 if (g_slist_length(devc->enabled_analog_probes) == 1)
810 devc->analog_frame_size = DS2000_ANALOG_MEM_WAVEFORM_SIZE_1C;
812 devc->analog_frame_size = DS2000_ANALOG_MEM_WAVEFORM_SIZE_2C;
813 /* Apparently for the DS2000 the memory
814 * depth can only be set in Running state -
815 * this matches the behaviour of the UI. */
816 if (sr_scpi_send(sdi->conn, ":RUN") != SR_OK)
818 if (sr_scpi_send(sdi->conn, "ACQ:MDEP %d", devc->analog_frame_size) != SR_OK)
820 if (sr_scpi_send(sdi->conn, ":STOP") != SR_OK)
823 devc->analog_frame_size = DS2000_ANALOG_LIVE_WAVEFORM_SIZE;
824 devc->channel_frame = devc->enabled_analog_probes->data;
825 if (rigol_ds_capture_start(sdi) != SR_OK)
833 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
835 struct dev_context *devc;
836 struct sr_scpi_dev_inst *scpi;
842 if (sdi->status != SR_ST_ACTIVE) {
843 sr_err("Device inactive, can't stop acquisition.");
847 g_slist_free(devc->enabled_analog_probes);
848 g_slist_free(devc->enabled_digital_probes);
849 devc->enabled_analog_probes = NULL;
850 devc->enabled_digital_probes = NULL;
852 sr_scpi_source_remove(scpi);
857 SR_PRIV struct sr_dev_driver rigol_ds_driver_info = {
859 .longname = "Rigol DS",
864 .dev_list = dev_list,
865 .dev_clear = dev_clear,
866 .config_get = config_get,
867 .config_set = config_set,
868 .config_list = config_list,
869 .dev_open = dev_open,
870 .dev_close = dev_close,
871 .dev_acquisition_start = dev_acquisition_start,
872 .dev_acquisition_stop = dev_acquisition_stop,