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, ...)
231 va_start(args, format);
232 ret = sr_scpi_send_variadic(sdi->conn, format, args);
238 return sr_scpi_get_opc(sdi->conn);
241 static int init(struct sr_context *sr_ctx)
243 return std_init(sr_ctx, di, LOG_PREFIX);
246 static int probe_port(const char *resource, const char *serialcomm, GSList **devices)
248 struct dev_context *devc;
249 struct sr_dev_inst *sdi;
250 struct sr_scpi_dev_inst *scpi;
251 struct sr_scpi_hw_info *hw_info;
252 struct sr_probe *probe;
254 const struct rigol_ds_model *model = NULL;
255 gchar *channel_name, *vendor;
259 if (!(scpi = scpi_dev_inst_new(resource, serialcomm)))
262 if (sr_scpi_open(scpi) != SR_OK) {
263 sr_info("Couldn't open SCPI device.");
268 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
269 sr_info("Couldn't get IDN response.");
275 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
276 if (!strcasecmp(hw_info->manufacturer, supported_models[i].vendor) &&
277 !strcmp(hw_info->model, supported_models[i].name)) {
278 model = &supported_models[i];
283 if (!strcmp(hw_info->manufacturer, RIGOL))
284 vendor = RIGOL_SHORT;
285 else if (!strcmp(hw_info->manufacturer, AGILENT))
286 vendor = AGILENT_SHORT;
288 vendor = hw_info->manufacturer;
289 if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
290 vendor, hw_info->model,
291 hw_info->firmware_version))) {
292 sr_scpi_hw_info_free(hw_info);
298 sr_scpi_hw_info_free(hw_info);
304 sdi->inst_type = SR_INST_SCPI;
306 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
307 return SR_ERR_MALLOC;
309 devc->limit_frames = 0;
312 for (i = 0; i < model->analog_channels; i++) {
313 if (!(channel_name = g_strdup_printf("CH%d", i + 1)))
314 return SR_ERR_MALLOC;
315 probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name);
316 sdi->probes = g_slist_append(sdi->probes, probe);
317 devc->analog_groups[i].name = channel_name;
318 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
319 sdi->probe_groups = g_slist_append(sdi->probe_groups,
320 &devc->analog_groups[i]);
323 if (devc->model->has_digital) {
324 for (i = 0; i < 16; i++) {
325 if (!(channel_name = g_strdup_printf("D%d", i)))
326 return SR_ERR_MALLOC;
327 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
328 g_free(channel_name);
330 return SR_ERR_MALLOC;
331 sdi->probes = g_slist_append(sdi->probes, probe);
332 devc->digital_group.probes = g_slist_append(
333 devc->digital_group.probes, probe);
335 devc->digital_group.name = "LA";
336 sdi->probe_groups = g_slist_append(sdi->probe_groups,
337 &devc->digital_group);
340 for (i = 0; i < NUM_TIMEBASE; i++) {
341 if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
342 devc->timebases = &timebases[i];
343 if (!memcmp(&devc->model->max_timebase, &timebases[i], sizeof(uint64_t[2])))
344 devc->num_timebases = &timebases[i] - devc->timebases + 1;
347 for (i = 0; i < NUM_VDIV; i++)
348 if (!memcmp(&devc->model->min_vdiv, &vdivs[i], sizeof(uint64_t[2])))
349 devc->vdivs = &vdivs[i];
351 if (!(devc->buffer = g_try_malloc(ACQ_BUFFER_SIZE)))
352 return SR_ERR_MALLOC;
353 if (!(devc->data = g_try_malloc(ACQ_BUFFER_SIZE * sizeof(float))))
354 return SR_ERR_MALLOC;
356 devc->data_source = DATA_SOURCE_LIVE;
360 *devices = g_slist_append(NULL, sdi);
365 static GSList *scan(GSList *options)
367 struct drv_context *drvc;
368 struct sr_config *src;
372 const gchar *dev_name;
374 gchar *serialcomm = NULL;
378 for (l = options; l; l = l->next) {
382 port = (char *)g_variant_get_string(src->data, NULL);
384 case SR_CONF_SERIALCOMM:
385 serialcomm = (char *)g_variant_get_string(src->data, NULL);
392 if (probe_port(port, serialcomm, &devices) == SR_ERR_MALLOC) {
399 if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
400 if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
402 while ((dev_name = g_dir_read_name(dir))) {
403 if (strncmp(dev_name, "usbtmc", 6))
405 port = g_strconcat("/dev/", dev_name, NULL);
406 ret = probe_port(port, serialcomm, &devices);
410 if (ret == SR_ERR_MALLOC) {
418 /* Tack a copy of the newly found devices onto the driver list. */
419 l = g_slist_copy(devices);
420 drvc->instances = g_slist_concat(drvc->instances, l);
425 static GSList *dev_list(void)
427 return ((struct drv_context *)(di->priv))->instances;
430 static int dev_open(struct sr_dev_inst *sdi)
432 struct sr_scpi_dev_inst *scpi = sdi->conn;
434 if (sr_scpi_open(scpi) < 0)
437 if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
440 sdi->status = SR_ST_ACTIVE;
445 static int dev_close(struct sr_dev_inst *sdi)
447 struct sr_scpi_dev_inst *scpi;
452 if (sr_scpi_close(scpi) < 0)
454 sdi->status = SR_ST_INACTIVE;
460 static int cleanup(void)
465 static int analog_frame_size(const struct sr_dev_inst *sdi)
467 struct dev_context *devc = sdi->priv;
468 struct sr_probe *probe;
469 int analog_probes = 0;
472 switch (devc->model->series) {
474 return VS5000_ANALOG_LIVE_WAVEFORM_SIZE;
476 return DS1000_ANALOG_LIVE_WAVEFORM_SIZE;
478 for (l = sdi->probes; l; l = l->next) {
480 if (probe->type == SR_PROBE_ANALOG && probe->enabled)
483 if (devc->data_source == DATA_SOURCE_MEMORY) {
484 if (analog_probes == 1)
485 return DS2000_ANALOG_MEM_WAVEFORM_SIZE_1C;
487 return DS2000_ANALOG_MEM_WAVEFORM_SIZE_2C;
489 if (devc->model->series == AGILENT_DSO1000)
490 return DSO1000_ANALOG_LIVE_WAVEFORM_SIZE;
492 return DS2000_ANALOG_LIVE_WAVEFORM_SIZE;
497 static int digital_frame_size(const struct sr_dev_inst *sdi)
499 struct dev_context *devc = sdi->priv;
501 switch (devc->model->series) {
503 return VS5000_DIGITAL_WAVEFORM_SIZE;
505 return DS1000_DIGITAL_WAVEFORM_SIZE;
511 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
512 const struct sr_probe_group *probe_group)
514 struct dev_context *devc;
515 struct sr_probe *probe;
518 int analog_channel = -1;
519 float smallest_diff = 0.0000000001;
523 if (!sdi || !(devc = sdi->priv))
526 /* If a probe group is specified, it must be a valid one. */
527 if (probe_group && !g_slist_find(sdi->probe_groups, probe_group)) {
528 sr_err("Invalid probe group specified.");
533 probe = g_slist_nth_data(probe_group->probes, 0);
536 if (probe->type == SR_PROBE_ANALOG) {
537 if (probe->name[2] < '1' || probe->name[2] > '4')
539 analog_channel = probe->name[2] - '1';
544 case SR_CONF_NUM_TIMEBASE:
545 *data = g_variant_new_int32(devc->model->num_horizontal_divs);
547 case SR_CONF_NUM_VDIV:
548 *data = g_variant_new_int32(NUM_VDIV);
549 case SR_CONF_DATA_SOURCE:
550 if (devc->data_source == DATA_SOURCE_LIVE)
551 *data = g_variant_new_string("Live");
552 else if (devc->data_source == DATA_SOURCE_MEMORY)
553 *data = g_variant_new_string("Memory");
555 *data = g_variant_new_string("Segmented");
557 case SR_CONF_SAMPLERATE:
558 if (devc->data_source == DATA_SOURCE_LIVE) {
559 samplerate = analog_frame_size(sdi) /
560 (devc->timebase * devc->model->num_horizontal_divs);
561 *data = g_variant_new_uint64(samplerate);
566 case SR_CONF_TRIGGER_SOURCE:
567 if (!strcmp(devc->trigger_source, "ACL"))
569 else if (!strcmp(devc->trigger_source, "CHAN1"))
571 else if (!strcmp(devc->trigger_source, "CHAN2"))
573 else if (!strcmp(devc->trigger_source, "CHAN3"))
575 else if (!strcmp(devc->trigger_source, "CHAN4"))
578 tmp_str = devc->trigger_source;
579 *data = g_variant_new_string(tmp_str);
581 case SR_CONF_TIMEBASE:
582 for (i = 0; i < devc->num_timebases; i++) {
583 float tb = (float)devc->timebases[i][0] / devc->timebases[i][1];
584 float diff = fabs(devc->timebase - tb);
585 if (diff < smallest_diff) {
586 smallest_diff = diff;
592 *data = g_variant_new("(tt)", devc->timebases[idx][0],
593 devc->timebases[idx][1]);
596 if (analog_channel < 0)
598 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
599 float vdiv = (float)vdivs[i][0] / vdivs[i][1];
600 float diff = fabs(devc->vdiv[analog_channel] - vdiv);
601 if (diff < smallest_diff) {
602 smallest_diff = diff;
608 *data = g_variant_new("(tt)", vdivs[idx][0], vdivs[idx][1]);
610 case SR_CONF_COUPLING:
611 if (analog_channel < 0)
613 *data = g_variant_new_string(devc->coupling[analog_channel]);
622 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
623 const struct sr_probe_group *probe_group)
625 struct dev_context *devc;
626 uint64_t tmp_u64, p, q;
633 if (!(devc = sdi->priv))
636 if (sdi->status != SR_ST_ACTIVE)
637 return SR_ERR_DEV_CLOSED;
639 /* If a probe group is specified, it must be a valid one. */
640 if (probe_group && !g_slist_find(sdi->probe_groups, probe_group)) {
641 sr_err("Invalid probe group specified.");
647 case SR_CONF_LIMIT_FRAMES:
648 devc->limit_frames = g_variant_get_uint64(data);
650 case SR_CONF_TRIGGER_SLOPE:
651 tmp_u64 = g_variant_get_uint64(data);
652 if (tmp_u64 != 0 && tmp_u64 != 1)
654 g_free(devc->trigger_slope);
655 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
656 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
658 case SR_CONF_HORIZ_TRIGGERPOS:
659 t_dbl = g_variant_get_double(data);
660 if (t_dbl < 0.0 || t_dbl > 1.0)
662 devc->horiz_triggerpos = t_dbl;
663 /* We have the trigger offset as a percentage of the frame, but
664 * need to express this in seconds. */
665 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
666 g_ascii_formatd(buffer, sizeof(buffer), "%.6f", t_dbl);
667 ret = set_cfg(sdi, ":TIM:OFFS %s", buffer);
669 case SR_CONF_TIMEBASE:
670 g_variant_get(data, "(tt)", &p, &q);
671 for (i = 0; i < devc->num_timebases; i++) {
672 if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
673 devc->timebase = (float)p / q;
674 g_ascii_formatd(buffer, sizeof(buffer), "%.9f",
676 ret = set_cfg(sdi, ":TIM:SCAL %s", buffer);
680 if (i == devc->num_timebases)
683 case SR_CONF_TRIGGER_SOURCE:
684 tmp_str = g_variant_get_string(data, NULL);
685 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
686 if (!strcmp(trigger_sources[i], tmp_str)) {
687 g_free(devc->trigger_source);
688 devc->trigger_source = g_strdup(trigger_sources[i]);
689 if (!strcmp(devc->trigger_source, "AC Line"))
691 else if (!strcmp(devc->trigger_source, "CH1"))
693 else if (!strcmp(devc->trigger_source, "CH2"))
695 else if (!strcmp(devc->trigger_source, "CH3"))
697 else if (!strcmp(devc->trigger_source, "CH4"))
700 tmp_str = (char *)devc->trigger_source;
701 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
705 if (i == ARRAY_SIZE(trigger_sources))
710 sr_err("No probe group specified.");
711 return SR_ERR_PROBE_GROUP;
713 g_variant_get(data, "(tt)", &p, &q);
714 for (i = 0; i < 2; i++) {
715 if (probe_group == &devc->analog_groups[i]) {
716 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
717 if (vdivs[j][0] != p || vdivs[j][1] != q)
719 devc->vdiv[i] = (float)p / q;
720 g_ascii_formatd(buffer, sizeof(buffer), "%.3f",
722 return set_cfg(sdi, ":CHAN%d:SCAL %s", i + 1,
729 case SR_CONF_COUPLING:
731 sr_err("No probe group specified.");
732 return SR_ERR_PROBE_GROUP;
734 tmp_str = g_variant_get_string(data, NULL);
735 for (i = 0; i < 2; i++) {
736 if (probe_group == &devc->analog_groups[i]) {
737 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
738 if (!strcmp(tmp_str, coupling[j])) {
739 g_free(devc->coupling[i]);
740 devc->coupling[i] = g_strdup(coupling[j]);
741 return set_cfg(sdi, ":CHAN%d:COUP %s", i + 1,
749 case SR_CONF_DATA_SOURCE:
750 tmp_str = g_variant_get_string(data, NULL);
751 if (!strcmp(tmp_str, "Live"))
752 devc->data_source = DATA_SOURCE_LIVE;
753 else if (!strcmp(tmp_str, "Memory"))
754 devc->data_source = DATA_SOURCE_MEMORY;
755 else if (devc->model->series >= RIGOL_DS1000Z
756 && !strcmp(tmp_str, "Segmented"))
757 devc->data_source = DATA_SOURCE_SEGMENTED;
769 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
770 const struct sr_probe_group *probe_group)
772 GVariant *tuple, *rational[2];
775 struct dev_context *devc = NULL;
780 if (key == SR_CONF_SCAN_OPTIONS) {
781 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
782 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
784 } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) {
785 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
786 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
790 /* Every other option requires a valid device instance. */
791 if (!sdi || !(devc = sdi->priv))
794 /* If a probe group is specified, it must be a valid one. */
796 if (probe_group != &devc->analog_groups[0]
797 && probe_group != &devc->analog_groups[1]) {
798 sr_err("Invalid probe group specified.");
804 case SR_CONF_DEVICE_OPTIONS:
806 sr_err("No probe group specified.");
807 return SR_ERR_PROBE_GROUP;
809 if (probe_group == &devc->digital_group) {
810 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
811 NULL, 0, sizeof(int32_t));
814 for (i = 0; i < 2; i++) {
815 if (probe_group == &devc->analog_groups[i]) {
816 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
817 analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t));
824 case SR_CONF_COUPLING:
826 sr_err("No probe group specified.");
827 return SR_ERR_PROBE_GROUP;
829 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
833 /* Can't know this until we have the exact model. */
836 sr_err("No probe group specified.");
837 return SR_ERR_PROBE_GROUP;
839 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
840 for (i = 0; i < NUM_VDIV; i++) {
841 rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
842 rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
843 tuple = g_variant_new_tuple(rational, 2);
844 g_variant_builder_add_value(&gvb, tuple);
846 *data = g_variant_builder_end(&gvb);
848 case SR_CONF_TIMEBASE:
850 /* Can't know this until we have the exact model. */
852 if (devc->num_timebases <= 0)
854 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
855 for (i = 0; i < devc->num_timebases; i++) {
856 rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
857 rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
858 tuple = g_variant_new_tuple(rational, 2);
859 g_variant_builder_add_value(&gvb, tuple);
861 *data = g_variant_builder_end(&gvb);
863 case SR_CONF_TRIGGER_SOURCE:
865 /* Can't know this until we have the exact model. */
867 *data = g_variant_new_strv(trigger_sources,
868 devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
870 case SR_CONF_DATA_SOURCE:
872 /* Can't know this until we have the exact model. */
874 /* This needs tweaking by series/model! */
875 if (devc->model->series == RIGOL_DS2000)
876 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
878 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
887 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
889 struct sr_scpi_dev_inst *scpi;
890 struct dev_context *devc;
891 struct sr_probe *probe;
892 struct sr_datafeed_packet packet;
895 if (sdi->status != SR_ST_ACTIVE)
896 return SR_ERR_DEV_CLOSED;
901 devc->num_frames = 0;
903 for (l = sdi->probes; l; l = l->next) {
905 sr_dbg("handling probe %s", probe->name);
906 if (probe->type == SR_PROBE_ANALOG) {
908 devc->enabled_analog_probes = g_slist_append(
909 devc->enabled_analog_probes, probe);
910 if (probe->enabled != devc->analog_channels[probe->index]) {
911 /* Enabled channel is currently disabled, or vice versa. */
912 if (set_cfg(sdi, ":CHAN%d:DISP %s", probe->index + 1,
913 probe->enabled ? "ON" : "OFF") != SR_OK)
915 devc->analog_channels[probe->index] = probe->enabled;
917 } else if (probe->type == SR_PROBE_LOGIC) {
918 if (probe->enabled) {
919 devc->enabled_digital_probes = g_slist_append(
920 devc->enabled_digital_probes, probe);
921 /* Turn on LA module if currently off. */
922 if (!devc->la_enabled) {
923 if (set_cfg(sdi, ":LA:DISP ON") != SR_OK)
925 devc->la_enabled = TRUE;
928 if (probe->enabled != devc->digital_channels[probe->index]) {
929 /* Enabled channel is currently disabled, or vice versa. */
930 if (set_cfg(sdi, ":DIG%d:TURN %s", probe->index,
931 probe->enabled ? "ON" : "OFF") != SR_OK)
933 devc->digital_channels[probe->index] = probe->enabled;
938 if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
941 /* Turn off LA module if on and no digital probes selected. */
942 if (devc->la_enabled && !devc->enabled_digital_probes)
943 if (set_cfg(sdi, ":LA:DISP OFF") != SR_OK)
946 if (devc->data_source == DATA_SOURCE_LIVE) {
947 if (set_cfg(sdi, ":RUN") != SR_OK)
949 } else if (devc->data_source == DATA_SOURCE_MEMORY) {
950 if (devc->model->series != RIGOL_DS2000) {
951 sr_err("Data source 'Memory' not supported for this device");
954 } else if (devc->data_source == DATA_SOURCE_SEGMENTED) {
955 sr_err("Data source 'Segmented' not yet supported");
959 sr_scpi_source_add(scpi, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
961 /* Send header packet to the session bus. */
962 std_session_send_df_header(cb_data, LOG_PREFIX);
964 if (devc->enabled_analog_probes)
965 devc->channel_entry = devc->enabled_analog_probes;
967 devc->channel_entry = devc->enabled_digital_probes;
969 devc->analog_frame_size = analog_frame_size(sdi);
970 devc->digital_frame_size = digital_frame_size(sdi);
972 if (devc->model->series < RIGOL_DS1000Z) {
973 /* Fetch the first frame. */
974 if (rigol_ds_channel_start(sdi) != SR_OK)
977 if (devc->enabled_analog_probes) {
978 if (devc->data_source == DATA_SOURCE_MEMORY) {
979 /* Apparently for the DS2000 the memory
980 * depth can only be set in Running state -
981 * this matches the behaviour of the UI. */
982 if (set_cfg(sdi, ":RUN") != SR_OK)
984 if (set_cfg(sdi, "ACQ:MDEP %d", devc->analog_frame_size) != SR_OK)
986 if (set_cfg(sdi, ":STOP") != SR_OK)
989 if (rigol_ds_capture_start(sdi) != SR_OK)
994 /* Start of first frame. */
995 packet.type = SR_DF_FRAME_BEGIN;
996 sr_session_send(cb_data, &packet);
1001 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
1003 struct dev_context *devc;
1004 struct sr_scpi_dev_inst *scpi;
1005 struct sr_datafeed_packet packet;
1011 if (sdi->status != SR_ST_ACTIVE) {
1012 sr_err("Device inactive, can't stop acquisition.");
1016 /* End of last frame. */
1017 packet.type = SR_DF_END;
1018 sr_session_send(sdi, &packet);
1020 g_slist_free(devc->enabled_analog_probes);
1021 g_slist_free(devc->enabled_digital_probes);
1022 devc->enabled_analog_probes = NULL;
1023 devc->enabled_digital_probes = NULL;
1025 sr_scpi_source_remove(scpi);
1030 SR_PRIV struct sr_dev_driver rigol_ds_driver_info = {
1032 .longname = "Rigol DS",
1037 .dev_list = dev_list,
1038 .dev_clear = dev_clear,
1039 .config_get = config_get,
1040 .config_set = config_set,
1041 .config_list = config_list,
1042 .dev_open = dev_open,
1043 .dev_close = dev_close,
1044 .dev_acquisition_start = dev_acquisition_start,
1045 .dev_acquisition_stop = dev_acquisition_stop,