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