]> sigrok.org Git - libsigrok.git/blame - hardware/asix-sigma/asix-sigma.c
asix-sigma: Cleanup FPGA initialization
[libsigrok.git] / hardware / asix-sigma / asix-sigma.c
CommitLineData
28a35d8a 1/*
50985c20 2 * This file is part of the libsigrok project.
28a35d8a 3 *
868501fa 4 * Copyright (C) 2010-2012 Håvard Espeland <gus@ping.uio.no>,
911f1834
UH
5 * Copyright (C) 2010 Martin Stensgård <mastensg@ping.uio.no>
6 * Copyright (C) 2010 Carl Henrik Lunde <chlunde@ping.uio.no>
28a35d8a
HE
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
911f1834 22/*
6352d030 23 * ASIX SIGMA/SIGMA2 logic analyzer driver
911f1834
UH
24 */
25
3bbd9849
UH
26#include <glib.h>
27#include <glib/gstdio.h>
28a35d8a
HE
28#include <ftdi.h>
29#include <string.h>
45c59c8b
BV
30#include "libsigrok.h"
31#include "libsigrok-internal.h"
28a35d8a
HE
32#include "asix-sigma.h"
33
34#define USB_VENDOR 0xa600
35#define USB_PRODUCT 0xa000
36#define USB_DESCRIPTION "ASIX SIGMA"
37#define USB_VENDOR_NAME "ASIX"
38#define USB_MODEL_NAME "SIGMA"
c50277a6 39#define TRIGGER_TYPE "rf10"
28a35d8a 40
ed300b9f 41SR_PRIV struct sr_dev_driver asix_sigma_driver_info;
a873c594 42static struct sr_dev_driver *di = &asix_sigma_driver_info;
6078d2c9 43static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
28a35d8a 44
b1648dea
MV
45/*
46 * The ASIX Sigma supports arbitrary integer frequency divider in
47 * the 50MHz mode. The divider is in range 1...256 , allowing for
48 * very precise sampling rate selection. This driver supports only
49 * a subset of the sampling rates.
50 */
2c9c0df8 51static const uint64_t samplerates[] = {
b1648dea
MV
52 SR_KHZ(200), /* div=250 */
53 SR_KHZ(250), /* div=200 */
54 SR_KHZ(500), /* div=100 */
55 SR_MHZ(1), /* div=50 */
56 SR_MHZ(5), /* div=10 */
57 SR_MHZ(10), /* div=5 */
58 SR_MHZ(25), /* div=2 */
59 SR_MHZ(50), /* div=1 */
60 SR_MHZ(100), /* Special FW needed */
61 SR_MHZ(200), /* Special FW needed */
28a35d8a
HE
62};
63
d261dbbf 64/*
ba7dd8bb 65 * Channel numbers seem to go from 1-16, according to this image:
d261dbbf
UH
66 * http://tools.asix.net/img/sigma_sigmacab_pins_720.jpg
67 * (the cable has two additional GND pins, and a TI and TO pin)
68 */
790c7ccc 69static const char *channel_names[] = {
78693401
UH
70 "1", "2", "3", "4", "5", "6", "7", "8",
71 "9", "10", "11", "12", "13", "14", "15", "16",
464d12c7
KS
72};
73
2c9c0df8 74static const int32_t hwcaps[] = {
1953564a
BV
75 SR_CONF_LOGIC_ANALYZER,
76 SR_CONF_SAMPLERATE,
38d32464 77 SR_CONF_TRIGGER_TYPE,
1953564a 78 SR_CONF_CAPTURE_RATIO,
1953564a 79 SR_CONF_LIMIT_MSEC,
6868626b 80 SR_CONF_LIMIT_SAMPLES,
28a35d8a
HE
81};
82
fefa1800
UH
83/* Initialize the logic analyzer mode. */
84static uint8_t logic_mode_start[] = {
85 0x00, 0x40, 0x0f, 0x25, 0x35, 0x40,
86 0x2a, 0x3a, 0x40, 0x03, 0x20, 0x38,
87};
88
eec5275e 89static const char *firmware_files[] = {
a8116d76
HE
90 "asix-sigma-50.fw", /* 50 MHz, supports 8 bit fractions */
91 "asix-sigma-100.fw", /* 100 MHz */
92 "asix-sigma-200.fw", /* 200 MHz */
ed09fd07 93 "asix-sigma-50sync.fw", /* Synchronous clock from pin */
a8116d76 94 "asix-sigma-phasor.fw", /* Frequency counter */
f6564c8d
HE
95};
96
0e1357e8 97static int sigma_read(void *buf, size_t size, struct dev_context *devc)
28a35d8a
HE
98{
99 int ret;
fefa1800 100
0e1357e8 101 ret = ftdi_read_data(&devc->ftdic, (unsigned char *)buf, size);
28a35d8a 102 if (ret < 0) {
47f4f073 103 sr_err("ftdi_read_data failed: %s",
0e1357e8 104 ftdi_get_error_string(&devc->ftdic));
28a35d8a
HE
105 }
106
107 return ret;
108}
109
0e1357e8 110static int sigma_write(void *buf, size_t size, struct dev_context *devc)
28a35d8a
HE
111{
112 int ret;
fefa1800 113
0e1357e8 114 ret = ftdi_write_data(&devc->ftdic, (unsigned char *)buf, size);
28a35d8a 115 if (ret < 0) {
47f4f073 116 sr_err("ftdi_write_data failed: %s",
0e1357e8 117 ftdi_get_error_string(&devc->ftdic));
fefa1800 118 } else if ((size_t) ret != size) {
47f4f073 119 sr_err("ftdi_write_data did not complete write.");
28a35d8a
HE
120 }
121
122 return ret;
123}
124
99965709 125static int sigma_write_register(uint8_t reg, uint8_t *data, size_t len,
0e1357e8 126 struct dev_context *devc)
28a35d8a
HE
127{
128 size_t i;
129 uint8_t buf[len + 2];
130 int idx = 0;
131
132 buf[idx++] = REG_ADDR_LOW | (reg & 0xf);
133 buf[idx++] = REG_ADDR_HIGH | (reg >> 4);
134
fefa1800 135 for (i = 0; i < len; ++i) {
28a35d8a
HE
136 buf[idx++] = REG_DATA_LOW | (data[i] & 0xf);
137 buf[idx++] = REG_DATA_HIGH_WRITE | (data[i] >> 4);
138 }
139
0e1357e8 140 return sigma_write(buf, idx, devc);
28a35d8a
HE
141}
142
0e1357e8 143static int sigma_set_register(uint8_t reg, uint8_t value, struct dev_context *devc)
28a35d8a 144{
0e1357e8 145 return sigma_write_register(reg, &value, 1, devc);
28a35d8a
HE
146}
147
99965709 148static int sigma_read_register(uint8_t reg, uint8_t *data, size_t len,
0e1357e8 149 struct dev_context *devc)
28a35d8a
HE
150{
151 uint8_t buf[3];
fefa1800 152
28a35d8a
HE
153 buf[0] = REG_ADDR_LOW | (reg & 0xf);
154 buf[1] = REG_ADDR_HIGH | (reg >> 4);
28a35d8a
HE
155 buf[2] = REG_READ_ADDR;
156
0e1357e8 157 sigma_write(buf, sizeof(buf), devc);
28a35d8a 158
0e1357e8 159 return sigma_read(data, len, devc);
28a35d8a
HE
160}
161
0e1357e8 162static uint8_t sigma_get_register(uint8_t reg, struct dev_context *devc)
28a35d8a
HE
163{
164 uint8_t value;
fefa1800 165
0e1357e8 166 if (1 != sigma_read_register(reg, &value, 1, devc)) {
47f4f073 167 sr_err("sigma_get_register: 1 byte expected");
28a35d8a
HE
168 return 0;
169 }
170
171 return value;
172}
173
99965709 174static int sigma_read_pos(uint32_t *stoppos, uint32_t *triggerpos,
0e1357e8 175 struct dev_context *devc)
28a35d8a
HE
176{
177 uint8_t buf[] = {
178 REG_ADDR_LOW | READ_TRIGGER_POS_LOW,
179
180 REG_READ_ADDR | NEXT_REG,
181 REG_READ_ADDR | NEXT_REG,
182 REG_READ_ADDR | NEXT_REG,
183 REG_READ_ADDR | NEXT_REG,
184 REG_READ_ADDR | NEXT_REG,
185 REG_READ_ADDR | NEXT_REG,
186 };
28a35d8a
HE
187 uint8_t result[6];
188
0e1357e8 189 sigma_write(buf, sizeof(buf), devc);
28a35d8a 190
0e1357e8 191 sigma_read(result, sizeof(result), devc);
28a35d8a
HE
192
193 *triggerpos = result[0] | (result[1] << 8) | (result[2] << 16);
194 *stoppos = result[3] | (result[4] << 8) | (result[5] << 16);
195
57bbf56b
HE
196 /* Not really sure why this must be done, but according to spec. */
197 if ((--*stoppos & 0x1ff) == 0x1ff)
198 stoppos -= 64;
199
200 if ((*--triggerpos & 0x1ff) == 0x1ff)
201 triggerpos -= 64;
202
28a35d8a
HE
203 return 1;
204}
205
99965709 206static int sigma_read_dram(uint16_t startchunk, size_t numchunks,
0e1357e8 207 uint8_t *data, struct dev_context *devc)
28a35d8a
HE
208{
209 size_t i;
210 uint8_t buf[4096];
211 int idx = 0;
212
fefa1800 213 /* Send the startchunk. Index start with 1. */
28a35d8a
HE
214 buf[0] = startchunk >> 8;
215 buf[1] = startchunk & 0xff;
0e1357e8 216 sigma_write_register(WRITE_MEMROW, buf, 2, devc);
28a35d8a 217
fefa1800 218 /* Read the DRAM. */
28a35d8a
HE
219 buf[idx++] = REG_DRAM_BLOCK;
220 buf[idx++] = REG_DRAM_WAIT_ACK;
221
222 for (i = 0; i < numchunks; ++i) {
fefa1800
UH
223 /* Alternate bit to copy from DRAM to cache. */
224 if (i != (numchunks - 1))
225 buf[idx++] = REG_DRAM_BLOCK | (((i + 1) % 2) << 4);
28a35d8a
HE
226
227 buf[idx++] = REG_DRAM_BLOCK_DATA | ((i % 2) << 4);
228
fefa1800 229 if (i != (numchunks - 1))
28a35d8a
HE
230 buf[idx++] = REG_DRAM_WAIT_ACK;
231 }
232
0e1357e8 233 sigma_write(buf, idx, devc);
28a35d8a 234
0e1357e8 235 return sigma_read(data, numchunks * CHUNK_SIZE, devc);
28a35d8a
HE
236}
237
4ae1f451 238/* Upload trigger look-up tables to Sigma. */
0e1357e8 239static int sigma_write_trigger_lut(struct triggerlut *lut, struct dev_context *devc)
ee492173
HE
240{
241 int i;
242 uint8_t tmp[2];
243 uint16_t bit;
244
245 /* Transpose the table and send to Sigma. */
246 for (i = 0; i < 16; ++i) {
247 bit = 1 << i;
248
249 tmp[0] = tmp[1] = 0;
250
251 if (lut->m2d[0] & bit)
252 tmp[0] |= 0x01;
253 if (lut->m2d[1] & bit)
254 tmp[0] |= 0x02;
255 if (lut->m2d[2] & bit)
256 tmp[0] |= 0x04;
257 if (lut->m2d[3] & bit)
258 tmp[0] |= 0x08;
259
260 if (lut->m3 & bit)
261 tmp[0] |= 0x10;
262 if (lut->m3s & bit)
263 tmp[0] |= 0x20;
264 if (lut->m4 & bit)
265 tmp[0] |= 0x40;
266
267 if (lut->m0d[0] & bit)
268 tmp[1] |= 0x01;
269 if (lut->m0d[1] & bit)
270 tmp[1] |= 0x02;
271 if (lut->m0d[2] & bit)
272 tmp[1] |= 0x04;
273 if (lut->m0d[3] & bit)
274 tmp[1] |= 0x08;
275
276 if (lut->m1d[0] & bit)
277 tmp[1] |= 0x10;
278 if (lut->m1d[1] & bit)
279 tmp[1] |= 0x20;
280 if (lut->m1d[2] & bit)
281 tmp[1] |= 0x40;
282 if (lut->m1d[3] & bit)
283 tmp[1] |= 0x80;
284
99965709 285 sigma_write_register(WRITE_TRIGGER_SELECT0, tmp, sizeof(tmp),
0e1357e8
BV
286 devc);
287 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x30 | i, devc);
ee492173
HE
288 }
289
290 /* Send the parameters */
291 sigma_write_register(WRITE_TRIGGER_SELECT0, (uint8_t *) &lut->params,
0e1357e8 292 sizeof(lut->params), devc);
ee492173 293
e46b8fb1 294 return SR_OK;
ee492173
HE
295}
296
fefa1800 297/* Generate the bitbang stream for programming the FPGA. */
28a35d8a 298static int bin2bitbang(const char *filename,
fefa1800 299 unsigned char **buf, size_t *buf_size)
28a35d8a 300{
fefa1800 301 FILE *f;
e3fff420 302 unsigned long file_size;
28a35d8a
HE
303 unsigned long offset = 0;
304 unsigned char *p;
e3fff420
HE
305 uint8_t *firmware;
306 unsigned long fwsize = 0;
28a35d8a
HE
307 const int buffer_size = 65536;
308 size_t i;
e3fff420 309 int c, bit, v;
fefa1800 310 uint32_t imm = 0x3f6df2ab;
28a35d8a 311
868d8cef 312 f = g_fopen(filename, "rb");
28a35d8a 313 if (!f) {
47f4f073 314 sr_err("g_fopen(\"%s\", \"rb\")", filename);
b53738ba 315 return SR_ERR;
28a35d8a
HE
316 }
317
318 if (-1 == fseek(f, 0, SEEK_END)) {
47f4f073 319 sr_err("fseek on %s failed", filename);
28a35d8a 320 fclose(f);
b53738ba 321 return SR_ERR;
28a35d8a
HE
322 }
323
324 file_size = ftell(f);
325
326 fseek(f, 0, SEEK_SET);
327
b53738ba 328 if (!(firmware = g_try_malloc(buffer_size))) {
47f4f073 329 sr_err("%s: firmware malloc failed", __func__);
12ad53f5 330 fclose(f);
b53738ba 331 return SR_ERR_MALLOC;
28a35d8a
HE
332 }
333
28a35d8a
HE
334 while ((c = getc(f)) != EOF) {
335 imm = (imm + 0xa853753) % 177 + (imm * 0x8034052);
e3fff420 336 firmware[fwsize++] = c ^ imm;
28a35d8a
HE
337 }
338 fclose(f);
339
e3fff420 340 if(fwsize != file_size) {
47f4f073 341 sr_err("%s: Error reading firmware", filename);
e3fff420
HE
342 fclose(f);
343 g_free(firmware);
344 return SR_ERR;
28a35d8a
HE
345 }
346
28a35d8a
HE
347 *buf_size = fwsize * 2 * 8;
348
b53738ba 349 *buf = p = (unsigned char *)g_try_malloc(*buf_size);
28a35d8a 350 if (!p) {
47f4f073 351 sr_err("%s: buf/p malloc failed", __func__);
12ad53f5 352 g_free(firmware);
b53738ba 353 return SR_ERR_MALLOC;
28a35d8a
HE
354 }
355
356 for (i = 0; i < fwsize; ++i) {
28a35d8a 357 for (bit = 7; bit >= 0; --bit) {
fefa1800 358 v = firmware[i] & 1 << bit ? 0x40 : 0x00;
28a35d8a
HE
359 p[offset++] = v | 0x01;
360 p[offset++] = v;
361 }
362 }
363
364 g_free(firmware);
365
366 if (offset != *buf_size) {
367 g_free(*buf);
47f4f073 368 sr_err("Error reading firmware %s "
0aeb0ccd 369 "offset=%ld, file_size=%ld, buf_size=%zd.",
133a37bf 370 filename, offset, file_size, *buf_size);
28a35d8a 371
b53738ba 372 return SR_ERR;
28a35d8a
HE
373 }
374
b53738ba 375 return SR_OK;
28a35d8a
HE
376}
377
3678cf73 378static void clear_helper(void *priv)
0448d110 379{
0e1357e8 380 struct dev_context *devc;
ce4d26dd 381
3678cf73 382 devc = priv;
0e1357e8 383
3678cf73
UH
384 ftdi_deinit(&devc->ftdic);
385}
0448d110 386
3b412e3a 387static int dev_clear(void)
3678cf73
UH
388{
389 return std_dev_clear(di, clear_helper);
0448d110
BV
390}
391
6078d2c9 392static int init(struct sr_context *sr_ctx)
61136ea6 393{
f6beaac5 394 return std_init(sr_ctx, di, LOG_PREFIX);
61136ea6
BV
395}
396
6078d2c9 397static GSList *scan(GSList *options)
28a35d8a 398{
d68e2d1a 399 struct sr_dev_inst *sdi;
ba7dd8bb 400 struct sr_channel *ch;
0e1357e8
BV
401 struct drv_context *drvc;
402 struct dev_context *devc;
0448d110 403 GSList *devices;
e3fff420
HE
404 struct ftdi_device_list *devlist;
405 char serial_txt[10];
406 uint32_t serial;
790c7ccc
MV
407 int ret;
408 unsigned int i;
28a35d8a 409
0448d110 410 (void)options;
64d33dc2 411
a873c594 412 drvc = di->priv;
4b97c74e 413
0448d110 414 devices = NULL;
4b97c74e 415
0e1357e8 416 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
47f4f073 417 sr_err("%s: devc malloc failed", __func__);
0448d110 418 return NULL;
b53738ba 419 }
99965709 420
0e1357e8 421 ftdi_init(&devc->ftdic);
28a35d8a 422
fefa1800 423 /* Look for SIGMAs. */
e3fff420 424
0e1357e8 425 if ((ret = ftdi_usb_find_all(&devc->ftdic, &devlist,
eec944c5
BV
426 USB_VENDOR, USB_PRODUCT)) <= 0) {
427 if (ret < 0)
428 sr_err("ftdi_usb_find_all(): %d", ret);
99965709 429 goto free;
eec944c5 430 }
99965709 431
e3fff420 432 /* Make sure it's a version 1 or 2 SIGMA. */
0e1357e8 433 ftdi_usb_get_strings(&devc->ftdic, devlist->dev, NULL, 0, NULL, 0,
6352d030 434 serial_txt, sizeof(serial_txt));
e3fff420
HE
435 sscanf(serial_txt, "%x", &serial);
436
6352d030 437 if (serial < 0xa6010000 || serial > 0xa602ffff) {
47f4f073
UH
438 sr_err("Only SIGMA and SIGMA2 are supported "
439 "in this version of libsigrok.");
e3fff420
HE
440 goto free;
441 }
442
443 sr_info("Found ASIX SIGMA - Serial: %s", serial_txt);
444
0e1357e8
BV
445 devc->cur_samplerate = 0;
446 devc->period_ps = 0;
447 devc->limit_msec = 0;
448 devc->cur_firmware = -1;
ba7dd8bb 449 devc->num_channels = 0;
0e1357e8
BV
450 devc->samples_per_event = 0;
451 devc->capture_ratio = 50;
452 devc->use_triggers = 0;
28a35d8a 453
fefa1800 454 /* Register SIGMA device. */
d68e2d1a 455 if (!(sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING, USB_VENDOR_NAME,
55f98c65 456 USB_MODEL_NAME, NULL))) {
47f4f073 457 sr_err("%s: sdi was NULL", __func__);
99965709 458 goto free;
d68e2d1a 459 }
a873c594 460 sdi->driver = di;
87ca93c5 461
790c7ccc
MV
462 for (i = 0; i < ARRAY_SIZE(channel_names); i++) {
463 ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
464 channel_names[i]);
465 if (!ch)
87ca93c5 466 return NULL;
ba7dd8bb 467 sdi->channels = g_slist_append(sdi->channels, ch);
87ca93c5
BV
468 }
469
0448d110 470 devices = g_slist_append(devices, sdi);
0e1357e8
BV
471 drvc->instances = g_slist_append(drvc->instances, sdi);
472 sdi->priv = devc;
28a35d8a 473
fefa1800 474 /* We will open the device again when we need it. */
e3fff420 475 ftdi_list_free(&devlist);
28a35d8a 476
0448d110 477 return devices;
ea9cfed7 478
99965709 479free:
0e1357e8
BV
480 ftdi_deinit(&devc->ftdic);
481 g_free(devc);
0448d110 482 return NULL;
28a35d8a
HE
483}
484
6078d2c9 485static GSList *dev_list(void)
811deee4 486{
0e94d524 487 return ((struct drv_context *)(di->priv))->instances;
811deee4
BV
488}
489
d5fa188a
MV
490/*
491 * Configure the FPGA for bitbang mode.
492 * This sequence is documented in section 2. of the ASIX Sigma programming
493 * manual. This sequence is necessary to configure the FPGA in the Sigma
494 * into Bitbang mode, in which it can be programmed with the firmware.
495 */
496static int sigma_fpga_init_bitbang(struct dev_context *devc)
497{
498 uint8_t suicide[] = {
499 0x84, 0x84, 0x88, 0x84, 0x88, 0x84, 0x88, 0x84,
500 };
501 uint8_t init_array[] = {
502 0x01, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
503 0x01, 0x01,
504 };
505 int i, ret, timeout = 10000;
506 uint8_t data;
507
508 /* Section 2. part 1), do the FPGA suicide. */
509 sigma_write(suicide, sizeof(suicide), devc);
510 sigma_write(suicide, sizeof(suicide), devc);
511 sigma_write(suicide, sizeof(suicide), devc);
512 sigma_write(suicide, sizeof(suicide), devc);
513
514 /* Section 2. part 2), do pulse on D1. */
515 sigma_write(init_array, sizeof(init_array), devc);
516 ftdi_usb_purge_buffers(&devc->ftdic);
517
518 /* Wait until the FPGA asserts D6/INIT_B. */
519 for (i = 0; i < timeout; i++) {
520 ret = sigma_read(&data, 1, devc);
521 if (ret < 0)
522 return ret;
523 /* Test if pin D6 got asserted. */
524 if (data & (1 << 5))
525 return 0;
526 /* The D6 was not asserted yet, wait a bit. */
527 usleep(10000);
528 }
529
530 return SR_ERR_TIMEOUT;
531}
532
0e1357e8 533static int upload_firmware(int firmware_idx, struct dev_context *devc)
28a35d8a
HE
534{
535 int ret;
536 unsigned char *buf;
537 unsigned char pins;
538 size_t buf_size;
28a35d8a 539 unsigned char result[32];
e8397563 540 char firmware_path[128];
28a35d8a 541
fefa1800 542 /* Make sure it's an ASIX SIGMA. */
0e1357e8 543 if ((ret = ftdi_usb_open_desc(&devc->ftdic,
28a35d8a 544 USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
47f4f073 545 sr_err("ftdi_usb_open failed: %s",
0e1357e8 546 ftdi_get_error_string(&devc->ftdic));
28a35d8a
HE
547 return 0;
548 }
549
0e1357e8 550 if ((ret = ftdi_set_bitmode(&devc->ftdic, 0xdf, BITMODE_BITBANG)) < 0) {
47f4f073 551 sr_err("ftdi_set_bitmode failed: %s",
0e1357e8 552 ftdi_get_error_string(&devc->ftdic));
28a35d8a
HE
553 return 0;
554 }
555
fefa1800 556 /* Four times the speed of sigmalogan - Works well. */
0e1357e8 557 if ((ret = ftdi_set_baudrate(&devc->ftdic, 750000)) < 0) {
47f4f073 558 sr_err("ftdi_set_baudrate failed: %s",
0e1357e8 559 ftdi_get_error_string(&devc->ftdic));
28a35d8a
HE
560 return 0;
561 }
562
d5fa188a
MV
563 /* Initialize the FPGA for firmware upload. */
564 ret = sigma_fpga_init_bitbang(devc);
565 if (ret)
566 return ret;
28a35d8a 567
9ddb2a12 568 /* Prepare firmware. */
e8397563 569 snprintf(firmware_path, sizeof(firmware_path), "%s/%s", FIRMWARE_DIR,
f6564c8d
HE
570 firmware_files[firmware_idx]);
571
b53738ba 572 if ((ret = bin2bitbang(firmware_path, &buf, &buf_size)) != SR_OK) {
47f4f073 573 sr_err("An error occured while reading the firmware: %s",
133a37bf 574 firmware_path);
b53738ba 575 return ret;
28a35d8a
HE
576 }
577
fefa1800 578 /* Upload firmare. */
47f4f073 579 sr_info("Uploading firmware file '%s'.", firmware_files[firmware_idx]);
0e1357e8 580 sigma_write(buf, buf_size, devc);
28a35d8a
HE
581
582 g_free(buf);
583
0e1357e8 584 if ((ret = ftdi_set_bitmode(&devc->ftdic, 0x00, BITMODE_RESET)) < 0) {
47f4f073 585 sr_err("ftdi_set_bitmode failed: %s",
0e1357e8 586 ftdi_get_error_string(&devc->ftdic));
e46b8fb1 587 return SR_ERR;
28a35d8a
HE
588 }
589
0e1357e8 590 ftdi_usb_purge_buffers(&devc->ftdic);
28a35d8a 591
fefa1800 592 /* Discard garbage. */
0e1357e8 593 while (1 == sigma_read(&pins, 1, devc))
28a35d8a
HE
594 ;
595
fefa1800 596 /* Initialize the logic analyzer mode. */
0e1357e8 597 sigma_write(logic_mode_start, sizeof(logic_mode_start), devc);
28a35d8a 598
fefa1800 599 /* Expect a 3 byte reply. */
0e1357e8 600 ret = sigma_read(result, 3, devc);
28a35d8a
HE
601 if (ret != 3 ||
602 result[0] != 0xa6 || result[1] != 0x55 || result[2] != 0xaa) {
47f4f073 603 sr_err("Configuration failed. Invalid reply received.");
e46b8fb1 604 return SR_ERR;
28a35d8a
HE
605 }
606
0e1357e8 607 devc->cur_firmware = firmware_idx;
f6564c8d 608
47f4f073 609 sr_info("Firmware uploaded.");
e3fff420 610
e46b8fb1 611 return SR_OK;
f6564c8d
HE
612}
613
6078d2c9 614static int dev_open(struct sr_dev_inst *sdi)
f6564c8d 615{
0e1357e8 616 struct dev_context *devc;
f6564c8d
HE
617 int ret;
618
0e1357e8 619 devc = sdi->priv;
99965709 620
9ddb2a12 621 /* Make sure it's an ASIX SIGMA. */
0e1357e8 622 if ((ret = ftdi_usb_open_desc(&devc->ftdic,
f6564c8d
HE
623 USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
624
47f4f073 625 sr_err("ftdi_usb_open failed: %s",
0e1357e8 626 ftdi_get_error_string(&devc->ftdic));
f6564c8d
HE
627
628 return 0;
629 }
28a35d8a 630
5a2326a7 631 sdi->status = SR_ST_ACTIVE;
28a35d8a 632
e46b8fb1 633 return SR_OK;
f6564c8d
HE
634}
635
6f4b1868 636static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
f6564c8d 637{
2c9c0df8
BV
638 struct dev_context *devc;
639 unsigned int i;
640 int ret;
f6564c8d 641
2c9c0df8 642 devc = sdi->priv;
f4abaa9f
UH
643 ret = SR_OK;
644
2c9c0df8
BV
645 for (i = 0; i < ARRAY_SIZE(samplerates); i++) {
646 if (samplerates[i] == samplerate)
f6564c8d
HE
647 break;
648 }
2c9c0df8 649 if (samplerates[i] == 0)
e46b8fb1 650 return SR_ERR_SAMPLERATE;
f6564c8d 651
59df0c77 652 if (samplerate <= SR_MHZ(50)) {
0e1357e8 653 ret = upload_firmware(0, devc);
ba7dd8bb 654 devc->num_channels = 16;
e8397563 655 }
59df0c77 656 if (samplerate == SR_MHZ(100)) {
0e1357e8 657 ret = upload_firmware(1, devc);
ba7dd8bb 658 devc->num_channels = 8;
f78898e9 659 }
59df0c77 660 else if (samplerate == SR_MHZ(200)) {
0e1357e8 661 ret = upload_firmware(2, devc);
ba7dd8bb 662 devc->num_channels = 4;
f78898e9 663 }
f6564c8d 664
0e1357e8 665 devc->cur_samplerate = samplerate;
5edc02c7 666 devc->period_ps = 1000000000000ULL / samplerate;
ba7dd8bb 667 devc->samples_per_event = 16 / devc->num_channels;
0e1357e8 668 devc->state.state = SIGMA_IDLE;
f6564c8d 669
e8397563 670 return ret;
28a35d8a
HE
671}
672
c53d793f
HE
673/*
674 * In 100 and 200 MHz mode, only a single pin rising/falling can be
675 * set as trigger. In other modes, two rising/falling triggers can be set,
ba7dd8bb 676 * in addition to value/mask trigger for any number of channels.
c53d793f
HE
677 *
678 * The Sigma supports complex triggers using boolean expressions, but this
679 * has not been implemented yet.
680 */
ba7dd8bb 681static int configure_channels(const struct sr_dev_inst *sdi)
57bbf56b 682{
0e1357e8 683 struct dev_context *devc = sdi->priv;
ba7dd8bb 684 const struct sr_channel *ch;
1b79df2f 685 const GSList *l;
57bbf56b 686 int trigger_set = 0;
ba7dd8bb 687 int channelbit;
57bbf56b 688
0e1357e8 689 memset(&devc->trigger, 0, sizeof(struct sigma_trigger));
eec5275e 690
ba7dd8bb
UH
691 for (l = sdi->channels; l; l = l->next) {
692 ch = (struct sr_channel *)l->data;
693 channelbit = 1 << (ch->index);
57bbf56b 694
ba7dd8bb 695 if (!ch->enabled || !ch->trigger)
57bbf56b
HE
696 continue;
697
0e1357e8 698 if (devc->cur_samplerate >= SR_MHZ(100)) {
c53d793f 699 /* Fast trigger support. */
ee492173 700 if (trigger_set) {
47f4f073
UH
701 sr_err("Only a single pin trigger in 100 and "
702 "200MHz mode is supported.");
e46b8fb1 703 return SR_ERR;
ee492173 704 }
ba7dd8bb
UH
705 if (ch->trigger[0] == 'f')
706 devc->trigger.fallingmask |= channelbit;
707 else if (ch->trigger[0] == 'r')
708 devc->trigger.risingmask |= channelbit;
ee492173 709 else {
47f4f073
UH
710 sr_err("Only rising/falling trigger in 100 "
711 "and 200MHz mode is supported.");
e46b8fb1 712 return SR_ERR;
ee492173 713 }
57bbf56b 714
c53d793f 715 ++trigger_set;
ee492173 716 } else {
c53d793f 717 /* Simple trigger support (event). */
ba7dd8bb
UH
718 if (ch->trigger[0] == '1') {
719 devc->trigger.simplevalue |= channelbit;
720 devc->trigger.simplemask |= channelbit;
c53d793f 721 }
ba7dd8bb
UH
722 else if (ch->trigger[0] == '0') {
723 devc->trigger.simplevalue &= ~channelbit;
724 devc->trigger.simplemask |= channelbit;
c53d793f 725 }
ba7dd8bb
UH
726 else if (ch->trigger[0] == 'f') {
727 devc->trigger.fallingmask |= channelbit;
c53d793f
HE
728 ++trigger_set;
729 }
ba7dd8bb
UH
730 else if (ch->trigger[0] == 'r') {
731 devc->trigger.risingmask |= channelbit;
c53d793f
HE
732 ++trigger_set;
733 }
ee492173 734
ea9cfed7
UH
735 /*
736 * Actually, Sigma supports 2 rising/falling triggers,
737 * but they are ORed and the current trigger syntax
738 * does not permit ORed triggers.
739 */
98b8cbc1 740 if (trigger_set > 1) {
47f4f073
UH
741 sr_err("Only 1 rising/falling trigger "
742 "is supported.");
e46b8fb1 743 return SR_ERR;
ee492173 744 }
ee492173 745 }
5b5ea7c6
HE
746
747 if (trigger_set)
0e1357e8 748 devc->use_triggers = 1;
57bbf56b
HE
749 }
750
e46b8fb1 751 return SR_OK;
57bbf56b
HE
752}
753
6078d2c9 754static int dev_close(struct sr_dev_inst *sdi)
28a35d8a 755{
0e1357e8 756 struct dev_context *devc;
28a35d8a 757
961009b0 758 devc = sdi->priv;
697785d1
UH
759
760 /* TODO */
761 if (sdi->status == SR_ST_ACTIVE)
0e1357e8 762 ftdi_usb_close(&devc->ftdic);
697785d1
UH
763
764 sdi->status = SR_ST_INACTIVE;
765
766 return SR_OK;
28a35d8a
HE
767}
768
6078d2c9 769static int cleanup(void)
28a35d8a 770{
3b412e3a 771 return dev_clear();
28a35d8a
HE
772}
773
8f996b89 774static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 775 const struct sr_channel_group *cg)
28a35d8a 776{
0e1357e8 777 struct dev_context *devc;
99965709 778
53b4680f 779 (void)cg;
8f996b89 780
035a1078 781 switch (id) {
123e1313 782 case SR_CONF_SAMPLERATE:
41479605 783 if (sdi) {
0e1357e8 784 devc = sdi->priv;
2c9c0df8 785 *data = g_variant_new_uint64(devc->cur_samplerate);
41479605
BV
786 } else
787 return SR_ERR;
28a35d8a 788 break;
d7bbecfd 789 default:
bd6fbf62 790 return SR_ERR_NA;
28a35d8a
HE
791 }
792
41479605 793 return SR_OK;
28a35d8a
HE
794}
795
8f996b89 796static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 797 const struct sr_channel_group *cg)
28a35d8a 798{
0e1357e8 799 struct dev_context *devc;
6868626b 800 uint64_t num_samples;
28a35d8a 801 int ret;
f6564c8d 802
53b4680f 803 (void)cg;
8f996b89 804
e73ffd42
BV
805 if (sdi->status != SR_ST_ACTIVE)
806 return SR_ERR_DEV_CLOSED;
807
0e1357e8 808 devc = sdi->priv;
99965709 809
6868626b
BV
810 switch (id) {
811 case SR_CONF_SAMPLERATE:
2c9c0df8 812 ret = set_samplerate(sdi, g_variant_get_uint64(data));
6868626b
BV
813 break;
814 case SR_CONF_LIMIT_MSEC:
2c9c0df8 815 devc->limit_msec = g_variant_get_uint64(data);
0e1357e8 816 if (devc->limit_msec > 0)
e46b8fb1 817 ret = SR_OK;
94ba4bd6 818 else
e46b8fb1 819 ret = SR_ERR;
6868626b
BV
820 break;
821 case SR_CONF_LIMIT_SAMPLES:
822 num_samples = g_variant_get_uint64(data);
823 devc->limit_msec = num_samples * 1000 / devc->cur_samplerate;
824 break;
825 case SR_CONF_CAPTURE_RATIO:
2c9c0df8 826 devc->capture_ratio = g_variant_get_uint64(data);
0e1357e8 827 if (devc->capture_ratio < 0 || devc->capture_ratio > 100)
e46b8fb1 828 ret = SR_ERR;
94ba4bd6 829 else
e46b8fb1 830 ret = SR_OK;
6868626b
BV
831 break;
832 default:
bd6fbf62 833 ret = SR_ERR_NA;
28a35d8a
HE
834 }
835
836 return ret;
837}
838
8f996b89 839static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 840 const struct sr_channel_group *cg)
a1c743fc 841{
2c9c0df8
BV
842 GVariant *gvar;
843 GVariantBuilder gvb;
a1c743fc
BV
844
845 (void)sdi;
53b4680f 846 (void)cg;
a1c743fc
BV
847
848 switch (key) {
9a6517d1 849 case SR_CONF_DEVICE_OPTIONS:
2c9c0df8
BV
850 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
851 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
9a6517d1 852 break;
a1c743fc 853 case SR_CONF_SAMPLERATE:
2c9c0df8
BV
854 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
855 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
856 ARRAY_SIZE(samplerates), sizeof(uint64_t));
857 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
858 *data = g_variant_builder_end(&gvb);
a1c743fc 859 break;
c50277a6 860 case SR_CONF_TRIGGER_TYPE:
2c9c0df8 861 *data = g_variant_new_string(TRIGGER_TYPE);
c50277a6 862 break;
a1c743fc 863 default:
bd6fbf62 864 return SR_ERR_NA;
a1c743fc
BV
865 }
866
867 return SR_OK;
868}
869
36b1c8e6
HE
870/* Software trigger to determine exact trigger position. */
871static int get_trigger_offset(uint16_t *samples, uint16_t last_sample,
872 struct sigma_trigger *t)
873{
874 int i;
875
876 for (i = 0; i < 8; ++i) {
877 if (i > 0)
878 last_sample = samples[i-1];
879
880 /* Simple triggers. */
881 if ((samples[i] & t->simplemask) != t->simplevalue)
882 continue;
883
884 /* Rising edge. */
885 if ((last_sample & t->risingmask) != 0 || (samples[i] &
886 t->risingmask) != t->risingmask)
887 continue;
888
889 /* Falling edge. */
bdfc7a89
HE
890 if ((last_sample & t->fallingmask) != t->fallingmask ||
891 (samples[i] & t->fallingmask) != 0)
36b1c8e6
HE
892 continue;
893
894 break;
895 }
896
897 /* If we did not match, return original trigger pos. */
898 return i & 0x7;
899}
900
28a35d8a 901/*
fefa1800
UH
902 * Decode chunk of 1024 bytes, 64 clusters, 7 events per cluster.
903 * Each event is 20ns apart, and can contain multiple samples.
f78898e9
HE
904 *
905 * For 200 MHz, events contain 4 samples for each channel, spread 5 ns apart.
906 * For 100 MHz, events contain 2 samples for each channel, spread 10 ns apart.
907 * For 50 MHz and below, events contain one sample for each channel,
908 * spread 20 ns apart.
28a35d8a
HE
909 */
910static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
88c51afe 911 uint16_t *lastsample, int triggerpos,
3cd3a20b 912 uint16_t limit_chunk, void *cb_data)
28a35d8a 913{
3cd3a20b 914 struct sr_dev_inst *sdi = cb_data;
0e1357e8 915 struct dev_context *devc = sdi->priv;
fefa1800 916 uint16_t tsdiff, ts;
0e1357e8 917 uint16_t samples[65536 * devc->samples_per_event];
b9c735a2 918 struct sr_datafeed_packet packet;
9c939c51 919 struct sr_datafeed_logic logic;
f78898e9 920 int i, j, k, l, numpad, tosend;
fefa1800 921 size_t n = 0, sent = 0;
0e1357e8 922 int clustersize = EVENTS_PER_CLUSTER * devc->samples_per_event;
fefa1800 923 uint16_t *event;
f78898e9 924 uint16_t cur_sample;
57bbf56b 925 int triggerts = -1;
ee492173 926
4ae1f451 927 /* Check if trigger is in this chunk. */
ee492173 928 if (triggerpos != -1) {
0e1357e8 929 if (devc->cur_samplerate <= SR_MHZ(50))
36b1c8e6 930 triggerpos -= EVENTS_PER_CLUSTER - 1;
ee492173
HE
931
932 if (triggerpos < 0)
933 triggerpos = 0;
57bbf56b 934
ee492173
HE
935 /* Find in which cluster the trigger occured. */
936 triggerts = triggerpos / 7;
937 }
28a35d8a 938
eec5275e 939 /* For each ts. */
28a35d8a 940 for (i = 0; i < 64; ++i) {
fefa1800 941 ts = *(uint16_t *) &buf[i * 16];
28a35d8a
HE
942 tsdiff = ts - *lastts;
943 *lastts = ts;
944
88c51afe
HE
945 /* Decode partial chunk. */
946 if (limit_chunk && ts > limit_chunk)
e46b8fb1 947 return SR_OK;
88c51afe 948
fefa1800 949 /* Pad last sample up to current point. */
0e1357e8 950 numpad = tsdiff * devc->samples_per_event - clustersize;
28a35d8a 951 if (numpad > 0) {
f78898e9
HE
952 for (j = 0; j < numpad; ++j)
953 samples[j] = *lastsample;
954
955 n = numpad;
28a35d8a
HE
956 }
957
57bbf56b
HE
958 /* Send samples between previous and this timestamp to sigrok. */
959 sent = 0;
960 while (sent < n) {
961 tosend = MIN(2048, n - sent);
962
5a2326a7 963 packet.type = SR_DF_LOGIC;
9c939c51
BV
964 packet.payload = &logic;
965 logic.length = tosend * sizeof(uint16_t);
966 logic.unitsize = 2;
967 logic.data = samples + sent;
3e9b7f9c 968 sr_session_send(devc->cb_data, &packet);
28a35d8a 969
57bbf56b
HE
970 sent += tosend;
971 }
972 n = 0;
973
974 event = (uint16_t *) &buf[i * 16 + 2];
f78898e9
HE
975 cur_sample = 0;
976
977 /* For each event in cluster. */
28a35d8a 978 for (j = 0; j < 7; ++j) {
f78898e9
HE
979
980 /* For each sample in event. */
0e1357e8 981 for (k = 0; k < devc->samples_per_event; ++k) {
f78898e9
HE
982 cur_sample = 0;
983
ba7dd8bb
UH
984 /* For each channel. */
985 for (l = 0; l < devc->num_channels; ++l)
edca2c5c 986 cur_sample |= (!!(event[j] & (1 << (l *
0e1357e8 987 devc->samples_per_event + k)))) << l;
f78898e9
HE
988
989 samples[n++] = cur_sample;
28a35d8a
HE
990 }
991 }
992
eec5275e 993 /* Send data up to trigger point (if triggered). */
fefa1800 994 sent = 0;
57bbf56b
HE
995 if (i == triggerts) {
996 /*
36b1c8e6
HE
997 * Trigger is not always accurate to sample because of
998 * pipeline delay. However, it always triggers before
999 * the actual event. We therefore look at the next
1000 * samples to pinpoint the exact position of the trigger.
57bbf56b 1001 */
bdfc7a89 1002 tosend = get_trigger_offset(samples, *lastsample,
0e1357e8 1003 &devc->trigger);
57bbf56b
HE
1004
1005 if (tosend > 0) {
5a2326a7 1006 packet.type = SR_DF_LOGIC;
9c939c51
BV
1007 packet.payload = &logic;
1008 logic.length = tosend * sizeof(uint16_t);
1009 logic.unitsize = 2;
1010 logic.data = samples;
3e9b7f9c 1011 sr_session_send(devc->cb_data, &packet);
57bbf56b
HE
1012
1013 sent += tosend;
1014 }
28a35d8a 1015
5b5ea7c6 1016 /* Only send trigger if explicitly enabled. */
0e1357e8 1017 if (devc->use_triggers) {
5a2326a7 1018 packet.type = SR_DF_TRIGGER;
3e9b7f9c 1019 sr_session_send(devc->cb_data, &packet);
5b5ea7c6 1020 }
28a35d8a 1021 }
57bbf56b 1022
eec5275e 1023 /* Send rest of the chunk to sigrok. */
57bbf56b
HE
1024 tosend = n - sent;
1025
abda62ce 1026 if (tosend > 0) {
5a2326a7 1027 packet.type = SR_DF_LOGIC;
9c939c51
BV
1028 packet.payload = &logic;
1029 logic.length = tosend * sizeof(uint16_t);
1030 logic.unitsize = 2;
1031 logic.data = samples + sent;
3e9b7f9c 1032 sr_session_send(devc->cb_data, &packet);
abda62ce 1033 }
ee492173
HE
1034
1035 *lastsample = samples[n - 1];
28a35d8a
HE
1036 }
1037
e46b8fb1 1038 return SR_OK;
28a35d8a
HE
1039}
1040
6868626b 1041static void download_capture(struct sr_dev_inst *sdi)
28a35d8a 1042{
6868626b 1043 struct dev_context *devc;
28a35d8a
HE
1044 const int chunks_per_read = 32;
1045 unsigned char buf[chunks_per_read * CHUNK_SIZE];
6868626b
BV
1046 int bufsz, i, numchunks, newchunks;
1047
1048 sr_info("Downloading sample data.");
1049
1050 devc = sdi->priv;
1051 devc->state.chunks_downloaded = 0;
1052 numchunks = (devc->state.stoppos + 511) / 512;
1053 newchunks = MIN(chunks_per_read, numchunks - devc->state.chunks_downloaded);
1054
1055 bufsz = sigma_read_dram(devc->state.chunks_downloaded, newchunks, buf, devc);
1056 /* TODO: Check bufsz. For now, just avoid compiler warnings. */
1057 (void)bufsz;
1058
1059 /* Find first ts. */
1060 if (devc->state.chunks_downloaded == 0) {
1061 devc->state.lastts = RL16(buf) - 1;
1062 devc->state.lastsample = 0;
1063 }
1064
1065 /* Decode chunks and send them to sigrok. */
1066 for (i = 0; i < newchunks; ++i) {
1067 int limit_chunk = 0;
1068
1069 /* The last chunk may potentially be only in part. */
1070 if (devc->state.chunks_downloaded == numchunks - 1) {
1071 /* Find the last valid timestamp */
1072 limit_chunk = devc->state.stoppos % 512 + devc->state.lastts;
1073 }
1074
1075 if (devc->state.chunks_downloaded + i == devc->state.triggerchunk)
1076 decode_chunk_ts(buf + (i * CHUNK_SIZE),
1077 &devc->state.lastts,
1078 &devc->state.lastsample,
1079 devc->state.triggerpos & 0x1ff,
1080 limit_chunk, sdi);
1081 else
1082 decode_chunk_ts(buf + (i * CHUNK_SIZE),
1083 &devc->state.lastts,
1084 &devc->state.lastsample,
1085 -1, limit_chunk, sdi);
1086
1087 ++devc->state.chunks_downloaded;
1088 }
1089
1090}
1091
1092static int receive_data(int fd, int revents, void *cb_data)
1093{
1094 struct sr_dev_inst *sdi;
1095 struct dev_context *devc;
1096 struct sr_datafeed_packet packet;
94ba4bd6 1097 uint64_t running_msec;
28a35d8a 1098 struct timeval tv;
6868626b
BV
1099 int numchunks;
1100 uint8_t modestatus;
28a35d8a 1101
cb93f8a9
UH
1102 (void)fd;
1103 (void)revents;
28a35d8a 1104
6868626b
BV
1105 sdi = cb_data;
1106 devc = sdi->priv;
1107
805919b0 1108 /* Get the current position. */
0e1357e8 1109 sigma_read_pos(&devc->state.stoppos, &devc->state.triggerpos, devc);
805919b0 1110
0e1357e8 1111 if (devc->state.state == SIGMA_IDLE)
805919b0 1112 return TRUE;
28a35d8a 1113
0e1357e8 1114 if (devc->state.state == SIGMA_CAPTURE) {
6868626b
BV
1115 numchunks = (devc->state.stoppos + 511) / 512;
1116
6aac7737
HE
1117 /* Check if the timer has expired, or memory is full. */
1118 gettimeofday(&tv, 0);
0e1357e8
BV
1119 running_msec = (tv.tv_sec - devc->start_tv.tv_sec) * 1000 +
1120 (tv.tv_usec - devc->start_tv.tv_usec) / 1000;
28a35d8a 1121
0e1357e8 1122 if (running_msec < devc->limit_msec && numchunks < 32767)
6868626b 1123 /* Still capturing. */
6aac7737 1124 return TRUE;
6aac7737 1125
6868626b
BV
1126 /* Stop acquisition. */
1127 sigma_set_register(WRITE_MODE, 0x11, devc);
28a35d8a 1128
6868626b
BV
1129 /* Set SDRAM Read Enable. */
1130 sigma_set_register(WRITE_MODE, 0x02, devc);
28a35d8a 1131
6868626b
BV
1132 /* Get the current position. */
1133 sigma_read_pos(&devc->state.stoppos, &devc->state.triggerpos, devc);
28a35d8a 1134
6868626b
BV
1135 /* Check if trigger has fired. */
1136 modestatus = sigma_get_register(READ_MODE, devc);
1137 if (modestatus & 0x20)
1138 devc->state.triggerchunk = devc->state.triggerpos / 512;
1139 else
1140 devc->state.triggerchunk = -1;
28a35d8a 1141
6868626b
BV
1142 /* Transfer captured data from device. */
1143 download_capture(sdi);
88c51afe 1144
6868626b
BV
1145 /* All done. */
1146 packet.type = SR_DF_END;
1147 sr_session_send(sdi, &packet);
88c51afe 1148
6868626b 1149 dev_acquisition_stop(sdi, sdi);
28a35d8a
HE
1150 }
1151
28a35d8a
HE
1152 return TRUE;
1153}
1154
c53d793f
HE
1155/* Build a LUT entry used by the trigger functions. */
1156static void build_lut_entry(uint16_t value, uint16_t mask, uint16_t *entry)
ee492173
HE
1157{
1158 int i, j, k, bit;
1159
ba7dd8bb 1160 /* For each quad channel. */
ee492173 1161 for (i = 0; i < 4; ++i) {
c53d793f 1162 entry[i] = 0xffff;
ee492173 1163
f758d074 1164 /* For each bit in LUT. */
ee492173
HE
1165 for (j = 0; j < 16; ++j)
1166
ba7dd8bb 1167 /* For each channel in quad. */
ee492173
HE
1168 for (k = 0; k < 4; ++k) {
1169 bit = 1 << (i * 4 + k);
1170
c53d793f
HE
1171 /* Set bit in entry */
1172 if ((mask & bit) &&
1173 ((!(value & bit)) !=
4ae1f451 1174 (!(j & (1 << k)))))
c53d793f 1175 entry[i] &= ~(1 << j);
ee492173
HE
1176 }
1177 }
c53d793f 1178}
ee492173 1179
c53d793f
HE
1180/* Add a logical function to LUT mask. */
1181static void add_trigger_function(enum triggerop oper, enum triggerfunc func,
1182 int index, int neg, uint16_t *mask)
1183{
1184 int i, j;
1185 int x[2][2], tmp, a, b, aset, bset, rset;
1186
1187 memset(x, 0, 4 * sizeof(int));
1188
1189 /* Trigger detect condition. */
1190 switch (oper) {
1191 case OP_LEVEL:
1192 x[0][1] = 1;
1193 x[1][1] = 1;
1194 break;
1195 case OP_NOT:
1196 x[0][0] = 1;
1197 x[1][0] = 1;
1198 break;
1199 case OP_RISE:
1200 x[0][1] = 1;
1201 break;
1202 case OP_FALL:
1203 x[1][0] = 1;
1204 break;
1205 case OP_RISEFALL:
1206 x[0][1] = 1;
1207 x[1][0] = 1;
1208 break;
1209 case OP_NOTRISE:
1210 x[1][1] = 1;
1211 x[0][0] = 1;
1212 x[1][0] = 1;
1213 break;
1214 case OP_NOTFALL:
1215 x[1][1] = 1;
1216 x[0][0] = 1;
1217 x[0][1] = 1;
1218 break;
1219 case OP_NOTRISEFALL:
1220 x[1][1] = 1;
1221 x[0][0] = 1;
1222 break;
1223 }
1224
1225 /* Transpose if neg is set. */
1226 if (neg) {
ea9cfed7 1227 for (i = 0; i < 2; ++i) {
c53d793f
HE
1228 for (j = 0; j < 2; ++j) {
1229 tmp = x[i][j];
1230 x[i][j] = x[1-i][1-j];
1231 x[1-i][1-j] = tmp;
1232 }
ea9cfed7 1233 }
c53d793f
HE
1234 }
1235
1236 /* Update mask with function. */
1237 for (i = 0; i < 16; ++i) {
1238 a = (i >> (2 * index + 0)) & 1;
1239 b = (i >> (2 * index + 1)) & 1;
1240
1241 aset = (*mask >> i) & 1;
1242 bset = x[b][a];
1243
1244 if (func == FUNC_AND || func == FUNC_NAND)
1245 rset = aset & bset;
1246 else if (func == FUNC_OR || func == FUNC_NOR)
1247 rset = aset | bset;
1248 else if (func == FUNC_XOR || func == FUNC_NXOR)
1249 rset = aset ^ bset;
1250
1251 if (func == FUNC_NAND || func == FUNC_NOR || func == FUNC_NXOR)
1252 rset = !rset;
1253
1254 *mask &= ~(1 << i);
1255
1256 if (rset)
1257 *mask |= 1 << i;
1258 }
1259}
1260
1261/*
1262 * Build trigger LUTs used by 50 MHz and lower sample rates for supporting
1263 * simple pin change and state triggers. Only two transitions (rise/fall) can be
1264 * set at any time, but a full mask and value can be set (0/1).
1265 */
0e1357e8 1266static int build_basic_trigger(struct triggerlut *lut, struct dev_context *devc)
c53d793f
HE
1267{
1268 int i,j;
4ae1f451 1269 uint16_t masks[2] = { 0, 0 };
c53d793f
HE
1270
1271 memset(lut, 0, sizeof(struct triggerlut));
1272
1273 /* Contant for simple triggers. */
1274 lut->m4 = 0xa000;
1275
1276 /* Value/mask trigger support. */
0e1357e8 1277 build_lut_entry(devc->trigger.simplevalue, devc->trigger.simplemask,
99965709 1278 lut->m2d);
c53d793f
HE
1279
1280 /* Rise/fall trigger support. */
1281 for (i = 0, j = 0; i < 16; ++i) {
0e1357e8
BV
1282 if (devc->trigger.risingmask & (1 << i) ||
1283 devc->trigger.fallingmask & (1 << i))
c53d793f
HE
1284 masks[j++] = 1 << i;
1285 }
1286
1287 build_lut_entry(masks[0], masks[0], lut->m0d);
1288 build_lut_entry(masks[1], masks[1], lut->m1d);
1289
1290 /* Add glue logic */
1291 if (masks[0] || masks[1]) {
1292 /* Transition trigger. */
0e1357e8 1293 if (masks[0] & devc->trigger.risingmask)
c53d793f 1294 add_trigger_function(OP_RISE, FUNC_OR, 0, 0, &lut->m3);
0e1357e8 1295 if (masks[0] & devc->trigger.fallingmask)
c53d793f 1296 add_trigger_function(OP_FALL, FUNC_OR, 0, 0, &lut->m3);
0e1357e8 1297 if (masks[1] & devc->trigger.risingmask)
c53d793f 1298 add_trigger_function(OP_RISE, FUNC_OR, 1, 0, &lut->m3);
0e1357e8 1299 if (masks[1] & devc->trigger.fallingmask)
c53d793f
HE
1300 add_trigger_function(OP_FALL, FUNC_OR, 1, 0, &lut->m3);
1301 } else {
1302 /* Only value/mask trigger. */
1303 lut->m3 = 0xffff;
1304 }
ee492173 1305
c53d793f 1306 /* Triggertype: event. */
ee492173
HE
1307 lut->params.selres = 3;
1308
e46b8fb1 1309 return SR_OK;
ee492173
HE
1310}
1311
6078d2c9 1312static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
28a35d8a 1313{
0e1357e8 1314 struct dev_context *devc;
9ddb2a12 1315 struct clockselect_50 clockselect;
82957b65 1316 int frac, triggerpin, ret;
f4abaa9f 1317 uint8_t triggerselect = 0;
57bbf56b 1318 struct triggerinout triggerinout_conf;
ee492173 1319 struct triggerlut lut;
28a35d8a 1320
e73ffd42
BV
1321 if (sdi->status != SR_ST_ACTIVE)
1322 return SR_ERR_DEV_CLOSED;
1323
0e1357e8 1324 devc = sdi->priv;
28a35d8a 1325
ba7dd8bb
UH
1326 if (configure_channels(sdi) != SR_OK) {
1327 sr_err("Failed to configure channels.");
014359e3
BV
1328 return SR_ERR;
1329 }
1330
ea9cfed7 1331 /* If the samplerate has not been set, default to 200 kHz. */
0e1357e8 1332 if (devc->cur_firmware == -1) {
82957b65
UH
1333 if ((ret = set_samplerate(sdi, SR_KHZ(200))) != SR_OK)
1334 return ret;
1335 }
e8397563 1336
eec5275e 1337 /* Enter trigger programming mode. */
0e1357e8 1338 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x20, devc);
28a35d8a 1339
eec5275e 1340 /* 100 and 200 MHz mode. */
0e1357e8
BV
1341 if (devc->cur_samplerate >= SR_MHZ(100)) {
1342 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x81, devc);
57bbf56b 1343
a42aec7f
HE
1344 /* Find which pin to trigger on from mask. */
1345 for (triggerpin = 0; triggerpin < 8; ++triggerpin)
0e1357e8 1346 if ((devc->trigger.risingmask | devc->trigger.fallingmask) &
a42aec7f
HE
1347 (1 << triggerpin))
1348 break;
1349
1350 /* Set trigger pin and light LED on trigger. */
1351 triggerselect = (1 << LEDSEL1) | (triggerpin & 0x7);
1352
1353 /* Default rising edge. */
0e1357e8 1354 if (devc->trigger.fallingmask)
a42aec7f 1355 triggerselect |= 1 << 3;
57bbf56b 1356
eec5275e 1357 /* All other modes. */
0e1357e8
BV
1358 } else if (devc->cur_samplerate <= SR_MHZ(50)) {
1359 build_basic_trigger(&lut, devc);
ee492173 1360
0e1357e8 1361 sigma_write_trigger_lut(&lut, devc);
57bbf56b
HE
1362
1363 triggerselect = (1 << LEDSEL1) | (1 << LEDSEL0);
1364 }
1365
eec5275e 1366 /* Setup trigger in and out pins to default values. */
57bbf56b
HE
1367 memset(&triggerinout_conf, 0, sizeof(struct triggerinout));
1368 triggerinout_conf.trgout_bytrigger = 1;
1369 triggerinout_conf.trgout_enable = 1;
1370
28a35d8a 1371 sigma_write_register(WRITE_TRIGGER_OPTION,
57bbf56b 1372 (uint8_t *) &triggerinout_conf,
0e1357e8 1373 sizeof(struct triggerinout), devc);
28a35d8a 1374
eec5275e 1375 /* Go back to normal mode. */
0e1357e8 1376 sigma_set_register(WRITE_TRIGGER_SELECT1, triggerselect, devc);
28a35d8a 1377
edca2c5c 1378 /* Set clock select register. */
0e1357e8 1379 if (devc->cur_samplerate == SR_MHZ(200))
ba7dd8bb 1380 /* Enable 4 channels. */
0e1357e8
BV
1381 sigma_set_register(WRITE_CLOCK_SELECT, 0xf0, devc);
1382 else if (devc->cur_samplerate == SR_MHZ(100))
ba7dd8bb 1383 /* Enable 8 channels. */
0e1357e8 1384 sigma_set_register(WRITE_CLOCK_SELECT, 0x00, devc);
edca2c5c
HE
1385 else {
1386 /*
9ddb2a12 1387 * 50 MHz mode (or fraction thereof). Any fraction down to
eec5275e 1388 * 50 MHz / 256 can be used, but is not supported by sigrok API.
edca2c5c 1389 */
0e1357e8 1390 frac = SR_MHZ(50) / devc->cur_samplerate - 1;
edca2c5c 1391
9ddb2a12
UH
1392 clockselect.async = 0;
1393 clockselect.fraction = frac;
ba7dd8bb 1394 clockselect.disabled_channels = 0;
edca2c5c
HE
1395
1396 sigma_write_register(WRITE_CLOCK_SELECT,
9ddb2a12 1397 (uint8_t *) &clockselect,
0e1357e8 1398 sizeof(clockselect), devc);
edca2c5c
HE
1399 }
1400
fefa1800 1401 /* Setup maximum post trigger time. */
99965709 1402 sigma_set_register(WRITE_POST_TRIGGER,
0e1357e8 1403 (devc->capture_ratio * 255) / 100, devc);
28a35d8a 1404
eec5275e 1405 /* Start acqusition. */
0e1357e8
BV
1406 gettimeofday(&devc->start_tv, 0);
1407 sigma_set_register(WRITE_MODE, 0x0d, devc);
99965709 1408
3e9b7f9c 1409 devc->cb_data = cb_data;
28a35d8a 1410
3c36c403 1411 /* Send header packet to the session bus. */
29a27196 1412 std_session_send_df_header(cb_data, LOG_PREFIX);
f366e86c 1413
f366e86c 1414 /* Add capture source. */
3ffb6964 1415 sr_source_add(0, G_IO_IN, 10, receive_data, (void *)sdi);
f366e86c 1416
0e1357e8 1417 devc->state.state = SIGMA_CAPTURE;
6aac7737 1418
e46b8fb1 1419 return SR_OK;
28a35d8a
HE
1420}
1421
6078d2c9 1422static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
28a35d8a 1423{
0e1357e8 1424 struct dev_context *devc;
6aac7737 1425
3cd3a20b 1426 (void)cb_data;
28a35d8a 1427
6868626b
BV
1428 devc = sdi->priv;
1429 devc->state.state = SIGMA_IDLE;
6aac7737 1430
6868626b 1431 sr_source_remove(0);
3010f21c
UH
1432
1433 return SR_OK;
28a35d8a
HE
1434}
1435
c09f0b57 1436SR_PRIV struct sr_dev_driver asix_sigma_driver_info = {
e519ba86 1437 .name = "asix-sigma",
6352d030 1438 .longname = "ASIX SIGMA/SIGMA2",
e519ba86 1439 .api_version = 1,
6078d2c9
UH
1440 .init = init,
1441 .cleanup = cleanup,
1442 .scan = scan,
1443 .dev_list = dev_list,
3b412e3a 1444 .dev_clear = dev_clear,
035a1078
BV
1445 .config_get = config_get,
1446 .config_set = config_set,
a1c743fc 1447 .config_list = config_list,
6078d2c9
UH
1448 .dev_open = dev_open,
1449 .dev_close = dev_close,
1450 .dev_acquisition_start = dev_acquisition_start,
1451 .dev_acquisition_stop = dev_acquisition_stop,
0e1357e8 1452 .priv = NULL,
28a35d8a 1453};