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 ret = sr_scpi_send_variadic(sdi->conn, format, args);
204 /* When setting a bunch of parameters in a row, the DS1052E scrambles
205 * some of them unless there is at least 100ms delay in between. */
206 sr_spew("delay %dms", 100);
212 static int init(struct sr_context *sr_ctx)
214 return std_init(sr_ctx, di, LOG_PREFIX);
217 static int probe_port(const char *port, GSList **devices)
219 struct dev_context *devc;
220 struct sr_dev_inst *sdi;
221 struct sr_scpi_dev_inst *scpi;
222 struct sr_scpi_hw_info *hw_info;
223 struct sr_probe *probe;
225 const struct rigol_ds_model *model = NULL;
229 if (!(scpi = scpi_usbtmc_dev_inst_new(port)))
230 return SR_ERR_MALLOC;
232 if (sr_scpi_open(scpi) != SR_OK) {
237 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
238 sr_info("Couldn't get IDN response.");
244 if (strcasecmp(hw_info->manufacturer, "Rigol Technologies")) {
245 sr_scpi_hw_info_free(hw_info);
250 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
251 if (!strcmp(hw_info->model, supported_models[i].name)) {
252 model = &supported_models[i];
257 if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
258 hw_info->manufacturer, hw_info->model,
259 hw_info->firmware_version))) {
260 sr_scpi_hw_info_free(hw_info);
265 sr_scpi_hw_info_free(hw_info);
270 sdi->inst_type = SR_INST_SCPI;
272 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
273 return SR_ERR_MALLOC;
275 devc->limit_frames = 0;
278 for (i = 0; i < 2; i++) {
279 channel_name = (i == 0 ? "CH1" : "CH2");
280 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name)))
281 return SR_ERR_MALLOC;
282 sdi->probes = g_slist_append(sdi->probes, probe);
283 devc->analog_groups[i].name = channel_name;
284 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
285 sdi->probe_groups = g_slist_append(sdi->probe_groups,
286 &devc->analog_groups[i]);
289 if (devc->model->has_digital) {
290 for (i = 0; i < 16; i++) {
291 if (!(channel_name = g_strdup_printf("D%d", i)))
292 return SR_ERR_MALLOC;
293 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
294 g_free(channel_name);
296 return SR_ERR_MALLOC;
297 sdi->probes = g_slist_append(sdi->probes, probe);
298 devc->digital_group.probes = g_slist_append(
299 devc->digital_group.probes, probe);
301 devc->digital_group.name = "LA";
302 sdi->probe_groups = g_slist_append(sdi->probe_groups,
303 &devc->digital_group);
306 for (i = 0; i < NUM_TIMEBASE; i++) {
307 if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
308 devc->timebases = &timebases[i];
309 if (!memcmp(&devc->model->max_timebase, &timebases[i], sizeof(uint64_t[2])))
310 devc->num_timebases = &timebases[i] - devc->timebases + 1;
313 for (i = 0; i < NUM_VDIV; i++) {
314 if (!memcmp(&devc->model->min_vdiv, &vdivs[i], sizeof(uint64_t[2]))) {
315 devc->vdivs = &vdivs[i];
316 devc->num_vdivs = NUM_VDIV - (&vdivs[i] - &vdivs[0]);
320 if (!(devc->buffer = g_try_malloc(ACQ_BUFFER_SIZE)))
321 return SR_ERR_MALLOC;
322 if (!(devc->data = g_try_malloc(ACQ_BUFFER_SIZE * sizeof(float))))
323 return SR_ERR_MALLOC;
325 devc->data_source = DATA_SOURCE_LIVE;
329 *devices = g_slist_append(NULL, sdi);
334 static GSList *scan(GSList *options)
336 struct drv_context *drvc;
337 struct sr_config *src;
341 const gchar *dev_name;
346 for (l = options; l; l = l->next) {
348 if (src->key == SR_CONF_CONN) {
349 port = (char *)g_variant_get_string(src->data, NULL);
356 if (probe_port(port, &devices) == SR_ERR_MALLOC)
359 if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
360 if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
362 while ((dev_name = g_dir_read_name(dir))) {
363 if (strncmp(dev_name, "usbtmc", 6))
365 port = g_strconcat("/dev/", dev_name, NULL);
366 ret = probe_port(port, &devices);
368 if (ret == SR_ERR_MALLOC) {
376 /* Tack a copy of the newly found devices onto the driver list. */
377 l = g_slist_copy(devices);
378 drvc->instances = g_slist_concat(drvc->instances, l);
383 static GSList *dev_list(void)
385 return ((struct drv_context *)(di->priv))->instances;
388 static int dev_open(struct sr_dev_inst *sdi)
390 struct sr_scpi_dev_inst *scpi = sdi->conn;
392 if (sr_scpi_open(scpi) < 0)
395 if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
398 sdi->status = SR_ST_ACTIVE;
403 static int dev_close(struct sr_dev_inst *sdi)
405 struct sr_scpi_dev_inst *scpi;
410 if (sr_scpi_close(scpi) < 0)
412 sdi->status = SR_ST_INACTIVE;
418 static int cleanup(void)
423 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
424 const struct sr_probe_group *probe_group)
426 struct dev_context *devc;
429 if (!sdi || !(devc = sdi->priv))
432 /* If a probe group is specified, it must be a valid one. */
434 if (probe_group != &devc->analog_groups[0]
435 && probe_group != &devc->analog_groups[1]) {
436 sr_err("Invalid probe group specified.");
442 case SR_CONF_NUM_TIMEBASE:
443 *data = g_variant_new_int32(devc->num_timebases);
445 case SR_CONF_NUM_VDIV:
447 sr_err("No probe group specified.");
448 return SR_ERR_PROBE_GROUP;
450 for (i = 0; i < 2; i++) {
451 if (probe_group == &devc->analog_groups[i]) {
452 *data = g_variant_new_int32(devc->num_vdivs);
457 case SR_CONF_DATA_SOURCE:
458 if (devc->data_source == DATA_SOURCE_LIVE)
459 *data = g_variant_new_string("Live");
460 else if (devc->data_source == DATA_SOURCE_MEMORY)
461 *data = g_variant_new_string("Memory");
463 *data = g_variant_new_string("Segmented");
472 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
473 const struct sr_probe_group *probe_group)
475 struct dev_context *devc;
476 uint64_t tmp_u64, p, q;
482 if (!(devc = sdi->priv))
485 if (sdi->status != SR_ST_ACTIVE)
486 return SR_ERR_DEV_CLOSED;
488 /* If a probe group is specified, it must be a valid one. */
490 if (probe_group != &devc->analog_groups[0]
491 && probe_group != &devc->analog_groups[1]) {
492 sr_err("Invalid probe group specified.");
499 case SR_CONF_LIMIT_FRAMES:
500 devc->limit_frames = g_variant_get_uint64(data);
502 case SR_CONF_TRIGGER_SLOPE:
503 tmp_u64 = g_variant_get_uint64(data);
504 if (tmp_u64 != 0 && tmp_u64 != 1)
506 g_free(devc->trigger_slope);
507 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
508 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
510 case SR_CONF_HORIZ_TRIGGERPOS:
511 t_dbl = g_variant_get_double(data);
512 if (t_dbl < 0.0 || t_dbl > 1.0)
514 devc->horiz_triggerpos = t_dbl;
515 /* We have the trigger offset as a percentage of the frame, but
516 * need to express this in seconds. */
517 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
518 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
520 case SR_CONF_TIMEBASE:
521 g_variant_get(data, "(tt)", &p, &q);
522 for (i = 0; i < devc->num_timebases; i++) {
523 if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
524 devc->timebase = (float)p / q;
525 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
529 if (i == devc->num_timebases)
532 case SR_CONF_TRIGGER_SOURCE:
533 tmp_str = g_variant_get_string(data, NULL);
534 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
535 if (!strcmp(trigger_sources[i], tmp_str)) {
536 g_free(devc->trigger_source);
537 devc->trigger_source = g_strdup(trigger_sources[i]);
538 if (!strcmp(devc->trigger_source, "AC Line"))
540 else if (!strcmp(devc->trigger_source, "CH1"))
542 else if (!strcmp(devc->trigger_source, "CH2"))
545 tmp_str = (char *)devc->trigger_source;
546 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
550 if (i == ARRAY_SIZE(trigger_sources))
555 sr_err("No probe group specified.");
556 return SR_ERR_PROBE_GROUP;
558 g_variant_get(data, "(tt)", &p, &q);
559 for (i = 0; i < 2; i++) {
560 if (probe_group == &devc->analog_groups[i]) {
561 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
562 if (vdivs[j][0] != p || vdivs[j][1] != q)
564 devc->vdiv[i] = (float)p / q;
565 return set_cfg(sdi, ":CHAN%d:SCAL %.3f", i + 1,
572 case SR_CONF_COUPLING:
574 sr_err("No probe group specified.");
575 return SR_ERR_PROBE_GROUP;
577 tmp_str = g_variant_get_string(data, NULL);
578 for (i = 0; i < 2; i++) {
579 if (probe_group == &devc->analog_groups[i]) {
580 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
581 if (!strcmp(tmp_str, coupling[j])) {
582 g_free(devc->coupling[i]);
583 devc->coupling[i] = g_strdup(coupling[j]);
584 return set_cfg(sdi, ":CHAN%d:COUP %s", i + 1,
592 case SR_CONF_DATA_SOURCE:
593 tmp_str = g_variant_get_string(data, NULL);
594 if (!strcmp(tmp_str, "Live"))
595 devc->data_source = DATA_SOURCE_LIVE;
596 else if (!strcmp(tmp_str, "Memory"))
597 devc->data_source = DATA_SOURCE_MEMORY;
598 else if (devc->model->protocol == PROTOCOL_IEEE488_2
599 && !strcmp(tmp_str, "Segmented"))
600 devc->data_source = DATA_SOURCE_SEGMENTED;
612 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
613 const struct sr_probe_group *probe_group)
615 GVariant *tuple, *rational[2];
618 struct dev_context *devc = NULL;
623 if (key == SR_CONF_SCAN_OPTIONS) {
624 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
625 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
627 } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) {
628 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
629 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
633 /* Every other option requires a valid device instance. */
634 if (!sdi || !(devc = sdi->priv))
637 /* If a probe group is specified, it must be a valid one. */
639 if (probe_group != &devc->analog_groups[0]
640 && probe_group != &devc->analog_groups[1]) {
641 sr_err("Invalid probe group specified.");
648 case SR_CONF_DEVICE_OPTIONS:
650 sr_err("No probe group specified.");
651 return SR_ERR_PROBE_GROUP;
653 if (probe_group == &devc->digital_group) {
654 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
655 NULL, 0, sizeof(int32_t));
658 for (i = 0; i < 2; i++) {
659 if (probe_group == &devc->analog_groups[i]) {
660 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
661 analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t));
668 case SR_CONF_COUPLING:
670 sr_err("No probe group specified.");
671 return SR_ERR_PROBE_GROUP;
673 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
677 /* Can't know this until we have the exact model. */
680 sr_err("No probe group specified.");
681 return SR_ERR_PROBE_GROUP;
683 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
684 for (i = 0; i < devc->num_vdivs; i++) {
685 rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
686 rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
687 tuple = g_variant_new_tuple(rational, 2);
688 g_variant_builder_add_value(&gvb, tuple);
690 *data = g_variant_builder_end(&gvb);
692 case SR_CONF_TIMEBASE:
694 /* Can't know this until we have the exact model. */
696 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
697 for (i = 0; i < devc->num_timebases; i++) {
698 rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
699 rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
700 tuple = g_variant_new_tuple(rational, 2);
701 g_variant_builder_add_value(&gvb, tuple);
703 *data = g_variant_builder_end(&gvb);
705 case SR_CONF_TRIGGER_SOURCE:
707 /* Can't know this until we have the exact model. */
709 *data = g_variant_new_strv(trigger_sources,
710 devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
712 case SR_CONF_DATA_SOURCE:
714 /* Can't know this until we have the exact model. */
716 /* This needs tweaking by series/model! */
717 if (devc->model->series == RIGOL_DS2000)
718 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
720 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
729 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
731 struct sr_scpi_dev_inst *scpi;
732 struct dev_context *devc;
733 struct sr_probe *probe;
737 if (sdi->status != SR_ST_ACTIVE)
738 return SR_ERR_DEV_CLOSED;
743 if (devc->data_source == DATA_SOURCE_LIVE) {
744 if (sr_scpi_send(sdi->conn, ":RUN") != SR_OK)
746 } else if (devc->data_source == DATA_SOURCE_MEMORY) {
747 if (devc->model->series != RIGOL_DS2000) {
748 sr_err("Data source 'Memory' not supported for this device");
751 } else if (devc->data_source == DATA_SOURCE_SEGMENTED) {
752 sr_err("Data source 'Segmented' not yet supported");
756 for (l = sdi->probes; l; l = l->next) {
758 sr_dbg("handling probe %s", probe->name);
759 if (probe->type == SR_PROBE_ANALOG) {
761 devc->enabled_analog_probes = g_slist_append(
762 devc->enabled_analog_probes, probe);
763 if (probe->enabled != devc->analog_channels[probe->index]) {
764 /* Enabled channel is currently disabled, or vice versa. */
765 sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
766 probe->enabled ? "ON" : "OFF");
767 if (sr_scpi_send(sdi->conn, cmd) != SR_OK)
770 } else if (probe->type == SR_PROBE_LOGIC) {
772 devc->enabled_digital_probes = g_slist_append(
773 devc->enabled_digital_probes, probe);
774 if (probe->enabled != devc->digital_channels[probe->index]) {
775 /* Enabled channel is currently disabled, or vice versa. */
776 sprintf(cmd, ":DIG%d:TURN %s", probe->index,
777 probe->enabled ? "ON" : "OFF");
778 if (sr_scpi_send(sdi->conn, cmd) != SR_OK)
783 if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
786 sr_scpi_source_add(scpi, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
788 /* Send header packet to the session bus. */
789 std_session_send_df_header(cb_data, LOG_PREFIX);
791 if (devc->model->protocol == PROTOCOL_LEGACY) {
792 /* Fetch the first frame. */
793 if (devc->enabled_analog_probes) {
794 devc->analog_frame_size = DS1000_ANALOG_LIVE_WAVEFORM_SIZE;
795 devc->channel_frame = devc->enabled_analog_probes->data;
796 if (sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%d",
797 devc->channel_frame->index + 1) != SR_OK)
800 devc->channel_frame = devc->enabled_digital_probes->data;
801 if (sr_scpi_send(sdi->conn, ":WAV:DATA? DIG") != SR_OK)
805 devc->num_frame_bytes = 0;
807 if (devc->enabled_analog_probes) {
808 if (devc->data_source == DATA_SOURCE_MEMORY)
810 if (g_slist_length(devc->enabled_analog_probes) == 1)
811 devc->analog_frame_size = DS2000_ANALOG_MEM_WAVEFORM_SIZE_1C;
813 devc->analog_frame_size = DS2000_ANALOG_MEM_WAVEFORM_SIZE_2C;
814 /* Apparently for the DS2000 the memory
815 * depth can only be set in Running state -
816 * this matches the behaviour of the UI. */
817 if (sr_scpi_send(sdi->conn, ":RUN") != SR_OK)
819 if (sr_scpi_send(sdi->conn, "ACQ:MDEP %d", devc->analog_frame_size) != SR_OK)
821 if (sr_scpi_send(sdi->conn, ":STOP") != SR_OK)
824 devc->analog_frame_size = DS2000_ANALOG_LIVE_WAVEFORM_SIZE;
825 devc->channel_frame = devc->enabled_analog_probes->data;
826 if (rigol_ds_capture_start(sdi) != SR_OK)
834 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
836 struct dev_context *devc;
837 struct sr_scpi_dev_inst *scpi;
843 if (sdi->status != SR_ST_ACTIVE) {
844 sr_err("Device inactive, can't stop acquisition.");
848 g_slist_free(devc->enabled_analog_probes);
849 g_slist_free(devc->enabled_digital_probes);
850 devc->enabled_analog_probes = NULL;
851 devc->enabled_digital_probes = NULL;
853 sr_scpi_source_remove(scpi);
858 SR_PRIV struct sr_dev_driver rigol_ds_driver_info = {
860 .longname = "Rigol DS",
865 .dev_list = dev_list,
866 .dev_clear = dev_clear,
867 .config_get = config_get,
868 .config_set = config_set,
869 .config_list = config_list,
870 .dev_open = dev_open,
871 .dev_close = dev_close,
872 .dev_acquisition_start = dev_acquisition_start,
873 .dev_acquisition_stop = dev_acquisition_stop,