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/>.
28 #include "libsigrok.h"
29 #include "libsigrok-internal.h"
32 static const int32_t hwopts[] = {
37 static const int32_t hwcaps[] = {
40 SR_CONF_TRIGGER_SOURCE,
41 SR_CONF_TRIGGER_SLOPE,
42 SR_CONF_HORIZ_TRIGGERPOS,
48 static const int32_t analog_hwcaps[] = {
55 static const uint64_t timebases[][2] = {
98 static const uint64_t vdivs[][2] = {
118 #define NUM_TIMEBASE ARRAY_SIZE(timebases)
119 #define NUM_VDIV ARRAY_SIZE(vdivs)
121 static const char *trigger_sources[] = {
146 static const char *coupling[] = {
152 /* Do not change the order of entries */
153 static const char *data_sources[] = {
160 * name, series, protocol flavor, min timebase, max timebase, min vdiv,
161 * digital channels, number of horizontal divs
164 #define RIGOL "Rigol Technologies"
165 #define AGILENT "Agilent Technologies"
167 static const struct rigol_ds_model supported_models[] = {
168 {RIGOL, "DS1052E", RIGOL_DS1000, PROTOCOL_IEEE488_2, {5, 1000000000}, {50, 1}, {2, 1000}, 2, false, 12},
169 {RIGOL, "DS1102E", RIGOL_DS1000, PROTOCOL_IEEE488_2, {2, 1000000000}, {50, 1}, {2, 1000}, 2, false, 12},
170 {RIGOL, "DS1152E", RIGOL_DS1000, PROTOCOL_IEEE488_2, {2, 1000000000}, {50, 1}, {2, 1000}, 2, false, 12},
171 {RIGOL, "DS1052D", RIGOL_DS1000, PROTOCOL_IEEE488_2, {5, 1000000000}, {50, 1}, {2, 1000}, 2, true, 12},
172 {RIGOL, "DS1102D", RIGOL_DS1000, PROTOCOL_IEEE488_2, {2, 1000000000}, {50, 1}, {2, 1000}, 2, true, 12},
173 {RIGOL, "DS1152D", RIGOL_DS1000, PROTOCOL_IEEE488_2, {2, 1000000000}, {50, 1}, {2, 1000}, 2, true, 12},
174 {RIGOL, "DS2072", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {500, 1}, {500, 1000000}, 2, false, 14},
175 {RIGOL, "DS2102", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {500, 1}, {500, 1000000}, 2, false, 14},
176 {RIGOL, "DS2202", RIGOL_DS2000, PROTOCOL_IEEE488_2, {2, 1000000000}, {500, 1}, {500, 1000000}, 2, false, 14},
177 {RIGOL, "DS2302", RIGOL_DS2000, PROTOCOL_IEEE488_2, {1, 1000000000}, {1000, 1}, {500, 1000000}, 2, false, 14},
178 {RIGOL, "DS2072A", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {1000, 1}, {500, 1000000}, 2, false, 14},
179 {RIGOL, "DS2102A", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {1000, 1}, {500, 1000000}, 2, false, 14},
180 {RIGOL, "DS2202A", RIGOL_DS2000, PROTOCOL_IEEE488_2, {2, 1000000000}, {1000, 1}, {500, 1000000}, 2, false, 14},
181 {RIGOL, "DS2302A", RIGOL_DS2000, PROTOCOL_IEEE488_2, {1, 1000000000}, {1000, 1}, {500, 1000000}, 2, false, 14},
182 {RIGOL, "VS5022", RIGOL_VS5000, PROTOCOL_LEGACY, {20, 1000000000}, {50, 1}, {2, 1000}, 2, false, 14},
183 {RIGOL, "VS5022D", RIGOL_VS5000, PROTOCOL_LEGACY, {20, 1000000000}, {50, 1}, {2, 1000}, 2, true, 14},
184 {RIGOL, "VS5042", RIGOL_VS5000, PROTOCOL_LEGACY, {10, 1000000000}, {50, 1}, {2, 1000}, 2, false, 14},
185 {RIGOL, "VS5042D", RIGOL_VS5000, PROTOCOL_LEGACY, {10, 1000000000}, {50, 1}, {2, 1000}, 2, true, 14},
186 {RIGOL, "VS5062", RIGOL_VS5000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, 2, false, 14},
187 {RIGOL, "VS5062D", RIGOL_VS5000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, 2, true, 14},
188 {RIGOL, "VS5102", RIGOL_VS5000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, 2, false, 14},
189 {RIGOL, "VS5102D", RIGOL_VS5000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, 2, true, 14},
190 {RIGOL, "VS5202", RIGOL_VS5000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, 2, false, 14},
191 {RIGOL, "VS5202D", RIGOL_VS5000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, 2, true, 14},
192 {AGILENT, "DSO1002A", AGILENT_DSO1000, PROTOCOL_IEEE488_2, {5, 1000000000}, {50, 1}, {2, 1000}, 2, false, 12},
193 {AGILENT, "DSO1004A", AGILENT_DSO1000, PROTOCOL_IEEE488_2, {5, 1000000000}, {50, 1}, {2, 1000}, 4, false, 12},
194 {AGILENT, "DSO1012A", AGILENT_DSO1000, PROTOCOL_IEEE488_2, {2, 1000000000}, {50, 1}, {2, 1000}, 2, false, 12},
195 {AGILENT, "DSO1014A", AGILENT_DSO1000, PROTOCOL_IEEE488_2, {2, 1000000000}, {50, 1}, {2, 1000}, 4, false, 12},
196 {AGILENT, "DSO1022A", AGILENT_DSO1000, PROTOCOL_IEEE488_2, {2, 1000000000}, {50, 1}, {2, 1000}, 2, false, 12},
197 {AGILENT, "DSO1024A", AGILENT_DSO1000, PROTOCOL_IEEE488_2, {2, 1000000000}, {50, 1}, {2, 1000}, 4, false, 12},
200 SR_PRIV struct sr_dev_driver rigol_ds_driver_info;
201 static struct sr_dev_driver *di = &rigol_ds_driver_info;
203 static void clear_helper(void *priv)
205 struct dev_context *devc;
209 g_free(devc->buffer);
210 g_free(devc->coupling[0]);
211 g_free(devc->coupling[1]);
212 g_free(devc->trigger_source);
213 g_free(devc->trigger_slope);
214 g_slist_free(devc->analog_groups[0].probes);
215 g_slist_free(devc->analog_groups[1].probes);
216 g_slist_free(devc->digital_group.probes);
219 static int dev_clear(void)
221 return std_dev_clear(di, clear_helper);
224 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
229 va_start(args, format);
230 ret = sr_scpi_send_variadic(sdi->conn, format, args);
236 return sr_scpi_get_opc(sdi->conn);
239 static int init(struct sr_context *sr_ctx)
241 return std_init(sr_ctx, di, LOG_PREFIX);
244 static int probe_port(const char *resource, const char *serialcomm, GSList **devices)
246 struct dev_context *devc;
247 struct sr_dev_inst *sdi;
248 struct sr_scpi_dev_inst *scpi;
249 struct sr_scpi_hw_info *hw_info;
250 struct sr_probe *probe;
252 const struct rigol_ds_model *model = NULL;
257 if (!(scpi = scpi_dev_inst_new(resource, serialcomm)))
260 if (sr_scpi_open(scpi) != SR_OK) {
261 sr_info("Couldn't open SCPI device.");
266 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
267 sr_info("Couldn't get IDN response.");
273 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
274 if (!strcasecmp(hw_info->manufacturer, supported_models[i].vendor) &&
275 !strcmp(hw_info->model, supported_models[i].name)) {
276 model = &supported_models[i];
281 if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
282 hw_info->manufacturer, hw_info->model,
283 hw_info->firmware_version))) {
284 sr_scpi_hw_info_free(hw_info);
290 sr_scpi_hw_info_free(hw_info);
296 sdi->inst_type = SR_INST_SCPI;
298 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
299 return SR_ERR_MALLOC;
301 devc->limit_frames = 0;
304 for (i = 0; i < model->analog_channels; i++) {
305 if (!(channel_name = g_strdup_printf("CH%d", i + 1)))
306 return SR_ERR_MALLOC;
307 probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name);
308 sdi->probes = g_slist_append(sdi->probes, probe);
309 devc->analog_groups[i].name = channel_name;
310 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
311 sdi->probe_groups = g_slist_append(sdi->probe_groups,
312 &devc->analog_groups[i]);
315 if (devc->model->has_digital) {
316 for (i = 0; i < 16; i++) {
317 if (!(channel_name = g_strdup_printf("D%d", i)))
318 return SR_ERR_MALLOC;
319 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
320 g_free(channel_name);
322 return SR_ERR_MALLOC;
323 sdi->probes = g_slist_append(sdi->probes, probe);
324 devc->digital_group.probes = g_slist_append(
325 devc->digital_group.probes, probe);
327 devc->digital_group.name = "LA";
328 sdi->probe_groups = g_slist_append(sdi->probe_groups,
329 &devc->digital_group);
332 for (i = 0; i < NUM_TIMEBASE; i++) {
333 if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
334 devc->timebases = &timebases[i];
335 if (!memcmp(&devc->model->max_timebase, &timebases[i], sizeof(uint64_t[2])))
336 devc->num_timebases = &timebases[i] - devc->timebases + 1;
339 for (i = 0; i < NUM_VDIV; i++)
340 if (!memcmp(&devc->model->min_vdiv, &vdivs[i], sizeof(uint64_t[2])))
341 devc->vdivs = &vdivs[i];
343 if (!(devc->buffer = g_try_malloc(ACQ_BUFFER_SIZE)))
344 return SR_ERR_MALLOC;
345 if (!(devc->data = g_try_malloc(ACQ_BUFFER_SIZE * sizeof(float))))
346 return SR_ERR_MALLOC;
348 devc->data_source = DATA_SOURCE_LIVE;
352 *devices = g_slist_append(NULL, sdi);
357 static GSList *scan(GSList *options)
359 struct drv_context *drvc;
360 struct sr_config *src;
364 const gchar *dev_name;
366 gchar *serialcomm = NULL;
370 for (l = options; l; l = l->next) {
374 port = (char *)g_variant_get_string(src->data, NULL);
376 case SR_CONF_SERIALCOMM:
377 serialcomm = (char *)g_variant_get_string(src->data, NULL);
384 if (probe_port(port, serialcomm, &devices) == SR_ERR_MALLOC) {
391 if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
392 if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
394 while ((dev_name = g_dir_read_name(dir))) {
395 if (strncmp(dev_name, "usbtmc", 6))
397 port = g_strconcat("/dev/", dev_name, NULL);
398 ret = probe_port(port, serialcomm, &devices);
402 if (ret == SR_ERR_MALLOC) {
410 /* Tack a copy of the newly found devices onto the driver list. */
411 l = g_slist_copy(devices);
412 drvc->instances = g_slist_concat(drvc->instances, l);
417 static GSList *dev_list(void)
419 return ((struct drv_context *)(di->priv))->instances;
422 static int dev_open(struct sr_dev_inst *sdi)
424 struct sr_scpi_dev_inst *scpi = sdi->conn;
426 if (sr_scpi_open(scpi) < 0)
429 if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
432 sdi->status = SR_ST_ACTIVE;
437 static int dev_close(struct sr_dev_inst *sdi)
439 struct sr_scpi_dev_inst *scpi;
444 if (sr_scpi_close(scpi) < 0)
446 sdi->status = SR_ST_INACTIVE;
452 static int cleanup(void)
457 static int analog_frame_size(const struct sr_dev_inst *sdi)
459 struct dev_context *devc = sdi->priv;
460 struct sr_probe *probe;
461 int analog_probes = 0;
464 switch (devc->model->series) {
466 return VS5000_ANALOG_LIVE_WAVEFORM_SIZE;
468 return DS1000_ANALOG_LIVE_WAVEFORM_SIZE;
470 for (l = sdi->probes; l; l = l->next) {
472 if (probe->type == SR_PROBE_ANALOG && probe->enabled)
475 if (devc->data_source == DATA_SOURCE_MEMORY) {
476 if (analog_probes == 1)
477 return DS2000_ANALOG_MEM_WAVEFORM_SIZE_1C;
479 return DS2000_ANALOG_MEM_WAVEFORM_SIZE_2C;
481 if (devc->model->series == AGILENT_DSO1000)
482 return DSO1000_ANALOG_LIVE_WAVEFORM_SIZE;
484 return DS2000_ANALOG_LIVE_WAVEFORM_SIZE;
489 static int digital_frame_size(const struct sr_dev_inst *sdi)
491 struct dev_context *devc = sdi->priv;
493 switch (devc->model->series) {
495 return VS5000_DIGITAL_WAVEFORM_SIZE;
497 return DS1000_DIGITAL_WAVEFORM_SIZE;
503 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
504 const struct sr_probe_group *probe_group)
506 struct dev_context *devc;
507 struct sr_probe *probe;
510 int analog_channel = -1;
511 float smallest_diff = 0.0000000001;
515 if (!sdi || !(devc = sdi->priv))
518 /* If a probe group is specified, it must be a valid one. */
519 if (probe_group && !g_slist_find(sdi->probe_groups, probe_group)) {
520 sr_err("Invalid probe group specified.");
525 probe = g_slist_nth_data(probe_group->probes, 0);
528 if (probe->type == SR_PROBE_ANALOG) {
529 if (probe->name[2] < '1' || probe->name[2] > '4')
531 analog_channel = probe->name[2] - '1';
536 case SR_CONF_NUM_TIMEBASE:
537 *data = g_variant_new_int32(devc->model->num_horizontal_divs);
539 case SR_CONF_NUM_VDIV:
540 *data = g_variant_new_int32(NUM_VDIV);
541 case SR_CONF_DATA_SOURCE:
542 if (devc->data_source == DATA_SOURCE_LIVE)
543 *data = g_variant_new_string("Live");
544 else if (devc->data_source == DATA_SOURCE_MEMORY)
545 *data = g_variant_new_string("Memory");
547 *data = g_variant_new_string("Segmented");
549 case SR_CONF_SAMPLERATE:
550 if (devc->data_source == DATA_SOURCE_LIVE) {
551 samplerate = analog_frame_size(sdi) /
552 (devc->timebase * devc->model->num_horizontal_divs);
553 *data = g_variant_new_uint64(samplerate);
558 case SR_CONF_TRIGGER_SOURCE:
559 if (!strcmp(devc->trigger_source, "ACL"))
561 else if (!strcmp(devc->trigger_source, "CHAN1"))
563 else if (!strcmp(devc->trigger_source, "CHAN2"))
565 else if (!strcmp(devc->trigger_source, "CHAN3"))
567 else if (!strcmp(devc->trigger_source, "CHAN4"))
570 tmp_str = devc->trigger_source;
571 *data = g_variant_new_string(tmp_str);
573 case SR_CONF_TIMEBASE:
574 for (i = 0; i < devc->num_timebases; i++) {
575 float tb = (float)devc->timebases[i][0] / devc->timebases[i][1];
576 float diff = fabs(devc->timebase - tb);
577 if (diff < smallest_diff) {
578 smallest_diff = diff;
584 *data = g_variant_new("(tt)", devc->timebases[idx][0],
585 devc->timebases[idx][1]);
588 if (analog_channel < 0)
590 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
591 float vdiv = (float)vdivs[i][0] / vdivs[i][1];
592 float diff = fabs(devc->vdiv[analog_channel] - vdiv);
593 if (diff < smallest_diff) {
594 smallest_diff = diff;
600 *data = g_variant_new("(tt)", vdivs[idx][0], vdivs[idx][1]);
602 case SR_CONF_COUPLING:
603 if (analog_channel < 0)
605 *data = g_variant_new_string(devc->coupling[analog_channel]);
614 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
615 const struct sr_probe_group *probe_group)
617 struct dev_context *devc;
618 uint64_t tmp_u64, p, q;
625 if (!(devc = sdi->priv))
628 if (sdi->status != SR_ST_ACTIVE)
629 return SR_ERR_DEV_CLOSED;
631 /* If a probe group is specified, it must be a valid one. */
632 if (probe_group && !g_slist_find(sdi->probe_groups, probe_group)) {
633 sr_err("Invalid probe group specified.");
639 case SR_CONF_LIMIT_FRAMES:
640 devc->limit_frames = g_variant_get_uint64(data);
642 case SR_CONF_TRIGGER_SLOPE:
643 tmp_u64 = g_variant_get_uint64(data);
644 if (tmp_u64 != 0 && tmp_u64 != 1)
646 g_free(devc->trigger_slope);
647 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
648 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
650 case SR_CONF_HORIZ_TRIGGERPOS:
651 t_dbl = g_variant_get_double(data);
652 if (t_dbl < 0.0 || t_dbl > 1.0)
654 devc->horiz_triggerpos = t_dbl;
655 /* We have the trigger offset as a percentage of the frame, but
656 * need to express this in seconds. */
657 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
658 g_ascii_formatd(buffer, sizeof(buffer), "%.6f", t_dbl);
659 ret = set_cfg(sdi, ":TIM:OFFS %s", buffer);
661 case SR_CONF_TIMEBASE:
662 g_variant_get(data, "(tt)", &p, &q);
663 for (i = 0; i < devc->num_timebases; i++) {
664 if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
665 devc->timebase = (float)p / q;
666 g_ascii_formatd(buffer, sizeof(buffer), "%.9f",
668 ret = set_cfg(sdi, ":TIM:SCAL %s", buffer);
672 if (i == devc->num_timebases)
675 case SR_CONF_TRIGGER_SOURCE:
676 tmp_str = g_variant_get_string(data, NULL);
677 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
678 if (!strcmp(trigger_sources[i], tmp_str)) {
679 g_free(devc->trigger_source);
680 devc->trigger_source = g_strdup(trigger_sources[i]);
681 if (!strcmp(devc->trigger_source, "AC Line"))
683 else if (!strcmp(devc->trigger_source, "CH1"))
685 else if (!strcmp(devc->trigger_source, "CH2"))
687 else if (!strcmp(devc->trigger_source, "CH3"))
689 else if (!strcmp(devc->trigger_source, "CH4"))
692 tmp_str = (char *)devc->trigger_source;
693 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
697 if (i == ARRAY_SIZE(trigger_sources))
702 sr_err("No probe group specified.");
703 return SR_ERR_PROBE_GROUP;
705 g_variant_get(data, "(tt)", &p, &q);
706 for (i = 0; i < 2; i++) {
707 if (probe_group == &devc->analog_groups[i]) {
708 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
709 if (vdivs[j][0] != p || vdivs[j][1] != q)
711 devc->vdiv[i] = (float)p / q;
712 g_ascii_formatd(buffer, sizeof(buffer), "%.3f",
714 return set_cfg(sdi, ":CHAN%d:SCAL %s", i + 1,
721 case SR_CONF_COUPLING:
723 sr_err("No probe group specified.");
724 return SR_ERR_PROBE_GROUP;
726 tmp_str = g_variant_get_string(data, NULL);
727 for (i = 0; i < 2; i++) {
728 if (probe_group == &devc->analog_groups[i]) {
729 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
730 if (!strcmp(tmp_str, coupling[j])) {
731 g_free(devc->coupling[i]);
732 devc->coupling[i] = g_strdup(coupling[j]);
733 return set_cfg(sdi, ":CHAN%d:COUP %s", i + 1,
741 case SR_CONF_DATA_SOURCE:
742 tmp_str = g_variant_get_string(data, NULL);
743 if (!strcmp(tmp_str, "Live"))
744 devc->data_source = DATA_SOURCE_LIVE;
745 else if (!strcmp(tmp_str, "Memory"))
746 devc->data_source = DATA_SOURCE_MEMORY;
747 else if (devc->model->series >= RIGOL_DS1000Z
748 && !strcmp(tmp_str, "Segmented"))
749 devc->data_source = DATA_SOURCE_SEGMENTED;
761 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
762 const struct sr_probe_group *probe_group)
764 GVariant *tuple, *rational[2];
767 struct dev_context *devc = NULL;
772 if (key == SR_CONF_SCAN_OPTIONS) {
773 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
774 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
776 } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) {
777 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
778 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
782 /* Every other option requires a valid device instance. */
783 if (!sdi || !(devc = sdi->priv))
786 /* If a probe group is specified, it must be a valid one. */
788 if (probe_group != &devc->analog_groups[0]
789 && probe_group != &devc->analog_groups[1]) {
790 sr_err("Invalid probe group specified.");
796 case SR_CONF_DEVICE_OPTIONS:
798 sr_err("No probe group specified.");
799 return SR_ERR_PROBE_GROUP;
801 if (probe_group == &devc->digital_group) {
802 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
803 NULL, 0, sizeof(int32_t));
806 for (i = 0; i < 2; i++) {
807 if (probe_group == &devc->analog_groups[i]) {
808 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
809 analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t));
816 case SR_CONF_COUPLING:
818 sr_err("No probe group specified.");
819 return SR_ERR_PROBE_GROUP;
821 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
825 /* Can't know this until we have the exact model. */
828 sr_err("No probe group specified.");
829 return SR_ERR_PROBE_GROUP;
831 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
832 for (i = 0; i < NUM_VDIV; i++) {
833 rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
834 rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
835 tuple = g_variant_new_tuple(rational, 2);
836 g_variant_builder_add_value(&gvb, tuple);
838 *data = g_variant_builder_end(&gvb);
840 case SR_CONF_TIMEBASE:
842 /* Can't know this until we have the exact model. */
844 if (devc->num_timebases <= 0)
846 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
847 for (i = 0; i < devc->num_timebases; i++) {
848 rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
849 rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
850 tuple = g_variant_new_tuple(rational, 2);
851 g_variant_builder_add_value(&gvb, tuple);
853 *data = g_variant_builder_end(&gvb);
855 case SR_CONF_TRIGGER_SOURCE:
857 /* Can't know this until we have the exact model. */
859 *data = g_variant_new_strv(trigger_sources,
860 devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
862 case SR_CONF_DATA_SOURCE:
864 /* Can't know this until we have the exact model. */
866 /* This needs tweaking by series/model! */
867 if (devc->model->series == RIGOL_DS2000)
868 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
870 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
879 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
881 struct sr_scpi_dev_inst *scpi;
882 struct dev_context *devc;
883 struct sr_probe *probe;
884 struct sr_datafeed_packet packet;
887 if (sdi->status != SR_ST_ACTIVE)
888 return SR_ERR_DEV_CLOSED;
893 devc->num_frames = 0;
895 for (l = sdi->probes; l; l = l->next) {
897 sr_dbg("handling probe %s", probe->name);
898 if (probe->type == SR_PROBE_ANALOG) {
900 devc->enabled_analog_probes = g_slist_append(
901 devc->enabled_analog_probes, probe);
902 if (probe->enabled != devc->analog_channels[probe->index]) {
903 /* Enabled channel is currently disabled, or vice versa. */
904 if (set_cfg(sdi, ":CHAN%d:DISP %s", probe->index + 1,
905 probe->enabled ? "ON" : "OFF") != SR_OK)
907 devc->analog_channels[probe->index] = probe->enabled;
909 } else if (probe->type == SR_PROBE_LOGIC) {
910 if (probe->enabled) {
911 devc->enabled_digital_probes = g_slist_append(
912 devc->enabled_digital_probes, probe);
913 /* Turn on LA module if currently off. */
914 if (!devc->la_enabled) {
915 if (set_cfg(sdi, ":LA:DISP ON") != SR_OK)
917 devc->la_enabled = TRUE;
920 if (probe->enabled != devc->digital_channels[probe->index]) {
921 /* Enabled channel is currently disabled, or vice versa. */
922 if (set_cfg(sdi, ":DIG%d:TURN %s", probe->index,
923 probe->enabled ? "ON" : "OFF") != SR_OK)
925 devc->digital_channels[probe->index] = probe->enabled;
930 if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
933 /* Turn off LA module if on and no digital probes selected. */
934 if (devc->la_enabled && !devc->enabled_digital_probes)
935 if (set_cfg(sdi, ":LA:DISP OFF") != SR_OK)
938 if (devc->data_source == DATA_SOURCE_LIVE) {
939 if (set_cfg(sdi, ":RUN") != SR_OK)
941 } else if (devc->data_source == DATA_SOURCE_MEMORY) {
942 if (devc->model->series != RIGOL_DS2000) {
943 sr_err("Data source 'Memory' not supported for this device");
946 } else if (devc->data_source == DATA_SOURCE_SEGMENTED) {
947 sr_err("Data source 'Segmented' not yet supported");
951 sr_scpi_source_add(scpi, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
953 /* Send header packet to the session bus. */
954 std_session_send_df_header(cb_data, LOG_PREFIX);
956 if (devc->enabled_analog_probes)
957 devc->channel_entry = devc->enabled_analog_probes;
959 devc->channel_entry = devc->enabled_digital_probes;
961 devc->analog_frame_size = analog_frame_size(sdi);
962 devc->digital_frame_size = digital_frame_size(sdi);
964 if (devc->model->series < RIGOL_DS1000Z) {
965 /* Fetch the first frame. */
966 if (rigol_ds_channel_start(sdi) != SR_OK)
969 if (devc->enabled_analog_probes) {
970 if (devc->data_source == DATA_SOURCE_MEMORY) {
971 /* Apparently for the DS2000 the memory
972 * depth can only be set in Running state -
973 * this matches the behaviour of the UI. */
974 if (set_cfg(sdi, ":RUN") != SR_OK)
976 if (set_cfg(sdi, "ACQ:MDEP %d", devc->analog_frame_size) != SR_OK)
978 if (set_cfg(sdi, ":STOP") != SR_OK)
981 if (rigol_ds_capture_start(sdi) != SR_OK)
986 /* Start of first frame. */
987 packet.type = SR_DF_FRAME_BEGIN;
988 sr_session_send(cb_data, &packet);
993 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
995 struct dev_context *devc;
996 struct sr_scpi_dev_inst *scpi;
997 struct sr_datafeed_packet packet;
1003 if (sdi->status != SR_ST_ACTIVE) {
1004 sr_err("Device inactive, can't stop acquisition.");
1008 /* End of last frame. */
1009 packet.type = SR_DF_END;
1010 sr_session_send(sdi, &packet);
1012 g_slist_free(devc->enabled_analog_probes);
1013 g_slist_free(devc->enabled_digital_probes);
1014 devc->enabled_analog_probes = NULL;
1015 devc->enabled_digital_probes = NULL;
1017 sr_scpi_source_remove(scpi);
1022 SR_PRIV struct sr_dev_driver rigol_ds_driver_info = {
1024 .longname = "Rigol DS",
1029 .dev_list = dev_list,
1030 .dev_clear = dev_clear,
1031 .config_get = config_get,
1032 .config_set = config_set,
1033 .config_list = config_list,
1034 .dev_open = dev_open,
1035 .dev_close = dev_close,
1036 .dev_acquisition_start = dev_acquisition_start,
1037 .dev_acquisition_stop = dev_acquisition_stop,