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