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_serial_dev_inst *serial;
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 (!(serial = sr_serial_dev_inst_new(port, NULL)))
231 return SR_ERR_MALLOC;
233 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
235 len = serial_write(serial, "*IDN?", 5);
236 len = serial_read(serial, buf, sizeof(buf));
237 if (serial_close(serial) != SR_OK)
240 sr_serial_dev_inst_free(serial);
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_serial_dev_inst_new(port, NULL)))
281 return SR_ERR_MALLOC;
283 sdi->inst_type = SR_INST_SERIAL;
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)
403 if (serial_open(sdi->conn, SERIAL_RDWR) != SR_OK)
406 if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
409 sdi->status = SR_ST_ACTIVE;
414 static int dev_close(struct sr_dev_inst *sdi)
416 struct sr_serial_dev_inst *serial;
419 if (serial && serial->fd != -1) {
420 serial_close(serial);
421 sdi->status = SR_ST_INACTIVE;
427 static int cleanup(void)
432 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
433 const struct sr_probe_group *probe_group)
435 struct dev_context *devc;
438 if (!sdi || !(devc = sdi->priv))
441 /* If a probe group is specified, it must be a valid one. */
443 if (probe_group != &devc->analog_groups[0]
444 && probe_group != &devc->analog_groups[1]) {
445 sr_err("Invalid probe group specified.");
451 case SR_CONF_NUM_TIMEBASE:
452 *data = g_variant_new_int32(devc->num_timebases);
454 case SR_CONF_NUM_VDIV:
456 sr_err("No probe group specified.");
457 return SR_ERR_PROBE_GROUP;
459 for (i = 0; i < 2; i++) {
460 if (probe_group == &devc->analog_groups[i]) {
461 *data = g_variant_new_int32(devc->num_vdivs);
466 case SR_CONF_DATA_SOURCE:
467 if (devc->data_source == DATA_SOURCE_LIVE)
468 *data = g_variant_new_string("Live");
469 else if (devc->data_source == DATA_SOURCE_MEMORY)
470 *data = g_variant_new_string("Memory");
472 *data = g_variant_new_string("Segmented");
481 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
482 const struct sr_probe_group *probe_group)
484 struct dev_context *devc;
485 uint64_t tmp_u64, p, q;
491 if (!(devc = sdi->priv))
494 if (sdi->status != SR_ST_ACTIVE)
495 return SR_ERR_DEV_CLOSED;
497 /* If a probe group is specified, it must be a valid one. */
499 if (probe_group != &devc->analog_groups[0]
500 && probe_group != &devc->analog_groups[1]) {
501 sr_err("Invalid probe group specified.");
508 case SR_CONF_LIMIT_FRAMES:
509 devc->limit_frames = g_variant_get_uint64(data);
511 case SR_CONF_TRIGGER_SLOPE:
512 tmp_u64 = g_variant_get_uint64(data);
513 if (tmp_u64 != 0 && tmp_u64 != 1)
515 g_free(devc->trigger_slope);
516 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
517 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
519 case SR_CONF_HORIZ_TRIGGERPOS:
520 t_dbl = g_variant_get_double(data);
521 if (t_dbl < 0.0 || t_dbl > 1.0)
523 devc->horiz_triggerpos = t_dbl;
524 /* We have the trigger offset as a percentage of the frame, but
525 * need to express this in seconds. */
526 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
527 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
529 case SR_CONF_TIMEBASE:
530 g_variant_get(data, "(tt)", &p, &q);
531 for (i = 0; i < devc->num_timebases; i++) {
532 if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
533 devc->timebase = (float)p / q;
534 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
538 if (i == devc->num_timebases)
541 case SR_CONF_TRIGGER_SOURCE:
542 tmp_str = g_variant_get_string(data, NULL);
543 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
544 if (!strcmp(trigger_sources[i], tmp_str)) {
545 g_free(devc->trigger_source);
546 devc->trigger_source = g_strdup(trigger_sources[i]);
547 if (!strcmp(devc->trigger_source, "AC Line"))
549 else if (!strcmp(devc->trigger_source, "CH1"))
551 else if (!strcmp(devc->trigger_source, "CH2"))
554 tmp_str = (char *)devc->trigger_source;
555 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
559 if (i == ARRAY_SIZE(trigger_sources))
564 sr_err("No probe group specified.");
565 return SR_ERR_PROBE_GROUP;
567 g_variant_get(data, "(tt)", &p, &q);
568 for (i = 0; i < 2; i++) {
569 if (probe_group == &devc->analog_groups[i]) {
570 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
571 if (vdivs[j][0] != p || vdivs[j][1] != q)
573 devc->vdiv[i] = (float)p / q;
574 return set_cfg(sdi, ":CHAN%d:SCAL %.3f", i + 1,
581 case SR_CONF_COUPLING:
583 sr_err("No probe group specified.");
584 return SR_ERR_PROBE_GROUP;
586 tmp_str = g_variant_get_string(data, NULL);
587 for (i = 0; i < 2; i++) {
588 if (probe_group == &devc->analog_groups[i]) {
589 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
590 if (!strcmp(tmp_str, coupling[j])) {
591 g_free(devc->coupling[i]);
592 devc->coupling[i] = g_strdup(coupling[j]);
593 return set_cfg(sdi, ":CHAN%d:COUP %s", i + 1,
601 case SR_CONF_DATA_SOURCE:
602 tmp_str = g_variant_get_string(data, NULL);
603 if (!strcmp(tmp_str, "Live"))
604 devc->data_source = DATA_SOURCE_LIVE;
605 else if (!strcmp(tmp_str, "Memory"))
606 devc->data_source = DATA_SOURCE_MEMORY;
607 else if (devc->model->protocol == PROTOCOL_IEEE488_2
608 && !strcmp(tmp_str, "Segmented"))
609 devc->data_source = DATA_SOURCE_SEGMENTED;
621 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
622 const struct sr_probe_group *probe_group)
624 GVariant *tuple, *rational[2];
627 struct dev_context *devc = NULL;
632 if (key == SR_CONF_SCAN_OPTIONS) {
633 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
634 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
636 } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) {
637 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
638 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
642 /* Every other option requires a valid device instance. */
643 if (!sdi || !(devc = sdi->priv))
646 /* If a probe group is specified, it must be a valid one. */
648 if (probe_group != &devc->analog_groups[0]
649 && probe_group != &devc->analog_groups[1]) {
650 sr_err("Invalid probe group specified.");
657 case SR_CONF_DEVICE_OPTIONS:
659 sr_err("No probe group specified.");
660 return SR_ERR_PROBE_GROUP;
662 if (probe_group == &devc->digital_group) {
663 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
664 NULL, 0, sizeof(int32_t));
667 for (i = 0; i < 2; i++) {
668 if (probe_group == &devc->analog_groups[i]) {
669 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
670 analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t));
677 case SR_CONF_COUPLING:
679 sr_err("No probe group specified.");
680 return SR_ERR_PROBE_GROUP;
682 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
686 /* Can't know this until we have the exact model. */
689 sr_err("No probe group specified.");
690 return SR_ERR_PROBE_GROUP;
692 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
693 for (i = 0; i < devc->num_vdivs; i++) {
694 rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
695 rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
696 tuple = g_variant_new_tuple(rational, 2);
697 g_variant_builder_add_value(&gvb, tuple);
699 *data = g_variant_builder_end(&gvb);
701 case SR_CONF_TIMEBASE:
703 /* Can't know this until we have the exact model. */
705 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
706 for (i = 0; i < devc->num_timebases; i++) {
707 rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
708 rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
709 tuple = g_variant_new_tuple(rational, 2);
710 g_variant_builder_add_value(&gvb, tuple);
712 *data = g_variant_builder_end(&gvb);
714 case SR_CONF_TRIGGER_SOURCE:
716 /* Can't know this until we have the exact model. */
718 *data = g_variant_new_strv(trigger_sources,
719 devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
721 case SR_CONF_DATA_SOURCE:
723 /* Can't know this until we have the exact model. */
725 /* This needs tweaking by series/model! */
726 if (devc->model->series == RIGOL_DS2000)
727 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
729 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
738 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
740 struct sr_serial_dev_inst *serial;
741 struct dev_context *devc;
742 struct sr_probe *probe;
746 if (sdi->status != SR_ST_ACTIVE)
747 return SR_ERR_DEV_CLOSED;
752 if (devc->data_source == DATA_SOURCE_LIVE) {
753 if (rigol_ds_send(sdi, ":RUN") != SR_OK)
755 } else if (devc->data_source == DATA_SOURCE_MEMORY) {
756 if (devc->model->series != RIGOL_DS2000) {
757 sr_err("Data source 'Memory' not supported for this device");
760 } else if (devc->data_source == DATA_SOURCE_SEGMENTED) {
761 sr_err("Data source 'Segmented' not yet supported");
765 for (l = sdi->probes; l; l = l->next) {
767 sr_dbg("handling probe %s", probe->name);
768 if (probe->type == SR_PROBE_ANALOG) {
770 devc->enabled_analog_probes = g_slist_append(
771 devc->enabled_analog_probes, probe);
772 if (probe->enabled != devc->analog_channels[probe->index]) {
773 /* Enabled channel is currently disabled, or vice versa. */
774 sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
775 probe->enabled ? "ON" : "OFF");
776 if (rigol_ds_send(sdi, cmd) != SR_OK)
779 } else if (probe->type == SR_PROBE_LOGIC) {
781 devc->enabled_digital_probes = g_slist_append(
782 devc->enabled_digital_probes, probe);
783 if (probe->enabled != devc->digital_channels[probe->index]) {
784 /* Enabled channel is currently disabled, or vice versa. */
785 sprintf(cmd, ":DIG%d:TURN %s", probe->index,
786 probe->enabled ? "ON" : "OFF");
787 if (rigol_ds_send(sdi, cmd) != SR_OK)
792 if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
795 sr_source_add(serial->fd, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
797 /* Send header packet to the session bus. */
798 std_session_send_df_header(cb_data, LOG_PREFIX);
800 if (devc->model->protocol == PROTOCOL_LEGACY) {
801 /* Fetch the first frame. */
802 if (devc->enabled_analog_probes) {
803 devc->analog_frame_size = DS1000_ANALOG_LIVE_WAVEFORM_SIZE;
804 devc->channel_frame = devc->enabled_analog_probes->data;
805 if (rigol_ds_send(sdi, ":WAV:DATA? CHAN%d",
806 devc->channel_frame->index + 1) != SR_OK)
809 devc->channel_frame = devc->enabled_digital_probes->data;
810 if (rigol_ds_send(sdi, ":WAV:DATA? DIG") != SR_OK)
814 devc->num_frame_bytes = 0;
816 if (devc->enabled_analog_probes) {
817 if (devc->data_source == DATA_SOURCE_MEMORY)
819 if (g_slist_length(devc->enabled_analog_probes) == 1)
820 devc->analog_frame_size = DS2000_ANALOG_MEM_WAVEFORM_SIZE_1C;
822 devc->analog_frame_size = DS2000_ANALOG_MEM_WAVEFORM_SIZE_2C;
823 /* Apparently for the DS2000 the memory
824 * depth can only be set in Running state -
825 * this matches the behaviour of the UI. */
826 if (rigol_ds_send(sdi, ":RUN") != SR_OK)
828 if (rigol_ds_send(sdi, "ACQ:MDEP %d", devc->analog_frame_size) != SR_OK)
830 if (rigol_ds_send(sdi, ":STOP") != SR_OK)
833 devc->analog_frame_size = DS2000_ANALOG_LIVE_WAVEFORM_SIZE;
834 devc->channel_frame = devc->enabled_analog_probes->data;
835 if (rigol_ds_capture_start(sdi) != SR_OK)
843 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
845 struct dev_context *devc;
846 struct sr_serial_dev_inst *serial;
852 if (sdi->status != SR_ST_ACTIVE) {
853 sr_err("Device inactive, can't stop acquisition.");
857 g_slist_free(devc->enabled_analog_probes);
858 g_slist_free(devc->enabled_digital_probes);
859 devc->enabled_analog_probes = NULL;
860 devc->enabled_digital_probes = NULL;
862 sr_source_remove(serial->fd);
867 SR_PRIV struct sr_dev_driver rigol_ds_driver_info = {
869 .longname = "Rigol DS",
874 .dev_list = dev_list,
875 .dev_clear = dev_clear,
876 .config_get = config_get,
877 .config_set = config_set,
878 .config_list = config_list,
879 .dev_open = dev_open,
880 .dev_close = dev_close,
881 .dev_acquisition_start = dev_acquisition_start,
882 .dev_acquisition_stop = dev_acquisition_stop,