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