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"
166 #define RIGOL_SHORT "Rigol"
167 #define AGILENT_SHORT "Agilent"
169 static const struct rigol_ds_model supported_models[] = {
170 {RIGOL, "DS1052E", RIGOL_DS1000, PROTOCOL_IEEE488_2, {5, 1000000000}, {50, 1}, {2, 1000}, 2, false, 12},
171 {RIGOL, "DS1102E", RIGOL_DS1000, PROTOCOL_IEEE488_2, {2, 1000000000}, {50, 1}, {2, 1000}, 2, false, 12},
172 {RIGOL, "DS1152E", RIGOL_DS1000, PROTOCOL_IEEE488_2, {2, 1000000000}, {50, 1}, {2, 1000}, 2, false, 12},
173 {RIGOL, "DS1052D", RIGOL_DS1000, PROTOCOL_IEEE488_2, {5, 1000000000}, {50, 1}, {2, 1000}, 2, true, 12},
174 {RIGOL, "DS1102D", RIGOL_DS1000, PROTOCOL_IEEE488_2, {2, 1000000000}, {50, 1}, {2, 1000}, 2, true, 12},
175 {RIGOL, "DS1152D", RIGOL_DS1000, PROTOCOL_IEEE488_2, {2, 1000000000}, {50, 1}, {2, 1000}, 2, true, 12},
176 {RIGOL, "DS2072", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {500, 1}, {500, 1000000}, 2, false, 14},
177 {RIGOL, "DS2102", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {500, 1}, {500, 1000000}, 2, false, 14},
178 {RIGOL, "DS2202", RIGOL_DS2000, PROTOCOL_IEEE488_2, {2, 1000000000}, {500, 1}, {500, 1000000}, 2, false, 14},
179 {RIGOL, "DS2302", RIGOL_DS2000, PROTOCOL_IEEE488_2, {1, 1000000000}, {1000, 1}, {500, 1000000}, 2, false, 14},
180 {RIGOL, "DS2072A", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {1000, 1}, {500, 1000000}, 2, false, 14},
181 {RIGOL, "DS2102A", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {1000, 1}, {500, 1000000}, 2, false, 14},
182 {RIGOL, "DS2202A", RIGOL_DS2000, PROTOCOL_IEEE488_2, {2, 1000000000}, {1000, 1}, {500, 1000000}, 2, false, 14},
183 {RIGOL, "DS2302A", RIGOL_DS2000, PROTOCOL_IEEE488_2, {1, 1000000000}, {1000, 1}, {500, 1000000}, 2, false, 14},
184 {RIGOL, "VS5022", RIGOL_VS5000, PROTOCOL_LEGACY, {20, 1000000000}, {50, 1}, {2, 1000}, 2, false, 14},
185 {RIGOL, "VS5022D", RIGOL_VS5000, PROTOCOL_LEGACY, {20, 1000000000}, {50, 1}, {2, 1000}, 2, true, 14},
186 {RIGOL, "VS5042", RIGOL_VS5000, PROTOCOL_LEGACY, {10, 1000000000}, {50, 1}, {2, 1000}, 2, false, 14},
187 {RIGOL, "VS5042D", RIGOL_VS5000, PROTOCOL_LEGACY, {10, 1000000000}, {50, 1}, {2, 1000}, 2, true, 14},
188 {RIGOL, "VS5062", RIGOL_VS5000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, 2, false, 14},
189 {RIGOL, "VS5062D", RIGOL_VS5000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, 2, true, 14},
190 {RIGOL, "VS5102", RIGOL_VS5000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, 2, false, 14},
191 {RIGOL, "VS5102D", RIGOL_VS5000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, 2, true, 14},
192 {RIGOL, "VS5202", RIGOL_VS5000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, 2, false, 14},
193 {RIGOL, "VS5202D", RIGOL_VS5000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, 2, true, 14},
194 {AGILENT, "DSO1002A", AGILENT_DSO1000, PROTOCOL_IEEE488_2, {5, 1000000000}, {50, 1}, {2, 1000}, 2, false, 12},
195 {AGILENT, "DSO1004A", AGILENT_DSO1000, PROTOCOL_IEEE488_2, {5, 1000000000}, {50, 1}, {2, 1000}, 4, false, 12},
196 {AGILENT, "DSO1012A", AGILENT_DSO1000, PROTOCOL_IEEE488_2, {2, 1000000000}, {50, 1}, {2, 1000}, 2, false, 12},
197 {AGILENT, "DSO1014A", AGILENT_DSO1000, PROTOCOL_IEEE488_2, {2, 1000000000}, {50, 1}, {2, 1000}, 4, false, 12},
198 {AGILENT, "DSO1022A", AGILENT_DSO1000, PROTOCOL_IEEE488_2, {2, 1000000000}, {50, 1}, {2, 1000}, 2, false, 12},
199 {AGILENT, "DSO1024A", AGILENT_DSO1000, PROTOCOL_IEEE488_2, {2, 1000000000}, {50, 1}, {2, 1000}, 4, false, 12},
202 SR_PRIV struct sr_dev_driver rigol_ds_driver_info;
203 static struct sr_dev_driver *di = &rigol_ds_driver_info;
205 static void clear_helper(void *priv)
207 struct dev_context *devc;
211 g_free(devc->buffer);
212 g_free(devc->coupling[0]);
213 g_free(devc->coupling[1]);
214 g_free(devc->trigger_source);
215 g_free(devc->trigger_slope);
216 g_slist_free(devc->analog_groups[0].probes);
217 g_slist_free(devc->analog_groups[1].probes);
218 g_slist_free(devc->digital_group.probes);
221 static int dev_clear(void)
223 return std_dev_clear(di, clear_helper);
226 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
228 struct dev_context *devc = sdi->priv;
232 va_start(args, format);
233 ret = sr_scpi_send_variadic(sdi->conn, format, args);
239 if (devc->model->series == RIGOL_DS1000) {
240 /* The DS1000 series needs this stupid delay, *OPC? doesn't work. */
241 sr_spew("delay %dms", 100);
245 return sr_scpi_get_opc(sdi->conn);
249 static int init(struct sr_context *sr_ctx)
251 return std_init(sr_ctx, di, LOG_PREFIX);
254 static int probe_port(const char *resource, const char *serialcomm, GSList **devices)
256 struct dev_context *devc;
257 struct sr_dev_inst *sdi;
258 struct sr_scpi_dev_inst *scpi;
259 struct sr_scpi_hw_info *hw_info;
260 struct sr_probe *probe;
263 const struct rigol_ds_model *model = NULL;
264 gchar *channel_name, *vendor, **version;
268 if (!(scpi = scpi_dev_inst_new(resource, serialcomm)))
271 if (sr_scpi_open(scpi) != SR_OK) {
272 sr_info("Couldn't open SCPI device.");
277 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
278 sr_info("Couldn't get IDN response.");
284 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
285 if (!strcasecmp(hw_info->manufacturer, supported_models[i].vendor) &&
286 !strcmp(hw_info->model, supported_models[i].name)) {
287 model = &supported_models[i];
292 if (!strcmp(hw_info->manufacturer, RIGOL))
293 vendor = RIGOL_SHORT;
294 else if (!strcmp(hw_info->manufacturer, AGILENT))
295 vendor = AGILENT_SHORT;
297 vendor = hw_info->manufacturer;
298 if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
299 vendor, hw_info->model,
300 hw_info->firmware_version))) {
301 sr_scpi_hw_info_free(hw_info);
312 sdi->inst_type = SR_INST_SCPI;
314 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
315 return SR_ERR_MALLOC;
317 devc->limit_frames = 0;
319 devc->protocol = model->protocol;
321 /* DS1000 models with firmware before 0.2.4 used the old
322 * legacy protocol. */
323 if (model->series == RIGOL_DS1000) {
324 version = g_strsplit(hw_info->firmware_version, ".", 0);
326 if (!version[0] || !version[1] || !version[2])
328 if (version[0][0] == 0 || version[1][0] == 0 || version[2][0] == 0)
330 for (i = 0; i < 3; i++) {
331 if (sr_atol(version[i], &n[i]) != SR_OK)
336 if (n[0] != 0 || n[1] > 2)
338 if (n[1] == 2 && n[2] > 3)
340 sr_dbg("Found DS1000 firmware < 0.2.4, using old protocol.");
341 devc->protocol = PROTOCOL_LEGACY;
346 sr_scpi_hw_info_free(hw_info);
348 for (i = 0; i < model->analog_channels; i++) {
349 if (!(channel_name = g_strdup_printf("CH%d", i + 1)))
350 return SR_ERR_MALLOC;
351 probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name);
352 sdi->probes = g_slist_append(sdi->probes, probe);
353 devc->analog_groups[i].name = channel_name;
354 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
355 sdi->probe_groups = g_slist_append(sdi->probe_groups,
356 &devc->analog_groups[i]);
359 if (devc->model->has_digital) {
360 for (i = 0; i < 16; i++) {
361 if (!(channel_name = g_strdup_printf("D%d", i)))
362 return SR_ERR_MALLOC;
363 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
364 g_free(channel_name);
366 return SR_ERR_MALLOC;
367 sdi->probes = g_slist_append(sdi->probes, probe);
368 devc->digital_group.probes = g_slist_append(
369 devc->digital_group.probes, probe);
371 devc->digital_group.name = "LA";
372 sdi->probe_groups = g_slist_append(sdi->probe_groups,
373 &devc->digital_group);
376 for (i = 0; i < NUM_TIMEBASE; i++) {
377 if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
378 devc->timebases = &timebases[i];
379 if (!memcmp(&devc->model->max_timebase, &timebases[i], sizeof(uint64_t[2])))
380 devc->num_timebases = &timebases[i] - devc->timebases + 1;
383 for (i = 0; i < NUM_VDIV; i++)
384 if (!memcmp(&devc->model->min_vdiv, &vdivs[i], sizeof(uint64_t[2])))
385 devc->vdivs = &vdivs[i];
387 if (!(devc->buffer = g_try_malloc(ACQ_BUFFER_SIZE)))
388 return SR_ERR_MALLOC;
389 if (!(devc->data = g_try_malloc(ACQ_BUFFER_SIZE * sizeof(float))))
390 return SR_ERR_MALLOC;
392 devc->data_source = DATA_SOURCE_LIVE;
396 *devices = g_slist_append(NULL, sdi);
401 static GSList *scan(GSList *options)
403 struct drv_context *drvc;
404 struct sr_config *src;
408 const gchar *dev_name;
410 gchar *serialcomm = NULL;
414 for (l = options; l; l = l->next) {
418 port = (char *)g_variant_get_string(src->data, NULL);
420 case SR_CONF_SERIALCOMM:
421 serialcomm = (char *)g_variant_get_string(src->data, NULL);
428 if (probe_port(port, serialcomm, &devices) == SR_ERR_MALLOC) {
435 if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
436 if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
438 while ((dev_name = g_dir_read_name(dir))) {
439 if (strncmp(dev_name, "usbtmc", 6))
441 port = g_strconcat("/dev/", dev_name, NULL);
442 ret = probe_port(port, serialcomm, &devices);
446 if (ret == SR_ERR_MALLOC) {
454 /* Tack a copy of the newly found devices onto the driver list. */
455 l = g_slist_copy(devices);
456 drvc->instances = g_slist_concat(drvc->instances, l);
461 static GSList *dev_list(void)
463 return ((struct drv_context *)(di->priv))->instances;
466 static int dev_open(struct sr_dev_inst *sdi)
468 struct sr_scpi_dev_inst *scpi = sdi->conn;
470 if (sr_scpi_open(scpi) < 0)
473 if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
476 sdi->status = SR_ST_ACTIVE;
481 static int dev_close(struct sr_dev_inst *sdi)
483 struct sr_scpi_dev_inst *scpi;
488 if (sr_scpi_close(scpi) < 0)
490 sdi->status = SR_ST_INACTIVE;
496 static int cleanup(void)
501 static int analog_frame_size(const struct sr_dev_inst *sdi)
503 struct dev_context *devc = sdi->priv;
504 struct sr_probe *probe;
505 int analog_probes = 0;
508 switch (devc->model->series) {
510 return VS5000_ANALOG_LIVE_WAVEFORM_SIZE;
512 return DS1000_ANALOG_LIVE_WAVEFORM_SIZE;
514 for (l = sdi->probes; l; l = l->next) {
516 if (probe->type == SR_PROBE_ANALOG && probe->enabled)
519 if (devc->data_source == DATA_SOURCE_MEMORY) {
520 if (analog_probes == 1)
521 return DS2000_ANALOG_MEM_WAVEFORM_SIZE_1C;
523 return DS2000_ANALOG_MEM_WAVEFORM_SIZE_2C;
525 if (devc->model->series == AGILENT_DSO1000)
526 return DSO1000_ANALOG_LIVE_WAVEFORM_SIZE;
528 return DS2000_ANALOG_LIVE_WAVEFORM_SIZE;
533 static int digital_frame_size(const struct sr_dev_inst *sdi)
535 struct dev_context *devc = sdi->priv;
537 switch (devc->model->series) {
539 return VS5000_DIGITAL_WAVEFORM_SIZE;
541 return DS1000_DIGITAL_WAVEFORM_SIZE;
547 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
548 const struct sr_probe_group *probe_group)
550 struct dev_context *devc;
551 struct sr_probe *probe;
554 int analog_channel = -1;
555 float smallest_diff = 0.0000000001;
559 if (!sdi || !(devc = sdi->priv))
562 /* If a probe group is specified, it must be a valid one. */
563 if (probe_group && !g_slist_find(sdi->probe_groups, probe_group)) {
564 sr_err("Invalid probe group specified.");
569 probe = g_slist_nth_data(probe_group->probes, 0);
572 if (probe->type == SR_PROBE_ANALOG) {
573 if (probe->name[2] < '1' || probe->name[2] > '4')
575 analog_channel = probe->name[2] - '1';
580 case SR_CONF_NUM_TIMEBASE:
581 *data = g_variant_new_int32(devc->model->num_horizontal_divs);
583 case SR_CONF_NUM_VDIV:
584 *data = g_variant_new_int32(NUM_VDIV);
585 case SR_CONF_DATA_SOURCE:
586 if (devc->data_source == DATA_SOURCE_LIVE)
587 *data = g_variant_new_string("Live");
588 else if (devc->data_source == DATA_SOURCE_MEMORY)
589 *data = g_variant_new_string("Memory");
591 *data = g_variant_new_string("Segmented");
593 case SR_CONF_SAMPLERATE:
594 if (devc->data_source == DATA_SOURCE_LIVE) {
595 samplerate = analog_frame_size(sdi) /
596 (devc->timebase * devc->model->num_horizontal_divs);
597 *data = g_variant_new_uint64(samplerate);
602 case SR_CONF_TRIGGER_SOURCE:
603 if (!strcmp(devc->trigger_source, "ACL"))
605 else if (!strcmp(devc->trigger_source, "CHAN1"))
607 else if (!strcmp(devc->trigger_source, "CHAN2"))
609 else if (!strcmp(devc->trigger_source, "CHAN3"))
611 else if (!strcmp(devc->trigger_source, "CHAN4"))
614 tmp_str = devc->trigger_source;
615 *data = g_variant_new_string(tmp_str);
617 case SR_CONF_TIMEBASE:
618 for (i = 0; i < devc->num_timebases; i++) {
619 float tb = (float)devc->timebases[i][0] / devc->timebases[i][1];
620 float diff = fabs(devc->timebase - tb);
621 if (diff < smallest_diff) {
622 smallest_diff = diff;
628 *data = g_variant_new("(tt)", devc->timebases[idx][0],
629 devc->timebases[idx][1]);
632 if (analog_channel < 0)
634 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
635 float vdiv = (float)vdivs[i][0] / vdivs[i][1];
636 float diff = fabs(devc->vdiv[analog_channel] - vdiv);
637 if (diff < smallest_diff) {
638 smallest_diff = diff;
644 *data = g_variant_new("(tt)", vdivs[idx][0], vdivs[idx][1]);
646 case SR_CONF_COUPLING:
647 if (analog_channel < 0)
649 *data = g_variant_new_string(devc->coupling[analog_channel]);
658 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
659 const struct sr_probe_group *probe_group)
661 struct dev_context *devc;
662 uint64_t tmp_u64, p, q;
669 if (!(devc = sdi->priv))
672 if (sdi->status != SR_ST_ACTIVE)
673 return SR_ERR_DEV_CLOSED;
675 /* If a probe group is specified, it must be a valid one. */
676 if (probe_group && !g_slist_find(sdi->probe_groups, probe_group)) {
677 sr_err("Invalid probe group specified.");
683 case SR_CONF_LIMIT_FRAMES:
684 devc->limit_frames = g_variant_get_uint64(data);
686 case SR_CONF_TRIGGER_SLOPE:
687 tmp_u64 = g_variant_get_uint64(data);
688 if (tmp_u64 != 0 && tmp_u64 != 1)
690 g_free(devc->trigger_slope);
691 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
692 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
694 case SR_CONF_HORIZ_TRIGGERPOS:
695 t_dbl = g_variant_get_double(data);
696 if (t_dbl < 0.0 || t_dbl > 1.0)
698 devc->horiz_triggerpos = t_dbl;
699 /* We have the trigger offset as a percentage of the frame, but
700 * need to express this in seconds. */
701 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
702 g_ascii_formatd(buffer, sizeof(buffer), "%.6f", t_dbl);
703 ret = set_cfg(sdi, ":TIM:OFFS %s", buffer);
705 case SR_CONF_TIMEBASE:
706 g_variant_get(data, "(tt)", &p, &q);
707 for (i = 0; i < devc->num_timebases; i++) {
708 if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
709 devc->timebase = (float)p / q;
710 g_ascii_formatd(buffer, sizeof(buffer), "%.9f",
712 ret = set_cfg(sdi, ":TIM:SCAL %s", buffer);
716 if (i == devc->num_timebases)
719 case SR_CONF_TRIGGER_SOURCE:
720 tmp_str = g_variant_get_string(data, NULL);
721 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
722 if (!strcmp(trigger_sources[i], tmp_str)) {
723 g_free(devc->trigger_source);
724 devc->trigger_source = g_strdup(trigger_sources[i]);
725 if (!strcmp(devc->trigger_source, "AC Line"))
727 else if (!strcmp(devc->trigger_source, "CH1"))
729 else if (!strcmp(devc->trigger_source, "CH2"))
731 else if (!strcmp(devc->trigger_source, "CH3"))
733 else if (!strcmp(devc->trigger_source, "CH4"))
736 tmp_str = (char *)devc->trigger_source;
737 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
741 if (i == ARRAY_SIZE(trigger_sources))
746 sr_err("No probe group specified.");
747 return SR_ERR_PROBE_GROUP;
749 g_variant_get(data, "(tt)", &p, &q);
750 for (i = 0; i < 2; i++) {
751 if (probe_group == &devc->analog_groups[i]) {
752 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
753 if (vdivs[j][0] != p || vdivs[j][1] != q)
755 devc->vdiv[i] = (float)p / q;
756 g_ascii_formatd(buffer, sizeof(buffer), "%.3f",
758 return set_cfg(sdi, ":CHAN%d:SCAL %s", i + 1,
765 case SR_CONF_COUPLING:
767 sr_err("No probe group specified.");
768 return SR_ERR_PROBE_GROUP;
770 tmp_str = g_variant_get_string(data, NULL);
771 for (i = 0; i < 2; i++) {
772 if (probe_group == &devc->analog_groups[i]) {
773 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
774 if (!strcmp(tmp_str, coupling[j])) {
775 g_free(devc->coupling[i]);
776 devc->coupling[i] = g_strdup(coupling[j]);
777 return set_cfg(sdi, ":CHAN%d:COUP %s", i + 1,
785 case SR_CONF_DATA_SOURCE:
786 tmp_str = g_variant_get_string(data, NULL);
787 if (!strcmp(tmp_str, "Live"))
788 devc->data_source = DATA_SOURCE_LIVE;
789 else if (!strcmp(tmp_str, "Memory"))
790 devc->data_source = DATA_SOURCE_MEMORY;
791 else if (devc->model->series >= RIGOL_DS1000Z
792 && !strcmp(tmp_str, "Segmented"))
793 devc->data_source = DATA_SOURCE_SEGMENTED;
805 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
806 const struct sr_probe_group *probe_group)
808 GVariant *tuple, *rational[2];
811 struct dev_context *devc = NULL;
816 if (key == SR_CONF_SCAN_OPTIONS) {
817 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
818 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
820 } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) {
821 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
822 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
826 /* Every other option requires a valid device instance. */
827 if (!sdi || !(devc = sdi->priv))
830 /* If a probe group is specified, it must be a valid one. */
832 if (probe_group != &devc->analog_groups[0]
833 && probe_group != &devc->analog_groups[1]) {
834 sr_err("Invalid probe group specified.");
840 case SR_CONF_DEVICE_OPTIONS:
842 sr_err("No probe group specified.");
843 return SR_ERR_PROBE_GROUP;
845 if (probe_group == &devc->digital_group) {
846 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
847 NULL, 0, sizeof(int32_t));
850 for (i = 0; i < 2; i++) {
851 if (probe_group == &devc->analog_groups[i]) {
852 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
853 analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t));
860 case SR_CONF_COUPLING:
862 sr_err("No probe group specified.");
863 return SR_ERR_PROBE_GROUP;
865 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
869 /* Can't know this until we have the exact model. */
872 sr_err("No probe group specified.");
873 return SR_ERR_PROBE_GROUP;
875 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
876 for (i = 0; i < NUM_VDIV; i++) {
877 rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
878 rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
879 tuple = g_variant_new_tuple(rational, 2);
880 g_variant_builder_add_value(&gvb, tuple);
882 *data = g_variant_builder_end(&gvb);
884 case SR_CONF_TIMEBASE:
886 /* Can't know this until we have the exact model. */
888 if (devc->num_timebases <= 0)
890 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
891 for (i = 0; i < devc->num_timebases; i++) {
892 rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
893 rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
894 tuple = g_variant_new_tuple(rational, 2);
895 g_variant_builder_add_value(&gvb, tuple);
897 *data = g_variant_builder_end(&gvb);
899 case SR_CONF_TRIGGER_SOURCE:
901 /* Can't know this until we have the exact model. */
903 *data = g_variant_new_strv(trigger_sources,
904 devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
906 case SR_CONF_DATA_SOURCE:
908 /* Can't know this until we have the exact model. */
910 /* This needs tweaking by series/model! */
911 if (devc->model->series == RIGOL_DS2000)
912 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
914 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
923 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
925 struct sr_scpi_dev_inst *scpi;
926 struct dev_context *devc;
927 struct sr_probe *probe;
928 struct sr_datafeed_packet packet;
931 if (sdi->status != SR_ST_ACTIVE)
932 return SR_ERR_DEV_CLOSED;
937 devc->num_frames = 0;
939 for (l = sdi->probes; l; l = l->next) {
941 sr_dbg("handling probe %s", probe->name);
942 if (probe->type == SR_PROBE_ANALOG) {
944 devc->enabled_analog_probes = g_slist_append(
945 devc->enabled_analog_probes, probe);
946 if (probe->enabled != devc->analog_channels[probe->index]) {
947 /* Enabled channel is currently disabled, or vice versa. */
948 if (set_cfg(sdi, ":CHAN%d:DISP %s", probe->index + 1,
949 probe->enabled ? "ON" : "OFF") != SR_OK)
951 devc->analog_channels[probe->index] = probe->enabled;
953 } else if (probe->type == SR_PROBE_LOGIC) {
954 if (probe->enabled) {
955 devc->enabled_digital_probes = g_slist_append(
956 devc->enabled_digital_probes, probe);
957 /* Turn on LA module if currently off. */
958 if (!devc->la_enabled) {
959 if (set_cfg(sdi, ":LA:DISP ON") != SR_OK)
961 devc->la_enabled = TRUE;
964 if (probe->enabled != devc->digital_channels[probe->index]) {
965 /* Enabled channel is currently disabled, or vice versa. */
966 if (set_cfg(sdi, ":DIG%d:TURN %s", probe->index,
967 probe->enabled ? "ON" : "OFF") != SR_OK)
969 devc->digital_channels[probe->index] = probe->enabled;
974 if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
977 /* Turn off LA module if on and no digital probes selected. */
978 if (devc->la_enabled && !devc->enabled_digital_probes)
979 if (set_cfg(sdi, ":LA:DISP OFF") != SR_OK)
982 if (devc->data_source == DATA_SOURCE_LIVE) {
983 if (set_cfg(sdi, ":RUN") != SR_OK)
985 } else if (devc->data_source == DATA_SOURCE_MEMORY) {
986 if (devc->model->series != RIGOL_DS2000) {
987 sr_err("Data source 'Memory' not supported for this device");
990 } else if (devc->data_source == DATA_SOURCE_SEGMENTED) {
991 sr_err("Data source 'Segmented' not yet supported");
995 sr_scpi_source_add(scpi, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
997 /* Send header packet to the session bus. */
998 std_session_send_df_header(cb_data, LOG_PREFIX);
1000 if (devc->enabled_analog_probes)
1001 devc->channel_entry = devc->enabled_analog_probes;
1003 devc->channel_entry = devc->enabled_digital_probes;
1005 devc->analog_frame_size = analog_frame_size(sdi);
1006 devc->digital_frame_size = digital_frame_size(sdi);
1008 if (devc->model->series < RIGOL_DS1000Z) {
1009 /* Fetch the first frame. */
1010 if (rigol_ds_channel_start(sdi) != SR_OK)
1013 if (devc->enabled_analog_probes) {
1014 if (devc->data_source == DATA_SOURCE_MEMORY) {
1015 /* Apparently for the DS2000 the memory
1016 * depth can only be set in Running state -
1017 * this matches the behaviour of the UI. */
1018 if (set_cfg(sdi, ":RUN") != SR_OK)
1020 if (set_cfg(sdi, "ACQ:MDEP %d", devc->analog_frame_size) != SR_OK)
1022 if (set_cfg(sdi, ":STOP") != SR_OK)
1025 if (rigol_ds_capture_start(sdi) != SR_OK)
1030 /* Start of first frame. */
1031 packet.type = SR_DF_FRAME_BEGIN;
1032 sr_session_send(cb_data, &packet);
1037 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
1039 struct dev_context *devc;
1040 struct sr_scpi_dev_inst *scpi;
1041 struct sr_datafeed_packet packet;
1047 if (sdi->status != SR_ST_ACTIVE) {
1048 sr_err("Device inactive, can't stop acquisition.");
1052 /* End of last frame. */
1053 packet.type = SR_DF_END;
1054 sr_session_send(sdi, &packet);
1056 g_slist_free(devc->enabled_analog_probes);
1057 g_slist_free(devc->enabled_digital_probes);
1058 devc->enabled_analog_probes = NULL;
1059 devc->enabled_digital_probes = NULL;
1061 sr_scpi_source_remove(scpi);
1066 SR_PRIV struct sr_dev_driver rigol_ds_driver_info = {
1068 .longname = "Rigol DS",
1073 .dev_list = dev_list,
1074 .dev_clear = dev_clear,
1075 .config_get = config_get,
1076 .config_set = config_set,
1077 .config_list = config_list,
1078 .dev_open = dev_open,
1079 .dev_close = dev_close,
1080 .dev_acquisition_start = dev_acquisition_start,
1081 .dev_acquisition_stop = dev_acquisition_stop,