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/>.
30 #include <libsigrok/libsigrok.h>
31 #include "libsigrok-internal.h"
35 static const uint32_t scanopts[] = {
40 static const uint32_t drvopts[] = {
44 static const uint32_t devopts[] = {
45 SR_CONF_LIMIT_FRAMES | SR_CONF_SET,
46 SR_CONF_SAMPLERATE | SR_CONF_GET,
47 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
48 SR_CONF_NUM_HDIV | SR_CONF_GET,
49 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_SET,
50 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
51 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
52 SR_CONF_TRIGGER_LEVEL | SR_CONF_GET | SR_CONF_SET,
53 SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
56 static const uint32_t analog_devopts[] = {
57 SR_CONF_NUM_VDIV | SR_CONF_GET,
58 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
59 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
60 SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
63 static const uint64_t timebases[][2] = {
106 static const uint64_t vdivs[][2] = {
129 #define NUM_TIMEBASE ARRAY_SIZE(timebases)
130 #define NUM_VDIV ARRAY_SIZE(vdivs)
132 static const char *trigger_sources[] = {
157 static const char *trigger_slopes[] = {
162 static const char *coupling[] = {
168 static const uint64_t probe_factor[] = {
181 /* Do not change the order of entries */
182 static const char *data_sources[] = {
202 /* short name, full name */
203 static const struct rigol_ds_vendor supported_vendors[] = {
204 [RIGOL] = {"Rigol", "Rigol Technologies"},
205 [AGILENT] = {"Agilent", "Agilent Technologies"},
208 #define VENDOR(x) &supported_vendors[x]
209 /* vendor, series, protocol, max timebase, min vdiv, number of horizontal divs,
210 * live waveform samples, memory buffer samples */
211 static const struct rigol_ds_series supported_series[] = {
212 [VS5000] = {VENDOR(RIGOL), "VS5000", PROTOCOL_V1, FORMAT_RAW,
213 {50, 1}, {2, 1000}, 14, 2048, 0},
214 [DS1000] = {VENDOR(RIGOL), "DS1000", PROTOCOL_V2, FORMAT_IEEE488_2,
215 {50, 1}, {2, 1000}, 12, 600, 1048576},
216 [DS2000] = {VENDOR(RIGOL), "DS2000", PROTOCOL_V3, FORMAT_IEEE488_2,
217 {500, 1}, {500, 1000000}, 14, 1400, 14000},
218 [DS2000A] = {VENDOR(RIGOL), "DS2000A", PROTOCOL_V3, FORMAT_IEEE488_2,
219 {1000, 1}, {500, 1000000}, 14, 1400, 14000},
220 [DSO1000] = {VENDOR(AGILENT), "DSO1000", PROTOCOL_V3, FORMAT_IEEE488_2,
221 {50, 1}, {2, 1000}, 12, 600, 20480},
222 [DS1000Z] = {VENDOR(RIGOL), "DS1000Z", PROTOCOL_V4, FORMAT_IEEE488_2,
223 {50, 1}, {1, 1000}, 12, 1200, 12000000},
226 #define SERIES(x) &supported_series[x]
227 /* series, model, min timebase, analog channels, digital */
228 static const struct rigol_ds_model supported_models[] = {
229 {SERIES(VS5000), "VS5022", {20, 1000000000}, 2, false},
230 {SERIES(VS5000), "VS5042", {10, 1000000000}, 2, false},
231 {SERIES(VS5000), "VS5062", {5, 1000000000}, 2, false},
232 {SERIES(VS5000), "VS5102", {2, 1000000000}, 2, false},
233 {SERIES(VS5000), "VS5202", {2, 1000000000}, 2, false},
234 {SERIES(VS5000), "VS5022D", {20, 1000000000}, 2, true},
235 {SERIES(VS5000), "VS5042D", {10, 1000000000}, 2, true},
236 {SERIES(VS5000), "VS5062D", {5, 1000000000}, 2, true},
237 {SERIES(VS5000), "VS5102D", {2, 1000000000}, 2, true},
238 {SERIES(VS5000), "VS5202D", {2, 1000000000}, 2, true},
239 {SERIES(DS1000), "DS1052E", {5, 1000000000}, 2, false},
240 {SERIES(DS1000), "DS1102E", {2, 1000000000}, 2, false},
241 {SERIES(DS1000), "DS1152E", {2, 1000000000}, 2, false},
242 {SERIES(DS1000), "DS1052D", {5, 1000000000}, 2, true},
243 {SERIES(DS1000), "DS1102D", {2, 1000000000}, 2, true},
244 {SERIES(DS1000), "DS1152D", {2, 1000000000}, 2, true},
245 {SERIES(DS2000), "DS2072", {5, 1000000000}, 2, false},
246 {SERIES(DS2000), "DS2102", {5, 1000000000}, 2, false},
247 {SERIES(DS2000), "DS2202", {2, 1000000000}, 2, false},
248 {SERIES(DS2000), "DS2302", {1, 1000000000}, 2, false},
249 {SERIES(DS2000A), "DS2072A", {5, 1000000000}, 2, false},
250 {SERIES(DS2000A), "DS2102A", {5, 1000000000}, 2, false},
251 {SERIES(DS2000A), "DS2202A", {2, 1000000000}, 2, false},
252 {SERIES(DS2000A), "DS2302A", {1, 1000000000}, 2, false},
253 {SERIES(DS2000A), "MSO2072A", {5, 1000000000}, 2, true},
254 {SERIES(DS2000A), "MSO2102A", {5, 1000000000}, 2, true},
255 {SERIES(DS2000A), "MSO2202A", {2, 1000000000}, 2, true},
256 {SERIES(DS2000A), "MSO2302A", {1, 1000000000}, 2, true},
257 {SERIES(DSO1000), "DSO1002A", {5, 1000000000}, 2, false},
258 {SERIES(DSO1000), "DSO1004A", {5, 1000000000}, 4, false},
259 {SERIES(DSO1000), "DSO1012A", {2, 1000000000}, 2, false},
260 {SERIES(DSO1000), "DSO1014A", {2, 1000000000}, 4, false},
261 {SERIES(DSO1000), "DSO1022A", {2, 1000000000}, 2, false},
262 {SERIES(DSO1000), "DSO1024A", {2, 1000000000}, 4, false},
263 {SERIES(DS1000Z), "DS1054Z", {5, 1000000000}, 4, false},
264 {SERIES(DS1000Z), "DS1074Z", {5, 1000000000}, 4, false},
265 {SERIES(DS1000Z), "DS1104Z", {5, 1000000000}, 4, false},
266 {SERIES(DS1000Z), "DS1074Z-S", {5, 1000000000}, 4, false},
267 {SERIES(DS1000Z), "DS1104Z-S", {5, 1000000000}, 4, false},
268 {SERIES(DS1000Z), "DS1074Z Plus", {5, 1000000000}, 4, false},
269 {SERIES(DS1000Z), "DS1104Z Plus", {5, 1000000000}, 4, false},
270 {SERIES(DS1000Z), "MSO1074Z", {5, 1000000000}, 4, true},
271 {SERIES(DS1000Z), "MSO1104Z", {5, 1000000000}, 4, true},
272 {SERIES(DS1000Z), "MSO1074Z-S", {5, 1000000000}, 4, true},
273 {SERIES(DS1000Z), "MSO1104Z-S", {5, 1000000000}, 4, true},
276 static struct sr_dev_driver rigol_ds_driver_info;
278 static void clear_helper(void *priv)
280 struct dev_context *devc;
285 g_free(devc->buffer);
286 for (i = 0; i < ARRAY_SIZE(devc->coupling); i++)
287 g_free(devc->coupling[i]);
288 g_free(devc->trigger_source);
289 g_free(devc->trigger_slope);
290 g_free(devc->analog_groups);
294 static int dev_clear(const struct sr_dev_driver *di)
296 return std_dev_clear(di, clear_helper);
299 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
301 struct dev_context *devc;
302 struct sr_dev_inst *sdi;
303 struct sr_scpi_hw_info *hw_info;
304 struct sr_channel *ch;
307 const struct rigol_ds_model *model = NULL;
308 gchar *channel_name, **version;
310 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
311 sr_info("Couldn't get IDN response, retrying.");
314 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
315 sr_info("Couldn't get IDN response.");
320 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
321 if (!g_ascii_strcasecmp(hw_info->manufacturer,
322 supported_models[i].series->vendor->full_name) &&
323 !strcmp(hw_info->model, supported_models[i].name)) {
324 model = &supported_models[i];
330 sr_scpi_hw_info_free(hw_info);
334 sdi = g_malloc0(sizeof(struct sr_dev_inst));
335 sdi->vendor = g_strdup(model->series->vendor->name);
336 sdi->model = g_strdup(model->name);
337 sdi->version = g_strdup(hw_info->firmware_version);
339 sdi->driver = &rigol_ds_driver_info;
340 sdi->inst_type = SR_INST_SCPI;
341 sdi->serial_num = g_strdup(hw_info->serial_number);
342 devc = g_malloc0(sizeof(struct dev_context));
343 devc->limit_frames = 0;
345 devc->format = model->series->format;
347 /* DS1000 models with firmware before 0.2.4 used the old data format. */
348 if (model->series == SERIES(DS1000)) {
349 version = g_strsplit(hw_info->firmware_version, ".", 0);
351 if (!version[0] || !version[1] || !version[2])
353 if (version[0][0] == 0 || version[1][0] == 0 || version[2][0] == 0)
355 for (i = 0; i < 3; i++) {
356 if (sr_atol(version[i], &n[i]) != SR_OK)
361 scpi->firmware_version = n[0] * 100 + n[1] * 10 + n[2];
362 if (scpi->firmware_version < 24) {
363 sr_dbg("Found DS1000 firmware < 0.2.4, using raw data format.");
364 devc->format = FORMAT_RAW;
371 sr_scpi_hw_info_free(hw_info);
373 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
374 model->analog_channels);
376 for (i = 0; i < model->analog_channels; i++) {
377 channel_name = g_strdup_printf("CH%d", i + 1);
378 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_name);
380 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
382 devc->analog_groups[i]->name = channel_name;
383 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
384 sdi->channel_groups = g_slist_append(sdi->channel_groups,
385 devc->analog_groups[i]);
388 if (devc->model->has_digital) {
389 devc->digital_group = g_malloc0(sizeof(struct sr_channel_group));
391 for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) {
392 channel_name = g_strdup_printf("D%d", i);
393 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
394 g_free(channel_name);
395 devc->digital_group->channels = g_slist_append(
396 devc->digital_group->channels, ch);
398 devc->digital_group->name = g_strdup("LA");
399 sdi->channel_groups = g_slist_append(sdi->channel_groups,
400 devc->digital_group);
403 for (i = 0; i < NUM_TIMEBASE; i++) {
404 if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
405 devc->timebases = &timebases[i];
406 if (!memcmp(&devc->model->series->max_timebase, &timebases[i], sizeof(uint64_t[2])))
407 devc->num_timebases = &timebases[i] - devc->timebases + 1;
410 for (i = 0; i < NUM_VDIV; i++) {
411 if (!memcmp(&devc->model->series->min_vdiv,
412 &vdivs[i], sizeof(uint64_t[2]))) {
413 devc->vdivs = &vdivs[i];
414 devc->num_vdivs = NUM_VDIV - i;
418 devc->buffer = g_malloc(ACQ_BUFFER_SIZE);
419 devc->data = g_malloc(ACQ_BUFFER_SIZE * sizeof(float));
421 devc->data_source = DATA_SOURCE_LIVE;
428 static GSList *scan(struct sr_dev_driver *di, GSList *options)
430 return sr_scpi_scan(di->context, options, probe_device);
433 static int dev_open(struct sr_dev_inst *sdi)
436 struct sr_scpi_dev_inst *scpi = sdi->conn;
438 if ((ret = sr_scpi_open(scpi)) < 0) {
439 sr_err("Failed to open SCPI device: %s.", sr_strerror(ret));
443 if ((ret = rigol_ds_get_dev_cfg(sdi)) < 0) {
444 sr_err("Failed to get device config: %s.", sr_strerror(ret));
451 static int dev_close(struct sr_dev_inst *sdi)
453 struct sr_scpi_dev_inst *scpi;
454 struct dev_context *devc;
459 if (devc->model->series->protocol == PROTOCOL_V2)
460 rigol_ds_config_set(sdi, ":KEY:LOCK DISABLE");
463 if (sr_scpi_close(scpi) < 0)
465 sdi->status = SR_ST_INACTIVE;
471 static int analog_frame_size(const struct sr_dev_inst *sdi)
473 struct dev_context *devc = sdi->priv;
474 struct sr_channel *ch;
475 int analog_channels = 0;
478 for (l = sdi->channels; l; l = l->next) {
480 if (ch->type == SR_CHANNEL_ANALOG && ch->enabled)
484 if (analog_channels == 0)
487 switch (devc->data_source) {
488 case DATA_SOURCE_LIVE:
489 return devc->model->series->live_samples;
490 case DATA_SOURCE_MEMORY:
491 return devc->model->series->buffer_samples / analog_channels;
497 static int digital_frame_size(const struct sr_dev_inst *sdi)
499 struct dev_context *devc = sdi->priv;
501 switch (devc->data_source) {
502 case DATA_SOURCE_LIVE:
503 return devc->model->series->live_samples * 2;
504 case DATA_SOURCE_MEMORY:
505 return devc->model->series->buffer_samples * 2;
511 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
512 const struct sr_channel_group *cg)
514 struct dev_context *devc;
515 struct sr_channel *ch;
518 int analog_channel = -1;
519 float smallest_diff = INFINITY;
528 /* If a channel group is specified, it must be a valid one. */
529 if (cg && !g_slist_find(sdi->channel_groups, cg)) {
530 sr_err("Invalid channel group specified.");
535 ch = g_slist_nth_data(cg->channels, 0);
538 if (ch->type == SR_CHANNEL_ANALOG) {
539 if (ch->name[2] < '1' || ch->name[2] > '4')
541 analog_channel = ch->name[2] - '1';
546 case SR_CONF_NUM_HDIV:
547 *data = g_variant_new_int32(devc->model->series->num_horizontal_divs);
549 case SR_CONF_NUM_VDIV:
550 *data = g_variant_new_int32(devc->num_vdivs);
552 case SR_CONF_DATA_SOURCE:
553 if (devc->data_source == DATA_SOURCE_LIVE)
554 *data = g_variant_new_string("Live");
555 else if (devc->data_source == DATA_SOURCE_MEMORY)
556 *data = g_variant_new_string("Memory");
558 *data = g_variant_new_string("Segmented");
560 case SR_CONF_SAMPLERATE:
561 if (devc->data_source == DATA_SOURCE_LIVE) {
562 samplerate = analog_frame_size(sdi) /
563 (devc->timebase * devc->model->series->num_horizontal_divs);
564 *data = g_variant_new_uint64(samplerate);
566 sr_dbg("Unknown data source: %d.", devc->data_source);
570 case SR_CONF_TRIGGER_SOURCE:
571 if (!strcmp(devc->trigger_source, "ACL"))
573 else if (!strcmp(devc->trigger_source, "CHAN1"))
575 else if (!strcmp(devc->trigger_source, "CHAN2"))
577 else if (!strcmp(devc->trigger_source, "CHAN3"))
579 else if (!strcmp(devc->trigger_source, "CHAN4"))
582 tmp_str = devc->trigger_source;
583 *data = g_variant_new_string(tmp_str);
585 case SR_CONF_TRIGGER_SLOPE:
586 if (!strncmp(devc->trigger_slope, "POS", 3)) {
588 } else if (!strncmp(devc->trigger_slope, "NEG", 3)) {
591 sr_dbg("Unknown trigger slope: '%s'.", devc->trigger_slope);
594 *data = g_variant_new_string(tmp_str);
596 case SR_CONF_TRIGGER_LEVEL:
597 *data = g_variant_new_double(devc->trigger_level);
599 case SR_CONF_TIMEBASE:
600 for (i = 0; i < devc->num_timebases; i++) {
601 float tb = (float)devc->timebases[i][0] / devc->timebases[i][1];
602 float diff = fabs(devc->timebase - tb);
603 if (diff < smallest_diff) {
604 smallest_diff = diff;
609 sr_dbg("Negative timebase index: %d.", idx);
612 *data = g_variant_new("(tt)", devc->timebases[idx][0],
613 devc->timebases[idx][1]);
616 if (analog_channel < 0) {
617 sr_dbg("Negative analog channel: %d.", analog_channel);
620 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
621 float vdiv = (float)vdivs[i][0] / vdivs[i][1];
622 float diff = fabs(devc->vdiv[analog_channel] - vdiv);
623 if (diff < smallest_diff) {
624 smallest_diff = diff;
629 sr_dbg("Negative vdiv index: %d.", idx);
632 *data = g_variant_new("(tt)", vdivs[idx][0], vdivs[idx][1]);
634 case SR_CONF_COUPLING:
635 if (analog_channel < 0) {
636 sr_dbg("Negative analog channel: %d.", analog_channel);
639 *data = g_variant_new_string(devc->coupling[analog_channel]);
641 case SR_CONF_PROBE_FACTOR:
642 if (analog_channel < 0) {
643 sr_dbg("Negative analog channel: %d.", analog_channel);
646 *data = g_variant_new_uint64(devc->attenuation[analog_channel]);
655 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
656 const struct sr_channel_group *cg)
658 struct dev_context *devc;
668 /* If a channel group is specified, it must be a valid one. */
669 if (cg && !g_slist_find(sdi->channel_groups, cg)) {
670 sr_err("Invalid channel group specified.");
676 case SR_CONF_LIMIT_FRAMES:
677 devc->limit_frames = g_variant_get_uint64(data);
679 case SR_CONF_TRIGGER_SLOPE:
680 tmp_str = g_variant_get_string(data, NULL);
682 if (!tmp_str || !(tmp_str[0] == 'f' || tmp_str[0] == 'r')) {
683 sr_err("Unknown trigger slope: '%s'.",
684 (tmp_str) ? tmp_str : "NULL");
688 g_free(devc->trigger_slope);
689 devc->trigger_slope = g_strdup((tmp_str[0] == 'r') ? "POS" : "NEG");
690 ret = rigol_ds_config_set(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
692 case SR_CONF_HORIZ_TRIGGERPOS:
693 t_dbl = g_variant_get_double(data);
694 if (t_dbl < 0.0 || t_dbl > 1.0) {
695 sr_err("Invalid horiz. trigger position: %g.", t_dbl);
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 = rigol_ds_config_set(sdi, ":TIM:OFFS %s", buffer);
705 case SR_CONF_TRIGGER_LEVEL:
706 t_dbl = g_variant_get_double(data);
707 g_ascii_formatd(buffer, sizeof(buffer), "%.3f", t_dbl);
708 ret = rigol_ds_config_set(sdi, ":TRIG:EDGE:LEV %s", buffer);
710 devc->trigger_level = t_dbl;
712 case SR_CONF_TIMEBASE:
713 g_variant_get(data, "(tt)", &p, &q);
714 for (i = 0; i < devc->num_timebases; i++) {
715 if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
716 devc->timebase = (float)p / q;
717 g_ascii_formatd(buffer, sizeof(buffer), "%.9f",
719 ret = rigol_ds_config_set(sdi, ":TIM:SCAL %s", buffer);
723 if (i == devc->num_timebases) {
724 sr_err("Invalid timebase index: %d.", i);
728 case SR_CONF_TRIGGER_SOURCE:
729 tmp_str = g_variant_get_string(data, NULL);
730 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
731 if (!strcmp(trigger_sources[i], tmp_str)) {
732 g_free(devc->trigger_source);
733 devc->trigger_source = g_strdup(trigger_sources[i]);
734 if (!strcmp(devc->trigger_source, "AC Line"))
736 else if (!strcmp(devc->trigger_source, "CH1"))
738 else if (!strcmp(devc->trigger_source, "CH2"))
740 else if (!strcmp(devc->trigger_source, "CH3"))
742 else if (!strcmp(devc->trigger_source, "CH4"))
745 tmp_str = (char *)devc->trigger_source;
746 ret = rigol_ds_config_set(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
750 if (i == ARRAY_SIZE(trigger_sources)) {
751 sr_err("Invalid trigger source index: %d.", i);
757 sr_err("No channel group specified.");
758 return SR_ERR_CHANNEL_GROUP;
760 g_variant_get(data, "(tt)", &p, &q);
761 for (i = 0; i < devc->model->analog_channels; i++) {
762 if (cg == devc->analog_groups[i]) {
763 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
764 if (vdivs[j][0] != p || vdivs[j][1] != q)
766 devc->vdiv[i] = (float)p / q;
767 g_ascii_formatd(buffer, sizeof(buffer), "%.3f",
769 return rigol_ds_config_set(sdi, ":CHAN%d:SCAL %s", i + 1,
772 sr_err("Invalid vdiv index: %d.", j);
776 sr_dbg("Didn't set vdiv, unknown channel(group).");
778 case SR_CONF_COUPLING:
780 sr_err("No channel group specified.");
781 return SR_ERR_CHANNEL_GROUP;
783 tmp_str = g_variant_get_string(data, NULL);
784 for (i = 0; i < devc->model->analog_channels; i++) {
785 if (cg == devc->analog_groups[i]) {
786 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
787 if (!strcmp(tmp_str, coupling[j])) {
788 g_free(devc->coupling[i]);
789 devc->coupling[i] = g_strdup(coupling[j]);
790 return rigol_ds_config_set(sdi, ":CHAN%d:COUP %s", i + 1,
794 sr_err("Invalid coupling index: %d.", j);
798 sr_dbg("Didn't set coupling, unknown channel(group).");
800 case SR_CONF_PROBE_FACTOR:
802 sr_err("No channel group specified.");
803 return SR_ERR_CHANNEL_GROUP;
805 p = g_variant_get_uint64(data);
806 for (i = 0; i < devc->model->analog_channels; i++) {
807 if (cg == devc->analog_groups[i]) {
808 for (j = 0; j < ARRAY_SIZE(probe_factor); j++) {
809 if (p == probe_factor[j]) {
810 devc->attenuation[i] = p;
811 ret = rigol_ds_config_set(sdi, ":CHAN%d:PROB %"PRIu64,
814 rigol_ds_get_dev_cfg_vertical(sdi);
818 sr_err("Invalid probe factor: %"PRIu64".", p);
822 sr_dbg("Didn't set probe factor, unknown channel(group).");
824 case SR_CONF_DATA_SOURCE:
825 tmp_str = g_variant_get_string(data, NULL);
826 if (!strcmp(tmp_str, "Live"))
827 devc->data_source = DATA_SOURCE_LIVE;
828 else if (devc->model->series->protocol >= PROTOCOL_V2
829 && !strcmp(tmp_str, "Memory"))
830 devc->data_source = DATA_SOURCE_MEMORY;
831 else if (devc->model->series->protocol >= PROTOCOL_V3
832 && !strcmp(tmp_str, "Segmented"))
833 devc->data_source = DATA_SOURCE_SEGMENTED;
835 sr_err("Unknown data source: '%s'.", tmp_str);
846 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
847 const struct sr_channel_group *cg)
849 GVariant *tuple, *rational[2];
852 struct dev_context *devc = NULL;
854 /* SR_CONF_SCAN_OPTIONS is always valid, regardless of sdi or channel group. */
855 if (key == SR_CONF_SCAN_OPTIONS) {
856 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
857 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
861 /* If sdi is NULL, nothing except SR_CONF_DEVICE_OPTIONS can be provided. */
862 if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
863 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
864 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
868 /* Every other option requires a valid device instance. */
873 /* If a channel group is specified, it must be a valid one. */
874 if (cg && !g_slist_find(sdi->channel_groups, cg)) {
875 sr_err("Invalid channel group specified.");
880 case SR_CONF_DEVICE_OPTIONS:
882 /* If cg is NULL, only the SR_CONF_DEVICE_OPTIONS that are not
883 * specific to a channel group must be returned. */
884 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
885 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
888 if (cg == devc->digital_group) {
889 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
890 NULL, 0, sizeof(uint32_t));
893 for (i = 0; i < devc->model->analog_channels; i++) {
894 if (cg == devc->analog_groups[i]) {
895 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
896 analog_devopts, ARRAY_SIZE(analog_devopts), sizeof(uint32_t));
903 case SR_CONF_COUPLING:
905 sr_err("No channel group specified.");
906 return SR_ERR_CHANNEL_GROUP;
908 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
910 case SR_CONF_PROBE_FACTOR:
912 sr_err("No channel group specified.");
913 return SR_ERR_CHANNEL_GROUP;
915 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
916 probe_factor, ARRAY_SIZE(probe_factor), sizeof(uint64_t));
920 /* Can't know this until we have the exact model. */
923 sr_err("No channel group specified.");
924 return SR_ERR_CHANNEL_GROUP;
926 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
927 for (i = 0; i < devc->num_vdivs; i++) {
928 rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
929 rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
930 tuple = g_variant_new_tuple(rational, 2);
931 g_variant_builder_add_value(&gvb, tuple);
933 *data = g_variant_builder_end(&gvb);
935 case SR_CONF_TIMEBASE:
937 /* Can't know this until we have the exact model. */
939 if (devc->num_timebases <= 0)
941 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
942 for (i = 0; i < devc->num_timebases; i++) {
943 rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
944 rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
945 tuple = g_variant_new_tuple(rational, 2);
946 g_variant_builder_add_value(&gvb, tuple);
948 *data = g_variant_builder_end(&gvb);
950 case SR_CONF_TRIGGER_SOURCE:
952 /* Can't know this until we have the exact model. */
954 *data = g_variant_new_strv(trigger_sources,
955 devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
957 case SR_CONF_TRIGGER_SLOPE:
958 *data = g_variant_new_strv(trigger_slopes, ARRAY_SIZE(trigger_slopes));
960 case SR_CONF_DATA_SOURCE:
962 /* Can't know this until we have the exact model. */
964 switch (devc->model->series->protocol) {
966 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 2);
969 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
972 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
983 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
985 struct sr_scpi_dev_inst *scpi;
986 struct dev_context *devc;
987 struct sr_channel *ch;
988 struct sr_datafeed_packet packet;
989 gboolean some_digital;
995 devc->num_frames = 0;
997 some_digital = FALSE;
998 for (l = sdi->channels; l; l = l->next) {
1000 sr_dbg("handling channel %s", ch->name);
1001 if (ch->type == SR_CHANNEL_ANALOG) {
1003 devc->enabled_channels = g_slist_append(
1004 devc->enabled_channels, ch);
1005 if (ch->enabled != devc->analog_channels[ch->index]) {
1006 /* Enabled channel is currently disabled, or vice versa. */
1007 if (rigol_ds_config_set(sdi, ":CHAN%d:DISP %s", ch->index + 1,
1008 ch->enabled ? "ON" : "OFF") != SR_OK)
1010 devc->analog_channels[ch->index] = ch->enabled;
1012 } else if (ch->type == SR_CHANNEL_LOGIC) {
1013 /* Only one list entry for older protocols. All channels are
1014 * retrieved together when this entry is processed. */
1015 if (ch->enabled && (
1016 devc->model->series->protocol > PROTOCOL_V3 ||
1018 devc->enabled_channels = g_slist_append(
1019 devc->enabled_channels, ch);
1021 some_digital = TRUE;
1022 /* Turn on LA module if currently off. */
1023 if (!devc->la_enabled) {
1024 if (rigol_ds_config_set(sdi,
1025 devc->model->series->protocol >= PROTOCOL_V3 ?
1026 ":LA:STAT ON" : ":LA:DISP ON") != SR_OK)
1028 devc->la_enabled = TRUE;
1031 if (ch->enabled != devc->digital_channels[ch->index]) {
1032 /* Enabled channel is currently disabled, or vice versa. */
1033 if (rigol_ds_config_set(sdi,
1034 devc->model->series->protocol >= PROTOCOL_V3 ?
1035 ":LA:DIG%d:DISP %s" : ":DIG%d:TURN %s", ch->index,
1036 ch->enabled ? "ON" : "OFF") != SR_OK)
1038 devc->digital_channels[ch->index] = ch->enabled;
1043 if (!devc->enabled_channels)
1046 /* Turn off LA module if on and no digital channels selected. */
1047 if (devc->la_enabled && !some_digital)
1048 if (rigol_ds_config_set(sdi,
1049 devc->model->series->protocol >= PROTOCOL_V3 ?
1050 ":LA:STAT OFF" : ":LA:DISP OFF") != SR_OK)
1053 /* Set memory mode. */
1054 if (devc->data_source == DATA_SOURCE_SEGMENTED) {
1055 sr_err("Data source 'Segmented' not yet supported");
1059 devc->analog_frame_size = analog_frame_size(sdi);
1060 devc->digital_frame_size = digital_frame_size(sdi);
1062 switch (devc->model->series->protocol) {
1064 if (rigol_ds_config_set(sdi, ":ACQ:MEMD LONG") != SR_OK)
1068 /* Apparently for the DS2000 the memory
1069 * depth can only be set in Running state -
1070 * this matches the behaviour of the UI. */
1071 if (rigol_ds_config_set(sdi, ":RUN") != SR_OK)
1073 if (rigol_ds_config_set(sdi, ":ACQ:MDEP %d",
1074 devc->analog_frame_size) != SR_OK)
1076 if (rigol_ds_config_set(sdi, ":STOP") != SR_OK)
1083 if (devc->data_source == DATA_SOURCE_LIVE)
1084 if (rigol_ds_config_set(sdi, ":RUN") != SR_OK)
1087 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
1088 rigol_ds_receive, (void *)sdi);
1090 std_session_send_df_header(sdi);
1092 devc->channel_entry = devc->enabled_channels;
1094 if (rigol_ds_capture_start(sdi) != SR_OK)
1097 /* Start of first frame. */
1098 packet.type = SR_DF_FRAME_BEGIN;
1099 sr_session_send(sdi, &packet);
1104 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
1106 struct dev_context *devc;
1107 struct sr_scpi_dev_inst *scpi;
1111 std_session_send_df_end(sdi);
1113 g_slist_free(devc->enabled_channels);
1114 devc->enabled_channels = NULL;
1116 sr_scpi_source_remove(sdi->session, scpi);
1121 static struct sr_dev_driver rigol_ds_driver_info = {
1123 .longname = "Rigol DS",
1126 .cleanup = std_cleanup,
1128 .dev_list = std_dev_list,
1129 .dev_clear = dev_clear,
1130 .config_get = config_get,
1131 .config_set = config_set,
1132 .config_list = config_list,
1133 .dev_open = dev_open,
1134 .dev_close = dev_close,
1135 .dev_acquisition_start = dev_acquisition_start,
1136 .dev_acquisition_stop = dev_acquisition_stop,
1139 SR_REGISTER_DEV_DRIVER(rigol_ds_driver_info);