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