]> sigrok.org Git - libsigrok.git/blame - src/hardware/asix-sigma/api.c
asix-sigma: rework time/count limits support, accept more samplerates
[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;
5e78a564
GS
236 devc->samplerate = samplerates[0];
237 sr_sw_limits_init(&devc->cfg_limits);
53a939ab 238 devc->cur_firmware = -1;
53a939ab
GS
239 devc->capture_ratio = 50;
240 devc->use_triggers = 0;
3ba56876 241 }
53a939ab
GS
242 libusb_free_device_list(devlist, 1);
243 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
3ba56876 244
53a939ab 245 return std_scan_complete(di, devices);
3ba56876 246}
247
3ba56876 248static int dev_open(struct sr_dev_inst *sdi)
249{
250 struct dev_context *devc;
53a939ab
GS
251 long vid, pid;
252 const char *serno;
3ba56876 253 int ret;
254
255 devc = sdi->priv;
256
53a939ab
GS
257 if (devc->id.type == ASIX_TYPE_OMEGA && !ASIX_WITH_OMEGA) {
258 sr_err("OMEGA support is not implemented yet.");
259 return SR_ERR_NA;
260 }
261 vid = devc->id.vid;
262 pid = devc->id.pid;
263 serno = sdi->serial_num;
264
265 ret = ftdi_init(&devc->ftdic);
266 if (ret < 0) {
267 sr_err("Cannot initialize FTDI context (%d): %s.",
268 ret, ftdi_get_error_string(&devc->ftdic));
269 return SR_ERR_IO;
270 }
271 ret = ftdi_usb_open_desc_index(&devc->ftdic, vid, pid, NULL, serno, 0);
272 if (ret < 0) {
273 sr_err("Cannot open device (%d): %s.",
274 ret, ftdi_get_error_string(&devc->ftdic));
275 return SR_ERR_IO;
3ba56876 276 }
277
3ba56876 278 return SR_OK;
279}
280
281static int dev_close(struct sr_dev_inst *sdi)
282{
283 struct dev_context *devc;
53a939ab 284 int ret;
3ba56876 285
286 devc = sdi->priv;
287
53a939ab
GS
288 ret = ftdi_usb_close(&devc->ftdic);
289 ftdi_deinit(&devc->ftdic);
290
291 return (ret == 0) ? SR_OK : SR_ERR;
3ba56876 292}
293
dd7a72ea
UH
294static int config_get(uint32_t key, GVariant **data,
295 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
3ba56876 296{
297 struct dev_context *devc;
298
299 (void)cg;
300
301 if (!sdi)
302 return SR_ERR;
303 devc = sdi->priv;
304
305 switch (key) {
53a939ab
GS
306 case SR_CONF_CONN:
307 *data = g_variant_new_string(sdi->connection_id);
308 break;
3ba56876 309 case SR_CONF_SAMPLERATE:
5e78a564 310 *data = g_variant_new_uint64(devc->samplerate);
3ba56876 311 break;
312 case SR_CONF_LIMIT_MSEC:
2f7e529c 313 case SR_CONF_LIMIT_SAMPLES:
5e78a564 314 return sr_sw_limits_config_get(&devc->cfg_limits, key, data);
de3f7acb 315#if ASIX_SIGMA_WITH_TRIGGER
3ba56876 316 case SR_CONF_CAPTURE_RATIO:
317 *data = g_variant_new_uint64(devc->capture_ratio);
318 break;
de3f7acb 319#endif
3ba56876 320 default:
321 return SR_ERR_NA;
322 }
323
324 return SR_OK;
325}
326
dd7a72ea
UH
327static int config_set(uint32_t key, GVariant *data,
328 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
3ba56876 329{
330 struct dev_context *devc;
5e78a564
GS
331 int ret;
332 uint64_t want_rate, have_rate;
3ba56876 333
334 (void)cg;
335
3ba56876 336 devc = sdi->priv;
337
3ba56876 338 switch (key) {
339 case SR_CONF_SAMPLERATE:
5e78a564
GS
340 want_rate = g_variant_get_uint64(data);
341 ret = sigma_normalize_samplerate(want_rate, &have_rate);
342 if (ret != SR_OK)
343 return ret;
344 if (have_rate != want_rate) {
345 char *text_want, *text_have;
346 text_want = sr_samplerate_string(want_rate);
347 text_have = sr_samplerate_string(have_rate);
348 sr_info("Adjusted samplerate %s to %s.",
349 text_want, text_have);
350 g_free(text_want);
351 g_free(text_have);
352 }
353 devc->samplerate = have_rate;
3ba56876 354 break;
5e78a564 355 case SR_CONF_LIMIT_MSEC:
3ba56876 356 case SR_CONF_LIMIT_SAMPLES:
5e78a564 357 return sr_sw_limits_config_set(&devc->cfg_limits, key, data);
de3f7acb 358#if ASIX_SIGMA_WITH_TRIGGER
3ba56876 359 case SR_CONF_CAPTURE_RATIO:
efad7ccc 360 devc->capture_ratio = g_variant_get_uint64(data);
3ba56876 361 break;
de3f7acb 362#endif
3ba56876 363 default:
758906aa 364 return SR_ERR_NA;
3ba56876 365 }
366
758906aa 367 return SR_OK;
3ba56876 368}
369
dd7a72ea
UH
370static int config_list(uint32_t key, GVariant **data,
371 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
3ba56876 372{
3ba56876 373 switch (key) {
53a939ab 374 case SR_CONF_SCAN_OPTIONS:
3ba56876 375 case SR_CONF_DEVICE_OPTIONS:
53a939ab
GS
376 if (cg)
377 return SR_ERR_NA;
378 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
3ba56876 379 case SR_CONF_SAMPLERATE:
463160cb 380 *data = std_gvar_samplerates(samplerates, samplerates_count);
3ba56876 381 break;
de3f7acb 382#if ASIX_SIGMA_WITH_TRIGGER
3ba56876 383 case SR_CONF_TRIGGER_MATCH:
53012da6 384 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
3ba56876 385 break;
de3f7acb 386#endif
3ba56876 387 default:
388 return SR_ERR_NA;
389 }
390
391 return SR_OK;
392}
393
695dc859 394static int dev_acquisition_start(const struct sr_dev_inst *sdi)
3ba56876 395{
396 struct dev_context *devc;
397 struct clockselect_50 clockselect;
8256ed15 398 int triggerpin, ret;
f06fb3e9 399 uint8_t triggerselect;
3ba56876 400 struct triggerinout triggerinout_conf;
401 struct triggerlut lut;
22f64ed8 402 uint8_t regval;
8256ed15
GS
403 uint8_t clock_bytes[sizeof(clockselect)];
404 size_t clock_idx;
3ba56876 405
3ba56876 406 devc = sdi->priv;
407
5e78a564
GS
408 /*
409 * Setup the device's samplerate from the value which up to now
410 * just got checked and stored. As a byproduct this can pick and
411 * send firmware to the device, reduce the number of available
412 * logic channels, etc.
413 *
414 * Determine an acquisition timeout from optionally configured
415 * sample count or time limits. Which depends on the samplerate.
416 */
417 ret = sigma_set_samplerate(sdi);
418 if (ret != SR_OK)
419 return ret;
420 ret = sigma_set_acquire_timeout(devc);
421 if (ret != SR_OK)
422 return ret;
423
3ba56876 424 if (sigma_convert_trigger(sdi) != SR_OK) {
425 sr_err("Failed to configure triggers.");
426 return SR_ERR;
427 }
428
3ba56876 429 /* Enter trigger programming mode. */
9fb4c632 430 sigma_set_register(WRITE_TRIGGER_SELECT2, 0x20, devc);
3ba56876 431
f06fb3e9 432 triggerselect = 0;
5e78a564 433 if (devc->samplerate >= SR_MHZ(100)) {
f06fb3e9 434 /* 100 and 200 MHz mode. */
9fb4c632 435 sigma_set_register(WRITE_TRIGGER_SELECT2, 0x81, devc);
3ba56876 436
437 /* Find which pin to trigger on from mask. */
0a1f7b09 438 for (triggerpin = 0; triggerpin < 8; triggerpin++)
3ba56876 439 if ((devc->trigger.risingmask | devc->trigger.fallingmask) &
440 (1 << triggerpin))
441 break;
442
443 /* Set trigger pin and light LED on trigger. */
444 triggerselect = (1 << LEDSEL1) | (triggerpin & 0x7);
445
446 /* Default rising edge. */
447 if (devc->trigger.fallingmask)
448 triggerselect |= 1 << 3;
449
5e78a564 450 } else if (devc->samplerate <= SR_MHZ(50)) {
f06fb3e9 451 /* All other modes. */
3ba56876 452 sigma_build_basic_trigger(&lut, devc);
453
454 sigma_write_trigger_lut(&lut, devc);
455
456 triggerselect = (1 << LEDSEL1) | (1 << LEDSEL0);
457 }
458
459 /* Setup trigger in and out pins to default values. */
460 memset(&triggerinout_conf, 0, sizeof(struct triggerinout));
461 triggerinout_conf.trgout_bytrigger = 1;
462 triggerinout_conf.trgout_enable = 1;
463
464 sigma_write_register(WRITE_TRIGGER_OPTION,
465 (uint8_t *) &triggerinout_conf,
466 sizeof(struct triggerinout), devc);
467
468 /* Go back to normal mode. */
9fb4c632 469 sigma_set_register(WRITE_TRIGGER_SELECT2, triggerselect, devc);
3ba56876 470
471 /* Set clock select register. */
8256ed15
GS
472 clockselect.async = 0;
473 clockselect.fraction = 1 - 1; /* Divider 1. */
474 clockselect.disabled_channels = 0x0000; /* All channels enabled. */
5e78a564 475 if (devc->samplerate == SR_MHZ(200)) {
3ba56876 476 /* Enable 4 channels. */
8256ed15 477 clockselect.disabled_channels = 0xf0ff;
5e78a564 478 } else if (devc->samplerate == SR_MHZ(100)) {
3ba56876 479 /* Enable 8 channels. */
8256ed15
GS
480 clockselect.disabled_channels = 0x00ff;
481 } else {
3ba56876 482 /*
8256ed15
GS
483 * 50 MHz mode, or fraction thereof. The 50MHz reference
484 * can get divided by any integer in the range 1 to 256.
485 * Divider minus 1 gets written to the hardware.
486 * (The driver lists a discrete set of sample rates, but
487 * all of them fit the above description.)
3ba56876 488 */
5e78a564 489 clockselect.fraction = SR_MHZ(50) / devc->samplerate - 1;
3ba56876 490 }
8256ed15
GS
491 clock_idx = 0;
492 clock_bytes[clock_idx++] = clockselect.async;
493 clock_bytes[clock_idx++] = clockselect.fraction;
494 clock_bytes[clock_idx++] = clockselect.disabled_channels & 0xff;
495 clock_bytes[clock_idx++] = clockselect.disabled_channels >> 8;
496 sigma_write_register(WRITE_CLOCK_SELECT, clock_bytes, clock_idx, devc);
3ba56876 497
498 /* Setup maximum post trigger time. */
499 sigma_set_register(WRITE_POST_TRIGGER,
500 (devc->capture_ratio * 255) / 100, devc);
501
502 /* Start acqusition. */
22f64ed8
GS
503 regval = WMR_TRGRES | WMR_SDRAMWRITEEN;
504#if ASIX_SIGMA_WITH_TRIGGER
505 regval |= WMR_TRGEN;
506#endif
507 sigma_set_register(WRITE_MODE, regval, devc);
3ba56876 508
bee2b016 509 std_session_send_df_header(sdi);
3ba56876 510
511 /* Add capture source. */
512 sr_session_source_add(sdi->session, -1, 0, 10, sigma_receive_data, (void *)sdi);
513
514 devc->state.state = SIGMA_CAPTURE;
515
516 return SR_OK;
517}
518
695dc859 519static int dev_acquisition_stop(struct sr_dev_inst *sdi)
3ba56876 520{
521 struct dev_context *devc;
522
3ba56876 523 devc = sdi->priv;
3ba56876 524
dde0175d
GS
525 /*
526 * When acquisition is currently running, keep the receive
527 * routine registered and have it stop the acquisition upon the
528 * next invocation. Else unregister the receive routine here
529 * already. The detour is required to have sample data retrieved
530 * for forced acquisition stops.
531 */
532 if (devc->state.state == SIGMA_CAPTURE) {
533 devc->state.state = SIGMA_STOPPING;
534 } else {
535 devc->state.state = SIGMA_IDLE;
536 sr_session_source_remove(sdi->session, -1);
537 }
3ba56876 538
539 return SR_OK;
540}
541
dd5c48a6 542static struct sr_dev_driver asix_sigma_driver_info = {
3ba56876 543 .name = "asix-sigma",
544 .longname = "ASIX SIGMA/SIGMA2",
545 .api_version = 1,
c2fdcc25 546 .init = std_init,
700d6b64 547 .cleanup = std_cleanup,
3ba56876 548 .scan = scan,
c01bf34c 549 .dev_list = std_dev_list,
3ba56876 550 .dev_clear = dev_clear,
551 .config_get = config_get,
552 .config_set = config_set,
553 .config_list = config_list,
554 .dev_open = dev_open,
555 .dev_close = dev_close,
556 .dev_acquisition_start = dev_acquisition_start,
557 .dev_acquisition_stop = dev_acquisition_stop,
558 .context = NULL,
559};
dd5c48a6 560SR_REGISTER_DEV_DRIVER(asix_sigma_driver_info);