]> sigrok.org Git - libsigrok.git/blob - hardware/rigol-ds/api.c
rigol-ds: Fix divide by zero when no analog probes selected.
[libsigrok.git] / hardware / rigol-ds / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
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>
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27 #include <glib.h>
28 #include "libsigrok.h"
29 #include "libsigrok-internal.h"
30 #include "protocol.h"
31
32 static const int32_t hwopts[] = {
33         SR_CONF_CONN,
34         SR_CONF_SERIALCOMM
35 };
36
37 static const int32_t hwcaps[] = {
38         SR_CONF_OSCILLOSCOPE,
39         SR_CONF_TIMEBASE,
40         SR_CONF_TRIGGER_SOURCE,
41         SR_CONF_TRIGGER_SLOPE,
42         SR_CONF_HORIZ_TRIGGERPOS,
43         SR_CONF_NUM_TIMEBASE,
44         SR_CONF_LIMIT_FRAMES,
45         SR_CONF_SAMPLERATE,
46 };
47
48 static const int32_t analog_hwcaps[] = {
49         SR_CONF_NUM_VDIV,
50         SR_CONF_VDIV,
51         SR_CONF_COUPLING,
52         SR_CONF_DATA_SOURCE,
53 };
54
55 static const uint64_t timebases[][2] = {
56         /* nanoseconds */
57         { 1, 1000000000 },
58         { 2, 1000000000 },
59         { 5, 1000000000 },
60         { 10, 1000000000 },
61         { 20, 1000000000 },
62         { 50, 1000000000 },
63         { 100, 1000000000 },
64         { 500, 1000000000 },
65         /* microseconds */
66         { 1, 1000000 },
67         { 2, 1000000 },
68         { 5, 1000000 },
69         { 10, 1000000 },
70         { 20, 1000000 },
71         { 50, 1000000 },
72         { 100, 1000000 },
73         { 200, 1000000 },
74         { 500, 1000000 },
75         /* milliseconds */
76         { 1, 1000 },
77         { 2, 1000 },
78         { 5, 1000 },
79         { 10, 1000 },
80         { 20, 1000 },
81         { 50, 1000 },
82         { 100, 1000 },
83         { 200, 1000 },
84         { 500, 1000 },
85         /* seconds */
86         { 1, 1 },
87         { 2, 1 },
88         { 5, 1 },
89         { 10, 1 },
90         { 20, 1 },
91         { 50, 1 },
92         { 100, 1 },
93         { 200, 1 },
94         { 500, 1 },
95         { 1000, 1 },
96 };
97
98 static const uint64_t vdivs[][2] = {
99         /* microvolts */
100         { 500, 1000000 },
101         /* millivolts */
102         { 1, 1000 },
103         { 2, 1000 },
104         { 5, 1000 },
105         { 10, 1000 },
106         { 20, 1000 },
107         { 50, 1000 },
108         { 100, 1000 },
109         { 200, 1000 },
110         { 500, 1000 },
111         /* volts */
112         { 1, 1 },
113         { 2, 1 },
114         { 5, 1 },
115         { 10, 1 },
116 };
117
118 #define NUM_TIMEBASE  ARRAY_SIZE(timebases)
119 #define NUM_VDIV      ARRAY_SIZE(vdivs)
120
121 static const char *trigger_sources[] = {
122         "CH1",
123         "CH2",
124         "CH3",
125         "CH4",
126         "EXT",
127         "AC Line",
128         "D0",
129         "D1",
130         "D2",
131         "D3",
132         "D4",
133         "D5",
134         "D6",
135         "D7",
136         "D8",
137         "D9",
138         "D10",
139         "D11",
140         "D12",
141         "D13",
142         "D14",
143         "D15",
144 };
145
146 static const char *coupling[] = {
147         "AC",
148         "DC",
149         "GND",
150 };
151
152 /* Do not change the order of entries */
153 static const char *data_sources[] = {
154         "Live",
155         "Memory",
156         "Segmented",
157 };
158
159 enum vendor {
160         RIGOL,
161         AGILENT,
162 };
163
164 enum series {
165         VS5000,
166         DS1000,
167         DS2000,
168         DS2000A,
169         DSO1000,
170 };
171
172 /* short name, full name */
173 static const struct rigol_ds_vendor supported_vendors[] = {
174         [RIGOL] = {"Rigol", "Rigol Technologies"},
175         [AGILENT] = {"Agilent", "Rigol Technologies"},
176 };
177
178 #define VENDOR(x) &supported_vendors[x]
179 /* vendor, series, protocol, max timebase, min vdiv, number of horizontal divs,
180  * live waveform samples, memory buffer samples */
181 static const struct rigol_ds_series supported_series[] = {
182         [VS5000] = {VENDOR(RIGOL), "VS5000", PROTOCOL_V1, FORMAT_RAW,
183                 {50, 1}, {2, 1000}, 14, 2048, 0},
184         [DS1000] = {VENDOR(RIGOL), "DS1000", PROTOCOL_V2, FORMAT_IEEE488_2,
185                 {50, 1}, {2, 1000}, 12, 600, 1048576},
186         [DS2000] = {VENDOR(RIGOL), "DS2000", PROTOCOL_V3, FORMAT_IEEE488_2,
187                 {500, 1}, {2, 1000}, 14, 1400, 14000},
188         [DS2000A] = {VENDOR(RIGOL), "DS2000A", PROTOCOL_V3, FORMAT_IEEE488_2,
189                 {1000, 1}, {500, 1000000}, 14, 1400, 14000},
190         [DSO1000] = {VENDOR(AGILENT), "DSO1000", PROTOCOL_V3, FORMAT_IEEE488_2,
191                 {50, 1}, {2, 1000}, 12, 600, 20480},
192 };
193
194 #define SERIES(x) &supported_series[x]
195 /* series, model, min timebase, analog channels, digital */
196 static const struct rigol_ds_model supported_models[] = {
197         {SERIES(VS5000), "VS5022", {20, 1000000000}, 2, false},
198         {SERIES(VS5000), "VS5042", {10, 1000000000}, 2, false},
199         {SERIES(VS5000), "VS5062", {5, 1000000000}, 2, false},
200         {SERIES(VS5000), "VS5102", {2, 1000000000}, 2, false},
201         {SERIES(VS5000), "VS5202", {2, 1000000000}, 2, false},
202         {SERIES(VS5000), "VS5022D", {20, 1000000000}, 2, true},
203         {SERIES(VS5000), "VS5042D", {10, 1000000000}, 2, true},
204         {SERIES(VS5000), "VS5062D", {5, 1000000000}, 2, true},
205         {SERIES(VS5000), "VS5102D", {2, 1000000000}, 2, true},
206         {SERIES(VS5000), "VS5202D", {2, 1000000000}, 2, true},
207         {SERIES(DS1000), "DS1052E", {5, 1000000000}, 2, false},
208         {SERIES(DS1000), "DS1102E", {2, 1000000000}, 2, false},
209         {SERIES(DS1000), "DS1152E", {2, 1000000000}, 2, false},
210         {SERIES(DS1000), "DS1052D", {5, 1000000000}, 2, true},
211         {SERIES(DS1000), "DS1102D", {2, 1000000000}, 2, true},
212         {SERIES(DS1000), "DS1152D", {2, 1000000000}, 2, true},
213         {SERIES(DS2000), "DS2072", {5, 1000000000}, 2, false},
214         {SERIES(DS2000), "DS2102", {5, 1000000000}, 2, false},
215         {SERIES(DS2000), "DS2202", {2, 1000000000}, 2, false},
216         {SERIES(DS2000), "DS2302", {1, 1000000000}, 2, false},
217         {SERIES(DS2000A), "DS2072A", {5, 1000000000}, 2, false},
218         {SERIES(DS2000A), "DS2102A", {5, 1000000000}, 2, false},
219         {SERIES(DS2000A), "DS2202A", {2, 1000000000}, 2, false},
220         {SERIES(DS2000A), "DS2302A", {1, 1000000000}, 2, false},
221         {SERIES(DSO1000), "DSO1002A", {5, 1000000000}, 2, false},
222         {SERIES(DSO1000), "DSO1004A", {5, 1000000000}, 4, false},
223         {SERIES(DSO1000), "DSO1012A", {2, 1000000000}, 2, false},
224         {SERIES(DSO1000), "DSO1014A", {2, 1000000000}, 4, false},
225         {SERIES(DSO1000), "DSO1022A", {2, 1000000000}, 2, false},
226         {SERIES(DSO1000), "DSO1024A", {2, 1000000000}, 4, false},
227 };
228
229 SR_PRIV struct sr_dev_driver rigol_ds_driver_info;
230 static struct sr_dev_driver *di = &rigol_ds_driver_info;
231
232 static void clear_helper(void *priv)
233 {
234         struct dev_context *devc;
235
236         devc = priv;
237         g_free(devc->data);
238         g_free(devc->buffer);
239         g_free(devc->coupling[0]);
240         g_free(devc->coupling[1]);
241         g_free(devc->trigger_source);
242         g_free(devc->trigger_slope);
243         g_slist_free(devc->analog_groups[0].probes);
244         g_slist_free(devc->analog_groups[1].probes);
245         g_slist_free(devc->digital_group.probes);
246 }
247
248 static int dev_clear(void)
249 {
250         return std_dev_clear(di, clear_helper);
251 }
252
253 static int init(struct sr_context *sr_ctx)
254 {
255         return std_init(sr_ctx, di, LOG_PREFIX);
256 }
257
258 static int probe_port(const char *resource, const char *serialcomm, GSList **devices)
259 {
260         struct dev_context *devc;
261         struct sr_dev_inst *sdi;
262         struct sr_scpi_dev_inst *scpi;
263         struct sr_scpi_hw_info *hw_info;
264         struct sr_probe *probe;
265         long n[3];
266         unsigned int i;
267         const struct rigol_ds_model *model = NULL;
268         gchar *channel_name, **version;
269
270         *devices = NULL;
271
272         if (!(scpi = scpi_dev_inst_new(resource, serialcomm)))
273                 return SR_ERR;
274
275         if (sr_scpi_open(scpi) != SR_OK) {
276                 sr_info("Couldn't open SCPI device.");
277                 sr_scpi_free(scpi);
278                 return SR_ERR;
279         };
280
281         if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
282                 sr_info("Couldn't get IDN response.");
283                 sr_scpi_close(scpi);
284                 sr_scpi_free(scpi);
285                 return SR_ERR;
286         }
287
288         for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
289                 if (!strcasecmp(hw_info->manufacturer,
290                                         supported_models[i].series->vendor->full_name) &&
291                                 !strcmp(hw_info->model, supported_models[i].name)) {
292                         model = &supported_models[i];
293                         break;
294                 }
295         }
296
297         if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
298                                               model->series->vendor->name,
299                                                   model->name,
300                                                   hw_info->firmware_version))) {
301                 sr_scpi_hw_info_free(hw_info);
302                 sr_scpi_close(scpi);
303                 sr_scpi_free(scpi);
304                 return SR_ERR_NA;
305         }
306
307         sr_scpi_close(scpi);
308
309         sdi->conn = scpi;
310
311         sdi->driver = di;
312         sdi->inst_type = SR_INST_SCPI;
313
314         if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
315                 return SR_ERR_MALLOC;
316
317         devc->limit_frames = 0;
318         devc->model = model;
319         devc->format = model->series->format;
320
321         /* DS1000 models with firmware before 0.2.4 used the old data format. */
322         if (model->series == SERIES(DS1000)) {
323                 version = g_strsplit(hw_info->firmware_version, ".", 0);
324                 do {
325                         if (!version[0] || !version[1] || !version[2])
326                                 break;
327                         if (version[0][0] == 0 || version[1][0] == 0 || version[2][0] == 0)
328                                 break;
329                         for (i = 0; i < 3; i++) {
330                                 if (sr_atol(version[i], &n[i]) != SR_OK)
331                                         break;
332                         }
333                         if (i != 3)
334                                 break;
335                         if (n[0] != 0 || n[1] > 2)
336                                 break;
337                         if (n[1] == 2 && n[2] > 3)
338                                 break;
339                         sr_dbg("Found DS1000 firmware < 0.2.4, using raw data format.");
340                         devc->format = FORMAT_RAW;
341                 } while(0);
342                 g_strfreev(version);
343         }
344
345         sr_scpi_hw_info_free(hw_info);
346
347         for (i = 0; i < model->analog_channels; i++) {
348                 if (!(channel_name = g_strdup_printf("CH%d", i + 1)))
349                         return SR_ERR_MALLOC;
350                 probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name);
351                 sdi->probes = g_slist_append(sdi->probes, probe);
352                 devc->analog_groups[i].name = channel_name;
353                 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
354                 sdi->probe_groups = g_slist_append(sdi->probe_groups,
355                                 &devc->analog_groups[i]);
356         }
357
358         if (devc->model->has_digital) {
359                 for (i = 0; i < 16; i++) {
360                         if (!(channel_name = g_strdup_printf("D%d", i)))
361                                 return SR_ERR_MALLOC;
362                         probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
363                         g_free(channel_name);
364                         if (!probe)
365                                 return SR_ERR_MALLOC;
366                         sdi->probes = g_slist_append(sdi->probes, probe);
367                         devc->digital_group.probes = g_slist_append(
368                                         devc->digital_group.probes, probe);
369                 }
370                 devc->digital_group.name = "LA";
371                 sdi->probe_groups = g_slist_append(sdi->probe_groups,
372                                 &devc->digital_group);
373         }
374
375         for (i = 0; i < NUM_TIMEBASE; i++) {
376                 if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
377                         devc->timebases = &timebases[i];
378                 if (!memcmp(&devc->model->series->max_timebase, &timebases[i], sizeof(uint64_t[2])))
379                         devc->num_timebases = &timebases[i] - devc->timebases + 1;
380         }
381
382         for (i = 0; i < NUM_VDIV; i++)
383                 if (!memcmp(&devc->model->series->min_vdiv, &vdivs[i], sizeof(uint64_t[2])))
384                         devc->vdivs = &vdivs[i];
385
386         if (!(devc->buffer = g_try_malloc(ACQ_BUFFER_SIZE)))
387                 return SR_ERR_MALLOC;
388         if (!(devc->data = g_try_malloc(ACQ_BUFFER_SIZE * sizeof(float))))
389                 return SR_ERR_MALLOC;
390
391         devc->data_source = DATA_SOURCE_LIVE;
392
393         sdi->priv = devc;
394
395         *devices = g_slist_append(NULL, sdi);
396
397         return SR_OK;
398 }
399
400 static GSList *scan(GSList *options)
401 {
402         struct drv_context *drvc;
403         struct sr_config *src;
404         GSList *l, *devices;
405         GDir *dir;
406         int ret;
407         const gchar *dev_name;
408         gchar *port = NULL;
409         gchar *serialcomm = NULL;
410
411         drvc = di->priv;
412
413         for (l = options; l; l = l->next) {
414                 src = l->data;
415                 switch (src->key) {
416                 case SR_CONF_CONN:
417                         port = (char *)g_variant_get_string(src->data, NULL);
418                         break;
419                 case SR_CONF_SERIALCOMM:
420                         serialcomm = (char *)g_variant_get_string(src->data, NULL);
421                         break;
422                 }
423         }
424
425         devices = NULL;
426         if (port) {
427                 if (probe_port(port, serialcomm, &devices) == SR_ERR_MALLOC) {
428                         g_free(port);
429                         if (serialcomm)
430                                 g_free(serialcomm);
431                         return NULL;
432                 }
433         } else {
434                 if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
435                         if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
436                                 return NULL;
437                 while ((dev_name = g_dir_read_name(dir))) {
438                         if (strncmp(dev_name, "usbtmc", 6))
439                                 continue;
440                         port = g_strconcat("/dev/", dev_name, NULL);
441                         ret = probe_port(port, serialcomm, &devices);
442                         g_free(port);
443                         if (serialcomm)
444                                 g_free(serialcomm);
445                         if (ret == SR_ERR_MALLOC) {
446                                 g_dir_close(dir);
447                                 return NULL;
448                         }
449                 }
450                 g_dir_close(dir);
451         }
452
453         /* Tack a copy of the newly found devices onto the driver list. */
454         l = g_slist_copy(devices);
455         drvc->instances = g_slist_concat(drvc->instances, l);
456
457         return devices;
458 }
459
460 static GSList *dev_list(void)
461 {
462         return ((struct drv_context *)(di->priv))->instances;
463 }
464
465 static int dev_open(struct sr_dev_inst *sdi)
466 {
467         struct sr_scpi_dev_inst *scpi = sdi->conn;
468
469         if (sr_scpi_open(scpi) < 0)
470                 return SR_ERR;
471
472         if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
473                 return SR_ERR;
474
475         sdi->status = SR_ST_ACTIVE;
476
477         return SR_OK;
478 }
479
480 static int dev_close(struct sr_dev_inst *sdi)
481 {
482         struct sr_scpi_dev_inst *scpi;
483         struct dev_context *devc;
484
485         scpi = sdi->conn;
486         devc = sdi->priv;
487
488         if (devc->model->series->protocol >= PROTOCOL_V2)
489                 rigol_ds_config_set(sdi, ":KEY:LOCK DISABLE");
490
491         if (scpi) {
492                 if (sr_scpi_close(scpi) < 0)
493                         return SR_ERR;
494                 sdi->status = SR_ST_INACTIVE;
495         }
496
497         return SR_OK;
498 }
499
500 static int cleanup(void)
501 {
502         return dev_clear();
503 }
504
505 static int analog_frame_size(const struct sr_dev_inst *sdi)
506 {
507         struct dev_context *devc = sdi->priv;
508         struct sr_probe *probe;
509         int analog_probes = 0;
510         GSList *l;
511
512         for (l = sdi->probes; l; l = l->next) {
513                 probe = l->data;
514                 if (probe->type == SR_PROBE_ANALOG && probe->enabled)
515                         analog_probes++;
516         }
517
518         if (analog_probes == 0)
519                 return 0;
520
521         switch (devc->data_source) {
522         case DATA_SOURCE_LIVE:
523                 return devc->model->series->live_samples;
524         case DATA_SOURCE_MEMORY:
525                 return devc->model->series->buffer_samples / analog_probes;
526         default:
527                 return 0;
528         }
529 }
530
531 static int digital_frame_size(const struct sr_dev_inst *sdi)
532 {
533         struct dev_context *devc = sdi->priv;
534
535         switch (devc->data_source) {
536         case DATA_SOURCE_LIVE:
537                 return devc->model->series->live_samples * 2;
538         case DATA_SOURCE_MEMORY:
539                 return devc->model->series->buffer_samples * 2;
540         default:
541                 return 0;
542         }
543 }
544
545 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
546                 const struct sr_probe_group *probe_group)
547 {
548         struct dev_context *devc;
549         struct sr_probe *probe;
550         const char *tmp_str;
551         uint64_t samplerate;
552         int analog_channel = -1;
553         float smallest_diff = 0.0000000001;
554         int idx = -1;
555         unsigned i;
556
557         if (!sdi || !(devc = sdi->priv))
558                 return SR_ERR_ARG;
559
560         /* If a probe group is specified, it must be a valid one. */
561         if (probe_group && !g_slist_find(sdi->probe_groups, probe_group)) {
562                 sr_err("Invalid probe group specified.");
563                 return SR_ERR;
564         }
565
566         if (probe_group) {
567                 probe = g_slist_nth_data(probe_group->probes, 0);
568                 if (!probe)
569                         return SR_ERR;
570                 if (probe->type == SR_PROBE_ANALOG) {
571                         if (probe->name[2] < '1' || probe->name[2] > '4')
572                                 return SR_ERR;
573                         analog_channel = probe->name[2] - '1';
574                 }
575         }
576
577         switch (id) {
578         case SR_CONF_NUM_TIMEBASE:
579                 *data = g_variant_new_int32(devc->model->series->num_horizontal_divs);
580                 break;
581         case SR_CONF_NUM_VDIV:
582                 *data = g_variant_new_int32(NUM_VDIV);
583         case SR_CONF_DATA_SOURCE:
584                 if (devc->data_source == DATA_SOURCE_LIVE)
585                         *data = g_variant_new_string("Live");
586                 else if (devc->data_source == DATA_SOURCE_MEMORY)
587                         *data = g_variant_new_string("Memory");
588                 else
589                         *data = g_variant_new_string("Segmented");
590                 break;
591         case SR_CONF_SAMPLERATE:
592                 if (devc->data_source == DATA_SOURCE_LIVE) {
593                         samplerate = analog_frame_size(sdi) /
594                                 (devc->timebase * devc->model->series->num_horizontal_divs);
595                         *data = g_variant_new_uint64(samplerate);
596                 } else {
597                         return SR_ERR_NA;
598                 }
599                 break;
600         case SR_CONF_TRIGGER_SOURCE:
601                 if (!strcmp(devc->trigger_source, "ACL"))
602                         tmp_str = "AC Line";
603                 else if (!strcmp(devc->trigger_source, "CHAN1"))
604                         tmp_str = "CH1";
605                 else if (!strcmp(devc->trigger_source, "CHAN2"))
606                         tmp_str = "CH2";
607                 else if (!strcmp(devc->trigger_source, "CHAN3"))
608                         tmp_str = "CH3";
609                 else if (!strcmp(devc->trigger_source, "CHAN4"))
610                         tmp_str = "CH4";
611                 else
612                         tmp_str = devc->trigger_source;
613                 *data = g_variant_new_string(tmp_str);
614                 break;
615         case SR_CONF_TIMEBASE:
616                 for (i = 0; i < devc->num_timebases; i++) {
617                         float tb = (float)devc->timebases[i][0] / devc->timebases[i][1];
618                         float diff = fabs(devc->timebase - tb);
619                         if (diff < smallest_diff) {
620                                 smallest_diff = diff;
621                                 idx = i;
622                         }
623                 }
624                 if (idx < 0)
625                         return SR_ERR_NA;
626                 *data = g_variant_new("(tt)", devc->timebases[idx][0],
627                                               devc->timebases[idx][1]);
628                 break;
629         case SR_CONF_VDIV:
630                 if (analog_channel < 0)
631                         return SR_ERR_NA;
632                 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
633                         float vdiv = (float)vdivs[i][0] / vdivs[i][1];
634                         float diff = fabs(devc->vdiv[analog_channel] - vdiv);
635                         if (diff < smallest_diff) {
636                                 smallest_diff = diff;
637                                 idx = i;
638                         }
639                 }
640                 if (idx < 0)
641                         return SR_ERR_NA;
642                 *data = g_variant_new("(tt)", vdivs[idx][0], vdivs[idx][1]);
643                 break;
644         case SR_CONF_COUPLING:
645                 if (analog_channel < 0)
646                         return SR_ERR_NA;
647                 *data = g_variant_new_string(devc->coupling[analog_channel]);
648                 break;
649         default:
650                 return SR_ERR_NA;
651         }
652
653         return SR_OK;
654 }
655
656 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
657                 const struct sr_probe_group *probe_group)
658 {
659         struct dev_context *devc;
660         uint64_t tmp_u64, p, q;
661         double t_dbl;
662         unsigned int i, j;
663         int ret;
664         const char *tmp_str;
665         char buffer[16];
666
667         if (!(devc = sdi->priv))
668                 return SR_ERR_ARG;
669
670         if (sdi->status != SR_ST_ACTIVE)
671                 return SR_ERR_DEV_CLOSED;
672
673         /* If a probe group is specified, it must be a valid one. */
674         if (probe_group && !g_slist_find(sdi->probe_groups, probe_group)) {
675                 sr_err("Invalid probe group specified.");
676                 return SR_ERR;
677         }
678
679         ret = SR_OK;
680         switch (id) {
681         case SR_CONF_LIMIT_FRAMES:
682                 devc->limit_frames = g_variant_get_uint64(data);
683                 break;
684         case SR_CONF_TRIGGER_SLOPE:
685                 tmp_u64 = g_variant_get_uint64(data);
686                 if (tmp_u64 != 0 && tmp_u64 != 1)
687                         return SR_ERR;
688                 g_free(devc->trigger_slope);
689                 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
690                 ret = rigol_ds_config_set(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
691                 break;
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                         return SR_ERR;
696                 devc->horiz_triggerpos = t_dbl;
697                 /* We have the trigger offset as a percentage of the frame, but
698                  * need to express this in seconds. */
699                 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
700                 g_ascii_formatd(buffer, sizeof(buffer), "%.6f", t_dbl);
701                 ret = rigol_ds_config_set(sdi, ":TIM:OFFS %s", buffer);
702                 break;
703         case SR_CONF_TIMEBASE:
704                 g_variant_get(data, "(tt)", &p, &q);
705                 for (i = 0; i < devc->num_timebases; i++) {
706                         if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
707                                 devc->timebase = (float)p / q;
708                                 g_ascii_formatd(buffer, sizeof(buffer), "%.9f",
709                                                 devc->timebase);
710                                 ret = rigol_ds_config_set(sdi, ":TIM:SCAL %s", buffer);
711                                 break;
712                         }
713                 }
714                 if (i == devc->num_timebases)
715                         ret = SR_ERR_ARG;
716                 break;
717         case SR_CONF_TRIGGER_SOURCE:
718                 tmp_str = g_variant_get_string(data, NULL);
719                 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
720                         if (!strcmp(trigger_sources[i], tmp_str)) {
721                                 g_free(devc->trigger_source);
722                                 devc->trigger_source = g_strdup(trigger_sources[i]);
723                                 if (!strcmp(devc->trigger_source, "AC Line"))
724                                         tmp_str = "ACL";
725                                 else if (!strcmp(devc->trigger_source, "CH1"))
726                                         tmp_str = "CHAN1";
727                                 else if (!strcmp(devc->trigger_source, "CH2"))
728                                         tmp_str = "CHAN2";
729                                 else if (!strcmp(devc->trigger_source, "CH3"))
730                                         tmp_str = "CHAN3";
731                                 else if (!strcmp(devc->trigger_source, "CH4"))
732                                         tmp_str = "CHAN4";
733                                 else
734                                         tmp_str = (char *)devc->trigger_source;
735                                 ret = rigol_ds_config_set(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
736                                 break;
737                         }
738                 }
739                 if (i == ARRAY_SIZE(trigger_sources))
740                         ret = SR_ERR_ARG;
741                 break;
742         case SR_CONF_VDIV:
743                 if (!probe_group) {
744                         sr_err("No probe group specified.");
745                         return SR_ERR_PROBE_GROUP;
746                 }
747                 g_variant_get(data, "(tt)", &p, &q);
748                 for (i = 0; i < 2; i++) {
749                         if (probe_group == &devc->analog_groups[i]) {
750                                 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
751                                         if (vdivs[j][0] != p || vdivs[j][1] != q)
752                                                 continue;
753                                         devc->vdiv[i] = (float)p / q;
754                                         g_ascii_formatd(buffer, sizeof(buffer), "%.3f",
755                                                         devc->vdiv[i]);
756                                         return rigol_ds_config_set(sdi, ":CHAN%d:SCAL %s", i + 1,
757                                                         buffer);
758                                 }
759                                 return SR_ERR_ARG;
760                         }
761                 }
762                 return SR_ERR_NA;
763         case SR_CONF_COUPLING:
764                 if (!probe_group) {
765                         sr_err("No probe group specified.");
766                         return SR_ERR_PROBE_GROUP;
767                 }
768                 tmp_str = g_variant_get_string(data, NULL);
769                 for (i = 0; i < 2; i++) {
770                         if (probe_group == &devc->analog_groups[i]) {
771                                 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
772                                         if (!strcmp(tmp_str, coupling[j])) {
773                                                 g_free(devc->coupling[i]);
774                                                 devc->coupling[i] = g_strdup(coupling[j]);
775                                                 return rigol_ds_config_set(sdi, ":CHAN%d:COUP %s", i + 1,
776                                                                 devc->coupling[i]);
777                                         }
778                                 }
779                                 return SR_ERR_ARG;
780                         }
781                 }
782                 return SR_ERR_NA;
783         case SR_CONF_DATA_SOURCE:
784                 tmp_str = g_variant_get_string(data, NULL);
785                 if (!strcmp(tmp_str, "Live"))
786                         devc->data_source = DATA_SOURCE_LIVE;
787                 else if (devc->model->series->protocol >= PROTOCOL_V2
788                         && !strcmp(tmp_str, "Memory"))
789                         devc->data_source = DATA_SOURCE_MEMORY;
790                 else if (devc->model->series->protocol >= PROTOCOL_V3
791                          && !strcmp(tmp_str, "Segmented"))
792                         devc->data_source = DATA_SOURCE_SEGMENTED;
793                 else
794                         return SR_ERR;
795                 break;
796         default:
797                 ret = SR_ERR_NA;
798                 break;
799         }
800
801         return ret;
802 }
803
804 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
805                 const struct sr_probe_group *probe_group)
806 {
807         GVariant *tuple, *rational[2];
808         GVariantBuilder gvb;
809         unsigned int i;
810         struct dev_context *devc = NULL;
811
812         if (sdi)
813                 devc = sdi->priv;
814
815         if (key == SR_CONF_SCAN_OPTIONS) {
816                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
817                                 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
818                 return SR_OK;
819         } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) {
820                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
821                         hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
822                 return SR_OK;
823         }
824
825         /* Every other option requires a valid device instance. */
826         if (!sdi || !(devc = sdi->priv))
827                 return SR_ERR_ARG;
828
829         /* If a probe group is specified, it must be a valid one. */
830         if (probe_group) {
831                 if (probe_group != &devc->analog_groups[0]
832                                 && probe_group != &devc->analog_groups[1]) {
833                         sr_err("Invalid probe group specified.");
834                         return SR_ERR;
835                 }
836         }
837
838         switch (key) {
839         case SR_CONF_DEVICE_OPTIONS:
840                 if (!probe_group) {
841                         sr_err("No probe group specified.");
842                         return SR_ERR_PROBE_GROUP;
843                 }
844                 if (probe_group == &devc->digital_group) {
845                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
846                                 NULL, 0, sizeof(int32_t));
847                         return SR_OK;
848                 } else {
849                         for (i = 0; i < 2; i++) {
850                                 if (probe_group == &devc->analog_groups[i]) {
851                                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
852                                                 analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t));
853                                         return SR_OK;
854                                 }
855                         }
856                         return SR_ERR_NA;
857                 }
858                 break;
859         case SR_CONF_COUPLING:
860                 if (!probe_group) {
861                         sr_err("No probe group specified.");
862                         return SR_ERR_PROBE_GROUP;
863                 }
864                 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
865                 break;
866         case SR_CONF_VDIV:
867                 if (!devc)
868                         /* Can't know this until we have the exact model. */
869                         return SR_ERR_ARG;
870                 if (!probe_group) {
871                         sr_err("No probe group specified.");
872                         return SR_ERR_PROBE_GROUP;
873                 }
874                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
875                 for (i = 0; i < NUM_VDIV; i++) {
876                         rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
877                         rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
878                         tuple = g_variant_new_tuple(rational, 2);
879                         g_variant_builder_add_value(&gvb, tuple);
880                 }
881                 *data = g_variant_builder_end(&gvb);
882                 break;
883         case SR_CONF_TIMEBASE:
884                 if (!devc)
885                         /* Can't know this until we have the exact model. */
886                         return SR_ERR_ARG;
887                 if (devc->num_timebases <= 0)
888                         return SR_ERR_NA;
889                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
890                 for (i = 0; i < devc->num_timebases; i++) {
891                         rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
892                         rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
893                         tuple = g_variant_new_tuple(rational, 2);
894                         g_variant_builder_add_value(&gvb, tuple);
895                 }
896                 *data = g_variant_builder_end(&gvb);
897                 break;
898         case SR_CONF_TRIGGER_SOURCE:
899                 if (!devc)
900                         /* Can't know this until we have the exact model. */
901                         return SR_ERR_ARG;
902                 *data = g_variant_new_strv(trigger_sources,
903                                 devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
904                 break;
905         case SR_CONF_DATA_SOURCE:
906                 if (!devc)
907                         /* Can't know this until we have the exact model. */
908                         return SR_ERR_ARG;
909                 switch (devc->model->series->protocol) {
910                 case PROTOCOL_V1:
911                         *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 2);
912                         break;
913                 case PROTOCOL_V2:
914                         *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
915                         break;
916                 default:
917                         *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
918                         break;
919                 }
920                 break;
921         default:
922                 return SR_ERR_NA;
923         }
924
925         return SR_OK;
926 }
927
928 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
929 {
930         struct sr_scpi_dev_inst *scpi;
931         struct dev_context *devc;
932         struct sr_probe *probe;
933         struct sr_datafeed_packet packet;
934         GSList *l;
935
936         if (sdi->status != SR_ST_ACTIVE)
937                 return SR_ERR_DEV_CLOSED;
938
939         scpi = sdi->conn;
940         devc = sdi->priv;
941
942         devc->num_frames = 0;
943
944         for (l = sdi->probes; l; l = l->next) {
945                 probe = l->data;
946                 sr_dbg("handling probe %s", probe->name);
947                 if (probe->type == SR_PROBE_ANALOG) {
948                         if (probe->enabled)
949                                 devc->enabled_analog_probes = g_slist_append(
950                                                 devc->enabled_analog_probes, probe);
951                         if (probe->enabled != devc->analog_channels[probe->index]) {
952                                 /* Enabled channel is currently disabled, or vice versa. */
953                                 if (rigol_ds_config_set(sdi, ":CHAN%d:DISP %s", probe->index + 1,
954                                                 probe->enabled ? "ON" : "OFF") != SR_OK)
955                                         return SR_ERR;
956                                 devc->analog_channels[probe->index] = probe->enabled;
957                         }
958                 } else if (probe->type == SR_PROBE_LOGIC) {
959                         if (probe->enabled) {
960                                 devc->enabled_digital_probes = g_slist_append(
961                                                 devc->enabled_digital_probes, probe);
962                                 /* Turn on LA module if currently off. */
963                                 if (!devc->la_enabled) {
964                                         if (rigol_ds_config_set(sdi, ":LA:DISP ON") != SR_OK)
965                                                 return SR_ERR;
966                                         devc->la_enabled = TRUE;
967                                 }
968                         }
969                         if (probe->enabled != devc->digital_channels[probe->index]) {
970                                 /* Enabled channel is currently disabled, or vice versa. */
971                                 if (rigol_ds_config_set(sdi, ":DIG%d:TURN %s", probe->index,
972                                                 probe->enabled ? "ON" : "OFF") != SR_OK)
973                                         return SR_ERR;
974                                 devc->digital_channels[probe->index] = probe->enabled;
975                         }
976                 }
977         }
978
979         if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
980                 return SR_ERR;
981
982         /* Turn off LA module if on and no digital probes selected. */
983         if (devc->la_enabled && !devc->enabled_digital_probes)
984                 if (rigol_ds_config_set(sdi, ":LA:DISP OFF") != SR_OK)
985                         return SR_ERR;
986
987         /* Set memory mode. */
988         if (devc->data_source == DATA_SOURCE_SEGMENTED) {
989                 sr_err("Data source 'Segmented' not yet supported");
990                 return SR_ERR;
991         }
992
993         devc->analog_frame_size = analog_frame_size(sdi);
994         devc->digital_frame_size = digital_frame_size(sdi);
995
996         switch (devc->model->series->protocol) {
997         case PROTOCOL_V2:
998                 if (rigol_ds_config_set(sdi, ":ACQ:MDEP LONG") != SR_OK)
999                         return SR_ERR;
1000                 break;
1001         case PROTOCOL_V3:
1002                 /* Apparently for the DS2000 the memory
1003                  * depth can only be set in Running state -
1004                  * this matches the behaviour of the UI. */
1005                 if (rigol_ds_config_set(sdi, ":RUN") != SR_OK)
1006                         return SR_ERR;
1007                 if (rigol_ds_config_set(sdi, ":ACQ:MDEP %d",
1008                                         devc->analog_frame_size) != SR_OK)
1009                         return SR_ERR;
1010                 if (rigol_ds_config_set(sdi, ":STOP") != SR_OK)
1011                         return SR_ERR;
1012                 break;
1013         default:
1014                 break;
1015         }
1016
1017         if (devc->data_source == DATA_SOURCE_LIVE)
1018                 if (rigol_ds_config_set(sdi, ":RUN") != SR_OK)
1019                         return SR_ERR;
1020
1021         sr_scpi_source_add(scpi, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
1022
1023         /* Send header packet to the session bus. */
1024         std_session_send_df_header(cb_data, LOG_PREFIX);
1025
1026         if (devc->enabled_analog_probes)
1027                 devc->channel_entry = devc->enabled_analog_probes;
1028         else
1029                 devc->channel_entry = devc->enabled_digital_probes;
1030
1031         if (rigol_ds_capture_start(sdi) != SR_OK)
1032                 return SR_ERR;
1033
1034         /* Start of first frame. */
1035         packet.type = SR_DF_FRAME_BEGIN;
1036         sr_session_send(cb_data, &packet);
1037
1038         return SR_OK;
1039 }
1040
1041 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
1042 {
1043         struct dev_context *devc;
1044         struct sr_scpi_dev_inst *scpi;
1045         struct sr_datafeed_packet packet;
1046
1047         (void)cb_data;
1048
1049         devc = sdi->priv;
1050
1051         if (sdi->status != SR_ST_ACTIVE) {
1052                 sr_err("Device inactive, can't stop acquisition.");
1053                 return SR_ERR;
1054         }
1055
1056         /* End of last frame. */
1057         packet.type = SR_DF_END;
1058         sr_session_send(sdi, &packet);
1059
1060         g_slist_free(devc->enabled_analog_probes);
1061         g_slist_free(devc->enabled_digital_probes);
1062         devc->enabled_analog_probes = NULL;
1063         devc->enabled_digital_probes = NULL;
1064         scpi = sdi->conn;
1065         sr_scpi_source_remove(scpi);
1066
1067         return SR_OK;
1068 }
1069
1070 SR_PRIV struct sr_dev_driver rigol_ds_driver_info = {
1071         .name = "rigol-ds",
1072         .longname = "Rigol DS",
1073         .api_version = 1,
1074         .init = init,
1075         .cleanup = cleanup,
1076         .scan = scan,
1077         .dev_list = dev_list,
1078         .dev_clear = dev_clear,
1079         .config_get = config_get,
1080         .config_set = config_set,
1081         .config_list = config_list,
1082         .dev_open = dev_open,
1083         .dev_close = dev_close,
1084         .dev_acquisition_start = dev_acquisition_start,
1085         .dev_acquisition_stop = dev_acquisition_stop,
1086         .priv = NULL,
1087 };