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