]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2010-2012 Håvard Espeland <gus@ping.uio.no>, | |
5 | * Copyright (C) 2010 Martin Stensgård <mastensg@ping.uio.no> | |
6 | * Copyright (C) 2010 Carl Henrik Lunde <chlunde@ping.uio.no> | |
7 | * Copyright (C) 2020 Gerhard Sittig <gerhard.sittig@gmx.net> | |
8 | * | |
9 | * This program is free software: you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation, either version 3 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | */ | |
22 | ||
23 | #include <config.h> | |
24 | #include "protocol.h" | |
25 | ||
26 | /* | |
27 | * Channels are labelled 1-16, see this vendor's image of the cable: | |
28 | * http://tools.asix.net/img/sigma_sigmacab_pins_720.jpg (TI/TO are | |
29 | * additional trigger in/out signals). | |
30 | */ | |
31 | static const char *channel_names[] = { | |
32 | "1", "2", "3", "4", "5", "6", "7", "8", | |
33 | "9", "10", "11", "12", "13", "14", "15", "16", | |
34 | }; | |
35 | ||
36 | static const uint32_t scanopts[] = { | |
37 | SR_CONF_CONN, | |
38 | SR_CONF_PROBE_NAMES, | |
39 | }; | |
40 | ||
41 | static const uint32_t drvopts[] = { | |
42 | SR_CONF_LOGIC_ANALYZER, | |
43 | }; | |
44 | ||
45 | static const uint32_t devopts[] = { | |
46 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
47 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
48 | SR_CONF_CONN | SR_CONF_GET, | |
49 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
50 | SR_CONF_EXTERNAL_CLOCK | SR_CONF_GET | SR_CONF_SET, | |
51 | SR_CONF_EXTERNAL_CLOCK_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
52 | SR_CONF_CLOCK_EDGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
53 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
54 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, | |
55 | /* Consider SR_CONF_TRIGGER_PATTERN (SR_T_STRING, GET/SET) support. */ | |
56 | }; | |
57 | ||
58 | static const char *ext_clock_edges[] = { | |
59 | [SIGMA_CLOCK_EDGE_RISING] = "rising", | |
60 | [SIGMA_CLOCK_EDGE_FALLING] = "falling", | |
61 | [SIGMA_CLOCK_EDGE_EITHER] = "either", | |
62 | }; | |
63 | ||
64 | static const int32_t trigger_matches[] = { | |
65 | SR_TRIGGER_ZERO, | |
66 | SR_TRIGGER_ONE, | |
67 | SR_TRIGGER_RISING, | |
68 | SR_TRIGGER_FALLING, | |
69 | }; | |
70 | ||
71 | static void clear_helper(struct dev_context *devc) | |
72 | { | |
73 | (void)sigma_force_close(devc); | |
74 | } | |
75 | ||
76 | static int dev_clear(const struct sr_dev_driver *di) | |
77 | { | |
78 | return std_dev_clear_with_callback(di, | |
79 | (std_dev_clear_callback)clear_helper); | |
80 | } | |
81 | ||
82 | static gboolean bus_addr_in_devices(int bus, int addr, GSList *devs) | |
83 | { | |
84 | struct sr_usb_dev_inst *usb; | |
85 | ||
86 | for (/* EMPTY */; devs; devs = devs->next) { | |
87 | usb = devs->data; | |
88 | if (usb->bus == bus && usb->address == addr) | |
89 | return TRUE; | |
90 | } | |
91 | ||
92 | return FALSE; | |
93 | } | |
94 | ||
95 | static gboolean known_vid_pid(const struct libusb_device_descriptor *des) | |
96 | { | |
97 | gboolean is_sigma, is_omega; | |
98 | ||
99 | if (des->idVendor != USB_VENDOR_ASIX) | |
100 | return FALSE; | |
101 | is_sigma = des->idProduct == USB_PRODUCT_SIGMA; | |
102 | is_omega = des->idProduct == USB_PRODUCT_OMEGA; | |
103 | if (!is_sigma && !is_omega) | |
104 | return FALSE; | |
105 | return TRUE; | |
106 | } | |
107 | ||
108 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
109 | { | |
110 | struct drv_context *drvc; | |
111 | libusb_context *usbctx; | |
112 | const char *conn; | |
113 | const char *probe_names; | |
114 | GSList *l, *conn_devices; | |
115 | struct sr_config *src; | |
116 | GSList *devices; | |
117 | libusb_device **devlist, *devitem; | |
118 | int bus, addr; | |
119 | struct libusb_device_descriptor des; | |
120 | struct libusb_device_handle *hdl; | |
121 | int ret; | |
122 | char conn_id[20]; | |
123 | char serno_txt[16]; | |
124 | char *end; | |
125 | unsigned long serno_num, serno_pre; | |
126 | enum asix_device_type dev_type; | |
127 | const char *dev_text; | |
128 | struct sr_dev_inst *sdi; | |
129 | struct dev_context *devc; | |
130 | size_t devidx, chidx; | |
131 | size_t count; | |
132 | ||
133 | drvc = di->context; | |
134 | usbctx = drvc->sr_ctx->libusb_ctx; | |
135 | ||
136 | /* Find all devices which match an (optional) conn= spec. */ | |
137 | conn = NULL; | |
138 | probe_names = NULL; | |
139 | for (l = options; l; l = l->next) { | |
140 | src = l->data; | |
141 | switch (src->key) { | |
142 | case SR_CONF_CONN: | |
143 | conn = g_variant_get_string(src->data, NULL); | |
144 | break; | |
145 | case SR_CONF_PROBE_NAMES: | |
146 | probe_names = g_variant_get_string(src->data, NULL); | |
147 | break; | |
148 | } | |
149 | } | |
150 | conn_devices = NULL; | |
151 | if (conn) | |
152 | conn_devices = sr_usb_find(usbctx, conn); | |
153 | if (conn && !conn_devices) | |
154 | return NULL; | |
155 | ||
156 | /* Find all ASIX logic analyzers (which match the connection spec). */ | |
157 | devices = NULL; | |
158 | libusb_get_device_list(usbctx, &devlist); | |
159 | for (devidx = 0; devlist[devidx]; devidx++) { | |
160 | devitem = devlist[devidx]; | |
161 | ||
162 | /* Check for connection match if a user spec was given. */ | |
163 | bus = libusb_get_bus_number(devitem); | |
164 | addr = libusb_get_device_address(devitem); | |
165 | if (conn && !bus_addr_in_devices(bus, addr, conn_devices)) | |
166 | continue; | |
167 | snprintf(conn_id, sizeof(conn_id), "%d.%d", bus, addr); | |
168 | ||
169 | /* | |
170 | * Check for known VID:PID pairs. Get the serial number, | |
171 | * to then derive the device type from it. | |
172 | */ | |
173 | libusb_get_device_descriptor(devitem, &des); | |
174 | if (!known_vid_pid(&des)) | |
175 | continue; | |
176 | if (!des.iSerialNumber) { | |
177 | sr_warn("Cannot get serial number (index 0)."); | |
178 | continue; | |
179 | } | |
180 | ret = libusb_open(devitem, &hdl); | |
181 | if (ret < 0) { | |
182 | sr_warn("Cannot open USB device %04x.%04x: %s.", | |
183 | des.idVendor, des.idProduct, | |
184 | libusb_error_name(ret)); | |
185 | continue; | |
186 | } | |
187 | ret = libusb_get_string_descriptor_ascii(hdl, | |
188 | des.iSerialNumber, | |
189 | (unsigned char *)serno_txt, sizeof(serno_txt)); | |
190 | if (ret < 0) { | |
191 | sr_warn("Cannot get serial number (%s).", | |
192 | libusb_error_name(ret)); | |
193 | libusb_close(hdl); | |
194 | continue; | |
195 | } | |
196 | libusb_close(hdl); | |
197 | ||
198 | /* | |
199 | * All ASIX logic analyzers have a serial number, which | |
200 | * reads as a hex number, and tells the device type. | |
201 | */ | |
202 | ret = sr_atoul_base(serno_txt, &serno_num, &end, 16); | |
203 | if (ret != SR_OK || !end || *end) { | |
204 | sr_warn("Cannot interpret serial number %s.", serno_txt); | |
205 | continue; | |
206 | } | |
207 | dev_type = ASIX_TYPE_NONE; | |
208 | dev_text = NULL; | |
209 | serno_pre = serno_num >> 16; | |
210 | switch (serno_pre) { | |
211 | case 0xa601: | |
212 | dev_type = ASIX_TYPE_SIGMA; | |
213 | dev_text = "SIGMA"; | |
214 | sr_info("Found SIGMA, serno %s.", serno_txt); | |
215 | break; | |
216 | case 0xa602: | |
217 | dev_type = ASIX_TYPE_SIGMA; | |
218 | dev_text = "SIGMA2"; | |
219 | sr_info("Found SIGMA2, serno %s.", serno_txt); | |
220 | break; | |
221 | case 0xa603: | |
222 | dev_type = ASIX_TYPE_OMEGA; | |
223 | dev_text = "OMEGA"; | |
224 | sr_info("Found OMEGA, serno %s.", serno_txt); | |
225 | if (!ASIX_WITH_OMEGA) { | |
226 | sr_warn("OMEGA support is not implemented yet."); | |
227 | continue; | |
228 | } | |
229 | break; | |
230 | default: | |
231 | sr_warn("Unknown serno %s, skipping.", serno_txt); | |
232 | continue; | |
233 | } | |
234 | ||
235 | /* Create a device instance, add it to the result set. */ | |
236 | ||
237 | sdi = g_malloc0(sizeof(*sdi)); | |
238 | devices = g_slist_append(devices, sdi); | |
239 | sdi->status = SR_ST_INITIALIZING; | |
240 | sdi->vendor = g_strdup("ASIX"); | |
241 | sdi->model = g_strdup(dev_text); | |
242 | sdi->serial_num = g_strdup(serno_txt); | |
243 | sdi->connection_id = g_strdup(conn_id); | |
244 | devc = g_malloc0(sizeof(*devc)); | |
245 | sdi->priv = devc; | |
246 | devc->channel_names = sr_parse_probe_names(probe_names, | |
247 | channel_names, ARRAY_SIZE(channel_names), | |
248 | ARRAY_SIZE(channel_names), &count); | |
249 | for (chidx = 0; chidx < count; chidx++) | |
250 | sr_channel_new(sdi, chidx, SR_CHANNEL_LOGIC, | |
251 | TRUE, devc->channel_names[chidx]); | |
252 | devc->id.vid = des.idVendor; | |
253 | devc->id.pid = des.idProduct; | |
254 | devc->id.serno = serno_num; | |
255 | devc->id.prefix = serno_pre; | |
256 | devc->id.type = dev_type; | |
257 | sr_sw_limits_init(&devc->limit.config); | |
258 | devc->capture_ratio = 50; | |
259 | devc->use_triggers = FALSE; | |
260 | ||
261 | /* Get current hardware configuration (or use defaults). */ | |
262 | (void)sigma_fetch_hw_config(sdi); | |
263 | } | |
264 | libusb_free_device_list(devlist, 1); | |
265 | g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free); | |
266 | ||
267 | return std_scan_complete(di, devices); | |
268 | } | |
269 | ||
270 | static int dev_open(struct sr_dev_inst *sdi) | |
271 | { | |
272 | struct dev_context *devc; | |
273 | ||
274 | devc = sdi->priv; | |
275 | ||
276 | if (devc->id.type == ASIX_TYPE_OMEGA && !ASIX_WITH_OMEGA) { | |
277 | sr_err("OMEGA support is not implemented yet."); | |
278 | return SR_ERR_NA; | |
279 | } | |
280 | ||
281 | return sigma_force_open(sdi); | |
282 | } | |
283 | ||
284 | static int dev_close(struct sr_dev_inst *sdi) | |
285 | { | |
286 | struct dev_context *devc; | |
287 | ||
288 | devc = sdi->priv; | |
289 | ||
290 | return sigma_force_close(devc); | |
291 | } | |
292 | ||
293 | static int config_get(uint32_t key, GVariant **data, | |
294 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
295 | { | |
296 | struct dev_context *devc; | |
297 | const char *clock_text; | |
298 | ||
299 | (void)cg; | |
300 | ||
301 | if (!sdi) | |
302 | return SR_ERR; | |
303 | devc = sdi->priv; | |
304 | ||
305 | switch (key) { | |
306 | case SR_CONF_CONN: | |
307 | *data = g_variant_new_string(sdi->connection_id); | |
308 | break; | |
309 | case SR_CONF_SAMPLERATE: | |
310 | *data = g_variant_new_uint64(devc->clock.samplerate); | |
311 | break; | |
312 | case SR_CONF_EXTERNAL_CLOCK: | |
313 | *data = g_variant_new_boolean(devc->clock.use_ext_clock); | |
314 | break; | |
315 | case SR_CONF_EXTERNAL_CLOCK_SOURCE: | |
316 | clock_text = devc->channel_names[devc->clock.clock_pin]; | |
317 | *data = g_variant_new_string(clock_text); | |
318 | break; | |
319 | case SR_CONF_CLOCK_EDGE: | |
320 | clock_text = ext_clock_edges[devc->clock.clock_edge]; | |
321 | *data = g_variant_new_string(clock_text); | |
322 | break; | |
323 | case SR_CONF_LIMIT_MSEC: | |
324 | case SR_CONF_LIMIT_SAMPLES: | |
325 | return sr_sw_limits_config_get(&devc->limit.config, key, data); | |
326 | case SR_CONF_CAPTURE_RATIO: | |
327 | *data = g_variant_new_uint64(devc->capture_ratio); | |
328 | break; | |
329 | default: | |
330 | return SR_ERR_NA; | |
331 | } | |
332 | ||
333 | return SR_OK; | |
334 | } | |
335 | ||
336 | static int config_set(uint32_t key, GVariant *data, | |
337 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
338 | { | |
339 | struct dev_context *devc; | |
340 | int ret; | |
341 | uint64_t want_rate, have_rate; | |
342 | const char **names; | |
343 | size_t count; | |
344 | int idx; | |
345 | ||
346 | (void)cg; | |
347 | ||
348 | devc = sdi->priv; | |
349 | ||
350 | switch (key) { | |
351 | case SR_CONF_SAMPLERATE: | |
352 | want_rate = g_variant_get_uint64(data); | |
353 | ret = sigma_normalize_samplerate(want_rate, &have_rate); | |
354 | if (ret != SR_OK) | |
355 | return ret; | |
356 | if (have_rate != want_rate) { | |
357 | char *text_want, *text_have; | |
358 | text_want = sr_samplerate_string(want_rate); | |
359 | text_have = sr_samplerate_string(have_rate); | |
360 | sr_info("Adjusted samplerate %s to %s.", | |
361 | text_want, text_have); | |
362 | g_free(text_want); | |
363 | g_free(text_have); | |
364 | } | |
365 | devc->clock.samplerate = have_rate; | |
366 | break; | |
367 | case SR_CONF_EXTERNAL_CLOCK: | |
368 | devc->clock.use_ext_clock = g_variant_get_boolean(data); | |
369 | break; | |
370 | case SR_CONF_EXTERNAL_CLOCK_SOURCE: | |
371 | names = (const char **)devc->channel_names; | |
372 | count = g_strv_length(devc->channel_names); | |
373 | idx = std_str_idx(data, names, count); | |
374 | if (idx < 0) | |
375 | return SR_ERR_ARG; | |
376 | devc->clock.clock_pin = idx; | |
377 | break; | |
378 | case SR_CONF_CLOCK_EDGE: | |
379 | idx = std_str_idx(data, ARRAY_AND_SIZE(ext_clock_edges)); | |
380 | if (idx < 0) | |
381 | return SR_ERR_ARG; | |
382 | devc->clock.clock_edge = idx; | |
383 | break; | |
384 | case SR_CONF_LIMIT_MSEC: | |
385 | case SR_CONF_LIMIT_SAMPLES: | |
386 | return sr_sw_limits_config_set(&devc->limit.config, key, data); | |
387 | case SR_CONF_CAPTURE_RATIO: | |
388 | devc->capture_ratio = g_variant_get_uint64(data); | |
389 | break; | |
390 | default: | |
391 | return SR_ERR_NA; | |
392 | } | |
393 | ||
394 | return SR_OK; | |
395 | } | |
396 | ||
397 | static int config_list(uint32_t key, GVariant **data, | |
398 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
399 | { | |
400 | struct dev_context *devc; | |
401 | const char **names; | |
402 | size_t count; | |
403 | ||
404 | devc = sdi->priv; | |
405 | switch (key) { | |
406 | case SR_CONF_SCAN_OPTIONS: | |
407 | case SR_CONF_DEVICE_OPTIONS: | |
408 | if (cg) | |
409 | return SR_ERR_NA; | |
410 | return STD_CONFIG_LIST(key, data, sdi, cg, | |
411 | scanopts, drvopts, devopts); | |
412 | case SR_CONF_SAMPLERATE: | |
413 | *data = sigma_get_samplerates_list(); | |
414 | break; | |
415 | case SR_CONF_EXTERNAL_CLOCK_SOURCE: | |
416 | names = (const char **)devc->channel_names; | |
417 | count = g_strv_length(devc->channel_names); | |
418 | *data = g_variant_new_strv(names, count); | |
419 | break; | |
420 | case SR_CONF_CLOCK_EDGE: | |
421 | *data = g_variant_new_strv(ARRAY_AND_SIZE(ext_clock_edges)); | |
422 | break; | |
423 | case SR_CONF_TRIGGER_MATCH: | |
424 | *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches)); | |
425 | break; | |
426 | default: | |
427 | return SR_ERR_NA; | |
428 | } | |
429 | ||
430 | return SR_OK; | |
431 | } | |
432 | ||
433 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
434 | { | |
435 | struct dev_context *devc; | |
436 | uint16_t pindis_mask; | |
437 | uint8_t async, div; | |
438 | int ret; | |
439 | size_t triggerpin; | |
440 | uint8_t trigsel2; | |
441 | struct triggerinout triggerinout_conf; | |
442 | struct triggerlut lut; | |
443 | uint8_t regval, cmd_bytes[4], *wrptr; | |
444 | ||
445 | devc = sdi->priv; | |
446 | ||
447 | /* Convert caller's trigger spec to driver's internal format. */ | |
448 | ret = sigma_convert_trigger(sdi); | |
449 | if (ret != SR_OK) { | |
450 | sr_err("Could not configure triggers."); | |
451 | return ret; | |
452 | } | |
453 | ||
454 | /* | |
455 | * Setup the device's samplerate from the value which up to now | |
456 | * just got checked and stored. As a byproduct this can pick and | |
457 | * send firmware to the device, reduce the number of available | |
458 | * logic channels, etc. | |
459 | * | |
460 | * Determine an acquisition timeout from optionally configured | |
461 | * sample count or time limits. Which depends on the samplerate. | |
462 | * Force 50MHz samplerate when external clock is in use. | |
463 | */ | |
464 | if (devc->clock.use_ext_clock) { | |
465 | if (devc->clock.samplerate != SR_MHZ(50)) | |
466 | sr_info("External clock, forcing 50MHz samplerate."); | |
467 | devc->clock.samplerate = SR_MHZ(50); | |
468 | } | |
469 | ret = sigma_set_samplerate(sdi); | |
470 | if (ret != SR_OK) | |
471 | return ret; | |
472 | ret = sigma_set_acquire_timeout(devc); | |
473 | if (ret != SR_OK) | |
474 | return ret; | |
475 | ||
476 | /* Enter trigger programming mode. */ | |
477 | trigsel2 = TRGSEL2_RESET; | |
478 | ret = sigma_set_register(devc, WRITE_TRIGGER_SELECT2, trigsel2); | |
479 | if (ret != SR_OK) | |
480 | return ret; | |
481 | ||
482 | trigsel2 = 0; | |
483 | if (devc->clock.samplerate >= SR_MHZ(100)) { | |
484 | /* 100 and 200 MHz mode. */ | |
485 | /* TODO Decipher the 0x81 magic number's purpose. */ | |
486 | ret = sigma_set_register(devc, WRITE_TRIGGER_SELECT2, 0x81); | |
487 | if (ret != SR_OK) | |
488 | return ret; | |
489 | ||
490 | /* Find which pin to trigger on from mask. */ | |
491 | for (triggerpin = 0; triggerpin < 8; triggerpin++) { | |
492 | if (devc->trigger.risingmask & BIT(triggerpin)) | |
493 | break; | |
494 | if (devc->trigger.fallingmask & BIT(triggerpin)) | |
495 | break; | |
496 | } | |
497 | ||
498 | /* Set trigger pin and light LED on trigger. */ | |
499 | trigsel2 = triggerpin & TRGSEL2_PINS_MASK; | |
500 | trigsel2 |= TRGSEL2_LEDSEL1; | |
501 | ||
502 | /* Default rising edge. */ | |
503 | /* TODO Documentation disagrees, bit set means _rising_ edge. */ | |
504 | if (devc->trigger.fallingmask) | |
505 | trigsel2 |= TRGSEL2_PINPOL_RISE; | |
506 | ||
507 | } else if (devc->clock.samplerate <= SR_MHZ(50)) { | |
508 | /* 50MHz firmware modes. */ | |
509 | ||
510 | /* Translate application specs to hardware perspective. */ | |
511 | ret = sigma_build_basic_trigger(devc, &lut); | |
512 | if (ret != SR_OK) | |
513 | return ret; | |
514 | ||
515 | /* Communicate resulting register values to the device. */ | |
516 | ret = sigma_write_trigger_lut(devc, &lut); | |
517 | if (ret != SR_OK) | |
518 | return ret; | |
519 | ||
520 | trigsel2 = TRGSEL2_LEDSEL1 | TRGSEL2_LEDSEL0; | |
521 | } | |
522 | ||
523 | /* Setup trigger in and out pins to default values. */ | |
524 | memset(&triggerinout_conf, 0, sizeof(triggerinout_conf)); | |
525 | triggerinout_conf.trgout_bytrigger = TRUE; | |
526 | triggerinout_conf.trgout_enable = TRUE; | |
527 | /* TODO | |
528 | * Verify the correctness of this implementation. The previous | |
529 | * version used to assign to a C language struct with bit fields | |
530 | * which is highly non-portable and hard to guess the resulting | |
531 | * raw memory layout or wire transfer content. The C struct's | |
532 | * field names did not match the vendor documentation's names. | |
533 | * Which means that I could not verify "on paper" either. Let's | |
534 | * re-visit this code later during research for trigger support. | |
535 | */ | |
536 | wrptr = cmd_bytes; | |
537 | regval = 0; | |
538 | if (triggerinout_conf.trgout_bytrigger) | |
539 | regval |= TRGOPT_TRGOOUTEN; | |
540 | write_u8_inc(&wrptr, regval); | |
541 | regval &= ~TRGOPT_CLEAR_MASK; | |
542 | if (triggerinout_conf.trgout_enable) | |
543 | regval |= TRGOPT_TRGOEN; | |
544 | write_u8_inc(&wrptr, regval); | |
545 | ret = sigma_write_register(devc, WRITE_TRIGGER_OPTION, | |
546 | cmd_bytes, wrptr - cmd_bytes); | |
547 | if (ret != SR_OK) | |
548 | return ret; | |
549 | ||
550 | /* Leave trigger programming mode. */ | |
551 | ret = sigma_set_register(devc, WRITE_TRIGGER_SELECT2, trigsel2); | |
552 | if (ret != SR_OK) | |
553 | return ret; | |
554 | ||
555 | /* | |
556 | * Samplerate dependent clock and channels configuration. Some | |
557 | * channels by design are not available at higher clock rates. | |
558 | * Register layout differs between firmware variants (depth 1 | |
559 | * with LSB channel mask above 50MHz, depth 4 with more details | |
560 | * up to 50MHz). | |
561 | * | |
562 | * Derive a mask where bits are set for unavailable channels. | |
563 | * Either send the single byte, or the full byte sequence. | |
564 | */ | |
565 | pindis_mask = ~BITS_MASK(devc->interp.num_channels); | |
566 | if (devc->clock.samplerate > SR_MHZ(50)) { | |
567 | ret = sigma_set_register(devc, WRITE_CLOCK_SELECT, | |
568 | pindis_mask & 0xff); | |
569 | } else { | |
570 | wrptr = cmd_bytes; | |
571 | /* Select 50MHz base clock, and divider. */ | |
572 | async = 0; | |
573 | div = SR_MHZ(50) / devc->clock.samplerate - 1; | |
574 | if (devc->clock.use_ext_clock) { | |
575 | async = CLKSEL_CLKSEL8; | |
576 | div = devc->clock.clock_pin + 1; | |
577 | switch (devc->clock.clock_edge) { | |
578 | case SIGMA_CLOCK_EDGE_RISING: | |
579 | div |= CLKSEL_RISING; | |
580 | break; | |
581 | case SIGMA_CLOCK_EDGE_FALLING: | |
582 | div |= CLKSEL_FALLING; | |
583 | break; | |
584 | case SIGMA_CLOCK_EDGE_EITHER: | |
585 | div |= CLKSEL_RISING; | |
586 | div |= CLKSEL_FALLING; | |
587 | break; | |
588 | } | |
589 | } | |
590 | write_u8_inc(&wrptr, async); | |
591 | write_u8_inc(&wrptr, div); | |
592 | write_u16be_inc(&wrptr, pindis_mask); | |
593 | ret = sigma_write_register(devc, WRITE_CLOCK_SELECT, | |
594 | cmd_bytes, wrptr - cmd_bytes); | |
595 | } | |
596 | if (ret != SR_OK) | |
597 | return ret; | |
598 | ||
599 | /* Setup maximum post trigger time. */ | |
600 | ret = sigma_set_register(devc, WRITE_POST_TRIGGER, | |
601 | (devc->capture_ratio * 255) / 100); | |
602 | if (ret != SR_OK) | |
603 | return ret; | |
604 | ||
605 | /* Start acqusition. */ | |
606 | regval = WMR_TRGRES | WMR_SDRAMWRITEEN; | |
607 | if (devc->use_triggers) | |
608 | regval |= WMR_TRGEN; | |
609 | ret = sigma_set_register(devc, WRITE_MODE, regval); | |
610 | if (ret != SR_OK) | |
611 | return ret; | |
612 | ||
613 | ret = std_session_send_df_header(sdi); | |
614 | if (ret != SR_OK) | |
615 | return ret; | |
616 | ||
617 | /* Add capture source. */ | |
618 | ret = sr_session_source_add(sdi->session, -1, 0, 10, | |
619 | sigma_receive_data, (void *)sdi); | |
620 | if (ret != SR_OK) | |
621 | return ret; | |
622 | ||
623 | devc->state = SIGMA_CAPTURE; | |
624 | ||
625 | return SR_OK; | |
626 | } | |
627 | ||
628 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
629 | { | |
630 | struct dev_context *devc; | |
631 | ||
632 | devc = sdi->priv; | |
633 | ||
634 | /* | |
635 | * When acquisition is currently running, keep the receive | |
636 | * routine registered and have it stop the acquisition upon the | |
637 | * next invocation. Else unregister the receive routine here | |
638 | * already. The detour is required to have sample data retrieved | |
639 | * for forced acquisition stops. | |
640 | */ | |
641 | if (devc->state == SIGMA_CAPTURE) { | |
642 | devc->state = SIGMA_STOPPING; | |
643 | } else { | |
644 | devc->state = SIGMA_IDLE; | |
645 | (void)sr_session_source_remove(sdi->session, -1); | |
646 | } | |
647 | ||
648 | return SR_OK; | |
649 | } | |
650 | ||
651 | static struct sr_dev_driver asix_sigma_driver_info = { | |
652 | .name = "asix-sigma", | |
653 | .longname = "ASIX SIGMA/SIGMA2", | |
654 | .api_version = 1, | |
655 | .init = std_init, | |
656 | .cleanup = std_cleanup, | |
657 | .scan = scan, | |
658 | .dev_list = std_dev_list, | |
659 | .dev_clear = dev_clear, | |
660 | .config_get = config_get, | |
661 | .config_set = config_set, | |
662 | .config_list = config_list, | |
663 | .dev_open = dev_open, | |
664 | .dev_close = dev_close, | |
665 | .dev_acquisition_start = dev_acquisition_start, | |
666 | .dev_acquisition_stop = dev_acquisition_stop, | |
667 | .context = NULL, | |
668 | }; | |
669 | SR_REGISTER_DEV_DRIVER(asix_sigma_driver_info); |