]>
Commit | Line | Data |
---|---|---|
05ac4a98 DG |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2015 Daniel Glöckner <daniel-gl@gmx.net> | |
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 | ||
6ec6c43b | 20 | #include <config.h> |
c9404469 DG |
21 | #include <ieee1284.h> |
22 | #include <string.h> | |
05ac4a98 DG |
23 | #include "protocol.h" |
24 | ||
c9404469 DG |
25 | static const uint32_t scanopts[] = { |
26 | SR_CONF_CONN, | |
27 | }; | |
28 | ||
29 | static const uint32_t drvopts[] = { | |
30 | SR_CONF_OSCILLOSCOPE, | |
31 | }; | |
32 | ||
33 | static const uint32_t devopts[] = { | |
34 | SR_CONF_CONN | SR_CONF_GET, | |
35 | SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET, | |
36 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
37 | SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
38 | SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
39 | SR_CONF_BUFFERSIZE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
40 | }; | |
41 | ||
42 | static const uint32_t cgopts[] = { | |
43 | SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
44 | SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
45 | SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET, | |
46 | }; | |
47 | ||
48 | static const uint64_t samplerates[] = { | |
49 | SR_MHZ(100), SR_MHZ(50), SR_MHZ(25), SR_MHZ(20), | |
50 | SR_MHZ(10), SR_MHZ(5), SR_KHZ(2500), SR_MHZ(2), | |
51 | SR_MHZ(1), SR_KHZ(500), SR_KHZ(250), SR_KHZ(200), | |
52 | SR_KHZ(100), SR_KHZ(50), SR_KHZ(25), SR_KHZ(20), | |
53 | SR_KHZ(10), SR_KHZ(5), SR_HZ(2500), SR_KHZ(2), | |
54 | SR_KHZ(1), SR_HZ(500), SR_HZ(250), SR_HZ(200), | |
55 | SR_HZ(100), SR_HZ(50), SR_HZ(25), SR_HZ(20) | |
56 | }; | |
57 | ||
58 | /* must be in sync with readout_steps[] in protocol.c */ | |
59 | static const uint64_t buffersizes[] = { | |
60 | 2 * 500, 3 * 500, 4 * 500, 5 * 500, | |
61 | 6 * 500, 7 * 500, 8 * 500, 9 * 500, 10 * 500, | |
62 | 12 * 500 - 2, 14 * 500 - 2, 16 * 500 - 2, | |
63 | 18 * 500 - 2, 20 * 500 - 2, 10240 - 2 | |
64 | }; | |
65 | ||
66 | static const uint64_t vdivs[][2] = { | |
67 | { 10, 1000 }, | |
68 | { 20, 1000 }, | |
69 | { 50, 1000 }, | |
70 | { 100, 1000 }, | |
71 | { 200, 1000 }, | |
72 | { 500, 1000 }, | |
73 | { 1, 1 }, | |
74 | { 2, 1 }, | |
75 | { 5, 1 }, | |
76 | }; | |
77 | ||
78 | /* Bits 4 and 5 enable relays that add /10 filters to the chain | |
79 | * Bits 0 and 1 select an output from a resistor array */ | |
80 | static const uint8_t vdivs_map[] = { | |
81 | 0x01, 0x02, 0x03, 0x21, 0x22, 0x23, 0x31, 0x32, 0x33 | |
82 | }; | |
83 | ||
84 | ||
85 | static const char *trigger_sources[] = { | |
86 | "A", "B", "EXT" | |
87 | }; | |
88 | ||
89 | static const uint8_t trigger_sources_map[] = { | |
90 | 0x00, 0x80, 0x40 | |
91 | }; | |
92 | ||
93 | static const char *trigger_slopes[] = { | |
94 | "f", "r" | |
95 | }; | |
96 | ||
97 | static const char *coupling[] = { | |
98 | "DC", "AC", "GND" | |
99 | }; | |
100 | ||
101 | static const uint8_t coupling_map[] = { | |
102 | 0x00, 0x08, 0x04 | |
103 | }; | |
104 | ||
15a5bfe4 | 105 | static GSList *scan_port(GSList *devices, struct parport *port) |
05ac4a98 | 106 | { |
c9404469 DG |
107 | struct sr_dev_inst *sdi; |
108 | struct sr_channel *ch; | |
109 | struct sr_channel_group *cg; | |
110 | struct dev_context *devc; | |
c9404469 | 111 | int i; |
05ac4a98 | 112 | |
c9404469 DG |
113 | if (ieee1284_open(port, 0, &i) != E1284_OK) { |
114 | sr_err("Can't open parallel port %s", port->name); | |
115 | goto fail1; | |
116 | } | |
05ac4a98 | 117 | |
c9404469 DG |
118 | if ((i & (CAP1284_RAW | CAP1284_BYTE)) != (CAP1284_RAW | CAP1284_BYTE)) { |
119 | sr_err("Parallel port %s does not provide low-level bidirection access", | |
120 | port->name); | |
121 | goto fail2; | |
122 | } | |
123 | ||
124 | if (ieee1284_claim(port) != E1284_OK) { | |
125 | sr_err("Parallel port %s already in use", port->name); | |
126 | goto fail2; | |
127 | } | |
128 | ||
129 | if (!hung_chang_dso_2100_check_id(port)) | |
130 | goto fail3; | |
131 | ||
132 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
133 | sdi->status = SR_ST_INACTIVE; | |
134 | sdi->vendor = g_strdup("Hung-Chang"); | |
135 | sdi->model = g_strdup("DSO-2100"); | |
c9404469 DG |
136 | sdi->inst_type = 0; /* FIXME */ |
137 | sdi->conn = port; | |
138 | ieee1284_ref(port); | |
139 | ||
140 | for (i = 0; i < NUM_CHANNELS; i++) { | |
141 | cg = g_malloc0(sizeof(struct sr_channel_group)); | |
142 | cg->name = g_strdup(trigger_sources[i]); | |
143 | ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, FALSE, trigger_sources[i]); | |
144 | cg->channels = g_slist_append(cg->channels, ch); | |
145 | sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); | |
146 | } | |
147 | ||
148 | devc = g_malloc0(sizeof(struct dev_context)); | |
149 | devc->enabled_channel = g_slist_append(NULL, NULL); | |
150 | devc->channel = 0; | |
151 | devc->rate = 0; | |
152 | devc->probe[0] = 10; | |
153 | devc->probe[1] = 10; | |
154 | devc->cctl[0] = 0x31; /* 1V/div, DC coupling, trigger on channel A*/ | |
155 | devc->cctl[1] = 0x31; /* 1V/div, DC coupling, no tv sync trigger */ | |
156 | devc->edge = 0; | |
157 | devc->tlevel = 0x80; | |
158 | devc->pos[0] = 0x80; | |
159 | devc->pos[1] = 0x80; | |
160 | devc->offset[0] = 0x80; | |
161 | devc->offset[1] = 0x80; | |
162 | devc->gain[0] = 0x80; | |
163 | devc->gain[1] = 0x80; | |
164 | devc->frame_limit = 0; | |
165 | devc->last_step = 0; /* buffersize = 1000 */ | |
166 | sdi->priv = devc; | |
167 | ||
c9404469 DG |
168 | devices = g_slist_append(devices, sdi); |
169 | ||
170 | fail3: | |
171 | ieee1284_release(port); | |
172 | fail2: | |
173 | ieee1284_close(port); | |
174 | fail1: | |
175 | return devices; | |
176 | } | |
177 | ||
178 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
179 | { | |
180 | struct parport_list ports; | |
181 | struct sr_config *src; | |
182 | const char *conn = NULL; | |
183 | GSList *devices, *option; | |
184 | gboolean port_found; | |
185 | int i; | |
186 | ||
187 | ||
188 | for (option = options; option; option = option->next) { | |
189 | src = option->data; | |
190 | if (src->key == SR_CONF_CONN) { | |
191 | conn = g_variant_get_string(src->data, NULL); | |
192 | break; | |
193 | } | |
194 | } | |
195 | ||
196 | if (!conn) | |
197 | return NULL; | |
05ac4a98 | 198 | |
c9404469 DG |
199 | if (ieee1284_find_ports(&ports, 0) != E1284_OK) |
200 | return NULL; | |
201 | ||
202 | devices = NULL; | |
203 | port_found = FALSE; | |
204 | for (i = 0; i < ports.portc; i++) | |
205 | if (!strcmp(ports.portv[i]->name, conn)) { | |
206 | port_found = TRUE; | |
15a5bfe4 | 207 | devices = scan_port(devices, ports.portv[i]); |
c9404469 DG |
208 | } |
209 | ||
210 | if (!port_found) { | |
211 | sr_err("Parallel port %s not found. Valid names are:", conn); | |
212 | for (i = 0; i < ports.portc; i++) | |
213 | sr_err("\t%s", ports.portv[i]->name); | |
214 | } | |
215 | ||
216 | ieee1284_free_ports(&ports); | |
05ac4a98 | 217 | |
15a5bfe4 | 218 | return std_scan_complete(di, devices); |
05ac4a98 DG |
219 | } |
220 | ||
3553451f | 221 | static void clear_helper(struct dev_context *devc) |
c9404469 | 222 | { |
c9404469 DG |
223 | g_slist_free(devc->enabled_channel); |
224 | } | |
225 | ||
05ac4a98 DG |
226 | static int dev_clear(const struct sr_dev_driver *di) |
227 | { | |
c9404469 DG |
228 | struct drv_context *drvc = di->context; |
229 | struct sr_dev_inst *sdi; | |
230 | GSList *l; | |
231 | ||
232 | if (drvc) { | |
233 | for (l = drvc->instances; l; l = l->next) { | |
234 | sdi = l->data; | |
235 | ieee1284_unref(sdi->conn); | |
236 | } | |
237 | } | |
238 | ||
3553451f | 239 | return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper); |
05ac4a98 DG |
240 | } |
241 | ||
242 | static int dev_open(struct sr_dev_inst *sdi) | |
243 | { | |
c9404469 DG |
244 | struct dev_context *devc = sdi->priv; |
245 | int i; | |
246 | ||
c9404469 DG |
247 | if (ieee1284_open(sdi->conn, 0, &i) != E1284_OK) |
248 | goto fail1; | |
05ac4a98 | 249 | |
c9404469 DG |
250 | if (ieee1284_claim(sdi->conn) != E1284_OK) |
251 | goto fail2; | |
252 | ||
253 | if (ieee1284_data_dir(sdi->conn, 1) != E1284_OK) | |
254 | goto fail3; | |
255 | ||
256 | if (hung_chang_dso_2100_move_to(sdi, 1)) | |
257 | goto fail3; | |
258 | ||
259 | devc->samples = g_try_malloc(1000 * sizeof(*devc->samples)); | |
260 | if (!devc->samples) | |
261 | goto fail3; | |
05ac4a98 | 262 | |
05ac4a98 | 263 | return SR_OK; |
c9404469 DG |
264 | |
265 | fail3: | |
266 | hung_chang_dso_2100_reset_port(sdi->conn); | |
267 | ieee1284_release(sdi->conn); | |
268 | fail2: | |
269 | ieee1284_close(sdi->conn); | |
270 | fail1: | |
271 | return SR_ERR; | |
05ac4a98 DG |
272 | } |
273 | ||
274 | static int dev_close(struct sr_dev_inst *sdi) | |
275 | { | |
c9404469 | 276 | struct dev_context *devc = sdi->priv; |
05ac4a98 | 277 | |
c9404469 DG |
278 | g_free(devc->samples); |
279 | hung_chang_dso_2100_reset_port(sdi->conn); | |
280 | ieee1284_release(sdi->conn); | |
281 | ieee1284_close(sdi->conn); | |
093e1cba | 282 | |
05ac4a98 DG |
283 | return SR_OK; |
284 | } | |
285 | ||
c9404469 DG |
286 | static int find_in_array(GVariant *data, const GVariantType *type, |
287 | const void *arr, int n) | |
288 | { | |
289 | const char * const *sarr; | |
290 | const char *s; | |
291 | const uint64_t *u64arr; | |
292 | const uint8_t *u8arr; | |
293 | uint64_t u64; | |
294 | uint8_t u8; | |
295 | int i; | |
296 | ||
297 | if (!g_variant_is_of_type(data, type)) | |
298 | return -1; | |
299 | ||
300 | switch (g_variant_classify(data)) { | |
301 | case G_VARIANT_CLASS_STRING: | |
302 | s = g_variant_get_string(data, NULL); | |
303 | sarr = arr; | |
304 | ||
305 | for (i = 0; i < n; i++) | |
306 | if (!strcmp(s, sarr[i])) | |
307 | return i; | |
308 | break; | |
309 | case G_VARIANT_CLASS_UINT64: | |
310 | u64 = g_variant_get_uint64(data); | |
311 | u64arr = arr; | |
312 | ||
313 | for (i = 0; i < n; i++) | |
314 | if (u64 == u64arr[i]) | |
315 | return i; | |
316 | break; | |
317 | case G_VARIANT_CLASS_BYTE: | |
318 | u8 = g_variant_get_byte(data); | |
319 | u8arr = arr; | |
320 | ||
321 | for (i = 0; i < n; i++) | |
322 | if (u8 == u8arr[i]) | |
323 | return i; | |
324 | default: | |
325 | break; | |
326 | } | |
327 | ||
328 | return -1; | |
329 | } | |
330 | ||
331 | static int reverse_map(uint8_t u, const uint8_t *arr, int n) | |
332 | { | |
333 | GVariant *v = g_variant_new_byte(u); | |
334 | int i = find_in_array(v, G_VARIANT_TYPE_BYTE, arr, n); | |
335 | g_variant_unref(v); | |
336 | return i; | |
05ac4a98 DG |
337 | } |
338 | ||
339 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
340 | const struct sr_channel_group *cg) | |
341 | { | |
c9404469 DG |
342 | struct dev_context *devc = sdi->priv; |
343 | struct parport *port; | |
a9010323 | 344 | int i, ch = -1; |
05ac4a98 | 345 | |
c9404469 DG |
346 | if (cg) /* sr_config_get will validate cg using config_list */ |
347 | ch = ((struct sr_channel *)cg->channels->data)->index; | |
05ac4a98 | 348 | |
05ac4a98 | 349 | switch (key) { |
c9404469 DG |
350 | case SR_CONF_CONN: |
351 | port = sdi->conn; | |
352 | *data = g_variant_new_string(port->name); | |
353 | break; | |
354 | case SR_CONF_LIMIT_FRAMES: | |
355 | *data = g_variant_new_uint64(devc->frame_limit); | |
356 | break; | |
357 | case SR_CONF_SAMPLERATE: | |
358 | *data = g_variant_new_uint64(samplerates[devc->rate]); | |
359 | break; | |
360 | case SR_CONF_TRIGGER_SOURCE: | |
361 | i = reverse_map(devc->cctl[0] & 0xC0, trigger_sources_map, | |
362 | ARRAY_SIZE(trigger_sources_map)); | |
363 | if (i == -1) | |
a9010323 | 364 | return SR_ERR; |
c9404469 DG |
365 | else |
366 | *data = g_variant_new_string(trigger_sources[i]); | |
367 | break; | |
368 | case SR_CONF_TRIGGER_SLOPE: | |
369 | if (devc->edge >= ARRAY_SIZE(trigger_slopes)) | |
a9010323 | 370 | return SR_ERR; |
c9404469 DG |
371 | else |
372 | *data = g_variant_new_string(trigger_slopes[devc->edge]); | |
373 | break; | |
374 | case SR_CONF_BUFFERSIZE: | |
375 | *data = g_variant_new_uint64(buffersizes[devc->last_step]); | |
376 | break; | |
377 | case SR_CONF_VDIV: | |
378 | if (ch == -1) { | |
a9010323 | 379 | return SR_ERR_CHANNEL_GROUP; |
c9404469 DG |
380 | } else { |
381 | i = reverse_map(devc->cctl[ch] & 0x33, vdivs_map, | |
382 | ARRAY_SIZE(vdivs_map)); | |
383 | if (i == -1) | |
a9010323 | 384 | return SR_ERR; |
c9404469 DG |
385 | else |
386 | *data = g_variant_new("(tt)", vdivs[i][0], | |
387 | vdivs[i][1]); | |
388 | } | |
389 | break; | |
390 | case SR_CONF_COUPLING: | |
391 | if (ch == -1) { | |
a9010323 | 392 | return SR_ERR_CHANNEL_GROUP; |
c9404469 DG |
393 | } else { |
394 | i = reverse_map(devc->cctl[ch] & 0x0C, coupling_map, | |
395 | ARRAY_SIZE(coupling_map)); | |
396 | if (i == -1) | |
a9010323 | 397 | return SR_ERR; |
c9404469 DG |
398 | else |
399 | *data = g_variant_new_string(coupling[i]); | |
400 | } | |
401 | break; | |
402 | case SR_CONF_PROBE_FACTOR: | |
403 | if (ch == -1) | |
a9010323 | 404 | return SR_ERR_CHANNEL_GROUP; |
c9404469 DG |
405 | else |
406 | *data = g_variant_new_uint64(devc->probe[ch]); | |
407 | break; | |
05ac4a98 | 408 | default: |
a9010323 | 409 | return SR_ERR_NA; |
05ac4a98 DG |
410 | } |
411 | ||
a9010323 | 412 | return SR_OK; |
05ac4a98 DG |
413 | } |
414 | ||
415 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
416 | const struct sr_channel_group *cg) | |
417 | { | |
c9404469 | 418 | struct dev_context *devc = sdi->priv; |
a9010323 | 419 | int i, ch = -1; |
c9404469 | 420 | uint64_t u, v; |
05ac4a98 | 421 | |
c9404469 DG |
422 | if (cg) /* sr_config_set will validate cg using config_list */ |
423 | ch = ((struct sr_channel *)cg->channels->data)->index; | |
05ac4a98 | 424 | |
05ac4a98 | 425 | switch (key) { |
c9404469 DG |
426 | case SR_CONF_LIMIT_FRAMES: |
427 | devc->frame_limit = g_variant_get_uint64(data); | |
428 | break; | |
429 | case SR_CONF_SAMPLERATE: | |
430 | i = find_in_array(data, G_VARIANT_TYPE_UINT64, | |
431 | samplerates, ARRAY_SIZE(samplerates)); | |
432 | if (i == -1) | |
a9010323 | 433 | return SR_ERR_ARG; |
c9404469 DG |
434 | else |
435 | devc->rate = i; | |
436 | break; | |
437 | case SR_CONF_TRIGGER_SOURCE: | |
438 | i = find_in_array(data, G_VARIANT_TYPE_STRING, | |
439 | trigger_sources, ARRAY_SIZE(trigger_sources)); | |
440 | if (i == -1) | |
a9010323 | 441 | return SR_ERR_ARG; |
c9404469 DG |
442 | else |
443 | devc->cctl[0] = (devc->cctl[0] & 0x3F) | |
444 | | trigger_sources_map[i]; | |
445 | break; | |
446 | case SR_CONF_TRIGGER_SLOPE: | |
447 | i = find_in_array(data, G_VARIANT_TYPE_STRING, | |
448 | trigger_slopes, ARRAY_SIZE(trigger_slopes)); | |
449 | if (i == -1) | |
a9010323 | 450 | return SR_ERR_ARG; |
c9404469 DG |
451 | else |
452 | devc->edge = i; | |
453 | break; | |
454 | case SR_CONF_BUFFERSIZE: | |
455 | i = find_in_array(data, G_VARIANT_TYPE_UINT64, | |
456 | buffersizes, ARRAY_SIZE(buffersizes)); | |
457 | if (i == -1) | |
a9010323 | 458 | return SR_ERR_ARG; |
c9404469 DG |
459 | else |
460 | devc->last_step = i; | |
461 | break; | |
462 | case SR_CONF_VDIV: | |
463 | if (ch == -1) { | |
a9010323 | 464 | return SR_ERR_CHANNEL_GROUP; |
c9404469 | 465 | } else if (!g_variant_is_of_type(data, G_VARIANT_TYPE("(tt)"))) { |
a9010323 | 466 | return SR_ERR_ARG; |
c9404469 DG |
467 | } else { |
468 | g_variant_get(data, "(tt)", &u, &v); | |
469 | for (i = 0; i < (int)ARRAY_SIZE(vdivs); i++) | |
470 | if (vdivs[i][0] == u && vdivs[i][1] == v) | |
471 | break; | |
472 | if (i == ARRAY_SIZE(vdivs)) | |
a9010323 | 473 | return SR_ERR_ARG; |
c9404469 DG |
474 | else |
475 | devc->cctl[ch] = (devc->cctl[ch] & 0xCC) | |
476 | | vdivs_map[i]; | |
477 | } | |
478 | break; | |
479 | case SR_CONF_COUPLING: | |
480 | if (ch == -1) { | |
a9010323 | 481 | return SR_ERR_CHANNEL_GROUP; |
c9404469 DG |
482 | } else { |
483 | i = find_in_array(data, G_VARIANT_TYPE_STRING, | |
484 | coupling, ARRAY_SIZE(coupling)); | |
485 | if (i == -1) | |
a9010323 | 486 | return SR_ERR_ARG; |
c9404469 DG |
487 | else |
488 | devc->cctl[ch] = (devc->cctl[ch] & 0xF3) | |
489 | | coupling_map[i]; | |
490 | } | |
491 | break; | |
492 | case SR_CONF_PROBE_FACTOR: | |
493 | if (ch == -1) { | |
a9010323 | 494 | return SR_ERR_CHANNEL_GROUP; |
c9404469 DG |
495 | } else { |
496 | u = g_variant_get_uint64(data); | |
497 | if (!u) | |
a9010323 | 498 | return SR_ERR_ARG; |
c9404469 DG |
499 | else |
500 | devc->probe[ch] = u; | |
501 | } | |
502 | break; | |
05ac4a98 | 503 | default: |
a9010323 | 504 | return SR_ERR_NA; |
05ac4a98 DG |
505 | } |
506 | ||
a9010323 | 507 | return SR_OK; |
05ac4a98 DG |
508 | } |
509 | ||
c9404469 DG |
510 | static int config_channel_set(const struct sr_dev_inst *sdi, |
511 | struct sr_channel *ch, | |
512 | unsigned int changes) | |
513 | { | |
514 | struct dev_context *devc = sdi->priv; | |
515 | uint8_t v; | |
516 | ||
517 | if (changes & SR_CHANNEL_SET_ENABLED) { | |
518 | if (ch->enabled) { | |
519 | v = devc->channel | (1 << ch->index); | |
520 | if (v & (v - 1)) | |
521 | return SR_ERR; | |
522 | devc->channel = v; | |
523 | devc->enabled_channel->data = ch; | |
524 | } else { | |
525 | devc->channel &= ~(1 << ch->index); | |
526 | } | |
527 | } | |
528 | return SR_OK; | |
529 | } | |
530 | ||
531 | static int config_commit(const struct sr_dev_inst *sdi) | |
05ac4a98 | 532 | { |
c9404469 | 533 | uint8_t state = hung_chang_dso_2100_read_mbox(sdi->conn, 0.02); |
05ac4a98 DG |
534 | int ret; |
535 | ||
c9404469 DG |
536 | switch (state) { |
537 | case 0x03: | |
538 | case 0x14: | |
539 | case 0x21: | |
540 | /* we will travel the complete config path on our way to state 1 */ | |
541 | break; | |
542 | case 0x00: | |
543 | state = 0x01; | |
276d7b18 | 544 | /* Fallthrough */ |
c9404469 DG |
545 | default: |
546 | ret = hung_chang_dso_2100_move_to(sdi, 1); | |
547 | if (ret != SR_OK) | |
548 | return ret; | |
276d7b18 | 549 | /* Fallthrough */ |
c9404469 DG |
550 | case 0x01: |
551 | hung_chang_dso_2100_write_mbox(sdi->conn, 4); | |
552 | } | |
553 | ret = hung_chang_dso_2100_move_to(sdi, 1); | |
554 | if (ret != SR_OK) | |
555 | return ret; | |
556 | return hung_chang_dso_2100_move_to(sdi, state); | |
557 | } | |
558 | ||
559 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
560 | const struct sr_channel_group *cg) | |
561 | { | |
562 | GVariantBuilder gvb; | |
563 | GVariant *gvar, *rational[2]; | |
564 | GSList *l; | |
565 | int i; | |
05ac4a98 | 566 | |
05ac4a98 | 567 | switch (key) { |
e66d1892 | 568 | case SR_CONF_SCAN_OPTIONS: |
c9404469 DG |
569 | case SR_CONF_DEVICE_OPTIONS: |
570 | break; | |
571 | case SR_CONF_SAMPLERATE: | |
572 | case SR_CONF_TRIGGER_SOURCE: | |
573 | case SR_CONF_TRIGGER_SLOPE: | |
574 | case SR_CONF_BUFFERSIZE: | |
575 | if (!sdi || cg) | |
576 | return SR_ERR_NA; | |
577 | break; | |
578 | case SR_CONF_VDIV: | |
579 | case SR_CONF_COUPLING: | |
580 | if (!sdi) | |
581 | return SR_ERR_NA; | |
582 | if (!cg) | |
583 | return SR_ERR_CHANNEL_GROUP; | |
584 | l = g_slist_find(sdi->channel_groups, cg); | |
585 | if (!l) | |
586 | return SR_ERR_ARG; | |
587 | break; | |
05ac4a98 DG |
588 | default: |
589 | return SR_ERR_NA; | |
590 | } | |
591 | ||
c9404469 DG |
592 | switch (key) { |
593 | case SR_CONF_SCAN_OPTIONS: | |
e66d1892 | 594 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, NULL, NULL); |
c9404469 | 595 | case SR_CONF_DEVICE_OPTIONS: |
e66d1892 UH |
596 | if (!cg) |
597 | return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts); | |
598 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
599 | cgopts, ARRAY_SIZE(cgopts), sizeof(uint32_t)); | |
c9404469 DG |
600 | break; |
601 | case SR_CONF_SAMPLERATE: | |
602 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
603 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), | |
604 | samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t)); | |
605 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
606 | *data = g_variant_builder_end(&gvb); | |
607 | break; | |
608 | case SR_CONF_TRIGGER_SOURCE: | |
609 | *data = g_variant_new_strv(trigger_sources, ARRAY_SIZE(trigger_sources)); | |
610 | break; | |
611 | case SR_CONF_TRIGGER_SLOPE: | |
612 | *data = g_variant_new_strv(trigger_slopes, ARRAY_SIZE(trigger_slopes)); | |
613 | break; | |
614 | case SR_CONF_BUFFERSIZE: | |
615 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64, | |
616 | buffersizes, ARRAY_SIZE(buffersizes), sizeof(uint64_t)); | |
617 | break; | |
618 | case SR_CONF_VDIV: | |
619 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
620 | for (i = 0; i < (int)ARRAY_SIZE(vdivs); i++) { | |
621 | rational[0] = g_variant_new_uint64(vdivs[i][0]); | |
622 | rational[1] = g_variant_new_uint64(vdivs[i][1]); | |
623 | gvar = g_variant_new_tuple(rational, 2); | |
624 | g_variant_builder_add_value(&gvb, gvar); | |
625 | } | |
626 | *data = g_variant_builder_end(&gvb); | |
627 | break; | |
628 | case SR_CONF_COUPLING: | |
629 | *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling)); | |
630 | break; | |
631 | } | |
632 | ||
633 | return SR_OK; | |
05ac4a98 DG |
634 | } |
635 | ||
695dc859 | 636 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
05ac4a98 | 637 | { |
c9404469 DG |
638 | struct dev_context *devc = sdi->priv; |
639 | int ret; | |
05ac4a98 | 640 | |
c9404469 DG |
641 | if (devc->channel) { |
642 | static const float res_array[] = {0.5, 1, 2, 5}; | |
643 | static const uint8_t relays[] = {100, 10, 10, 1}; | |
644 | devc->factor = devc->probe[devc->channel - 1] / 32.0; | |
645 | devc->factor *= res_array[devc->cctl[devc->channel - 1] & 0x03]; | |
646 | devc->factor /= relays[(devc->cctl[devc->channel - 1] >> 4) & 0x03]; | |
647 | } | |
648 | devc->frame = 0; | |
c9404469 DG |
649 | devc->state_known = TRUE; |
650 | devc->step = 0; | |
651 | devc->adc2 = FALSE; | |
652 | devc->retries = MAX_RETRIES; | |
653 | ||
654 | ret = hung_chang_dso_2100_move_to(sdi, 0x21); | |
655 | if (ret != SR_OK) | |
656 | return ret; | |
657 | ||
bee2b016 | 658 | std_session_send_df_header(sdi); |
c9404469 | 659 | |
b165a242 | 660 | sr_session_source_add(sdi->session, -1, 0, 8, |
c9404469 | 661 | hung_chang_dso_2100_poll, (void *)sdi); |
05ac4a98 DG |
662 | |
663 | return SR_OK; | |
664 | } | |
665 | ||
d2f7c417 | 666 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
05ac4a98 | 667 | { |
bee2b016 | 668 | std_session_send_df_end(sdi); |
b165a242 | 669 | sr_session_source_remove(sdi->session, -1); |
c9404469 | 670 | hung_chang_dso_2100_move_to(sdi, 1); |
05ac4a98 DG |
671 | |
672 | return SR_OK; | |
673 | } | |
674 | ||
dd5c48a6 | 675 | static struct sr_dev_driver hung_chang_dso_2100_driver_info = { |
05ac4a98 DG |
676 | .name = "hung-chang-dso-2100", |
677 | .longname = "Hung-Chang DSO-2100", | |
678 | .api_version = 1, | |
c2fdcc25 | 679 | .init = std_init, |
700d6b64 | 680 | .cleanup = std_cleanup, |
05ac4a98 | 681 | .scan = scan, |
c01bf34c | 682 | .dev_list = std_dev_list, |
05ac4a98 DG |
683 | .dev_clear = dev_clear, |
684 | .config_get = config_get, | |
685 | .config_set = config_set, | |
c9404469 DG |
686 | .config_channel_set = config_channel_set, |
687 | .config_commit = config_commit, | |
05ac4a98 DG |
688 | .config_list = config_list, |
689 | .dev_open = dev_open, | |
690 | .dev_close = dev_close, | |
691 | .dev_acquisition_start = dev_acquisition_start, | |
692 | .dev_acquisition_stop = dev_acquisition_stop, | |
693 | .context = NULL, | |
694 | }; | |
dd5c48a6 | 695 | SR_REGISTER_DEV_DRIVER(hung_chang_dso_2100_driver_info); |