]> sigrok.org Git - libsigrok.git/blame - src/hardware/asix-sigma/protocol.c
dev_clear(): Consistently name callback 'clear_helper()'.
[libsigrok.git] / src / hardware / asix-sigma / protocol.c
CommitLineData
28a35d8a 1/*
50985c20 2 * This file is part of the libsigrok project.
28a35d8a 3 *
868501fa 4 * Copyright (C) 2010-2012 Håvard Espeland <gus@ping.uio.no>,
911f1834
UH
5 * Copyright (C) 2010 Martin Stensgård <mastensg@ping.uio.no>
6 * Copyright (C) 2010 Carl Henrik Lunde <chlunde@ping.uio.no>
28a35d8a
HE
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
911f1834 22/*
6352d030 23 * ASIX SIGMA/SIGMA2 logic analyzer driver
911f1834
UH
24 */
25
6ec6c43b 26#include <config.h>
3ba56876 27#include "protocol.h"
28a35d8a 28
b1648dea
MV
29/*
30 * The ASIX Sigma supports arbitrary integer frequency divider in
31 * the 50MHz mode. The divider is in range 1...256 , allowing for
32 * very precise sampling rate selection. This driver supports only
33 * a subset of the sampling rates.
34 */
3ba56876 35SR_PRIV const uint64_t samplerates[] = {
b1648dea
MV
36 SR_KHZ(200), /* div=250 */
37 SR_KHZ(250), /* div=200 */
38 SR_KHZ(500), /* div=100 */
39 SR_MHZ(1), /* div=50 */
40 SR_MHZ(5), /* div=10 */
41 SR_MHZ(10), /* div=5 */
42 SR_MHZ(25), /* div=2 */
43 SR_MHZ(50), /* div=1 */
44 SR_MHZ(100), /* Special FW needed */
45 SR_MHZ(200), /* Special FW needed */
28a35d8a
HE
46};
47
4154a516 48SR_PRIV const size_t samplerates_count = ARRAY_SIZE(samplerates);
39c64c6a 49
8e2d6c9d 50static const char sigma_firmware_files[][24] = {
499b17e9 51 /* 50 MHz, supports 8 bit fractions */
8e2d6c9d 52 "asix-sigma-50.fw",
499b17e9 53 /* 100 MHz */
8e2d6c9d 54 "asix-sigma-100.fw",
499b17e9 55 /* 200 MHz */
8e2d6c9d 56 "asix-sigma-200.fw",
499b17e9 57 /* Synchronous clock from pin */
8e2d6c9d 58 "asix-sigma-50sync.fw",
499b17e9 59 /* Frequency counter */
8e2d6c9d 60 "asix-sigma-phasor.fw",
f6564c8d
HE
61};
62
0e1357e8 63static int sigma_read(void *buf, size_t size, struct dev_context *devc)
28a35d8a
HE
64{
65 int ret;
fefa1800 66
0e1357e8 67 ret = ftdi_read_data(&devc->ftdic, (unsigned char *)buf, size);
28a35d8a 68 if (ret < 0) {
47f4f073 69 sr_err("ftdi_read_data failed: %s",
0e1357e8 70 ftdi_get_error_string(&devc->ftdic));
28a35d8a
HE
71 }
72
73 return ret;
74}
75
0e1357e8 76static int sigma_write(void *buf, size_t size, struct dev_context *devc)
28a35d8a
HE
77{
78 int ret;
fefa1800 79
0e1357e8 80 ret = ftdi_write_data(&devc->ftdic, (unsigned char *)buf, size);
28a35d8a 81 if (ret < 0) {
47f4f073 82 sr_err("ftdi_write_data failed: %s",
0e1357e8 83 ftdi_get_error_string(&devc->ftdic));
fefa1800 84 } else if ((size_t) ret != size) {
47f4f073 85 sr_err("ftdi_write_data did not complete write.");
28a35d8a
HE
86 }
87
88 return ret;
89}
90
e8686e3a
AG
91/*
92 * NOTE: We chose the buffer size to be large enough to hold any write to the
93 * device. We still print a message just in case.
94 */
3ba56876 95SR_PRIV int sigma_write_register(uint8_t reg, uint8_t *data, size_t len,
96 struct dev_context *devc)
28a35d8a
HE
97{
98 size_t i;
e8686e3a 99 uint8_t buf[80];
28a35d8a
HE
100 int idx = 0;
101
7c86d853 102 if ((2 * len + 2) > sizeof(buf)) {
e8686e3a 103 sr_err("Attempted to write %zu bytes, but buffer is too small.",
7c86d853 104 len);
e8686e3a
AG
105 return SR_ERR_BUG;
106 }
107
28a35d8a
HE
108 buf[idx++] = REG_ADDR_LOW | (reg & 0xf);
109 buf[idx++] = REG_ADDR_HIGH | (reg >> 4);
110
0a1f7b09 111 for (i = 0; i < len; i++) {
28a35d8a
HE
112 buf[idx++] = REG_DATA_LOW | (data[i] & 0xf);
113 buf[idx++] = REG_DATA_HIGH_WRITE | (data[i] >> 4);
114 }
115
0e1357e8 116 return sigma_write(buf, idx, devc);
28a35d8a
HE
117}
118
3ba56876 119SR_PRIV int sigma_set_register(uint8_t reg, uint8_t value, struct dev_context *devc)
28a35d8a 120{
0e1357e8 121 return sigma_write_register(reg, &value, 1, devc);
28a35d8a
HE
122}
123
99965709 124static int sigma_read_register(uint8_t reg, uint8_t *data, size_t len,
0e1357e8 125 struct dev_context *devc)
28a35d8a
HE
126{
127 uint8_t buf[3];
fefa1800 128
28a35d8a
HE
129 buf[0] = REG_ADDR_LOW | (reg & 0xf);
130 buf[1] = REG_ADDR_HIGH | (reg >> 4);
28a35d8a
HE
131 buf[2] = REG_READ_ADDR;
132
0e1357e8 133 sigma_write(buf, sizeof(buf), devc);
28a35d8a 134
0e1357e8 135 return sigma_read(data, len, devc);
28a35d8a
HE
136}
137
0e1357e8 138static uint8_t sigma_get_register(uint8_t reg, struct dev_context *devc)
28a35d8a
HE
139{
140 uint8_t value;
fefa1800 141
0e1357e8 142 if (1 != sigma_read_register(reg, &value, 1, devc)) {
47f4f073 143 sr_err("sigma_get_register: 1 byte expected");
28a35d8a
HE
144 return 0;
145 }
146
147 return value;
148}
149
99965709 150static int sigma_read_pos(uint32_t *stoppos, uint32_t *triggerpos,
0e1357e8 151 struct dev_context *devc)
28a35d8a
HE
152{
153 uint8_t buf[] = {
154 REG_ADDR_LOW | READ_TRIGGER_POS_LOW,
155
156 REG_READ_ADDR | NEXT_REG,
157 REG_READ_ADDR | NEXT_REG,
158 REG_READ_ADDR | NEXT_REG,
159 REG_READ_ADDR | NEXT_REG,
160 REG_READ_ADDR | NEXT_REG,
161 REG_READ_ADDR | NEXT_REG,
162 };
28a35d8a
HE
163 uint8_t result[6];
164
0e1357e8 165 sigma_write(buf, sizeof(buf), devc);
28a35d8a 166
0e1357e8 167 sigma_read(result, sizeof(result), devc);
28a35d8a
HE
168
169 *triggerpos = result[0] | (result[1] << 8) | (result[2] << 16);
170 *stoppos = result[3] | (result[4] << 8) | (result[5] << 16);
171
57bbf56b
HE
172 /* Not really sure why this must be done, but according to spec. */
173 if ((--*stoppos & 0x1ff) == 0x1ff)
382cb19f 174 *stoppos -= 64;
57bbf56b
HE
175
176 if ((*--triggerpos & 0x1ff) == 0x1ff)
382cb19f 177 *triggerpos -= 64;
57bbf56b 178
28a35d8a
HE
179 return 1;
180}
181
99965709 182static int sigma_read_dram(uint16_t startchunk, size_t numchunks,
0e1357e8 183 uint8_t *data, struct dev_context *devc)
28a35d8a
HE
184{
185 size_t i;
186 uint8_t buf[4096];
f06fb3e9 187 int idx;
28a35d8a 188
fefa1800 189 /* Send the startchunk. Index start with 1. */
f06fb3e9
GS
190 idx = 0;
191 buf[idx++] = startchunk >> 8;
192 buf[idx++] = startchunk & 0xff;
193 sigma_write_register(WRITE_MEMROW, buf, idx, devc);
28a35d8a 194
fefa1800 195 /* Read the DRAM. */
f06fb3e9 196 idx = 0;
28a35d8a
HE
197 buf[idx++] = REG_DRAM_BLOCK;
198 buf[idx++] = REG_DRAM_WAIT_ACK;
199
0a1f7b09 200 for (i = 0; i < numchunks; i++) {
fefa1800
UH
201 /* Alternate bit to copy from DRAM to cache. */
202 if (i != (numchunks - 1))
203 buf[idx++] = REG_DRAM_BLOCK | (((i + 1) % 2) << 4);
28a35d8a
HE
204
205 buf[idx++] = REG_DRAM_BLOCK_DATA | ((i % 2) << 4);
206
fefa1800 207 if (i != (numchunks - 1))
28a35d8a
HE
208 buf[idx++] = REG_DRAM_WAIT_ACK;
209 }
210
0e1357e8 211 sigma_write(buf, idx, devc);
28a35d8a 212
0e1357e8 213 return sigma_read(data, numchunks * CHUNK_SIZE, devc);
28a35d8a
HE
214}
215
4ae1f451 216/* Upload trigger look-up tables to Sigma. */
3ba56876 217SR_PRIV int sigma_write_trigger_lut(struct triggerlut *lut, struct dev_context *devc)
ee492173
HE
218{
219 int i;
220 uint8_t tmp[2];
221 uint16_t bit;
222
223 /* Transpose the table and send to Sigma. */
0a1f7b09 224 for (i = 0; i < 16; i++) {
ee492173
HE
225 bit = 1 << i;
226
227 tmp[0] = tmp[1] = 0;
228
229 if (lut->m2d[0] & bit)
230 tmp[0] |= 0x01;
231 if (lut->m2d[1] & bit)
232 tmp[0] |= 0x02;
233 if (lut->m2d[2] & bit)
234 tmp[0] |= 0x04;
235 if (lut->m2d[3] & bit)
236 tmp[0] |= 0x08;
237
238 if (lut->m3 & bit)
239 tmp[0] |= 0x10;
240 if (lut->m3s & bit)
241 tmp[0] |= 0x20;
242 if (lut->m4 & bit)
243 tmp[0] |= 0x40;
244
245 if (lut->m0d[0] & bit)
246 tmp[1] |= 0x01;
247 if (lut->m0d[1] & bit)
248 tmp[1] |= 0x02;
249 if (lut->m0d[2] & bit)
250 tmp[1] |= 0x04;
251 if (lut->m0d[3] & bit)
252 tmp[1] |= 0x08;
253
254 if (lut->m1d[0] & bit)
255 tmp[1] |= 0x10;
256 if (lut->m1d[1] & bit)
257 tmp[1] |= 0x20;
258 if (lut->m1d[2] & bit)
259 tmp[1] |= 0x40;
260 if (lut->m1d[3] & bit)
261 tmp[1] |= 0x80;
262
99965709 263 sigma_write_register(WRITE_TRIGGER_SELECT0, tmp, sizeof(tmp),
0e1357e8
BV
264 devc);
265 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x30 | i, devc);
ee492173
HE
266 }
267
268 /* Send the parameters */
269 sigma_write_register(WRITE_TRIGGER_SELECT0, (uint8_t *) &lut->params,
0e1357e8 270 sizeof(lut->params), devc);
ee492173 271
e46b8fb1 272 return SR_OK;
ee492173
HE
273}
274
d5fa188a
MV
275/*
276 * Configure the FPGA for bitbang mode.
277 * This sequence is documented in section 2. of the ASIX Sigma programming
278 * manual. This sequence is necessary to configure the FPGA in the Sigma
279 * into Bitbang mode, in which it can be programmed with the firmware.
280 */
281static int sigma_fpga_init_bitbang(struct dev_context *devc)
282{
283 uint8_t suicide[] = {
284 0x84, 0x84, 0x88, 0x84, 0x88, 0x84, 0x88, 0x84,
285 };
286 uint8_t init_array[] = {
287 0x01, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
288 0x01, 0x01,
289 };
1a46cc62 290 int i, ret, timeout = (10 * 1000);
d5fa188a
MV
291 uint8_t data;
292
293 /* Section 2. part 1), do the FPGA suicide. */
294 sigma_write(suicide, sizeof(suicide), devc);
295 sigma_write(suicide, sizeof(suicide), devc);
296 sigma_write(suicide, sizeof(suicide), devc);
297 sigma_write(suicide, sizeof(suicide), devc);
298
299 /* Section 2. part 2), do pulse on D1. */
300 sigma_write(init_array, sizeof(init_array), devc);
301 ftdi_usb_purge_buffers(&devc->ftdic);
302
303 /* Wait until the FPGA asserts D6/INIT_B. */
304 for (i = 0; i < timeout; i++) {
305 ret = sigma_read(&data, 1, devc);
306 if (ret < 0)
307 return ret;
308 /* Test if pin D6 got asserted. */
309 if (data & (1 << 5))
310 return 0;
311 /* The D6 was not asserted yet, wait a bit. */
1a46cc62 312 g_usleep(10 * 1000);
d5fa188a
MV
313 }
314
315 return SR_ERR_TIMEOUT;
316}
317
64fe661b
MV
318/*
319 * Configure the FPGA for logic-analyzer mode.
320 */
321static int sigma_fpga_init_la(struct dev_context *devc)
322{
323 /* Initialize the logic analyzer mode. */
22f64ed8 324 uint8_t mode_regval = WMR_SDRAMINIT;
64fe661b 325 uint8_t logic_mode_start[] = {
011f1091 326 REG_ADDR_LOW | (READ_ID & 0xf),
84a6ed1a 327 REG_ADDR_HIGH | (READ_ID >> 4),
011f1091
MV
328 REG_READ_ADDR, /* Read ID register. */
329
330 REG_ADDR_LOW | (WRITE_TEST & 0xf),
331 REG_DATA_LOW | 0x5,
332 REG_DATA_HIGH_WRITE | 0x5,
333 REG_READ_ADDR, /* Read scratch register. */
334
335 REG_DATA_LOW | 0xa,
336 REG_DATA_HIGH_WRITE | 0xa,
337 REG_READ_ADDR, /* Read scratch register. */
338
339 REG_ADDR_LOW | (WRITE_MODE & 0xf),
22f64ed8
GS
340 REG_DATA_LOW | (mode_regval & 0xf),
341 REG_DATA_HIGH_WRITE | (mode_regval >> 4),
64fe661b
MV
342 };
343
344 uint8_t result[3];
345 int ret;
346
347 /* Initialize the logic analyzer mode. */
348 sigma_write(logic_mode_start, sizeof(logic_mode_start), devc);
349
011f1091 350 /* Expect a 3 byte reply since we issued three READ requests. */
64fe661b
MV
351 ret = sigma_read(result, 3, devc);
352 if (ret != 3)
353 goto err;
354
355 if (result[0] != 0xa6 || result[1] != 0x55 || result[2] != 0xaa)
356 goto err;
357
358 return SR_OK;
359err:
360 sr_err("Configuration failed. Invalid reply received.");
361 return SR_ERR;
362}
363
a80226bb
MV
364/*
365 * Read the firmware from a file and transform it into a series of bitbang
366 * pulses used to program the FPGA. Note that the *bb_cmd must be free()'d
367 * by the caller of this function.
368 */
8e2d6c9d 369static int sigma_fw_2_bitbang(struct sr_context *ctx, const char *name,
a80226bb
MV
370 uint8_t **bb_cmd, gsize *bb_cmd_size)
371{
8e2d6c9d
DE
372 size_t i, file_size, bb_size;
373 char *firmware;
a80226bb
MV
374 uint8_t *bb_stream, *bbs;
375 uint32_t imm;
376 int bit, v;
377 int ret = SR_OK;
378
387825dc 379 /* Retrieve the on-disk firmware file content. */
8e2d6c9d
DE
380 firmware = sr_resource_load(ctx, SR_RESOURCE_FIRMWARE,
381 name, &file_size, 256 * 1024);
382 if (!firmware)
383 return SR_ERR;
a80226bb 384
387825dc 385 /* Unscramble the file content (XOR with "random" sequence). */
a80226bb
MV
386 imm = 0x3f6df2ab;
387 for (i = 0; i < file_size; i++) {
388 imm = (imm + 0xa853753) % 177 + (imm * 0x8034052);
389 firmware[i] ^= imm & 0xff;
390 }
391
392 /*
387825dc
GS
393 * Generate a sequence of bitbang samples. With two samples per
394 * FPGA configuration bit, providing the level for the DIN signal
395 * as well as two edges for CCLK. See Xilinx UG332 for details
396 * ("slave serial" mode).
397 *
398 * Note that CCLK is inverted in hardware. That's why the
399 * respective bit is first set and then cleared in the bitbang
400 * sample sets. So that the DIN level will be stable when the
401 * data gets sampled at the rising CCLK edge, and the signals'
402 * setup time constraint will be met.
403 *
404 * The caller will put the FPGA into download mode, will send
405 * the bitbang samples, and release the allocated memory.
a80226bb 406 */
a80226bb
MV
407 bb_size = file_size * 8 * 2;
408 bb_stream = (uint8_t *)g_try_malloc(bb_size);
409 if (!bb_stream) {
410 sr_err("%s: Failed to allocate bitbang stream", __func__);
411 ret = SR_ERR_MALLOC;
412 goto exit;
413 }
a80226bb
MV
414 bbs = bb_stream;
415 for (i = 0; i < file_size; i++) {
416 for (bit = 7; bit >= 0; bit--) {
417 v = (firmware[i] & (1 << bit)) ? 0x40 : 0x00;
418 *bbs++ = v | 0x01;
419 *bbs++ = v;
420 }
421 }
422
423 /* The transformation completed successfully, return the result. */
424 *bb_cmd = bb_stream;
425 *bb_cmd_size = bb_size;
426
427exit:
8e2d6c9d 428 g_free(firmware);
a80226bb
MV
429 return ret;
430}
431
8e2d6c9d
DE
432static int upload_firmware(struct sr_context *ctx,
433 int firmware_idx, struct dev_context *devc)
28a35d8a
HE
434{
435 int ret;
436 unsigned char *buf;
437 unsigned char pins;
438 size_t buf_size;
a9016883 439 const char *firmware;
a9016883
GS
440
441 /* Avoid downloading the same firmware multiple times. */
442 firmware = sigma_firmware_files[firmware_idx];
443 if (devc->cur_firmware == firmware_idx) {
444 sr_info("Not uploading firmware file '%s' again.", firmware);
445 return SR_OK;
446 }
28a35d8a 447
1f4f98e0 448 ret = ftdi_set_bitmode(&devc->ftdic, 0xdf, BITMODE_BITBANG);
8bbf7627 449 if (ret < 0) {
47f4f073 450 sr_err("ftdi_set_bitmode failed: %s",
1f4f98e0 451 ftdi_get_error_string(&devc->ftdic));
7bcf2168 452 return SR_ERR;
28a35d8a
HE
453 }
454
fefa1800 455 /* Four times the speed of sigmalogan - Works well. */
1f4f98e0 456 ret = ftdi_set_baudrate(&devc->ftdic, 750 * 1000);
8bbf7627 457 if (ret < 0) {
47f4f073 458 sr_err("ftdi_set_baudrate failed: %s",
1f4f98e0 459 ftdi_get_error_string(&devc->ftdic));
7bcf2168 460 return SR_ERR;
28a35d8a
HE
461 }
462
d5fa188a
MV
463 /* Initialize the FPGA for firmware upload. */
464 ret = sigma_fpga_init_bitbang(devc);
465 if (ret)
466 return ret;
28a35d8a 467
9ddb2a12 468 /* Prepare firmware. */
8e2d6c9d 469 ret = sigma_fw_2_bitbang(ctx, firmware, &buf, &buf_size);
8bbf7627 470 if (ret != SR_OK) {
f3f19d11 471 sr_err("An error occurred while reading the firmware: %s",
499b17e9 472 firmware);
b53738ba 473 return ret;
28a35d8a
HE
474 }
475
f3f19d11 476 /* Upload firmware. */
499b17e9 477 sr_info("Uploading firmware file '%s'.", firmware);
0e1357e8 478 sigma_write(buf, buf_size, devc);
28a35d8a
HE
479
480 g_free(buf);
481
1f4f98e0 482 ret = ftdi_set_bitmode(&devc->ftdic, 0x00, BITMODE_RESET);
8bbf7627 483 if (ret < 0) {
47f4f073 484 sr_err("ftdi_set_bitmode failed: %s",
1f4f98e0 485 ftdi_get_error_string(&devc->ftdic));
e46b8fb1 486 return SR_ERR;
28a35d8a
HE
487 }
488
1f4f98e0 489 ftdi_usb_purge_buffers(&devc->ftdic);
28a35d8a 490
fefa1800 491 /* Discard garbage. */
29b66a2e 492 while (sigma_read(&pins, 1, devc) == 1)
28a35d8a
HE
493 ;
494
64fe661b
MV
495 /* Initialize the FPGA for logic-analyzer mode. */
496 ret = sigma_fpga_init_la(devc);
497 if (ret != SR_OK)
498 return ret;
28a35d8a 499
0e1357e8 500 devc->cur_firmware = firmware_idx;
f6564c8d 501
47f4f073 502 sr_info("Firmware uploaded.");
e3fff420 503
e46b8fb1 504 return SR_OK;
f6564c8d
HE
505}
506
9a0a606a
GS
507/*
508 * Sigma doesn't support limiting the number of samples, so we have to
509 * translate the number and the samplerate to an elapsed time.
510 *
511 * In addition we need to ensure that the last data cluster has passed
512 * the hardware pipeline, and became available to the PC side. With RLE
513 * compression up to 327ms could pass before another cluster accumulates
514 * at 200kHz samplerate when input pins don't change.
515 */
516SR_PRIV uint64_t sigma_limit_samples_to_msec(const struct dev_context *devc,
517 uint64_t limit_samples)
518{
519 uint64_t limit_msec;
520 uint64_t worst_cluster_time_ms;
521
522 limit_msec = limit_samples * 1000 / devc->cur_samplerate;
523 worst_cluster_time_ms = 65536 * 1000 / devc->cur_samplerate;
524 /*
525 * One cluster time is not enough to flush pipeline when sampling
526 * grounded pins with 1 sample limit at 200kHz. Hence the 2* fix.
527 */
528 return limit_msec + 2 * worst_cluster_time_ms;
529}
530
3ba56876 531SR_PRIV int sigma_set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
f6564c8d 532{
2c9c0df8 533 struct dev_context *devc;
8e2d6c9d 534 struct drv_context *drvc;
4154a516 535 size_t i;
2c9c0df8 536 int ret;
ac9534f4 537 int num_channels;
f6564c8d 538
2c9c0df8 539 devc = sdi->priv;
8e2d6c9d 540 drvc = sdi->driver->context;
f4abaa9f
UH
541 ret = SR_OK;
542
2f7e529c 543 /* Reject rates that are not in the list of supported rates. */
4154a516 544 for (i = 0; i < samplerates_count; i++) {
2c9c0df8 545 if (samplerates[i] == samplerate)
f6564c8d
HE
546 break;
547 }
4154a516 548 if (i >= samplerates_count || samplerates[i] == 0)
e46b8fb1 549 return SR_ERR_SAMPLERATE;
f6564c8d 550
2f7e529c
GS
551 /*
552 * Depending on the samplerates of 200/100/50- MHz, specific
553 * firmware is required and higher rates might limit the set
554 * of available channels.
555 */
ac9534f4 556 num_channels = devc->num_channels;
59df0c77 557 if (samplerate <= SR_MHZ(50)) {
8e2d6c9d 558 ret = upload_firmware(drvc->sr_ctx, 0, devc);
ac9534f4 559 num_channels = 16;
6b2d3385 560 } else if (samplerate == SR_MHZ(100)) {
8e2d6c9d 561 ret = upload_firmware(drvc->sr_ctx, 1, devc);
ac9534f4 562 num_channels = 8;
6b2d3385 563 } else if (samplerate == SR_MHZ(200)) {
8e2d6c9d 564 ret = upload_firmware(drvc->sr_ctx, 2, devc);
ac9534f4 565 num_channels = 4;
f78898e9 566 }
f6564c8d 567
2f7e529c
GS
568 /*
569 * Derive the sample period from the sample rate as well as the
570 * number of samples that the device will communicate within
571 * an "event" (memory organization internal to the device).
572 */
6b2d3385 573 if (ret == SR_OK) {
ac9534f4 574 devc->num_channels = num_channels;
6b2d3385 575 devc->cur_samplerate = samplerate;
6b2d3385
BV
576 devc->samples_per_event = 16 / devc->num_channels;
577 devc->state.state = SIGMA_IDLE;
578 }
f6564c8d 579
2f7e529c
GS
580 /*
581 * Support for "limit_samples" is implemented by stopping
582 * acquisition after a corresponding period of time.
583 * Re-calculate that period of time, in case the limit is
584 * set first and the samplerate gets (re-)configured later.
585 */
586 if (ret == SR_OK && devc->limit_samples) {
587 uint64_t msecs;
9a0a606a 588 msecs = sigma_limit_samples_to_msec(devc, devc->limit_samples);
2f7e529c
GS
589 devc->limit_msec = msecs;
590 }
591
e8397563 592 return ret;
28a35d8a
HE
593}
594
c53d793f
HE
595/*
596 * In 100 and 200 MHz mode, only a single pin rising/falling can be
597 * set as trigger. In other modes, two rising/falling triggers can be set,
ba7dd8bb 598 * in addition to value/mask trigger for any number of channels.
c53d793f
HE
599 *
600 * The Sigma supports complex triggers using boolean expressions, but this
601 * has not been implemented yet.
602 */
3ba56876 603SR_PRIV int sigma_convert_trigger(const struct sr_dev_inst *sdi)
57bbf56b 604{
39c64c6a
BV
605 struct dev_context *devc;
606 struct sr_trigger *trigger;
607 struct sr_trigger_stage *stage;
608 struct sr_trigger_match *match;
609 const GSList *l, *m;
610 int channelbit, trigger_set;
57bbf56b 611
39c64c6a 612 devc = sdi->priv;
0e1357e8 613 memset(&devc->trigger, 0, sizeof(struct sigma_trigger));
0812c40e 614 if (!(trigger = sr_session_trigger_get(sdi->session)))
39c64c6a
BV
615 return SR_OK;
616
617 trigger_set = 0;
618 for (l = trigger->stages; l; l = l->next) {
619 stage = l->data;
620 for (m = stage->matches; m; m = m->next) {
621 match = m->data;
622 if (!match->channel->enabled)
623 /* Ignore disabled channels with a trigger. */
624 continue;
625 channelbit = 1 << (match->channel->index);
626 if (devc->cur_samplerate >= SR_MHZ(100)) {
627 /* Fast trigger support. */
628 if (trigger_set) {
629 sr_err("Only a single pin trigger is "
630 "supported in 100 and 200MHz mode.");
631 return SR_ERR;
632 }
633 if (match->match == SR_TRIGGER_FALLING)
634 devc->trigger.fallingmask |= channelbit;
635 else if (match->match == SR_TRIGGER_RISING)
636 devc->trigger.risingmask |= channelbit;
637 else {
638 sr_err("Only rising/falling trigger is "
639 "supported in 100 and 200MHz mode.");
640 return SR_ERR;
641 }
eec5275e 642
0a1f7b09 643 trigger_set++;
39c64c6a
BV
644 } else {
645 /* Simple trigger support (event). */
646 if (match->match == SR_TRIGGER_ONE) {
647 devc->trigger.simplevalue |= channelbit;
648 devc->trigger.simplemask |= channelbit;
649 }
650 else if (match->match == SR_TRIGGER_ZERO) {
651 devc->trigger.simplevalue &= ~channelbit;
652 devc->trigger.simplemask |= channelbit;
653 }
654 else if (match->match == SR_TRIGGER_FALLING) {
655 devc->trigger.fallingmask |= channelbit;
0a1f7b09 656 trigger_set++;
39c64c6a
BV
657 }
658 else if (match->match == SR_TRIGGER_RISING) {
659 devc->trigger.risingmask |= channelbit;
0a1f7b09 660 trigger_set++;
39c64c6a
BV
661 }
662
663 /*
664 * Actually, Sigma supports 2 rising/falling triggers,
665 * but they are ORed and the current trigger syntax
666 * does not permit ORed triggers.
667 */
668 if (trigger_set > 1) {
669 sr_err("Only 1 rising/falling trigger "
670 "is supported.");
671 return SR_ERR;
672 }
ee492173 673 }
ee492173 674 }
57bbf56b
HE
675 }
676
e46b8fb1 677 return SR_OK;
57bbf56b
HE
678}
679
a1c743fc 680
36b1c8e6 681/* Software trigger to determine exact trigger position. */
5fc01191 682static int get_trigger_offset(uint8_t *samples, uint16_t last_sample,
36b1c8e6
HE
683 struct sigma_trigger *t)
684{
685 int i;
5fc01191 686 uint16_t sample = 0;
36b1c8e6 687
0a1f7b09 688 for (i = 0; i < 8; i++) {
36b1c8e6 689 if (i > 0)
5fc01191
MV
690 last_sample = sample;
691 sample = samples[2 * i] | (samples[2 * i + 1] << 8);
36b1c8e6
HE
692
693 /* Simple triggers. */
5fc01191 694 if ((sample & t->simplemask) != t->simplevalue)
36b1c8e6
HE
695 continue;
696
697 /* Rising edge. */
5fc01191
MV
698 if (((last_sample & t->risingmask) != 0) ||
699 ((sample & t->risingmask) != t->risingmask))
36b1c8e6
HE
700 continue;
701
702 /* Falling edge. */
bdfc7a89 703 if ((last_sample & t->fallingmask) != t->fallingmask ||
5fc01191 704 (sample & t->fallingmask) != 0)
36b1c8e6
HE
705 continue;
706
707 break;
708 }
709
710 /* If we did not match, return original trigger pos. */
711 return i & 0x7;
712}
713
3513d965
MV
714/*
715 * Return the timestamp of "DRAM cluster".
716 */
717static uint16_t sigma_dram_cluster_ts(struct sigma_dram_cluster *cluster)
718{
719 return (cluster->timestamp_hi << 8) | cluster->timestamp_lo;
720}
721
0498f743
GS
722/*
723 * Return one 16bit data entity of a DRAM cluster at the specified index.
724 */
725static uint16_t sigma_dram_cluster_data(struct sigma_dram_cluster *cl, int idx)
726{
727 uint16_t sample;
728
729 sample = 0;
730 sample |= cl->samples[idx].sample_lo << 0;
731 sample |= cl->samples[idx].sample_hi << 8;
3281cf59 732 sample = (sample >> 8) | (sample << 8);
0498f743
GS
733 return sample;
734}
735
85c032e4
GS
736/*
737 * Deinterlace sample data that was retrieved at 100MHz samplerate.
738 * One 16bit item contains two samples of 8bits each. The bits of
739 * multiple samples are interleaved.
740 */
741static uint16_t sigma_deinterlace_100mhz_data(uint16_t indata, int idx)
742{
743 uint16_t outdata;
744
745 indata >>= idx;
746 outdata = 0;
747 outdata |= (indata >> (0 * 2 - 0)) & (1 << 0);
748 outdata |= (indata >> (1 * 2 - 1)) & (1 << 1);
749 outdata |= (indata >> (2 * 2 - 2)) & (1 << 2);
750 outdata |= (indata >> (3 * 2 - 3)) & (1 << 3);
751 outdata |= (indata >> (4 * 2 - 4)) & (1 << 4);
752 outdata |= (indata >> (5 * 2 - 5)) & (1 << 5);
753 outdata |= (indata >> (6 * 2 - 6)) & (1 << 6);
754 outdata |= (indata >> (7 * 2 - 7)) & (1 << 7);
755 return outdata;
756}
757
758/*
759 * Deinterlace sample data that was retrieved at 200MHz samplerate.
760 * One 16bit item contains four samples of 4bits each. The bits of
761 * multiple samples are interleaved.
762 */
763static uint16_t sigma_deinterlace_200mhz_data(uint16_t indata, int idx)
764{
765 uint16_t outdata;
766
767 indata >>= idx;
768 outdata = 0;
769 outdata |= (indata >> (0 * 4 - 0)) & (1 << 0);
770 outdata |= (indata >> (1 * 4 - 1)) & (1 << 1);
771 outdata |= (indata >> (2 * 4 - 2)) & (1 << 2);
772 outdata |= (indata >> (3 * 4 - 3)) & (1 << 3);
773 return outdata;
774}
775
0498f743
GS
776static void store_sr_sample(uint8_t *samples, int idx, uint16_t data)
777{
778 samples[2 * idx + 0] = (data >> 0) & 0xff;
779 samples[2 * idx + 1] = (data >> 8) & 0xff;
780}
781
735ed8a1
GS
782/*
783 * Local wrapper around sr_session_send() calls. Make sure to not send
784 * more samples to the session's datafeed than what was requested by a
785 * previously configured (optional) sample count.
786 */
787static void sigma_session_send(struct sr_dev_inst *sdi,
788 struct sr_datafeed_packet *packet)
789{
790 struct dev_context *devc;
791 struct sr_datafeed_logic *logic;
792 uint64_t send_now;
793
794 devc = sdi->priv;
795 if (devc->limit_samples) {
796 logic = (void *)packet->payload;
797 send_now = logic->length / logic->unitsize;
798 if (devc->sent_samples + send_now > devc->limit_samples) {
799 send_now = devc->limit_samples - devc->sent_samples;
800 logic->length = send_now * logic->unitsize;
801 }
802 if (!send_now)
803 return;
804 devc->sent_samples += send_now;
805 }
806
807 sr_session_send(sdi, packet);
808}
809
85c032e4
GS
810/*
811 * This size translates to: event count (1K events per cluster), times
812 * the sample width (unitsize, 16bits per event), times the maximum
813 * number of samples per event.
814 */
815#define SAMPLES_BUFFER_SIZE (1024 * 2 * 4)
816
23239b5c
MV
817static void sigma_decode_dram_cluster(struct sigma_dram_cluster *dram_cluster,
818 unsigned int events_in_cluster,
1e23158b 819 unsigned int triggered,
23239b5c
MV
820 struct sr_dev_inst *sdi)
821{
822 struct dev_context *devc = sdi->priv;
823 struct sigma_state *ss = &devc->state;
824 struct sr_datafeed_packet packet;
825 struct sr_datafeed_logic logic;
85c032e4
GS
826 uint16_t tsdiff, ts, sample, item16;
827 uint8_t samples[SAMPLES_BUFFER_SIZE];
828 uint8_t *send_ptr;
829 size_t send_count, trig_count;
23239b5c 830 unsigned int i;
85c032e4 831 int j;
23239b5c 832
23239b5c
MV
833 ts = sigma_dram_cluster_ts(dram_cluster);
834 tsdiff = ts - ss->lastts;
a44b3b3f 835 ss->lastts = ts + EVENTS_PER_CLUSTER;
23239b5c
MV
836
837 packet.type = SR_DF_LOGIC;
838 packet.payload = &logic;
839 logic.unitsize = 2;
840 logic.data = samples;
841
842 /*
468f17f2
GS
843 * If this cluster is not adjacent to the previously received
844 * cluster, then send the appropriate number of samples with the
845 * previous values to the sigrok session. This "decodes RLE".
23239b5c 846 */
a44b3b3f 847 for (ts = 0; ts < tsdiff; ts++) {
23239b5c 848 i = ts % 1024;
0498f743 849 store_sr_sample(samples, i, ss->lastsample);
23239b5c
MV
850
851 /*
852 * If we have 1024 samples ready or we're at the
853 * end of submitting the padding samples, submit
85c032e4
GS
854 * the packet to Sigrok. Since constant data is
855 * sent, duplication of data for rates above 50MHz
856 * is simple.
23239b5c 857 */
a44b3b3f 858 if ((i == 1023) || (ts == tsdiff - 1)) {
23239b5c 859 logic.length = (i + 1) * logic.unitsize;
85c032e4 860 for (j = 0; j < devc->samples_per_event; j++)
735ed8a1 861 sigma_session_send(sdi, &packet);
23239b5c
MV
862 }
863 }
864
865 /*
866 * Parse the samples in current cluster and prepare them
85c032e4
GS
867 * to be submitted to Sigrok. Cope with memory layouts that
868 * vary with the samplerate.
23239b5c 869 */
85c032e4
GS
870 send_ptr = &samples[0];
871 send_count = 0;
0498f743 872 sample = 0;
23239b5c 873 for (i = 0; i < events_in_cluster; i++) {
85c032e4
GS
874 item16 = sigma_dram_cluster_data(dram_cluster, i);
875 if (devc->cur_samplerate == SR_MHZ(200)) {
876 sample = sigma_deinterlace_200mhz_data(item16, 0);
877 store_sr_sample(samples, send_count++, sample);
878 sample = sigma_deinterlace_200mhz_data(item16, 1);
879 store_sr_sample(samples, send_count++, sample);
880 sample = sigma_deinterlace_200mhz_data(item16, 2);
881 store_sr_sample(samples, send_count++, sample);
882 sample = sigma_deinterlace_200mhz_data(item16, 3);
883 store_sr_sample(samples, send_count++, sample);
884 } else if (devc->cur_samplerate == SR_MHZ(100)) {
885 sample = sigma_deinterlace_100mhz_data(item16, 0);
886 store_sr_sample(samples, send_count++, sample);
887 sample = sigma_deinterlace_100mhz_data(item16, 1);
888 store_sr_sample(samples, send_count++, sample);
889 } else {
890 sample = item16;
891 store_sr_sample(samples, send_count++, sample);
892 }
23239b5c
MV
893 }
894
de3f7acb
GS
895 /*
896 * If a trigger position applies, then provide the datafeed with
897 * the first part of data up to that position, then send the
898 * trigger marker.
899 */
23239b5c 900 int trigger_offset = 0;
1e23158b 901 if (triggered) {
23239b5c
MV
902 /*
903 * Trigger is not always accurate to sample because of
904 * pipeline delay. However, it always triggers before
905 * the actual event. We therefore look at the next
906 * samples to pinpoint the exact position of the trigger.
907 */
908 trigger_offset = get_trigger_offset(samples,
909 ss->lastsample, &devc->trigger);
910
911 if (trigger_offset > 0) {
85c032e4 912 trig_count = trigger_offset * devc->samples_per_event;
23239b5c 913 packet.type = SR_DF_LOGIC;
85c032e4 914 logic.length = trig_count * logic.unitsize;
735ed8a1 915 sigma_session_send(sdi, &packet);
85c032e4
GS
916 send_ptr += trig_count * logic.unitsize;
917 send_count -= trig_count;
23239b5c
MV
918 }
919
920 /* Only send trigger if explicitly enabled. */
921 if (devc->use_triggers) {
922 packet.type = SR_DF_TRIGGER;
102f1239 923 sr_session_send(sdi, &packet);
23239b5c
MV
924 }
925 }
926
de3f7acb
GS
927 /*
928 * Send the data after the trigger, or all of the received data
929 * if no trigger position applies.
930 */
85c032e4 931 if (send_count) {
23239b5c 932 packet.type = SR_DF_LOGIC;
85c032e4
GS
933 logic.length = send_count * logic.unitsize;
934 logic.data = send_ptr;
735ed8a1 935 sigma_session_send(sdi, &packet);
23239b5c
MV
936 }
937
0498f743 938 ss->lastsample = sample;
23239b5c
MV
939}
940
28a35d8a 941/*
fefa1800
UH
942 * Decode chunk of 1024 bytes, 64 clusters, 7 events per cluster.
943 * Each event is 20ns apart, and can contain multiple samples.
f78898e9
HE
944 *
945 * For 200 MHz, events contain 4 samples for each channel, spread 5 ns apart.
946 * For 100 MHz, events contain 2 samples for each channel, spread 10 ns apart.
947 * For 50 MHz and below, events contain one sample for each channel,
948 * spread 20 ns apart.
28a35d8a 949 */
1e23158b
MV
950static int decode_chunk_ts(struct sigma_dram_line *dram_line,
951 uint16_t events_in_line,
952 uint32_t trigger_event,
102f1239 953 struct sr_dev_inst *sdi)
28a35d8a 954{
3628074d 955 struct sigma_dram_cluster *dram_cluster;
f06fb3e9
GS
956 struct dev_context *devc;
957 unsigned int clusters_in_line;
5fc01191 958 unsigned int events_in_cluster;
23239b5c 959 unsigned int i;
f06fb3e9
GS
960 uint32_t trigger_cluster, triggered;
961
962 devc = sdi->priv;
963 clusters_in_line = events_in_line;
964 clusters_in_line += EVENTS_PER_CLUSTER - 1;
965 clusters_in_line /= EVENTS_PER_CLUSTER;
966 trigger_cluster = ~0;
967 triggered = 0;
ee492173 968
4ae1f451 969 /* Check if trigger is in this chunk. */
1e23158b
MV
970 if (trigger_event < (64 * 7)) {
971 if (devc->cur_samplerate <= SR_MHZ(50)) {
972 trigger_event -= MIN(EVENTS_PER_CLUSTER - 1,
973 trigger_event);
974 }
57bbf56b 975
f3f19d11 976 /* Find in which cluster the trigger occurred. */
1e23158b 977 trigger_cluster = trigger_event / EVENTS_PER_CLUSTER;
ee492173 978 }
28a35d8a 979
5fc01191
MV
980 /* For each full DRAM cluster. */
981 for (i = 0; i < clusters_in_line; i++) {
3628074d 982 dram_cluster = &dram_line->cluster[i];
5fc01191 983
5fc01191 984 /* The last cluster might not be full. */
23239b5c
MV
985 if ((i == clusters_in_line - 1) &&
986 (events_in_line % EVENTS_PER_CLUSTER)) {
5fc01191 987 events_in_cluster = events_in_line % EVENTS_PER_CLUSTER;
23239b5c 988 } else {
5fc01191 989 events_in_cluster = EVENTS_PER_CLUSTER;
abda62ce 990 }
ee492173 991
1e23158b
MV
992 triggered = (i == trigger_cluster);
993 sigma_decode_dram_cluster(dram_cluster, events_in_cluster,
994 triggered, sdi);
28a35d8a
HE
995 }
996
e46b8fb1 997 return SR_OK;
28a35d8a
HE
998}
999
6057d9fa 1000static int download_capture(struct sr_dev_inst *sdi)
28a35d8a 1001{
e15e5873 1002 const uint32_t chunks_per_read = 32;
f06fb3e9
GS
1003
1004 struct dev_context *devc;
fd830beb 1005 struct sigma_dram_line *dram_line;
c6648b66 1006 int bufsz;
462fe786 1007 uint32_t stoppos, triggerpos;
6057d9fa 1008 uint8_t modestatus;
c6648b66
MV
1009 uint32_t i;
1010 uint32_t dl_lines_total, dl_lines_curr, dl_lines_done;
74d453ab 1011 uint32_t dl_first_line, dl_line;
f06fb3e9
GS
1012 uint32_t dl_events_in_line;
1013 uint32_t trg_line, trg_event;
1014
1015 devc = sdi->priv;
1016 dl_events_in_line = 64 * 7;
1017 trg_line = ~0;
1018 trg_event = ~0;
c6648b66 1019
fd830beb
MV
1020 dram_line = g_try_malloc0(chunks_per_read * sizeof(*dram_line));
1021 if (!dram_line)
1022 return FALSE;
1023
6868626b
BV
1024 sr_info("Downloading sample data.");
1025
22f64ed8
GS
1026 /*
1027 * Ask the hardware to stop data acquisition. Reception of the
1028 * FORCESTOP request makes the hardware "disable RLE" (store
1029 * clusters to DRAM regardless of whether pin state changes) and
1030 * raise the POSTTRIGGERED flag.
1031 */
1032 sigma_set_register(WRITE_MODE, WMR_FORCESTOP | WMR_SDRAMWRITEEN, devc);
1033 do {
1034 modestatus = sigma_get_register(READ_MODE, devc);
1035 } while (!(modestatus & RMR_POSTTRIGGERED));
6057d9fa
MV
1036
1037 /* Set SDRAM Read Enable. */
22f64ed8 1038 sigma_set_register(WRITE_MODE, WMR_SDRAMREADEN, devc);
6057d9fa
MV
1039
1040 /* Get the current position. */
462fe786 1041 sigma_read_pos(&stoppos, &triggerpos, devc);
6057d9fa
MV
1042
1043 /* Check if trigger has fired. */
1044 modestatus = sigma_get_register(READ_MODE, devc);
22f64ed8 1045 if (modestatus & RMR_TRIGGERED) {
c6648b66 1046 trg_line = triggerpos >> 9;
1e23158b
MV
1047 trg_event = triggerpos & 0x1ff;
1048 }
6057d9fa 1049
735ed8a1
GS
1050 devc->sent_samples = 0;
1051
c6648b66 1052 /*
74d453ab
GS
1053 * Determine how many "DRAM lines" of 1024 bytes each we need to
1054 * retrieve from the Sigma hardware, so that we have a complete
1055 * set of samples. Note that the last line need not contain 64
1056 * clusters, it might be partially filled only.
1057 *
1058 * When RMR_ROUND is set, the circular buffer in DRAM has wrapped
1059 * around. Since the status of the very next line is uncertain in
1060 * that case, we skip it and start reading from the next line. The
1061 * circular buffer has 32K lines (0x8000).
c6648b66
MV
1062 */
1063 dl_lines_total = (stoppos >> 9) + 1;
74d453ab
GS
1064 if (modestatus & RMR_ROUND) {
1065 dl_first_line = dl_lines_total + 1;
1066 dl_lines_total = 0x8000 - 2;
1067 } else {
1068 dl_first_line = 0;
1069 }
c6648b66 1070 dl_lines_done = 0;
c6648b66
MV
1071 while (dl_lines_total > dl_lines_done) {
1072 /* We can download only up-to 32 DRAM lines in one go! */
547c4cdc 1073 dl_lines_curr = MIN(chunks_per_read, dl_lines_total - dl_lines_done);
6868626b 1074
74d453ab
GS
1075 dl_line = dl_first_line + dl_lines_done;
1076 dl_line %= 0x8000;
1077 bufsz = sigma_read_dram(dl_line, dl_lines_curr,
f41a4cae 1078 (uint8_t *)dram_line, devc);
c6648b66
MV
1079 /* TODO: Check bufsz. For now, just avoid compiler warnings. */
1080 (void)bufsz;
6868626b 1081
c6648b66
MV
1082 /* This is the first DRAM line, so find the initial timestamp. */
1083 if (dl_lines_done == 0) {
3513d965
MV
1084 devc->state.lastts =
1085 sigma_dram_cluster_ts(&dram_line[0].cluster[0]);
c6648b66 1086 devc->state.lastsample = 0;
6868626b
BV
1087 }
1088
c6648b66 1089 for (i = 0; i < dl_lines_curr; i++) {
1e23158b 1090 uint32_t trigger_event = ~0;
c6648b66
MV
1091 /* The last "DRAM line" can be only partially full. */
1092 if (dl_lines_done + i == dl_lines_total - 1)
46641fac 1093 dl_events_in_line = stoppos & 0x1ff;
c6648b66 1094
e69ad48e 1095 /* Test if the trigger happened on this line. */
c6648b66 1096 if (dl_lines_done + i == trg_line)
1e23158b 1097 trigger_event = trg_event;
e69ad48e 1098
1e23158b
MV
1099 decode_chunk_ts(dram_line + i, dl_events_in_line,
1100 trigger_event, sdi);
c6648b66 1101 }
6868626b 1102
c6648b66 1103 dl_lines_done += dl_lines_curr;
6868626b
BV
1104 }
1105
bee2b016 1106 std_session_send_df_end(sdi);
6057d9fa 1107
d2f7c417 1108 sr_dev_acquisition_stop(sdi);
6057d9fa 1109
fd830beb
MV
1110 g_free(dram_line);
1111
6057d9fa 1112 return TRUE;
6868626b
BV
1113}
1114
d4051930 1115/*
74d453ab
GS
1116 * Periodically check the Sigma status when in CAPTURE mode. This routine
1117 * checks whether the configured sample count or sample time have passed,
1118 * and will stop acquisition and download the acquired samples.
d4051930
MV
1119 */
1120static int sigma_capture_mode(struct sr_dev_inst *sdi)
6868626b 1121{
f06fb3e9 1122 struct dev_context *devc;
94ba4bd6 1123 uint64_t running_msec;
2f425a56 1124 uint64_t current_time;
28a35d8a 1125
f06fb3e9
GS
1126 devc = sdi->priv;
1127
74d453ab
GS
1128 /*
1129 * Check if the selected sampling duration passed. Sample count
1130 * limits are covered by this enforced timeout as well.
1131 */
2f425a56
GS
1132 current_time = g_get_monotonic_time();
1133 running_msec = (current_time - devc->start_time) / 1000;
00c86508 1134 if (running_msec >= devc->limit_msec)
6057d9fa 1135 return download_capture(sdi);
00c86508 1136
d4051930
MV
1137 return TRUE;
1138}
28a35d8a 1139
3ba56876 1140SR_PRIV int sigma_receive_data(int fd, int revents, void *cb_data)
d4051930
MV
1141{
1142 struct sr_dev_inst *sdi;
1143 struct dev_context *devc;
88c51afe 1144
d4051930
MV
1145 (void)fd;
1146 (void)revents;
88c51afe 1147
d4051930
MV
1148 sdi = cb_data;
1149 devc = sdi->priv;
1150
1151 if (devc->state.state == SIGMA_IDLE)
1152 return TRUE;
1153
1154 if (devc->state.state == SIGMA_CAPTURE)
1155 return sigma_capture_mode(sdi);
28a35d8a 1156
28a35d8a
HE
1157 return TRUE;
1158}
1159
c53d793f
HE
1160/* Build a LUT entry used by the trigger functions. */
1161static void build_lut_entry(uint16_t value, uint16_t mask, uint16_t *entry)
ee492173
HE
1162{
1163 int i, j, k, bit;
1164
ba7dd8bb 1165 /* For each quad channel. */
0a1f7b09 1166 for (i = 0; i < 4; i++) {
c53d793f 1167 entry[i] = 0xffff;
ee492173 1168
f758d074 1169 /* For each bit in LUT. */
0a1f7b09 1170 for (j = 0; j < 16; j++)
ee492173 1171
ba7dd8bb 1172 /* For each channel in quad. */
0a1f7b09 1173 for (k = 0; k < 4; k++) {
ee492173
HE
1174 bit = 1 << (i * 4 + k);
1175
c53d793f 1176 /* Set bit in entry */
0a1f7b09
UH
1177 if ((mask & bit) && ((!(value & bit)) !=
1178 (!(j & (1 << k)))))
c53d793f 1179 entry[i] &= ~(1 << j);
ee492173
HE
1180 }
1181 }
c53d793f 1182}
ee492173 1183
c53d793f
HE
1184/* Add a logical function to LUT mask. */
1185static void add_trigger_function(enum triggerop oper, enum triggerfunc func,
1186 int index, int neg, uint16_t *mask)
1187{
1188 int i, j;
1189 int x[2][2], tmp, a, b, aset, bset, rset;
1190
1191 memset(x, 0, 4 * sizeof(int));
1192
1193 /* Trigger detect condition. */
1194 switch (oper) {
1195 case OP_LEVEL:
1196 x[0][1] = 1;
1197 x[1][1] = 1;
1198 break;
1199 case OP_NOT:
1200 x[0][0] = 1;
1201 x[1][0] = 1;
1202 break;
1203 case OP_RISE:
1204 x[0][1] = 1;
1205 break;
1206 case OP_FALL:
1207 x[1][0] = 1;
1208 break;
1209 case OP_RISEFALL:
1210 x[0][1] = 1;
1211 x[1][0] = 1;
1212 break;
1213 case OP_NOTRISE:
1214 x[1][1] = 1;
1215 x[0][0] = 1;
1216 x[1][0] = 1;
1217 break;
1218 case OP_NOTFALL:
1219 x[1][1] = 1;
1220 x[0][0] = 1;
1221 x[0][1] = 1;
1222 break;
1223 case OP_NOTRISEFALL:
1224 x[1][1] = 1;
1225 x[0][0] = 1;
1226 break;
1227 }
1228
1229 /* Transpose if neg is set. */
1230 if (neg) {
0a1f7b09
UH
1231 for (i = 0; i < 2; i++) {
1232 for (j = 0; j < 2; j++) {
c53d793f 1233 tmp = x[i][j];
0a1f7b09
UH
1234 x[i][j] = x[1 - i][1 - j];
1235 x[1 - i][1 - j] = tmp;
c53d793f 1236 }
ea9cfed7 1237 }
c53d793f
HE
1238 }
1239
1240 /* Update mask with function. */
0a1f7b09 1241 for (i = 0; i < 16; i++) {
c53d793f
HE
1242 a = (i >> (2 * index + 0)) & 1;
1243 b = (i >> (2 * index + 1)) & 1;
1244
1245 aset = (*mask >> i) & 1;
1246 bset = x[b][a];
1247
382cb19f 1248 rset = 0;
c53d793f
HE
1249 if (func == FUNC_AND || func == FUNC_NAND)
1250 rset = aset & bset;
1251 else if (func == FUNC_OR || func == FUNC_NOR)
1252 rset = aset | bset;
1253 else if (func == FUNC_XOR || func == FUNC_NXOR)
1254 rset = aset ^ bset;
1255
1256 if (func == FUNC_NAND || func == FUNC_NOR || func == FUNC_NXOR)
1257 rset = !rset;
1258
1259 *mask &= ~(1 << i);
1260
1261 if (rset)
1262 *mask |= 1 << i;
1263 }
1264}
1265
1266/*
1267 * Build trigger LUTs used by 50 MHz and lower sample rates for supporting
1268 * simple pin change and state triggers. Only two transitions (rise/fall) can be
1269 * set at any time, but a full mask and value can be set (0/1).
1270 */
3ba56876 1271SR_PRIV int sigma_build_basic_trigger(struct triggerlut *lut, struct dev_context *devc)
c53d793f
HE
1272{
1273 int i,j;
4ae1f451 1274 uint16_t masks[2] = { 0, 0 };
c53d793f
HE
1275
1276 memset(lut, 0, sizeof(struct triggerlut));
1277
f3f19d11 1278 /* Constant for simple triggers. */
c53d793f
HE
1279 lut->m4 = 0xa000;
1280
1281 /* Value/mask trigger support. */
0e1357e8 1282 build_lut_entry(devc->trigger.simplevalue, devc->trigger.simplemask,
99965709 1283 lut->m2d);
c53d793f
HE
1284
1285 /* Rise/fall trigger support. */
0a1f7b09 1286 for (i = 0, j = 0; i < 16; i++) {
0e1357e8
BV
1287 if (devc->trigger.risingmask & (1 << i) ||
1288 devc->trigger.fallingmask & (1 << i))
c53d793f
HE
1289 masks[j++] = 1 << i;
1290 }
1291
1292 build_lut_entry(masks[0], masks[0], lut->m0d);
1293 build_lut_entry(masks[1], masks[1], lut->m1d);
1294
1295 /* Add glue logic */
1296 if (masks[0] || masks[1]) {
1297 /* Transition trigger. */
0e1357e8 1298 if (masks[0] & devc->trigger.risingmask)
c53d793f 1299 add_trigger_function(OP_RISE, FUNC_OR, 0, 0, &lut->m3);
0e1357e8 1300 if (masks[0] & devc->trigger.fallingmask)
c53d793f 1301 add_trigger_function(OP_FALL, FUNC_OR, 0, 0, &lut->m3);
0e1357e8 1302 if (masks[1] & devc->trigger.risingmask)
c53d793f 1303 add_trigger_function(OP_RISE, FUNC_OR, 1, 0, &lut->m3);
0e1357e8 1304 if (masks[1] & devc->trigger.fallingmask)
c53d793f
HE
1305 add_trigger_function(OP_FALL, FUNC_OR, 1, 0, &lut->m3);
1306 } else {
1307 /* Only value/mask trigger. */
1308 lut->m3 = 0xffff;
1309 }
ee492173 1310
c53d793f 1311 /* Triggertype: event. */
ee492173
HE
1312 lut->params.selres = 3;
1313
e46b8fb1 1314 return SR_OK;
ee492173 1315}