]> sigrok.org Git - libsigrok.git/blame - src/hardware/asix-sigma/api.c
asix-sigma: rework scan for USB devices, add support for conn= specs
[libsigrok.git] / src / hardware / asix-sigma / api.c
CommitLineData
3ba56876 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 *
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
3ba56876 22#include <config.h>
23#include "protocol.h"
24
3ba56876 25/*
26 * Channel numbers seem to go from 1-16, according to this image:
27 * http://tools.asix.net/img/sigma_sigmacab_pins_720.jpg
28 * (the cable has two additional GND pins, and a TI and TO pin)
29 */
30static const char *channel_names[] = {
31 "1", "2", "3", "4", "5", "6", "7", "8",
32 "9", "10", "11", "12", "13", "14", "15", "16",
33};
34
53a939ab
GS
35static const uint32_t scanopts[] = {
36 SR_CONF_CONN,
37};
38
3ba56876 39static const uint32_t drvopts[] = {
40 SR_CONF_LOGIC_ANALYZER,
41};
42
43static const uint32_t devopts[] = {
44 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
2f7e529c 45 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
53a939ab 46 SR_CONF_CONN | SR_CONF_GET,
3ba56876 47 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
de3f7acb 48#if ASIX_SIGMA_WITH_TRIGGER
3ba56876 49 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
50 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
de3f7acb 51#endif
3ba56876 52};
53
eac48b34 54#if ASIX_SIGMA_WITH_TRIGGER
3ba56876 55static const int32_t trigger_matches[] = {
56 SR_TRIGGER_ZERO,
57 SR_TRIGGER_ONE,
58 SR_TRIGGER_RISING,
59 SR_TRIGGER_FALLING,
60};
eac48b34 61#endif
3ba56876 62
3553451f 63static void clear_helper(struct dev_context *devc)
53279f13 64{
53279f13
UH
65 ftdi_deinit(&devc->ftdic);
66}
67
3ba56876 68static int dev_clear(const struct sr_dev_driver *di)
69{
3553451f 70 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
3ba56876 71}
72
53a939ab 73static gboolean bus_addr_in_devices(int bus, int addr, GSList *devs)
3ba56876 74{
53a939ab 75 struct sr_usb_dev_inst *usb;
3ba56876 76
53a939ab
GS
77 for (/* EMPTY */; devs; devs = devs->next) {
78 usb = devs->data;
79 if (usb->bus == bus && usb->address == addr)
80 return TRUE;
81 }
3ba56876 82
53a939ab
GS
83 return FALSE;
84}
3ba56876 85
53a939ab
GS
86static gboolean known_vid_pid(const struct libusb_device_descriptor *des)
87{
88 if (des->idVendor != USB_VENDOR_ASIX)
89 return FALSE;
90 if (des->idProduct != USB_PRODUCT_SIGMA && des->idProduct != USB_PRODUCT_OMEGA)
91 return FALSE;
92 return TRUE;
93}
3ba56876 94
53a939ab
GS
95static GSList *scan(struct sr_dev_driver *di, GSList *options)
96{
97 struct drv_context *drvc;
98 libusb_context *usbctx;
99 const char *conn;
100 GSList *l, *conn_devices;
101 struct sr_config *src;
102 GSList *devices;
103 libusb_device **devlist, *devitem;
104 int bus, addr;
105 struct libusb_device_descriptor des;
106 struct libusb_device_handle *hdl;
107 int ret;
108 char conn_id[20];
109 char serno_txt[16];
110 char *end;
111 long serno_num, serno_pre;
112 enum asix_device_type dev_type;
113 const char *dev_text;
114 struct sr_dev_inst *sdi;
115 struct dev_context *devc;
116 size_t devidx, chidx;
117
118 drvc = di->context;
119 usbctx = drvc->sr_ctx->libusb_ctx;
120
121 /* Find all devices which match an (optional) conn= spec. */
122 conn = NULL;
123 for (l = options; l; l = l->next) {
124 src = l->data;
125 switch (src->key) {
126 case SR_CONF_CONN:
127 conn = g_variant_get_string(src->data, NULL);
128 break;
129 }
3ba56876 130 }
53a939ab
GS
131 conn_devices = NULL;
132 if (conn)
133 conn_devices = sr_usb_find(usbctx, conn);
134 if (conn && !conn_devices)
135 return NULL;
136
137 /* Find all ASIX logic analyzers (which match the connection spec). */
138 devices = NULL;
139 libusb_get_device_list(usbctx, &devlist);
140 for (devidx = 0; devlist[devidx]; devidx++) {
141 devitem = devlist[devidx];
142
143 /* Check for connection match if a user spec was given. */
144 bus = libusb_get_bus_number(devitem);
145 addr = libusb_get_device_address(devitem);
146 if (conn && !bus_addr_in_devices(bus, addr, conn_devices))
147 continue;
148 snprintf(conn_id, sizeof(conn_id), "%d.%d", bus, addr);
149
150 /*
151 * Check for known VID:PID pairs. Get the serial number,
152 * to then derive the device type from it.
153 */
154 libusb_get_device_descriptor(devitem, &des);
155 if (!known_vid_pid(&des))
156 continue;
157 if (!des.iSerialNumber) {
158 sr_warn("Cannot get serial number (index 0).");
159 continue;
160 }
161 ret = libusb_open(devitem, &hdl);
162 if (ret < 0) {
163 sr_warn("Cannot open USB device %04x.%04x: %s.",
164 des.idVendor, des.idProduct,
165 libusb_error_name(ret));
166 continue;
167 }
168 ret = libusb_get_string_descriptor_ascii(hdl,
169 des.iSerialNumber,
170 (unsigned char *)serno_txt, sizeof(serno_txt));
171 if (ret < 0) {
172 sr_warn("Cannot get serial number (%s).",
173 libusb_error_name(ret));
174 libusb_close(hdl);
175 continue;
176 }
177 libusb_close(hdl);
178
179 /*
180 * All ASIX logic analyzers have a serial number, which
181 * reads as a hex number, and tells the device type.
182 */
183 ret = sr_atol_base(serno_txt, &serno_num, &end, 16);
184 if (ret != SR_OK || !end || *end) {
185 sr_warn("Cannot interpret serial number %s.", serno_txt);
186 continue;
187 }
188 dev_type = ASIX_TYPE_NONE;
189 dev_text = NULL;
190 serno_pre = serno_num >> 16;
191 switch (serno_pre) {
192 case 0xa601:
193 dev_type = ASIX_TYPE_SIGMA;
194 dev_text = "SIGMA";
195 sr_info("Found SIGMA, serno %s.", serno_txt);
196 break;
197 case 0xa602:
198 dev_type = ASIX_TYPE_SIGMA;
199 dev_text = "SIGMA2";
200 sr_info("Found SIGMA2, serno %s.", serno_txt);
201 break;
202 case 0xa603:
203 dev_type = ASIX_TYPE_OMEGA;
204 dev_text = "OMEGA";
205 sr_info("Found OMEGA, serno %s.", serno_txt);
206 if (!ASIX_WITH_OMEGA) {
207 sr_warn("OMEGA support is not implemented yet.");
208 continue;
209 }
210 break;
211 default:
212 sr_warn("Unknown serno %s, skipping.", serno_txt);
213 continue;
214 }
215
216 /* Create a device instance, add it to the result set. */
217
218 sdi = g_malloc0(sizeof(*sdi));
219 devices = g_slist_append(devices, sdi);
220 sdi->status = SR_ST_INITIALIZING;
221 sdi->vendor = g_strdup("ASIX");
222 sdi->model = g_strdup(dev_text);
223 sdi->serial_num = g_strdup(serno_txt);
224 sdi->connection_id = g_strdup(conn_id);
225 for (chidx = 0; chidx < ARRAY_SIZE(channel_names); chidx++)
226 sr_channel_new(sdi, chidx, SR_CHANNEL_LOGIC,
227 TRUE, channel_names[chidx]);
228
229 devc = g_malloc0(sizeof(*devc));
230 sdi->priv = devc;
231 devc->id.vid = des.idVendor;
232 devc->id.pid = des.idProduct;
233 devc->id.serno = serno_num;
234 devc->id.prefix = serno_pre;
235 devc->id.type = dev_type;
236 devc->cur_samplerate = samplerates[0];
237 devc->limit_msec = 0;
238 devc->limit_samples = 0;
239 devc->cur_firmware = -1;
240 devc->num_channels = 0;
241 devc->samples_per_event = 0;
242 devc->capture_ratio = 50;
243 devc->use_triggers = 0;
3ba56876 244 }
53a939ab
GS
245 libusb_free_device_list(devlist, 1);
246 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
3ba56876 247
53a939ab 248 return std_scan_complete(di, devices);
3ba56876 249}
250
3ba56876 251static int dev_open(struct sr_dev_inst *sdi)
252{
253 struct dev_context *devc;
53a939ab
GS
254 long vid, pid;
255 const char *serno;
3ba56876 256 int ret;
257
258 devc = sdi->priv;
259
53a939ab
GS
260 if (devc->id.type == ASIX_TYPE_OMEGA && !ASIX_WITH_OMEGA) {
261 sr_err("OMEGA support is not implemented yet.");
262 return SR_ERR_NA;
263 }
264 vid = devc->id.vid;
265 pid = devc->id.pid;
266 serno = sdi->serial_num;
267
268 ret = ftdi_init(&devc->ftdic);
269 if (ret < 0) {
270 sr_err("Cannot initialize FTDI context (%d): %s.",
271 ret, ftdi_get_error_string(&devc->ftdic));
272 return SR_ERR_IO;
273 }
274 ret = ftdi_usb_open_desc_index(&devc->ftdic, vid, pid, NULL, serno, 0);
275 if (ret < 0) {
276 sr_err("Cannot open device (%d): %s.",
277 ret, ftdi_get_error_string(&devc->ftdic));
278 return SR_ERR_IO;
3ba56876 279 }
280
3ba56876 281 return SR_OK;
282}
283
284static int dev_close(struct sr_dev_inst *sdi)
285{
286 struct dev_context *devc;
53a939ab 287 int ret;
3ba56876 288
289 devc = sdi->priv;
290
53a939ab
GS
291 ret = ftdi_usb_close(&devc->ftdic);
292 ftdi_deinit(&devc->ftdic);
293
294 return (ret == 0) ? SR_OK : SR_ERR;
3ba56876 295}
296
dd7a72ea
UH
297static int config_get(uint32_t key, GVariant **data,
298 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
3ba56876 299{
300 struct dev_context *devc;
301
302 (void)cg;
303
304 if (!sdi)
305 return SR_ERR;
306 devc = sdi->priv;
307
308 switch (key) {
53a939ab
GS
309 case SR_CONF_CONN:
310 *data = g_variant_new_string(sdi->connection_id);
311 break;
3ba56876 312 case SR_CONF_SAMPLERATE:
313 *data = g_variant_new_uint64(devc->cur_samplerate);
314 break;
315 case SR_CONF_LIMIT_MSEC:
316 *data = g_variant_new_uint64(devc->limit_msec);
317 break;
2f7e529c
GS
318 case SR_CONF_LIMIT_SAMPLES:
319 *data = g_variant_new_uint64(devc->limit_samples);
320 break;
de3f7acb 321#if ASIX_SIGMA_WITH_TRIGGER
3ba56876 322 case SR_CONF_CAPTURE_RATIO:
323 *data = g_variant_new_uint64(devc->capture_ratio);
324 break;
de3f7acb 325#endif
3ba56876 326 default:
327 return SR_ERR_NA;
328 }
329
330 return SR_OK;
331}
332
dd7a72ea
UH
333static int config_set(uint32_t key, GVariant *data,
334 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
3ba56876 335{
336 struct dev_context *devc;
3ba56876 337
338 (void)cg;
339
3ba56876 340 devc = sdi->priv;
341
3ba56876 342 switch (key) {
343 case SR_CONF_SAMPLERATE:
758906aa 344 return sigma_set_samplerate(sdi, g_variant_get_uint64(data));
3ba56876 345 case SR_CONF_LIMIT_MSEC:
50ccb36f 346 devc->limit_msec = g_variant_get_uint64(data);
3ba56876 347 break;
348 case SR_CONF_LIMIT_SAMPLES:
efad7ccc
UH
349 devc->limit_samples = g_variant_get_uint64(data);
350 devc->limit_msec = sigma_limit_samples_to_msec(devc,
351 devc->limit_samples);
3ba56876 352 break;
de3f7acb 353#if ASIX_SIGMA_WITH_TRIGGER
3ba56876 354 case SR_CONF_CAPTURE_RATIO:
efad7ccc 355 devc->capture_ratio = g_variant_get_uint64(data);
3ba56876 356 break;
de3f7acb 357#endif
3ba56876 358 default:
758906aa 359 return SR_ERR_NA;
3ba56876 360 }
361
758906aa 362 return SR_OK;
3ba56876 363}
364
dd7a72ea
UH
365static int config_list(uint32_t key, GVariant **data,
366 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
3ba56876 367{
3ba56876 368 switch (key) {
53a939ab 369 case SR_CONF_SCAN_OPTIONS:
3ba56876 370 case SR_CONF_DEVICE_OPTIONS:
53a939ab
GS
371 if (cg)
372 return SR_ERR_NA;
373 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
3ba56876 374 case SR_CONF_SAMPLERATE:
463160cb 375 *data = std_gvar_samplerates(samplerates, samplerates_count);
3ba56876 376 break;
de3f7acb 377#if ASIX_SIGMA_WITH_TRIGGER
3ba56876 378 case SR_CONF_TRIGGER_MATCH:
53012da6 379 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
3ba56876 380 break;
de3f7acb 381#endif
3ba56876 382 default:
383 return SR_ERR_NA;
384 }
385
386 return SR_OK;
387}
388
695dc859 389static int dev_acquisition_start(const struct sr_dev_inst *sdi)
3ba56876 390{
391 struct dev_context *devc;
392 struct clockselect_50 clockselect;
8256ed15 393 int triggerpin, ret;
f06fb3e9 394 uint8_t triggerselect;
3ba56876 395 struct triggerinout triggerinout_conf;
396 struct triggerlut lut;
22f64ed8 397 uint8_t regval;
8256ed15
GS
398 uint8_t clock_bytes[sizeof(clockselect)];
399 size_t clock_idx;
3ba56876 400
3ba56876 401 devc = sdi->priv;
402
403 if (sigma_convert_trigger(sdi) != SR_OK) {
404 sr_err("Failed to configure triggers.");
405 return SR_ERR;
406 }
407
408 /* If the samplerate has not been set, default to 200 kHz. */
409 if (devc->cur_firmware == -1) {
410 if ((ret = sigma_set_samplerate(sdi, SR_KHZ(200))) != SR_OK)
411 return ret;
412 }
413
414 /* Enter trigger programming mode. */
415 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x20, devc);
416
f06fb3e9 417 triggerselect = 0;
3ba56876 418 if (devc->cur_samplerate >= SR_MHZ(100)) {
f06fb3e9 419 /* 100 and 200 MHz mode. */
3ba56876 420 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x81, devc);
421
422 /* Find which pin to trigger on from mask. */
0a1f7b09 423 for (triggerpin = 0; triggerpin < 8; triggerpin++)
3ba56876 424 if ((devc->trigger.risingmask | devc->trigger.fallingmask) &
425 (1 << triggerpin))
426 break;
427
428 /* Set trigger pin and light LED on trigger. */
429 triggerselect = (1 << LEDSEL1) | (triggerpin & 0x7);
430
431 /* Default rising edge. */
432 if (devc->trigger.fallingmask)
433 triggerselect |= 1 << 3;
434
3ba56876 435 } else if (devc->cur_samplerate <= SR_MHZ(50)) {
f06fb3e9 436 /* All other modes. */
3ba56876 437 sigma_build_basic_trigger(&lut, devc);
438
439 sigma_write_trigger_lut(&lut, devc);
440
441 triggerselect = (1 << LEDSEL1) | (1 << LEDSEL0);
442 }
443
444 /* Setup trigger in and out pins to default values. */
445 memset(&triggerinout_conf, 0, sizeof(struct triggerinout));
446 triggerinout_conf.trgout_bytrigger = 1;
447 triggerinout_conf.trgout_enable = 1;
448
449 sigma_write_register(WRITE_TRIGGER_OPTION,
450 (uint8_t *) &triggerinout_conf,
451 sizeof(struct triggerinout), devc);
452
453 /* Go back to normal mode. */
454 sigma_set_register(WRITE_TRIGGER_SELECT1, triggerselect, devc);
455
456 /* Set clock select register. */
8256ed15
GS
457 clockselect.async = 0;
458 clockselect.fraction = 1 - 1; /* Divider 1. */
459 clockselect.disabled_channels = 0x0000; /* All channels enabled. */
460 if (devc->cur_samplerate == SR_MHZ(200)) {
3ba56876 461 /* Enable 4 channels. */
8256ed15
GS
462 clockselect.disabled_channels = 0xf0ff;
463 } else if (devc->cur_samplerate == SR_MHZ(100)) {
3ba56876 464 /* Enable 8 channels. */
8256ed15
GS
465 clockselect.disabled_channels = 0x00ff;
466 } else {
3ba56876 467 /*
8256ed15
GS
468 * 50 MHz mode, or fraction thereof. The 50MHz reference
469 * can get divided by any integer in the range 1 to 256.
470 * Divider minus 1 gets written to the hardware.
471 * (The driver lists a discrete set of sample rates, but
472 * all of them fit the above description.)
3ba56876 473 */
8256ed15 474 clockselect.fraction = SR_MHZ(50) / devc->cur_samplerate - 1;
3ba56876 475 }
8256ed15
GS
476 clock_idx = 0;
477 clock_bytes[clock_idx++] = clockselect.async;
478 clock_bytes[clock_idx++] = clockselect.fraction;
479 clock_bytes[clock_idx++] = clockselect.disabled_channels & 0xff;
480 clock_bytes[clock_idx++] = clockselect.disabled_channels >> 8;
481 sigma_write_register(WRITE_CLOCK_SELECT, clock_bytes, clock_idx, devc);
3ba56876 482
483 /* Setup maximum post trigger time. */
484 sigma_set_register(WRITE_POST_TRIGGER,
485 (devc->capture_ratio * 255) / 100, devc);
486
487 /* Start acqusition. */
2f425a56 488 devc->start_time = g_get_monotonic_time();
22f64ed8
GS
489 regval = WMR_TRGRES | WMR_SDRAMWRITEEN;
490#if ASIX_SIGMA_WITH_TRIGGER
491 regval |= WMR_TRGEN;
492#endif
493 sigma_set_register(WRITE_MODE, regval, devc);
3ba56876 494
bee2b016 495 std_session_send_df_header(sdi);
3ba56876 496
497 /* Add capture source. */
498 sr_session_source_add(sdi->session, -1, 0, 10, sigma_receive_data, (void *)sdi);
499
500 devc->state.state = SIGMA_CAPTURE;
501
502 return SR_OK;
503}
504
695dc859 505static int dev_acquisition_stop(struct sr_dev_inst *sdi)
3ba56876 506{
507 struct dev_context *devc;
508
3ba56876 509 devc = sdi->priv;
3ba56876 510
dde0175d
GS
511 /*
512 * When acquisition is currently running, keep the receive
513 * routine registered and have it stop the acquisition upon the
514 * next invocation. Else unregister the receive routine here
515 * already. The detour is required to have sample data retrieved
516 * for forced acquisition stops.
517 */
518 if (devc->state.state == SIGMA_CAPTURE) {
519 devc->state.state = SIGMA_STOPPING;
520 } else {
521 devc->state.state = SIGMA_IDLE;
522 sr_session_source_remove(sdi->session, -1);
523 }
3ba56876 524
525 return SR_OK;
526}
527
dd5c48a6 528static struct sr_dev_driver asix_sigma_driver_info = {
3ba56876 529 .name = "asix-sigma",
530 .longname = "ASIX SIGMA/SIGMA2",
531 .api_version = 1,
c2fdcc25 532 .init = std_init,
700d6b64 533 .cleanup = std_cleanup,
3ba56876 534 .scan = scan,
c01bf34c 535 .dev_list = std_dev_list,
3ba56876 536 .dev_clear = dev_clear,
537 .config_get = config_get,
538 .config_set = config_set,
539 .config_list = config_list,
540 .dev_open = dev_open,
541 .dev_close = dev_close,
542 .dev_acquisition_start = dev_acquisition_start,
543 .dev_acquisition_stop = dev_acquisition_stop,
544 .context = NULL,
545};
dd5c48a6 546SR_REGISTER_DEV_DRIVER(asix_sigma_driver_info);