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