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 devopts[] = {
42 SR_CONF_LIMIT_FRAMES | SR_CONF_SET,
43 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
44 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
45 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46 SR_CONF_TRIGGER_LEVEL | SR_CONF_GET | SR_CONF_SET,
47 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_SET,
48 SR_CONF_NUM_HDIV | SR_CONF_GET,
49 SR_CONF_SAMPLERATE | SR_CONF_GET,
50 SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
53 static const uint32_t analog_devopts[] = {
54 SR_CONF_NUM_VDIV | SR_CONF_GET,
55 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
56 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
57 SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
60 static const uint64_t timebases[][2] = {
103 static const uint64_t vdivs[][2] = {
126 #define NUM_TIMEBASE ARRAY_SIZE(timebases)
127 #define NUM_VDIV ARRAY_SIZE(vdivs)
129 static const char *trigger_sources[] = {
154 static const char *trigger_slopes[] = {
159 static const char *coupling[] = {
165 static const uint64_t probe_factor[] = {
178 /* Do not change the order of entries */
179 static const char *data_sources[] = {
199 /* short name, full name */
200 static const struct rigol_ds_vendor supported_vendors[] = {
201 [RIGOL] = {"Rigol", "Rigol Technologies"},
202 [AGILENT] = {"Agilent", "Agilent Technologies"},
205 #define VENDOR(x) &supported_vendors[x]
206 /* vendor, series, protocol, max timebase, min vdiv, number of horizontal divs,
207 * live waveform samples, memory buffer samples */
208 static const struct rigol_ds_series supported_series[] = {
209 [VS5000] = {VENDOR(RIGOL), "VS5000", PROTOCOL_V1, FORMAT_RAW,
210 {50, 1}, {2, 1000}, 14, 2048, 0},
211 [DS1000] = {VENDOR(RIGOL), "DS1000", PROTOCOL_V2, FORMAT_IEEE488_2,
212 {50, 1}, {2, 1000}, 12, 600, 1048576},
213 [DS2000] = {VENDOR(RIGOL), "DS2000", PROTOCOL_V3, FORMAT_IEEE488_2,
214 {500, 1}, {500, 1000000}, 14, 1400, 14000},
215 [DS2000A] = {VENDOR(RIGOL), "DS2000A", PROTOCOL_V3, FORMAT_IEEE488_2,
216 {1000, 1}, {500, 1000000}, 14, 1400, 14000},
217 [DSO1000] = {VENDOR(AGILENT), "DSO1000", PROTOCOL_V3, FORMAT_IEEE488_2,
218 {50, 1}, {2, 1000}, 12, 600, 20480},
219 [DS1000Z] = {VENDOR(RIGOL), "DS1000Z", PROTOCOL_V4, FORMAT_IEEE488_2,
220 {50, 1}, {1, 1000}, 12, 1200, 12000000},
223 #define SERIES(x) &supported_series[x]
224 /* series, model, min timebase, analog channels, digital */
225 static const struct rigol_ds_model supported_models[] = {
226 {SERIES(VS5000), "VS5022", {20, 1000000000}, 2, false},
227 {SERIES(VS5000), "VS5042", {10, 1000000000}, 2, false},
228 {SERIES(VS5000), "VS5062", {5, 1000000000}, 2, false},
229 {SERIES(VS5000), "VS5102", {2, 1000000000}, 2, false},
230 {SERIES(VS5000), "VS5202", {2, 1000000000}, 2, false},
231 {SERIES(VS5000), "VS5022D", {20, 1000000000}, 2, true},
232 {SERIES(VS5000), "VS5042D", {10, 1000000000}, 2, true},
233 {SERIES(VS5000), "VS5062D", {5, 1000000000}, 2, true},
234 {SERIES(VS5000), "VS5102D", {2, 1000000000}, 2, true},
235 {SERIES(VS5000), "VS5202D", {2, 1000000000}, 2, true},
236 {SERIES(DS1000), "DS1052E", {5, 1000000000}, 2, false},
237 {SERIES(DS1000), "DS1102E", {2, 1000000000}, 2, false},
238 {SERIES(DS1000), "DS1152E", {2, 1000000000}, 2, false},
239 {SERIES(DS1000), "DS1052D", {5, 1000000000}, 2, true},
240 {SERIES(DS1000), "DS1102D", {2, 1000000000}, 2, true},
241 {SERIES(DS1000), "DS1152D", {2, 1000000000}, 2, true},
242 {SERIES(DS2000), "DS2072", {5, 1000000000}, 2, false},
243 {SERIES(DS2000), "DS2102", {5, 1000000000}, 2, false},
244 {SERIES(DS2000), "DS2202", {2, 1000000000}, 2, false},
245 {SERIES(DS2000), "DS2302", {1, 1000000000}, 2, false},
246 {SERIES(DS2000A), "DS2072A", {5, 1000000000}, 2, false},
247 {SERIES(DS2000A), "DS2102A", {5, 1000000000}, 2, false},
248 {SERIES(DS2000A), "DS2202A", {2, 1000000000}, 2, false},
249 {SERIES(DS2000A), "DS2302A", {1, 1000000000}, 2, false},
250 {SERIES(DSO1000), "DSO1002A", {5, 1000000000}, 2, false},
251 {SERIES(DSO1000), "DSO1004A", {5, 1000000000}, 4, false},
252 {SERIES(DSO1000), "DSO1012A", {2, 1000000000}, 2, false},
253 {SERIES(DSO1000), "DSO1014A", {2, 1000000000}, 4, false},
254 {SERIES(DSO1000), "DSO1022A", {2, 1000000000}, 2, false},
255 {SERIES(DSO1000), "DSO1024A", {2, 1000000000}, 4, false},
256 {SERIES(DS1000Z), "DS1054Z", {5, 1000000000}, 4, false},
257 {SERIES(DS1000Z), "DS1074Z", {5, 1000000000}, 4, false},
258 {SERIES(DS1000Z), "DS1104Z", {5, 1000000000}, 4, false},
259 {SERIES(DS1000Z), "DS1074Z-S", {5, 1000000000}, 4, false},
260 {SERIES(DS1000Z), "DS1104Z-S", {5, 1000000000}, 4, false},
261 {SERIES(DS1000Z), "DS1074Z Plus", {5, 1000000000}, 4, false},
262 {SERIES(DS1000Z), "DS1104Z Plus", {5, 1000000000}, 4, false},
263 {SERIES(DS1000Z), "MSO1074Z", {5, 1000000000}, 4, true},
264 {SERIES(DS1000Z), "MSO1104Z", {5, 1000000000}, 4, true},
265 {SERIES(DS1000Z), "MSO1074Z-S", {5, 1000000000}, 4, true},
266 {SERIES(DS1000Z), "MSO1104Z-S", {5, 1000000000}, 4, true},
269 static struct sr_dev_driver rigol_ds_driver_info;
271 static void clear_helper(void *priv)
273 struct dev_context *devc;
278 g_free(devc->buffer);
279 for (i = 0; i < ARRAY_SIZE(devc->coupling); i++)
280 g_free(devc->coupling[i]);
281 g_free(devc->trigger_source);
282 g_free(devc->trigger_slope);
283 g_free(devc->analog_groups);
287 static int dev_clear(const struct sr_dev_driver *di)
289 return std_dev_clear(di, clear_helper);
292 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
294 struct dev_context *devc;
295 struct sr_dev_inst *sdi;
296 struct sr_scpi_hw_info *hw_info;
297 struct sr_channel *ch;
300 const struct rigol_ds_model *model = NULL;
301 gchar *channel_name, **version;
303 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
304 sr_info("Couldn't get IDN response, retrying.");
307 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
308 sr_info("Couldn't get IDN response.");
313 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
314 if (!g_ascii_strcasecmp(hw_info->manufacturer,
315 supported_models[i].series->vendor->full_name) &&
316 !strcmp(hw_info->model, supported_models[i].name)) {
317 model = &supported_models[i];
323 sr_scpi_hw_info_free(hw_info);
327 sdi = g_malloc0(sizeof(struct sr_dev_inst));
328 sdi->vendor = g_strdup(model->series->vendor->name);
329 sdi->model = g_strdup(model->name);
330 sdi->version = g_strdup(hw_info->firmware_version);
332 sdi->driver = &rigol_ds_driver_info;
333 sdi->inst_type = SR_INST_SCPI;
334 sdi->serial_num = g_strdup(hw_info->serial_number);
335 devc = g_malloc0(sizeof(struct dev_context));
336 devc->limit_frames = 0;
338 devc->format = model->series->format;
340 /* DS1000 models with firmware before 0.2.4 used the old data format. */
341 if (model->series == SERIES(DS1000)) {
342 version = g_strsplit(hw_info->firmware_version, ".", 0);
344 if (!version[0] || !version[1] || !version[2])
346 if (version[0][0] == 0 || version[1][0] == 0 || version[2][0] == 0)
348 for (i = 0; i < 3; i++) {
349 if (sr_atol(version[i], &n[i]) != SR_OK)
354 scpi->firmware_version = n[0] * 100 + n[1] * 10 + n[2];
355 if (scpi->firmware_version < 24) {
356 sr_dbg("Found DS1000 firmware < 0.2.4, using raw data format.");
357 devc->format = FORMAT_RAW;
364 sr_scpi_hw_info_free(hw_info);
366 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
367 model->analog_channels);
369 for (i = 0; i < model->analog_channels; i++) {
370 channel_name = g_strdup_printf("CH%d", i + 1);
371 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_name);
373 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
375 devc->analog_groups[i]->name = channel_name;
376 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
377 sdi->channel_groups = g_slist_append(sdi->channel_groups,
378 devc->analog_groups[i]);
381 if (devc->model->has_digital) {
382 devc->digital_group = g_malloc0(sizeof(struct sr_channel_group));
384 for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) {
385 channel_name = g_strdup_printf("D%d", i);
386 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
387 g_free(channel_name);
388 devc->digital_group->channels = g_slist_append(
389 devc->digital_group->channels, ch);
391 devc->digital_group->name = g_strdup("LA");
392 sdi->channel_groups = g_slist_append(sdi->channel_groups,
393 devc->digital_group);
396 for (i = 0; i < NUM_TIMEBASE; i++) {
397 if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
398 devc->timebases = &timebases[i];
399 if (!memcmp(&devc->model->series->max_timebase, &timebases[i], sizeof(uint64_t[2])))
400 devc->num_timebases = &timebases[i] - devc->timebases + 1;
403 for (i = 0; i < NUM_VDIV; i++) {
404 if (!memcmp(&devc->model->series->min_vdiv,
405 &vdivs[i], sizeof(uint64_t[2]))) {
406 devc->vdivs = &vdivs[i];
407 devc->num_vdivs = NUM_VDIV - i;
411 devc->buffer = g_malloc(ACQ_BUFFER_SIZE);
412 devc->data = g_malloc(ACQ_BUFFER_SIZE * sizeof(float));
414 devc->data_source = DATA_SOURCE_LIVE;
421 static GSList *scan(struct sr_dev_driver *di, GSList *options)
423 return sr_scpi_scan(di->context, options, probe_device);
426 static int dev_open(struct sr_dev_inst *sdi)
429 struct sr_scpi_dev_inst *scpi = sdi->conn;
431 if ((ret = sr_scpi_open(scpi)) < 0) {
432 sr_err("Failed to open SCPI device: %s.", sr_strerror(ret));
436 if ((ret = rigol_ds_get_dev_cfg(sdi)) < 0) {
437 sr_err("Failed to get device config: %s.", sr_strerror(ret));
441 sdi->status = SR_ST_ACTIVE;
446 static int dev_close(struct sr_dev_inst *sdi)
448 struct sr_scpi_dev_inst *scpi;
449 struct dev_context *devc;
451 if (sdi->status != SR_ST_ACTIVE)
452 return SR_ERR_DEV_CLOSED;
457 if (devc->model->series->protocol == PROTOCOL_V2)
458 rigol_ds_config_set(sdi, ":KEY:LOCK DISABLE");
461 if (sr_scpi_close(scpi) < 0)
463 sdi->status = SR_ST_INACTIVE;
469 static int analog_frame_size(const struct sr_dev_inst *sdi)
471 struct dev_context *devc = sdi->priv;
472 struct sr_channel *ch;
473 int analog_channels = 0;
476 for (l = sdi->channels; l; l = l->next) {
478 if (ch->type == SR_CHANNEL_ANALOG && ch->enabled)
482 if (analog_channels == 0)
485 switch (devc->data_source) {
486 case DATA_SOURCE_LIVE:
487 return devc->model->series->live_samples;
488 case DATA_SOURCE_MEMORY:
489 return devc->model->series->buffer_samples / analog_channels;
495 static int digital_frame_size(const struct sr_dev_inst *sdi)
497 struct dev_context *devc = sdi->priv;
499 switch (devc->data_source) {
500 case DATA_SOURCE_LIVE:
501 return devc->model->series->live_samples * 2;
502 case DATA_SOURCE_MEMORY:
503 return devc->model->series->buffer_samples * 2;
509 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
510 const struct sr_channel_group *cg)
512 struct dev_context *devc;
513 struct sr_channel *ch;
516 int analog_channel = -1;
517 float smallest_diff = INFINITY;
526 /* If a channel group is specified, it must be a valid one. */
527 if (cg && !g_slist_find(sdi->channel_groups, cg)) {
528 sr_err("Invalid channel group specified.");
533 ch = g_slist_nth_data(cg->channels, 0);
536 if (ch->type == SR_CHANNEL_ANALOG) {
537 if (ch->name[2] < '1' || ch->name[2] > '4')
539 analog_channel = ch->name[2] - '1';
544 case SR_CONF_NUM_HDIV:
545 *data = g_variant_new_int32(devc->model->series->num_horizontal_divs);
547 case SR_CONF_NUM_VDIV:
548 *data = g_variant_new_int32(devc->num_vdivs);
550 case SR_CONF_DATA_SOURCE:
551 if (devc->data_source == DATA_SOURCE_LIVE)
552 *data = g_variant_new_string("Live");
553 else if (devc->data_source == DATA_SOURCE_MEMORY)
554 *data = g_variant_new_string("Memory");
556 *data = g_variant_new_string("Segmented");
558 case SR_CONF_SAMPLERATE:
559 if (devc->data_source == DATA_SOURCE_LIVE) {
560 samplerate = analog_frame_size(sdi) /
561 (devc->timebase * devc->model->series->num_horizontal_divs);
562 *data = g_variant_new_uint64(samplerate);
564 sr_dbg("Unknown data source: %d.", devc->data_source);
568 case SR_CONF_TRIGGER_SOURCE:
569 if (!strcmp(devc->trigger_source, "ACL"))
571 else if (!strcmp(devc->trigger_source, "CHAN1"))
573 else if (!strcmp(devc->trigger_source, "CHAN2"))
575 else if (!strcmp(devc->trigger_source, "CHAN3"))
577 else if (!strcmp(devc->trigger_source, "CHAN4"))
580 tmp_str = devc->trigger_source;
581 *data = g_variant_new_string(tmp_str);
583 case SR_CONF_TRIGGER_SLOPE:
584 if (!strncmp(devc->trigger_slope, "POS", 3)) {
586 } else if (!strncmp(devc->trigger_slope, "NEG", 3)) {
589 sr_dbg("Unknown trigger slope: '%s'.", devc->trigger_slope);
592 *data = g_variant_new_string(tmp_str);
594 case SR_CONF_TRIGGER_LEVEL:
595 *data = g_variant_new_double(devc->trigger_level);
597 case SR_CONF_TIMEBASE:
598 for (i = 0; i < devc->num_timebases; i++) {
599 float tb = (float)devc->timebases[i][0] / devc->timebases[i][1];
600 float diff = fabs(devc->timebase - tb);
601 if (diff < smallest_diff) {
602 smallest_diff = diff;
607 sr_dbg("Negative timebase index: %d.", idx);
610 *data = g_variant_new("(tt)", devc->timebases[idx][0],
611 devc->timebases[idx][1]);
614 if (analog_channel < 0) {
615 sr_dbg("Negative analog channel: %d.", analog_channel);
618 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
619 float vdiv = (float)vdivs[i][0] / vdivs[i][1];
620 float diff = fabs(devc->vdiv[analog_channel] - vdiv);
621 if (diff < smallest_diff) {
622 smallest_diff = diff;
627 sr_dbg("Negative vdiv index: %d.", idx);
630 *data = g_variant_new("(tt)", vdivs[idx][0], vdivs[idx][1]);
632 case SR_CONF_COUPLING:
633 if (analog_channel < 0) {
634 sr_dbg("Negative analog channel: %d.", analog_channel);
637 *data = g_variant_new_string(devc->coupling[analog_channel]);
639 case SR_CONF_PROBE_FACTOR:
640 if (analog_channel < 0) {
641 sr_dbg("Negative analog channel: %d.", analog_channel);
644 *data = g_variant_new_uint64(devc->attenuation[analog_channel]);
653 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
654 const struct sr_channel_group *cg)
656 struct dev_context *devc;
666 if (sdi->status != SR_ST_ACTIVE)
667 return SR_ERR_DEV_CLOSED;
669 /* If a channel group is specified, it must be a valid one. */
670 if (cg && !g_slist_find(sdi->channel_groups, cg)) {
671 sr_err("Invalid channel group specified.");
677 case SR_CONF_LIMIT_FRAMES:
678 devc->limit_frames = g_variant_get_uint64(data);
680 case SR_CONF_TRIGGER_SLOPE:
681 tmp_str = g_variant_get_string(data, NULL);
683 if (!tmp_str || !(tmp_str[0] == 'f' || tmp_str[0] == 'r')) {
684 sr_err("Unknown trigger slope: '%s'.",
685 (tmp_str) ? tmp_str : "NULL");
689 g_free(devc->trigger_slope);
690 devc->trigger_slope = g_strdup((tmp_str[0] == 'r') ? "POS" : "NEG");
691 ret = rigol_ds_config_set(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
693 case SR_CONF_HORIZ_TRIGGERPOS:
694 t_dbl = g_variant_get_double(data);
695 if (t_dbl < 0.0 || t_dbl > 1.0) {
696 sr_err("Invalid horiz. trigger position: %g.", t_dbl);
699 devc->horiz_triggerpos = t_dbl;
700 /* We have the trigger offset as a percentage of the frame, but
701 * need to express this in seconds. */
702 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
703 g_ascii_formatd(buffer, sizeof(buffer), "%.6f", t_dbl);
704 ret = rigol_ds_config_set(sdi, ":TIM:OFFS %s", buffer);
706 case SR_CONF_TRIGGER_LEVEL:
707 t_dbl = g_variant_get_double(data);
708 g_ascii_formatd(buffer, sizeof(buffer), "%.3f", t_dbl);
709 ret = rigol_ds_config_set(sdi, ":TRIG:EDGE:LEV %s", buffer);
711 devc->trigger_level = t_dbl;
713 case SR_CONF_TIMEBASE:
714 g_variant_get(data, "(tt)", &p, &q);
715 for (i = 0; i < devc->num_timebases; i++) {
716 if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
717 devc->timebase = (float)p / q;
718 g_ascii_formatd(buffer, sizeof(buffer), "%.9f",
720 ret = rigol_ds_config_set(sdi, ":TIM:SCAL %s", buffer);
724 if (i == devc->num_timebases) {
725 sr_err("Invalid timebase index: %d.", i);
729 case SR_CONF_TRIGGER_SOURCE:
730 tmp_str = g_variant_get_string(data, NULL);
731 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
732 if (!strcmp(trigger_sources[i], tmp_str)) {
733 g_free(devc->trigger_source);
734 devc->trigger_source = g_strdup(trigger_sources[i]);
735 if (!strcmp(devc->trigger_source, "AC Line"))
737 else if (!strcmp(devc->trigger_source, "CH1"))
739 else if (!strcmp(devc->trigger_source, "CH2"))
741 else if (!strcmp(devc->trigger_source, "CH3"))
743 else if (!strcmp(devc->trigger_source, "CH4"))
746 tmp_str = (char *)devc->trigger_source;
747 ret = rigol_ds_config_set(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
751 if (i == ARRAY_SIZE(trigger_sources)) {
752 sr_err("Invalid trigger source index: %d.", i);
758 sr_err("No channel group specified.");
759 return SR_ERR_CHANNEL_GROUP;
761 g_variant_get(data, "(tt)", &p, &q);
762 for (i = 0; i < devc->model->analog_channels; i++) {
763 if (cg == devc->analog_groups[i]) {
764 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
765 if (vdivs[j][0] != p || vdivs[j][1] != q)
767 devc->vdiv[i] = (float)p / q;
768 g_ascii_formatd(buffer, sizeof(buffer), "%.3f",
770 return rigol_ds_config_set(sdi, ":CHAN%d:SCAL %s", i + 1,
773 sr_err("Invalid vdiv index: %d.", j);
777 sr_dbg("Didn't set vdiv, unknown channel(group).");
779 case SR_CONF_COUPLING:
781 sr_err("No channel group specified.");
782 return SR_ERR_CHANNEL_GROUP;
784 tmp_str = g_variant_get_string(data, NULL);
785 for (i = 0; i < devc->model->analog_channels; i++) {
786 if (cg == devc->analog_groups[i]) {
787 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
788 if (!strcmp(tmp_str, coupling[j])) {
789 g_free(devc->coupling[i]);
790 devc->coupling[i] = g_strdup(coupling[j]);
791 return rigol_ds_config_set(sdi, ":CHAN%d:COUP %s", i + 1,
795 sr_err("Invalid coupling index: %d.", j);
799 sr_dbg("Didn't set coupling, unknown channel(group).");
801 case SR_CONF_PROBE_FACTOR:
803 sr_err("No channel group specified.");
804 return SR_ERR_CHANNEL_GROUP;
806 p = g_variant_get_uint64(data);
807 for (i = 0; i < devc->model->analog_channels; i++) {
808 if (cg == devc->analog_groups[i]) {
809 for (j = 0; j < ARRAY_SIZE(probe_factor); j++) {
810 if (p == probe_factor[j]) {
811 devc->attenuation[i] = p;
812 ret = rigol_ds_config_set(sdi, ":CHAN%d:PROB %"PRIu64,
815 rigol_ds_get_dev_cfg_vertical(sdi);
819 sr_err("Invalid probe factor: %"PRIu64".", p);
823 sr_dbg("Didn't set probe factor, unknown channel(group).");
825 case SR_CONF_DATA_SOURCE:
826 tmp_str = g_variant_get_string(data, NULL);
827 if (!strcmp(tmp_str, "Live"))
828 devc->data_source = DATA_SOURCE_LIVE;
829 else if (devc->model->series->protocol >= PROTOCOL_V2
830 && !strcmp(tmp_str, "Memory"))
831 devc->data_source = DATA_SOURCE_MEMORY;
832 else if (devc->model->series->protocol >= PROTOCOL_V3
833 && !strcmp(tmp_str, "Segmented"))
834 devc->data_source = DATA_SOURCE_SEGMENTED;
836 sr_err("Unknown data source: '%s'.", tmp_str);
847 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
848 const struct sr_channel_group *cg)
850 GVariant *tuple, *rational[2];
853 struct dev_context *devc = NULL;
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));
859 } else if (key == SR_CONF_DEVICE_OPTIONS && !cg) {
860 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
861 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
865 /* Every other option requires a valid device instance. */
870 /* If a channel group is specified, it must be a valid one. */
871 if (cg && !g_slist_find(sdi->channel_groups, cg)) {
872 sr_err("Invalid channel group specified.");
877 case SR_CONF_DEVICE_OPTIONS:
879 sr_err("No channel group specified.");
880 return SR_ERR_CHANNEL_GROUP;
882 if (cg == devc->digital_group) {
883 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
884 NULL, 0, sizeof(uint32_t));
887 for (i = 0; i < devc->model->analog_channels; i++) {
888 if (cg == devc->analog_groups[i]) {
889 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
890 analog_devopts, ARRAY_SIZE(analog_devopts), sizeof(uint32_t));
897 case SR_CONF_COUPLING:
899 sr_err("No channel group specified.");
900 return SR_ERR_CHANNEL_GROUP;
902 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
904 case SR_CONF_PROBE_FACTOR:
906 sr_err("No channel group specified.");
907 return SR_ERR_CHANNEL_GROUP;
909 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
910 probe_factor, ARRAY_SIZE(probe_factor), sizeof(uint64_t));
914 /* Can't know this until we have the exact model. */
917 sr_err("No channel group specified.");
918 return SR_ERR_CHANNEL_GROUP;
920 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
921 for (i = 0; i < devc->num_vdivs; i++) {
922 rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
923 rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
924 tuple = g_variant_new_tuple(rational, 2);
925 g_variant_builder_add_value(&gvb, tuple);
927 *data = g_variant_builder_end(&gvb);
929 case SR_CONF_TIMEBASE:
931 /* Can't know this until we have the exact model. */
933 if (devc->num_timebases <= 0)
935 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
936 for (i = 0; i < devc->num_timebases; i++) {
937 rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
938 rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
939 tuple = g_variant_new_tuple(rational, 2);
940 g_variant_builder_add_value(&gvb, tuple);
942 *data = g_variant_builder_end(&gvb);
944 case SR_CONF_TRIGGER_SOURCE:
946 /* Can't know this until we have the exact model. */
948 *data = g_variant_new_strv(trigger_sources,
949 devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
951 case SR_CONF_TRIGGER_SLOPE:
952 *data = g_variant_new_strv(trigger_slopes, ARRAY_SIZE(trigger_slopes));
954 case SR_CONF_DATA_SOURCE:
956 /* Can't know this until we have the exact model. */
958 switch (devc->model->series->protocol) {
960 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 2);
963 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
966 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
977 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
979 struct sr_scpi_dev_inst *scpi;
980 struct dev_context *devc;
981 struct sr_channel *ch;
982 struct sr_datafeed_packet packet;
983 gboolean some_digital;
986 if (sdi->status != SR_ST_ACTIVE)
987 return SR_ERR_DEV_CLOSED;
992 devc->num_frames = 0;
994 some_digital = FALSE;
995 for (l = sdi->channels; l; l = l->next) {
997 sr_dbg("handling channel %s", ch->name);
998 if (ch->type == SR_CHANNEL_ANALOG) {
1000 devc->enabled_channels = g_slist_append(
1001 devc->enabled_channels, ch);
1002 if (ch->enabled != devc->analog_channels[ch->index]) {
1003 /* Enabled channel is currently disabled, or vice versa. */
1004 if (rigol_ds_config_set(sdi, ":CHAN%d:DISP %s", ch->index + 1,
1005 ch->enabled ? "ON" : "OFF") != SR_OK)
1007 devc->analog_channels[ch->index] = ch->enabled;
1009 } else if (ch->type == SR_CHANNEL_LOGIC) {
1010 /* Only one list entry for DS1000D series. All channels are retrieved
1011 * together when this entry is processed. */
1012 if (ch->enabled && (
1013 devc->model->series->protocol > PROTOCOL_V2 ||
1015 devc->enabled_channels = g_slist_append(
1016 devc->enabled_channels, ch);
1018 some_digital = TRUE;
1019 /* Turn on LA module if currently off. */
1020 if (!devc->la_enabled) {
1021 if (rigol_ds_config_set(sdi,
1022 devc->model->series->protocol >= PROTOCOL_V4 ?
1023 ":LA:STAT ON" : ":LA:DISP ON") != SR_OK)
1025 devc->la_enabled = TRUE;
1028 if (ch->enabled != devc->digital_channels[ch->index]) {
1029 /* Enabled channel is currently disabled, or vice versa. */
1030 if (rigol_ds_config_set(sdi,
1031 devc->model->series->protocol >= PROTOCOL_V4 ?
1032 ":LA:DIG%d:DISP %s" : ":DIG%d:TURN %s", ch->index,
1033 ch->enabled ? "ON" : "OFF") != SR_OK)
1035 devc->digital_channels[ch->index] = ch->enabled;
1040 if (!devc->enabled_channels)
1043 /* Turn off LA module if on and no digital channels selected. */
1044 if (devc->la_enabled && !some_digital)
1045 if (rigol_ds_config_set(sdi,
1046 devc->model->series->protocol >= PROTOCOL_V4 ?
1047 ":LA:STAT OFF" : ":LA:DISP OFF") != SR_OK)
1050 /* Set memory mode. */
1051 if (devc->data_source == DATA_SOURCE_SEGMENTED) {
1052 sr_err("Data source 'Segmented' not yet supported");
1056 devc->analog_frame_size = analog_frame_size(sdi);
1057 devc->digital_frame_size = digital_frame_size(sdi);
1059 switch (devc->model->series->protocol) {
1061 if (rigol_ds_config_set(sdi, ":ACQ:MEMD LONG") != SR_OK)
1065 /* Apparently for the DS2000 the memory
1066 * depth can only be set in Running state -
1067 * this matches the behaviour of the UI. */
1068 if (rigol_ds_config_set(sdi, ":RUN") != SR_OK)
1070 if (rigol_ds_config_set(sdi, ":ACQ:MDEP %d",
1071 devc->analog_frame_size) != SR_OK)
1073 if (rigol_ds_config_set(sdi, ":STOP") != SR_OK)
1080 if (devc->data_source == DATA_SOURCE_LIVE)
1081 if (rigol_ds_config_set(sdi, ":RUN") != SR_OK)
1084 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
1085 rigol_ds_receive, (void *)sdi);
1087 std_session_send_df_header(sdi);
1089 devc->channel_entry = devc->enabled_channels;
1091 if (rigol_ds_capture_start(sdi) != SR_OK)
1094 /* Start of first frame. */
1095 packet.type = SR_DF_FRAME_BEGIN;
1096 sr_session_send(sdi, &packet);
1101 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
1103 struct dev_context *devc;
1104 struct sr_scpi_dev_inst *scpi;
1108 if (sdi->status != SR_ST_ACTIVE) {
1109 sr_err("Device inactive, can't stop acquisition.");
1113 std_session_send_df_end(sdi);
1115 g_slist_free(devc->enabled_channels);
1116 devc->enabled_channels = NULL;
1118 sr_scpi_source_remove(sdi->session, scpi);
1123 static struct sr_dev_driver rigol_ds_driver_info = {
1125 .longname = "Rigol DS",
1128 .cleanup = std_cleanup,
1130 .dev_list = std_dev_list,
1131 .dev_clear = dev_clear,
1132 .config_get = config_get,
1133 .config_set = config_set,
1134 .config_list = config_list,
1135 .dev_open = dev_open,
1136 .dev_close = dev_close,
1137 .dev_acquisition_start = dev_acquisition_start,
1138 .dev_acquisition_stop = dev_acquisition_stop,
1141 SR_REGISTER_DEV_DRIVER(rigol_ds_driver_info);