]>
Commit | Line | Data |
---|---|---|
28a35d8a HE |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
911f1834 UH |
4 | * Copyright (C) 2010 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> | |
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 UH |
22 | /* |
23 | * ASIX Sigma Logic Analyzer Driver | |
24 | */ | |
25 | ||
28a35d8a HE |
26 | #include <ftdi.h> |
27 | #include <string.h> | |
28 | #include <zlib.h> | |
fefa1800 | 29 | #include <sigrok.h> |
28a35d8a HE |
30 | #include "asix-sigma.h" |
31 | ||
32 | #define USB_VENDOR 0xa600 | |
33 | #define USB_PRODUCT 0xa000 | |
34 | #define USB_DESCRIPTION "ASIX SIGMA" | |
35 | #define USB_VENDOR_NAME "ASIX" | |
36 | #define USB_MODEL_NAME "SIGMA" | |
37 | #define USB_MODEL_VERSION "" | |
ee492173 | 38 | #define TRIGGER_TYPES "rf10" |
28a35d8a HE |
39 | |
40 | static GSList *device_instances = NULL; | |
41 | ||
42 | // XXX These should be per device | |
43 | static struct ftdi_context ftdic; | |
f78898e9 | 44 | static uint64_t cur_samplerate = 0; |
28a35d8a HE |
45 | static uint32_t limit_msec = 0; |
46 | static struct timeval start_tv; | |
f6564c8d | 47 | static int cur_firmware = -1; |
f78898e9 HE |
48 | static int num_probes = 0; |
49 | static int samples_per_event = 0; | |
57bbf56b | 50 | static int capture_ratio = 50; |
c53d793f | 51 | static struct sigma_trigger trigger; |
6aac7737 | 52 | static struct sigma_state sigma; |
ee492173 | 53 | |
28a35d8a | 54 | static uint64_t supported_samplerates[] = { |
ed09fd07 | 55 | KHZ(200), |
edca2c5c | 56 | KHZ(250), |
ed09fd07 | 57 | KHZ(500), |
edca2c5c | 58 | MHZ(1), |
ed09fd07 | 59 | MHZ(5), |
edca2c5c HE |
60 | MHZ(10), |
61 | MHZ(25), | |
e8397563 HE |
62 | MHZ(50), |
63 | MHZ(100), | |
28a35d8a HE |
64 | MHZ(200), |
65 | 0, | |
66 | }; | |
67 | ||
68 | static struct samplerates samplerates = { | |
ed09fd07 | 69 | KHZ(200), |
28a35d8a HE |
70 | MHZ(200), |
71 | 0, | |
72 | supported_samplerates, | |
73 | }; | |
74 | ||
75 | static int capabilities[] = { | |
76 | HWCAP_LOGIC_ANALYZER, | |
77 | HWCAP_SAMPLERATE, | |
57bbf56b HE |
78 | HWCAP_CAPTURE_RATIO, |
79 | HWCAP_PROBECONFIG, | |
28a35d8a | 80 | |
28a35d8a HE |
81 | HWCAP_LIMIT_MSEC, |
82 | 0, | |
83 | }; | |
84 | ||
fefa1800 UH |
85 | /* Force the FPGA to reboot. */ |
86 | static uint8_t suicide[] = { | |
87 | 0x84, 0x84, 0x88, 0x84, 0x88, 0x84, 0x88, 0x84, | |
88 | }; | |
89 | ||
90 | /* Prepare to upload firmware (FPGA specific). */ | |
91 | static uint8_t init[] = { | |
92 | 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, | |
93 | }; | |
94 | ||
95 | /* Initialize the logic analyzer mode. */ | |
96 | static uint8_t logic_mode_start[] = { | |
97 | 0x00, 0x40, 0x0f, 0x25, 0x35, 0x40, | |
98 | 0x2a, 0x3a, 0x40, 0x03, 0x20, 0x38, | |
99 | }; | |
100 | ||
eec5275e | 101 | static const char *firmware_files[] = { |
a8116d76 HE |
102 | "asix-sigma-50.fw", /* 50 MHz, supports 8 bit fractions */ |
103 | "asix-sigma-100.fw", /* 100 MHz */ | |
104 | "asix-sigma-200.fw", /* 200 MHz */ | |
ed09fd07 | 105 | "asix-sigma-50sync.fw", /* Synchronous clock from pin */ |
a8116d76 | 106 | "asix-sigma-phasor.fw", /* Frequency counter */ |
f6564c8d HE |
107 | }; |
108 | ||
6aac7737 HE |
109 | static void hw_stop_acquisition(int device_index, gpointer session_device_id); |
110 | ||
9ddb2a12 | 111 | static int sigma_read(void *buf, size_t size) |
28a35d8a HE |
112 | { |
113 | int ret; | |
fefa1800 UH |
114 | |
115 | ret = ftdi_read_data(&ftdic, (unsigned char *)buf, size); | |
28a35d8a HE |
116 | if (ret < 0) { |
117 | g_warning("ftdi_read_data failed: %s", | |
fefa1800 | 118 | ftdi_get_error_string(&ftdic)); |
28a35d8a HE |
119 | } |
120 | ||
121 | return ret; | |
122 | } | |
123 | ||
fefa1800 | 124 | static int sigma_write(void *buf, size_t size) |
28a35d8a HE |
125 | { |
126 | int ret; | |
fefa1800 UH |
127 | |
128 | ret = ftdi_write_data(&ftdic, (unsigned char *)buf, size); | |
28a35d8a HE |
129 | if (ret < 0) { |
130 | g_warning("ftdi_write_data failed: %s", | |
fefa1800 UH |
131 | ftdi_get_error_string(&ftdic)); |
132 | } else if ((size_t) ret != size) { | |
28a35d8a HE |
133 | g_warning("ftdi_write_data did not complete write\n"); |
134 | } | |
135 | ||
136 | return ret; | |
137 | } | |
138 | ||
139 | static int sigma_write_register(uint8_t reg, uint8_t *data, size_t len) | |
140 | { | |
141 | size_t i; | |
142 | uint8_t buf[len + 2]; | |
143 | int idx = 0; | |
144 | ||
145 | buf[idx++] = REG_ADDR_LOW | (reg & 0xf); | |
146 | buf[idx++] = REG_ADDR_HIGH | (reg >> 4); | |
147 | ||
fefa1800 | 148 | for (i = 0; i < len; ++i) { |
28a35d8a HE |
149 | buf[idx++] = REG_DATA_LOW | (data[i] & 0xf); |
150 | buf[idx++] = REG_DATA_HIGH_WRITE | (data[i] >> 4); | |
151 | } | |
152 | ||
153 | return sigma_write(buf, idx); | |
154 | } | |
155 | ||
156 | static int sigma_set_register(uint8_t reg, uint8_t value) | |
157 | { | |
158 | return sigma_write_register(reg, &value, 1); | |
159 | } | |
160 | ||
161 | static int sigma_read_register(uint8_t reg, uint8_t *data, size_t len) | |
162 | { | |
163 | uint8_t buf[3]; | |
fefa1800 | 164 | |
28a35d8a HE |
165 | buf[0] = REG_ADDR_LOW | (reg & 0xf); |
166 | buf[1] = REG_ADDR_HIGH | (reg >> 4); | |
28a35d8a HE |
167 | buf[2] = REG_READ_ADDR; |
168 | ||
169 | sigma_write(buf, sizeof(buf)); | |
170 | ||
171 | return sigma_read(data, len); | |
172 | } | |
173 | ||
174 | static uint8_t sigma_get_register(uint8_t reg) | |
175 | { | |
176 | uint8_t value; | |
fefa1800 | 177 | |
28a35d8a HE |
178 | if (1 != sigma_read_register(reg, &value, 1)) { |
179 | g_warning("Sigma_get_register: 1 byte expected"); | |
180 | return 0; | |
181 | } | |
182 | ||
183 | return value; | |
184 | } | |
185 | ||
186 | static int sigma_read_pos(uint32_t *stoppos, uint32_t *triggerpos) | |
187 | { | |
188 | uint8_t buf[] = { | |
189 | REG_ADDR_LOW | READ_TRIGGER_POS_LOW, | |
190 | ||
191 | REG_READ_ADDR | NEXT_REG, | |
192 | REG_READ_ADDR | NEXT_REG, | |
193 | REG_READ_ADDR | NEXT_REG, | |
194 | REG_READ_ADDR | NEXT_REG, | |
195 | REG_READ_ADDR | NEXT_REG, | |
196 | REG_READ_ADDR | NEXT_REG, | |
197 | }; | |
28a35d8a HE |
198 | uint8_t result[6]; |
199 | ||
200 | sigma_write(buf, sizeof(buf)); | |
201 | ||
202 | sigma_read(result, sizeof(result)); | |
203 | ||
204 | *triggerpos = result[0] | (result[1] << 8) | (result[2] << 16); | |
205 | *stoppos = result[3] | (result[4] << 8) | (result[5] << 16); | |
206 | ||
57bbf56b HE |
207 | /* Not really sure why this must be done, but according to spec. */ |
208 | if ((--*stoppos & 0x1ff) == 0x1ff) | |
209 | stoppos -= 64; | |
210 | ||
211 | if ((*--triggerpos & 0x1ff) == 0x1ff) | |
212 | triggerpos -= 64; | |
213 | ||
28a35d8a HE |
214 | return 1; |
215 | } | |
216 | ||
217 | static int sigma_read_dram(uint16_t startchunk, size_t numchunks, uint8_t *data) | |
218 | { | |
219 | size_t i; | |
220 | uint8_t buf[4096]; | |
221 | int idx = 0; | |
222 | ||
fefa1800 | 223 | /* Send the startchunk. Index start with 1. */ |
28a35d8a HE |
224 | buf[0] = startchunk >> 8; |
225 | buf[1] = startchunk & 0xff; | |
226 | sigma_write_register(WRITE_MEMROW, buf, 2); | |
227 | ||
fefa1800 | 228 | /* Read the DRAM. */ |
28a35d8a HE |
229 | buf[idx++] = REG_DRAM_BLOCK; |
230 | buf[idx++] = REG_DRAM_WAIT_ACK; | |
231 | ||
232 | for (i = 0; i < numchunks; ++i) { | |
fefa1800 UH |
233 | /* Alternate bit to copy from DRAM to cache. */ |
234 | if (i != (numchunks - 1)) | |
235 | buf[idx++] = REG_DRAM_BLOCK | (((i + 1) % 2) << 4); | |
28a35d8a HE |
236 | |
237 | buf[idx++] = REG_DRAM_BLOCK_DATA | ((i % 2) << 4); | |
238 | ||
fefa1800 | 239 | if (i != (numchunks - 1)) |
28a35d8a HE |
240 | buf[idx++] = REG_DRAM_WAIT_ACK; |
241 | } | |
242 | ||
243 | sigma_write(buf, idx); | |
244 | ||
245 | return sigma_read(data, numchunks * CHUNK_SIZE); | |
246 | } | |
247 | ||
4ae1f451 | 248 | /* Upload trigger look-up tables to Sigma. */ |
ee492173 HE |
249 | static int sigma_write_trigger_lut(struct triggerlut *lut) |
250 | { | |
251 | int i; | |
252 | uint8_t tmp[2]; | |
253 | uint16_t bit; | |
254 | ||
255 | /* Transpose the table and send to Sigma. */ | |
256 | for (i = 0; i < 16; ++i) { | |
257 | bit = 1 << i; | |
258 | ||
259 | tmp[0] = tmp[1] = 0; | |
260 | ||
261 | if (lut->m2d[0] & bit) | |
262 | tmp[0] |= 0x01; | |
263 | if (lut->m2d[1] & bit) | |
264 | tmp[0] |= 0x02; | |
265 | if (lut->m2d[2] & bit) | |
266 | tmp[0] |= 0x04; | |
267 | if (lut->m2d[3] & bit) | |
268 | tmp[0] |= 0x08; | |
269 | ||
270 | if (lut->m3 & bit) | |
271 | tmp[0] |= 0x10; | |
272 | if (lut->m3s & bit) | |
273 | tmp[0] |= 0x20; | |
274 | if (lut->m4 & bit) | |
275 | tmp[0] |= 0x40; | |
276 | ||
277 | if (lut->m0d[0] & bit) | |
278 | tmp[1] |= 0x01; | |
279 | if (lut->m0d[1] & bit) | |
280 | tmp[1] |= 0x02; | |
281 | if (lut->m0d[2] & bit) | |
282 | tmp[1] |= 0x04; | |
283 | if (lut->m0d[3] & bit) | |
284 | tmp[1] |= 0x08; | |
285 | ||
286 | if (lut->m1d[0] & bit) | |
287 | tmp[1] |= 0x10; | |
288 | if (lut->m1d[1] & bit) | |
289 | tmp[1] |= 0x20; | |
290 | if (lut->m1d[2] & bit) | |
291 | tmp[1] |= 0x40; | |
292 | if (lut->m1d[3] & bit) | |
293 | tmp[1] |= 0x80; | |
294 | ||
295 | sigma_write_register(WRITE_TRIGGER_SELECT0, tmp, sizeof(tmp)); | |
296 | sigma_set_register(WRITE_TRIGGER_SELECT1, 0x30 | i); | |
297 | } | |
298 | ||
299 | /* Send the parameters */ | |
300 | sigma_write_register(WRITE_TRIGGER_SELECT0, (uint8_t *) &lut->params, | |
301 | sizeof(lut->params)); | |
302 | ||
303 | return SIGROK_OK; | |
304 | } | |
305 | ||
fefa1800 | 306 | /* Generate the bitbang stream for programming the FPGA. */ |
28a35d8a | 307 | static int bin2bitbang(const char *filename, |
fefa1800 | 308 | unsigned char **buf, size_t *buf_size) |
28a35d8a | 309 | { |
fefa1800 | 310 | FILE *f; |
28a35d8a HE |
311 | long file_size; |
312 | unsigned long offset = 0; | |
313 | unsigned char *p; | |
314 | uint8_t *compressed_buf, *firmware; | |
315 | uLongf csize, fwsize; | |
316 | const int buffer_size = 65536; | |
317 | size_t i; | |
fefa1800 UH |
318 | int c, ret, bit, v; |
319 | uint32_t imm = 0x3f6df2ab; | |
28a35d8a | 320 | |
fefa1800 | 321 | f = fopen(filename, "r"); |
28a35d8a HE |
322 | if (!f) { |
323 | g_warning("fopen(\"%s\", \"r\")", filename); | |
324 | return -1; | |
325 | } | |
326 | ||
327 | if (-1 == fseek(f, 0, SEEK_END)) { | |
328 | g_warning("fseek on %s failed", filename); | |
329 | fclose(f); | |
330 | return -1; | |
331 | } | |
332 | ||
333 | file_size = ftell(f); | |
334 | ||
335 | fseek(f, 0, SEEK_SET); | |
336 | ||
28a35d8a HE |
337 | compressed_buf = g_malloc(file_size); |
338 | firmware = g_malloc(buffer_size); | |
339 | ||
340 | if (!compressed_buf || !firmware) { | |
341 | g_warning("Error allocating buffers"); | |
342 | return -1; | |
343 | } | |
344 | ||
28a35d8a HE |
345 | csize = 0; |
346 | while ((c = getc(f)) != EOF) { | |
347 | imm = (imm + 0xa853753) % 177 + (imm * 0x8034052); | |
348 | compressed_buf[csize++] = c ^ imm; | |
349 | } | |
350 | fclose(f); | |
351 | ||
352 | fwsize = buffer_size; | |
353 | ret = uncompress(firmware, &fwsize, compressed_buf, csize); | |
354 | if (ret < 0) { | |
355 | g_free(compressed_buf); | |
356 | g_free(firmware); | |
357 | g_warning("Could not unpack Sigma firmware. (Error %d)\n", ret); | |
358 | return -1; | |
359 | } | |
360 | ||
361 | g_free(compressed_buf); | |
362 | ||
363 | *buf_size = fwsize * 2 * 8; | |
364 | ||
fefa1800 | 365 | *buf = p = (unsigned char *)g_malloc(*buf_size); |
28a35d8a HE |
366 | |
367 | if (!p) { | |
368 | g_warning("Error allocating buffers"); | |
369 | return -1; | |
370 | } | |
371 | ||
372 | for (i = 0; i < fwsize; ++i) { | |
28a35d8a | 373 | for (bit = 7; bit >= 0; --bit) { |
fefa1800 | 374 | v = firmware[i] & 1 << bit ? 0x40 : 0x00; |
28a35d8a HE |
375 | p[offset++] = v | 0x01; |
376 | p[offset++] = v; | |
377 | } | |
378 | } | |
379 | ||
380 | g_free(firmware); | |
381 | ||
382 | if (offset != *buf_size) { | |
383 | g_free(*buf); | |
384 | g_warning("Error reading firmware %s " | |
fefa1800 UH |
385 | "offset=%ld, file_size=%ld, buf_size=%zd\n", |
386 | filename, offset, file_size, *buf_size); | |
28a35d8a HE |
387 | |
388 | return -1; | |
389 | } | |
390 | ||
391 | return 0; | |
392 | } | |
393 | ||
394 | static int hw_init(char *deviceinfo) | |
395 | { | |
396 | struct sigrok_device_instance *sdi; | |
397 | ||
398 | deviceinfo = deviceinfo; | |
399 | ||
400 | ftdi_init(&ftdic); | |
401 | ||
fefa1800 UH |
402 | /* Look for SIGMAs. */ |
403 | if (ftdi_usb_open_desc(&ftdic, USB_VENDOR, USB_PRODUCT, | |
404 | USB_DESCRIPTION, NULL) < 0) | |
28a35d8a HE |
405 | return 0; |
406 | ||
fefa1800 | 407 | /* Register SIGMA device. */ |
28a35d8a HE |
408 | sdi = sigrok_device_instance_new(0, ST_INITIALIZING, |
409 | USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION); | |
410 | if (!sdi) | |
411 | return 0; | |
412 | ||
413 | device_instances = g_slist_append(device_instances, sdi); | |
414 | ||
fefa1800 | 415 | /* We will open the device again when we need it. */ |
28a35d8a HE |
416 | ftdi_usb_close(&ftdic); |
417 | ||
418 | return 1; | |
419 | } | |
420 | ||
f6564c8d | 421 | static int upload_firmware(int firmware_idx) |
28a35d8a HE |
422 | { |
423 | int ret; | |
424 | unsigned char *buf; | |
425 | unsigned char pins; | |
426 | size_t buf_size; | |
28a35d8a | 427 | unsigned char result[32]; |
e8397563 | 428 | char firmware_path[128]; |
28a35d8a | 429 | |
fefa1800 | 430 | /* Make sure it's an ASIX SIGMA. */ |
28a35d8a HE |
431 | if ((ret = ftdi_usb_open_desc(&ftdic, |
432 | USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) { | |
28a35d8a | 433 | g_warning("ftdi_usb_open failed: %s", |
fefa1800 | 434 | ftdi_get_error_string(&ftdic)); |
28a35d8a HE |
435 | return 0; |
436 | } | |
437 | ||
438 | if ((ret = ftdi_set_bitmode(&ftdic, 0xdf, BITMODE_BITBANG)) < 0) { | |
439 | g_warning("ftdi_set_bitmode failed: %s", | |
fefa1800 | 440 | ftdi_get_error_string(&ftdic)); |
28a35d8a HE |
441 | return 0; |
442 | } | |
443 | ||
fefa1800 | 444 | /* Four times the speed of sigmalogan - Works well. */ |
28a35d8a HE |
445 | if ((ret = ftdi_set_baudrate(&ftdic, 750000)) < 0) { |
446 | g_warning("ftdi_set_baudrate failed: %s", | |
fefa1800 | 447 | ftdi_get_error_string(&ftdic)); |
28a35d8a HE |
448 | return 0; |
449 | } | |
450 | ||
fefa1800 | 451 | /* Force the FPGA to reboot. */ |
28a35d8a HE |
452 | sigma_write(suicide, sizeof(suicide)); |
453 | sigma_write(suicide, sizeof(suicide)); | |
454 | sigma_write(suicide, sizeof(suicide)); | |
455 | sigma_write(suicide, sizeof(suicide)); | |
456 | ||
fefa1800 | 457 | /* Prepare to upload firmware (FPGA specific). */ |
28a35d8a HE |
458 | sigma_write(init, sizeof(init)); |
459 | ||
460 | ftdi_usb_purge_buffers(&ftdic); | |
461 | ||
fefa1800 | 462 | /* Wait until the FPGA asserts INIT_B. */ |
28a35d8a HE |
463 | while (1) { |
464 | ret = sigma_read(result, 1); | |
465 | if (result[0] & 0x20) | |
466 | break; | |
467 | } | |
468 | ||
9ddb2a12 | 469 | /* Prepare firmware. */ |
e8397563 | 470 | snprintf(firmware_path, sizeof(firmware_path), "%s/%s", FIRMWARE_DIR, |
f6564c8d HE |
471 | firmware_files[firmware_idx]); |
472 | ||
e8397563 | 473 | if (-1 == bin2bitbang(firmware_path, &buf, &buf_size)) { |
28a35d8a | 474 | g_warning("An error occured while reading the firmware: %s", |
e8397563 | 475 | firmware_path); |
28a35d8a HE |
476 | return SIGROK_ERR; |
477 | } | |
478 | ||
fefa1800 | 479 | /* Upload firmare. */ |
28a35d8a HE |
480 | sigma_write(buf, buf_size); |
481 | ||
482 | g_free(buf); | |
483 | ||
484 | if ((ret = ftdi_set_bitmode(&ftdic, 0x00, BITMODE_RESET)) < 0) { | |
9ddb2a12 | 485 | g_warning("ftdi_set_bitmode failed: %s", |
fefa1800 | 486 | ftdi_get_error_string(&ftdic)); |
28a35d8a HE |
487 | return SIGROK_ERR; |
488 | } | |
489 | ||
490 | ftdi_usb_purge_buffers(&ftdic); | |
491 | ||
fefa1800 | 492 | /* Discard garbage. */ |
28a35d8a HE |
493 | while (1 == sigma_read(&pins, 1)) |
494 | ; | |
495 | ||
fefa1800 | 496 | /* Initialize the logic analyzer mode. */ |
28a35d8a HE |
497 | sigma_write(logic_mode_start, sizeof(logic_mode_start)); |
498 | ||
fefa1800 | 499 | /* Expect a 3 byte reply. */ |
28a35d8a HE |
500 | ret = sigma_read(result, 3); |
501 | if (ret != 3 || | |
502 | result[0] != 0xa6 || result[1] != 0x55 || result[2] != 0xaa) { | |
fefa1800 | 503 | g_warning("Configuration failed. Invalid reply received."); |
28a35d8a HE |
504 | return SIGROK_ERR; |
505 | } | |
506 | ||
f6564c8d HE |
507 | cur_firmware = firmware_idx; |
508 | ||
509 | return SIGROK_OK; | |
510 | } | |
511 | ||
512 | static int hw_opendev(int device_index) | |
513 | { | |
514 | struct sigrok_device_instance *sdi; | |
515 | int ret; | |
516 | ||
9ddb2a12 | 517 | /* Make sure it's an ASIX SIGMA. */ |
f6564c8d HE |
518 | if ((ret = ftdi_usb_open_desc(&ftdic, |
519 | USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) { | |
520 | ||
521 | g_warning("ftdi_usb_open failed: %s", | |
522 | ftdi_get_error_string(&ftdic)); | |
523 | ||
524 | return 0; | |
525 | } | |
28a35d8a HE |
526 | |
527 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
528 | return SIGROK_ERR; | |
529 | ||
530 | sdi->status = ST_ACTIVE; | |
531 | ||
f6564c8d HE |
532 | return SIGROK_OK; |
533 | } | |
534 | ||
6aac7737 HE |
535 | static int set_samplerate(struct sigrok_device_instance *sdi, |
536 | uint64_t samplerate) | |
f6564c8d | 537 | { |
e8397563 | 538 | int i, ret; |
f6564c8d HE |
539 | |
540 | sdi = sdi; | |
541 | ||
542 | for (i = 0; supported_samplerates[i]; i++) { | |
543 | if (supported_samplerates[i] == samplerate) | |
544 | break; | |
545 | } | |
546 | if (supported_samplerates[i] == 0) | |
547 | return SIGROK_ERR_SAMPLERATE; | |
548 | ||
e8397563 HE |
549 | if (samplerate <= MHZ(50)) { |
550 | ret = upload_firmware(0); | |
f78898e9 | 551 | num_probes = 16; |
e8397563 | 552 | } |
f78898e9 | 553 | if (samplerate == MHZ(100)) { |
e8397563 | 554 | ret = upload_firmware(1); |
f78898e9 HE |
555 | num_probes = 8; |
556 | } | |
557 | else if (samplerate == MHZ(200)) { | |
e8397563 | 558 | ret = upload_firmware(2); |
f78898e9 HE |
559 | num_probes = 4; |
560 | } | |
f6564c8d | 561 | |
e8397563 | 562 | cur_samplerate = samplerate; |
f78898e9 | 563 | samples_per_event = 16 / num_probes; |
6aac7737 | 564 | sigma.state = SIGMA_IDLE; |
f6564c8d | 565 | |
28a35d8a HE |
566 | g_message("Firmware uploaded"); |
567 | ||
e8397563 | 568 | return ret; |
28a35d8a HE |
569 | } |
570 | ||
c53d793f HE |
571 | /* |
572 | * In 100 and 200 MHz mode, only a single pin rising/falling can be | |
573 | * set as trigger. In other modes, two rising/falling triggers can be set, | |
574 | * in addition to value/mask trigger for any number of probes. | |
575 | * | |
576 | * The Sigma supports complex triggers using boolean expressions, but this | |
577 | * has not been implemented yet. | |
578 | */ | |
57bbf56b HE |
579 | static int configure_probes(GSList *probes) |
580 | { | |
581 | struct probe *probe; | |
582 | GSList *l; | |
583 | int trigger_set = 0; | |
a42aec7f | 584 | int probebit; |
57bbf56b | 585 | |
c53d793f | 586 | memset(&trigger, 0, sizeof(struct sigma_trigger)); |
eec5275e | 587 | |
57bbf56b HE |
588 | for (l = probes; l; l = l->next) { |
589 | probe = (struct probe *)l->data; | |
a42aec7f | 590 | probebit = 1 << (probe->index - 1); |
57bbf56b HE |
591 | |
592 | if (!probe->enabled || !probe->trigger) | |
593 | continue; | |
594 | ||
ee492173 | 595 | if (cur_samplerate >= MHZ(100)) { |
c53d793f | 596 | /* Fast trigger support. */ |
ee492173 | 597 | if (trigger_set) { |
6aac7737 HE |
598 | g_warning("Asix Sigma only supports a single " |
599 | "pin trigger in 100 and 200 " | |
600 | "MHz mode."); | |
ee492173 HE |
601 | return SIGROK_ERR; |
602 | } | |
603 | if (probe->trigger[0] == 'f') | |
a42aec7f | 604 | trigger.fallingmask |= probebit; |
ee492173 | 605 | else if (probe->trigger[0] == 'r') |
a42aec7f | 606 | trigger.risingmask |= probebit; |
ee492173 HE |
607 | else { |
608 | g_warning("Asix Sigma only supports " | |
609 | "rising/falling trigger in 100 " | |
610 | "and 200 MHz mode."); | |
611 | return SIGROK_ERR; | |
612 | } | |
57bbf56b | 613 | |
c53d793f | 614 | ++trigger_set; |
ee492173 | 615 | } else { |
c53d793f HE |
616 | /* Simple trigger support (event). */ |
617 | if (probe->trigger[0] == '1') { | |
a42aec7f HE |
618 | trigger.simplevalue |= probebit; |
619 | trigger.simplemask |= probebit; | |
c53d793f HE |
620 | } |
621 | else if (probe->trigger[0] == '0') { | |
a42aec7f HE |
622 | trigger.simplevalue &= ~probebit; |
623 | trigger.simplemask |= probebit; | |
c53d793f HE |
624 | } |
625 | else if (probe->trigger[0] == 'f') { | |
a42aec7f | 626 | trigger.fallingmask |= probebit; |
c53d793f HE |
627 | ++trigger_set; |
628 | } | |
629 | else if (probe->trigger[0] == 'r') { | |
a42aec7f | 630 | trigger.risingmask |= probebit; |
c53d793f HE |
631 | ++trigger_set; |
632 | } | |
ee492173 | 633 | |
c53d793f HE |
634 | if (trigger_set > 2) { |
635 | g_warning("Asix Sigma only supports 2 rising/" | |
636 | "falling triggers."); | |
ee492173 HE |
637 | return SIGROK_ERR; |
638 | } | |
ee492173 | 639 | } |
57bbf56b HE |
640 | } |
641 | ||
642 | return SIGROK_OK; | |
643 | } | |
644 | ||
28a35d8a HE |
645 | static void hw_closedev(int device_index) |
646 | { | |
9be9893e | 647 | struct sigrok_device_instance *sdi; |
28a35d8a | 648 | |
9be9893e HE |
649 | if ((sdi = get_sigrok_device_instance(device_instances, device_index))) |
650 | { | |
651 | if (sdi->status == ST_ACTIVE) | |
652 | ftdi_usb_close(&ftdic); | |
653 | ||
654 | sdi->status = ST_INACTIVE; | |
655 | } | |
28a35d8a HE |
656 | } |
657 | ||
28a35d8a HE |
658 | static void hw_cleanup(void) |
659 | { | |
660 | } | |
661 | ||
28a35d8a HE |
662 | static void *hw_get_device_info(int device_index, int device_info_id) |
663 | { | |
664 | struct sigrok_device_instance *sdi; | |
665 | void *info = NULL; | |
666 | ||
667 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) { | |
668 | fprintf(stderr, "It's NULL.\n"); | |
669 | return NULL; | |
670 | } | |
671 | ||
672 | switch (device_info_id) { | |
673 | case DI_INSTANCE: | |
674 | info = sdi; | |
675 | break; | |
676 | case DI_NUM_PROBES: | |
edca2c5c | 677 | info = GINT_TO_POINTER(16); |
28a35d8a HE |
678 | break; |
679 | case DI_SAMPLERATES: | |
680 | info = &samplerates; | |
681 | break; | |
682 | case DI_TRIGGER_TYPES: | |
57bbf56b | 683 | info = (char *)TRIGGER_TYPES; |
28a35d8a HE |
684 | break; |
685 | case DI_CUR_SAMPLERATE: | |
686 | info = &cur_samplerate; | |
687 | break; | |
688 | } | |
689 | ||
690 | return info; | |
691 | } | |
692 | ||
28a35d8a HE |
693 | static int hw_get_status(int device_index) |
694 | { | |
695 | struct sigrok_device_instance *sdi; | |
696 | ||
697 | sdi = get_sigrok_device_instance(device_instances, device_index); | |
698 | if (sdi) | |
699 | return sdi->status; | |
700 | else | |
701 | return ST_NOT_FOUND; | |
702 | } | |
703 | ||
28a35d8a HE |
704 | static int *hw_get_capabilities(void) |
705 | { | |
706 | return capabilities; | |
707 | } | |
708 | ||
709 | static int hw_set_configuration(int device_index, int capability, void *value) | |
710 | { | |
711 | struct sigrok_device_instance *sdi; | |
712 | int ret; | |
f6564c8d | 713 | |
28a35d8a HE |
714 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) |
715 | return SIGROK_ERR; | |
716 | ||
717 | if (capability == HWCAP_SAMPLERATE) { | |
f6564c8d | 718 | ret = set_samplerate(sdi, *(uint64_t*) value); |
28a35d8a | 719 | } else if (capability == HWCAP_PROBECONFIG) { |
57bbf56b | 720 | ret = configure_probes(value); |
28a35d8a HE |
721 | } else if (capability == HWCAP_LIMIT_MSEC) { |
722 | limit_msec = strtoull(value, NULL, 10); | |
723 | ret = SIGROK_OK; | |
57bbf56b HE |
724 | } else if (capability == HWCAP_CAPTURE_RATIO) { |
725 | capture_ratio = strtoull(value, NULL, 10); | |
726 | ret = SIGROK_OK; | |
727 | } else if (capability == HWCAP_PROBECONFIG) { | |
728 | ret = configure_probes((GSList *) value); | |
28a35d8a HE |
729 | } else { |
730 | ret = SIGROK_ERR; | |
731 | } | |
732 | ||
733 | return ret; | |
734 | } | |
735 | ||
36b1c8e6 HE |
736 | /* Software trigger to determine exact trigger position. */ |
737 | static int get_trigger_offset(uint16_t *samples, uint16_t last_sample, | |
738 | struct sigma_trigger *t) | |
739 | { | |
740 | int i; | |
741 | ||
742 | for (i = 0; i < 8; ++i) { | |
743 | if (i > 0) | |
744 | last_sample = samples[i-1]; | |
745 | ||
746 | /* Simple triggers. */ | |
747 | if ((samples[i] & t->simplemask) != t->simplevalue) | |
748 | continue; | |
749 | ||
750 | /* Rising edge. */ | |
751 | if ((last_sample & t->risingmask) != 0 || (samples[i] & | |
752 | t->risingmask) != t->risingmask) | |
753 | continue; | |
754 | ||
755 | /* Falling edge. */ | |
bdfc7a89 HE |
756 | if ((last_sample & t->fallingmask) != t->fallingmask || |
757 | (samples[i] & t->fallingmask) != 0) | |
36b1c8e6 HE |
758 | continue; |
759 | ||
760 | break; | |
761 | } | |
762 | ||
763 | /* If we did not match, return original trigger pos. */ | |
764 | return i & 0x7; | |
765 | } | |
766 | ||
28a35d8a | 767 | /* |
fefa1800 UH |
768 | * Decode chunk of 1024 bytes, 64 clusters, 7 events per cluster. |
769 | * Each event is 20ns apart, and can contain multiple samples. | |
f78898e9 HE |
770 | * |
771 | * For 200 MHz, events contain 4 samples for each channel, spread 5 ns apart. | |
772 | * For 100 MHz, events contain 2 samples for each channel, spread 10 ns apart. | |
773 | * For 50 MHz and below, events contain one sample for each channel, | |
774 | * spread 20 ns apart. | |
28a35d8a HE |
775 | */ |
776 | static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts, | |
57bbf56b | 777 | uint16_t *lastsample, int triggerpos, void *user_data) |
28a35d8a | 778 | { |
fefa1800 | 779 | uint16_t tsdiff, ts; |
f78898e9 | 780 | uint16_t samples[65536 * samples_per_event]; |
28a35d8a | 781 | struct datafeed_packet packet; |
f78898e9 | 782 | int i, j, k, l, numpad, tosend; |
fefa1800 | 783 | size_t n = 0, sent = 0; |
f78898e9 | 784 | int clustersize = EVENTS_PER_CLUSTER * samples_per_event; |
fefa1800 | 785 | uint16_t *event; |
f78898e9 | 786 | uint16_t cur_sample; |
57bbf56b | 787 | int triggerts = -1; |
ee492173 | 788 | |
4ae1f451 | 789 | /* Check if trigger is in this chunk. */ |
ee492173 HE |
790 | if (triggerpos != -1) { |
791 | if (cur_samplerate <= MHZ(50)) | |
36b1c8e6 | 792 | triggerpos -= EVENTS_PER_CLUSTER - 1; |
ee492173 HE |
793 | |
794 | if (triggerpos < 0) | |
795 | triggerpos = 0; | |
57bbf56b | 796 | |
ee492173 HE |
797 | /* Find in which cluster the trigger occured. */ |
798 | triggerts = triggerpos / 7; | |
799 | } | |
28a35d8a | 800 | |
eec5275e | 801 | /* For each ts. */ |
28a35d8a | 802 | for (i = 0; i < 64; ++i) { |
fefa1800 | 803 | ts = *(uint16_t *) &buf[i * 16]; |
28a35d8a HE |
804 | tsdiff = ts - *lastts; |
805 | *lastts = ts; | |
806 | ||
fefa1800 UH |
807 | /* Pad last sample up to current point. */ |
808 | numpad = tsdiff * samples_per_event - clustersize; | |
28a35d8a | 809 | if (numpad > 0) { |
f78898e9 HE |
810 | for (j = 0; j < numpad; ++j) |
811 | samples[j] = *lastsample; | |
812 | ||
813 | n = numpad; | |
28a35d8a HE |
814 | } |
815 | ||
57bbf56b HE |
816 | /* Send samples between previous and this timestamp to sigrok. */ |
817 | sent = 0; | |
818 | while (sent < n) { | |
819 | tosend = MIN(2048, n - sent); | |
820 | ||
4c046c6b | 821 | packet.type = DF_LOGIC; |
57bbf56b | 822 | packet.length = tosend * sizeof(uint16_t); |
4c046c6b | 823 | packet.unitsize = 2; |
57bbf56b HE |
824 | packet.payload = samples + sent; |
825 | session_bus(user_data, &packet); | |
28a35d8a | 826 | |
57bbf56b HE |
827 | sent += tosend; |
828 | } | |
829 | n = 0; | |
830 | ||
831 | event = (uint16_t *) &buf[i * 16 + 2]; | |
f78898e9 HE |
832 | cur_sample = 0; |
833 | ||
834 | /* For each event in cluster. */ | |
28a35d8a | 835 | for (j = 0; j < 7; ++j) { |
f78898e9 HE |
836 | |
837 | /* For each sample in event. */ | |
28a35d8a | 838 | for (k = 0; k < samples_per_event; ++k) { |
f78898e9 HE |
839 | cur_sample = 0; |
840 | ||
841 | /* For each probe. */ | |
842 | for (l = 0; l < num_probes; ++l) | |
edca2c5c HE |
843 | cur_sample |= (!!(event[j] & (1 << (l * |
844 | samples_per_event + k)))) | |
845 | << l; | |
f78898e9 HE |
846 | |
847 | samples[n++] = cur_sample; | |
28a35d8a HE |
848 | } |
849 | } | |
850 | ||
eec5275e | 851 | /* Send data up to trigger point (if triggered). */ |
fefa1800 | 852 | sent = 0; |
57bbf56b HE |
853 | if (i == triggerts) { |
854 | /* | |
36b1c8e6 HE |
855 | * Trigger is not always accurate to sample because of |
856 | * pipeline delay. However, it always triggers before | |
857 | * the actual event. We therefore look at the next | |
858 | * samples to pinpoint the exact position of the trigger. | |
57bbf56b | 859 | */ |
bdfc7a89 HE |
860 | tosend = get_trigger_offset(samples, *lastsample, |
861 | &trigger); | |
57bbf56b HE |
862 | |
863 | if (tosend > 0) { | |
4c046c6b | 864 | packet.type = DF_LOGIC; |
57bbf56b | 865 | packet.length = tosend * sizeof(uint16_t); |
4c046c6b | 866 | packet.unitsize = 2; |
57bbf56b HE |
867 | packet.payload = samples; |
868 | session_bus(user_data, &packet); | |
869 | ||
870 | sent += tosend; | |
871 | } | |
28a35d8a | 872 | |
57bbf56b HE |
873 | packet.type = DF_TRIGGER; |
874 | packet.length = 0; | |
875 | packet.payload = 0; | |
28a35d8a | 876 | session_bus(user_data, &packet); |
28a35d8a | 877 | } |
57bbf56b | 878 | |
eec5275e | 879 | /* Send rest of the chunk to sigrok. */ |
57bbf56b HE |
880 | tosend = n - sent; |
881 | ||
4c046c6b | 882 | packet.type = DF_LOGIC; |
57bbf56b | 883 | packet.length = tosend * sizeof(uint16_t); |
4c046c6b | 884 | packet.unitsize = 2; |
57bbf56b HE |
885 | packet.payload = samples + sent; |
886 | session_bus(user_data, &packet); | |
ee492173 HE |
887 | |
888 | *lastsample = samples[n - 1]; | |
28a35d8a HE |
889 | } |
890 | ||
f78898e9 | 891 | return SIGROK_OK; |
28a35d8a HE |
892 | } |
893 | ||
894 | static int receive_data(int fd, int revents, void *user_data) | |
895 | { | |
896 | struct datafeed_packet packet; | |
28a35d8a HE |
897 | const int chunks_per_read = 32; |
898 | unsigned char buf[chunks_per_read * CHUNK_SIZE]; | |
6aac7737 HE |
899 | int bufsz, numchunks, i, newchunks; |
900 | uint32_t running_msec; | |
28a35d8a | 901 | struct timeval tv; |
28a35d8a HE |
902 | |
903 | fd = fd; | |
904 | revents = revents; | |
905 | ||
6aac7737 | 906 | numchunks = sigma.stoppos / 512; |
28a35d8a | 907 | |
6aac7737 | 908 | if (sigma.state == SIGMA_IDLE) |
28a35d8a HE |
909 | return FALSE; |
910 | ||
6aac7737 | 911 | if (sigma.state == SIGMA_CAPTURE) { |
28a35d8a | 912 | |
6aac7737 HE |
913 | /* Check if the timer has expired, or memory is full. */ |
914 | gettimeofday(&tv, 0); | |
915 | running_msec = (tv.tv_sec - start_tv.tv_sec) * 1000 + | |
916 | (tv.tv_usec - start_tv.tv_usec) / 1000; | |
28a35d8a | 917 | |
6aac7737 HE |
918 | if (running_msec < limit_msec && numchunks < 32767) |
919 | return FALSE; | |
28a35d8a | 920 | |
6aac7737 HE |
921 | hw_stop_acquisition(-1, user_data); |
922 | ||
923 | return FALSE; | |
924 | ||
925 | } else if (sigma.state == SIGMA_DOWNLOAD) { | |
926 | if (sigma.chunks_downloaded >= numchunks) { | |
927 | /* End of samples. */ | |
928 | packet.type = DF_END; | |
929 | packet.length = 0; | |
930 | session_bus(user_data, &packet); | |
931 | ||
932 | sigma.state = SIGMA_IDLE; | |
f78898e9 | 933 | |
6aac7737 HE |
934 | return TRUE; |
935 | } | |
936 | ||
937 | newchunks = MIN(chunks_per_read, | |
938 | numchunks - sigma.chunks_downloaded); | |
28a35d8a HE |
939 | |
940 | g_message("Downloading sample data: %.0f %%", | |
6aac7737 | 941 | 100.0 * sigma.chunks_downloaded / numchunks); |
28a35d8a | 942 | |
6aac7737 HE |
943 | bufsz = sigma_read_dram(sigma.chunks_downloaded, |
944 | newchunks, buf); | |
28a35d8a | 945 | |
fefa1800 | 946 | /* Find first ts. */ |
6aac7737 HE |
947 | if (sigma.chunks_downloaded == 0) { |
948 | sigma.lastts = *(uint16_t *) buf - 1; | |
949 | sigma.lastsample = 0; | |
950 | } | |
28a35d8a | 951 | |
fefa1800 | 952 | /* Decode chunks and send them to sigrok. */ |
28a35d8a | 953 | for (i = 0; i < newchunks; ++i) { |
6aac7737 | 954 | if (sigma.chunks_downloaded + i == sigma.triggerchunk) |
57bbf56b | 955 | decode_chunk_ts(buf + (i * CHUNK_SIZE), |
6aac7737 HE |
956 | &sigma.lastts, &sigma.lastsample, |
957 | sigma.triggerpos & 0x1ff, | |
958 | user_data); | |
57bbf56b HE |
959 | else |
960 | decode_chunk_ts(buf + (i * CHUNK_SIZE), | |
6aac7737 | 961 | &sigma.lastts, &sigma.lastsample, |
57bbf56b | 962 | -1, user_data); |
28a35d8a HE |
963 | } |
964 | ||
6aac7737 | 965 | sigma.chunks_downloaded += newchunks; |
28a35d8a HE |
966 | } |
967 | ||
28a35d8a HE |
968 | return TRUE; |
969 | } | |
970 | ||
c53d793f HE |
971 | /* Build a LUT entry used by the trigger functions. */ |
972 | static void build_lut_entry(uint16_t value, uint16_t mask, uint16_t *entry) | |
ee492173 HE |
973 | { |
974 | int i, j, k, bit; | |
975 | ||
f758d074 | 976 | /* For each quad probe. */ |
ee492173 | 977 | for (i = 0; i < 4; ++i) { |
c53d793f | 978 | entry[i] = 0xffff; |
ee492173 | 979 | |
f758d074 | 980 | /* For each bit in LUT. */ |
ee492173 HE |
981 | for (j = 0; j < 16; ++j) |
982 | ||
f758d074 | 983 | /* For each probe in quad. */ |
ee492173 HE |
984 | for (k = 0; k < 4; ++k) { |
985 | bit = 1 << (i * 4 + k); | |
986 | ||
c53d793f HE |
987 | /* Set bit in entry */ |
988 | if ((mask & bit) && | |
989 | ((!(value & bit)) != | |
4ae1f451 | 990 | (!(j & (1 << k))))) |
c53d793f | 991 | entry[i] &= ~(1 << j); |
ee492173 HE |
992 | } |
993 | } | |
c53d793f | 994 | } |
ee492173 | 995 | |
c53d793f HE |
996 | /* Add a logical function to LUT mask. */ |
997 | static void add_trigger_function(enum triggerop oper, enum triggerfunc func, | |
998 | int index, int neg, uint16_t *mask) | |
999 | { | |
1000 | int i, j; | |
1001 | int x[2][2], tmp, a, b, aset, bset, rset; | |
1002 | ||
1003 | memset(x, 0, 4 * sizeof(int)); | |
1004 | ||
1005 | /* Trigger detect condition. */ | |
1006 | switch (oper) { | |
1007 | case OP_LEVEL: | |
1008 | x[0][1] = 1; | |
1009 | x[1][1] = 1; | |
1010 | break; | |
1011 | case OP_NOT: | |
1012 | x[0][0] = 1; | |
1013 | x[1][0] = 1; | |
1014 | break; | |
1015 | case OP_RISE: | |
1016 | x[0][1] = 1; | |
1017 | break; | |
1018 | case OP_FALL: | |
1019 | x[1][0] = 1; | |
1020 | break; | |
1021 | case OP_RISEFALL: | |
1022 | x[0][1] = 1; | |
1023 | x[1][0] = 1; | |
1024 | break; | |
1025 | case OP_NOTRISE: | |
1026 | x[1][1] = 1; | |
1027 | x[0][0] = 1; | |
1028 | x[1][0] = 1; | |
1029 | break; | |
1030 | case OP_NOTFALL: | |
1031 | x[1][1] = 1; | |
1032 | x[0][0] = 1; | |
1033 | x[0][1] = 1; | |
1034 | break; | |
1035 | case OP_NOTRISEFALL: | |
1036 | x[1][1] = 1; | |
1037 | x[0][0] = 1; | |
1038 | break; | |
1039 | } | |
1040 | ||
1041 | /* Transpose if neg is set. */ | |
1042 | if (neg) { | |
1043 | for (i = 0; i < 2; ++i) | |
1044 | for (j = 0; j < 2; ++j) { | |
1045 | tmp = x[i][j]; | |
1046 | x[i][j] = x[1-i][1-j]; | |
1047 | x[1-i][1-j] = tmp; | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | /* Update mask with function. */ | |
1052 | for (i = 0; i < 16; ++i) { | |
1053 | a = (i >> (2 * index + 0)) & 1; | |
1054 | b = (i >> (2 * index + 1)) & 1; | |
1055 | ||
1056 | aset = (*mask >> i) & 1; | |
1057 | bset = x[b][a]; | |
1058 | ||
1059 | if (func == FUNC_AND || func == FUNC_NAND) | |
1060 | rset = aset & bset; | |
1061 | else if (func == FUNC_OR || func == FUNC_NOR) | |
1062 | rset = aset | bset; | |
1063 | else if (func == FUNC_XOR || func == FUNC_NXOR) | |
1064 | rset = aset ^ bset; | |
1065 | ||
1066 | if (func == FUNC_NAND || func == FUNC_NOR || func == FUNC_NXOR) | |
1067 | rset = !rset; | |
1068 | ||
1069 | *mask &= ~(1 << i); | |
1070 | ||
1071 | if (rset) | |
1072 | *mask |= 1 << i; | |
1073 | } | |
1074 | } | |
1075 | ||
1076 | /* | |
1077 | * Build trigger LUTs used by 50 MHz and lower sample rates for supporting | |
1078 | * simple pin change and state triggers. Only two transitions (rise/fall) can be | |
1079 | * set at any time, but a full mask and value can be set (0/1). | |
1080 | */ | |
1081 | static int build_basic_trigger(struct triggerlut *lut) | |
1082 | { | |
1083 | int i,j; | |
4ae1f451 | 1084 | uint16_t masks[2] = { 0, 0 }; |
c53d793f HE |
1085 | |
1086 | memset(lut, 0, sizeof(struct triggerlut)); | |
1087 | ||
1088 | /* Contant for simple triggers. */ | |
1089 | lut->m4 = 0xa000; | |
1090 | ||
1091 | /* Value/mask trigger support. */ | |
1092 | build_lut_entry(trigger.simplevalue, trigger.simplemask, lut->m2d); | |
1093 | ||
1094 | /* Rise/fall trigger support. */ | |
1095 | for (i = 0, j = 0; i < 16; ++i) { | |
1096 | if (trigger.risingmask & (1 << i) || | |
1097 | trigger.fallingmask & (1 << i)) | |
1098 | masks[j++] = 1 << i; | |
1099 | } | |
1100 | ||
1101 | build_lut_entry(masks[0], masks[0], lut->m0d); | |
1102 | build_lut_entry(masks[1], masks[1], lut->m1d); | |
1103 | ||
1104 | /* Add glue logic */ | |
1105 | if (masks[0] || masks[1]) { | |
1106 | /* Transition trigger. */ | |
1107 | if (masks[0] & trigger.risingmask) | |
1108 | add_trigger_function(OP_RISE, FUNC_OR, 0, 0, &lut->m3); | |
1109 | if (masks[0] & trigger.fallingmask) | |
1110 | add_trigger_function(OP_FALL, FUNC_OR, 0, 0, &lut->m3); | |
1111 | if (masks[1] & trigger.risingmask) | |
1112 | add_trigger_function(OP_RISE, FUNC_OR, 1, 0, &lut->m3); | |
1113 | if (masks[1] & trigger.fallingmask) | |
1114 | add_trigger_function(OP_FALL, FUNC_OR, 1, 0, &lut->m3); | |
1115 | } else { | |
1116 | /* Only value/mask trigger. */ | |
1117 | lut->m3 = 0xffff; | |
1118 | } | |
ee492173 | 1119 | |
c53d793f | 1120 | /* Triggertype: event. */ |
ee492173 HE |
1121 | lut->params.selres = 3; |
1122 | ||
1123 | return SIGROK_OK; | |
1124 | } | |
1125 | ||
28a35d8a HE |
1126 | static int hw_start_acquisition(int device_index, gpointer session_device_id) |
1127 | { | |
1128 | struct sigrok_device_instance *sdi; | |
1129 | struct datafeed_packet packet; | |
1130 | struct datafeed_header header; | |
9ddb2a12 UH |
1131 | struct clockselect_50 clockselect; |
1132 | int frac; | |
57bbf56b HE |
1133 | uint8_t triggerselect; |
1134 | struct triggerinout triggerinout_conf; | |
ee492173 | 1135 | struct triggerlut lut; |
a42aec7f | 1136 | int triggerpin; |
28a35d8a HE |
1137 | |
1138 | session_device_id = session_device_id; | |
1139 | ||
1140 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
1141 | return SIGROK_ERR; | |
1142 | ||
1143 | device_index = device_index; | |
1144 | ||
ed09fd07 | 1145 | /* If the samplerate has not been set, default to 50 MHz. */ |
9ddb2a12 | 1146 | if (cur_firmware == -1) |
ed09fd07 | 1147 | set_samplerate(sdi, MHZ(50)); |
e8397563 | 1148 | |
eec5275e | 1149 | /* Enter trigger programming mode. */ |
28a35d8a HE |
1150 | sigma_set_register(WRITE_TRIGGER_SELECT1, 0x20); |
1151 | ||
eec5275e | 1152 | /* 100 and 200 MHz mode. */ |
57bbf56b HE |
1153 | if (cur_samplerate >= MHZ(100)) { |
1154 | sigma_set_register(WRITE_TRIGGER_SELECT1, 0x81); | |
1155 | ||
a42aec7f HE |
1156 | /* Find which pin to trigger on from mask. */ |
1157 | for (triggerpin = 0; triggerpin < 8; ++triggerpin) | |
1158 | if ((trigger.risingmask | trigger.fallingmask) & | |
1159 | (1 << triggerpin)) | |
1160 | break; | |
1161 | ||
1162 | /* Set trigger pin and light LED on trigger. */ | |
1163 | triggerselect = (1 << LEDSEL1) | (triggerpin & 0x7); | |
1164 | ||
1165 | /* Default rising edge. */ | |
1166 | if (trigger.fallingmask) | |
1167 | triggerselect |= 1 << 3; | |
57bbf56b | 1168 | |
eec5275e | 1169 | /* All other modes. */ |
57bbf56b | 1170 | } else if (cur_samplerate <= MHZ(50)) { |
ee492173 HE |
1171 | build_basic_trigger(&lut); |
1172 | ||
1173 | sigma_write_trigger_lut(&lut); | |
57bbf56b HE |
1174 | |
1175 | triggerselect = (1 << LEDSEL1) | (1 << LEDSEL0); | |
1176 | } | |
1177 | ||
eec5275e | 1178 | /* Setup trigger in and out pins to default values. */ |
57bbf56b HE |
1179 | memset(&triggerinout_conf, 0, sizeof(struct triggerinout)); |
1180 | triggerinout_conf.trgout_bytrigger = 1; | |
1181 | triggerinout_conf.trgout_enable = 1; | |
1182 | ||
28a35d8a | 1183 | sigma_write_register(WRITE_TRIGGER_OPTION, |
57bbf56b HE |
1184 | (uint8_t *) &triggerinout_conf, |
1185 | sizeof(struct triggerinout)); | |
28a35d8a | 1186 | |
eec5275e | 1187 | /* Go back to normal mode. */ |
57bbf56b | 1188 | sigma_set_register(WRITE_TRIGGER_SELECT1, triggerselect); |
28a35d8a | 1189 | |
edca2c5c HE |
1190 | /* Set clock select register. */ |
1191 | if (cur_samplerate == MHZ(200)) | |
1192 | /* Enable 4 probes. */ | |
1193 | sigma_set_register(WRITE_CLOCK_SELECT, 0xf0); | |
1194 | else if (cur_samplerate == MHZ(100)) | |
1195 | /* Enable 8 probes. */ | |
1196 | sigma_set_register(WRITE_CLOCK_SELECT, 0x00); | |
1197 | else { | |
1198 | /* | |
9ddb2a12 | 1199 | * 50 MHz mode (or fraction thereof). Any fraction down to |
eec5275e | 1200 | * 50 MHz / 256 can be used, but is not supported by sigrok API. |
edca2c5c | 1201 | */ |
9ddb2a12 | 1202 | frac = MHZ(50) / cur_samplerate - 1; |
edca2c5c | 1203 | |
9ddb2a12 UH |
1204 | clockselect.async = 0; |
1205 | clockselect.fraction = frac; | |
1206 | clockselect.disabled_probes = 0; | |
edca2c5c HE |
1207 | |
1208 | sigma_write_register(WRITE_CLOCK_SELECT, | |
9ddb2a12 UH |
1209 | (uint8_t *) &clockselect, |
1210 | sizeof(clockselect)); | |
edca2c5c HE |
1211 | } |
1212 | ||
fefa1800 | 1213 | /* Setup maximum post trigger time. */ |
11fc8d9d | 1214 | sigma_set_register(WRITE_POST_TRIGGER, (capture_ratio * 255) / 100); |
28a35d8a | 1215 | |
eec5275e | 1216 | /* Start acqusition. */ |
28a35d8a HE |
1217 | gettimeofday(&start_tv, 0); |
1218 | sigma_set_register(WRITE_MODE, 0x0d); | |
1219 | ||
28a35d8a HE |
1220 | /* Send header packet to the session bus. */ |
1221 | packet.type = DF_HEADER; | |
1222 | packet.length = sizeof(struct datafeed_header); | |
1223 | packet.payload = &header; | |
1224 | header.feed_version = 1; | |
1225 | gettimeofday(&header.starttime, NULL); | |
1226 | header.samplerate = cur_samplerate; | |
1227 | header.protocol_id = PROTO_RAW; | |
c2616fb9 DR |
1228 | header.num_logic_probes = num_probes; |
1229 | header.num_analog_probes = 0; | |
28a35d8a HE |
1230 | session_bus(session_device_id, &packet); |
1231 | ||
57bbf56b HE |
1232 | /* Add capture source. */ |
1233 | source_add(0, G_IO_IN, 10, receive_data, session_device_id); | |
1234 | ||
6aac7737 HE |
1235 | sigma.state = SIGMA_CAPTURE; |
1236 | ||
28a35d8a HE |
1237 | return SIGROK_OK; |
1238 | } | |
1239 | ||
28a35d8a HE |
1240 | static void hw_stop_acquisition(int device_index, gpointer session_device_id) |
1241 | { | |
6aac7737 HE |
1242 | uint8_t modestatus; |
1243 | ||
28a35d8a HE |
1244 | device_index = device_index; |
1245 | session_device_id = session_device_id; | |
1246 | ||
fefa1800 | 1247 | /* Stop acquisition. */ |
28a35d8a HE |
1248 | sigma_set_register(WRITE_MODE, 0x11); |
1249 | ||
6aac7737 HE |
1250 | /* Set SDRAM Read Enable. */ |
1251 | sigma_set_register(WRITE_MODE, 0x02); | |
1252 | ||
1253 | /* Get the current position. */ | |
1254 | sigma_read_pos(&sigma.stoppos, &sigma.triggerpos); | |
1255 | ||
1256 | /* Check if trigger has fired. */ | |
1257 | modestatus = sigma_get_register(READ_MODE); | |
1258 | if (modestatus & 0x20) { | |
1259 | sigma.triggerchunk = sigma.triggerpos / 512; | |
1260 | ||
1261 | } else | |
1262 | sigma.triggerchunk = -1; | |
1263 | ||
1264 | sigma.chunks_downloaded = 0; | |
1265 | ||
1266 | sigma.state = SIGMA_DOWNLOAD; | |
28a35d8a HE |
1267 | } |
1268 | ||
28a35d8a HE |
1269 | struct device_plugin asix_sigma_plugin_info = { |
1270 | "asix-sigma", | |
1271 | 1, | |
1272 | hw_init, | |
1273 | hw_cleanup, | |
28a35d8a HE |
1274 | hw_opendev, |
1275 | hw_closedev, | |
1276 | hw_get_device_info, | |
1277 | hw_get_status, | |
1278 | hw_get_capabilities, | |
1279 | hw_set_configuration, | |
1280 | hw_start_acquisition, | |
9ddb2a12 | 1281 | hw_stop_acquisition, |
28a35d8a | 1282 | }; |