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 devopts_cg_analog[] = {
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 static const char *trigger_sources_2_chans[] = {
132 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
133 "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
136 static const char *trigger_sources_4_chans[] = {
137 "CH1", "CH2", "CH3", "CH4",
139 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
140 "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
143 static const char *trigger_slopes[] = {
147 static const char *coupling[] = {
151 static const uint64_t probe_factor[] = {
152 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000,
155 /* Do not change the order of entries */
156 static const char *data_sources[] = {
177 /* short name, full name */
178 static const struct rigol_ds_vendor supported_vendors[] = {
179 [RIGOL] = {"Rigol", "Rigol Technologies"},
180 [AGILENT] = {"Agilent", "Agilent Technologies"},
183 #define VENDOR(x) &supported_vendors[x]
184 /* vendor, series/name, protocol, data format, max timebase, min vdiv,
185 * number of horizontal divs, live waveform samples, memory buffer samples */
186 static const struct rigol_ds_series supported_series[] = {
187 [VS5000] = {VENDOR(RIGOL), "VS5000", PROTOCOL_V1, FORMAT_RAW,
188 {50, 1}, {2, 1000}, 14, 2048, 0},
189 [DS1000] = {VENDOR(RIGOL), "DS1000", PROTOCOL_V2, FORMAT_IEEE488_2,
190 {50, 1}, {2, 1000}, 12, 600, 1048576},
191 [DS2000] = {VENDOR(RIGOL), "DS2000", PROTOCOL_V3, FORMAT_IEEE488_2,
192 {500, 1}, {500, 1000000}, 14, 1400, 14000},
193 [DS2000A] = {VENDOR(RIGOL), "DS2000A", PROTOCOL_V3, FORMAT_IEEE488_2,
194 {1000, 1}, {500, 1000000}, 14, 1400, 14000},
195 [DSO1000] = {VENDOR(AGILENT), "DSO1000", PROTOCOL_V3, FORMAT_IEEE488_2,
196 {50, 1}, {2, 1000}, 12, 600, 20480},
197 [DS1000Z] = {VENDOR(RIGOL), "DS1000Z", PROTOCOL_V4, FORMAT_IEEE488_2,
198 {50, 1}, {1, 1000}, 12, 1200, 12000000},
199 [DS4000] = {VENDOR(RIGOL), "DS4000", PROTOCOL_V4, FORMAT_IEEE488_2,
200 {1000, 1}, {1, 1000}, 14, 1400, 14000},
203 #define SERIES(x) &supported_series[x]
205 * Use a macro to select the correct list of trigger sources and its length
206 * based on the number of analog channels and presence of digital channels.
208 #define CH_INFO(num, digital) \
209 num, digital, trigger_sources_##num##_chans, \
210 digital ? ARRAY_SIZE(trigger_sources_##num##_chans) : (num + 2)
211 /* series, model, min timebase, analog channels, digital */
212 static const struct rigol_ds_model supported_models[] = {
213 {SERIES(VS5000), "VS5022", {20, 1000000000}, CH_INFO(2, false)},
214 {SERIES(VS5000), "VS5042", {10, 1000000000}, CH_INFO(2, false)},
215 {SERIES(VS5000), "VS5062", {5, 1000000000}, CH_INFO(2, false)},
216 {SERIES(VS5000), "VS5102", {2, 1000000000}, CH_INFO(2, false)},
217 {SERIES(VS5000), "VS5202", {2, 1000000000}, CH_INFO(2, false)},
218 {SERIES(VS5000), "VS5022D", {20, 1000000000}, CH_INFO(2, true)},
219 {SERIES(VS5000), "VS5042D", {10, 1000000000}, CH_INFO(2, true)},
220 {SERIES(VS5000), "VS5062D", {5, 1000000000}, CH_INFO(2, true)},
221 {SERIES(VS5000), "VS5102D", {2, 1000000000}, CH_INFO(2, true)},
222 {SERIES(VS5000), "VS5202D", {2, 1000000000}, CH_INFO(2, true)},
223 {SERIES(DS1000), "DS1052E", {5, 1000000000}, CH_INFO(2, false)},
224 {SERIES(DS1000), "DS1102E", {2, 1000000000}, CH_INFO(2, false)},
225 {SERIES(DS1000), "DS1152E", {2, 1000000000}, CH_INFO(2, false)},
226 {SERIES(DS1000), "DS1052D", {5, 1000000000}, CH_INFO(2, true)},
227 {SERIES(DS1000), "DS1102D", {2, 1000000000}, CH_INFO(2, true)},
228 {SERIES(DS1000), "DS1152D", {2, 1000000000}, CH_INFO(2, true)},
229 {SERIES(DS2000), "DS2072", {5, 1000000000}, CH_INFO(2, false)},
230 {SERIES(DS2000), "DS2102", {5, 1000000000}, CH_INFO(2, false)},
231 {SERIES(DS2000), "DS2202", {2, 1000000000}, CH_INFO(2, false)},
232 {SERIES(DS2000), "DS2302", {1, 1000000000}, CH_INFO(2, false)},
233 {SERIES(DS2000A), "DS2072A", {5, 1000000000}, CH_INFO(2, false)},
234 {SERIES(DS2000A), "DS2102A", {5, 1000000000}, CH_INFO(2, false)},
235 {SERIES(DS2000A), "DS2202A", {2, 1000000000}, CH_INFO(2, false)},
236 {SERIES(DS2000A), "DS2302A", {1, 1000000000}, CH_INFO(2, false)},
237 {SERIES(DS2000A), "MSO2072A", {5, 1000000000}, CH_INFO(2, true)},
238 {SERIES(DS2000A), "MSO2102A", {5, 1000000000}, CH_INFO(2, true)},
239 {SERIES(DS2000A), "MSO2202A", {2, 1000000000}, CH_INFO(2, true)},
240 {SERIES(DS2000A), "MSO2302A", {1, 1000000000}, CH_INFO(2, true)},
241 {SERIES(DSO1000), "DSO1002A", {5, 1000000000}, CH_INFO(2, false)},
242 {SERIES(DSO1000), "DSO1004A", {5, 1000000000}, CH_INFO(4, false)},
243 {SERIES(DSO1000), "DSO1012A", {2, 1000000000}, CH_INFO(2, false)},
244 {SERIES(DSO1000), "DSO1014A", {2, 1000000000}, CH_INFO(4, false)},
245 {SERIES(DSO1000), "DSO1022A", {2, 1000000000}, CH_INFO(2, false)},
246 {SERIES(DSO1000), "DSO1024A", {2, 1000000000}, CH_INFO(4, false)},
247 {SERIES(DS1000Z), "DS1054Z", {5, 1000000000}, CH_INFO(4, false)},
248 {SERIES(DS1000Z), "DS1074Z", {5, 1000000000}, CH_INFO(4, false)},
249 {SERIES(DS1000Z), "DS1104Z", {5, 1000000000}, CH_INFO(4, false)},
250 {SERIES(DS1000Z), "DS1074Z-S", {5, 1000000000}, CH_INFO(4, false)},
251 {SERIES(DS1000Z), "DS1104Z-S", {5, 1000000000}, CH_INFO(4, false)},
252 {SERIES(DS1000Z), "DS1074Z Plus", {5, 1000000000}, CH_INFO(4, false)},
253 {SERIES(DS1000Z), "DS1104Z Plus", {5, 1000000000}, CH_INFO(4, false)},
254 {SERIES(DS1000Z), "MSO1074Z", {5, 1000000000}, CH_INFO(4, true)},
255 {SERIES(DS1000Z), "MSO1104Z", {5, 1000000000}, CH_INFO(4, true)},
256 {SERIES(DS1000Z), "MSO1074Z-S", {5, 1000000000}, CH_INFO(4, true)},
257 {SERIES(DS1000Z), "MSO1104Z-S", {5, 1000000000}, CH_INFO(4, true)},
258 {SERIES(DS4000), "DS4024", {1, 1000000000}, CH_INFO(4, false)},
261 static struct sr_dev_driver rigol_ds_driver_info;
263 static void clear_helper(struct dev_context *devc)
268 g_free(devc->buffer);
269 for (i = 0; i < ARRAY_SIZE(devc->coupling); i++)
270 g_free(devc->coupling[i]);
271 g_free(devc->trigger_source);
272 g_free(devc->trigger_slope);
273 g_free(devc->analog_groups);
276 static int dev_clear(const struct sr_dev_driver *di)
278 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
281 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
283 struct dev_context *devc;
284 struct sr_dev_inst *sdi;
285 struct sr_scpi_hw_info *hw_info;
286 struct sr_channel *ch;
289 const struct rigol_ds_model *model = NULL;
290 gchar *channel_name, **version;
292 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
293 sr_info("Couldn't get IDN response, retrying.");
296 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
297 sr_info("Couldn't get IDN response.");
302 for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
303 if (!g_ascii_strcasecmp(hw_info->manufacturer,
304 supported_models[i].series->vendor->full_name) &&
305 !strcmp(hw_info->model, supported_models[i].name)) {
306 model = &supported_models[i];
312 sr_scpi_hw_info_free(hw_info);
316 sdi = g_malloc0(sizeof(struct sr_dev_inst));
317 sdi->vendor = g_strdup(model->series->vendor->name);
318 sdi->model = g_strdup(model->name);
319 sdi->version = g_strdup(hw_info->firmware_version);
321 sdi->driver = &rigol_ds_driver_info;
322 sdi->inst_type = SR_INST_SCPI;
323 sdi->serial_num = g_strdup(hw_info->serial_number);
324 devc = g_malloc0(sizeof(struct dev_context));
325 devc->limit_frames = 0;
327 devc->format = model->series->format;
329 /* DS1000 models with firmware before 0.2.4 used the old data format. */
330 if (model->series == SERIES(DS1000)) {
331 version = g_strsplit(hw_info->firmware_version, ".", 0);
333 if (!version[0] || !version[1] || !version[2])
335 if (version[0][0] == 0 || version[1][0] == 0 || version[2][0] == 0)
337 for (i = 0; i < 3; i++) {
338 if (sr_atol(version[i], &n[i]) != SR_OK)
343 scpi->firmware_version = n[0] * 100 + n[1] * 10 + n[2];
344 if (scpi->firmware_version < 24) {
345 sr_dbg("Found DS1000 firmware < 0.2.4, using raw data format.");
346 devc->format = FORMAT_RAW;
353 sr_scpi_hw_info_free(hw_info);
355 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
356 model->analog_channels);
358 for (i = 0; i < model->analog_channels; i++) {
359 channel_name = g_strdup_printf("CH%d", i + 1);
360 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_name);
362 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
364 devc->analog_groups[i]->name = channel_name;
365 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
366 sdi->channel_groups = g_slist_append(sdi->channel_groups,
367 devc->analog_groups[i]);
370 if (devc->model->has_digital) {
371 devc->digital_group = g_malloc0(sizeof(struct sr_channel_group));
373 for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) {
374 channel_name = g_strdup_printf("D%d", i);
375 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
376 g_free(channel_name);
377 devc->digital_group->channels = g_slist_append(
378 devc->digital_group->channels, ch);
380 devc->digital_group->name = g_strdup("LA");
381 sdi->channel_groups = g_slist_append(sdi->channel_groups,
382 devc->digital_group);
385 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
386 if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
387 devc->timebases = &timebases[i];
388 if (!memcmp(&devc->model->series->max_timebase, &timebases[i], sizeof(uint64_t[2])))
389 devc->num_timebases = &timebases[i] - devc->timebases + 1;
392 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
393 if (!memcmp(&devc->model->series->min_vdiv,
394 &vdivs[i], sizeof(uint64_t[2]))) {
395 devc->vdivs = &vdivs[i];
396 devc->num_vdivs = ARRAY_SIZE(vdivs) - i;
400 devc->buffer = g_malloc(ACQ_BUFFER_SIZE);
401 devc->data = g_malloc(ACQ_BUFFER_SIZE * sizeof(float));
403 devc->data_source = DATA_SOURCE_LIVE;
410 static GSList *scan(struct sr_dev_driver *di, GSList *options)
412 return sr_scpi_scan(di->context, options, probe_device);
415 static int dev_open(struct sr_dev_inst *sdi)
418 struct sr_scpi_dev_inst *scpi = sdi->conn;
420 if ((ret = sr_scpi_open(scpi)) < 0) {
421 sr_err("Failed to open SCPI device: %s.", sr_strerror(ret));
425 if ((ret = rigol_ds_get_dev_cfg(sdi)) < 0) {
426 sr_err("Failed to get device config: %s.", sr_strerror(ret));
433 static int dev_close(struct sr_dev_inst *sdi)
435 struct sr_scpi_dev_inst *scpi;
436 struct dev_context *devc;
444 if (devc->model->series->protocol == PROTOCOL_V2)
445 rigol_ds_config_set(sdi, ":KEY:LOCK DISABLE");
447 return sr_scpi_close(scpi);
450 static int analog_frame_size(const struct sr_dev_inst *sdi)
452 struct dev_context *devc = sdi->priv;
453 struct sr_channel *ch;
454 int analog_channels = 0;
457 for (l = sdi->channels; l; l = l->next) {
459 if (ch->type == SR_CHANNEL_ANALOG && ch->enabled)
463 if (analog_channels == 0)
466 switch (devc->data_source) {
467 case DATA_SOURCE_LIVE:
468 return devc->model->series->live_samples;
469 case DATA_SOURCE_MEMORY:
470 return devc->model->series->buffer_samples / analog_channels;
476 static int digital_frame_size(const struct sr_dev_inst *sdi)
478 struct dev_context *devc = sdi->priv;
480 switch (devc->data_source) {
481 case DATA_SOURCE_LIVE:
482 return devc->model->series->live_samples * 2;
483 case DATA_SOURCE_MEMORY:
484 return devc->model->series->buffer_samples * 2;
490 static int config_get(uint32_t key, GVariant **data,
491 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
493 struct dev_context *devc;
494 struct sr_channel *ch;
497 int analog_channel = -1;
498 float smallest_diff = INFINITY;
507 /* If a channel group is specified, it must be a valid one. */
508 if (cg && !g_slist_find(sdi->channel_groups, cg)) {
509 sr_err("Invalid channel group specified.");
514 ch = g_slist_nth_data(cg->channels, 0);
517 if (ch->type == SR_CHANNEL_ANALOG) {
518 if (ch->name[2] < '1' || ch->name[2] > '4')
520 analog_channel = ch->name[2] - '1';
525 case SR_CONF_NUM_HDIV:
526 *data = g_variant_new_int32(devc->model->series->num_horizontal_divs);
528 case SR_CONF_NUM_VDIV:
529 *data = g_variant_new_int32(devc->num_vdivs);
531 case SR_CONF_DATA_SOURCE:
532 if (devc->data_source == DATA_SOURCE_LIVE)
533 *data = g_variant_new_string("Live");
534 else if (devc->data_source == DATA_SOURCE_MEMORY)
535 *data = g_variant_new_string("Memory");
537 *data = g_variant_new_string("Segmented");
539 case SR_CONF_SAMPLERATE:
540 if (devc->data_source == DATA_SOURCE_LIVE) {
541 samplerate = analog_frame_size(sdi) /
542 (devc->timebase * devc->model->series->num_horizontal_divs);
543 *data = g_variant_new_uint64(samplerate);
545 sr_dbg("Unknown data source: %d.", devc->data_source);
549 case SR_CONF_TRIGGER_SOURCE:
550 if (!strcmp(devc->trigger_source, "ACL"))
552 else if (!strcmp(devc->trigger_source, "CHAN1"))
554 else if (!strcmp(devc->trigger_source, "CHAN2"))
556 else if (!strcmp(devc->trigger_source, "CHAN3"))
558 else if (!strcmp(devc->trigger_source, "CHAN4"))
561 tmp_str = devc->trigger_source;
562 *data = g_variant_new_string(tmp_str);
564 case SR_CONF_TRIGGER_SLOPE:
565 if (!strncmp(devc->trigger_slope, "POS", 3)) {
567 } else if (!strncmp(devc->trigger_slope, "NEG", 3)) {
570 sr_dbg("Unknown trigger slope: '%s'.", devc->trigger_slope);
573 *data = g_variant_new_string(tmp_str);
575 case SR_CONF_TRIGGER_LEVEL:
576 *data = g_variant_new_double(devc->trigger_level);
578 case SR_CONF_TIMEBASE:
579 for (i = 0; i < devc->num_timebases; i++) {
580 float tb = (float)devc->timebases[i][0] / devc->timebases[i][1];
581 float diff = fabs(devc->timebase - tb);
582 if (diff < smallest_diff) {
583 smallest_diff = diff;
588 sr_dbg("Negative timebase index: %d.", idx);
591 *data = g_variant_new("(tt)", devc->timebases[idx][0],
592 devc->timebases[idx][1]);
595 if (analog_channel < 0) {
596 sr_dbg("Negative analog channel: %d.", analog_channel);
599 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
600 float vdiv = (float)vdivs[i][0] / vdivs[i][1];
601 float diff = fabs(devc->vdiv[analog_channel] - vdiv);
602 if (diff < smallest_diff) {
603 smallest_diff = diff;
608 sr_dbg("Negative vdiv index: %d.", idx);
611 *data = g_variant_new("(tt)", vdivs[idx][0], vdivs[idx][1]);
613 case SR_CONF_COUPLING:
614 if (analog_channel < 0) {
615 sr_dbg("Negative analog channel: %d.", analog_channel);
618 *data = g_variant_new_string(devc->coupling[analog_channel]);
620 case SR_CONF_PROBE_FACTOR:
621 if (analog_channel < 0) {
622 sr_dbg("Negative analog channel: %d.", analog_channel);
625 *data = g_variant_new_uint64(devc->attenuation[analog_channel]);
634 static int config_set(uint32_t key, GVariant *data,
635 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
637 struct dev_context *devc;
646 /* If a channel group is specified, it must be a valid one. */
647 if (cg && !g_slist_find(sdi->channel_groups, cg)) {
648 sr_err("Invalid channel group specified.");
653 case SR_CONF_LIMIT_FRAMES:
654 devc->limit_frames = g_variant_get_uint64(data);
656 case SR_CONF_TRIGGER_SLOPE:
657 if ((idx = std_str_idx(data, ARRAY_AND_SIZE(trigger_slopes))) < 0)
659 g_free(devc->trigger_slope);
660 devc->trigger_slope = g_strdup((trigger_slopes[idx][0] == 'r') ? "POS" : "NEG");
661 return rigol_ds_config_set(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
662 case SR_CONF_HORIZ_TRIGGERPOS:
663 t_dbl = g_variant_get_double(data);
664 if (t_dbl < 0.0 || t_dbl > 1.0) {
665 sr_err("Invalid horiz. trigger position: %g.", t_dbl);
668 devc->horiz_triggerpos = t_dbl;
669 /* We have the trigger offset as a percentage of the frame, but
670 * need to express this in seconds. */
671 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
672 g_ascii_formatd(buffer, sizeof(buffer), "%.6f", t_dbl);
673 return rigol_ds_config_set(sdi, ":TIM:OFFS %s", buffer);
674 case SR_CONF_TRIGGER_LEVEL:
675 t_dbl = g_variant_get_double(data);
676 g_ascii_formatd(buffer, sizeof(buffer), "%.3f", t_dbl);
677 ret = rigol_ds_config_set(sdi, ":TRIG:EDGE:LEV %s", buffer);
679 devc->trigger_level = t_dbl;
681 case SR_CONF_TIMEBASE:
682 if ((idx = std_u64_tuple_idx(data, devc->timebases, devc->num_timebases)) < 0)
684 devc->timebase = (float)devc->timebases[idx][0] / devc->timebases[idx][1];
685 g_ascii_formatd(buffer, sizeof(buffer), "%.9f",
687 return rigol_ds_config_set(sdi, ":TIM:SCAL %s", buffer);
688 case SR_CONF_TRIGGER_SOURCE:
689 if ((idx = std_str_idx(data, devc->model->trigger_sources, devc->model->num_trigger_sources)) < 0)
691 g_free(devc->trigger_source);
692 devc->trigger_source = g_strdup(devc->model->trigger_sources[idx]);
693 if (!strcmp(devc->trigger_source, "AC Line"))
695 else if (!strcmp(devc->trigger_source, "CH1"))
697 else if (!strcmp(devc->trigger_source, "CH2"))
699 else if (!strcmp(devc->trigger_source, "CH3"))
701 else if (!strcmp(devc->trigger_source, "CH4"))
704 tmp_str = (char *)devc->trigger_source;
705 return rigol_ds_config_set(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
708 return SR_ERR_CHANNEL_GROUP;
709 if ((i = std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels)) < 0)
711 if ((idx = std_u64_tuple_idx(data, ARRAY_AND_SIZE(vdivs))) < 0)
713 devc->vdiv[i] = (float)vdivs[idx][0] / vdivs[idx][1];
714 g_ascii_formatd(buffer, sizeof(buffer), "%.3f", devc->vdiv[i]);
715 return rigol_ds_config_set(sdi, ":CHAN%d:SCAL %s", i + 1, buffer);
716 case SR_CONF_COUPLING:
718 return SR_ERR_CHANNEL_GROUP;
719 if ((i = std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels)) < 0)
721 if ((idx = std_str_idx(data, ARRAY_AND_SIZE(coupling))) < 0)
723 g_free(devc->coupling[i]);
724 devc->coupling[i] = g_strdup(coupling[idx]);
725 return rigol_ds_config_set(sdi, ":CHAN%d:COUP %s", i + 1, devc->coupling[i]);
726 case SR_CONF_PROBE_FACTOR:
728 return SR_ERR_CHANNEL_GROUP;
729 if ((i = std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels)) < 0)
731 if ((idx = std_u64_idx(data, ARRAY_AND_SIZE(probe_factor))) < 0)
733 p = g_variant_get_uint64(data);
734 devc->attenuation[i] = probe_factor[idx];
735 ret = rigol_ds_config_set(sdi, ":CHAN%d:PROB %"PRIu64, i + 1, p);
737 rigol_ds_get_dev_cfg_vertical(sdi);
739 case SR_CONF_DATA_SOURCE:
740 tmp_str = g_variant_get_string(data, NULL);
741 if (!strcmp(tmp_str, "Live"))
742 devc->data_source = DATA_SOURCE_LIVE;
743 else if (devc->model->series->protocol >= PROTOCOL_V2
744 && !strcmp(tmp_str, "Memory"))
745 devc->data_source = DATA_SOURCE_MEMORY;
746 else if (devc->model->series->protocol >= PROTOCOL_V3
747 && !strcmp(tmp_str, "Segmented"))
748 devc->data_source = DATA_SOURCE_SEGMENTED;
750 sr_err("Unknown data source: '%s'.", tmp_str);
761 static int config_list(uint32_t key, GVariant **data,
762 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
764 struct dev_context *devc;
766 devc = (sdi) ? sdi->priv : NULL;
769 case SR_CONF_SCAN_OPTIONS:
770 case SR_CONF_DEVICE_OPTIONS:
772 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
775 if (cg == devc->digital_group) {
776 *data = std_gvar_array_u32(NULL, 0);
779 if (std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels) < 0)
781 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog));
785 case SR_CONF_COUPLING:
787 return SR_ERR_CHANNEL_GROUP;
788 *data = g_variant_new_strv(ARRAY_AND_SIZE(coupling));
790 case SR_CONF_PROBE_FACTOR:
792 return SR_ERR_CHANNEL_GROUP;
793 *data = std_gvar_array_u64(ARRAY_AND_SIZE(probe_factor));
797 /* Can't know this until we have the exact model. */
800 return SR_ERR_CHANNEL_GROUP;
801 *data = std_gvar_tuple_array(devc->vdivs, devc->num_vdivs);
803 case SR_CONF_TIMEBASE:
805 /* Can't know this until we have the exact model. */
807 if (devc->num_timebases <= 0)
809 *data = std_gvar_tuple_array(devc->timebases, devc->num_timebases);
811 case SR_CONF_TRIGGER_SOURCE:
813 /* Can't know this until we have the exact model. */
815 *data = g_variant_new_strv(devc->model->trigger_sources, devc->model->num_trigger_sources);
817 case SR_CONF_TRIGGER_SLOPE:
818 *data = g_variant_new_strv(ARRAY_AND_SIZE(trigger_slopes));
820 case SR_CONF_DATA_SOURCE:
822 /* Can't know this until we have the exact model. */
824 switch (devc->model->series->protocol) {
826 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 2);
829 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
832 *data = g_variant_new_strv(ARRAY_AND_SIZE(data_sources));
843 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
845 struct sr_scpi_dev_inst *scpi;
846 struct dev_context *devc;
847 struct sr_channel *ch;
848 struct sr_datafeed_packet packet;
849 gboolean some_digital;
855 devc->num_frames = 0;
857 some_digital = FALSE;
858 for (l = sdi->channels; l; l = l->next) {
860 sr_dbg("handling channel %s", ch->name);
861 if (ch->type == SR_CHANNEL_ANALOG) {
863 devc->enabled_channels = g_slist_append(
864 devc->enabled_channels, ch);
865 if (ch->enabled != devc->analog_channels[ch->index]) {
866 /* Enabled channel is currently disabled, or vice versa. */
867 if (rigol_ds_config_set(sdi, ":CHAN%d:DISP %s", ch->index + 1,
868 ch->enabled ? "ON" : "OFF") != SR_OK)
870 devc->analog_channels[ch->index] = ch->enabled;
872 } else if (ch->type == SR_CHANNEL_LOGIC) {
873 /* Only one list entry for older protocols. All channels are
874 * retrieved together when this entry is processed. */
876 devc->model->series->protocol > PROTOCOL_V3 ||
878 devc->enabled_channels = g_slist_append(
879 devc->enabled_channels, ch);
882 /* Turn on LA module if currently off. */
883 if (!devc->la_enabled) {
884 if (rigol_ds_config_set(sdi,
885 devc->model->series->protocol >= PROTOCOL_V3 ?
886 ":LA:STAT ON" : ":LA:DISP ON") != SR_OK)
888 devc->la_enabled = TRUE;
891 if (ch->enabled != devc->digital_channels[ch->index]) {
892 /* Enabled channel is currently disabled, or vice versa. */
893 if (rigol_ds_config_set(sdi,
894 devc->model->series->protocol >= PROTOCOL_V3 ?
895 ":LA:DIG%d:DISP %s" : ":DIG%d:TURN %s", ch->index,
896 ch->enabled ? "ON" : "OFF") != SR_OK)
898 devc->digital_channels[ch->index] = ch->enabled;
903 if (!devc->enabled_channels)
906 /* Turn off LA module if on and no digital channels selected. */
907 if (devc->la_enabled && !some_digital)
908 if (rigol_ds_config_set(sdi,
909 devc->model->series->protocol >= PROTOCOL_V3 ?
910 ":LA:STAT OFF" : ":LA:DISP OFF") != SR_OK)
913 /* Set memory mode. */
914 if (devc->data_source == DATA_SOURCE_SEGMENTED) {
915 sr_err("Data source 'Segmented' not yet supported");
919 devc->analog_frame_size = analog_frame_size(sdi);
920 devc->digital_frame_size = digital_frame_size(sdi);
922 switch (devc->model->series->protocol) {
924 if (rigol_ds_config_set(sdi, ":ACQ:MEMD LONG") != SR_OK)
928 /* Apparently for the DS2000 the memory
929 * depth can only be set in Running state -
930 * this matches the behaviour of the UI. */
931 if (rigol_ds_config_set(sdi, ":RUN") != SR_OK)
933 if (rigol_ds_config_set(sdi, ":ACQ:MDEP %d",
934 devc->analog_frame_size) != SR_OK)
936 if (rigol_ds_config_set(sdi, ":STOP") != SR_OK)
943 if (devc->data_source == DATA_SOURCE_LIVE)
944 if (rigol_ds_config_set(sdi, ":RUN") != SR_OK)
947 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
948 rigol_ds_receive, (void *)sdi);
950 std_session_send_df_header(sdi);
952 devc->channel_entry = devc->enabled_channels;
954 if (rigol_ds_capture_start(sdi) != SR_OK)
957 /* Start of first frame. */
958 packet.type = SR_DF_FRAME_BEGIN;
959 sr_session_send(sdi, &packet);
964 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
966 struct dev_context *devc;
967 struct sr_scpi_dev_inst *scpi;
971 std_session_send_df_end(sdi);
973 g_slist_free(devc->enabled_channels);
974 devc->enabled_channels = NULL;
976 sr_scpi_source_remove(sdi->session, scpi);
981 static struct sr_dev_driver rigol_ds_driver_info = {
983 .longname = "Rigol DS",
986 .cleanup = std_cleanup,
988 .dev_list = std_dev_list,
989 .dev_clear = dev_clear,
990 .config_get = config_get,
991 .config_set = config_set,
992 .config_list = config_list,
993 .dev_open = dev_open,
994 .dev_close = dev_close,
995 .dev_acquisition_start = dev_acquisition_start,
996 .dev_acquisition_stop = dev_acquisition_stop,
999 SR_REGISTER_DEV_DRIVER(rigol_ds_driver_info);