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