]>
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 | size_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 | size_t lut_addr; | |
436 | uint16_t bit; | |
437 | uint8_t m3d, m2d, m1d, m0d; | |
438 | uint8_t buf[6], *wrptr; | |
439 | uint8_t trgsel2; | |
440 | uint16_t lutreg, selreg; | |
441 | int ret; | |
442 | ||
443 | /* | |
444 | * Translate the LUT part of the trigger configuration from the | |
445 | * application's perspective to the hardware register's bitfield | |
446 | * layout. Send the LUT to the device. This configures the logic | |
447 | * which combines pin levels or edges. | |
448 | */ | |
449 | for (lut_addr = 0; lut_addr < 16; lut_addr++) { | |
450 | bit = BIT(lut_addr); | |
451 | ||
452 | /* - M4 M3S M3Q */ | |
453 | m3d = 0; | |
454 | if (lut->m4 & bit) | |
455 | m3d |= BIT(2); | |
456 | if (lut->m3s & bit) | |
457 | m3d |= BIT(1); | |
458 | if (lut->m3q & bit) | |
459 | m3d |= BIT(0); | |
460 | ||
461 | /* M2D3 M2D2 M2D1 M2D0 */ | |
462 | m2d = 0; | |
463 | if (lut->m2d[3] & bit) | |
464 | m2d |= BIT(3); | |
465 | if (lut->m2d[2] & bit) | |
466 | m2d |= BIT(2); | |
467 | if (lut->m2d[1] & bit) | |
468 | m2d |= BIT(1); | |
469 | if (lut->m2d[0] & bit) | |
470 | m2d |= BIT(0); | |
471 | ||
472 | /* M1D3 M1D2 M1D1 M1D0 */ | |
473 | m1d = 0; | |
474 | if (lut->m1d[3] & bit) | |
475 | m1d |= BIT(3); | |
476 | if (lut->m1d[2] & bit) | |
477 | m1d |= BIT(2); | |
478 | if (lut->m1d[1] & bit) | |
479 | m1d |= BIT(1); | |
480 | if (lut->m1d[0] & bit) | |
481 | m1d |= BIT(0); | |
482 | ||
483 | /* M0D3 M0D2 M0D1 M0D0 */ | |
484 | m0d = 0; | |
485 | if (lut->m0d[3] & bit) | |
486 | m0d |= BIT(3); | |
487 | if (lut->m0d[2] & bit) | |
488 | m0d |= BIT(2); | |
489 | if (lut->m0d[1] & bit) | |
490 | m0d |= BIT(1); | |
491 | if (lut->m0d[0] & bit) | |
492 | m0d |= BIT(0); | |
493 | ||
494 | /* | |
495 | * Send 16bits with M3D/M2D and M1D/M0D bit masks to the | |
496 | * TriggerSelect register, then strobe the LUT write by | |
497 | * passing A3-A0 to TriggerSelect2. Hold RESET during LUT | |
498 | * programming. | |
499 | */ | |
500 | wrptr = buf; | |
501 | lutreg = 0; | |
502 | lutreg <<= 4; lutreg |= m3d; | |
503 | lutreg <<= 4; lutreg |= m2d; | |
504 | lutreg <<= 4; lutreg |= m1d; | |
505 | lutreg <<= 4; lutreg |= m0d; | |
506 | write_u16be_inc(&wrptr, lutreg); | |
507 | ret = sigma_write_register(devc, WRITE_TRIGGER_SELECT, | |
508 | buf, wrptr - buf); | |
509 | if (ret != SR_OK) | |
510 | return ret; | |
511 | trgsel2 = TRGSEL2_RESET | TRGSEL2_LUT_WRITE | | |
512 | (lut_addr & TRGSEL2_LUT_ADDR_MASK); | |
513 | ret = sigma_set_register(devc, WRITE_TRIGGER_SELECT2, trgsel2); | |
514 | if (ret != SR_OK) | |
515 | return ret; | |
516 | } | |
517 | ||
518 | /* | |
519 | * Send the parameters. This covers counters and durations. | |
520 | */ | |
521 | wrptr = buf; | |
522 | selreg = 0; | |
523 | selreg |= (lut->params.selinc & TRGSEL_SELINC_MASK) << TRGSEL_SELINC_SHIFT; | |
524 | selreg |= (lut->params.selres & TRGSEL_SELRES_MASK) << TRGSEL_SELRES_SHIFT; | |
525 | selreg |= (lut->params.sela & TRGSEL_SELA_MASK) << TRGSEL_SELA_SHIFT; | |
526 | selreg |= (lut->params.selb & TRGSEL_SELB_MASK) << TRGSEL_SELB_SHIFT; | |
527 | selreg |= (lut->params.selc & TRGSEL_SELC_MASK) << TRGSEL_SELC_SHIFT; | |
528 | selreg |= (lut->params.selpresc & TRGSEL_SELPRESC_MASK) << TRGSEL_SELPRESC_SHIFT; | |
529 | write_u16be_inc(&wrptr, selreg); | |
530 | write_u16be_inc(&wrptr, lut->params.cmpb); | |
531 | write_u16be_inc(&wrptr, lut->params.cmpa); | |
532 | ret = sigma_write_register(devc, WRITE_TRIGGER_SELECT, buf, wrptr - buf); | |
533 | if (ret != SR_OK) | |
534 | return ret; | |
535 | ||
536 | return SR_OK; | |
537 | } | |
538 | ||
539 | /* | |
540 | * See Xilinx UG332 for Spartan-3 FPGA configuration. The SIGMA device | |
541 | * uses FTDI bitbang mode for netlist download in slave serial mode. | |
542 | * (LATER: The OMEGA device's cable contains a more capable FTDI chip | |
543 | * and uses MPSSE mode for bitbang. -- Can we also use FT232H in FT245 | |
544 | * compatible bitbang mode? For maximum code re-use and reduced libftdi | |
545 | * dependency? See section 3.5.5 of FT232H: D0 clk, D1 data (out), D2 | |
546 | * data (in), D3 select, D4-7 GPIOL. See section 3.5.7 for MCU FIFO.) | |
547 | * | |
548 | * 750kbps rate (four times the speed of sigmalogan) works well for | |
549 | * netlist download. All pins except INIT_B are output pins during | |
550 | * configuration download. | |
551 | * | |
552 | * Some pins are inverted as a byproduct of level shifting circuitry. | |
553 | * That's why high CCLK level (from the cable's point of view) is idle | |
554 | * from the FPGA's perspective. | |
555 | * | |
556 | * The vendor's literature discusses a "suicide sequence" which ends | |
557 | * regular FPGA execution and should be sent before entering bitbang | |
558 | * mode and sending configuration data. Set D7 and toggle D2, D3, D4 | |
559 | * a few times. | |
560 | */ | |
561 | #define BB_PIN_CCLK BIT(0) /* D0, CCLK */ | |
562 | #define BB_PIN_PROG BIT(1) /* D1, PROG */ | |
563 | #define BB_PIN_D2 BIT(2) /* D2, (part of) SUICIDE */ | |
564 | #define BB_PIN_D3 BIT(3) /* D3, (part of) SUICIDE */ | |
565 | #define BB_PIN_D4 BIT(4) /* D4, (part of) SUICIDE (unused?) */ | |
566 | #define BB_PIN_INIT BIT(5) /* D5, INIT, input pin */ | |
567 | #define BB_PIN_DIN BIT(6) /* D6, DIN */ | |
568 | #define BB_PIN_D7 BIT(7) /* D7, (part of) SUICIDE */ | |
569 | ||
570 | #define BB_BITRATE (750 * 1000) | |
571 | #define BB_PINMASK (0xff & ~BB_PIN_INIT) | |
572 | ||
573 | /* | |
574 | * Initiate slave serial mode for configuration download. Which is done | |
575 | * by pulsing PROG_B and sensing INIT_B. Make sure CCLK is idle before | |
576 | * initiating the configuration download. | |
577 | * | |
578 | * Run a "suicide sequence" first to terminate the regular FPGA operation | |
579 | * before reconfiguration. The FTDI cable is single channel, and shares | |
580 | * pins which are used for data communication in FIFO mode with pins that | |
581 | * are used for FPGA configuration in bitbang mode. Hardware defaults for | |
582 | * unconfigured hardware, and runtime conditions after FPGA configuration | |
583 | * need to cooperate such that re-configuration of the FPGA can start. | |
584 | */ | |
585 | static int sigma_fpga_init_bitbang_once(struct dev_context *devc) | |
586 | { | |
587 | const uint8_t suicide[] = { | |
588 | BB_PIN_D7 | BB_PIN_D2, | |
589 | BB_PIN_D7 | BB_PIN_D2, | |
590 | BB_PIN_D7 | BB_PIN_D3, | |
591 | BB_PIN_D7 | BB_PIN_D2, | |
592 | BB_PIN_D7 | BB_PIN_D3, | |
593 | BB_PIN_D7 | BB_PIN_D2, | |
594 | BB_PIN_D7 | BB_PIN_D3, | |
595 | BB_PIN_D7 | BB_PIN_D2, | |
596 | }; | |
597 | const uint8_t init_array[] = { | |
598 | BB_PIN_CCLK, | |
599 | BB_PIN_CCLK | BB_PIN_PROG, | |
600 | BB_PIN_CCLK | BB_PIN_PROG, | |
601 | BB_PIN_CCLK, | |
602 | BB_PIN_CCLK, | |
603 | BB_PIN_CCLK, | |
604 | BB_PIN_CCLK, | |
605 | BB_PIN_CCLK, | |
606 | BB_PIN_CCLK, | |
607 | BB_PIN_CCLK, | |
608 | }; | |
609 | size_t retries; | |
610 | int ret; | |
611 | uint8_t data; | |
612 | ||
613 | /* Section 2. part 1), do the FPGA suicide. */ | |
614 | ret = SR_OK; | |
615 | ret |= sigma_write_sr(devc, suicide, sizeof(suicide)); | |
616 | ret |= sigma_write_sr(devc, suicide, sizeof(suicide)); | |
617 | ret |= sigma_write_sr(devc, suicide, sizeof(suicide)); | |
618 | ret |= sigma_write_sr(devc, suicide, sizeof(suicide)); | |
619 | if (ret != SR_OK) | |
620 | return SR_ERR_IO; | |
621 | g_usleep(10 * 1000); | |
622 | ||
623 | /* Section 2. part 2), pulse PROG. */ | |
624 | ret = sigma_write_sr(devc, init_array, sizeof(init_array)); | |
625 | if (ret != SR_OK) | |
626 | return ret; | |
627 | g_usleep(10 * 1000); | |
628 | ftdi_usb_purge_buffers(&devc->ftdi.ctx); | |
629 | ||
630 | /* | |
631 | * Wait until the FPGA asserts INIT_B. Check in a maximum number | |
632 | * of bursts with a given delay between them. Read as many pin | |
633 | * capture results as the combination of FTDI chip and FTID lib | |
634 | * may provide. Cope with absence of pin capture data in a cycle. | |
635 | * This approach shall result in fast reponse in case of success, | |
636 | * low cost of execution during wait, reliable error handling in | |
637 | * the transport layer, and robust response to failure or absence | |
638 | * of result data (hardware inactivity after stimulus). | |
639 | */ | |
640 | retries = 10; | |
641 | while (retries--) { | |
642 | do { | |
643 | ret = sigma_read_raw(devc, &data, sizeof(data)); | |
644 | if (ret < 0) | |
645 | return SR_ERR_IO; | |
646 | if (ret == sizeof(data) && (data & BB_PIN_INIT)) | |
647 | return SR_OK; | |
648 | } while (ret == sizeof(data)); | |
649 | if (retries) | |
650 | g_usleep(10 * 1000); | |
651 | } | |
652 | ||
653 | return SR_ERR_TIMEOUT; | |
654 | } | |
655 | ||
656 | /* | |
657 | * This is belt and braces. Re-run the bitbang initiation sequence a few | |
658 | * times should first attempts fail. Failure is rare but can happen (was | |
659 | * observed during driver development). | |
660 | */ | |
661 | static int sigma_fpga_init_bitbang(struct dev_context *devc) | |
662 | { | |
663 | size_t retries; | |
664 | int ret; | |
665 | ||
666 | retries = 10; | |
667 | while (retries--) { | |
668 | ret = sigma_fpga_init_bitbang_once(devc); | |
669 | if (ret == SR_OK) | |
670 | return ret; | |
671 | if (ret != SR_ERR_TIMEOUT) | |
672 | return ret; | |
673 | } | |
674 | return ret; | |
675 | } | |
676 | ||
677 | /* | |
678 | * Configure the FPGA for logic-analyzer mode. | |
679 | */ | |
680 | static int sigma_fpga_init_la(struct dev_context *devc) | |
681 | { | |
682 | uint8_t buf[20], *wrptr; | |
683 | uint8_t data_55, data_aa, mode; | |
684 | uint8_t result[3]; | |
685 | const uint8_t *rdptr; | |
686 | int ret; | |
687 | ||
688 | wrptr = buf; | |
689 | ||
690 | /* Read ID register. */ | |
691 | write_u8_inc(&wrptr, REG_ADDR_LOW | LO4(READ_ID)); | |
692 | write_u8_inc(&wrptr, REG_ADDR_HIGH | HI4(READ_ID)); | |
693 | write_u8_inc(&wrptr, REG_READ_ADDR); | |
694 | ||
695 | /* Write 0x55 to scratch register, read back. */ | |
696 | data_55 = 0x55; | |
697 | write_u8_inc(&wrptr, REG_ADDR_LOW | LO4(WRITE_TEST)); | |
698 | write_u8_inc(&wrptr, REG_ADDR_HIGH | HI4(WRITE_TEST)); | |
699 | write_u8_inc(&wrptr, REG_DATA_LOW | LO4(data_55)); | |
700 | write_u8_inc(&wrptr, REG_DATA_HIGH_WRITE | HI4(data_55)); | |
701 | write_u8_inc(&wrptr, REG_READ_ADDR); | |
702 | ||
703 | /* Write 0xaa to scratch register, read back. */ | |
704 | data_aa = 0xaa; | |
705 | write_u8_inc(&wrptr, REG_ADDR_LOW | LO4(WRITE_TEST)); | |
706 | write_u8_inc(&wrptr, REG_ADDR_HIGH | HI4(WRITE_TEST)); | |
707 | write_u8_inc(&wrptr, REG_DATA_LOW | LO4(data_aa)); | |
708 | write_u8_inc(&wrptr, REG_DATA_HIGH_WRITE | HI4(data_aa)); | |
709 | write_u8_inc(&wrptr, REG_READ_ADDR); | |
710 | ||
711 | /* Initiate SDRAM initialization in mode register. */ | |
712 | mode = WMR_SDRAMINIT; | |
713 | write_u8_inc(&wrptr, REG_ADDR_LOW | LO4(WRITE_MODE)); | |
714 | write_u8_inc(&wrptr, REG_ADDR_HIGH | HI4(WRITE_MODE)); | |
715 | write_u8_inc(&wrptr, REG_DATA_LOW | LO4(mode)); | |
716 | write_u8_inc(&wrptr, REG_DATA_HIGH_WRITE | HI4(mode)); | |
717 | ||
718 | /* | |
719 | * Send the command sequence which contains 3 READ requests. | |
720 | * Expect to see the corresponding 3 response bytes. | |
721 | */ | |
722 | ret = sigma_write_sr(devc, buf, wrptr - buf); | |
723 | if (ret != SR_OK) { | |
724 | sr_err("Could not request LA start response."); | |
725 | return ret; | |
726 | } | |
727 | ret = sigma_read_sr(devc, result, ARRAY_SIZE(result)); | |
728 | if (ret != SR_OK) { | |
729 | sr_err("Could not receive LA start response."); | |
730 | return SR_ERR_IO; | |
731 | } | |
732 | rdptr = result; | |
733 | if (read_u8_inc(&rdptr) != 0xa6) { | |
734 | sr_err("Unexpected ID response."); | |
735 | return SR_ERR_DATA; | |
736 | } | |
737 | if (read_u8_inc(&rdptr) != data_55) { | |
738 | sr_err("Unexpected scratch read-back (55)."); | |
739 | return SR_ERR_DATA; | |
740 | } | |
741 | if (read_u8_inc(&rdptr) != data_aa) { | |
742 | sr_err("Unexpected scratch read-back (aa)."); | |
743 | return SR_ERR_DATA; | |
744 | } | |
745 | ||
746 | return SR_OK; | |
747 | } | |
748 | ||
749 | /* | |
750 | * Read the firmware from a file and transform it into a series of bitbang | |
751 | * pulses used to program the FPGA. Note that the *bb_cmd must be free()'d | |
752 | * by the caller of this function. | |
753 | */ | |
754 | static int sigma_fw_2_bitbang(struct sr_context *ctx, const char *name, | |
755 | uint8_t **bb_cmd, size_t *bb_cmd_size) | |
756 | { | |
757 | uint8_t *firmware; | |
758 | size_t file_size; | |
759 | uint8_t *p; | |
760 | size_t l; | |
761 | uint32_t imm; | |
762 | size_t bb_size; | |
763 | uint8_t *bb_stream, *bbs, byte, mask, v; | |
764 | ||
765 | /* Retrieve the on-disk firmware file content. */ | |
766 | firmware = sr_resource_load(ctx, SR_RESOURCE_FIRMWARE, name, | |
767 | &file_size, SIGMA_FIRMWARE_SIZE_LIMIT); | |
768 | if (!firmware) | |
769 | return SR_ERR_IO; | |
770 | ||
771 | /* Unscramble the file content (XOR with "random" sequence). */ | |
772 | p = firmware; | |
773 | l = file_size; | |
774 | imm = 0x3f6df2ab; | |
775 | while (l--) { | |
776 | imm = (imm + 0xa853753) % 177 + (imm * 0x8034052); | |
777 | *p++ ^= imm & 0xff; | |
778 | } | |
779 | ||
780 | /* | |
781 | * Generate a sequence of bitbang samples. With two samples per | |
782 | * FPGA configuration bit, providing the level for the DIN signal | |
783 | * as well as two edges for CCLK. See Xilinx UG332 for details | |
784 | * ("slave serial" mode). | |
785 | * | |
786 | * Note that CCLK is inverted in hardware. That's why the | |
787 | * respective bit is first set and then cleared in the bitbang | |
788 | * sample sets. So that the DIN level will be stable when the | |
789 | * data gets sampled at the rising CCLK edge, and the signals' | |
790 | * setup time constraint will be met. | |
791 | * | |
792 | * The caller will put the FPGA into download mode, will send | |
793 | * the bitbang samples, and release the allocated memory. | |
794 | */ | |
795 | bb_size = file_size * 8 * 2; | |
796 | bb_stream = g_try_malloc(bb_size); | |
797 | if (!bb_stream) { | |
798 | sr_err("Memory allocation failed during firmware upload."); | |
799 | g_free(firmware); | |
800 | return SR_ERR_MALLOC; | |
801 | } | |
802 | bbs = bb_stream; | |
803 | p = firmware; | |
804 | l = file_size; | |
805 | while (l--) { | |
806 | byte = *p++; | |
807 | mask = 0x80; | |
808 | while (mask) { | |
809 | v = (byte & mask) ? BB_PIN_DIN : 0; | |
810 | mask >>= 1; | |
811 | *bbs++ = v | BB_PIN_CCLK; | |
812 | *bbs++ = v; | |
813 | } | |
814 | } | |
815 | g_free(firmware); | |
816 | ||
817 | /* The transformation completed successfully, return the result. */ | |
818 | *bb_cmd = bb_stream; | |
819 | *bb_cmd_size = bb_size; | |
820 | ||
821 | return SR_OK; | |
822 | } | |
823 | ||
824 | static int upload_firmware(struct sr_context *ctx, struct dev_context *devc, | |
825 | enum sigma_firmware_idx firmware_idx) | |
826 | { | |
827 | int ret; | |
828 | uint8_t *buf; | |
829 | uint8_t pins; | |
830 | size_t buf_size; | |
831 | const char *firmware; | |
832 | ||
833 | /* Check for valid firmware file selection. */ | |
834 | if (firmware_idx >= ARRAY_SIZE(firmware_files)) | |
835 | return SR_ERR_ARG; | |
836 | firmware = firmware_files[firmware_idx]; | |
837 | if (!firmware || !*firmware) | |
838 | return SR_ERR_ARG; | |
839 | ||
840 | /* Avoid downloading the same firmware multiple times. */ | |
841 | if (devc->firmware_idx == firmware_idx) { | |
842 | sr_info("Not uploading firmware file '%s' again.", firmware); | |
843 | return SR_OK; | |
844 | } | |
845 | ||
846 | devc->state = SIGMA_CONFIG; | |
847 | ||
848 | /* Set the cable to bitbang mode. */ | |
849 | ret = ftdi_set_bitmode(&devc->ftdi.ctx, BB_PINMASK, BITMODE_BITBANG); | |
850 | if (ret < 0) { | |
851 | sr_err("Could not setup cable mode for upload: %s", | |
852 | ftdi_get_error_string(&devc->ftdi.ctx)); | |
853 | return SR_ERR; | |
854 | } | |
855 | ret = ftdi_set_baudrate(&devc->ftdi.ctx, BB_BITRATE); | |
856 | if (ret < 0) { | |
857 | sr_err("Could not setup bitrate for upload: %s", | |
858 | ftdi_get_error_string(&devc->ftdi.ctx)); | |
859 | return SR_ERR; | |
860 | } | |
861 | ||
862 | /* Initiate FPGA configuration mode. */ | |
863 | ret = sigma_fpga_init_bitbang(devc); | |
864 | if (ret) { | |
865 | sr_err("Could not initiate firmware upload to hardware"); | |
866 | return ret; | |
867 | } | |
868 | ||
869 | /* Prepare wire format of the firmware image. */ | |
870 | ret = sigma_fw_2_bitbang(ctx, firmware, &buf, &buf_size); | |
871 | if (ret != SR_OK) { | |
872 | sr_err("Could not prepare file %s for upload.", firmware); | |
873 | return ret; | |
874 | } | |
875 | ||
876 | /* Write the FPGA netlist to the cable. */ | |
877 | sr_info("Uploading firmware file '%s'.", firmware); | |
878 | ret = sigma_write_sr(devc, buf, buf_size); | |
879 | g_free(buf); | |
880 | if (ret != SR_OK) { | |
881 | sr_err("Could not upload firmware file '%s'.", firmware); | |
882 | return ret; | |
883 | } | |
884 | ||
885 | /* Leave bitbang mode and discard pending input data. */ | |
886 | ret = ftdi_set_bitmode(&devc->ftdi.ctx, 0, BITMODE_RESET); | |
887 | if (ret < 0) { | |
888 | sr_err("Could not setup cable mode after upload: %s", | |
889 | ftdi_get_error_string(&devc->ftdi.ctx)); | |
890 | return SR_ERR; | |
891 | } | |
892 | ftdi_usb_purge_buffers(&devc->ftdi.ctx); | |
893 | while (sigma_read_raw(devc, &pins, sizeof(pins)) > 0) | |
894 | ; | |
895 | ||
896 | /* Initialize the FPGA for logic-analyzer mode. */ | |
897 | ret = sigma_fpga_init_la(devc); | |
898 | if (ret != SR_OK) { | |
899 | sr_err("Hardware response after firmware upload failed."); | |
900 | return ret; | |
901 | } | |
902 | ||
903 | /* Keep track of successful firmware download completion. */ | |
904 | devc->state = SIGMA_IDLE; | |
905 | devc->firmware_idx = firmware_idx; | |
906 | sr_info("Firmware uploaded."); | |
907 | ||
908 | return SR_OK; | |
909 | } | |
910 | ||
911 | /* | |
912 | * The driver supports user specified time or sample count limits. The | |
913 | * device's hardware supports neither, and hardware compression prevents | |
914 | * reliable detection of "fill levels" (currently reached sample counts) | |
915 | * from register values during acquisition. That's why the driver needs | |
916 | * to apply some heuristics: | |
917 | * | |
918 | * - The (optional) sample count limit and the (normalized) samplerate | |
919 | * get mapped to an estimated duration for these samples' acquisition. | |
920 | * - The (optional) time limit gets checked as well. The lesser of the | |
921 | * two limits will terminate the data acquisition phase. The exact | |
922 | * sample count limit gets enforced in session feed submission paths. | |
923 | * - Some slack needs to be given to account for hardware pipelines as | |
924 | * well as late storage of last chunks after compression thresholds | |
925 | * are tripped. The resulting data set will span at least the caller | |
926 | * specified period of time, which shall be perfectly acceptable. | |
927 | * | |
928 | * With RLE compression active, up to 64K sample periods can pass before | |
929 | * a cluster accumulates. Which translates to 327ms at 200kHz. Add two | |
930 | * times that period for good measure, one is not enough to flush the | |
931 | * hardware pipeline (observation from an earlier experiment). | |
932 | */ | |
933 | SR_PRIV int sigma_set_acquire_timeout(struct dev_context *devc) | |
934 | { | |
935 | int ret; | |
936 | GVariant *data; | |
937 | uint64_t user_count, user_msecs; | |
938 | uint64_t worst_cluster_time_ms; | |
939 | uint64_t count_msecs, acquire_msecs; | |
940 | ||
941 | sr_sw_limits_init(&devc->limit.acquire); | |
942 | ||
943 | /* Get sample count limit, convert to msecs. */ | |
944 | ret = sr_sw_limits_config_get(&devc->limit.config, | |
945 | SR_CONF_LIMIT_SAMPLES, &data); | |
946 | if (ret != SR_OK) | |
947 | return ret; | |
948 | user_count = g_variant_get_uint64(data); | |
949 | g_variant_unref(data); | |
950 | count_msecs = 0; | |
951 | if (user_count) | |
952 | count_msecs = 1000 * user_count / devc->clock.samplerate + 1; | |
953 | ||
954 | /* Get time limit, which is in msecs. */ | |
955 | ret = sr_sw_limits_config_get(&devc->limit.config, | |
956 | SR_CONF_LIMIT_MSEC, &data); | |
957 | if (ret != SR_OK) | |
958 | return ret; | |
959 | user_msecs = g_variant_get_uint64(data); | |
960 | g_variant_unref(data); | |
961 | ||
962 | /* Get the lesser of them, with both being optional. */ | |
963 | acquire_msecs = ~0ull; | |
964 | if (user_count && count_msecs < acquire_msecs) | |
965 | acquire_msecs = count_msecs; | |
966 | if (user_msecs && user_msecs < acquire_msecs) | |
967 | acquire_msecs = user_msecs; | |
968 | if (acquire_msecs == ~0ull) | |
969 | return SR_OK; | |
970 | ||
971 | /* Add some slack, and use that timeout for acquisition. */ | |
972 | worst_cluster_time_ms = 1000 * 65536 / devc->clock.samplerate; | |
973 | acquire_msecs += 2 * worst_cluster_time_ms; | |
974 | data = g_variant_new_uint64(acquire_msecs); | |
975 | ret = sr_sw_limits_config_set(&devc->limit.acquire, | |
976 | SR_CONF_LIMIT_MSEC, data); | |
977 | g_variant_unref(data); | |
978 | if (ret != SR_OK) | |
979 | return ret; | |
980 | ||
981 | sr_sw_limits_acquisition_start(&devc->limit.acquire); | |
982 | return SR_OK; | |
983 | } | |
984 | ||
985 | /* | |
986 | * Check whether a caller specified samplerate matches the device's | |
987 | * hardware constraints (can be used for acquisition). Optionally yield | |
988 | * a value that approximates the original spec. | |
989 | * | |
990 | * This routine assumes that input specs are in the 200kHz to 200MHz | |
991 | * range of supported rates, and callers typically want to normalize a | |
992 | * given value to the hardware capabilities. Values in the 50MHz range | |
993 | * get rounded up by default, to avoid a more expensive check for the | |
994 | * closest match, while higher sampling rate is always desirable during | |
995 | * measurement. Input specs which exactly match hardware capabilities | |
996 | * remain unaffected. Because 100/200MHz rates also limit the number of | |
997 | * available channels, they are not suggested by this routine, instead | |
998 | * callers need to pick them consciously. | |
999 | */ | |
1000 | SR_PRIV int sigma_normalize_samplerate(uint64_t want_rate, uint64_t *have_rate) | |
1001 | { | |
1002 | uint64_t div, rate; | |
1003 | ||
1004 | /* Accept exact matches for 100/200MHz. */ | |
1005 | if (want_rate == SR_MHZ(200) || want_rate == SR_MHZ(100)) { | |
1006 | if (have_rate) | |
1007 | *have_rate = want_rate; | |
1008 | return SR_OK; | |
1009 | } | |
1010 | ||
1011 | /* Accept 200kHz to 50MHz range, and map to near value. */ | |
1012 | if (want_rate >= SR_KHZ(200) && want_rate <= SR_MHZ(50)) { | |
1013 | div = SR_MHZ(50) / want_rate; | |
1014 | rate = SR_MHZ(50) / div; | |
1015 | if (have_rate) | |
1016 | *have_rate = rate; | |
1017 | return SR_OK; | |
1018 | } | |
1019 | ||
1020 | return SR_ERR_ARG; | |
1021 | } | |
1022 | ||
1023 | /* Gets called at probe time. Can seed software settings from hardware state. */ | |
1024 | SR_PRIV int sigma_fetch_hw_config(const struct sr_dev_inst *sdi) | |
1025 | { | |
1026 | struct dev_context *devc; | |
1027 | int ret; | |
1028 | uint8_t regaddr, regval; | |
1029 | ||
1030 | devc = sdi->priv; | |
1031 | if (!devc) | |
1032 | return SR_ERR_ARG; | |
1033 | ||
1034 | /* Seed configuration values from defaults. */ | |
1035 | devc->firmware_idx = SIGMA_FW_NONE; | |
1036 | devc->clock.samplerate = samplerates[0]; | |
1037 | ||
1038 | /* TODO | |
1039 | * Ideally the device driver could retrieve recently stored | |
1040 | * details from hardware registers, thus re-use user specified | |
1041 | * configuration values across sigrok sessions. Which could | |
1042 | * avoid repeated expensive though unnecessary firmware uploads, | |
1043 | * improve performance and usability. Unfortunately it appears | |
1044 | * that the registers range which is documented as available for | |
1045 | * application use keeps providing 0xff data content. At least | |
1046 | * with the netlist version which ships with sigrok. The same | |
1047 | * was observed with unused registers in the first page. | |
1048 | */ | |
1049 | return SR_ERR_NA; | |
1050 | ||
1051 | /* This is for research, currently does not work yet. */ | |
1052 | ret = sigma_check_open(sdi); | |
1053 | regaddr = 16; | |
1054 | regaddr = 14; | |
1055 | ret = sigma_set_register(devc, regaddr, 'F'); | |
1056 | ret = sigma_get_register(devc, regaddr, ®val); | |
1057 | sr_warn("%s() reg[%u] val[%u] rc[%d]", __func__, regaddr, regval, ret); | |
1058 | ret = sigma_check_close(devc); | |
1059 | return ret; | |
1060 | } | |
1061 | ||
1062 | /* Gets called after successful (volatile) hardware configuration. */ | |
1063 | SR_PRIV int sigma_store_hw_config(const struct sr_dev_inst *sdi) | |
1064 | { | |
1065 | /* TODO See above, registers seem to not hold written data. */ | |
1066 | (void)sdi; | |
1067 | return SR_ERR_NA; | |
1068 | } | |
1069 | ||
1070 | SR_PRIV int sigma_set_samplerate(const struct sr_dev_inst *sdi) | |
1071 | { | |
1072 | struct dev_context *devc; | |
1073 | struct drv_context *drvc; | |
1074 | uint64_t samplerate; | |
1075 | int ret; | |
1076 | size_t num_channels; | |
1077 | ||
1078 | devc = sdi->priv; | |
1079 | drvc = sdi->driver->context; | |
1080 | ||
1081 | /* Accept any caller specified rate which the hardware supports. */ | |
1082 | ret = sigma_normalize_samplerate(devc->clock.samplerate, &samplerate); | |
1083 | if (ret != SR_OK) | |
1084 | return ret; | |
1085 | ||
1086 | /* | |
1087 | * Depending on the samplerates of 200/100/50- MHz, specific | |
1088 | * firmware is required and higher rates might limit the set | |
1089 | * of available channels. | |
1090 | */ | |
1091 | num_channels = devc->interp.num_channels; | |
1092 | if (samplerate <= SR_MHZ(50)) { | |
1093 | ret = upload_firmware(drvc->sr_ctx, devc, SIGMA_FW_50MHZ); | |
1094 | num_channels = 16; | |
1095 | } else if (samplerate == SR_MHZ(100)) { | |
1096 | ret = upload_firmware(drvc->sr_ctx, devc, SIGMA_FW_100MHZ); | |
1097 | num_channels = 8; | |
1098 | } else if (samplerate == SR_MHZ(200)) { | |
1099 | ret = upload_firmware(drvc->sr_ctx, devc, SIGMA_FW_200MHZ); | |
1100 | num_channels = 4; | |
1101 | } | |
1102 | ||
1103 | /* | |
1104 | * The samplerate affects the number of available logic channels | |
1105 | * as well as a sample memory layout detail (the number of samples | |
1106 | * which the device will communicate within an "event"). | |
1107 | */ | |
1108 | if (ret == SR_OK) { | |
1109 | devc->interp.num_channels = num_channels; | |
1110 | devc->interp.samples_per_event = 16 / devc->interp.num_channels; | |
1111 | } | |
1112 | ||
1113 | /* | |
1114 | * Store the firmware type and most recently configured samplerate | |
1115 | * in hardware, such that subsequent sessions can start from there. | |
1116 | * This is a "best effort" approach. Failure is non-fatal. | |
1117 | */ | |
1118 | if (ret == SR_OK) | |
1119 | (void)sigma_store_hw_config(sdi); | |
1120 | ||
1121 | return ret; | |
1122 | } | |
1123 | ||
1124 | /* | |
1125 | * Arrange for a session feed submit buffer. A queue where a number of | |
1126 | * samples gets accumulated to reduce the number of send calls. Which | |
1127 | * also enforces an optional sample count limit for data acquisition. | |
1128 | * | |
1129 | * The buffer holds up to CHUNK_SIZE bytes. The unit size is fixed (the | |
1130 | * driver provides a fixed channel layout regardless of samplerate). | |
1131 | */ | |
1132 | ||
1133 | #define CHUNK_SIZE (4 * 1024 * 1024) | |
1134 | ||
1135 | struct submit_buffer { | |
1136 | size_t unit_size; | |
1137 | size_t max_samples, curr_samples; | |
1138 | uint8_t *sample_data; | |
1139 | uint8_t *write_pointer; | |
1140 | struct sr_dev_inst *sdi; | |
1141 | struct sr_datafeed_packet packet; | |
1142 | struct sr_datafeed_logic logic; | |
1143 | }; | |
1144 | ||
1145 | static int alloc_submit_buffer(struct sr_dev_inst *sdi) | |
1146 | { | |
1147 | struct dev_context *devc; | |
1148 | struct submit_buffer *buffer; | |
1149 | size_t size; | |
1150 | ||
1151 | devc = sdi->priv; | |
1152 | ||
1153 | buffer = g_malloc0(sizeof(*buffer)); | |
1154 | devc->buffer = buffer; | |
1155 | ||
1156 | buffer->unit_size = sizeof(uint16_t); | |
1157 | size = CHUNK_SIZE; | |
1158 | size /= buffer->unit_size; | |
1159 | buffer->max_samples = size; | |
1160 | size *= buffer->unit_size; | |
1161 | buffer->sample_data = g_try_malloc0(size); | |
1162 | if (!buffer->sample_data) | |
1163 | return SR_ERR_MALLOC; | |
1164 | buffer->write_pointer = buffer->sample_data; | |
1165 | sr_sw_limits_init(&devc->limit.submit); | |
1166 | ||
1167 | buffer->sdi = sdi; | |
1168 | memset(&buffer->logic, 0, sizeof(buffer->logic)); | |
1169 | buffer->logic.unitsize = buffer->unit_size; | |
1170 | buffer->logic.data = buffer->sample_data; | |
1171 | memset(&buffer->packet, 0, sizeof(buffer->packet)); | |
1172 | buffer->packet.type = SR_DF_LOGIC; | |
1173 | buffer->packet.payload = &buffer->logic; | |
1174 | ||
1175 | return SR_OK; | |
1176 | } | |
1177 | ||
1178 | static int setup_submit_limit(struct dev_context *devc) | |
1179 | { | |
1180 | struct sr_sw_limits *limits; | |
1181 | int ret; | |
1182 | GVariant *data; | |
1183 | uint64_t total; | |
1184 | ||
1185 | limits = &devc->limit.submit; | |
1186 | ||
1187 | ret = sr_sw_limits_config_get(&devc->limit.config, | |
1188 | SR_CONF_LIMIT_SAMPLES, &data); | |
1189 | if (ret != SR_OK) | |
1190 | return ret; | |
1191 | total = g_variant_get_uint64(data); | |
1192 | g_variant_unref(data); | |
1193 | ||
1194 | sr_sw_limits_init(limits); | |
1195 | if (total) { | |
1196 | data = g_variant_new_uint64(total); | |
1197 | ret = sr_sw_limits_config_set(limits, | |
1198 | SR_CONF_LIMIT_SAMPLES, data); | |
1199 | g_variant_unref(data); | |
1200 | if (ret != SR_OK) | |
1201 | return ret; | |
1202 | } | |
1203 | ||
1204 | sr_sw_limits_acquisition_start(limits); | |
1205 | ||
1206 | return SR_OK; | |
1207 | } | |
1208 | ||
1209 | static void free_submit_buffer(struct dev_context *devc) | |
1210 | { | |
1211 | struct submit_buffer *buffer; | |
1212 | ||
1213 | if (!devc) | |
1214 | return; | |
1215 | ||
1216 | buffer = devc->buffer; | |
1217 | if (!buffer) | |
1218 | return; | |
1219 | devc->buffer = NULL; | |
1220 | ||
1221 | g_free(buffer->sample_data); | |
1222 | g_free(buffer); | |
1223 | } | |
1224 | ||
1225 | static int flush_submit_buffer(struct dev_context *devc) | |
1226 | { | |
1227 | struct submit_buffer *buffer; | |
1228 | int ret; | |
1229 | ||
1230 | buffer = devc->buffer; | |
1231 | ||
1232 | /* Is queued sample data available? */ | |
1233 | if (!buffer->curr_samples) | |
1234 | return SR_OK; | |
1235 | ||
1236 | /* Submit to the session feed. */ | |
1237 | buffer->logic.length = buffer->curr_samples * buffer->unit_size; | |
1238 | ret = sr_session_send(buffer->sdi, &buffer->packet); | |
1239 | if (ret != SR_OK) | |
1240 | return ret; | |
1241 | ||
1242 | /* Rewind queue position. */ | |
1243 | buffer->curr_samples = 0; | |
1244 | buffer->write_pointer = buffer->sample_data; | |
1245 | ||
1246 | return SR_OK; | |
1247 | } | |
1248 | ||
1249 | static int addto_submit_buffer(struct dev_context *devc, | |
1250 | uint16_t sample, size_t count) | |
1251 | { | |
1252 | struct submit_buffer *buffer; | |
1253 | struct sr_sw_limits *limits; | |
1254 | int ret; | |
1255 | ||
1256 | buffer = devc->buffer; | |
1257 | limits = &devc->limit.submit; | |
1258 | if (!devc->use_triggers && sr_sw_limits_check(limits)) | |
1259 | count = 0; | |
1260 | ||
1261 | /* | |
1262 | * Individually accumulate and check each sample, such that | |
1263 | * accumulation between flushes won't exceed local storage, and | |
1264 | * enforcement of user specified limits is exact. | |
1265 | */ | |
1266 | while (count--) { | |
1267 | write_u16le_inc(&buffer->write_pointer, sample); | |
1268 | buffer->curr_samples++; | |
1269 | if (buffer->curr_samples == buffer->max_samples) { | |
1270 | ret = flush_submit_buffer(devc); | |
1271 | if (ret != SR_OK) | |
1272 | return ret; | |
1273 | } | |
1274 | sr_sw_limits_update_samples_read(limits, 1); | |
1275 | if (!devc->use_triggers && sr_sw_limits_check(limits)) | |
1276 | break; | |
1277 | } | |
1278 | ||
1279 | return SR_OK; | |
1280 | } | |
1281 | ||
1282 | static void sigma_location_break_down(struct sigma_location *loc) | |
1283 | { | |
1284 | ||
1285 | loc->line = loc->raw / ROW_LENGTH_U16; | |
1286 | loc->line += ROW_COUNT; | |
1287 | loc->line %= ROW_COUNT; | |
1288 | loc->cluster = loc->raw % ROW_LENGTH_U16; | |
1289 | loc->event = loc->cluster % EVENTS_PER_CLUSTER; | |
1290 | loc->cluster = loc->cluster / EVENTS_PER_CLUSTER; | |
1291 | } | |
1292 | ||
1293 | static gboolean sigma_location_is_eq(struct sigma_location *loc1, | |
1294 | struct sigma_location *loc2, gboolean with_event) | |
1295 | { | |
1296 | ||
1297 | if (!loc1 || !loc2) | |
1298 | return FALSE; | |
1299 | ||
1300 | if (loc1->line != loc2->line) | |
1301 | return FALSE; | |
1302 | if (loc1->cluster != loc2->cluster) | |
1303 | return FALSE; | |
1304 | ||
1305 | if (with_event && loc1->event != loc2->event) | |
1306 | return FALSE; | |
1307 | ||
1308 | return TRUE; | |
1309 | } | |
1310 | ||
1311 | /* Decrement the broken-down location fields (leave 'raw' as is). */ | |
1312 | static void sigma_location_decrement(struct sigma_location *loc, | |
1313 | gboolean with_event) | |
1314 | { | |
1315 | ||
1316 | if (!loc) | |
1317 | return; | |
1318 | ||
1319 | if (with_event) { | |
1320 | if (loc->event--) | |
1321 | return; | |
1322 | loc->event = EVENTS_PER_CLUSTER - 1; | |
1323 | } | |
1324 | ||
1325 | if (loc->cluster--) | |
1326 | return; | |
1327 | loc->cluster = CLUSTERS_PER_ROW - 1; | |
1328 | ||
1329 | if (loc->line--) | |
1330 | return; | |
1331 | loc->line = ROW_COUNT - 1; | |
1332 | } | |
1333 | ||
1334 | static void sigma_location_increment(struct sigma_location *loc) | |
1335 | { | |
1336 | ||
1337 | if (!loc) | |
1338 | return; | |
1339 | ||
1340 | if (++loc->event < EVENTS_PER_CLUSTER) | |
1341 | return; | |
1342 | loc->event = 0; | |
1343 | if (++loc->cluster < CLUSTERS_PER_ROW) | |
1344 | return; | |
1345 | loc->cluster = 0; | |
1346 | if (++loc->line < ROW_COUNT) | |
1347 | return; | |
1348 | loc->line = 0; | |
1349 | } | |
1350 | ||
1351 | /* | |
1352 | * Determine the position where to open the period of trigger match | |
1353 | * checks. Setup an "impossible" location when triggers are not used. | |
1354 | * Start from the hardware provided 'trig' position otherwise, and | |
1355 | * go back a few clusters, but don't go before the 'start' position. | |
1356 | */ | |
1357 | static void rewind_trig_arm_pos(struct dev_context *devc, size_t count) | |
1358 | { | |
1359 | struct sigma_sample_interp *interp; | |
1360 | ||
1361 | if (!devc) | |
1362 | return; | |
1363 | interp = &devc->interp; | |
1364 | ||
1365 | if (!devc->use_triggers) { | |
1366 | interp->trig_arm.raw = ~0; | |
1367 | sigma_location_break_down(&interp->trig_arm); | |
1368 | return; | |
1369 | } | |
1370 | ||
1371 | interp->trig_arm = interp->trig; | |
1372 | while (count--) { | |
1373 | if (sigma_location_is_eq(&interp->trig_arm, &interp->start, TRUE)) | |
1374 | break; | |
1375 | sigma_location_decrement(&interp->trig_arm, TRUE); | |
1376 | } | |
1377 | } | |
1378 | ||
1379 | static int alloc_sample_buffer(struct dev_context *devc, | |
1380 | size_t stop_pos, size_t trig_pos, uint8_t mode) | |
1381 | { | |
1382 | struct sigma_sample_interp *interp; | |
1383 | gboolean wrapped; | |
1384 | size_t alloc_size; | |
1385 | ||
1386 | interp = &devc->interp; | |
1387 | ||
1388 | /* | |
1389 | * Either fetch sample memory from absolute start of DRAM to the | |
1390 | * current write position. Or from after the current write position | |
1391 | * to before the current write position, if the write pointer has | |
1392 | * wrapped around at the upper DRAM boundary. Assume that the line | |
1393 | * which most recently got written to is of unknown state, ignore | |
1394 | * its content in the "wrapped" case. | |
1395 | */ | |
1396 | wrapped = mode & RMR_ROUND; | |
1397 | interp->start.raw = 0; | |
1398 | interp->stop.raw = stop_pos; | |
1399 | if (wrapped) { | |
1400 | interp->start.raw = stop_pos; | |
1401 | interp->start.raw >>= ROW_SHIFT; | |
1402 | interp->start.raw++; | |
1403 | interp->start.raw <<= ROW_SHIFT; | |
1404 | interp->stop.raw = stop_pos; | |
1405 | interp->stop.raw >>= ROW_SHIFT; | |
1406 | interp->stop.raw--; | |
1407 | interp->stop.raw <<= ROW_SHIFT; | |
1408 | } | |
1409 | interp->trig.raw = trig_pos; | |
1410 | interp->iter.raw = 0; | |
1411 | ||
1412 | /* Break down raw values to line, cluster, event fields. */ | |
1413 | sigma_location_break_down(&interp->start); | |
1414 | sigma_location_break_down(&interp->stop); | |
1415 | sigma_location_break_down(&interp->trig); | |
1416 | sigma_location_break_down(&interp->iter); | |
1417 | ||
1418 | /* | |
1419 | * The hardware provided trigger location "is late" because of | |
1420 | * latency in hardware pipelines. It points to after the trigger | |
1421 | * condition match. Arrange for a software check of sample data | |
1422 | * matches starting just a little before the hardware provided | |
1423 | * location. The "4 clusters" distance is an arbitrary choice. | |
1424 | */ | |
1425 | rewind_trig_arm_pos(devc, 4 * EVENTS_PER_CLUSTER); | |
1426 | memset(&interp->trig_chk, 0, sizeof(interp->trig_chk)); | |
1427 | ||
1428 | /* Determine which DRAM lines to fetch from the device. */ | |
1429 | memset(&interp->fetch, 0, sizeof(interp->fetch)); | |
1430 | interp->fetch.lines_total = interp->stop.line + 1; | |
1431 | interp->fetch.lines_total -= interp->start.line; | |
1432 | interp->fetch.lines_total += ROW_COUNT; | |
1433 | interp->fetch.lines_total %= ROW_COUNT; | |
1434 | interp->fetch.lines_done = 0; | |
1435 | ||
1436 | /* Arrange for chunked download, N lines per USB request. */ | |
1437 | interp->fetch.lines_per_read = 32; | |
1438 | alloc_size = sizeof(devc->interp.fetch.rcvd_lines[0]); | |
1439 | alloc_size *= devc->interp.fetch.lines_per_read; | |
1440 | devc->interp.fetch.rcvd_lines = g_try_malloc0(alloc_size); | |
1441 | if (!devc->interp.fetch.rcvd_lines) | |
1442 | return SR_ERR_MALLOC; | |
1443 | ||
1444 | return SR_OK; | |
1445 | } | |
1446 | ||
1447 | static uint16_t sigma_deinterlace_data_4x4(uint16_t indata, int idx); | |
1448 | static uint16_t sigma_deinterlace_data_2x8(uint16_t indata, int idx); | |
1449 | ||
1450 | static int fetch_sample_buffer(struct dev_context *devc) | |
1451 | { | |
1452 | struct sigma_sample_interp *interp; | |
1453 | size_t count; | |
1454 | int ret; | |
1455 | const uint8_t *rdptr; | |
1456 | uint16_t ts, data; | |
1457 | ||
1458 | interp = &devc->interp; | |
1459 | ||
1460 | /* First invocation? Seed the iteration position. */ | |
1461 | if (!interp->fetch.lines_done) { | |
1462 | interp->iter = interp->start; | |
1463 | } | |
1464 | ||
1465 | /* Get another set of DRAM lines in one read call. */ | |
1466 | count = interp->fetch.lines_total - interp->fetch.lines_done; | |
1467 | if (count > interp->fetch.lines_per_read) | |
1468 | count = interp->fetch.lines_per_read; | |
1469 | ret = sigma_read_dram(devc, interp->iter.line, count, | |
1470 | (uint8_t *)interp->fetch.rcvd_lines); | |
1471 | if (ret != SR_OK) | |
1472 | return ret; | |
1473 | interp->fetch.lines_rcvd = count; | |
1474 | interp->fetch.curr_line = &interp->fetch.rcvd_lines[0]; | |
1475 | ||
1476 | /* First invocation? Get initial timestamp and sample data. */ | |
1477 | if (!interp->fetch.lines_done) { | |
1478 | rdptr = (void *)interp->fetch.curr_line; | |
1479 | ts = read_u16le_inc(&rdptr); | |
1480 | data = read_u16le_inc(&rdptr); | |
1481 | if (interp->samples_per_event == 4) { | |
1482 | data = sigma_deinterlace_data_4x4(data, 0); | |
1483 | } else if (interp->samples_per_event == 2) { | |
1484 | data = sigma_deinterlace_data_2x8(data, 0); | |
1485 | } | |
1486 | interp->last.ts = ts; | |
1487 | interp->last.sample = data; | |
1488 | } | |
1489 | ||
1490 | return SR_OK; | |
1491 | } | |
1492 | ||
1493 | static void free_sample_buffer(struct dev_context *devc) | |
1494 | { | |
1495 | g_free(devc->interp.fetch.rcvd_lines); | |
1496 | devc->interp.fetch.rcvd_lines = NULL; | |
1497 | devc->interp.fetch.lines_per_read = 0; | |
1498 | } | |
1499 | ||
1500 | /* | |
1501 | * In 100 and 200 MHz mode, only a single pin rising/falling can be | |
1502 | * set as trigger. In other modes, two rising/falling triggers can be set, | |
1503 | * in addition to value/mask trigger for any number of channels. | |
1504 | * | |
1505 | * The Sigma supports complex triggers using boolean expressions, but this | |
1506 | * has not been implemented yet. | |
1507 | */ | |
1508 | SR_PRIV int sigma_convert_trigger(const struct sr_dev_inst *sdi) | |
1509 | { | |
1510 | struct dev_context *devc; | |
1511 | struct sr_trigger *trigger; | |
1512 | struct sr_trigger_stage *stage; | |
1513 | struct sr_trigger_match *match; | |
1514 | const GSList *l, *m; | |
1515 | uint16_t channelbit; | |
1516 | size_t trigger_set; | |
1517 | ||
1518 | devc = sdi->priv; | |
1519 | memset(&devc->trigger, 0, sizeof(devc->trigger)); | |
1520 | devc->use_triggers = FALSE; | |
1521 | trigger = sr_session_trigger_get(sdi->session); | |
1522 | if (!trigger) | |
1523 | return SR_OK; | |
1524 | ||
1525 | if (!ASIX_SIGMA_WITH_TRIGGER) { | |
1526 | sr_warn("Trigger support is not implemented. Ignoring the spec."); | |
1527 | return SR_OK; | |
1528 | } | |
1529 | ||
1530 | trigger_set = 0; | |
1531 | for (l = trigger->stages; l; l = l->next) { | |
1532 | stage = l->data; | |
1533 | for (m = stage->matches; m; m = m->next) { | |
1534 | match = m->data; | |
1535 | /* Ignore disabled channels with a trigger. */ | |
1536 | if (!match->channel->enabled) | |
1537 | continue; | |
1538 | channelbit = BIT(match->channel->index); | |
1539 | if (devc->clock.samplerate >= SR_MHZ(100)) { | |
1540 | /* Fast trigger support. */ | |
1541 | if (trigger_set) { | |
1542 | sr_err("100/200MHz modes limited to single trigger pin."); | |
1543 | return SR_ERR; | |
1544 | } | |
1545 | if (match->match == SR_TRIGGER_FALLING) { | |
1546 | devc->trigger.fallingmask |= channelbit; | |
1547 | } else if (match->match == SR_TRIGGER_RISING) { | |
1548 | devc->trigger.risingmask |= channelbit; | |
1549 | } else { | |
1550 | sr_err("100/200MHz modes limited to edge trigger."); | |
1551 | return SR_ERR; | |
1552 | } | |
1553 | ||
1554 | trigger_set++; | |
1555 | } else { | |
1556 | /* Simple trigger support (event). */ | |
1557 | if (match->match == SR_TRIGGER_ONE) { | |
1558 | devc->trigger.simplevalue |= channelbit; | |
1559 | devc->trigger.simplemask |= channelbit; | |
1560 | } else if (match->match == SR_TRIGGER_ZERO) { | |
1561 | devc->trigger.simplevalue &= ~channelbit; | |
1562 | devc->trigger.simplemask |= channelbit; | |
1563 | } else if (match->match == SR_TRIGGER_FALLING) { | |
1564 | devc->trigger.fallingmask |= channelbit; | |
1565 | trigger_set++; | |
1566 | } else if (match->match == SR_TRIGGER_RISING) { | |
1567 | devc->trigger.risingmask |= channelbit; | |
1568 | trigger_set++; | |
1569 | } | |
1570 | ||
1571 | /* | |
1572 | * Actually, Sigma supports 2 rising/falling triggers, | |
1573 | * but they are ORed and the current trigger syntax | |
1574 | * does not permit ORed triggers. | |
1575 | */ | |
1576 | if (trigger_set > 1) { | |
1577 | sr_err("Limited to 1 edge trigger."); | |
1578 | return SR_ERR; | |
1579 | } | |
1580 | } | |
1581 | } | |
1582 | } | |
1583 | ||
1584 | /* Keep track whether triggers are involved during acquisition. */ | |
1585 | devc->use_triggers = TRUE; | |
1586 | ||
1587 | return SR_OK; | |
1588 | } | |
1589 | ||
1590 | static gboolean sample_matches_trigger(struct dev_context *devc, uint16_t sample) | |
1591 | { | |
1592 | struct sigma_sample_interp *interp; | |
1593 | uint16_t last_sample; | |
1594 | struct sigma_trigger *t; | |
1595 | gboolean simple_match, rising_match, falling_match; | |
1596 | gboolean matched; | |
1597 | ||
1598 | /* | |
1599 | * This logic is about improving the precision of the hardware | |
1600 | * provided trigger match position. Software checks are only | |
1601 | * required for a short range of samples, and only when a user | |
1602 | * specified trigger condition was involved during acquisition. | |
1603 | */ | |
1604 | if (!devc) | |
1605 | return FALSE; | |
1606 | if (!devc->use_triggers) | |
1607 | return FALSE; | |
1608 | interp = &devc->interp; | |
1609 | if (!interp->trig_chk.armed) | |
1610 | return FALSE; | |
1611 | ||
1612 | /* | |
1613 | * Check if the current sample and its most recent transition | |
1614 | * match the initially provided trigger condition. The data | |
1615 | * must not fail either of the individual checks. Unused | |
1616 | * trigger features remain neutral in the summary expression. | |
1617 | */ | |
1618 | last_sample = interp->last.sample; | |
1619 | t = &devc->trigger; | |
1620 | simple_match = (sample & t->simplemask) == t->simplevalue; | |
1621 | rising_match = ((last_sample & t->risingmask) == 0) && | |
1622 | ((sample & t->risingmask) == t->risingmask); | |
1623 | falling_match = ((last_sample & t->fallingmask) == t->fallingmask) && | |
1624 | ((sample & t->fallingmask) == 0); | |
1625 | matched = simple_match && rising_match && falling_match; | |
1626 | ||
1627 | return matched; | |
1628 | } | |
1629 | ||
1630 | static int send_trigger_marker(struct dev_context *devc) | |
1631 | { | |
1632 | int ret; | |
1633 | ||
1634 | ret = flush_submit_buffer(devc); | |
1635 | if (ret != SR_OK) | |
1636 | return ret; | |
1637 | ret = std_session_send_df_trigger(devc->buffer->sdi); | |
1638 | if (ret != SR_OK) | |
1639 | return ret; | |
1640 | ||
1641 | return SR_OK; | |
1642 | } | |
1643 | ||
1644 | static int check_and_submit_sample(struct dev_context *devc, | |
1645 | uint16_t sample, size_t count) | |
1646 | { | |
1647 | gboolean triggered; | |
1648 | int ret; | |
1649 | ||
1650 | triggered = sample_matches_trigger(devc, sample); | |
1651 | if (triggered) { | |
1652 | send_trigger_marker(devc); | |
1653 | devc->interp.trig_chk.matched = TRUE; | |
1654 | } | |
1655 | ||
1656 | ret = addto_submit_buffer(devc, sample, count); | |
1657 | if (ret != SR_OK) | |
1658 | return ret; | |
1659 | ||
1660 | return SR_OK; | |
1661 | } | |
1662 | ||
1663 | static void sigma_location_check(struct dev_context *devc) | |
1664 | { | |
1665 | struct sigma_sample_interp *interp; | |
1666 | ||
1667 | if (!devc) | |
1668 | return; | |
1669 | interp = &devc->interp; | |
1670 | ||
1671 | /* | |
1672 | * Manage the period of trigger match checks in software. | |
1673 | * Start supervision somewhere before the hardware provided | |
1674 | * location. Stop supervision after an arbitrary amount of | |
1675 | * event slots, or when a match was found. | |
1676 | */ | |
1677 | if (interp->trig_chk.armed) { | |
1678 | interp->trig_chk.evt_remain--; | |
1679 | if (!interp->trig_chk.evt_remain || interp->trig_chk.matched) | |
1680 | interp->trig_chk.armed = FALSE; | |
1681 | } | |
1682 | if (!interp->trig_chk.armed && !interp->trig_chk.matched) { | |
1683 | if (sigma_location_is_eq(&interp->iter, &interp->trig_arm, TRUE)) { | |
1684 | interp->trig_chk.armed = TRUE; | |
1685 | interp->trig_chk.matched = FALSE; | |
1686 | interp->trig_chk.evt_remain = 8 * EVENTS_PER_CLUSTER; | |
1687 | } | |
1688 | } | |
1689 | ||
1690 | /* | |
1691 | * Force a trigger marker when the software check found no match | |
1692 | * yet while the hardware provided position was reached. This | |
1693 | * very probably is a user initiated button press. | |
1694 | */ | |
1695 | if (interp->trig_chk.armed) { | |
1696 | if (sigma_location_is_eq(&interp->iter, &interp->trig, TRUE)) { | |
1697 | (void)send_trigger_marker(devc); | |
1698 | interp->trig_chk.matched = TRUE; | |
1699 | } | |
1700 | } | |
1701 | } | |
1702 | ||
1703 | /* | |
1704 | * Return the timestamp of "DRAM cluster". | |
1705 | */ | |
1706 | static uint16_t sigma_dram_cluster_ts(struct sigma_dram_cluster *cluster) | |
1707 | { | |
1708 | return read_u16le((const uint8_t *)&cluster->timestamp); | |
1709 | } | |
1710 | ||
1711 | /* | |
1712 | * Return one 16bit data entity of a DRAM cluster at the specified index. | |
1713 | */ | |
1714 | static uint16_t sigma_dram_cluster_data(struct sigma_dram_cluster *cl, int idx) | |
1715 | { | |
1716 | return read_u16le((const uint8_t *)&cl->samples[idx]); | |
1717 | } | |
1718 | ||
1719 | /* | |
1720 | * Deinterlace sample data that was retrieved at 100MHz samplerate. | |
1721 | * One 16bit item contains two samples of 8bits each. The bits of | |
1722 | * multiple samples are interleaved. | |
1723 | */ | |
1724 | static uint16_t sigma_deinterlace_data_2x8(uint16_t indata, int idx) | |
1725 | { | |
1726 | uint16_t outdata; | |
1727 | ||
1728 | indata >>= idx; | |
1729 | outdata = 0; | |
1730 | outdata |= (indata >> (0 * 2 - 0)) & (1 << 0); | |
1731 | outdata |= (indata >> (1 * 2 - 1)) & (1 << 1); | |
1732 | outdata |= (indata >> (2 * 2 - 2)) & (1 << 2); | |
1733 | outdata |= (indata >> (3 * 2 - 3)) & (1 << 3); | |
1734 | outdata |= (indata >> (4 * 2 - 4)) & (1 << 4); | |
1735 | outdata |= (indata >> (5 * 2 - 5)) & (1 << 5); | |
1736 | outdata |= (indata >> (6 * 2 - 6)) & (1 << 6); | |
1737 | outdata |= (indata >> (7 * 2 - 7)) & (1 << 7); | |
1738 | return outdata; | |
1739 | } | |
1740 | ||
1741 | /* | |
1742 | * Deinterlace sample data that was retrieved at 200MHz samplerate. | |
1743 | * One 16bit item contains four samples of 4bits each. The bits of | |
1744 | * multiple samples are interleaved. | |
1745 | */ | |
1746 | static uint16_t sigma_deinterlace_data_4x4(uint16_t indata, int idx) | |
1747 | { | |
1748 | uint16_t outdata; | |
1749 | ||
1750 | indata >>= idx; | |
1751 | outdata = 0; | |
1752 | outdata |= (indata >> (0 * 4 - 0)) & (1 << 0); | |
1753 | outdata |= (indata >> (1 * 4 - 1)) & (1 << 1); | |
1754 | outdata |= (indata >> (2 * 4 - 2)) & (1 << 2); | |
1755 | outdata |= (indata >> (3 * 4 - 3)) & (1 << 3); | |
1756 | return outdata; | |
1757 | } | |
1758 | ||
1759 | static void sigma_decode_dram_cluster(struct dev_context *devc, | |
1760 | struct sigma_dram_cluster *dram_cluster, | |
1761 | size_t events_in_cluster) | |
1762 | { | |
1763 | uint16_t tsdiff, ts, sample, item16; | |
1764 | size_t count; | |
1765 | size_t evt; | |
1766 | ||
1767 | /* | |
1768 | * If this cluster is not adjacent to the previously received | |
1769 | * cluster, then send the appropriate number of samples with the | |
1770 | * previous values to the sigrok session. This "decodes RLE". | |
1771 | * | |
1772 | * These samples cannot match the trigger since they just repeat | |
1773 | * the previously submitted data pattern. (This assumption holds | |
1774 | * for simple level and edge triggers. It would not for timed or | |
1775 | * counted conditions, which currently are not supported.) | |
1776 | */ | |
1777 | ts = sigma_dram_cluster_ts(dram_cluster); | |
1778 | tsdiff = ts - devc->interp.last.ts; | |
1779 | if (tsdiff > 0) { | |
1780 | sample = devc->interp.last.sample; | |
1781 | count = tsdiff * devc->interp.samples_per_event; | |
1782 | (void)check_and_submit_sample(devc, sample, count); | |
1783 | } | |
1784 | devc->interp.last.ts = ts + EVENTS_PER_CLUSTER; | |
1785 | ||
1786 | /* | |
1787 | * Grab sample data from the current cluster and prepare their | |
1788 | * submission to the session feed. Handle samplerate dependent | |
1789 | * memory layout of sample data. Accumulation of data chunks | |
1790 | * before submission is transparent to this code path, specific | |
1791 | * buffer depth is neither assumed nor required here. | |
1792 | */ | |
1793 | sample = 0; | |
1794 | for (evt = 0; evt < events_in_cluster; evt++) { | |
1795 | item16 = sigma_dram_cluster_data(dram_cluster, evt); | |
1796 | if (devc->interp.samples_per_event == 4) { | |
1797 | sample = sigma_deinterlace_data_4x4(item16, 0); | |
1798 | check_and_submit_sample(devc, sample, 1); | |
1799 | devc->interp.last.sample = sample; | |
1800 | sample = sigma_deinterlace_data_4x4(item16, 1); | |
1801 | check_and_submit_sample(devc, sample, 1); | |
1802 | devc->interp.last.sample = sample; | |
1803 | sample = sigma_deinterlace_data_4x4(item16, 2); | |
1804 | check_and_submit_sample(devc, sample, 1); | |
1805 | devc->interp.last.sample = sample; | |
1806 | sample = sigma_deinterlace_data_4x4(item16, 3); | |
1807 | check_and_submit_sample(devc, sample, 1); | |
1808 | devc->interp.last.sample = sample; | |
1809 | } else if (devc->interp.samples_per_event == 2) { | |
1810 | sample = sigma_deinterlace_data_2x8(item16, 0); | |
1811 | check_and_submit_sample(devc, sample, 1); | |
1812 | devc->interp.last.sample = sample; | |
1813 | sample = sigma_deinterlace_data_2x8(item16, 1); | |
1814 | check_and_submit_sample(devc, sample, 1); | |
1815 | devc->interp.last.sample = sample; | |
1816 | } else { | |
1817 | sample = item16; | |
1818 | check_and_submit_sample(devc, sample, 1); | |
1819 | devc->interp.last.sample = sample; | |
1820 | } | |
1821 | sigma_location_increment(&devc->interp.iter); | |
1822 | sigma_location_check(devc); | |
1823 | } | |
1824 | } | |
1825 | ||
1826 | /* | |
1827 | * Decode chunk of 1024 bytes, 64 clusters, 7 events per cluster. | |
1828 | * Each event is 20ns apart, and can contain multiple samples. | |
1829 | * | |
1830 | * For 200 MHz, events contain 4 samples for each channel, spread 5 ns apart. | |
1831 | * For 100 MHz, events contain 2 samples for each channel, spread 10 ns apart. | |
1832 | * For 50 MHz and below, events contain one sample for each channel, | |
1833 | * spread 20 ns apart. | |
1834 | */ | |
1835 | static int decode_chunk_ts(struct dev_context *devc, | |
1836 | struct sigma_dram_line *dram_line, | |
1837 | size_t events_in_line) | |
1838 | { | |
1839 | struct sigma_dram_cluster *dram_cluster; | |
1840 | size_t clusters_in_line; | |
1841 | size_t events_in_cluster; | |
1842 | size_t cluster; | |
1843 | ||
1844 | clusters_in_line = events_in_line; | |
1845 | clusters_in_line += EVENTS_PER_CLUSTER - 1; | |
1846 | clusters_in_line /= EVENTS_PER_CLUSTER; | |
1847 | ||
1848 | /* For each full DRAM cluster. */ | |
1849 | for (cluster = 0; cluster < clusters_in_line; cluster++) { | |
1850 | dram_cluster = &dram_line->cluster[cluster]; | |
1851 | ||
1852 | /* The last cluster might not be full. */ | |
1853 | if ((cluster == clusters_in_line - 1) && | |
1854 | (events_in_line % EVENTS_PER_CLUSTER)) { | |
1855 | events_in_cluster = events_in_line % EVENTS_PER_CLUSTER; | |
1856 | } else { | |
1857 | events_in_cluster = EVENTS_PER_CLUSTER; | |
1858 | } | |
1859 | ||
1860 | sigma_decode_dram_cluster(devc, dram_cluster, | |
1861 | events_in_cluster); | |
1862 | } | |
1863 | ||
1864 | return SR_OK; | |
1865 | } | |
1866 | ||
1867 | static int download_capture(struct sr_dev_inst *sdi) | |
1868 | { | |
1869 | struct dev_context *devc; | |
1870 | struct sigma_sample_interp *interp; | |
1871 | uint32_t stoppos, triggerpos; | |
1872 | uint8_t modestatus; | |
1873 | int ret; | |
1874 | size_t chunks_per_receive_call; | |
1875 | ||
1876 | devc = sdi->priv; | |
1877 | interp = &devc->interp; | |
1878 | ||
1879 | /* | |
1880 | * Check the mode register. Force stop the current acquisition | |
1881 | * if it has not yet terminated before. Will block until the | |
1882 | * acquisition stops, assuming that this won't take long. Should | |
1883 | * execute exactly once, then keep finding its condition met. | |
1884 | * | |
1885 | * Ask the hardware to stop data acquisition. Reception of the | |
1886 | * FORCESTOP request makes the hardware "disable RLE" (store | |
1887 | * clusters to DRAM regardless of whether pin state changes) and | |
1888 | * raise the POSTTRIGGERED flag. | |
1889 | */ | |
1890 | ret = sigma_get_register(devc, READ_MODE, &modestatus); | |
1891 | if (ret != SR_OK) { | |
1892 | sr_err("Could not determine current device state."); | |
1893 | return FALSE; | |
1894 | } | |
1895 | if (!(modestatus & RMR_POSTTRIGGERED)) { | |
1896 | sr_info("Downloading sample data."); | |
1897 | devc->state = SIGMA_DOWNLOAD; | |
1898 | ||
1899 | modestatus = WMR_FORCESTOP | WMR_SDRAMWRITEEN; | |
1900 | ret = sigma_set_register(devc, WRITE_MODE, modestatus); | |
1901 | if (ret != SR_OK) | |
1902 | return FALSE; | |
1903 | do { | |
1904 | ret = sigma_get_register(devc, READ_MODE, &modestatus); | |
1905 | if (ret != SR_OK) { | |
1906 | sr_err("Could not poll for post-trigger state."); | |
1907 | return FALSE; | |
1908 | } | |
1909 | } while (!(modestatus & RMR_POSTTRIGGERED)); | |
1910 | } | |
1911 | ||
1912 | /* | |
1913 | * Switch the hardware from DRAM write (data acquisition) to | |
1914 | * DRAM read (sample memory download). Prepare resources for | |
1915 | * sample memory content retrieval. Should execute exactly once, | |
1916 | * then keep finding its condition met. | |
1917 | * | |
1918 | * Get the current positions (acquisition write pointer, and | |
1919 | * trigger match location). With disabled triggers, use a value | |
1920 | * for the location that will never match during interpretation. | |
1921 | * Determine which area of the sample memory to retrieve, | |
1922 | * allocate a receive buffer, and setup counters/pointers. | |
1923 | */ | |
1924 | if (!interp->fetch.lines_per_read) { | |
1925 | ret = sigma_set_register(devc, WRITE_MODE, WMR_SDRAMREADEN); | |
1926 | if (ret != SR_OK) | |
1927 | return FALSE; | |
1928 | ||
1929 | ret = sigma_read_pos(devc, &stoppos, &triggerpos, &modestatus); | |
1930 | if (ret != SR_OK) { | |
1931 | sr_err("Could not query capture positions/state."); | |
1932 | return FALSE; | |
1933 | } | |
1934 | if (!devc->use_triggers) | |
1935 | triggerpos = ~0; | |
1936 | if (!(modestatus & RMR_TRIGGERED)) | |
1937 | triggerpos = ~0; | |
1938 | ||
1939 | ret = alloc_sample_buffer(devc, stoppos, triggerpos, modestatus); | |
1940 | if (ret != SR_OK) | |
1941 | return FALSE; | |
1942 | ||
1943 | ret = alloc_submit_buffer(sdi); | |
1944 | if (ret != SR_OK) | |
1945 | return FALSE; | |
1946 | ret = setup_submit_limit(devc); | |
1947 | if (ret != SR_OK) | |
1948 | return FALSE; | |
1949 | } | |
1950 | ||
1951 | /* | |
1952 | * Get another set of sample memory rows, and interpret its | |
1953 | * content. Will execute as many times as it takes to complete | |
1954 | * the memory region that the recent acquisition spans. | |
1955 | * | |
1956 | * The size of a receive call's workload and the main loop's | |
1957 | * receive call poll period determine the UI responsiveness and | |
1958 | * the overall transfer time for the sample memory content. | |
1959 | */ | |
1960 | chunks_per_receive_call = 50; | |
1961 | while (interp->fetch.lines_done < interp->fetch.lines_total) { | |
1962 | size_t dl_events_in_line; | |
1963 | ||
1964 | /* Read another chunk of sample memory (several lines). */ | |
1965 | ret = fetch_sample_buffer(devc); | |
1966 | if (ret != SR_OK) | |
1967 | return FALSE; | |
1968 | ||
1969 | /* Process lines of sample data. Last line may be short. */ | |
1970 | while (interp->fetch.lines_rcvd--) { | |
1971 | dl_events_in_line = EVENTS_PER_ROW; | |
1972 | if (interp->iter.line == interp->stop.line) { | |
1973 | dl_events_in_line = interp->stop.raw & ROW_MASK; | |
1974 | } | |
1975 | decode_chunk_ts(devc, interp->fetch.curr_line, | |
1976 | dl_events_in_line); | |
1977 | interp->fetch.curr_line++; | |
1978 | interp->fetch.lines_done++; | |
1979 | } | |
1980 | ||
1981 | /* Keep returning to application code for large data sets. */ | |
1982 | if (!--chunks_per_receive_call) { | |
1983 | ret = flush_submit_buffer(devc); | |
1984 | if (ret != SR_OK) | |
1985 | return FALSE; | |
1986 | break; | |
1987 | } | |
1988 | } | |
1989 | ||
1990 | /* | |
1991 | * Release previously allocated resources, and adjust state when | |
1992 | * all of the sample memory was retrieved, and interpretation has | |
1993 | * completed. Should execute exactly once. | |
1994 | */ | |
1995 | if (interp->fetch.lines_done >= interp->fetch.lines_total) { | |
1996 | ret = flush_submit_buffer(devc); | |
1997 | if (ret != SR_OK) | |
1998 | return FALSE; | |
1999 | free_submit_buffer(devc); | |
2000 | free_sample_buffer(devc); | |
2001 | ||
2002 | ret = std_session_send_df_end(sdi); | |
2003 | if (ret != SR_OK) | |
2004 | return FALSE; | |
2005 | ||
2006 | devc->state = SIGMA_IDLE; | |
2007 | sr_dev_acquisition_stop(sdi); | |
2008 | } | |
2009 | ||
2010 | return TRUE; | |
2011 | } | |
2012 | ||
2013 | /* | |
2014 | * Periodically check the Sigma status when in CAPTURE mode. This routine | |
2015 | * checks whether the configured sample count or sample time have passed, | |
2016 | * and will stop acquisition and download the acquired samples. | |
2017 | */ | |
2018 | static int sigma_capture_mode(struct sr_dev_inst *sdi) | |
2019 | { | |
2020 | struct dev_context *devc; | |
2021 | ||
2022 | devc = sdi->priv; | |
2023 | if (sr_sw_limits_check(&devc->limit.acquire)) | |
2024 | return download_capture(sdi); | |
2025 | ||
2026 | return TRUE; | |
2027 | } | |
2028 | ||
2029 | SR_PRIV int sigma_receive_data(int fd, int revents, void *cb_data) | |
2030 | { | |
2031 | struct sr_dev_inst *sdi; | |
2032 | struct dev_context *devc; | |
2033 | ||
2034 | (void)fd; | |
2035 | (void)revents; | |
2036 | ||
2037 | sdi = cb_data; | |
2038 | devc = sdi->priv; | |
2039 | ||
2040 | if (devc->state == SIGMA_IDLE) | |
2041 | return TRUE; | |
2042 | ||
2043 | /* | |
2044 | * When the application has requested to stop the acquisition, | |
2045 | * then immediately start downloading sample data. Continue a | |
2046 | * previously initiated download until completion. Otherwise | |
2047 | * keep checking configured limits which will terminate the | |
2048 | * acquisition and initiate download. | |
2049 | */ | |
2050 | if (devc->state == SIGMA_STOPPING) | |
2051 | return download_capture(sdi); | |
2052 | if (devc->state == SIGMA_DOWNLOAD) | |
2053 | return download_capture(sdi); | |
2054 | if (devc->state == SIGMA_CAPTURE) | |
2055 | return sigma_capture_mode(sdi); | |
2056 | ||
2057 | return TRUE; | |
2058 | } | |
2059 | ||
2060 | /* Build a LUT entry used by the trigger functions. */ | |
2061 | static void build_lut_entry(uint16_t *lut_entry, | |
2062 | uint16_t spec_value, uint16_t spec_mask) | |
2063 | { | |
2064 | size_t quad, bitidx, ch; | |
2065 | uint16_t quadmask, bitmask; | |
2066 | gboolean spec_value_low, bit_idx_low; | |
2067 | ||
2068 | /* | |
2069 | * For each quad-channel-group, for each bit in the LUT (each | |
2070 | * bit pattern of the channel signals, aka LUT address), for | |
2071 | * each channel in the quad, setup the bit in the LUT entry. | |
2072 | * | |
2073 | * Start from all-ones in the LUT (true, always matches), then | |
2074 | * "pessimize the truthness" for specified conditions. | |
2075 | */ | |
2076 | for (quad = 0; quad < 4; quad++) { | |
2077 | lut_entry[quad] = ~0; | |
2078 | for (bitidx = 0; bitidx < 16; bitidx++) { | |
2079 | for (ch = 0; ch < 4; ch++) { | |
2080 | quadmask = BIT(ch); | |
2081 | bitmask = quadmask << (quad * 4); | |
2082 | if (!(spec_mask & bitmask)) | |
2083 | continue; | |
2084 | /* | |
2085 | * This bit is part of the spec. The | |
2086 | * condition which gets checked here | |
2087 | * (got checked in all implementations | |
2088 | * so far) is uncertain. A bit position | |
2089 | * in the current index' number(!) is | |
2090 | * checked? | |
2091 | */ | |
2092 | spec_value_low = !(spec_value & bitmask); | |
2093 | bit_idx_low = !(bitidx & quadmask); | |
2094 | if (spec_value_low == bit_idx_low) | |
2095 | continue; | |
2096 | lut_entry[quad] &= ~BIT(bitidx); | |
2097 | } | |
2098 | } | |
2099 | } | |
2100 | } | |
2101 | ||
2102 | /* Add a logical function to LUT mask. */ | |
2103 | static void add_trigger_function(enum triggerop oper, enum triggerfunc func, | |
2104 | size_t index, gboolean neg, uint16_t *mask) | |
2105 | { | |
2106 | int x[2][2], a, b, aset, bset, rset; | |
2107 | size_t bitidx; | |
2108 | ||
2109 | /* | |
2110 | * Beware! The x, a, b, aset, bset, rset variables strictly | |
2111 | * require the limited 0..1 range. They are not interpreted | |
2112 | * as logically true, instead bit arith is done on them. | |
2113 | */ | |
2114 | ||
2115 | /* Construct a pattern which detects the condition. */ | |
2116 | memset(x, 0, sizeof(x)); | |
2117 | switch (oper) { | |
2118 | case OP_LEVEL: | |
2119 | x[0][1] = 1; | |
2120 | x[1][1] = 1; | |
2121 | break; | |
2122 | case OP_NOT: | |
2123 | x[0][0] = 1; | |
2124 | x[1][0] = 1; | |
2125 | break; | |
2126 | case OP_RISE: | |
2127 | x[0][1] = 1; | |
2128 | break; | |
2129 | case OP_FALL: | |
2130 | x[1][0] = 1; | |
2131 | break; | |
2132 | case OP_RISEFALL: | |
2133 | x[0][1] = 1; | |
2134 | x[1][0] = 1; | |
2135 | break; | |
2136 | case OP_NOTRISE: | |
2137 | x[1][1] = 1; | |
2138 | x[0][0] = 1; | |
2139 | x[1][0] = 1; | |
2140 | break; | |
2141 | case OP_NOTFALL: | |
2142 | x[1][1] = 1; | |
2143 | x[0][0] = 1; | |
2144 | x[0][1] = 1; | |
2145 | break; | |
2146 | case OP_NOTRISEFALL: | |
2147 | x[1][1] = 1; | |
2148 | x[0][0] = 1; | |
2149 | break; | |
2150 | } | |
2151 | ||
2152 | /* Transpose the pattern if the condition is negated. */ | |
2153 | if (neg) { | |
2154 | size_t i, j; | |
2155 | int tmp; | |
2156 | ||
2157 | for (i = 0; i < 2; i++) { | |
2158 | for (j = 0; j < 2; j++) { | |
2159 | tmp = x[i][j]; | |
2160 | x[i][j] = x[1 - i][1 - j]; | |
2161 | x[1 - i][1 - j] = tmp; | |
2162 | } | |
2163 | } | |
2164 | } | |
2165 | ||
2166 | /* Update the LUT mask with the function's condition. */ | |
2167 | for (bitidx = 0; bitidx < 16; bitidx++) { | |
2168 | a = (bitidx & BIT(2 * index + 0)) ? 1 : 0; | |
2169 | b = (bitidx & BIT(2 * index + 1)) ? 1 : 0; | |
2170 | ||
2171 | aset = (*mask & BIT(bitidx)) ? 1 : 0; | |
2172 | bset = x[b][a]; | |
2173 | ||
2174 | if (func == FUNC_AND || func == FUNC_NAND) | |
2175 | rset = aset & bset; | |
2176 | else if (func == FUNC_OR || func == FUNC_NOR) | |
2177 | rset = aset | bset; | |
2178 | else if (func == FUNC_XOR || func == FUNC_NXOR) | |
2179 | rset = aset ^ bset; | |
2180 | else | |
2181 | rset = 0; | |
2182 | ||
2183 | if (func == FUNC_NAND || func == FUNC_NOR || func == FUNC_NXOR) | |
2184 | rset = 1 - rset; | |
2185 | ||
2186 | if (rset) | |
2187 | *mask |= BIT(bitidx); | |
2188 | else | |
2189 | *mask &= ~BIT(bitidx); | |
2190 | } | |
2191 | } | |
2192 | ||
2193 | /* | |
2194 | * Build trigger LUTs used by 50 MHz and lower sample rates for supporting | |
2195 | * simple pin change and state triggers. Only two transitions (rise/fall) can be | |
2196 | * set at any time, but a full mask and value can be set (0/1). | |
2197 | */ | |
2198 | SR_PRIV int sigma_build_basic_trigger(struct dev_context *devc, | |
2199 | struct triggerlut *lut) | |
2200 | { | |
2201 | uint16_t masks[2]; | |
2202 | size_t bitidx, condidx; | |
2203 | uint16_t value, mask; | |
2204 | ||
2205 | /* Setup something that "won't match" in the absence of a spec. */ | |
2206 | memset(lut, 0, sizeof(*lut)); | |
2207 | if (!devc->use_triggers) | |
2208 | return SR_OK; | |
2209 | ||
2210 | /* Start assuming simple triggers. Edges are handled below. */ | |
2211 | lut->m4 = 0xa000; | |
2212 | lut->m3q = 0xffff; | |
2213 | ||
2214 | /* Process value/mask triggers. */ | |
2215 | value = devc->trigger.simplevalue; | |
2216 | mask = devc->trigger.simplemask; | |
2217 | build_lut_entry(lut->m2d, value, mask); | |
2218 | ||
2219 | /* Scan for and process rise/fall triggers. */ | |
2220 | memset(&masks, 0, sizeof(masks)); | |
2221 | condidx = 0; | |
2222 | for (bitidx = 0; bitidx < 16; bitidx++) { | |
2223 | mask = BIT(bitidx); | |
2224 | value = devc->trigger.risingmask | devc->trigger.fallingmask; | |
2225 | if (!(value & mask)) | |
2226 | continue; | |
2227 | if (condidx == 0) | |
2228 | build_lut_entry(lut->m0d, mask, mask); | |
2229 | if (condidx == 1) | |
2230 | build_lut_entry(lut->m1d, mask, mask); | |
2231 | masks[condidx++] = mask; | |
2232 | if (condidx == ARRAY_SIZE(masks)) | |
2233 | break; | |
2234 | } | |
2235 | ||
2236 | /* Add glue logic for rise/fall triggers. */ | |
2237 | if (masks[0] || masks[1]) { | |
2238 | lut->m3q = 0; | |
2239 | if (masks[0] & devc->trigger.risingmask) | |
2240 | add_trigger_function(OP_RISE, FUNC_OR, 0, 0, &lut->m3q); | |
2241 | if (masks[0] & devc->trigger.fallingmask) | |
2242 | add_trigger_function(OP_FALL, FUNC_OR, 0, 0, &lut->m3q); | |
2243 | if (masks[1] & devc->trigger.risingmask) | |
2244 | add_trigger_function(OP_RISE, FUNC_OR, 1, 0, &lut->m3q); | |
2245 | if (masks[1] & devc->trigger.fallingmask) | |
2246 | add_trigger_function(OP_FALL, FUNC_OR, 1, 0, &lut->m3q); | |
2247 | } | |
2248 | ||
2249 | /* Triggertype: event. */ | |
2250 | lut->params.selres = TRGSEL_SELCODE_NEVER; | |
2251 | lut->params.selinc = TRGSEL_SELCODE_LEVEL; | |
2252 | lut->params.sela = 0; /* Counter >= CMPA && LEVEL */ | |
2253 | lut->params.cmpa = 0; /* Count 0 -> 1 already triggers. */ | |
2254 | ||
2255 | return SR_OK; | |
2256 | } |