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