]> sigrok.org Git - libsigrok.git/blame - src/hardware/asix-sigma/protocol.c
asix-sigma: prepare FTDI open/close for "optional open"
[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>
9334ed6c 7 * Copyright (C) 2020 Gerhard Sittig <gerhard.sittig@gmx.net>
28a35d8a
HE
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
911f1834 23/*
6352d030 24 * ASIX SIGMA/SIGMA2 logic analyzer driver
911f1834
UH
25 */
26
6ec6c43b 27#include <config.h>
3ba56876 28#include "protocol.h"
28a35d8a 29
b1648dea 30/*
b65649f6
GS
31 * The ASIX SIGMA hardware supports fixed 200MHz and 100MHz sample rates
32 * (by means of separate firmware images). As well as 50MHz divided by
33 * an integer divider in the 1..256 range (by the "typical" firmware).
34 * Which translates to a strict lower boundary of around 195kHz.
35 *
36 * This driver "suggests" a subset of the available rates by listing a
37 * few discrete values, while setter routines accept any user specified
38 * rate that is supported by the hardware.
b1648dea 39 */
3ba56876 40SR_PRIV const uint64_t samplerates[] = {
b65649f6
GS
41 /* 50MHz and integer divider. 1/2/5 steps (where possible). */
42 SR_KHZ(200), SR_KHZ(500),
43 SR_MHZ(1), SR_MHZ(2), SR_MHZ(5),
44 SR_MHZ(10), SR_MHZ(25), SR_MHZ(50),
45 /* 100MHz/200MHz, fixed rates in special firmware. */
46 SR_MHZ(100), SR_MHZ(200),
28a35d8a
HE
47};
48
4154a516 49SR_PRIV const size_t samplerates_count = ARRAY_SIZE(samplerates);
39c64c6a 50
742368a2 51static const char *firmware_files[] = {
80e717b3
GS
52 [SIGMA_FW_50MHZ] = "asix-sigma-50.fw", /* 50MHz, 8bit divider. */
53 [SIGMA_FW_100MHZ] = "asix-sigma-100.fw", /* 100MHz, fixed. */
54 [SIGMA_FW_200MHZ] = "asix-sigma-200.fw", /* 200MHz, fixed. */
55 [SIGMA_FW_SYNC] = "asix-sigma-50sync.fw", /* Sync from external pin. */
56 [SIGMA_FW_FREQ] = "asix-sigma-phasor.fw", /* Frequency counter. */
f6564c8d
HE
57};
58
742368a2
GS
59#define SIGMA_FIRMWARE_SIZE_LIMIT (256 * 1024)
60
7fe1f91f
GS
61static int sigma_ftdi_open(const struct sr_dev_inst *sdi)
62{
63 struct dev_context *devc;
64 int vid, pid;
65 const char *serno;
66 int ret;
67
68 devc = sdi->priv;
69 if (!devc)
70 return SR_ERR_ARG;
71
72 if (devc->ftdi.is_open)
73 return SR_OK;
74
75 vid = devc->id.vid;
76 pid = devc->id.pid;
77 serno = sdi->serial_num;
78 if (!vid || !pid || !serno || !*serno)
79 return SR_ERR_ARG;
80
81 ret = ftdi_init(&devc->ftdi.ctx);
82 if (ret < 0) {
83 sr_err("Cannot initialize FTDI context (%d): %s.",
84 ret, ftdi_get_error_string(&devc->ftdi.ctx));
85 return SR_ERR_IO;
86 }
87 ret = ftdi_usb_open_desc_index(&devc->ftdi.ctx,
88 vid, pid, NULL, serno, 0);
89 if (ret < 0) {
90 sr_err("Cannot open device (%d): %s.",
91 ret, ftdi_get_error_string(&devc->ftdi.ctx));
92 return SR_ERR_IO;
93 }
94 devc->ftdi.is_open = TRUE;
95
96 return SR_OK;
97}
98
99static int sigma_ftdi_close(struct dev_context *devc)
100{
101 int ret;
102
103 ret = ftdi_usb_close(&devc->ftdi.ctx);
104 devc->ftdi.is_open = FALSE;
105 devc->ftdi.must_close = FALSE;
106 ftdi_deinit(&devc->ftdi.ctx);
107
108 return ret == 0 ? SR_OK : SR_ERR_IO;
109}
110
111SR_PRIV int sigma_check_open(const struct sr_dev_inst *sdi)
112{
113 struct dev_context *devc;
114 int ret;
115
116 if (!sdi)
117 return SR_ERR_ARG;
118 devc = sdi->priv;
119 if (!devc)
120 return SR_ERR_ARG;
121
122 if (devc->ftdi.is_open)
123 return SR_OK;
124
125 ret = sigma_ftdi_open(sdi);
126 if (ret != SR_OK)
127 return ret;
128 devc->ftdi.must_close = TRUE;
129
130 return ret;
131}
132
133SR_PRIV int sigma_check_close(struct dev_context *devc)
134{
135 int ret;
136
137 if (!devc)
138 return SR_ERR_ARG;
139
140 if (devc->ftdi.must_close) {
141 ret = sigma_ftdi_close(devc);
142 if (ret != SR_OK)
143 return ret;
144 devc->ftdi.must_close = FALSE;
145 }
146
147 return SR_OK;
148}
149
150SR_PRIV int sigma_force_open(const struct sr_dev_inst *sdi)
151{
152 struct dev_context *devc;
153 int ret;
154
155 if (!sdi)
156 return SR_ERR_ARG;
157 devc = sdi->priv;
158 if (!devc)
159 return SR_ERR_ARG;
160
161 ret = sigma_ftdi_open(sdi);
162 if (ret != SR_OK)
163 return ret;
164 devc->ftdi.must_close = FALSE;
165
166 return SR_OK;
167}
168
169SR_PRIV int sigma_force_close(struct dev_context *devc)
170{
171 return sigma_ftdi_close(devc);
172}
173
88a5f9ea
GS
174/*
175 * BEWARE! Error propagation is important, as are kinds of return values.
176 *
177 * - Raw USB tranport communicates the number of sent or received bytes,
178 * or negative error codes in the external library's(!) range of codes.
179 * - Internal routines at the "sigrok driver level" communicate success
180 * or failure in terms of SR_OK et al error codes.
181 * - Main loop style receive callbacks communicate booleans which arrange
182 * for repeated calls to drive progress during acquisition.
183 *
184 * Careful consideration by maintainers is essential, because all of the
185 * above kinds of values are assignment compatbile from the compiler's
186 * point of view. Implementation errors will go unnoticed at build time.
187 */
188
189static int sigma_read_raw(struct dev_context *devc, void *buf, size_t size)
28a35d8a
HE
190{
191 int ret;
fefa1800 192
7fe1f91f 193 ret = ftdi_read_data(&devc->ftdi.ctx, (unsigned char *)buf, size);
28a35d8a 194 if (ret < 0) {
88a5f9ea 195 sr_err("USB data read failed: %s",
7fe1f91f 196 ftdi_get_error_string(&devc->ftdi.ctx));
28a35d8a
HE
197 }
198
199 return ret;
200}
201
88a5f9ea 202static int sigma_write_raw(struct dev_context *devc, const void *buf, size_t size)
28a35d8a
HE
203{
204 int ret;
fefa1800 205
7fe1f91f 206 ret = ftdi_write_data(&devc->ftdi.ctx, buf, size);
88a5f9ea
GS
207 if (ret < 0) {
208 sr_err("USB data write failed: %s",
7fe1f91f 209 ftdi_get_error_string(&devc->ftdi.ctx));
88a5f9ea
GS
210 } else if ((size_t)ret != size) {
211 sr_err("USB data write length mismatch.");
212 }
28a35d8a
HE
213
214 return ret;
215}
216
88a5f9ea
GS
217static int sigma_read_sr(struct dev_context *devc, void *buf, size_t size)
218{
219 int ret;
220
221 ret = sigma_read_raw(devc, buf, size);
222 if (ret < 0 || (size_t)ret != size)
223 return SR_ERR_IO;
224
225 return SR_OK;
226}
227
228static int sigma_write_sr(struct dev_context *devc, const void *buf, size_t size)
229{
230 int ret;
231
232 ret = sigma_write_raw(devc, buf, size);
233 if (ret < 0 || (size_t)ret != size)
234 return SR_ERR_IO;
235
236 return SR_OK;
237}
238
e8686e3a 239/*
88a5f9ea
GS
240 * Implementor's note: The local write buffer's size shall suffice for
241 * any know FPGA register transaction that is involved in the supported
242 * feature set of this sigrok device driver. If the length check trips,
243 * that's a programmer's error and needs adjustment in the complete call
244 * stack of the respective code path.
e8686e3a 245 */
9b4d261f
GS
246SR_PRIV int sigma_write_register(struct dev_context *devc,
247 uint8_t reg, uint8_t *data, size_t len)
28a35d8a 248{
a53b8e4d 249 uint8_t buf[80], *wrptr;
88a5f9ea 250 size_t idx;
28a35d8a 251
a53b8e4d 252 if (2 + 2 * len > sizeof(buf)) {
88a5f9ea 253 sr_err("Short write buffer for %zu bytes to reg %u.", len, reg);
e8686e3a
AG
254 return SR_ERR_BUG;
255 }
256
a53b8e4d
GS
257 wrptr = buf;
258 write_u8_inc(&wrptr, REG_ADDR_LOW | (reg & 0xf));
259 write_u8_inc(&wrptr, REG_ADDR_HIGH | (reg >> 4));
260 for (idx = 0; idx < len; idx++) {
261 write_u8_inc(&wrptr, REG_DATA_LOW | (data[idx] & 0xf));
262 write_u8_inc(&wrptr, REG_DATA_HIGH_WRITE | (data[idx] >> 4));
28a35d8a
HE
263 }
264
88a5f9ea 265 return sigma_write_sr(devc, buf, wrptr - buf);
28a35d8a
HE
266}
267
9b4d261f
GS
268SR_PRIV int sigma_set_register(struct dev_context *devc,
269 uint8_t reg, uint8_t value)
28a35d8a 270{
9b4d261f 271 return sigma_write_register(devc, reg, &value, sizeof(value));
28a35d8a
HE
272}
273
9b4d261f
GS
274static int sigma_read_register(struct dev_context *devc,
275 uint8_t reg, uint8_t *data, size_t len)
28a35d8a 276{
a53b8e4d 277 uint8_t buf[3], *wrptr;
88a5f9ea 278 int ret;
28a35d8a 279
a53b8e4d
GS
280 wrptr = buf;
281 write_u8_inc(&wrptr, REG_ADDR_LOW | (reg & 0xf));
282 write_u8_inc(&wrptr, REG_ADDR_HIGH | (reg >> 4));
283 write_u8_inc(&wrptr, REG_READ_ADDR);
88a5f9ea
GS
284 ret = sigma_write_sr(devc, buf, wrptr - buf);
285 if (ret != SR_OK)
286 return ret;
28a35d8a 287
88a5f9ea 288 return sigma_read_sr(devc, data, len);
28a35d8a
HE
289}
290
9b4d261f 291static int sigma_read_pos(struct dev_context *devc,
88a5f9ea 292 uint32_t *stoppos, uint32_t *triggerpos, uint8_t *mode)
28a35d8a 293{
07411a60 294 /*
88a5f9ea
GS
295 * Read 7 registers starting at trigger position LSB.
296 * Which yields two 24bit counter values, and mode flags.
07411a60 297 */
a53b8e4d 298 const uint8_t buf[] = {
88a5f9ea 299 /* Setup first register address. */
28a35d8a 300 REG_ADDR_LOW | READ_TRIGGER_POS_LOW,
88a5f9ea
GS
301 /* Retrieve trigger position. */
302 REG_READ_ADDR | REG_ADDR_INC,
07411a60
GS
303 REG_READ_ADDR | REG_ADDR_INC,
304 REG_READ_ADDR | REG_ADDR_INC,
88a5f9ea 305 /* Retrieve stop position. */
07411a60
GS
306 REG_READ_ADDR | REG_ADDR_INC,
307 REG_READ_ADDR | REG_ADDR_INC,
308 REG_READ_ADDR | REG_ADDR_INC,
88a5f9ea 309 /* Retrieve mode register. */
07411a60 310 REG_READ_ADDR | REG_ADDR_INC,
a53b8e4d 311 }, *rdptr;
88a5f9ea
GS
312 uint8_t result[7];
313 uint32_t v32;
314 uint8_t v8;
315 int ret;
28a35d8a 316
88a5f9ea
GS
317 ret = sigma_write_sr(devc, buf, sizeof(buf));
318 if (ret != SR_OK)
319 return ret;
28a35d8a 320
88a5f9ea
GS
321 ret = sigma_read_sr(devc, result, sizeof(result));
322 if (ret != SR_OK)
323 return ret;
28a35d8a 324
a53b8e4d 325 rdptr = &result[0];
88a5f9ea
GS
326 v32 = read_u24le_inc(&rdptr);
327 if (triggerpos)
328 *triggerpos = v32;
329 v32 = read_u24le_inc(&rdptr);
330 if (stoppos)
331 *stoppos = v32;
332 v8 = read_u8_inc(&rdptr);
333 if (mode)
334 *mode = v8;
28a35d8a 335
dc400817 336 /*
a53b8e4d
GS
337 * These positions consist of "the memory row" in the MSB fields,
338 * and "an event index" within the row in the LSB fields. Part
339 * of the memory row's content is sample data, another part is
340 * timestamps.
2c33b092 341 *
a53b8e4d
GS
342 * The retrieved register values point to after the captured
343 * position. So they need to get decremented, and adjusted to
344 * cater for the timestamps when the decrement carries over to
345 * a different memory row.
dc400817 346 */
88a5f9ea 347 if (stoppos && (--*stoppos & ROW_MASK) == ROW_MASK)
a53b8e4d 348 *stoppos -= CLUSTERS_PER_ROW;
88a5f9ea 349 if (triggerpos && (--*triggerpos & ROW_MASK) == ROW_MASK)
a53b8e4d 350 *triggerpos -= CLUSTERS_PER_ROW;
57bbf56b 351
a53b8e4d 352 return SR_OK;
28a35d8a
HE
353}
354
9b4d261f
GS
355static int sigma_read_dram(struct dev_context *devc,
356 uint16_t startchunk, size_t numchunks, uint8_t *data)
28a35d8a 357{
a53b8e4d 358 uint8_t buf[128], *wrptr;
07411a60 359 size_t chunk;
88a5f9ea 360 int sel, ret;
07411a60 361 gboolean is_last;
28a35d8a 362
a53b8e4d 363 if (2 + 3 * numchunks > ARRAY_SIZE(buf)) {
88a5f9ea 364 sr_err("Short write buffer for %zu DRAM row reads.", numchunks);
a53b8e4d
GS
365 return SR_ERR_BUG;
366 }
367
07411a60 368 /* Communicate DRAM start address (memory row, aka samples line). */
a53b8e4d
GS
369 wrptr = buf;
370 write_u8_inc(&wrptr, startchunk >> 8);
371 write_u8_inc(&wrptr, startchunk & 0xff);
88a5f9ea
GS
372 ret = sigma_write_register(devc, WRITE_MEMROW, buf, wrptr - buf);
373 if (ret != SR_OK)
374 return ret;
28a35d8a 375
07411a60
GS
376 /*
377 * Access DRAM content. Fetch from DRAM to FPGA's internal RAM,
378 * then transfer via USB. Interleave the FPGA's DRAM access and
379 * USB transfer, use alternating buffers (0/1) in the process.
380 */
a53b8e4d
GS
381 wrptr = buf;
382 write_u8_inc(&wrptr, REG_DRAM_BLOCK);
383 write_u8_inc(&wrptr, REG_DRAM_WAIT_ACK);
07411a60
GS
384 for (chunk = 0; chunk < numchunks; chunk++) {
385 sel = chunk % 2;
386 is_last = chunk == numchunks - 1;
387 if (!is_last)
a53b8e4d
GS
388 write_u8_inc(&wrptr, REG_DRAM_BLOCK | REG_DRAM_SEL_BOOL(!sel));
389 write_u8_inc(&wrptr, REG_DRAM_BLOCK_DATA | REG_DRAM_SEL_BOOL(sel));
07411a60 390 if (!is_last)
a53b8e4d 391 write_u8_inc(&wrptr, REG_DRAM_WAIT_ACK);
28a35d8a 392 }
88a5f9ea
GS
393 ret = sigma_write_sr(devc, buf, wrptr - buf);
394 if (ret != SR_OK)
395 return ret;
28a35d8a 396
88a5f9ea 397 return sigma_read_sr(devc, data, numchunks * ROW_LENGTH_BYTES);
28a35d8a
HE
398}
399
4ae1f451 400/* Upload trigger look-up tables to Sigma. */
9b4d261f
GS
401SR_PRIV int sigma_write_trigger_lut(struct dev_context *devc,
402 struct triggerlut *lut)
ee492173
HE
403{
404 int i;
405 uint8_t tmp[2];
406 uint16_t bit;
a53b8e4d 407 uint8_t buf[6], *wrptr, regval;
88a5f9ea 408 int ret;
ee492173
HE
409
410 /* Transpose the table and send to Sigma. */
0a1f7b09 411 for (i = 0; i < 16; i++) {
ee492173
HE
412 bit = 1 << i;
413
414 tmp[0] = tmp[1] = 0;
415
416 if (lut->m2d[0] & bit)
417 tmp[0] |= 0x01;
418 if (lut->m2d[1] & bit)
419 tmp[0] |= 0x02;
420 if (lut->m2d[2] & bit)
421 tmp[0] |= 0x04;
422 if (lut->m2d[3] & bit)
423 tmp[0] |= 0x08;
424
425 if (lut->m3 & bit)
426 tmp[0] |= 0x10;
427 if (lut->m3s & bit)
428 tmp[0] |= 0x20;
429 if (lut->m4 & bit)
430 tmp[0] |= 0x40;
431
432 if (lut->m0d[0] & bit)
433 tmp[1] |= 0x01;
434 if (lut->m0d[1] & bit)
435 tmp[1] |= 0x02;
436 if (lut->m0d[2] & bit)
437 tmp[1] |= 0x04;
438 if (lut->m0d[3] & bit)
439 tmp[1] |= 0x08;
440
441 if (lut->m1d[0] & bit)
442 tmp[1] |= 0x10;
443 if (lut->m1d[1] & bit)
444 tmp[1] |= 0x20;
445 if (lut->m1d[2] & bit)
446 tmp[1] |= 0x40;
447 if (lut->m1d[3] & bit)
448 tmp[1] |= 0x80;
449
a53b8e4d
GS
450 /*
451 * This logic seems redundant, but separates the value
452 * determination from the wire format, and is useful
453 * during future maintenance and research.
454 */
455 wrptr = buf;
456 write_u8_inc(&wrptr, tmp[0]);
457 write_u8_inc(&wrptr, tmp[1]);
88a5f9ea
GS
458 ret = sigma_write_register(devc, WRITE_TRIGGER_SELECT, buf, wrptr - buf);
459 if (ret != SR_OK)
460 return ret;
461 ret = sigma_set_register(devc, WRITE_TRIGGER_SELECT2, 0x30 | i);
462 if (ret != SR_OK)
463 return ret;
ee492173
HE
464 }
465
466 /* Send the parameters */
a53b8e4d
GS
467 wrptr = buf;
468 regval = 0;
469 regval |= lut->params.selc << 6;
470 regval |= lut->params.selpresc << 0;
471 write_u8_inc(&wrptr, regval);
472 regval = 0;
473 regval |= lut->params.selinc << 6;
474 regval |= lut->params.selres << 4;
475 regval |= lut->params.sela << 2;
476 regval |= lut->params.selb << 0;
477 write_u8_inc(&wrptr, regval);
478 write_u16le_inc(&wrptr, lut->params.cmpb);
479 write_u16le_inc(&wrptr, lut->params.cmpa);
88a5f9ea
GS
480 ret = sigma_write_register(devc, WRITE_TRIGGER_SELECT, buf, wrptr - buf);
481 if (ret != SR_OK)
482 return ret;
ee492173 483
e46b8fb1 484 return SR_OK;
ee492173
HE
485}
486
d5fa188a 487/*
dc0906e2
GS
488 * See Xilinx UG332 for Spartan-3 FPGA configuration. The SIGMA device
489 * uses FTDI bitbang mode for netlist download in slave serial mode.
490 * (LATER: The OMEGA device's cable contains a more capable FTDI chip
491 * and uses MPSSE mode for bitbang. -- Can we also use FT232H in FT245
492 * compatible bitbang mode? For maximum code re-use and reduced libftdi
493 * dependency? See section 3.5.5 of FT232H: D0 clk, D1 data (out), D2
494 * data (in), D3 select, D4-7 GPIOL. See section 3.5.7 for MCU FIFO.)
495 *
496 * 750kbps rate (four times the speed of sigmalogan) works well for
497 * netlist download. All pins except INIT_B are output pins during
498 * configuration download.
499 *
500 * Some pins are inverted as a byproduct of level shifting circuitry.
501 * That's why high CCLK level (from the cable's point of view) is idle
502 * from the FPGA's perspective.
503 *
504 * The vendor's literature discusses a "suicide sequence" which ends
505 * regular FPGA execution and should be sent before entering bitbang
506 * mode and sending configuration data. Set D7 and toggle D2, D3, D4
507 * a few times.
508 */
509#define BB_PIN_CCLK (1 << 0) /* D0, CCLK */
510#define BB_PIN_PROG (1 << 1) /* D1, PROG */
511#define BB_PIN_D2 (1 << 2) /* D2, (part of) SUICIDE */
512#define BB_PIN_D3 (1 << 3) /* D3, (part of) SUICIDE */
513#define BB_PIN_D4 (1 << 4) /* D4, (part of) SUICIDE (unused?) */
514#define BB_PIN_INIT (1 << 5) /* D5, INIT, input pin */
515#define BB_PIN_DIN (1 << 6) /* D6, DIN */
516#define BB_PIN_D7 (1 << 7) /* D7, (part of) SUICIDE */
517
518#define BB_BITRATE (750 * 1000)
519#define BB_PINMASK (0xff & ~BB_PIN_INIT)
520
521/*
522 * Initiate slave serial mode for configuration download. Which is done
523 * by pulsing PROG_B and sensing INIT_B. Make sure CCLK is idle before
c749d1ca
GS
524 * initiating the configuration download.
525 *
526 * Run a "suicide sequence" first to terminate the regular FPGA operation
527 * before reconfiguration. The FTDI cable is single channel, and shares
528 * pins which are used for data communication in FIFO mode with pins that
529 * are used for FPGA configuration in bitbang mode. Hardware defaults for
530 * unconfigured hardware, and runtime conditions after FPGA configuration
531 * need to cooperate such that re-configuration of the FPGA can start.
d5fa188a 532 */
c749d1ca 533static int sigma_fpga_init_bitbang_once(struct dev_context *devc)
d5fa188a 534{
a53b8e4d 535 const uint8_t suicide[] = {
dc0906e2
GS
536 BB_PIN_D7 | BB_PIN_D2,
537 BB_PIN_D7 | BB_PIN_D2,
538 BB_PIN_D7 | BB_PIN_D3,
539 BB_PIN_D7 | BB_PIN_D2,
540 BB_PIN_D7 | BB_PIN_D3,
541 BB_PIN_D7 | BB_PIN_D2,
542 BB_PIN_D7 | BB_PIN_D3,
543 BB_PIN_D7 | BB_PIN_D2,
d5fa188a 544 };
a53b8e4d 545 const uint8_t init_array[] = {
dc0906e2
GS
546 BB_PIN_CCLK,
547 BB_PIN_CCLK | BB_PIN_PROG,
548 BB_PIN_CCLK | BB_PIN_PROG,
549 BB_PIN_CCLK,
550 BB_PIN_CCLK,
551 BB_PIN_CCLK,
552 BB_PIN_CCLK,
553 BB_PIN_CCLK,
554 BB_PIN_CCLK,
555 BB_PIN_CCLK,
d5fa188a 556 };
dc0906e2 557 int retries, ret;
d5fa188a
MV
558 uint8_t data;
559
560 /* Section 2. part 1), do the FPGA suicide. */
88a5f9ea
GS
561 ret = SR_OK;
562 ret |= sigma_write_sr(devc, suicide, sizeof(suicide));
563 ret |= sigma_write_sr(devc, suicide, sizeof(suicide));
564 ret |= sigma_write_sr(devc, suicide, sizeof(suicide));
565 ret |= sigma_write_sr(devc, suicide, sizeof(suicide));
566 if (ret != SR_OK)
567 return SR_ERR_IO;
c749d1ca 568 g_usleep(10 * 1000);
d5fa188a 569
dc0906e2 570 /* Section 2. part 2), pulse PROG. */
88a5f9ea
GS
571 ret = sigma_write_sr(devc, init_array, sizeof(init_array));
572 if (ret != SR_OK)
573 return ret;
c749d1ca 574 g_usleep(10 * 1000);
7fe1f91f 575 ftdi_usb_purge_buffers(&devc->ftdi.ctx);
d5fa188a 576
88a5f9ea
GS
577 /*
578 * Wait until the FPGA asserts INIT_B. Check in a maximum number
579 * of bursts with a given delay between them. Read as many pin
580 * capture results as the combination of FTDI chip and FTID lib
581 * may provide. Cope with absence of pin capture data in a cycle.
582 * This approach shall result in fast reponse in case of success,
583 * low cost of execution during wait, reliable error handling in
584 * the transport layer, and robust response to failure or absence
585 * of result data (hardware inactivity after stimulus).
586 */
dc0906e2
GS
587 retries = 10;
588 while (retries--) {
88a5f9ea
GS
589 do {
590 ret = sigma_read_raw(devc, &data, sizeof(data));
591 if (ret < 0)
592 return SR_ERR_IO;
593 if (ret == sizeof(data) && (data & BB_PIN_INIT))
594 return SR_OK;
595 } while (ret == sizeof(data));
596 if (retries)
597 g_usleep(10 * 1000);
d5fa188a
MV
598 }
599
600 return SR_ERR_TIMEOUT;
601}
602
c749d1ca
GS
603/*
604 * This is belt and braces. Re-run the bitbang initiation sequence a few
605 * times should first attempts fail. Failure is rare but can happen (was
606 * observed during driver development).
607 */
608static int sigma_fpga_init_bitbang(struct dev_context *devc)
609{
610 size_t retries;
611 int ret;
612
613 retries = 10;
614 while (retries--) {
615 ret = sigma_fpga_init_bitbang_once(devc);
616 if (ret == SR_OK)
617 return ret;
618 if (ret != SR_ERR_TIMEOUT)
619 return ret;
620 }
621 return ret;
622}
623
64fe661b
MV
624/*
625 * Configure the FPGA for logic-analyzer mode.
626 */
627static int sigma_fpga_init_la(struct dev_context *devc)
628{
a53b8e4d
GS
629 uint8_t buf[16], *wrptr;
630 uint8_t data_55, data_aa, mode;
64fe661b 631 uint8_t result[3];
a53b8e4d 632 const uint8_t *rdptr;
64fe661b
MV
633 int ret;
634
a53b8e4d
GS
635 wrptr = buf;
636
637 /* Read ID register. */
638 write_u8_inc(&wrptr, REG_ADDR_LOW | (READ_ID & 0xf));
639 write_u8_inc(&wrptr, REG_ADDR_HIGH | (READ_ID >> 4));
640 write_u8_inc(&wrptr, REG_READ_ADDR);
641
642 /* Write 0x55 to scratch register, read back. */
643 data_55 = 0x55;
644 write_u8_inc(&wrptr, REG_ADDR_LOW | (WRITE_TEST & 0xf));
645 write_u8_inc(&wrptr, REG_DATA_LOW | (data_55 & 0xf));
646 write_u8_inc(&wrptr, REG_DATA_HIGH_WRITE | (data_55 >> 4));
647 write_u8_inc(&wrptr, REG_READ_ADDR);
648
649 /* Write 0xaa to scratch register, read back. */
650 data_aa = 0xaa;
651 write_u8_inc(&wrptr, REG_ADDR_LOW | (WRITE_TEST & 0xf));
652 write_u8_inc(&wrptr, REG_DATA_LOW | (data_aa & 0xf));
653 write_u8_inc(&wrptr, REG_DATA_HIGH_WRITE | (data_aa >> 4));
654 write_u8_inc(&wrptr, REG_READ_ADDR);
655
656 /* Initiate SDRAM initialization in mode register. */
657 mode = WMR_SDRAMINIT;
658 write_u8_inc(&wrptr, REG_ADDR_LOW | (WRITE_MODE & 0xf));
659 write_u8_inc(&wrptr, REG_DATA_LOW | (mode & 0xf));
660 write_u8_inc(&wrptr, REG_DATA_HIGH_WRITE | (mode >> 4));
661
dc0906e2
GS
662 /*
663 * Send the command sequence which contains 3 READ requests.
664 * Expect to see the corresponding 3 response bytes.
665 */
88a5f9ea
GS
666 ret = sigma_write_sr(devc, buf, wrptr - buf);
667 if (ret != SR_OK) {
668 sr_err("Could not request LA start response.");
669 return ret;
670 }
671 ret = sigma_read_sr(devc, result, ARRAY_SIZE(result));
672 if (ret != SR_OK) {
673 sr_err("Could not receive LA start response.");
a53b8e4d
GS
674 return SR_ERR_IO;
675 }
676 rdptr = result;
677 if (read_u8_inc(&rdptr) != 0xa6) {
678 sr_err("Unexpected ID response.");
679 return SR_ERR_DATA;
680 }
681 if (read_u8_inc(&rdptr) != data_55) {
682 sr_err("Unexpected scratch read-back (55).");
683 return SR_ERR_DATA;
684 }
685 if (read_u8_inc(&rdptr) != data_aa) {
686 sr_err("Unexpected scratch read-back (aa).");
687 return SR_ERR_DATA;
688 }
64fe661b
MV
689
690 return SR_OK;
64fe661b
MV
691}
692
a80226bb
MV
693/*
694 * Read the firmware from a file and transform it into a series of bitbang
695 * pulses used to program the FPGA. Note that the *bb_cmd must be free()'d
696 * by the caller of this function.
697 */
8e2d6c9d 698static int sigma_fw_2_bitbang(struct sr_context *ctx, const char *name,
9b4d261f 699 uint8_t **bb_cmd, gsize *bb_cmd_size)
a80226bb 700{
dc0906e2
GS
701 uint8_t *firmware;
702 size_t file_size;
703 uint8_t *p;
704 size_t l;
a80226bb 705 uint32_t imm;
dc0906e2
GS
706 size_t bb_size;
707 uint8_t *bb_stream, *bbs, byte, mask, v;
a80226bb 708
387825dc 709 /* Retrieve the on-disk firmware file content. */
742368a2
GS
710 firmware = sr_resource_load(ctx, SR_RESOURCE_FIRMWARE, name,
711 &file_size, SIGMA_FIRMWARE_SIZE_LIMIT);
8e2d6c9d 712 if (!firmware)
dc0906e2 713 return SR_ERR_IO;
a80226bb 714
387825dc 715 /* Unscramble the file content (XOR with "random" sequence). */
dc0906e2
GS
716 p = firmware;
717 l = file_size;
a80226bb 718 imm = 0x3f6df2ab;
dc0906e2 719 while (l--) {
a80226bb 720 imm = (imm + 0xa853753) % 177 + (imm * 0x8034052);
dc0906e2 721 *p++ ^= imm & 0xff;
a80226bb
MV
722 }
723
724 /*
387825dc
GS
725 * Generate a sequence of bitbang samples. With two samples per
726 * FPGA configuration bit, providing the level for the DIN signal
727 * as well as two edges for CCLK. See Xilinx UG332 for details
728 * ("slave serial" mode).
729 *
730 * Note that CCLK is inverted in hardware. That's why the
731 * respective bit is first set and then cleared in the bitbang
732 * sample sets. So that the DIN level will be stable when the
733 * data gets sampled at the rising CCLK edge, and the signals'
734 * setup time constraint will be met.
735 *
736 * The caller will put the FPGA into download mode, will send
737 * the bitbang samples, and release the allocated memory.
a80226bb 738 */
a80226bb 739 bb_size = file_size * 8 * 2;
dc0906e2 740 bb_stream = g_try_malloc(bb_size);
a80226bb 741 if (!bb_stream) {
88a5f9ea 742 sr_err("Memory allocation failed during firmware upload.");
dc0906e2
GS
743 g_free(firmware);
744 return SR_ERR_MALLOC;
a80226bb 745 }
a80226bb 746 bbs = bb_stream;
dc0906e2
GS
747 p = firmware;
748 l = file_size;
749 while (l--) {
750 byte = *p++;
751 mask = 0x80;
752 while (mask) {
753 v = (byte & mask) ? BB_PIN_DIN : 0;
754 mask >>= 1;
755 *bbs++ = v | BB_PIN_CCLK;
a80226bb
MV
756 *bbs++ = v;
757 }
758 }
dc0906e2 759 g_free(firmware);
a80226bb
MV
760
761 /* The transformation completed successfully, return the result. */
762 *bb_cmd = bb_stream;
763 *bb_cmd_size = bb_size;
764
dc0906e2 765 return SR_OK;
a80226bb
MV
766}
767
9b4d261f
GS
768static int upload_firmware(struct sr_context *ctx, struct dev_context *devc,
769 enum sigma_firmware_idx firmware_idx)
28a35d8a
HE
770{
771 int ret;
a53b8e4d
GS
772 uint8_t *buf;
773 uint8_t pins;
28a35d8a 774 size_t buf_size;
a9016883 775 const char *firmware;
a9016883 776
80e717b3
GS
777 /* Check for valid firmware file selection. */
778 if (firmware_idx >= ARRAY_SIZE(firmware_files))
779 return SR_ERR_ARG;
4b25cbff 780 firmware = firmware_files[firmware_idx];
80e717b3
GS
781 if (!firmware || !*firmware)
782 return SR_ERR_ARG;
783
784 /* Avoid downloading the same firmware multiple times. */
785 if (devc->firmware_idx == firmware_idx) {
a9016883
GS
786 sr_info("Not uploading firmware file '%s' again.", firmware);
787 return SR_OK;
788 }
28a35d8a 789
1bb9dc82
GS
790 devc->state.state = SIGMA_CONFIG;
791
dc0906e2 792 /* Set the cable to bitbang mode. */
7fe1f91f 793 ret = ftdi_set_bitmode(&devc->ftdi.ctx, BB_PINMASK, BITMODE_BITBANG);
8bbf7627 794 if (ret < 0) {
88a5f9ea 795 sr_err("Could not setup cable mode for upload: %s",
7fe1f91f 796 ftdi_get_error_string(&devc->ftdi.ctx));
7bcf2168 797 return SR_ERR;
28a35d8a 798 }
7fe1f91f 799 ret = ftdi_set_baudrate(&devc->ftdi.ctx, BB_BITRATE);
8bbf7627 800 if (ret < 0) {
88a5f9ea 801 sr_err("Could not setup bitrate for upload: %s",
7fe1f91f 802 ftdi_get_error_string(&devc->ftdi.ctx));
7bcf2168 803 return SR_ERR;
28a35d8a
HE
804 }
805
dc0906e2 806 /* Initiate FPGA configuration mode. */
d5fa188a 807 ret = sigma_fpga_init_bitbang(devc);
88a5f9ea
GS
808 if (ret) {
809 sr_err("Could not initiate firmware upload to hardware");
d5fa188a 810 return ret;
88a5f9ea 811 }
28a35d8a 812
dc0906e2 813 /* Prepare wire format of the firmware image. */
8e2d6c9d 814 ret = sigma_fw_2_bitbang(ctx, firmware, &buf, &buf_size);
8bbf7627 815 if (ret != SR_OK) {
88a5f9ea 816 sr_err("Could not prepare file %s for upload.", firmware);
b53738ba 817 return ret;
28a35d8a
HE
818 }
819
dc0906e2 820 /* Write the FPGA netlist to the cable. */
499b17e9 821 sr_info("Uploading firmware file '%s'.", firmware);
88a5f9ea 822 ret = sigma_write_sr(devc, buf, buf_size);
28a35d8a 823 g_free(buf);
88a5f9ea
GS
824 if (ret != SR_OK) {
825 sr_err("Could not upload firmware file '%s'.", firmware);
826 return ret;
827 }
28a35d8a 828
dc0906e2 829 /* Leave bitbang mode and discard pending input data. */
7fe1f91f 830 ret = ftdi_set_bitmode(&devc->ftdi.ctx, 0, BITMODE_RESET);
8bbf7627 831 if (ret < 0) {
88a5f9ea 832 sr_err("Could not setup cable mode after upload: %s",
7fe1f91f 833 ftdi_get_error_string(&devc->ftdi.ctx));
e46b8fb1 834 return SR_ERR;
28a35d8a 835 }
7fe1f91f 836 ftdi_usb_purge_buffers(&devc->ftdi.ctx);
88a5f9ea 837 while (sigma_read_raw(devc, &pins, sizeof(pins)) > 0)
28a35d8a
HE
838 ;
839
64fe661b
MV
840 /* Initialize the FPGA for logic-analyzer mode. */
841 ret = sigma_fpga_init_la(devc);
88a5f9ea
GS
842 if (ret != SR_OK) {
843 sr_err("Hardware response after firmware upload failed.");
64fe661b 844 return ret;
88a5f9ea 845 }
28a35d8a 846
dc0906e2 847 /* Keep track of successful firmware download completion. */
1bb9dc82 848 devc->state.state = SIGMA_IDLE;
80e717b3 849 devc->firmware_idx = firmware_idx;
47f4f073 850 sr_info("Firmware uploaded.");
e3fff420 851
e46b8fb1 852 return SR_OK;
f6564c8d
HE
853}
854
9a0a606a 855/*
5e78a564
GS
856 * The driver supports user specified time or sample count limits. The
857 * device's hardware supports neither, and hardware compression prevents
858 * reliable detection of "fill levels" (currently reached sample counts)
859 * from register values during acquisition. That's why the driver needs
860 * to apply some heuristics:
9a0a606a 861 *
5e78a564
GS
862 * - The (optional) sample count limit and the (normalized) samplerate
863 * get mapped to an estimated duration for these samples' acquisition.
864 * - The (optional) time limit gets checked as well. The lesser of the
865 * two limits will terminate the data acquisition phase. The exact
866 * sample count limit gets enforced in session feed submission paths.
867 * - Some slack needs to be given to account for hardware pipelines as
868 * well as late storage of last chunks after compression thresholds
869 * are tripped. The resulting data set will span at least the caller
870 * specified period of time, which shall be perfectly acceptable.
871 *
872 * With RLE compression active, up to 64K sample periods can pass before
873 * a cluster accumulates. Which translates to 327ms at 200kHz. Add two
874 * times that period for good measure, one is not enough to flush the
875 * hardware pipeline (observation from an earlier experiment).
9a0a606a 876 */
5e78a564 877SR_PRIV int sigma_set_acquire_timeout(struct dev_context *devc)
9a0a606a 878{
5e78a564
GS
879 int ret;
880 GVariant *data;
881 uint64_t user_count, user_msecs;
9a0a606a 882 uint64_t worst_cluster_time_ms;
5e78a564 883 uint64_t count_msecs, acquire_msecs;
9a0a606a 884
5e78a564
GS
885 sr_sw_limits_init(&devc->acq_limits);
886
887 /* Get sample count limit, convert to msecs. */
888 ret = sr_sw_limits_config_get(&devc->cfg_limits,
889 SR_CONF_LIMIT_SAMPLES, &data);
890 if (ret != SR_OK)
891 return ret;
892 user_count = g_variant_get_uint64(data);
893 g_variant_unref(data);
894 count_msecs = 0;
895 if (user_count)
896 count_msecs = 1000 * user_count / devc->samplerate + 1;
897
898 /* Get time limit, which is in msecs. */
899 ret = sr_sw_limits_config_get(&devc->cfg_limits,
900 SR_CONF_LIMIT_MSEC, &data);
901 if (ret != SR_OK)
902 return ret;
903 user_msecs = g_variant_get_uint64(data);
904 g_variant_unref(data);
905
906 /* Get the lesser of them, with both being optional. */
907 acquire_msecs = ~0ull;
908 if (user_count && count_msecs < acquire_msecs)
909 acquire_msecs = count_msecs;
910 if (user_msecs && user_msecs < acquire_msecs)
911 acquire_msecs = user_msecs;
912 if (acquire_msecs == ~0ull)
913 return SR_OK;
914
915 /* Add some slack, and use that timeout for acquisition. */
916 worst_cluster_time_ms = 1000 * 65536 / devc->samplerate;
917 acquire_msecs += 2 * worst_cluster_time_ms;
918 data = g_variant_new_uint64(acquire_msecs);
919 ret = sr_sw_limits_config_set(&devc->acq_limits,
920 SR_CONF_LIMIT_MSEC, data);
921 g_variant_unref(data);
922 if (ret != SR_OK)
923 return ret;
924
925 sr_sw_limits_acquisition_start(&devc->acq_limits);
926 return SR_OK;
9a0a606a
GS
927}
928
5e78a564
GS
929/*
930 * Check whether a caller specified samplerate matches the device's
931 * hardware constraints (can be used for acquisition). Optionally yield
932 * a value that approximates the original spec.
933 *
934 * This routine assumes that input specs are in the 200kHz to 200MHz
935 * range of supported rates, and callers typically want to normalize a
936 * given value to the hardware capabilities. Values in the 50MHz range
937 * get rounded up by default, to avoid a more expensive check for the
938 * closest match, while higher sampling rate is always desirable during
939 * measurement. Input specs which exactly match hardware capabilities
940 * remain unaffected. Because 100/200MHz rates also limit the number of
941 * available channels, they are not suggested by this routine, instead
942 * callers need to pick them consciously.
943 */
944SR_PRIV int sigma_normalize_samplerate(uint64_t want_rate, uint64_t *have_rate)
945{
946 uint64_t div, rate;
947
948 /* Accept exact matches for 100/200MHz. */
949 if (want_rate == SR_MHZ(200) || want_rate == SR_MHZ(100)) {
950 if (have_rate)
951 *have_rate = want_rate;
952 return SR_OK;
953 }
954
955 /* Accept 200kHz to 50MHz range, and map to near value. */
956 if (want_rate >= SR_KHZ(200) && want_rate <= SR_MHZ(50)) {
957 div = SR_MHZ(50) / want_rate;
958 rate = SR_MHZ(50) / div;
959 if (have_rate)
960 *have_rate = rate;
961 return SR_OK;
962 }
963
964 return SR_ERR_ARG;
965}
966
967SR_PRIV int sigma_set_samplerate(const struct sr_dev_inst *sdi)
f6564c8d 968{
2c9c0df8 969 struct dev_context *devc;
8e2d6c9d 970 struct drv_context *drvc;
5e78a564 971 uint64_t samplerate;
2c9c0df8 972 int ret;
ac9534f4 973 int num_channels;
f6564c8d 974
2c9c0df8 975 devc = sdi->priv;
8e2d6c9d 976 drvc = sdi->driver->context;
f4abaa9f 977
5e78a564
GS
978 /* Accept any caller specified rate which the hardware supports. */
979 ret = sigma_normalize_samplerate(devc->samplerate, &samplerate);
980 if (ret != SR_OK)
981 return ret;
f6564c8d 982
2f7e529c
GS
983 /*
984 * Depending on the samplerates of 200/100/50- MHz, specific
985 * firmware is required and higher rates might limit the set
986 * of available channels.
987 */
ac9534f4 988 num_channels = devc->num_channels;
59df0c77 989 if (samplerate <= SR_MHZ(50)) {
80e717b3 990 ret = upload_firmware(drvc->sr_ctx, devc, SIGMA_FW_50MHZ);
ac9534f4 991 num_channels = 16;
6b2d3385 992 } else if (samplerate == SR_MHZ(100)) {
80e717b3 993 ret = upload_firmware(drvc->sr_ctx, devc, SIGMA_FW_100MHZ);
ac9534f4 994 num_channels = 8;
6b2d3385 995 } else if (samplerate == SR_MHZ(200)) {
80e717b3 996 ret = upload_firmware(drvc->sr_ctx, devc, SIGMA_FW_200MHZ);
ac9534f4 997 num_channels = 4;
f78898e9 998 }
f6564c8d 999
2f7e529c 1000 /*
5e78a564
GS
1001 * The samplerate affects the number of available logic channels
1002 * as well as a sample memory layout detail (the number of samples
1003 * which the device will communicate within an "event").
2f7e529c 1004 */
6b2d3385 1005 if (ret == SR_OK) {
ac9534f4 1006 devc->num_channels = num_channels;
6b2d3385 1007 devc->samples_per_event = 16 / devc->num_channels;
6b2d3385 1008 }
f6564c8d 1009
e8397563 1010 return ret;
28a35d8a
HE
1011}
1012
98b43eb3
GS
1013/*
1014 * Arrange for a session feed submit buffer. A queue where a number of
1015 * samples gets accumulated to reduce the number of send calls. Which
1016 * also enforces an optional sample count limit for data acquisition.
1017 *
1018 * The buffer holds up to CHUNK_SIZE bytes. The unit size is fixed (the
1019 * driver provides a fixed channel layout regardless of samplerate).
1020 */
1021
1022#define CHUNK_SIZE (4 * 1024 * 1024)
1023
1024struct submit_buffer {
1025 size_t unit_size;
1026 size_t max_samples, curr_samples;
1027 uint8_t *sample_data;
1028 uint8_t *write_pointer;
1029 struct sr_dev_inst *sdi;
1030 struct sr_datafeed_packet packet;
1031 struct sr_datafeed_logic logic;
98b43eb3
GS
1032};
1033
1034static int alloc_submit_buffer(struct sr_dev_inst *sdi)
1035{
1036 struct dev_context *devc;
1037 struct submit_buffer *buffer;
1038 size_t size;
1039
1040 devc = sdi->priv;
1041
1042 buffer = g_malloc0(sizeof(*buffer));
1043 devc->buffer = buffer;
1044
1045 buffer->unit_size = sizeof(uint16_t);
1046 size = CHUNK_SIZE;
1047 size /= buffer->unit_size;
1048 buffer->max_samples = size;
1049 size *= buffer->unit_size;
1050 buffer->sample_data = g_try_malloc0(size);
1051 if (!buffer->sample_data)
1052 return SR_ERR_MALLOC;
1053 buffer->write_pointer = buffer->sample_data;
5e78a564 1054 sr_sw_limits_init(&devc->feed_limits);
98b43eb3
GS
1055
1056 buffer->sdi = sdi;
1057 memset(&buffer->logic, 0, sizeof(buffer->logic));
1058 buffer->logic.unitsize = buffer->unit_size;
1059 buffer->logic.data = buffer->sample_data;
1060 memset(&buffer->packet, 0, sizeof(buffer->packet));
1061 buffer->packet.type = SR_DF_LOGIC;
1062 buffer->packet.payload = &buffer->logic;
1063
1064 return SR_OK;
1065}
1066
5e78a564 1067static int setup_submit_limit(struct dev_context *devc)
98b43eb3 1068{
5e78a564 1069 struct sr_sw_limits *limits;
98b43eb3
GS
1070 int ret;
1071 GVariant *data;
1072 uint64_t total;
1073
5e78a564 1074 limits = &devc->feed_limits;
98b43eb3 1075
5e78a564
GS
1076 ret = sr_sw_limits_config_get(&devc->cfg_limits,
1077 SR_CONF_LIMIT_SAMPLES, &data);
1078 if (ret != SR_OK)
1079 return ret;
1080 total = g_variant_get_uint64(data);
1081 g_variant_unref(data);
1082
1083 sr_sw_limits_init(limits);
98b43eb3
GS
1084 if (total) {
1085 data = g_variant_new_uint64(total);
5e78a564 1086 ret = sr_sw_limits_config_set(limits,
98b43eb3
GS
1087 SR_CONF_LIMIT_SAMPLES, data);
1088 g_variant_unref(data);
1089 if (ret != SR_OK)
1090 return ret;
1091 }
1092
5e78a564 1093 sr_sw_limits_acquisition_start(limits);
98b43eb3
GS
1094
1095 return SR_OK;
1096}
1097
1098static void free_submit_buffer(struct dev_context *devc)
1099{
1100 struct submit_buffer *buffer;
1101
1102 if (!devc)
1103 return;
1104
1105 buffer = devc->buffer;
1106 if (!buffer)
1107 return;
1108 devc->buffer = NULL;
1109
1110 g_free(buffer->sample_data);
1111 g_free(buffer);
1112}
1113
1114static int flush_submit_buffer(struct dev_context *devc)
1115{
1116 struct submit_buffer *buffer;
1117 int ret;
1118
1119 buffer = devc->buffer;
1120
1121 /* Is queued sample data available? */
1122 if (!buffer->curr_samples)
1123 return SR_OK;
1124
1125 /* Submit to the session feed. */
1126 buffer->logic.length = buffer->curr_samples * buffer->unit_size;
1127 ret = sr_session_send(buffer->sdi, &buffer->packet);
1128 if (ret != SR_OK)
1129 return ret;
1130
1131 /* Rewind queue position. */
1132 buffer->curr_samples = 0;
1133 buffer->write_pointer = buffer->sample_data;
1134
1135 return SR_OK;
1136}
1137
1138static int addto_submit_buffer(struct dev_context *devc,
1139 uint16_t sample, size_t count)
1140{
1141 struct submit_buffer *buffer;
5e78a564 1142 struct sr_sw_limits *limits;
98b43eb3
GS
1143 int ret;
1144
1145 buffer = devc->buffer;
5e78a564
GS
1146 limits = &devc->feed_limits;
1147 if (sr_sw_limits_check(limits))
98b43eb3
GS
1148 count = 0;
1149
1150 /*
1151 * Individually accumulate and check each sample, such that
1152 * accumulation between flushes won't exceed local storage, and
1153 * enforcement of user specified limits is exact.
1154 */
1155 while (count--) {
a53b8e4d 1156 write_u16le_inc(&buffer->write_pointer, sample);
98b43eb3
GS
1157 buffer->curr_samples++;
1158 if (buffer->curr_samples == buffer->max_samples) {
1159 ret = flush_submit_buffer(devc);
1160 if (ret != SR_OK)
1161 return ret;
1162 }
5e78a564
GS
1163 sr_sw_limits_update_samples_read(limits, 1);
1164 if (sr_sw_limits_check(limits))
98b43eb3
GS
1165 break;
1166 }
1167
1168 return SR_OK;
1169}
1170
c53d793f
HE
1171/*
1172 * In 100 and 200 MHz mode, only a single pin rising/falling can be
1173 * set as trigger. In other modes, two rising/falling triggers can be set,
ba7dd8bb 1174 * in addition to value/mask trigger for any number of channels.
c53d793f
HE
1175 *
1176 * The Sigma supports complex triggers using boolean expressions, but this
1177 * has not been implemented yet.
1178 */
3ba56876 1179SR_PRIV int sigma_convert_trigger(const struct sr_dev_inst *sdi)
57bbf56b 1180{
39c64c6a
BV
1181 struct dev_context *devc;
1182 struct sr_trigger *trigger;
1183 struct sr_trigger_stage *stage;
1184 struct sr_trigger_match *match;
1185 const GSList *l, *m;
1186 int channelbit, trigger_set;
57bbf56b 1187
39c64c6a 1188 devc = sdi->priv;
5c231fc4
GS
1189 memset(&devc->trigger, 0, sizeof(devc->trigger));
1190 trigger = sr_session_trigger_get(sdi->session);
1191 if (!trigger)
39c64c6a
BV
1192 return SR_OK;
1193
1194 trigger_set = 0;
1195 for (l = trigger->stages; l; l = l->next) {
1196 stage = l->data;
1197 for (m = stage->matches; m; m = m->next) {
1198 match = m->data;
9b4d261f 1199 /* Ignore disabled channels with a trigger. */
39c64c6a 1200 if (!match->channel->enabled)
39c64c6a 1201 continue;
a53b8e4d 1202 channelbit = 1 << match->channel->index;
5e78a564 1203 if (devc->samplerate >= SR_MHZ(100)) {
39c64c6a
BV
1204 /* Fast trigger support. */
1205 if (trigger_set) {
88a5f9ea 1206 sr_err("100/200MHz modes limited to single trigger pin.");
39c64c6a
BV
1207 return SR_ERR;
1208 }
a53b8e4d 1209 if (match->match == SR_TRIGGER_FALLING) {
39c64c6a 1210 devc->trigger.fallingmask |= channelbit;
a53b8e4d 1211 } else if (match->match == SR_TRIGGER_RISING) {
39c64c6a 1212 devc->trigger.risingmask |= channelbit;
a53b8e4d 1213 } else {
88a5f9ea 1214 sr_err("100/200MHz modes limited to edge trigger.");
39c64c6a
BV
1215 return SR_ERR;
1216 }
eec5275e 1217
0a1f7b09 1218 trigger_set++;
39c64c6a
BV
1219 } else {
1220 /* Simple trigger support (event). */
1221 if (match->match == SR_TRIGGER_ONE) {
1222 devc->trigger.simplevalue |= channelbit;
1223 devc->trigger.simplemask |= channelbit;
8ebad343 1224 } else if (match->match == SR_TRIGGER_ZERO) {
39c64c6a
BV
1225 devc->trigger.simplevalue &= ~channelbit;
1226 devc->trigger.simplemask |= channelbit;
8ebad343 1227 } else if (match->match == SR_TRIGGER_FALLING) {
39c64c6a 1228 devc->trigger.fallingmask |= channelbit;
0a1f7b09 1229 trigger_set++;
8ebad343 1230 } else if (match->match == SR_TRIGGER_RISING) {
39c64c6a 1231 devc->trigger.risingmask |= channelbit;
0a1f7b09 1232 trigger_set++;
39c64c6a
BV
1233 }
1234
1235 /*
1236 * Actually, Sigma supports 2 rising/falling triggers,
1237 * but they are ORed and the current trigger syntax
1238 * does not permit ORed triggers.
1239 */
1240 if (trigger_set > 1) {
88a5f9ea 1241 sr_err("Limited to 1 edge trigger.");
39c64c6a
BV
1242 return SR_ERR;
1243 }
ee492173 1244 }
ee492173 1245 }
57bbf56b
HE
1246 }
1247
e46b8fb1 1248 return SR_OK;
57bbf56b
HE
1249}
1250
36b1c8e6 1251/* Software trigger to determine exact trigger position. */
5fc01191 1252static int get_trigger_offset(uint8_t *samples, uint16_t last_sample,
9b4d261f 1253 struct sigma_trigger *t)
36b1c8e6 1254{
a53b8e4d 1255 const uint8_t *rdptr;
36b1c8e6 1256 int i;
a53b8e4d 1257 uint16_t sample;
36b1c8e6 1258
a53b8e4d
GS
1259 rdptr = samples;
1260 sample = 0;
0a1f7b09 1261 for (i = 0; i < 8; i++) {
36b1c8e6 1262 if (i > 0)
5fc01191 1263 last_sample = sample;
a53b8e4d 1264 sample = read_u16le_inc(&rdptr);
36b1c8e6
HE
1265
1266 /* Simple triggers. */
5fc01191 1267 if ((sample & t->simplemask) != t->simplevalue)
36b1c8e6
HE
1268 continue;
1269
1270 /* Rising edge. */
5fc01191
MV
1271 if (((last_sample & t->risingmask) != 0) ||
1272 ((sample & t->risingmask) != t->risingmask))
36b1c8e6
HE
1273 continue;
1274
1275 /* Falling edge. */
bdfc7a89 1276 if ((last_sample & t->fallingmask) != t->fallingmask ||
5fc01191 1277 (sample & t->fallingmask) != 0)
36b1c8e6
HE
1278 continue;
1279
1280 break;
1281 }
1282
1283 /* If we did not match, return original trigger pos. */
1284 return i & 0x7;
1285}
1286
98b43eb3
GS
1287static gboolean sample_matches_trigger(struct dev_context *devc, uint16_t sample)
1288{
1289 /* TODO
1290 * Check whether the combination of this very sample and the
1291 * previous state match the configured trigger condition. This
1292 * improves the resolution of the trigger marker's position.
1293 * The hardware provided position is coarse, and may point to
1294 * a position before the actual match.
1295 *
1296 * See the previous get_trigger_offset() implementation. This
1297 * code needs to get re-used here.
1298 */
1299 (void)devc;
1300 (void)sample;
1301 (void)get_trigger_offset;
1302
1303 return FALSE;
1304}
1305
1306static int check_and_submit_sample(struct dev_context *devc,
1307 uint16_t sample, size_t count, gboolean check_trigger)
1308{
1309 gboolean triggered;
1310 int ret;
1311
1312 triggered = check_trigger && sample_matches_trigger(devc, sample);
1313 if (triggered) {
1314 ret = flush_submit_buffer(devc);
1315 if (ret != SR_OK)
1316 return ret;
1317 ret = std_session_send_df_trigger(devc->buffer->sdi);
1318 if (ret != SR_OK)
1319 return ret;
1320 }
1321
1322 ret = addto_submit_buffer(devc, sample, count);
1323 if (ret != SR_OK)
1324 return ret;
1325
1326 return SR_OK;
1327}
1328
3513d965
MV
1329/*
1330 * Return the timestamp of "DRAM cluster".
1331 */
1332static uint16_t sigma_dram_cluster_ts(struct sigma_dram_cluster *cluster)
1333{
2a62a9c4 1334 return read_u16le((const uint8_t *)&cluster->timestamp);
3513d965
MV
1335}
1336
0498f743
GS
1337/*
1338 * Return one 16bit data entity of a DRAM cluster at the specified index.
1339 */
1340static uint16_t sigma_dram_cluster_data(struct sigma_dram_cluster *cl, int idx)
1341{
2a62a9c4 1342 return read_u16le((const uint8_t *)&cl->samples[idx]);
0498f743
GS
1343}
1344
85c032e4
GS
1345/*
1346 * Deinterlace sample data that was retrieved at 100MHz samplerate.
1347 * One 16bit item contains two samples of 8bits each. The bits of
1348 * multiple samples are interleaved.
1349 */
1350static uint16_t sigma_deinterlace_100mhz_data(uint16_t indata, int idx)
1351{
1352 uint16_t outdata;
1353
1354 indata >>= idx;
1355 outdata = 0;
1356 outdata |= (indata >> (0 * 2 - 0)) & (1 << 0);
1357 outdata |= (indata >> (1 * 2 - 1)) & (1 << 1);
1358 outdata |= (indata >> (2 * 2 - 2)) & (1 << 2);
1359 outdata |= (indata >> (3 * 2 - 3)) & (1 << 3);
1360 outdata |= (indata >> (4 * 2 - 4)) & (1 << 4);
1361 outdata |= (indata >> (5 * 2 - 5)) & (1 << 5);
1362 outdata |= (indata >> (6 * 2 - 6)) & (1 << 6);
1363 outdata |= (indata >> (7 * 2 - 7)) & (1 << 7);
1364 return outdata;
1365}
1366
1367/*
1368 * Deinterlace sample data that was retrieved at 200MHz samplerate.
1369 * One 16bit item contains four samples of 4bits each. The bits of
1370 * multiple samples are interleaved.
1371 */
1372static uint16_t sigma_deinterlace_200mhz_data(uint16_t indata, int idx)
1373{
1374 uint16_t outdata;
1375
1376 indata >>= idx;
1377 outdata = 0;
1378 outdata |= (indata >> (0 * 4 - 0)) & (1 << 0);
1379 outdata |= (indata >> (1 * 4 - 1)) & (1 << 1);
1380 outdata |= (indata >> (2 * 4 - 2)) & (1 << 2);
1381 outdata |= (indata >> (3 * 4 - 3)) & (1 << 3);
1382 return outdata;
1383}
1384
98b43eb3
GS
1385static void sigma_decode_dram_cluster(struct dev_context *devc,
1386 struct sigma_dram_cluster *dram_cluster,
1387 size_t events_in_cluster, gboolean triggered)
23239b5c 1388{
98b43eb3 1389 struct sigma_state *ss;
85c032e4 1390 uint16_t tsdiff, ts, sample, item16;
23239b5c 1391 unsigned int i;
23239b5c 1392
98b43eb3
GS
1393 if (!devc->use_triggers || !ASIX_SIGMA_WITH_TRIGGER)
1394 triggered = FALSE;
23239b5c
MV
1395
1396 /*
468f17f2
GS
1397 * If this cluster is not adjacent to the previously received
1398 * cluster, then send the appropriate number of samples with the
1399 * previous values to the sigrok session. This "decodes RLE".
2c33b092 1400 *
98b43eb3
GS
1401 * These samples cannot match the trigger since they just repeat
1402 * the previously submitted data pattern. (This assumption holds
1403 * for simple level and edge triggers. It would not for timed or
1404 * counted conditions, which currently are not supported.)
23239b5c 1405 */
98b43eb3
GS
1406 ss = &devc->state;
1407 ts = sigma_dram_cluster_ts(dram_cluster);
1408 tsdiff = ts - ss->lastts;
1409 if (tsdiff > 0) {
1410 size_t count;
9b4d261f 1411 sample = ss->lastsample;
98b43eb3 1412 count = tsdiff * devc->samples_per_event;
9b4d261f 1413 (void)check_and_submit_sample(devc, sample, count, FALSE);
23239b5c 1414 }
98b43eb3 1415 ss->lastts = ts + EVENTS_PER_CLUSTER;
23239b5c
MV
1416
1417 /*
98b43eb3
GS
1418 * Grab sample data from the current cluster and prepare their
1419 * submission to the session feed. Handle samplerate dependent
1420 * memory layout of sample data. Accumulation of data chunks
1421 * before submission is transparent to this code path, specific
1422 * buffer depth is neither assumed nor required here.
23239b5c 1423 */
0498f743 1424 sample = 0;
23239b5c 1425 for (i = 0; i < events_in_cluster; i++) {
85c032e4 1426 item16 = sigma_dram_cluster_data(dram_cluster, i);
5e78a564 1427 if (devc->samplerate == SR_MHZ(200)) {
85c032e4 1428 sample = sigma_deinterlace_200mhz_data(item16, 0);
98b43eb3 1429 check_and_submit_sample(devc, sample, 1, triggered);
85c032e4 1430 sample = sigma_deinterlace_200mhz_data(item16, 1);
98b43eb3 1431 check_and_submit_sample(devc, sample, 1, triggered);
85c032e4 1432 sample = sigma_deinterlace_200mhz_data(item16, 2);
98b43eb3 1433 check_and_submit_sample(devc, sample, 1, triggered);
85c032e4 1434 sample = sigma_deinterlace_200mhz_data(item16, 3);
98b43eb3 1435 check_and_submit_sample(devc, sample, 1, triggered);
5e78a564 1436 } else if (devc->samplerate == SR_MHZ(100)) {
85c032e4 1437 sample = sigma_deinterlace_100mhz_data(item16, 0);
98b43eb3 1438 check_and_submit_sample(devc, sample, 1, triggered);
85c032e4 1439 sample = sigma_deinterlace_100mhz_data(item16, 1);
98b43eb3 1440 check_and_submit_sample(devc, sample, 1, triggered);
85c032e4
GS
1441 } else {
1442 sample = item16;
98b43eb3 1443 check_and_submit_sample(devc, sample, 1, triggered);
23239b5c 1444 }
23239b5c 1445 }
0498f743 1446 ss->lastsample = sample;
23239b5c
MV
1447}
1448
28a35d8a 1449/*
fefa1800
UH
1450 * Decode chunk of 1024 bytes, 64 clusters, 7 events per cluster.
1451 * Each event is 20ns apart, and can contain multiple samples.
f78898e9
HE
1452 *
1453 * For 200 MHz, events contain 4 samples for each channel, spread 5 ns apart.
1454 * For 100 MHz, events contain 2 samples for each channel, spread 10 ns apart.
1455 * For 50 MHz and below, events contain one sample for each channel,
1456 * spread 20 ns apart.
28a35d8a 1457 */
98b43eb3
GS
1458static int decode_chunk_ts(struct dev_context *devc,
1459 struct sigma_dram_line *dram_line,
1460 size_t events_in_line, size_t trigger_event)
28a35d8a 1461{
3628074d 1462 struct sigma_dram_cluster *dram_cluster;
f06fb3e9 1463 unsigned int clusters_in_line;
5fc01191 1464 unsigned int events_in_cluster;
23239b5c 1465 unsigned int i;
98b43eb3 1466 uint32_t trigger_cluster;
f06fb3e9 1467
f06fb3e9
GS
1468 clusters_in_line = events_in_line;
1469 clusters_in_line += EVENTS_PER_CLUSTER - 1;
1470 clusters_in_line /= EVENTS_PER_CLUSTER;
1471 trigger_cluster = ~0;
ee492173 1472
4ae1f451 1473 /* Check if trigger is in this chunk. */
2c33b092 1474 if (trigger_event < EVENTS_PER_ROW) {
5e78a564 1475 if (devc->samplerate <= SR_MHZ(50)) {
1e23158b
MV
1476 trigger_event -= MIN(EVENTS_PER_CLUSTER - 1,
1477 trigger_event);
1478 }
57bbf56b 1479
f3f19d11 1480 /* Find in which cluster the trigger occurred. */
1e23158b 1481 trigger_cluster = trigger_event / EVENTS_PER_CLUSTER;
ee492173 1482 }
28a35d8a 1483
5fc01191
MV
1484 /* For each full DRAM cluster. */
1485 for (i = 0; i < clusters_in_line; i++) {
3628074d 1486 dram_cluster = &dram_line->cluster[i];
5fc01191 1487
5fc01191 1488 /* The last cluster might not be full. */
23239b5c
MV
1489 if ((i == clusters_in_line - 1) &&
1490 (events_in_line % EVENTS_PER_CLUSTER)) {
5fc01191 1491 events_in_cluster = events_in_line % EVENTS_PER_CLUSTER;
23239b5c 1492 } else {
5fc01191 1493 events_in_cluster = EVENTS_PER_CLUSTER;
abda62ce 1494 }
ee492173 1495
98b43eb3
GS
1496 sigma_decode_dram_cluster(devc, dram_cluster,
1497 events_in_cluster, i == trigger_cluster);
28a35d8a
HE
1498 }
1499
e46b8fb1 1500 return SR_OK;
28a35d8a
HE
1501}
1502
6057d9fa 1503static int download_capture(struct sr_dev_inst *sdi)
28a35d8a 1504{
e15e5873 1505 const uint32_t chunks_per_read = 32;
f06fb3e9
GS
1506
1507 struct dev_context *devc;
fd830beb 1508 struct sigma_dram_line *dram_line;
462fe786 1509 uint32_t stoppos, triggerpos;
6057d9fa 1510 uint8_t modestatus;
c6648b66
MV
1511 uint32_t i;
1512 uint32_t dl_lines_total, dl_lines_curr, dl_lines_done;
74d453ab 1513 uint32_t dl_first_line, dl_line;
5c231fc4 1514 uint32_t dl_events_in_line, trigger_event;
f06fb3e9 1515 uint32_t trg_line, trg_event;
98b43eb3 1516 int ret;
f06fb3e9
GS
1517
1518 devc = sdi->priv;
c6648b66 1519
6868626b 1520 sr_info("Downloading sample data.");
dde0175d 1521 devc->state.state = SIGMA_DOWNLOAD;
6868626b 1522
22f64ed8
GS
1523 /*
1524 * Ask the hardware to stop data acquisition. Reception of the
1525 * FORCESTOP request makes the hardware "disable RLE" (store
1526 * clusters to DRAM regardless of whether pin state changes) and
1527 * raise the POSTTRIGGERED flag.
1528 */
88a5f9ea
GS
1529 modestatus = WMR_FORCESTOP | WMR_SDRAMWRITEEN;
1530 ret = sigma_set_register(devc, WRITE_MODE, modestatus);
1531 if (ret != SR_OK)
1532 return ret;
22f64ed8 1533 do {
a53b8e4d
GS
1534 ret = sigma_read_register(devc, READ_MODE,
1535 &modestatus, sizeof(modestatus));
88a5f9ea
GS
1536 if (ret != SR_OK) {
1537 sr_err("Could not poll for post-trigger state.");
f73b00b6
DT
1538 return FALSE;
1539 }
22f64ed8 1540 } while (!(modestatus & RMR_POSTTRIGGERED));
6057d9fa
MV
1541
1542 /* Set SDRAM Read Enable. */
88a5f9ea
GS
1543 ret = sigma_set_register(devc, WRITE_MODE, WMR_SDRAMREADEN);
1544 if (ret != SR_OK)
1545 return ret;
6057d9fa 1546
88a5f9ea
GS
1547 /* Get the current position. Check if trigger has fired. */
1548 ret = sigma_read_pos(devc, &stoppos, &triggerpos, &modestatus);
1549 if (ret != SR_OK) {
1550 sr_err("Could not query capture positions/state.");
f73b00b6
DT
1551 return FALSE;
1552 }
dc400817
GS
1553 trg_line = ~0;
1554 trg_event = ~0;
22f64ed8 1555 if (modestatus & RMR_TRIGGERED) {
a53b8e4d
GS
1556 trg_line = triggerpos >> ROW_SHIFT;
1557 trg_event = triggerpos & ROW_MASK;
1e23158b 1558 }
6057d9fa 1559
c6648b66 1560 /*
74d453ab
GS
1561 * Determine how many "DRAM lines" of 1024 bytes each we need to
1562 * retrieve from the Sigma hardware, so that we have a complete
1563 * set of samples. Note that the last line need not contain 64
1564 * clusters, it might be partially filled only.
1565 *
1566 * When RMR_ROUND is set, the circular buffer in DRAM has wrapped
1567 * around. Since the status of the very next line is uncertain in
2c33b092 1568 * that case, we skip it and start reading from the next line.
c6648b66 1569 */
2c33b092
GS
1570 dl_first_line = 0;
1571 dl_lines_total = (stoppos >> ROW_SHIFT) + 1;
74d453ab
GS
1572 if (modestatus & RMR_ROUND) {
1573 dl_first_line = dl_lines_total + 1;
2c33b092 1574 dl_lines_total = ROW_COUNT - 2;
74d453ab 1575 }
44081095
DT
1576 dram_line = g_try_malloc0(chunks_per_read * sizeof(*dram_line));
1577 if (!dram_line)
1578 return FALSE;
98b43eb3
GS
1579 ret = alloc_submit_buffer(sdi);
1580 if (ret != SR_OK)
1581 return FALSE;
5e78a564 1582 ret = setup_submit_limit(devc);
98b43eb3
GS
1583 if (ret != SR_OK)
1584 return FALSE;
c6648b66 1585 dl_lines_done = 0;
c6648b66
MV
1586 while (dl_lines_total > dl_lines_done) {
1587 /* We can download only up-to 32 DRAM lines in one go! */
547c4cdc 1588 dl_lines_curr = MIN(chunks_per_read, dl_lines_total - dl_lines_done);
6868626b 1589
74d453ab 1590 dl_line = dl_first_line + dl_lines_done;
2c33b092 1591 dl_line %= ROW_COUNT;
88a5f9ea
GS
1592 ret = sigma_read_dram(devc, dl_line, dl_lines_curr,
1593 (uint8_t *)dram_line);
1594 if (ret != SR_OK)
1595 return FALSE;
6868626b 1596
c6648b66
MV
1597 /* This is the first DRAM line, so find the initial timestamp. */
1598 if (dl_lines_done == 0) {
3513d965
MV
1599 devc->state.lastts =
1600 sigma_dram_cluster_ts(&dram_line[0].cluster[0]);
c6648b66 1601 devc->state.lastsample = 0;
6868626b
BV
1602 }
1603
c6648b66 1604 for (i = 0; i < dl_lines_curr; i++) {
a53b8e4d 1605 /* The last "DRAM line" need not span its full length. */
5c231fc4 1606 dl_events_in_line = EVENTS_PER_ROW;
c6648b66 1607 if (dl_lines_done + i == dl_lines_total - 1)
a53b8e4d 1608 dl_events_in_line = stoppos & ROW_MASK;
c6648b66 1609
e69ad48e 1610 /* Test if the trigger happened on this line. */
5c231fc4 1611 trigger_event = ~0;
c6648b66 1612 if (dl_lines_done + i == trg_line)
1e23158b 1613 trigger_event = trg_event;
e69ad48e 1614
98b43eb3
GS
1615 decode_chunk_ts(devc, dram_line + i,
1616 dl_events_in_line, trigger_event);
c6648b66 1617 }
6868626b 1618
c6648b66 1619 dl_lines_done += dl_lines_curr;
6868626b 1620 }
98b43eb3
GS
1621 flush_submit_buffer(devc);
1622 free_submit_buffer(devc);
dde0175d 1623 g_free(dram_line);
6868626b 1624
bee2b016 1625 std_session_send_df_end(sdi);
6057d9fa 1626
dde0175d 1627 devc->state.state = SIGMA_IDLE;
d2f7c417 1628 sr_dev_acquisition_stop(sdi);
6057d9fa
MV
1629
1630 return TRUE;
6868626b
BV
1631}
1632
d4051930 1633/*
74d453ab
GS
1634 * Periodically check the Sigma status when in CAPTURE mode. This routine
1635 * checks whether the configured sample count or sample time have passed,
1636 * and will stop acquisition and download the acquired samples.
d4051930
MV
1637 */
1638static int sigma_capture_mode(struct sr_dev_inst *sdi)
6868626b 1639{
f06fb3e9 1640 struct dev_context *devc;
28a35d8a 1641
f06fb3e9 1642 devc = sdi->priv;
5e78a564 1643 if (sr_sw_limits_check(&devc->acq_limits))
6057d9fa 1644 return download_capture(sdi);
00c86508 1645
d4051930
MV
1646 return TRUE;
1647}
28a35d8a 1648
3ba56876 1649SR_PRIV int sigma_receive_data(int fd, int revents, void *cb_data)
d4051930
MV
1650{
1651 struct sr_dev_inst *sdi;
1652 struct dev_context *devc;
88c51afe 1653
d4051930
MV
1654 (void)fd;
1655 (void)revents;
88c51afe 1656
d4051930
MV
1657 sdi = cb_data;
1658 devc = sdi->priv;
1659
1660 if (devc->state.state == SIGMA_IDLE)
1661 return TRUE;
1662
dde0175d
GS
1663 /*
1664 * When the application has requested to stop the acquisition,
1665 * then immediately start downloading sample data. Otherwise
1666 * keep checking configured limits which will terminate the
1667 * acquisition and initiate download.
1668 */
1669 if (devc->state.state == SIGMA_STOPPING)
1670 return download_capture(sdi);
d4051930
MV
1671 if (devc->state.state == SIGMA_CAPTURE)
1672 return sigma_capture_mode(sdi);
28a35d8a 1673
28a35d8a
HE
1674 return TRUE;
1675}
1676
c53d793f
HE
1677/* Build a LUT entry used by the trigger functions. */
1678static void build_lut_entry(uint16_t value, uint16_t mask, uint16_t *entry)
ee492173
HE
1679{
1680 int i, j, k, bit;
1681
ba7dd8bb 1682 /* For each quad channel. */
0a1f7b09 1683 for (i = 0; i < 4; i++) {
c53d793f 1684 entry[i] = 0xffff;
ee492173 1685
f758d074 1686 /* For each bit in LUT. */
a53b8e4d 1687 for (j = 0; j < 16; j++) {
ee492173 1688
ba7dd8bb 1689 /* For each channel in quad. */
0a1f7b09 1690 for (k = 0; k < 4; k++) {
ee492173
HE
1691 bit = 1 << (i * 4 + k);
1692
c53d793f 1693 /* Set bit in entry */
0a1f7b09
UH
1694 if ((mask & bit) && ((!(value & bit)) !=
1695 (!(j & (1 << k)))))
c53d793f 1696 entry[i] &= ~(1 << j);
ee492173 1697 }
a53b8e4d 1698 }
ee492173 1699 }
c53d793f 1700}
ee492173 1701
c53d793f
HE
1702/* Add a logical function to LUT mask. */
1703static void add_trigger_function(enum triggerop oper, enum triggerfunc func,
9b4d261f 1704 int index, int neg, uint16_t *mask)
c53d793f
HE
1705{
1706 int i, j;
1707 int x[2][2], tmp, a, b, aset, bset, rset;
1708
5c231fc4 1709 memset(x, 0, sizeof(x));
c53d793f
HE
1710
1711 /* Trigger detect condition. */
1712 switch (oper) {
1713 case OP_LEVEL:
1714 x[0][1] = 1;
1715 x[1][1] = 1;
1716 break;
1717 case OP_NOT:
1718 x[0][0] = 1;
1719 x[1][0] = 1;
1720 break;
1721 case OP_RISE:
1722 x[0][1] = 1;
1723 break;
1724 case OP_FALL:
1725 x[1][0] = 1;
1726 break;
1727 case OP_RISEFALL:
1728 x[0][1] = 1;
1729 x[1][0] = 1;
1730 break;
1731 case OP_NOTRISE:
1732 x[1][1] = 1;
1733 x[0][0] = 1;
1734 x[1][0] = 1;
1735 break;
1736 case OP_NOTFALL:
1737 x[1][1] = 1;
1738 x[0][0] = 1;
1739 x[0][1] = 1;
1740 break;
1741 case OP_NOTRISEFALL:
1742 x[1][1] = 1;
1743 x[0][0] = 1;
1744 break;
1745 }
1746
1747 /* Transpose if neg is set. */
1748 if (neg) {
0a1f7b09
UH
1749 for (i = 0; i < 2; i++) {
1750 for (j = 0; j < 2; j++) {
c53d793f 1751 tmp = x[i][j];
0a1f7b09
UH
1752 x[i][j] = x[1 - i][1 - j];
1753 x[1 - i][1 - j] = tmp;
c53d793f 1754 }
ea9cfed7 1755 }
c53d793f
HE
1756 }
1757
1758 /* Update mask with function. */
0a1f7b09 1759 for (i = 0; i < 16; i++) {
c53d793f
HE
1760 a = (i >> (2 * index + 0)) & 1;
1761 b = (i >> (2 * index + 1)) & 1;
1762
1763 aset = (*mask >> i) & 1;
1764 bset = x[b][a];
1765
382cb19f 1766 rset = 0;
c53d793f
HE
1767 if (func == FUNC_AND || func == FUNC_NAND)
1768 rset = aset & bset;
1769 else if (func == FUNC_OR || func == FUNC_NOR)
1770 rset = aset | bset;
1771 else if (func == FUNC_XOR || func == FUNC_NXOR)
1772 rset = aset ^ bset;
1773
1774 if (func == FUNC_NAND || func == FUNC_NOR || func == FUNC_NXOR)
1775 rset = !rset;
1776
1777 *mask &= ~(1 << i);
1778
1779 if (rset)
1780 *mask |= 1 << i;
1781 }
1782}
1783
1784/*
1785 * Build trigger LUTs used by 50 MHz and lower sample rates for supporting
1786 * simple pin change and state triggers. Only two transitions (rise/fall) can be
1787 * set at any time, but a full mask and value can be set (0/1).
1788 */
9b4d261f
GS
1789SR_PRIV int sigma_build_basic_trigger(struct dev_context *devc,
1790 struct triggerlut *lut)
c53d793f
HE
1791{
1792 int i,j;
5c231fc4 1793 uint16_t masks[2];
c53d793f 1794
5c231fc4
GS
1795 memset(lut, 0, sizeof(*lut));
1796 memset(&masks, 0, sizeof(masks));
c53d793f 1797
f3f19d11 1798 /* Constant for simple triggers. */
c53d793f
HE
1799 lut->m4 = 0xa000;
1800
1801 /* Value/mask trigger support. */
0e1357e8 1802 build_lut_entry(devc->trigger.simplevalue, devc->trigger.simplemask,
99965709 1803 lut->m2d);
c53d793f
HE
1804
1805 /* Rise/fall trigger support. */
0a1f7b09 1806 for (i = 0, j = 0; i < 16; i++) {
0e1357e8
BV
1807 if (devc->trigger.risingmask & (1 << i) ||
1808 devc->trigger.fallingmask & (1 << i))
c53d793f
HE
1809 masks[j++] = 1 << i;
1810 }
1811
1812 build_lut_entry(masks[0], masks[0], lut->m0d);
1813 build_lut_entry(masks[1], masks[1], lut->m1d);
1814
1815 /* Add glue logic */
1816 if (masks[0] || masks[1]) {
1817 /* Transition trigger. */
0e1357e8 1818 if (masks[0] & devc->trigger.risingmask)
c53d793f 1819 add_trigger_function(OP_RISE, FUNC_OR, 0, 0, &lut->m3);
0e1357e8 1820 if (masks[0] & devc->trigger.fallingmask)
c53d793f 1821 add_trigger_function(OP_FALL, FUNC_OR, 0, 0, &lut->m3);
0e1357e8 1822 if (masks[1] & devc->trigger.risingmask)
c53d793f 1823 add_trigger_function(OP_RISE, FUNC_OR, 1, 0, &lut->m3);
0e1357e8 1824 if (masks[1] & devc->trigger.fallingmask)
c53d793f
HE
1825 add_trigger_function(OP_FALL, FUNC_OR, 1, 0, &lut->m3);
1826 } else {
1827 /* Only value/mask trigger. */
1828 lut->m3 = 0xffff;
1829 }
ee492173 1830
c53d793f 1831 /* Triggertype: event. */
ee492173
HE
1832 lut->params.selres = 3;
1833
e46b8fb1 1834 return SR_OK;
ee492173 1835}