]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2018 mhooijboer <marchelh@gmail.com> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
21 | #include <fcntl.h> | |
22 | #include <glib.h> | |
23 | #include <math.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <strings.h> | |
27 | #include <unistd.h> | |
28 | #include <libsigrok/libsigrok.h> | |
29 | #include "libsigrok-internal.h" | |
30 | #include "protocol.h" | |
31 | #include "scpi.h" | |
32 | ||
33 | static const uint32_t scanopts[] = { | |
34 | SR_CONF_CONN, | |
35 | SR_CONF_SERIALCOMM, | |
36 | }; | |
37 | ||
38 | static const uint32_t drvopts[] = { | |
39 | SR_CONF_OSCILLOSCOPE, | |
40 | SR_CONF_LOGIC_ANALYZER, | |
41 | }; | |
42 | ||
43 | static const uint32_t devopts[] = { | |
44 | SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
45 | SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
46 | SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
47 | SR_CONF_TRIGGER_LEVEL | SR_CONF_GET | SR_CONF_SET, | |
48 | SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET, | |
49 | SR_CONF_NUM_HDIV | SR_CONF_GET | SR_CONF_LIST, | |
50 | SR_CONF_SAMPLERATE | SR_CONF_GET, | |
51 | SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET, | |
52 | SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
53 | SR_CONF_AVERAGING | SR_CONF_GET | SR_CONF_SET, | |
54 | SR_CONF_AVG_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
55 | }; | |
56 | ||
57 | static const uint32_t devopts_cg_analog[] = { | |
58 | SR_CONF_NUM_VDIV | SR_CONF_GET, | |
59 | SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
60 | SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
61 | SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
62 | SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
63 | SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
64 | }; | |
65 | ||
66 | static const uint64_t timebases[][2] = { | |
67 | /* nanoseconds */ | |
68 | { 1, 1000000000 }, | |
69 | { 2, 1000000000 }, | |
70 | { 5, 1000000000 }, | |
71 | { 10, 1000000000 }, | |
72 | { 20, 1000000000 }, | |
73 | { 50, 1000000000 }, | |
74 | { 100, 1000000000 }, | |
75 | { 200, 1000000000 }, | |
76 | { 500, 1000000000 }, | |
77 | /* microseconds */ | |
78 | { 1, 1000000 }, | |
79 | { 2, 1000000 }, | |
80 | { 5, 1000000 }, | |
81 | { 10, 1000000 }, | |
82 | { 20, 1000000 }, | |
83 | { 50, 1000000 }, | |
84 | { 100, 1000000 }, | |
85 | { 200, 1000000 }, | |
86 | { 500, 1000000 }, | |
87 | /* milliseconds */ | |
88 | { 1, 1000 }, | |
89 | { 2, 1000 }, | |
90 | { 5, 1000 }, | |
91 | { 10, 1000 }, | |
92 | { 20, 1000 }, | |
93 | { 50, 1000 }, | |
94 | { 100, 1000 }, | |
95 | { 200, 1000 }, | |
96 | { 500, 1000 }, | |
97 | /* seconds */ | |
98 | { 1, 1 }, | |
99 | { 2, 1 }, | |
100 | { 5, 1 }, | |
101 | { 10, 1 }, | |
102 | { 20, 1 }, | |
103 | { 50, 1 }, | |
104 | { 100, 1 }, | |
105 | }; | |
106 | ||
107 | static const uint64_t vdivs[][2] = { | |
108 | /* microvolts */ | |
109 | { 500, 100000 }, | |
110 | /* millivolts */ | |
111 | { 1, 1000 }, | |
112 | { 2, 1000 }, | |
113 | { 5, 1000 }, | |
114 | { 10, 1000 }, | |
115 | { 20, 1000 }, | |
116 | { 50, 1000 }, | |
117 | { 100, 1000 }, | |
118 | { 200, 1000 }, | |
119 | { 500, 1000 }, | |
120 | /* volts */ | |
121 | { 1, 1 }, | |
122 | { 2, 1 }, | |
123 | { 5, 1 }, | |
124 | { 10, 1 }, | |
125 | { 20, 1 }, | |
126 | { 50, 1 }, | |
127 | { 100, 1 }, | |
128 | }; | |
129 | ||
130 | static const char *trigger_sources[] = { | |
131 | "CH1", "CH2", "Ext", "Ext /5", "AC Line", | |
132 | "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", | |
133 | "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15", | |
134 | }; | |
135 | ||
136 | static const char *trigger_slopes[] = { | |
137 | "r", "f", | |
138 | }; | |
139 | ||
140 | static const char *coupling[] = { | |
141 | "A1M AC 1 Meg", | |
142 | "A50 AC 50 Ohm", | |
143 | "D1M DC 1 Meg", | |
144 | "D50 DC 50 Ohm", | |
145 | "GND", | |
146 | }; | |
147 | ||
148 | static const uint64_t probe_factor[] = { | |
149 | 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, | |
150 | }; | |
151 | ||
152 | static const uint64_t averages[] = { | |
153 | 4, 16, 32, 64, 128, 256, 512, 1024, | |
154 | }; | |
155 | ||
156 | /* Do not change the order of entries. */ | |
157 | static const char *data_sources[] = { | |
158 | "Display", | |
159 | "History", | |
160 | }; | |
161 | ||
162 | enum vendor { | |
163 | SIGLENT, | |
164 | }; | |
165 | ||
166 | enum series { | |
167 | SDS1000CML, | |
168 | SDS1000CNL, | |
169 | SDS1000DL, | |
170 | SDS1000X, | |
171 | SDS1000XP, | |
172 | SDS1000XE, | |
173 | SDS2000X, | |
174 | }; | |
175 | ||
176 | /* short name, full name */ | |
177 | static const struct siglent_sds_vendor supported_vendors[] = { | |
178 | [SIGLENT] = {"Siglent", "Siglent Technologies"}, | |
179 | }; | |
180 | ||
181 | #define VENDOR(x) &supported_vendors[x] | |
182 | /* vendor, series, protocol, max timebase, min vdiv, number of horizontal divs, | |
183 | * number of vertical divs, live waveform samples, memory buffer samples */ | |
184 | static const struct siglent_sds_series supported_series[] = { | |
185 | [SDS1000CML] = {VENDOR(SIGLENT), "SDS1000CML", NON_SPO_MODEL, | |
186 | { 50, 1 }, { 2, 1000 }, 18, 8, 1400363}, | |
187 | [SDS1000CNL] = {VENDOR(SIGLENT), "SDS1000CNL", NON_SPO_MODEL, | |
188 | { 50, 1 }, { 2, 1000 }, 18, 8, 1400363}, | |
189 | [SDS1000DL] = {VENDOR(SIGLENT), "SDS1000DL", NON_SPO_MODEL, | |
190 | { 50, 1 }, { 2, 1000 }, 18, 8, 1400363}, | |
191 | [SDS1000X] = {VENDOR(SIGLENT), "SDS1000X", SPO_MODEL, | |
192 | { 50, 1 }, { 500, 100000 }, 14, 8, 14000363}, | |
193 | [SDS1000XP] = {VENDOR(SIGLENT), "SDS1000X+", SPO_MODEL, | |
194 | { 50, 1 }, { 500, 100000 }, 14, 8, 14000363}, | |
195 | [SDS1000XE] = {VENDOR(SIGLENT), "SDS1000XE", ESERIES, | |
196 | { 50, 1 }, { 500, 100000 }, 14, 8, 14000363}, | |
197 | [SDS2000X] = {VENDOR(SIGLENT), "SDS2000X", SPO_MODEL, | |
198 | { 50, 1 }, { 500, 100000 }, 14, 8, 14000363}, | |
199 | }; | |
200 | ||
201 | #define SERIES(x) &supported_series[x] | |
202 | /* series, model, min timebase, analog channels, digital */ | |
203 | static const struct siglent_sds_model supported_models[] = { | |
204 | { SERIES(SDS1000CML), "SDS1152CML", { 20, 1000000000 }, 2, FALSE, 0 }, | |
205 | { SERIES(SDS1000CML), "SDS1102CML", { 10, 1000000000 }, 2, FALSE, 0 }, | |
206 | { SERIES(SDS1000CML), "SDS1072CML", { 5, 1000000000 }, 2, FALSE, 0 }, | |
207 | { SERIES(SDS1000CNL), "SDS1202CNL", { 20, 1000000000 }, 2, FALSE, 0 }, | |
208 | { SERIES(SDS1000CNL), "SDS1102CNL", { 10, 1000000000 }, 2, FALSE, 0 }, | |
209 | { SERIES(SDS1000CNL), "SDS1072CNL", { 5, 1000000000 }, 2, FALSE, 0 }, | |
210 | { SERIES(SDS1000DL), "SDS1202DL", { 20, 1000000000 }, 2, FALSE, 0 }, | |
211 | { SERIES(SDS1000DL), "SDS1102DL", { 10, 1000000000 }, 2, FALSE, 0 }, | |
212 | { SERIES(SDS1000DL), "SDS1022DL", { 5, 1000000000 }, 2, FALSE, 0 }, | |
213 | { SERIES(SDS1000DL), "SDS1052DL", { 5, 1000000000 }, 2, FALSE, 0 }, | |
214 | { SERIES(SDS1000DL), "SDS1052DL+", { 5, 1000000000 }, 2, FALSE, 0 }, | |
215 | { SERIES(SDS1000X), "SDS1102X", { 2, 1000000000 }, 2, FALSE, 0 }, | |
216 | { SERIES(SDS1000XP), "SDS1102X+", { 2, 1000000000 }, 2, FALSE, 0 }, | |
217 | { SERIES(SDS1000X), "SDS1202X", { 2, 1000000000 }, 2, FALSE, 0 }, | |
218 | { SERIES(SDS1000XP), "SDS1202X+", { 2, 1000000000 }, 2, FALSE, 0 }, | |
219 | { SERIES(SDS1000XE), "SDS1202X-E", { 1, 1000000000 }, 2, FALSE, 0 }, | |
220 | { SERIES(SDS1000XE), "SDS1104X-E", { 1, 1000000000 }, 4, TRUE, 16 }, | |
221 | { SERIES(SDS1000XE), "SDS1204X-E", { 1, 1000000000 }, 4, TRUE, 16 }, | |
222 | { SERIES(SDS2000X), "SDS2072X", { 2, 1000000000 }, 2, FALSE, 0 }, | |
223 | { SERIES(SDS2000X), "SDS2074X", { 2, 1000000000 }, 4, FALSE, 0 }, | |
224 | { SERIES(SDS2000X), "SDS2102X", { 2, 1000000000 }, 2, FALSE, 0 }, | |
225 | { SERIES(SDS2000X), "SDS2104X", { 2, 1000000000 }, 4, FALSE, 0 }, | |
226 | { SERIES(SDS2000X), "SDS2202X", { 2, 1000000000 }, 2, FALSE, 0 }, | |
227 | { SERIES(SDS2000X), "SDS2204X", { 2, 1000000000 }, 4, FALSE, 0 }, | |
228 | { SERIES(SDS2000X), "SDS2302X", { 2, 1000000000 }, 2, FALSE, 0 }, | |
229 | { SERIES(SDS2000X), "SDS2304X", { 2, 1000000000 }, 4, FALSE, 0 }, | |
230 | }; | |
231 | ||
232 | static struct sr_dev_driver siglent_sds_driver_info; | |
233 | ||
234 | static void clear_helper(void *priv) | |
235 | { | |
236 | struct dev_context *devc; | |
237 | ||
238 | devc = priv; | |
239 | if (!devc) | |
240 | return; | |
241 | g_free(devc->analog_groups); | |
242 | g_free(devc->enabled_channels); | |
243 | } | |
244 | ||
245 | static int dev_clear(const struct sr_dev_driver *di) | |
246 | { | |
247 | return std_dev_clear_with_callback(di, clear_helper); | |
248 | } | |
249 | ||
250 | static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) | |
251 | { | |
252 | struct dev_context *devc; | |
253 | struct sr_dev_inst *sdi; | |
254 | struct sr_scpi_hw_info *hw_info; | |
255 | struct sr_channel *ch; | |
256 | unsigned int i; | |
257 | const struct siglent_sds_model *model; | |
258 | gchar *channel_name; | |
259 | ||
260 | if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) { | |
261 | sr_info("Couldn't get IDN response, retrying."); | |
262 | sr_scpi_close(scpi); | |
263 | sr_scpi_open(scpi); | |
264 | if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) { | |
265 | sr_info("Couldn't get IDN response."); | |
266 | return NULL; | |
267 | } | |
268 | } | |
269 | ||
270 | model = NULL; | |
271 | for (i = 0; i < ARRAY_SIZE(supported_models); i++) { | |
272 | if (!strcmp(hw_info->model, supported_models[i].name)) { | |
273 | model = &supported_models[i]; | |
274 | break; | |
275 | } | |
276 | } | |
277 | ||
278 | if (!model) { | |
279 | sr_scpi_hw_info_free(hw_info); | |
280 | return NULL; | |
281 | } | |
282 | ||
283 | sr_dbg("Setting Communication Headers to off."); | |
284 | if (sr_scpi_send(scpi, "CHDR OFF") != SR_OK) | |
285 | return NULL; | |
286 | ||
287 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
288 | sdi->vendor = g_strdup(model->series->vendor->name); | |
289 | sdi->model = g_strdup(model->name); | |
290 | sdi->version = g_strdup(hw_info->firmware_version); | |
291 | sdi->conn = scpi; | |
292 | sdi->driver = &siglent_sds_driver_info; | |
293 | sdi->inst_type = SR_INST_SCPI; | |
294 | sdi->serial_num = g_strdup(hw_info->serial_number); | |
295 | devc = g_malloc0(sizeof(struct dev_context)); | |
296 | devc->limit_frames = 1; | |
297 | devc->model = model; | |
298 | ||
299 | sr_scpi_hw_info_free(hw_info); | |
300 | ||
301 | devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group *) * | |
302 | model->analog_channels); | |
303 | ||
304 | for (i = 0; i < model->analog_channels; i++) { | |
305 | channel_name = g_strdup_printf("CH%d", i + 1); | |
306 | ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_name); | |
307 | ||
308 | devc->analog_groups[i] = sr_channel_group_new(sdi, | |
309 | channel_name, NULL); | |
310 | devc->analog_groups[i]->channels = g_slist_append(NULL, ch); | |
311 | } | |
312 | ||
313 | if (devc->model->has_digital) { | |
314 | devc->digital_group = sr_channel_group_new(sdi, "LA", NULL); | |
315 | for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) { | |
316 | channel_name = g_strdup_printf("D%d", i); | |
317 | ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name); | |
318 | g_free(channel_name); | |
319 | devc->digital_group->channels = g_slist_append( | |
320 | devc->digital_group->channels, ch); | |
321 | } | |
322 | } | |
323 | ||
324 | for (i = 0; i < ARRAY_SIZE(timebases); i++) { | |
325 | if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2]))) | |
326 | devc->timebases = &timebases[i]; | |
327 | ||
328 | if (!memcmp(&devc->model->series->max_timebase, &timebases[i], sizeof(uint64_t[2]))) | |
329 | devc->num_timebases = &timebases[i] - devc->timebases + 1; | |
330 | } | |
331 | ||
332 | for (i = 0; i < ARRAY_SIZE(vdivs); i++) { | |
333 | devc->vdivs = &vdivs[i]; | |
334 | if (!memcmp(&devc->model->series->min_vdiv, | |
335 | &vdivs[i], sizeof(uint64_t[2]))) { | |
336 | devc->vdivs = &vdivs[i]; | |
337 | devc->num_vdivs = ARRAY_SIZE(vdivs) - i; | |
338 | break; | |
339 | } | |
340 | } | |
341 | ||
342 | devc->buffer = g_malloc(devc->model->series->buffer_samples); | |
343 | sr_dbg("Setting device context buffer size: %i.", devc->model->series->buffer_samples); | |
344 | devc->data = g_malloc(devc->model->series->buffer_samples * sizeof(float)); | |
345 | ||
346 | devc->data_source = DATA_SOURCE_SCREEN; | |
347 | ||
348 | sdi->priv = devc; | |
349 | ||
350 | return sdi; | |
351 | } | |
352 | ||
353 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
354 | { | |
355 | /* TODO: Implement RPC call for LXI device discovery. */ | |
356 | return sr_scpi_scan(di->context, options, probe_device); | |
357 | } | |
358 | ||
359 | static int dev_open(struct sr_dev_inst *sdi) | |
360 | { | |
361 | int ret; | |
362 | struct sr_scpi_dev_inst *scpi = sdi->conn; | |
363 | ||
364 | if ((ret = sr_scpi_open(scpi)) < 0) { | |
365 | sr_err("Failed to open SCPI device: %s.", sr_strerror(ret)); | |
366 | return SR_ERR; | |
367 | } | |
368 | ||
369 | if ((ret = siglent_sds_get_dev_cfg(sdi)) < 0) { | |
370 | sr_err("Failed to get device config: %s.", sr_strerror(ret)); | |
371 | return SR_ERR; | |
372 | } | |
373 | ||
374 | return SR_OK; | |
375 | } | |
376 | ||
377 | static int dev_close(struct sr_dev_inst *sdi) | |
378 | { | |
379 | return sr_scpi_close(sdi->conn); | |
380 | } | |
381 | ||
382 | static int config_get(uint32_t key, GVariant **data, | |
383 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
384 | { | |
385 | struct dev_context *devc; | |
386 | struct sr_channel *ch; | |
387 | const char *tmp_str; | |
388 | int analog_channel = -1; | |
389 | float smallest_diff = INFINITY; | |
390 | int idx = -1; | |
391 | unsigned i; | |
392 | ||
393 | if (!sdi) | |
394 | return SR_ERR_ARG; | |
395 | ||
396 | devc = sdi->priv; | |
397 | ||
398 | /* If a channel group is specified, it must be a valid one. */ | |
399 | if (cg && !g_slist_find(sdi->channel_groups, cg)) { | |
400 | sr_err("Invalid channel group specified."); | |
401 | return SR_ERR; | |
402 | } | |
403 | ||
404 | if (cg) { | |
405 | ch = g_slist_nth_data(cg->channels, 0); | |
406 | if (!ch) | |
407 | return SR_ERR; | |
408 | if (ch->type == SR_CHANNEL_ANALOG) { | |
409 | if (ch->name[2] < '1' || ch->name[2] > '4') | |
410 | return SR_ERR; | |
411 | analog_channel = ch->name[2] - '1'; | |
412 | } | |
413 | } | |
414 | ||
415 | switch (key) { | |
416 | case SR_CONF_NUM_HDIV: | |
417 | *data = g_variant_new_int32(devc->model->series->num_horizontal_divs); | |
418 | break; | |
419 | case SR_CONF_NUM_VDIV: | |
420 | *data = g_variant_new_int32(devc->num_vdivs); | |
421 | break; | |
422 | case SR_CONF_LIMIT_FRAMES: | |
423 | *data = g_variant_new_uint64(devc->limit_frames); | |
424 | break; | |
425 | case SR_CONF_DATA_SOURCE: | |
426 | if (devc->data_source == DATA_SOURCE_SCREEN) | |
427 | *data = g_variant_new_string("Screen"); | |
428 | else if (devc->data_source == DATA_SOURCE_HISTORY) | |
429 | *data = g_variant_new_string("History"); | |
430 | break; | |
431 | case SR_CONF_SAMPLERATE: | |
432 | siglent_sds_get_dev_cfg_horizontal(sdi); | |
433 | *data = g_variant_new_uint64(devc->samplerate); | |
434 | break; | |
435 | case SR_CONF_TRIGGER_SOURCE: | |
436 | if (!strcmp(devc->trigger_source, "ACL")) | |
437 | tmp_str = "AC Line"; | |
438 | else if (!strcmp(devc->trigger_source, "CHAN1")) | |
439 | tmp_str = "CH1"; | |
440 | else if (!strcmp(devc->trigger_source, "CHAN2")) | |
441 | tmp_str = "CH2"; | |
442 | else | |
443 | tmp_str = devc->trigger_source; | |
444 | *data = g_variant_new_string(tmp_str); | |
445 | break; | |
446 | case SR_CONF_TRIGGER_SLOPE: | |
447 | if (!strncmp(devc->trigger_slope, "POS", 3)) { | |
448 | tmp_str = "r"; | |
449 | } else if (!strncmp(devc->trigger_slope, "NEG", 3)) { | |
450 | tmp_str = "f"; | |
451 | } else { | |
452 | sr_dbg("Unknown trigger slope: '%s'.", devc->trigger_slope); | |
453 | return SR_ERR_NA; | |
454 | } | |
455 | *data = g_variant_new_string(tmp_str); | |
456 | break; | |
457 | case SR_CONF_TRIGGER_LEVEL: | |
458 | *data = g_variant_new_double(devc->trigger_level); | |
459 | break; | |
460 | case SR_CONF_HORIZ_TRIGGERPOS: | |
461 | *data = g_variant_new_double(devc->horiz_triggerpos); | |
462 | break; | |
463 | case SR_CONF_TIMEBASE: | |
464 | for (i = 0; i < devc->num_timebases; i++) { | |
465 | float tb, diff; | |
466 | ||
467 | tb = (float)devc->timebases[i][0] / devc->timebases[i][1]; | |
468 | diff = fabs(devc->timebase - tb); | |
469 | if (diff < smallest_diff) { | |
470 | smallest_diff = diff; | |
471 | idx = i; | |
472 | } | |
473 | } | |
474 | if (idx < 0) { | |
475 | sr_dbg("Negative timebase index: %d.", idx); | |
476 | return SR_ERR_NA; | |
477 | } | |
478 | *data = g_variant_new("(tt)", devc->timebases[idx][0], | |
479 | devc->timebases[idx][1]); | |
480 | break; | |
481 | case SR_CONF_VDIV: | |
482 | if (analog_channel < 0) { | |
483 | sr_dbg("Negative analog channel: %d.", analog_channel); | |
484 | return SR_ERR_NA; | |
485 | } | |
486 | for (i = 0; i < ARRAY_SIZE(vdivs); i++) { | |
487 | float vdiv = (float)vdivs[i][0] / vdivs[i][1]; | |
488 | float diff = fabsf(devc->vdiv[analog_channel] - vdiv); | |
489 | if (diff < smallest_diff) { | |
490 | smallest_diff = diff; | |
491 | idx = i; | |
492 | } | |
493 | } | |
494 | if (idx < 0) { | |
495 | sr_dbg("Negative vdiv index: %d.", idx); | |
496 | return SR_ERR_NA; | |
497 | } | |
498 | *data = g_variant_new("(tt)", vdivs[idx][0], vdivs[idx][1]); | |
499 | break; | |
500 | case SR_CONF_COUPLING: | |
501 | if (analog_channel < 0) { | |
502 | sr_dbg("Negative analog channel: %d.", analog_channel); | |
503 | return SR_ERR_NA; | |
504 | } | |
505 | *data = g_variant_new_string(devc->coupling[analog_channel]); | |
506 | break; | |
507 | case SR_CONF_PROBE_FACTOR: | |
508 | if (analog_channel < 0) { | |
509 | sr_dbg("Negative analog channel: %d.", analog_channel); | |
510 | return SR_ERR_NA; | |
511 | } | |
512 | *data = g_variant_new_uint64(devc->attenuation[analog_channel]); | |
513 | break; | |
514 | case SR_CONF_AVERAGING: | |
515 | *data = g_variant_new_boolean(devc->average_enabled); | |
516 | break; | |
517 | case SR_CONF_AVG_SAMPLES: | |
518 | *data = g_variant_new_uint64(devc->average_samples); | |
519 | break; | |
520 | default: | |
521 | return SR_ERR_NA; | |
522 | } | |
523 | ||
524 | return SR_OK; | |
525 | } | |
526 | ||
527 | static int config_set(uint32_t key, GVariant *data, | |
528 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
529 | { | |
530 | struct dev_context *devc; | |
531 | uint64_t p; | |
532 | double t_dbl; | |
533 | int i; | |
534 | int ret, idx; | |
535 | const char *tmp_str; | |
536 | char buffer[16]; | |
537 | char *cmd = ""; | |
538 | char cmd4[4]; | |
539 | ||
540 | devc = sdi->priv; | |
541 | ||
542 | /* If a channel group is specified, it must be a valid one. */ | |
543 | if (cg && !g_slist_find(sdi->channel_groups, cg)) { | |
544 | sr_err("Invalid channel group specified."); | |
545 | return SR_ERR; | |
546 | } | |
547 | ||
548 | ret = SR_OK; | |
549 | switch (key) { | |
550 | case SR_CONF_LIMIT_FRAMES: | |
551 | devc->limit_frames = g_variant_get_uint64(data); | |
552 | break; | |
553 | case SR_CONF_TRIGGER_SLOPE: | |
554 | if ((idx = std_str_idx(data, ARRAY_AND_SIZE(trigger_slopes))) < 0) | |
555 | return SR_ERR_ARG; | |
556 | g_free(devc->trigger_slope); | |
557 | devc->trigger_slope = g_strdup((trigger_slopes[idx][0] == 'r') ? "POS" : "NEG"); | |
558 | return siglent_sds_config_set(sdi, "%s:TRSL %s", | |
559 | devc->trigger_source, devc->trigger_slope); | |
560 | case SR_CONF_HORIZ_TRIGGERPOS: | |
561 | t_dbl = g_variant_get_double(data); | |
562 | if (t_dbl < 0.0 || t_dbl > 1.0) { | |
563 | sr_err("Invalid horiz. trigger position: %g.", t_dbl); | |
564 | return SR_ERR; | |
565 | } | |
566 | devc->horiz_triggerpos = t_dbl; | |
567 | /* We have the trigger offset as a percentage of the frame, but | |
568 | * need to express this in seconds. */ | |
569 | t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases; | |
570 | g_ascii_formatd(buffer, sizeof(buffer), "%.6f", t_dbl); | |
571 | return siglent_sds_config_set(sdi, ":TIM:OFFS %s", buffer); | |
572 | case SR_CONF_TRIGGER_LEVEL: | |
573 | t_dbl = g_variant_get_double(data); | |
574 | g_ascii_formatd(buffer, sizeof(buffer), "%.3f", t_dbl); | |
575 | ret = siglent_sds_config_set(sdi, ":TRIG:EDGE:LEV %s", buffer); | |
576 | if (ret == SR_OK) | |
577 | devc->trigger_level = t_dbl; | |
578 | break; | |
579 | case SR_CONF_TIMEBASE: | |
580 | if ((idx = std_u64_tuple_idx(data, devc->timebases, devc->num_timebases)) < 0) | |
581 | return SR_ERR_ARG; | |
582 | devc->timebase = (float)devc->timebases[idx][0] / devc->timebases[idx][1]; | |
583 | p = devc->timebases[idx][0]; | |
584 | switch (devc->timebases[idx][1]) { | |
585 | case 1: | |
586 | cmd = g_strdup_printf("%" PRIu64 "S", p); | |
587 | break; | |
588 | case 1000: | |
589 | cmd = g_strdup_printf("%" PRIu64 "MS", p); | |
590 | break; | |
591 | case 1000000: | |
592 | cmd = g_strdup_printf("%" PRIu64 "US", p); | |
593 | break; | |
594 | case 1000000000: | |
595 | cmd = g_strdup_printf("%" PRIu64 "NS", p); | |
596 | break; | |
597 | } | |
598 | ret = siglent_sds_config_set(sdi, "TDIV %s", cmd); | |
599 | g_free(cmd); | |
600 | return ret; | |
601 | case SR_CONF_TRIGGER_SOURCE: | |
602 | if ((idx = std_str_idx(data, ARRAY_AND_SIZE(trigger_sources))) < 0) | |
603 | return SR_ERR_ARG; | |
604 | g_free(devc->trigger_source); | |
605 | devc->trigger_source = g_strdup(trigger_sources[idx]); | |
606 | if (!strcmp(devc->trigger_source, "AC Line")) | |
607 | tmp_str = "LINE"; | |
608 | else if (!strcmp(devc->trigger_source, "CH1")) | |
609 | tmp_str = "C1"; | |
610 | else if (!strcmp(devc->trigger_source, "CH2")) | |
611 | tmp_str = "C2"; | |
612 | else if (!strcmp(devc->trigger_source, "CH3")) | |
613 | tmp_str = "C3"; | |
614 | else if (!strcmp(devc->trigger_source, "CH4")) | |
615 | tmp_str = "C4"; | |
616 | else if (!strcmp(devc->trigger_source, "Ext")) | |
617 | tmp_str = "EX"; | |
618 | else if (!strcmp(devc->trigger_source, "Ext /5")) | |
619 | tmp_str = "EX5"; | |
620 | else | |
621 | tmp_str = (char *)devc->trigger_source; | |
622 | return siglent_sds_config_set(sdi, "TRSE EDGE,SR,%s,OFF", tmp_str); | |
623 | case SR_CONF_VDIV: | |
624 | if (!cg) | |
625 | return SR_ERR_CHANNEL_GROUP; | |
626 | if ((i = std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels)) < 0) | |
627 | return SR_ERR_ARG; | |
628 | if ((idx = std_u64_tuple_idx(data, ARRAY_AND_SIZE(vdivs))) < 0) | |
629 | return SR_ERR_ARG; | |
630 | devc->vdiv[i] = (float)vdivs[idx][0] / vdivs[idx][1]; | |
631 | p = vdivs[idx][0]; | |
632 | switch (vdivs[idx][1]) { | |
633 | case 1: | |
634 | cmd = g_strdup_printf("%" PRIu64 "V", p); | |
635 | break; | |
636 | case 1000: | |
637 | cmd = g_strdup_printf("%" PRIu64 "MV", p); | |
638 | break; | |
639 | case 100000: | |
640 | cmd = g_strdup_printf("%" PRIu64 "UV", p); | |
641 | break; | |
642 | } | |
643 | ret = siglent_sds_config_set(sdi, "C%d:VDIV %s", i + 1, cmd); | |
644 | g_free(cmd); | |
645 | return ret; | |
646 | case SR_CONF_COUPLING: | |
647 | if (!cg) | |
648 | return SR_ERR_CHANNEL_GROUP; | |
649 | if ((i = std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels)) < 0) | |
650 | return SR_ERR_ARG; | |
651 | if ((idx = std_str_idx(data, ARRAY_AND_SIZE(coupling))) < 0) | |
652 | return SR_ERR_ARG; | |
653 | g_free(devc->coupling[i]); | |
654 | devc->coupling[i] = g_strdup(coupling[idx]); | |
655 | strncpy(cmd4, devc->coupling[i], 3); | |
656 | cmd4[3] = 0; | |
657 | return siglent_sds_config_set(sdi, "C%d:CPL %s", i + 1, cmd4); | |
658 | case SR_CONF_PROBE_FACTOR: | |
659 | if (!cg) | |
660 | return SR_ERR_CHANNEL_GROUP; | |
661 | if ((i = std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels)) < 0) | |
662 | return SR_ERR_ARG; | |
663 | if ((idx = std_u64_idx(data, ARRAY_AND_SIZE(probe_factor))) < 0) | |
664 | return SR_ERR_ARG; | |
665 | p = g_variant_get_uint64(data); | |
666 | devc->attenuation[i] = probe_factor[idx]; | |
667 | ret = siglent_sds_config_set(sdi, "C%d:ATTN %" PRIu64, i + 1, p); | |
668 | if (ret == SR_OK) | |
669 | siglent_sds_get_dev_cfg_vertical(sdi); | |
670 | return ret; | |
671 | case SR_CONF_DATA_SOURCE: | |
672 | tmp_str = g_variant_get_string(data, NULL); | |
673 | if (!strcmp(tmp_str, "Display")) | |
674 | devc->data_source = DATA_SOURCE_SCREEN; | |
675 | else if (devc->model->series->protocol >= SPO_MODEL | |
676 | && !strcmp(tmp_str, "History")) | |
677 | devc->data_source = DATA_SOURCE_HISTORY; | |
678 | else { | |
679 | sr_err("Unknown data source: '%s'.", tmp_str); | |
680 | return SR_ERR; | |
681 | } | |
682 | break; | |
683 | case SR_CONF_SAMPLERATE: | |
684 | siglent_sds_get_dev_cfg_horizontal(sdi); | |
685 | data = g_variant_new_uint64(devc->samplerate); | |
686 | break; | |
687 | case SR_CONF_AVERAGING: | |
688 | devc->average_enabled = g_variant_get_boolean(data); | |
689 | sr_dbg("%s averaging", devc->average_enabled ? "Enabling" : "Disabling"); | |
690 | break; | |
691 | case SR_CONF_AVG_SAMPLES: | |
692 | devc->average_samples = g_variant_get_uint64(data); | |
693 | sr_dbg("Setting averaging rate to %" PRIu64, devc->average_samples); | |
694 | break; | |
695 | default: | |
696 | return SR_ERR_NA; | |
697 | } | |
698 | ||
699 | return ret; | |
700 | } | |
701 | ||
702 | static int config_list(uint32_t key, GVariant **data, | |
703 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
704 | { | |
705 | struct dev_context *devc; | |
706 | ||
707 | devc = (sdi) ? sdi->priv : NULL; | |
708 | ||
709 | switch (key) { | |
710 | case SR_CONF_SCAN_OPTIONS: | |
711 | case SR_CONF_DEVICE_OPTIONS: | |
712 | if (!cg) | |
713 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
714 | if (!devc) | |
715 | return SR_ERR_ARG; | |
716 | if (cg == devc->digital_group) { | |
717 | *data = std_gvar_array_u32(NULL, 0); | |
718 | return SR_OK; | |
719 | } else { | |
720 | if (std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels) < 0) | |
721 | return SR_ERR_ARG; | |
722 | *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog)); | |
723 | return SR_OK; | |
724 | } | |
725 | break; | |
726 | case SR_CONF_COUPLING: | |
727 | if (!cg) | |
728 | return SR_ERR_CHANNEL_GROUP; | |
729 | *data = g_variant_new_strv(ARRAY_AND_SIZE(coupling)); | |
730 | break; | |
731 | case SR_CONF_PROBE_FACTOR: | |
732 | if (!cg) | |
733 | return SR_ERR_CHANNEL_GROUP; | |
734 | *data = std_gvar_array_u64(ARRAY_AND_SIZE(probe_factor)); | |
735 | break; | |
736 | case SR_CONF_VDIV: | |
737 | if (!devc) | |
738 | /* Can't know this until we have the exact model. */ | |
739 | return SR_ERR_ARG; | |
740 | if (!cg) | |
741 | return SR_ERR_CHANNEL_GROUP; | |
742 | *data = std_gvar_tuple_array(devc->vdivs, devc->num_vdivs); | |
743 | break; | |
744 | case SR_CONF_TIMEBASE: | |
745 | if (!devc) | |
746 | /* Can't know this until we have the exact model. */ | |
747 | return SR_ERR_ARG; | |
748 | if (devc->num_timebases <= 0) | |
749 | return SR_ERR_NA; | |
750 | *data = std_gvar_tuple_array(devc->timebases, devc->num_timebases); | |
751 | break; | |
752 | case SR_CONF_TRIGGER_SOURCE: | |
753 | if (!devc) | |
754 | /* Can't know this until we have the exact model. */ | |
755 | return SR_ERR_ARG; | |
756 | *data = g_variant_new_strv(trigger_sources, | |
757 | devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 5); | |
758 | break; | |
759 | case SR_CONF_TRIGGER_SLOPE: | |
760 | *data = g_variant_new_strv(ARRAY_AND_SIZE(trigger_slopes)); | |
761 | break; | |
762 | case SR_CONF_DATA_SOURCE: | |
763 | if (!devc) | |
764 | /* Can't know this until we have the exact model. */ | |
765 | return SR_ERR_ARG; | |
766 | switch (devc->model->series->protocol) { | |
767 | /* TODO: Check what must be done here for the data source buffer sizes. */ | |
768 | case NON_SPO_MODEL: | |
769 | *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1); | |
770 | break; | |
771 | case SPO_MODEL: | |
772 | case ESERIES: | |
773 | *data = g_variant_new_strv(ARRAY_AND_SIZE(data_sources)); | |
774 | break; | |
775 | } | |
776 | break; | |
777 | case SR_CONF_NUM_HDIV: | |
778 | *data = g_variant_new_int32(devc->model->series->num_horizontal_divs); | |
779 | break; | |
780 | case SR_CONF_AVG_SAMPLES: | |
781 | *data = std_gvar_array_u64(ARRAY_AND_SIZE(averages)); | |
782 | break; | |
783 | default: | |
784 | return SR_ERR_NA; | |
785 | } | |
786 | ||
787 | return SR_OK; | |
788 | } | |
789 | ||
790 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
791 | { | |
792 | struct sr_scpi_dev_inst *scpi; | |
793 | struct dev_context *devc; | |
794 | struct sr_channel *ch; | |
795 | gboolean some_digital; | |
796 | GSList *l, *d; | |
797 | ||
798 | scpi = sdi->conn; | |
799 | devc = sdi->priv; | |
800 | ||
801 | devc->num_frames = 0; | |
802 | some_digital = FALSE; | |
803 | ||
804 | /* | |
805 | * Check if there are any logic channels enabled, if so then enable | |
806 | * the MSO, otherwise skip the digital channel setup. Enable and | |
807 | * disable channels on the device is very slow and it is faster when | |
808 | * checked in a small loop without the actual actions. | |
809 | */ | |
810 | for (d = sdi->channels; d; d = d->next) { | |
811 | ch = d->data; | |
812 | if (ch->type == SR_CHANNEL_LOGIC && ch->enabled) | |
813 | some_digital = TRUE; | |
814 | } | |
815 | ||
816 | for (l = sdi->channels; l; l = l->next) { | |
817 | ch = l->data; | |
818 | if (ch->type == SR_CHANNEL_ANALOG) { | |
819 | if (ch->enabled) | |
820 | devc->enabled_channels = g_slist_append( | |
821 | devc->enabled_channels, ch); | |
822 | if (ch->enabled != devc->analog_channels[ch->index]) { | |
823 | /* Enabled channel is currently disabled, or vice versa. */ | |
824 | if (siglent_sds_config_set(sdi, "C%d:TRA %s", ch->index + 1, | |
825 | ch->enabled ? "ON" : "OFF") != SR_OK) | |
826 | return SR_ERR; | |
827 | devc->analog_channels[ch->index] = ch->enabled; | |
828 | } | |
829 | } else if (ch->type == SR_CHANNEL_LOGIC && some_digital) { | |
830 | if (ch->enabled) { | |
831 | /* Turn on LA module if currently off and digital channels are enabled. */ | |
832 | if (!devc->la_enabled) { | |
833 | if (siglent_sds_config_set(sdi, "DI:SW?") != SR_OK) | |
834 | return SR_ERR; | |
835 | devc->la_enabled = TRUE; | |
836 | } | |
837 | devc->enabled_channels = g_slist_append( | |
838 | devc->enabled_channels, ch); | |
839 | } | |
840 | /* Enabled channel is currently disabled, or vice versa. */ | |
841 | if (siglent_sds_config_set(sdi, "D%d:TRA %s", ch->index, | |
842 | ch->enabled ? "ON" : "OFF") != SR_OK) | |
843 | return SR_ERR; | |
844 | devc->digital_channels[ch->index] = ch->enabled; | |
845 | } | |
846 | } | |
847 | if (!devc->enabled_channels) | |
848 | return SR_ERR; | |
849 | /* Turn off LA module if on and no digital channels selected. */ | |
850 | if (devc->la_enabled && !some_digital) | |
851 | if (siglent_sds_config_set(sdi, "DGST OFF") != SR_OK) { | |
852 | devc->la_enabled = FALSE; | |
853 | g_usleep(500000); | |
854 | return SR_ERR; | |
855 | } | |
856 | ||
857 | // devc->analog_frame_size = devc->model->series->buffer_samples; | |
858 | // devc->digital_frame_size = devc->model->series->buffer_samples; | |
859 | ||
860 | siglent_sds_get_dev_cfg_horizontal(sdi); | |
861 | switch (devc->model->series->protocol) { | |
862 | case SPO_MODEL: | |
863 | if (siglent_sds_config_set(sdi, "WFSU SP,0,TYPE,1") != SR_OK) | |
864 | return SR_ERR; | |
865 | if (devc->average_enabled) { | |
866 | if (siglent_sds_config_set(sdi, "ACQW AVERAGE,%i", devc->average_samples) != SR_OK) | |
867 | return SR_ERR; | |
868 | } else { | |
869 | if (siglent_sds_config_set(sdi, "ACQW SAMPLING") != SR_OK) | |
870 | return SR_ERR; | |
871 | } | |
872 | break; | |
873 | case NON_SPO_MODEL: | |
874 | /* TODO: Implement CML/CNL/DL models. */ | |
875 | if (siglent_sds_config_set(sdi, "WFSU SP,0,TYPE,1") != SR_OK) | |
876 | return SR_ERR; | |
877 | if (siglent_sds_config_set(sdi, "ACQW SAMPLING") != SR_OK) | |
878 | return SR_ERR; | |
879 | break; | |
880 | default: | |
881 | break; | |
882 | } | |
883 | ||
884 | sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 7000, | |
885 | siglent_sds_receive, (void *) sdi); | |
886 | ||
887 | std_session_send_df_header(sdi); | |
888 | ||
889 | devc->channel_entry = devc->enabled_channels; | |
890 | ||
891 | if (siglent_sds_capture_start(sdi) != SR_OK) | |
892 | return SR_ERR; | |
893 | ||
894 | /* Start of first frame. */ | |
895 | std_session_send_df_frame_begin(sdi); | |
896 | ||
897 | return SR_OK; | |
898 | } | |
899 | ||
900 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
901 | { | |
902 | struct dev_context *devc; | |
903 | struct sr_scpi_dev_inst *scpi; | |
904 | ||
905 | devc = sdi->priv; | |
906 | ||
907 | std_session_send_df_end(sdi); | |
908 | ||
909 | g_slist_free(devc->enabled_channels); | |
910 | devc->enabled_channels = NULL; | |
911 | scpi = sdi->conn; | |
912 | sr_scpi_source_remove(sdi->session, scpi); | |
913 | ||
914 | return SR_OK; | |
915 | } | |
916 | ||
917 | static struct sr_dev_driver siglent_sds_driver_info = { | |
918 | .name = "siglent-sds", | |
919 | .longname = "Siglent SDS1000/SDS2000", | |
920 | .api_version = 1, | |
921 | .init = std_init, | |
922 | .cleanup = std_cleanup, | |
923 | .scan = scan, | |
924 | .dev_list = std_dev_list, | |
925 | .dev_clear = dev_clear, | |
926 | .config_get = config_get, | |
927 | .config_set = config_set, | |
928 | .config_list = config_list, | |
929 | .dev_open = dev_open, | |
930 | .dev_close = dev_close, | |
931 | .dev_acquisition_start = dev_acquisition_start, | |
932 | .dev_acquisition_stop = dev_acquisition_stop, | |
933 | .context = NULL, | |
934 | }; | |
935 | SR_REGISTER_DEV_DRIVER(siglent_sds_driver_info); |