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