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