]>
Commit | Line | Data |
---|---|---|
f2cd2deb FS |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
7047acc8 | 4 | * Copyright (C) 2022 Gerhard Sittig <gerhard.sittig@gmx.net> |
f2cd2deb FS |
5 | * Copyright (C) 2020 Florian Schmidt <schmidt_florian@gmx.de> |
6 | * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se> | |
7 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
8 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
9 | * | |
10 | * This program is free software: you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation, either version 3 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
22 | */ | |
23 | ||
24 | #include <config.h> | |
a7740b06 | 25 | |
f2cd2deb | 26 | #include <libsigrok/libsigrok.h> |
a7740b06 GS |
27 | #include <string.h> |
28 | ||
f2cd2deb FS |
29 | #include "libsigrok-internal.h" |
30 | #include "protocol.h" | |
31 | ||
d466f61c GS |
32 | /* USB PID dependent MCU firmware. Model dependent FPGA bitstream. */ |
33 | #define MCU_FWFILE_FMT "kingst-la-%04x.fw" | |
34 | #define FPGA_FWFILE_FMT "kingst-%s-fpga.bitstream" | |
35 | ||
36 | /* | |
37 | * List of supported devices and their features. See @ref kingst_model | |
38 | * for the fields' type and meaning. Table is sorted by EEPROM magic. | |
39 | * | |
40 | * TODO | |
41 | * - Below LA1016 properties were guessed, need verification. | |
42 | * - Add LA5016 and LA5032 devices when their EEPROM magic is known. | |
43 | * - Does LA1010 fit the driver implementation? Samplerates vary with | |
44 | * channel counts, lack of local sample memory. Most probably not. | |
45 | */ | |
46 | static const struct kingst_model models[] = { | |
47 | { 2, "LA2016", "la2016", SR_MHZ(200), 16, 1, }, | |
48 | { 3, "LA1016", "la1016", SR_MHZ(100), 16, 1, }, | |
49 | { 8, "LA2016", "la2016a1", SR_MHZ(200), 16, 1, }, | |
50 | { 9, "LA1016", "la1016a1", SR_MHZ(100), 16, 1, }, | |
51 | }; | |
f2cd2deb | 52 | |
96dc954e | 53 | /* USB vendor class control requests, executed by the Cypress FX2 MCU. */ |
84fe94bd | 54 | #define CMD_FPGA_ENABLE 0x10 |
96dc954e GS |
55 | #define CMD_FPGA_SPI 0x20 /* R/W access to FPGA registers via SPI. */ |
56 | #define CMD_BULK_START 0x30 /* Start sample data download via USB EP6 IN. */ | |
57 | #define CMD_BULK_RESET 0x38 /* Flush FIFO of FX2 USB EP6 IN. */ | |
58 | #define CMD_FPGA_INIT 0x50 /* Used before and after FPGA bitstream upload. */ | |
59 | #define CMD_KAUTH 0x60 /* Communicate to auth IC (U10). Not used. */ | |
60 | #define CMD_EEPROM 0xa2 /* R/W access to EEPROM content. */ | |
00849545 | 61 | |
42f6dd55 | 62 | /* |
96dc954e GS |
63 | * FPGA register addresses (base addresses when registers span multiple |
64 | * bytes, in that case data is kept in little endian format). Passed to | |
65 | * CMD_FPGA_SPI requests. The FX2 MCU transparently handles the detail | |
66 | * of SPI transfers encoding the read (1) or write (0) direction in the | |
67 | * MSB of the address field. There are some 60 byte-wide FPGA registers. | |
d6f89d4b GS |
68 | * |
69 | * Unfortunately the FPGA registers change their meaning between the | |
70 | * read and write directions of access, or exclusively provide one of | |
71 | * these directions and not the other. This is an arbitrary vendor's | |
72 | * choice, there is nothing which the sigrok driver could do about it. | |
73 | * Values written to registers typically cannot get read back, neither | |
74 | * verified after writing a configuration, nor queried upon startup for | |
75 | * automatic detection of the current configuration. Neither appear to | |
76 | * be there echo registers for presence and communication checks, nor | |
77 | * version identifying registers, as far as we know. | |
42f6dd55 | 78 | */ |
96dc954e GS |
79 | #define REG_RUN 0x00 /* Read capture status, write start capture. */ |
80 | #define REG_PWM_EN 0x02 /* User PWM channels on/off. */ | |
81 | #define REG_CAPT_MODE 0x03 /* Write 0x00 capture to SDRAM, 0x01 streaming. */ | |
82 | #define REG_BULK 0x08 /* Write start addr, byte count to download samples. */ | |
83 | #define REG_SAMPLING 0x10 /* Write capture config, read capture SDRAM location. */ | |
3ab60908 GS |
84 | #define REG_TRIGGER 0x20 /* Write level and edge trigger config. */ |
85 | #define REG_UNKNOWN_30 0x30 | |
96dc954e GS |
86 | #define REG_THRESHOLD 0x68 /* Write PWM config to setup input threshold DAC. */ |
87 | #define REG_PWM1 0x70 /* Write config for user PWM1. */ | |
88 | #define REG_PWM2 0x78 /* Write config for user PWM2. */ | |
f2cd2deb | 89 | |
972d191b GS |
90 | /* Bit patterns to write to REG_CAPT_MODE. */ |
91 | #define CAPTMODE_TO_RAM 0x00 | |
92 | #define CAPTMODE_STREAM 0x01 | |
93 | ||
852c7d14 GS |
94 | /* Bit patterns to write to REG_RUN, setup run mode. */ |
95 | #define RUNMODE_HALT 0x00 | |
96 | #define RUNMODE_RUN 0x03 | |
97 | ||
b711fd8e GS |
98 | /* Bit patterns when reading from REG_RUN, get run state. */ |
99 | #define RUNSTATE_IDLE_BIT (1UL << 0) | |
100 | #define RUNSTATE_DRAM_BIT (1UL << 1) | |
101 | #define RUNSTATE_TRGD_BIT (1UL << 2) | |
102 | #define RUNSTATE_POST_BIT (1UL << 3) | |
103 | ||
f2cd2deb | 104 | static int ctrl_in(const struct sr_dev_inst *sdi, |
1ed93110 GS |
105 | uint8_t bRequest, uint16_t wValue, uint16_t wIndex, |
106 | void *data, uint16_t wLength) | |
f2cd2deb FS |
107 | { |
108 | struct sr_usb_dev_inst *usb; | |
109 | int ret; | |
110 | ||
111 | usb = sdi->conn; | |
112 | ||
411ad77c GS |
113 | ret = libusb_control_transfer(usb->devhdl, |
114 | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN, | |
115 | bRequest, wValue, wIndex, data, wLength, | |
116 | DEFAULT_TIMEOUT_MS); | |
117 | if (ret != wLength) { | |
91f73872 GS |
118 | sr_dbg("USB ctrl in: %d bytes, req %d val %#x idx %d: %s.", |
119 | wLength, bRequest, wValue, wIndex, | |
120 | libusb_error_name(ret)); | |
121 | sr_err("Cannot read %d bytes from USB: %s.", | |
122 | wLength, libusb_error_name(ret)); | |
286b3e13 | 123 | return SR_ERR_IO; |
f2cd2deb FS |
124 | } |
125 | ||
126 | return SR_OK; | |
127 | } | |
128 | ||
129 | static int ctrl_out(const struct sr_dev_inst *sdi, | |
1ed93110 GS |
130 | uint8_t bRequest, uint16_t wValue, uint16_t wIndex, |
131 | void *data, uint16_t wLength) | |
f2cd2deb FS |
132 | { |
133 | struct sr_usb_dev_inst *usb; | |
134 | int ret; | |
135 | ||
136 | usb = sdi->conn; | |
137 | ||
411ad77c GS |
138 | ret = libusb_control_transfer(usb->devhdl, |
139 | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT, | |
140 | bRequest, wValue, wIndex, data, wLength, | |
141 | DEFAULT_TIMEOUT_MS); | |
142 | if (ret != wLength) { | |
91f73872 GS |
143 | sr_dbg("USB ctrl out: %d bytes, req %d val %#x idx %d: %s.", |
144 | wLength, bRequest, wValue, wIndex, | |
145 | libusb_error_name(ret)); | |
146 | sr_err("Cannot write %d bytes to USB: %s.", | |
147 | wLength, libusb_error_name(ret)); | |
286b3e13 | 148 | return SR_ERR_IO; |
f2cd2deb FS |
149 | } |
150 | ||
151 | return SR_OK; | |
152 | } | |
153 | ||
33020165 GS |
154 | /* HACK Experiment to spot FPGA registers of interest. */ |
155 | static void la2016_dump_fpga_registers(const struct sr_dev_inst *sdi, | |
156 | const char *caption, size_t reg_lower, size_t reg_upper) | |
157 | { | |
158 | static const size_t dump_chunk_len = 16; | |
159 | ||
160 | size_t rdlen; | |
161 | uint8_t rdbuf[0x80 - 0x00]; /* Span all FPGA registers. */ | |
162 | const uint8_t *rdptr; | |
163 | int ret; | |
164 | size_t dump_addr, indent, dump_len; | |
165 | GString *txt; | |
166 | ||
167 | if (sr_log_loglevel_get() < SR_LOG_SPEW) | |
168 | return; | |
169 | ||
170 | if (!reg_lower && !reg_upper) { | |
171 | reg_lower = 0; | |
172 | reg_upper = sizeof(rdbuf); | |
173 | } | |
174 | if (reg_upper - reg_lower > sizeof(rdbuf)) | |
175 | reg_upper = sizeof(rdbuf) - reg_lower; | |
176 | ||
177 | rdlen = reg_upper - reg_lower; | |
178 | ret = ctrl_in(sdi, CMD_FPGA_SPI, reg_lower, 0, rdbuf, rdlen); | |
179 | if (ret != SR_OK) { | |
180 | sr_err("Cannot get registers space."); | |
181 | return; | |
182 | } | |
183 | rdptr = rdbuf; | |
184 | ||
185 | sr_spew("FPGA registers dump: %s", caption ? : "for fun"); | |
186 | dump_addr = reg_lower; | |
187 | while (rdlen) { | |
188 | dump_len = rdlen; | |
189 | indent = dump_addr % dump_chunk_len; | |
190 | if (dump_len > dump_chunk_len) | |
191 | dump_len = dump_chunk_len; | |
192 | if (dump_len + indent > dump_chunk_len) | |
193 | dump_len = dump_chunk_len - indent; | |
194 | txt = sr_hexdump_new(rdptr, dump_len); | |
195 | sr_spew(" %04zx %*s%s", | |
196 | dump_addr, (int)(3 * indent), "", txt->str); | |
197 | sr_hexdump_free(txt); | |
198 | dump_addr += dump_len; | |
199 | rdptr += dump_len; | |
200 | rdlen -= dump_len; | |
201 | } | |
202 | } | |
203 | ||
d6f89d4b GS |
204 | /* |
205 | * Check the necessity for FPGA bitstream upload, because another upload | |
206 | * would take some 600ms which is undesirable after program startup. Try | |
207 | * to access some FPGA registers and check the values' plausibility. The | |
208 | * check should fail on the safe side, request another upload when in | |
209 | * doubt. A positive response (the request to continue operation with the | |
210 | * currently active bitstream) should be conservative. Accessing multiple | |
211 | * registers is considered cheap compared to the cost of bitstream upload. | |
212 | * | |
213 | * It helps though that both the vendor software and the sigrok driver | |
214 | * use the same bundle of MCU firmware and FPGA bitstream for any of the | |
215 | * supported models. We don't expect to successfully communicate to the | |
216 | * device yet disagree on its protocol. Ideally we would access version | |
217 | * identifying registers for improved robustness, but are not aware of | |
218 | * any. A bitstream reload can always be forced by a power cycle. | |
219 | */ | |
220 | static int check_fpga_bitstream(const struct sr_dev_inst *sdi) | |
221 | { | |
222 | uint8_t init_rsp; | |
3ab60908 | 223 | uint8_t buff[REG_PWM_EN - REG_RUN]; /* Larger of REG_RUN, REG_PWM_EN. */ |
d6f89d4b GS |
224 | int ret; |
225 | uint16_t run_state; | |
226 | uint8_t pwm_en; | |
227 | size_t read_len; | |
d6f89d4b GS |
228 | const uint8_t *rdptr; |
229 | ||
230 | sr_dbg("Checking operation of the FPGA bitstream."); | |
33020165 | 231 | la2016_dump_fpga_registers(sdi, "bitstream check", 0, 0); |
d6f89d4b | 232 | |
852c7d14 | 233 | init_rsp = ~0; |
d6f89d4b GS |
234 | ret = ctrl_in(sdi, CMD_FPGA_INIT, 0x00, 0, &init_rsp, sizeof(init_rsp)); |
235 | if (ret != SR_OK || init_rsp != 0) { | |
236 | sr_dbg("FPGA init query failed, or unexpected response."); | |
237 | return SR_ERR_IO; | |
238 | } | |
239 | ||
240 | read_len = sizeof(run_state); | |
241 | ret = ctrl_in(sdi, CMD_FPGA_SPI, REG_RUN, 0, buff, read_len); | |
242 | if (ret != SR_OK) { | |
243 | sr_dbg("FPGA register access failed (run state)."); | |
244 | return SR_ERR_IO; | |
245 | } | |
246 | rdptr = buff; | |
247 | run_state = read_u16le_inc(&rdptr); | |
248 | sr_spew("FPGA register: run state 0x%04x.", run_state); | |
249 | if (run_state && (run_state & 0x3) != 0x1) { | |
250 | sr_dbg("Unexpected FPGA register content (run state)."); | |
251 | return SR_ERR_DATA; | |
252 | } | |
253 | if (run_state && (run_state & ~0xf) != 0x85e0) { | |
254 | sr_dbg("Unexpected FPGA register content (run state)."); | |
255 | return SR_ERR_DATA; | |
256 | } | |
257 | ||
258 | read_len = sizeof(pwm_en); | |
259 | ret = ctrl_in(sdi, CMD_FPGA_SPI, REG_PWM_EN, 0, buff, read_len); | |
260 | if (ret != SR_OK) { | |
261 | sr_dbg("FPGA register access failed (PWM enable)."); | |
262 | return SR_ERR_IO; | |
263 | } | |
264 | rdptr = buff; | |
265 | pwm_en = read_u8_inc(&rdptr); | |
266 | sr_spew("FPGA register: PWM enable 0x%02x.", pwm_en); | |
267 | if ((pwm_en & 0x3) != 0x0) { | |
268 | sr_dbg("Unexpected FPGA register content (PWM enable)."); | |
269 | return SR_ERR_DATA; | |
270 | } | |
271 | ||
272 | sr_info("Could re-use current FPGA bitstream. No upload required."); | |
273 | return SR_OK; | |
274 | } | |
275 | ||
1ed93110 GS |
276 | static int upload_fpga_bitstream(const struct sr_dev_inst *sdi, |
277 | const char *bitstream_fname) | |
f2cd2deb FS |
278 | { |
279 | struct drv_context *drvc; | |
280 | struct sr_usb_dev_inst *usb; | |
281 | struct sr_resource bitstream; | |
b0d0131e | 282 | uint32_t bitstream_size; |
c3d40037 HK |
283 | uint8_t buffer[sizeof(uint32_t)]; |
284 | uint8_t *wrptr; | |
f2cd2deb | 285 | uint8_t block[4096]; |
3f48ab02 FS |
286 | int len, act_len; |
287 | unsigned int pos; | |
f2cd2deb | 288 | int ret; |
b0d0131e | 289 | unsigned int zero_pad_to; |
f2cd2deb FS |
290 | |
291 | drvc = sdi->driver->context; | |
292 | usb = sdi->conn; | |
293 | ||
9de389b1 | 294 | sr_info("Uploading FPGA bitstream '%s'.", bitstream_fname); |
f2cd2deb | 295 | |
411ad77c GS |
296 | ret = sr_resource_open(drvc->sr_ctx, &bitstream, |
297 | SR_RESOURCE_FIRMWARE, bitstream_fname); | |
f2cd2deb | 298 | if (ret != SR_OK) { |
91f73872 | 299 | sr_err("Cannot find FPGA bitstream %s.", bitstream_fname); |
f2cd2deb FS |
300 | return ret; |
301 | } | |
302 | ||
b0d0131e | 303 | bitstream_size = (uint32_t)bitstream.size; |
c3d40037 | 304 | wrptr = buffer; |
b0d0131e | 305 | write_u32le_inc(&wrptr, bitstream_size); |
411ad77c GS |
306 | ret = ctrl_out(sdi, CMD_FPGA_INIT, 0x00, 0, buffer, wrptr - buffer); |
307 | if (ret != SR_OK) { | |
91f73872 | 308 | sr_err("Cannot initiate FPGA bitstream upload."); |
f2cd2deb FS |
309 | sr_resource_close(drvc->sr_ctx, &bitstream); |
310 | return ret; | |
311 | } | |
b0d0131e GS |
312 | zero_pad_to = bitstream_size; |
313 | zero_pad_to += LA2016_EP2_PADDING - 1; | |
314 | zero_pad_to /= LA2016_EP2_PADDING; | |
315 | zero_pad_to *= LA2016_EP2_PADDING; | |
f2cd2deb FS |
316 | |
317 | pos = 0; | |
318 | while (1) { | |
3f48ab02 | 319 | if (pos < bitstream.size) { |
411ad77c GS |
320 | len = (int)sr_resource_read(drvc->sr_ctx, &bitstream, |
321 | block, sizeof(block)); | |
3f48ab02 | 322 | if (len < 0) { |
91f73872 | 323 | sr_err("Cannot read FPGA bitstream."); |
3f48ab02 | 324 | sr_resource_close(drvc->sr_ctx, &bitstream); |
286b3e13 | 325 | return SR_ERR_IO; |
3f48ab02 FS |
326 | } |
327 | } else { | |
96dc954e | 328 | /* Zero-pad until 'zero_pad_to'. */ |
3f48ab02 FS |
329 | len = zero_pad_to - pos; |
330 | if ((unsigned)len > sizeof(block)) | |
331 | len = sizeof(block); | |
332 | memset(&block, 0, len); | |
f2cd2deb FS |
333 | } |
334 | if (len == 0) | |
335 | break; | |
336 | ||
852c7d14 | 337 | ret = libusb_bulk_transfer(usb->devhdl, USB_EP_FPGA_BITSTREAM, |
1ed93110 | 338 | &block[0], len, &act_len, DEFAULT_TIMEOUT_MS); |
f2cd2deb | 339 | if (ret != 0) { |
91f73872 GS |
340 | sr_dbg("Cannot write FPGA bitstream, block %#x len %d: %s.", |
341 | pos, (int)len, libusb_error_name(ret)); | |
286b3e13 | 342 | ret = SR_ERR_IO; |
f2cd2deb FS |
343 | break; |
344 | } | |
345 | if (act_len != len) { | |
91f73872 GS |
346 | sr_dbg("Short write for FPGA bitstream, block %#x len %d: got %d.", |
347 | pos, (int)len, act_len); | |
286b3e13 | 348 | ret = SR_ERR_IO; |
f2cd2deb FS |
349 | break; |
350 | } | |
351 | pos += len; | |
352 | } | |
353 | sr_resource_close(drvc->sr_ctx, &bitstream); | |
5eb1b63d | 354 | if (ret != SR_OK) |
f2cd2deb | 355 | return ret; |
91f73872 GS |
356 | sr_info("FPGA bitstream upload (%" PRIu64 " bytes) done.", |
357 | bitstream.size); | |
f2cd2deb | 358 | |
d6f89d4b GS |
359 | return SR_OK; |
360 | } | |
361 | ||
362 | static int enable_fpga_bitstream(const struct sr_dev_inst *sdi) | |
363 | { | |
364 | int ret; | |
411ad77c | 365 | uint8_t resp; |
d6f89d4b | 366 | |
411ad77c GS |
367 | ret = ctrl_in(sdi, CMD_FPGA_INIT, 0x00, 0, &resp, sizeof(resp)); |
368 | if (ret != SR_OK) { | |
91f73872 | 369 | sr_err("Cannot read response after FPGA bitstream upload."); |
f2cd2deb FS |
370 | return ret; |
371 | } | |
411ad77c | 372 | if (resp != 0) { |
91f73872 | 373 | sr_err("Unexpected FPGA bitstream upload response, got 0x%02x, want 0.", |
411ad77c | 374 | resp); |
286b3e13 | 375 | return SR_ERR_DATA; |
3f48ab02 | 376 | } |
852c7d14 | 377 | g_usleep(30 * 1000); |
f2cd2deb | 378 | |
411ad77c GS |
379 | ret = ctrl_out(sdi, CMD_FPGA_ENABLE, 0x01, 0, NULL, 0); |
380 | if (ret != SR_OK) { | |
91f73872 | 381 | sr_err("Cannot enable FPGA after bitstream upload."); |
f2cd2deb FS |
382 | return ret; |
383 | } | |
852c7d14 | 384 | g_usleep(40 * 1000); |
d6f89d4b | 385 | |
f2cd2deb FS |
386 | return SR_OK; |
387 | } | |
388 | ||
389 | static int set_threshold_voltage(const struct sr_dev_inst *sdi, float voltage) | |
390 | { | |
f2cd2deb | 391 | int ret; |
1ed93110 | 392 | uint16_t duty_R79, duty_R56; |
3ab60908 | 393 | uint8_t buf[REG_PWM1 - REG_THRESHOLD]; /* Width of REG_THRESHOLD. */ |
f2ad79d1 KG |
394 | uint8_t *wrptr; |
395 | ||
96dc954e | 396 | /* Clamp threshold setting to valid range for LA2016. */ |
c35baf6e GS |
397 | if (voltage > LA2016_THR_VOLTAGE_MAX) { |
398 | voltage = LA2016_THR_VOLTAGE_MAX; | |
399 | } else if (voltage < -LA2016_THR_VOLTAGE_MAX) { | |
400 | voltage = -LA2016_THR_VOLTAGE_MAX; | |
f2ad79d1 KG |
401 | } |
402 | ||
403 | /* | |
96dc954e GS |
404 | * Two PWM output channels feed one DAC which generates a bias |
405 | * voltage, which offsets the input probe's voltage level, and | |
406 | * in combination with the FPGA pins' fixed threshold result in | |
407 | * a programmable input threshold from the user's perspective. | |
408 | * The PWM outputs can be seen on R79 and R56 respectively, the | |
409 | * frequency is 100kHz and the duty cycle varies. The R79 PWM | |
410 | * uses three discrete settings. The R56 PWM varies with desired | |
411 | * thresholds and depends on the R79 PWM configuration. See the | |
412 | * schematics comments which discuss the formulae. | |
f2ad79d1 KG |
413 | */ |
414 | if (voltage >= 2.9) { | |
96dc954e | 415 | duty_R79 = 0; /* PWM off (0V). */ |
f2ad79d1 | 416 | duty_R56 = (uint16_t)(302 * voltage - 363); |
c34f4a89 | 417 | } else if (voltage > -0.4) { |
96dc954e | 418 | duty_R79 = 0x00f2; /* 25% duty cycle. */ |
f2ad79d1 | 419 | duty_R56 = (uint16_t)(302 * voltage + 121); |
c34f4a89 GS |
420 | } else { |
421 | duty_R79 = 0x02d7; /* 72% duty cycle. */ | |
422 | duty_R56 = (uint16_t)(302 * voltage + 1090); | |
f2ad79d1 KG |
423 | } |
424 | ||
96dc954e | 425 | /* Clamp duty register values to sensible limits. */ |
f2ad79d1 KG |
426 | if (duty_R56 < 10) { |
427 | duty_R56 = 10; | |
1ed93110 | 428 | } else if (duty_R56 > 1100) { |
f2ad79d1 KG |
429 | duty_R56 = 1100; |
430 | } | |
431 | ||
91f73872 GS |
432 | sr_dbg("Set threshold voltage %.2fV.", voltage); |
433 | sr_dbg("Duty cycle values: R56 0x%04x, R79 0x%04x.", duty_R56, duty_R79); | |
f2ad79d1 KG |
434 | |
435 | wrptr = buf; | |
436 | write_u16le_inc(&wrptr, duty_R56); | |
437 | write_u16le_inc(&wrptr, duty_R79); | |
438 | ||
439 | ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_THRESHOLD, 0, buf, wrptr - buf); | |
f2cd2deb | 440 | if (ret != SR_OK) { |
91f73872 | 441 | sr_err("Cannot set threshold voltage %.2fV.", voltage); |
f2cd2deb FS |
442 | return ret; |
443 | } | |
f2cd2deb FS |
444 | |
445 | return SR_OK; | |
446 | } | |
447 | ||
08a49848 GS |
448 | /* |
449 | * Communicates a channel's configuration to the device after the | |
450 | * parameters may have changed. Configuration of one channel may | |
451 | * interfere with other channels since they share FPGA registers. | |
452 | */ | |
453 | static int set_pwm_config(const struct sr_dev_inst *sdi, size_t idx) | |
f2cd2deb | 454 | { |
08a49848 | 455 | static uint8_t reg_bases[] = { REG_PWM1, REG_PWM2, }; |
86d77b75 | 456 | |
f2cd2deb | 457 | struct dev_context *devc; |
08a49848 GS |
458 | struct pwm_setting *params; |
459 | uint8_t reg_base; | |
460 | double val_f; | |
461 | uint32_t val_u; | |
462 | uint32_t period, duty; | |
463 | size_t ch; | |
f2cd2deb | 464 | int ret; |
08a49848 GS |
465 | uint8_t enable_all, enable_cfg, reg_val; |
466 | uint8_t buf[REG_PWM2 - REG_PWM1]; /* Width of one REG_PWMx. */ | |
c3d40037 | 467 | uint8_t *wrptr; |
f2cd2deb FS |
468 | |
469 | devc = sdi->priv; | |
08a49848 GS |
470 | if (idx >= ARRAY_SIZE(devc->pwm_setting)) |
471 | return SR_ERR_ARG; | |
472 | params = &devc->pwm_setting[idx]; | |
473 | if (idx >= ARRAY_SIZE(reg_bases)) | |
474 | return SR_ERR_ARG; | |
475 | reg_base = reg_bases[idx]; | |
f2cd2deb | 476 | |
08a49848 GS |
477 | /* |
478 | * Map application's specs to hardware register values. Do math | |
479 | * in floating point initially, but convert to u32 eventually. | |
480 | */ | |
481 | sr_dbg("PWM config, app spec, ch %zu, en %d, freq %.1f, duty %.1f.", | |
482 | idx, params->enabled ? 1 : 0, params->freq, params->duty); | |
483 | val_f = PWM_CLOCK; | |
484 | val_f /= params->freq; | |
485 | val_u = val_f; | |
486 | period = val_u; | |
487 | val_f = period; | |
488 | val_f *= params->duty; | |
489 | val_f /= 100.0; | |
490 | val_f += 0.5; | |
491 | val_u = val_f; | |
492 | duty = val_u; | |
493 | sr_dbg("PWM config, reg 0x%04x, freq %u, duty %u.", | |
494 | (unsigned)reg_base, (unsigned)period, (unsigned)duty); | |
495 | ||
496 | /* Get the "enabled" state of all supported PWM channels. */ | |
497 | enable_all = 0; | |
498 | for (ch = 0; ch < ARRAY_SIZE(devc->pwm_setting); ch++) { | |
499 | if (!devc->pwm_setting[ch].enabled) | |
500 | continue; | |
501 | enable_all |= 1U << ch; | |
f2cd2deb | 502 | } |
08a49848 GS |
503 | enable_cfg = 1U << idx; |
504 | sr_spew("PWM config, enable all 0x%02hhx, cfg 0x%02hhx.", | |
505 | enable_all, enable_cfg); | |
f2cd2deb | 506 | |
08a49848 GS |
507 | /* |
508 | * Disable the to-get-configured channel before its parameters | |
509 | * will change. Or disable and exit when the channel is supposed | |
510 | * to get turned off. | |
511 | */ | |
512 | sr_spew("PWM config, disabling before param change."); | |
513 | reg_val = enable_all & ~enable_cfg; | |
514 | ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_PWM_EN, 0, | |
515 | ®_val, sizeof(reg_val)); | |
f2cd2deb | 516 | if (ret != SR_OK) { |
08a49848 | 517 | sr_err("Cannot adjust PWM enabled state."); |
f2cd2deb FS |
518 | return ret; |
519 | } | |
08a49848 GS |
520 | if (!params->enabled) |
521 | return SR_OK; | |
86d77b75 | 522 | |
08a49848 GS |
523 | /* Write register values to device. */ |
524 | sr_spew("PWM config, sending new parameters."); | |
525 | wrptr = buf; | |
526 | write_u32le_inc(&wrptr, period); | |
527 | write_u32le_inc(&wrptr, duty); | |
528 | ret = ctrl_out(sdi, CMD_FPGA_SPI, reg_base, 0, buf, wrptr - buf); | |
529 | if (ret != SR_OK) { | |
530 | sr_err("Cannot change PWM parameters."); | |
f2cd2deb | 531 | return ret; |
08a49848 | 532 | } |
f2cd2deb | 533 | |
08a49848 GS |
534 | /* Enable configured channel after write completion. */ |
535 | sr_spew("PWM config, enabling after param change."); | |
536 | reg_val = enable_all | enable_cfg; | |
537 | ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_PWM_EN, 0, | |
538 | ®_val, sizeof(reg_val)); | |
539 | if (ret != SR_OK) { | |
540 | sr_err("Cannot adjust PWM enabled state."); | |
f2cd2deb | 541 | return ret; |
08a49848 | 542 | } |
f2cd2deb FS |
543 | |
544 | return SR_OK; | |
545 | } | |
546 | ||
0fbb464b GS |
547 | /* |
548 | * Determine the number of enabled channels as well as their bitmask | |
549 | * representation. Derive data here which later simplifies processing | |
550 | * of raw capture data memory content in streaming mode. | |
551 | */ | |
552 | static void la2016_prepare_stream(const struct sr_dev_inst *sdi) | |
ea436ba7 | 553 | { |
0fbb464b GS |
554 | struct dev_context *devc; |
555 | struct stream_state_t *stream; | |
556 | size_t channel_mask; | |
ea436ba7 GS |
557 | GSList *l; |
558 | struct sr_channel *ch; | |
559 | ||
0fbb464b GS |
560 | devc = sdi->priv; |
561 | stream = &devc->stream; | |
562 | memset(stream, 0, sizeof(*stream)); | |
563 | ||
564 | stream->enabled_count = 0; | |
ea436ba7 GS |
565 | for (l = sdi->channels; l; l = l->next) { |
566 | ch = l->data; | |
567 | if (ch->type != SR_CHANNEL_LOGIC) | |
568 | continue; | |
569 | if (!ch->enabled) | |
570 | continue; | |
0fbb464b GS |
571 | channel_mask = 1UL << ch->index; |
572 | stream->enabled_mask |= channel_mask; | |
573 | stream->channel_masks[stream->enabled_count++] = channel_mask; | |
ea436ba7 | 574 | } |
0fbb464b | 575 | stream->channel_index = 0; |
ea436ba7 GS |
576 | } |
577 | ||
0fbb464b GS |
578 | /* |
579 | * This routine configures the set of enabled channels, as well as the | |
580 | * trigger condition (if one was specified). Also prepares the capture | |
581 | * data processing in stream mode, where the memory layout dramatically | |
582 | * differs from normal mode. | |
583 | */ | |
f2cd2deb FS |
584 | static int set_trigger_config(const struct sr_dev_inst *sdi) |
585 | { | |
586 | struct dev_context *devc; | |
587 | struct sr_trigger *trigger; | |
edb13f41 | 588 | struct trigger_cfg { |
972d191b GS |
589 | uint32_t channels; /* Actually: Enabled channels? */ |
590 | uint32_t enabled; /* Actually: Triggering channels? */ | |
edb13f41 GS |
591 | uint32_t level; |
592 | uint32_t high_or_falling; | |
593 | } cfg; | |
f2cd2deb FS |
594 | GSList *stages; |
595 | GSList *channel; | |
596 | struct sr_trigger_stage *stage1; | |
597 | struct sr_trigger_match *match; | |
4276ca94 | 598 | uint32_t ch_mask; |
f2cd2deb | 599 | int ret; |
3ab60908 | 600 | uint8_t buf[REG_UNKNOWN_30 - REG_TRIGGER]; /* Width of REG_TRIGGER. */ |
c3d40037 | 601 | uint8_t *wrptr; |
f2cd2deb FS |
602 | |
603 | devc = sdi->priv; | |
f2cd2deb | 604 | |
0fbb464b | 605 | la2016_prepare_stream(sdi); |
f2cd2deb | 606 | |
0fbb464b GS |
607 | memset(&cfg, 0, sizeof(cfg)); |
608 | cfg.channels = devc->stream.enabled_mask; | |
609 | if (!cfg.channels) { | |
610 | sr_err("Need at least one enabled logic channel."); | |
611 | return SR_ERR_ARG; | |
612 | } | |
613 | trigger = sr_session_trigger_get(sdi->session); | |
f2cd2deb FS |
614 | if (trigger && trigger->stages) { |
615 | stages = trigger->stages; | |
616 | stage1 = stages->data; | |
617 | if (stages->next) { | |
618 | sr_err("Only one trigger stage supported for now."); | |
286b3e13 | 619 | return SR_ERR_ARG; |
f2cd2deb FS |
620 | } |
621 | channel = stage1->matches; | |
622 | while (channel) { | |
623 | match = channel->data; | |
cf057ac4 | 624 | ch_mask = 1UL << match->channel->index; |
f2cd2deb FS |
625 | |
626 | switch (match->match) { | |
627 | case SR_TRIGGER_ZERO: | |
628 | cfg.level |= ch_mask; | |
629 | cfg.high_or_falling &= ~ch_mask; | |
630 | break; | |
631 | case SR_TRIGGER_ONE: | |
632 | cfg.level |= ch_mask; | |
633 | cfg.high_or_falling |= ch_mask; | |
634 | break; | |
635 | case SR_TRIGGER_RISING: | |
636 | if ((cfg.enabled & ~cfg.level)) { | |
91f73872 | 637 | sr_err("Device only supports one edge trigger."); |
286b3e13 | 638 | return SR_ERR_ARG; |
f2cd2deb FS |
639 | } |
640 | cfg.level &= ~ch_mask; | |
641 | cfg.high_or_falling &= ~ch_mask; | |
642 | break; | |
643 | case SR_TRIGGER_FALLING: | |
644 | if ((cfg.enabled & ~cfg.level)) { | |
91f73872 | 645 | sr_err("Device only supports one edge trigger."); |
286b3e13 | 646 | return SR_ERR_ARG; |
f2cd2deb FS |
647 | } |
648 | cfg.level &= ~ch_mask; | |
649 | cfg.high_or_falling |= ch_mask; | |
650 | break; | |
651 | default: | |
91f73872 | 652 | sr_err("Unknown trigger condition."); |
286b3e13 | 653 | return SR_ERR_ARG; |
f2cd2deb FS |
654 | } |
655 | cfg.enabled |= ch_mask; | |
656 | channel = channel->next; | |
657 | } | |
658 | } | |
91f73872 | 659 | sr_dbg("Set trigger config: " |
972d191b | 660 | "enabled-channels 0x%04x, triggering-channels 0x%04x, " |
91f73872 GS |
661 | "level-triggered 0x%04x, high/falling 0x%04x.", |
662 | cfg.channels, cfg.enabled, cfg.level, cfg.high_or_falling); | |
f2cd2deb | 663 | |
0fbb464b GS |
664 | /* |
665 | * Don't configure hardware trigger parameters in streaming mode | |
666 | * or when the device lacks local memory. Yet the above dump of | |
667 | * derived parameters from user specs is considered valueable. | |
668 | * | |
669 | * TODO Add support for soft triggers when hardware triggers in | |
670 | * the device are not used or are not available at all. | |
671 | */ | |
672 | if (!devc->model->memory_bits || devc->continuous) { | |
673 | if (!devc->model->memory_bits) | |
674 | sr_dbg("Device without memory. No hardware triggers."); | |
675 | else if (devc->continuous) | |
676 | sr_dbg("Streaming mode. No hardware triggers."); | |
677 | cfg.enabled = 0; | |
678 | cfg.level = 0; | |
679 | cfg.high_or_falling = 0; | |
680 | } | |
681 | ||
cf057ac4 | 682 | devc->trigger_involved = cfg.enabled != 0; |
f2cd2deb | 683 | |
c3d40037 HK |
684 | wrptr = buf; |
685 | write_u32le_inc(&wrptr, cfg.channels); | |
686 | write_u32le_inc(&wrptr, cfg.enabled); | |
687 | write_u32le_inc(&wrptr, cfg.level); | |
688 | write_u32le_inc(&wrptr, cfg.high_or_falling); | |
852c7d14 GS |
689 | /* TODO |
690 | * Comment on this literal 16. Origin, meaning? Cannot be the | |
691 | * register offset, nor the transfer length. Is it a channels | |
692 | * count that is relevant for 16 and 32 channel models? Is it | |
693 | * an obsolete experiment? | |
694 | */ | |
42f6dd55 | 695 | ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_TRIGGER, 16, buf, wrptr - buf); |
f2cd2deb | 696 | if (ret != SR_OK) { |
91f73872 | 697 | sr_err("Cannot setup trigger configuration."); |
f2cd2deb FS |
698 | return ret; |
699 | } | |
700 | ||
701 | return SR_OK; | |
702 | } | |
703 | ||
0fbb464b GS |
704 | /* |
705 | * This routine communicates the sample configuration to the device: | |
706 | * Total samples count and samplerate, pre-trigger configuration. | |
707 | */ | |
f2cd2deb FS |
708 | static int set_sample_config(const struct sr_dev_inst *sdi) |
709 | { | |
710 | struct dev_context *devc; | |
d8fbfcd9 | 711 | uint64_t min_samplerate, eff_samplerate; |
0fbb464b | 712 | uint64_t stream_bandwidth; |
adab4d91 | 713 | uint16_t divider_u16; |
a38f0f5e | 714 | uint64_t limit_samples; |
adab4d91 GS |
715 | uint64_t pre_trigger_samples; |
716 | uint64_t pre_trigger_memory; | |
717 | uint8_t buf[REG_TRIGGER - REG_SAMPLING]; /* Width of REG_SAMPLING. */ | |
c3d40037 | 718 | uint8_t *wrptr; |
adab4d91 | 719 | int ret; |
f2cd2deb FS |
720 | |
721 | devc = sdi->priv; | |
f2cd2deb | 722 | |
edc0b015 | 723 | if (devc->samplerate > devc->model->samplerate) { |
91f73872 | 724 | sr_err("Too high a sample rate: %" PRIu64 ".", |
edc0b015 | 725 | devc->samplerate); |
ea436ba7 GS |
726 | return SR_ERR_ARG; |
727 | } | |
d8fbfcd9 GS |
728 | min_samplerate = devc->model->samplerate; |
729 | min_samplerate /= 65536; | |
edc0b015 | 730 | if (devc->samplerate < min_samplerate) { |
ea436ba7 | 731 | sr_err("Too low a sample rate: %" PRIu64 ".", |
edc0b015 | 732 | devc->samplerate); |
ea436ba7 | 733 | return SR_ERR_ARG; |
f2cd2deb | 734 | } |
edc0b015 | 735 | divider_u16 = devc->model->samplerate / devc->samplerate; |
d8fbfcd9 | 736 | eff_samplerate = devc->model->samplerate / divider_u16; |
f2cd2deb | 737 | |
a38f0f5e GS |
738 | ret = sr_sw_limits_get_remain(&devc->sw_limits, |
739 | &limit_samples, NULL, NULL, NULL); | |
740 | if (ret != SR_OK) { | |
741 | sr_err("Cannot get acquisition limits."); | |
742 | return ret; | |
f2cd2deb | 743 | } |
a38f0f5e | 744 | if (limit_samples > LA2016_NUM_SAMPLES_MAX) { |
d8fbfcd9 GS |
745 | sr_warn("Too high a sample depth: %" PRIu64 ", capping.", |
746 | limit_samples); | |
747 | limit_samples = LA2016_NUM_SAMPLES_MAX; | |
a38f0f5e | 748 | } |
d8fbfcd9 GS |
749 | if (limit_samples == 0) { |
750 | limit_samples = LA2016_NUM_SAMPLES_MAX; | |
751 | sr_dbg("Passing %" PRIu64 " to HW for unlimited samples.", | |
752 | limit_samples); | |
ea436ba7 | 753 | } |
f2cd2deb | 754 | |
adab4d91 GS |
755 | /* |
756 | * The acquisition configuration communicates "pre-trigger" | |
757 | * specs in several formats. sigrok users provide a percentage | |
758 | * (0-100%), which translates to a pre-trigger samples count | |
759 | * (assuming that a total samples count limit was specified). | |
760 | * The device supports hardware compression, which depends on | |
761 | * slowly changing input data to be effective. Fast changing | |
762 | * input data may occupy more space in sample memory than its | |
763 | * uncompressed form would. This is why a third parameter can | |
764 | * limit the amount of sample memory to use for pre-trigger | |
765 | * data. Only the upper 24 bits of that memory size spec get | |
766 | * communicated to the device (written to its FPGA register). | |
767 | */ | |
0fbb464b GS |
768 | if (!devc->model->memory_bits) { |
769 | sr_dbg("Memory-less device, skipping pre-trigger config."); | |
770 | pre_trigger_samples = 0; | |
771 | pre_trigger_memory = 0; | |
772 | } else if (devc->trigger_involved) { | |
d8fbfcd9 GS |
773 | pre_trigger_samples = limit_samples; |
774 | pre_trigger_samples *= devc->capture_ratio; | |
775 | pre_trigger_samples /= 100; | |
776 | pre_trigger_memory = devc->model->memory_bits; | |
777 | pre_trigger_memory *= UINT64_C(1024 * 1024 * 1024); | |
778 | pre_trigger_memory /= 8; /* devc->model->channel_count ? */ | |
779 | pre_trigger_memory *= devc->capture_ratio; | |
780 | pre_trigger_memory /= 100; | |
781 | } else { | |
782 | sr_dbg("No trigger setup, skipping pre-trigger config."); | |
0fbb464b | 783 | pre_trigger_samples = 0; |
d8fbfcd9 GS |
784 | pre_trigger_memory = 0; |
785 | } | |
786 | /* Ensure non-zero value after LSB shift out in HW reg. */ | |
0fbb464b | 787 | if (pre_trigger_memory < 0x100) |
d8fbfcd9 | 788 | pre_trigger_memory = 0x100; |
f2cd2deb | 789 | |
0fbb464b GS |
790 | sr_dbg("Set sample config: %" PRIu64 "kHz (div %" PRIu16 "), %" PRIu64 " samples.", |
791 | eff_samplerate / SR_KHZ(1), divider_u16, limit_samples); | |
adab4d91 GS |
792 | sr_dbg("Capture ratio %" PRIu64 "%%, count %" PRIu64 ", mem %" PRIu64 ".", |
793 | devc->capture_ratio, pre_trigger_samples, pre_trigger_memory); | |
f2cd2deb | 794 | |
0fbb464b GS |
795 | if (devc->continuous) { |
796 | stream_bandwidth = eff_samplerate; | |
797 | stream_bandwidth *= devc->stream.enabled_count; | |
798 | sr_dbg("Streaming: channel count %zu, product %" PRIu64 ".", | |
799 | devc->stream.enabled_count, stream_bandwidth); | |
800 | stream_bandwidth /= 1000 * 1000; | |
801 | if (stream_bandwidth >= LA2016_STREAM_MBPS_MAX) { | |
802 | sr_warn("High USB stream bandwidth: %" PRIu64 "Mbps.", | |
803 | stream_bandwidth); | |
804 | } | |
805 | if (stream_bandwidth < LA2016_STREAM_PUSH_THR) { | |
806 | sr_dbg("Streaming: low Mbps, suggest periodic flush."); | |
807 | devc->stream.flush_period_ms = LA2016_STREAM_PUSH_IVAL; | |
808 | } | |
809 | } | |
810 | ||
b1a17c1a GS |
811 | /* |
812 | * The acquisition configuration occupies a total of 16 bytes: | |
813 | * - A 34bit total samples count limit (up to 10 billions) that | |
814 | * is kept in a 40bit register. | |
815 | * - A 34bit pre-trigger samples count limit (up to 10 billions) | |
816 | * in another 40bit register. | |
817 | * - A 32bit pre-trigger memory space limit (in bytes) of which | |
818 | * the upper 24bits are kept in an FPGA register. | |
819 | * - A 16bit clock divider which gets applied to the maximum | |
820 | * samplerate of the device. | |
821 | * - An 8bit register of unknown meaning. Currently always 0. | |
822 | */ | |
c3d40037 | 823 | wrptr = buf; |
a38f0f5e | 824 | write_u40le_inc(&wrptr, limit_samples); |
b1a17c1a GS |
825 | write_u40le_inc(&wrptr, pre_trigger_samples); |
826 | write_u24le_inc(&wrptr, pre_trigger_memory >> 8); | |
adab4d91 | 827 | write_u16le_inc(&wrptr, divider_u16); |
0d8e1ffc | 828 | write_u8_inc(&wrptr, 0); |
42f6dd55 | 829 | ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_SAMPLING, 0, buf, wrptr - buf); |
f2cd2deb | 830 | if (ret != SR_OK) { |
91f73872 | 831 | sr_err("Cannot setup acquisition configuration."); |
f2cd2deb FS |
832 | return ret; |
833 | } | |
834 | ||
835 | return SR_OK; | |
836 | } | |
837 | ||
96dc954e GS |
838 | /* |
839 | * FPGA register REG_RUN holds the run state (u16le format). Bit fields | |
840 | * of interest: | |
841 | * bit 0: value 1 = idle | |
842 | * bit 1: value 1 = writing to SDRAM | |
843 | * bit 2: value 0 = waiting for trigger, 1 = trigger seen | |
844 | * bit 3: value 0 = pretrigger sampling, 1 = posttrigger sampling | |
845 | * The meaning of other bit fields is unknown. | |
7601dca7 | 846 | * |
96dc954e | 847 | * Typical values in order of appearance during execution: |
b711fd8e GS |
848 | * 0x85e1: idle, no acquisition pending |
849 | * IDLE set, TRGD don't care, POST don't care; DRAM don't care | |
850 | * "In idle state." Takes precedence over all others. | |
96dc954e GS |
851 | * 0x85e2: pre-sampling, samples before the trigger position, |
852 | * when capture ratio > 0% | |
b711fd8e GS |
853 | * IDLE clear, TRGD clear, POST clear; DRAM don't care |
854 | * "Not idle any more, no post yet, not triggered yet." | |
96dc954e GS |
855 | * 0x85ea: pre-sampling complete, now waiting for the trigger |
856 | * (whilst sampling continuously) | |
b711fd8e GS |
857 | * IDLE clear, TRGD clear, POST set; DRAM don't care |
858 | * "Post set thus after pre, not triggered yet" | |
96dc954e | 859 | * 0x85ee: trigger seen, capturing post-trigger samples, running |
b711fd8e GS |
860 | * IDLE clear, TRGD set, POST set; DRAM don't care |
861 | * "Triggered and in post, not idle yet." | |
96dc954e | 862 | * 0x85ed: idle |
b711fd8e GS |
863 | * IDLE set, TRGD don't care, POST don't care; DRAM don't care |
864 | * "In idle state." TRGD/POST don't care, same meaning as above. | |
f2cd2deb | 865 | */ |
b711fd8e GS |
866 | static const uint16_t runstate_mask_idle = RUNSTATE_IDLE_BIT; |
867 | static const uint16_t runstate_patt_idle = RUNSTATE_IDLE_BIT; | |
868 | static const uint16_t runstate_mask_step = | |
869 | RUNSTATE_IDLE_BIT | RUNSTATE_TRGD_BIT | RUNSTATE_POST_BIT; | |
870 | static const uint16_t runstate_patt_pre_trig = 0; | |
871 | static const uint16_t runstate_patt_wait_trig = RUNSTATE_POST_BIT; | |
872 | static const uint16_t runstate_patt_post_trig = | |
873 | RUNSTATE_TRGD_BIT | RUNSTATE_POST_BIT; | |
874 | ||
f2cd2deb FS |
875 | static uint16_t run_state(const struct sr_dev_inst *sdi) |
876 | { | |
21d68fd9 GS |
877 | static uint16_t previous_state; |
878 | ||
f2cd2deb | 879 | int ret; |
21d68fd9 | 880 | uint16_t state; |
3ab60908 | 881 | uint8_t buff[REG_PWM_EN - REG_RUN]; /* Width of REG_RUN. */ |
21d68fd9 GS |
882 | const uint8_t *rdptr; |
883 | const char *label; | |
f2cd2deb | 884 | |
411ad77c GS |
885 | ret = ctrl_in(sdi, CMD_FPGA_SPI, REG_RUN, 0, buff, sizeof(state)); |
886 | if (ret != SR_OK) { | |
91f73872 | 887 | sr_err("Cannot read run state."); |
f2cd2deb FS |
888 | return ret; |
889 | } | |
21d68fd9 GS |
890 | rdptr = buff; |
891 | state = read_u16le_inc(&rdptr); | |
7601dca7 | 892 | |
96dc954e GS |
893 | /* |
894 | * Avoid flooding the log, only dump values as they change. | |
895 | * The routine is called about every 50ms. | |
7601dca7 | 896 | */ |
b711fd8e GS |
897 | if (state == previous_state) |
898 | return state; | |
899 | ||
900 | previous_state = state; | |
901 | label = NULL; | |
902 | if ((state & runstate_mask_idle) == runstate_patt_idle) | |
903 | label = "idle"; | |
904 | if ((state & runstate_mask_step) == runstate_patt_pre_trig) | |
905 | label = "pre-trigger sampling"; | |
906 | if ((state & runstate_mask_step) == runstate_patt_wait_trig) | |
907 | label = "sampling, waiting for trigger"; | |
908 | if ((state & runstate_mask_step) == runstate_patt_post_trig) | |
909 | label = "post-trigger sampling"; | |
910 | if (label && *label) | |
911 | sr_dbg("Run state: 0x%04x (%s).", state, label); | |
912 | else | |
913 | sr_dbg("Run state: 0x%04x.", state); | |
f2cd2deb FS |
914 | |
915 | return state; | |
916 | } | |
917 | ||
7a38cdf7 | 918 | static gboolean la2016_is_idle(const struct sr_dev_inst *sdi) |
c34f4a89 GS |
919 | { |
920 | uint16_t state; | |
921 | ||
922 | state = run_state(sdi); | |
b711fd8e | 923 | if ((state & runstate_mask_idle) == runstate_patt_idle) |
7a38cdf7 | 924 | return TRUE; |
c34f4a89 | 925 | |
7a38cdf7 | 926 | return FALSE; |
c34f4a89 GS |
927 | } |
928 | ||
929 | static int set_run_mode(const struct sr_dev_inst *sdi, uint8_t mode) | |
f2cd2deb FS |
930 | { |
931 | int ret; | |
932 | ||
411ad77c GS |
933 | ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_RUN, 0, &mode, sizeof(mode)); |
934 | if (ret != SR_OK) { | |
c34f4a89 | 935 | sr_err("Cannot configure run mode %d.", mode); |
f2cd2deb FS |
936 | return ret; |
937 | } | |
938 | ||
939 | return SR_OK; | |
940 | } | |
941 | ||
942 | static int get_capture_info(const struct sr_dev_inst *sdi) | |
943 | { | |
944 | struct dev_context *devc; | |
945 | int ret; | |
3ab60908 | 946 | uint8_t buf[REG_TRIGGER - REG_SAMPLING]; /* Width of REG_SAMPLING. */ |
c3d40037 | 947 | const uint8_t *rdptr; |
f2cd2deb FS |
948 | |
949 | devc = sdi->priv; | |
950 | ||
411ad77c GS |
951 | ret = ctrl_in(sdi, CMD_FPGA_SPI, REG_SAMPLING, 0, buf, sizeof(buf)); |
952 | if (ret != SR_OK) { | |
91f73872 | 953 | sr_err("Cannot read capture info."); |
f2cd2deb FS |
954 | return ret; |
955 | } | |
c3d40037 HK |
956 | |
957 | rdptr = buf; | |
958 | devc->info.n_rep_packets = read_u32le_inc(&rdptr); | |
959 | devc->info.n_rep_packets_before_trigger = read_u32le_inc(&rdptr); | |
960 | devc->info.write_pos = read_u32le_inc(&rdptr); | |
f2cd2deb | 961 | |
cf057ac4 | 962 | sr_dbg("Capture info: n_rep_packets: 0x%08x/%d, before_trigger: 0x%08x/%d, write_pos: 0x%08x/%d.", |
1ed93110 GS |
963 | devc->info.n_rep_packets, devc->info.n_rep_packets, |
964 | devc->info.n_rep_packets_before_trigger, | |
965 | devc->info.n_rep_packets_before_trigger, | |
966 | devc->info.write_pos, devc->info.write_pos); | |
f2cd2deb | 967 | |
038e65c1 GS |
968 | if (devc->info.n_rep_packets % devc->packets_per_chunk) { |
969 | sr_warn("Unexpected packets count %lu, not a multiple of %lu.", | |
852c7d14 | 970 | (unsigned long)devc->info.n_rep_packets, |
038e65c1 | 971 | (unsigned long)devc->packets_per_chunk); |
91f73872 | 972 | } |
f2cd2deb FS |
973 | |
974 | return SR_OK; | |
975 | } | |
976 | ||
d466f61c | 977 | SR_PRIV int la2016_upload_firmware(const struct sr_dev_inst *sdi, |
91aa0f04 | 978 | struct sr_context *sr_ctx, libusb_device *dev, gboolean skip_upload) |
f2cd2deb | 979 | { |
d466f61c | 980 | struct dev_context *devc; |
91aa0f04 GS |
981 | uint16_t pid; |
982 | char *fw; | |
d466f61c GS |
983 | int ret; |
984 | ||
985 | devc = sdi ? sdi->priv : NULL; | |
91aa0f04 GS |
986 | if (!devc || !devc->usb_pid) |
987 | return SR_ERR_ARG; | |
988 | pid = devc->usb_pid; | |
d466f61c | 989 | |
91aa0f04 GS |
990 | fw = g_strdup_printf(MCU_FWFILE_FMT, pid); |
991 | sr_info("USB PID %04hx, MCU firmware '%s'.", pid, fw); | |
992 | devc->mcu_firmware = g_strdup(fw); | |
d466f61c | 993 | |
91aa0f04 GS |
994 | if (skip_upload) |
995 | ret = SR_OK; | |
996 | else | |
997 | ret = ezusb_upload_firmware(sr_ctx, dev, USB_CONFIGURATION, fw); | |
998 | g_free(fw); | |
999 | if (ret != SR_OK) | |
d466f61c | 1000 | return ret; |
d466f61c GS |
1001 | |
1002 | return SR_OK; | |
f2cd2deb FS |
1003 | } |
1004 | ||
1291ea43 GS |
1005 | static void LIBUSB_CALL receive_transfer(struct libusb_transfer *xfer); |
1006 | ||
796ce0bf GS |
1007 | static void la2016_usbxfer_release_cb(gpointer p) |
1008 | { | |
1009 | struct libusb_transfer *xfer; | |
1010 | ||
1011 | xfer = p; | |
1012 | g_free(xfer->buffer); | |
1013 | libusb_free_transfer(xfer); | |
1014 | } | |
1015 | ||
1291ea43 GS |
1016 | static int la2016_usbxfer_release(const struct sr_dev_inst *sdi) |
1017 | { | |
1018 | struct dev_context *devc; | |
1291ea43 GS |
1019 | |
1020 | devc = sdi ? sdi->priv : NULL; | |
1021 | if (!devc) | |
1022 | return SR_ERR_ARG; | |
1023 | ||
1024 | /* Release all USB transfers. */ | |
796ce0bf GS |
1025 | g_slist_free_full(devc->transfers, la2016_usbxfer_release_cb); |
1026 | devc->transfers = NULL; | |
1291ea43 GS |
1027 | |
1028 | return SR_OK; | |
1029 | } | |
1030 | ||
1031 | static int la2016_usbxfer_allocate(const struct sr_dev_inst *sdi) | |
1032 | { | |
1033 | struct dev_context *devc; | |
796ce0bf | 1034 | size_t bufsize, xfercount; |
1291ea43 GS |
1035 | uint8_t *buffer; |
1036 | struct libusb_transfer *xfer; | |
1037 | ||
1038 | devc = sdi ? sdi->priv : NULL; | |
1039 | if (!devc) | |
1040 | return SR_ERR_ARG; | |
1041 | ||
1042 | /* Transfers were already allocated before? */ | |
796ce0bf | 1043 | if (devc->transfers) |
1291ea43 GS |
1044 | return SR_OK; |
1045 | ||
1046 | /* | |
1047 | * Allocate all USB transfers and their buffers. Arrange for a | |
1048 | * buffer size which is within the device's capabilities, and | |
1049 | * is a multiple of the USB endpoint's size, to make use of the | |
1050 | * RAW_IO performance feature. | |
1051 | * | |
1052 | * Implementation detail: The LA2016_USB_BUFSZ value happens | |
1053 | * to match all those constraints. No additional arithmetics is | |
1054 | * required in this location. | |
1055 | */ | |
1056 | bufsize = LA2016_USB_BUFSZ; | |
796ce0bf GS |
1057 | xfercount = LA2016_USB_XFER_COUNT; |
1058 | while (xfercount--) { | |
1059 | buffer = g_try_malloc(bufsize); | |
1060 | if (!buffer) { | |
1061 | sr_err("Cannot allocate USB transfer buffer."); | |
1062 | return SR_ERR_MALLOC; | |
1063 | } | |
1064 | xfer = libusb_alloc_transfer(0); | |
1065 | if (!xfer) { | |
1066 | sr_err("Cannot allocate USB transfer."); | |
1067 | g_free(buffer); | |
1068 | return SR_ERR_MALLOC; | |
1069 | } | |
1070 | xfer->buffer = buffer; | |
1071 | devc->transfers = g_slist_append(devc->transfers, xfer); | |
1291ea43 | 1072 | } |
1291ea43 GS |
1073 | devc->transfer_bufsize = bufsize; |
1074 | ||
1075 | return SR_OK; | |
1076 | } | |
1077 | ||
1078 | static int la2016_usbxfer_cancel_all(const struct sr_dev_inst *sdi) | |
1079 | { | |
1080 | struct dev_context *devc; | |
796ce0bf | 1081 | GSList *l; |
1291ea43 GS |
1082 | struct libusb_transfer *xfer; |
1083 | ||
1084 | devc = sdi ? sdi->priv : NULL; | |
1085 | if (!devc) | |
1086 | return SR_ERR_ARG; | |
1087 | ||
1088 | /* Unconditionally cancel the transfer. Ignore errors. */ | |
796ce0bf GS |
1089 | for (l = devc->transfers; l; l = l->next) { |
1090 | xfer = l->data; | |
1091 | if (!xfer) | |
1092 | continue; | |
1291ea43 | 1093 | libusb_cancel_transfer(xfer); |
796ce0bf | 1094 | } |
1291ea43 GS |
1095 | |
1096 | return SR_OK; | |
1097 | } | |
1098 | ||
1099 | static int la2016_usbxfer_resubmit(const struct sr_dev_inst *sdi, | |
1100 | struct libusb_transfer *xfer) | |
1101 | { | |
1102 | struct dev_context *devc; | |
1103 | struct sr_usb_dev_inst *usb; | |
1104 | libusb_transfer_cb_fn cb; | |
1105 | int ret; | |
1106 | ||
1107 | devc = sdi ? sdi->priv : NULL; | |
1108 | usb = sdi ? sdi->conn : NULL; | |
1109 | if (!devc || !usb) | |
1110 | return SR_ERR_ARG; | |
1111 | ||
1112 | if (!xfer) | |
1113 | return SR_ERR_ARG; | |
1114 | ||
1115 | cb = receive_transfer; | |
1116 | libusb_fill_bulk_transfer(xfer, usb->devhdl, | |
1117 | USB_EP_CAPTURE_DATA | LIBUSB_ENDPOINT_IN, | |
1118 | xfer->buffer, devc->transfer_bufsize, | |
1119 | cb, (void *)sdi, CAPTURE_TIMEOUT_MS); | |
1120 | ret = libusb_submit_transfer(xfer); | |
1121 | if (ret != 0) { | |
1122 | sr_err("Cannot submit USB transfer: %s.", | |
1123 | libusb_error_name(ret)); | |
1124 | return SR_ERR_IO; | |
1125 | } | |
1126 | ||
1127 | return SR_OK; | |
1128 | } | |
1129 | ||
1130 | static int la2016_usbxfer_submit_all(const struct sr_dev_inst *sdi) | |
1131 | { | |
1132 | struct dev_context *devc; | |
796ce0bf GS |
1133 | GSList *l; |
1134 | struct libusb_transfer *xfer; | |
1291ea43 GS |
1135 | int ret; |
1136 | ||
1137 | devc = sdi ? sdi->priv : NULL; | |
1138 | if (!devc) | |
1139 | return SR_ERR_ARG; | |
1140 | ||
796ce0bf GS |
1141 | for (l = devc->transfers; l; l = l->next) { |
1142 | xfer = l->data; | |
1143 | if (!xfer) | |
1144 | return SR_ERR_ARG; | |
1145 | ret = la2016_usbxfer_resubmit(sdi, xfer); | |
1146 | if (ret != SR_OK) | |
1147 | return ret; | |
1148 | } | |
1291ea43 GS |
1149 | |
1150 | return SR_OK; | |
1151 | } | |
1152 | ||
9270f8f4 GS |
1153 | SR_PRIV int la2016_setup_acquisition(const struct sr_dev_inst *sdi, |
1154 | double voltage) | |
f2cd2deb | 1155 | { |
0fbb464b | 1156 | struct dev_context *devc; |
f2cd2deb FS |
1157 | int ret; |
1158 | uint8_t cmd; | |
1159 | ||
0fbb464b GS |
1160 | devc = sdi->priv; |
1161 | ||
9270f8f4 | 1162 | ret = set_threshold_voltage(sdi, voltage); |
f2cd2deb FS |
1163 | if (ret != SR_OK) |
1164 | return ret; | |
1165 | ||
0fbb464b | 1166 | cmd = devc->continuous ? CAPTMODE_STREAM : CAPTMODE_TO_RAM; |
411ad77c GS |
1167 | ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_CAPT_MODE, 0, &cmd, sizeof(cmd)); |
1168 | if (ret != SR_OK) { | |
91f73872 | 1169 | sr_err("Cannot send command to stop sampling."); |
f2cd2deb FS |
1170 | return ret; |
1171 | } | |
1172 | ||
1173 | ret = set_trigger_config(sdi); | |
1174 | if (ret != SR_OK) | |
1175 | return ret; | |
1176 | ||
1177 | ret = set_sample_config(sdi); | |
1178 | if (ret != SR_OK) | |
1179 | return ret; | |
1180 | ||
1181 | return SR_OK; | |
1182 | } | |
1183 | ||
1184 | SR_PRIV int la2016_start_acquisition(const struct sr_dev_inst *sdi) | |
1185 | { | |
0fbb464b | 1186 | struct dev_context *devc; |
3ebc1cb2 GS |
1187 | int ret; |
1188 | ||
0fbb464b GS |
1189 | devc = sdi->priv; |
1190 | ||
1291ea43 GS |
1191 | ret = la2016_usbxfer_allocate(sdi); |
1192 | if (ret != SR_OK) | |
1193 | return ret; | |
1194 | ||
0fbb464b GS |
1195 | if (devc->continuous) { |
1196 | ret = ctrl_out(sdi, CMD_BULK_RESET, 0x00, 0, NULL, 0); | |
1197 | if (ret != SR_OK) | |
1198 | return ret; | |
1199 | ||
1200 | ret = la2016_usbxfer_submit_all(sdi); | |
1201 | if (ret != SR_OK) | |
1202 | return ret; | |
1203 | ||
1204 | /* | |
1205 | * Periodic receive callback will set runmode. This | |
1206 | * activity MUST be close to data reception, a pause | |
1207 | * between these steps breaks the stream's operation. | |
1208 | */ | |
1209 | } else { | |
1210 | ret = set_run_mode(sdi, RUNMODE_RUN); | |
1211 | if (ret != SR_OK) | |
1212 | return ret; | |
1213 | } | |
3ebc1cb2 GS |
1214 | |
1215 | return SR_OK; | |
f2cd2deb FS |
1216 | } |
1217 | ||
3ebc1cb2 | 1218 | static int la2016_stop_acquisition(const struct sr_dev_inst *sdi) |
f2cd2deb | 1219 | { |
0fbb464b | 1220 | struct dev_context *devc; |
3ebc1cb2 GS |
1221 | int ret; |
1222 | ||
852c7d14 | 1223 | ret = set_run_mode(sdi, RUNMODE_HALT); |
3ebc1cb2 GS |
1224 | if (ret != SR_OK) |
1225 | return ret; | |
1226 | ||
0fbb464b GS |
1227 | devc = sdi->priv; |
1228 | if (devc->continuous) | |
1229 | devc->download_finished = TRUE; | |
1230 | ||
3ebc1cb2 | 1231 | return SR_OK; |
f2cd2deb FS |
1232 | } |
1233 | ||
1234 | SR_PRIV int la2016_abort_acquisition(const struct sr_dev_inst *sdi) | |
1235 | { | |
3ebc1cb2 | 1236 | int ret; |
3ebc1cb2 GS |
1237 | |
1238 | ret = la2016_stop_acquisition(sdi); | |
1239 | if (ret != SR_OK) | |
1240 | return ret; | |
1241 | ||
1291ea43 | 1242 | (void)la2016_usbxfer_cancel_all(sdi); |
3ebc1cb2 GS |
1243 | |
1244 | return SR_OK; | |
f2cd2deb FS |
1245 | } |
1246 | ||
1291ea43 | 1247 | static int la2016_start_download(const struct sr_dev_inst *sdi) |
f2cd2deb FS |
1248 | { |
1249 | struct dev_context *devc; | |
f2cd2deb | 1250 | int ret; |
3ab60908 | 1251 | uint8_t wrbuf[REG_SAMPLING - REG_BULK]; /* Width of REG_BULK. */ |
c3d40037 | 1252 | uint8_t *wrptr; |
f2cd2deb FS |
1253 | |
1254 | devc = sdi->priv; | |
f2cd2deb | 1255 | |
411ad77c GS |
1256 | ret = get_capture_info(sdi); |
1257 | if (ret != SR_OK) | |
f2cd2deb FS |
1258 | return ret; |
1259 | ||
038e65c1 GS |
1260 | devc->n_transfer_packets_to_read = devc->info.n_rep_packets; |
1261 | devc->n_transfer_packets_to_read /= devc->packets_per_chunk; | |
1262 | devc->n_bytes_to_read = devc->n_transfer_packets_to_read; | |
1263 | devc->n_bytes_to_read *= TRANSFER_PACKET_LENGTH; | |
f2cd2deb FS |
1264 | devc->read_pos = devc->info.write_pos - devc->n_bytes_to_read; |
1265 | devc->n_reps_until_trigger = devc->info.n_rep_packets_before_trigger; | |
1266 | ||
91f73872 | 1267 | sr_dbg("Want to read %u xfer-packets starting from pos %" PRIu32 ".", |
1ed93110 | 1268 | devc->n_transfer_packets_to_read, devc->read_pos); |
f2cd2deb | 1269 | |
411ad77c GS |
1270 | ret = ctrl_out(sdi, CMD_BULK_RESET, 0x00, 0, NULL, 0); |
1271 | if (ret != SR_OK) { | |
91f73872 | 1272 | sr_err("Cannot reset USB bulk state."); |
f2cd2deb FS |
1273 | return ret; |
1274 | } | |
91f73872 GS |
1275 | sr_dbg("Will read from 0x%08lx, 0x%08x bytes.", |
1276 | (unsigned long)devc->read_pos, devc->n_bytes_to_read); | |
c3d40037 HK |
1277 | wrptr = wrbuf; |
1278 | write_u32le_inc(&wrptr, devc->read_pos); | |
1279 | write_u32le_inc(&wrptr, devc->n_bytes_to_read); | |
411ad77c GS |
1280 | ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_BULK, 0, wrbuf, wrptr - wrbuf); |
1281 | if (ret != SR_OK) { | |
91f73872 | 1282 | sr_err("Cannot send USB bulk config."); |
f2cd2deb FS |
1283 | return ret; |
1284 | } | |
1291ea43 GS |
1285 | |
1286 | ret = la2016_usbxfer_submit_all(sdi); | |
411ad77c | 1287 | if (ret != SR_OK) { |
1291ea43 | 1288 | sr_err("Cannot submit USB bulk transfers."); |
f2cd2deb FS |
1289 | return ret; |
1290 | } | |
1291 | ||
1291ea43 GS |
1292 | ret = ctrl_out(sdi, CMD_BULK_START, 0x00, 0, NULL, 0); |
1293 | if (ret != SR_OK) { | |
1294 | sr_err("Cannot start USB bulk transfers."); | |
1295 | return ret; | |
f2cd2deb FS |
1296 | } |
1297 | ||
1298 | return SR_OK; | |
1299 | } | |
1300 | ||
480efba2 GS |
1301 | /* |
1302 | * A chunk (received via USB) contains a number of transfers (USB length | |
1303 | * divided by 16) which contain a number of packets (5 per transfer) which | |
1304 | * contain a number of samples (8bit repeat count per 16bit sample data). | |
1305 | */ | |
dfac9592 | 1306 | static void send_chunk(struct sr_dev_inst *sdi, |
c7d14e31 | 1307 | const uint8_t *data_buffer, size_t data_length) |
dfac9592 GS |
1308 | { |
1309 | struct dev_context *devc; | |
c7d14e31 | 1310 | size_t num_xfers, num_pkts; |
dfac9592 | 1311 | const uint8_t *rp; |
4276ca94 | 1312 | uint32_t sample_value; |
480efba2 GS |
1313 | size_t repetitions; |
1314 | uint8_t sample_buff[sizeof(sample_value)]; | |
dfac9592 GS |
1315 | |
1316 | devc = sdi->priv; | |
1317 | ||
a38f0f5e GS |
1318 | /* Ignore incoming USB data after complete sample data download. */ |
1319 | if (devc->download_finished) | |
1320 | return; | |
dfac9592 | 1321 | |
cf057ac4 | 1322 | if (devc->trigger_involved && !devc->trigger_marked && devc->info.n_rep_packets_before_trigger == 0) { |
a38f0f5e | 1323 | feed_queue_logic_send_trigger(devc->feed_queue); |
cf057ac4 | 1324 | devc->trigger_marked = TRUE; |
dfac9592 GS |
1325 | } |
1326 | ||
c7d14e31 GS |
1327 | /* |
1328 | * Adjust the number of remaining bytes to read from the device | |
1329 | * before the processing of the currently received chunk affects | |
1330 | * the variable which holds the number of received bytes. | |
1331 | */ | |
1332 | if (data_length > devc->n_bytes_to_read) | |
1333 | devc->n_bytes_to_read = 0; | |
1334 | else | |
1335 | devc->n_bytes_to_read -= data_length; | |
1336 | ||
1337 | /* Process the received chunk of capture data. */ | |
4276ca94 | 1338 | sample_value = 0; |
c7d14e31 GS |
1339 | rp = data_buffer; |
1340 | num_xfers = data_length / TRANSFER_PACKET_LENGTH; | |
480efba2 | 1341 | while (num_xfers--) { |
038e65c1 | 1342 | num_pkts = devc->packets_per_chunk; |
480efba2 | 1343 | while (num_pkts--) { |
dfac9592 | 1344 | |
4276ca94 GS |
1345 | /* TODO Verify 32channel layout. */ |
1346 | if (devc->model->channel_count == 32) | |
1347 | sample_value = read_u32le_inc(&rp); | |
1348 | else if (devc->model->channel_count == 16) | |
1349 | sample_value = read_u16le_inc(&rp); | |
dfac9592 | 1350 | repetitions = read_u8_inc(&rp); |
dfac9592 | 1351 | |
dfac9592 | 1352 | devc->total_samples += repetitions; |
480efba2 | 1353 | |
4276ca94 | 1354 | write_u32le(sample_buff, sample_value); |
a38f0f5e GS |
1355 | feed_queue_logic_submit(devc->feed_queue, |
1356 | sample_buff, repetitions); | |
1357 | sr_sw_limits_update_samples_read(&devc->sw_limits, | |
1358 | repetitions); | |
480efba2 | 1359 | |
cf057ac4 GS |
1360 | if (devc->trigger_involved && !devc->trigger_marked) { |
1361 | if (!--devc->n_reps_until_trigger) { | |
a38f0f5e | 1362 | feed_queue_logic_send_trigger(devc->feed_queue); |
cf057ac4 | 1363 | devc->trigger_marked = TRUE; |
91f73872 | 1364 | sr_dbg("Trigger position after %" PRIu64 " samples, %.6fms.", |
1ed93110 | 1365 | devc->total_samples, |
edc0b015 | 1366 | (double)devc->total_samples / devc->samplerate * 1e3); |
dfac9592 GS |
1367 | } |
1368 | } | |
1369 | } | |
1370 | (void)read_u8_inc(&rp); /* Skip sequence number. */ | |
1371 | } | |
a38f0f5e | 1372 | |
c7d14e31 GS |
1373 | /* |
1374 | * Check for several conditions which shall terminate the | |
1375 | * capture data download: When the amount of capture data in | |
1376 | * the device is exhausted. When the user specified samples | |
1377 | * count limit is reached. | |
1378 | */ | |
1379 | if (!devc->n_bytes_to_read) { | |
1380 | devc->download_finished = TRUE; | |
1381 | } else { | |
1382 | sr_dbg("%" PRIu32 " more bytes to download from the device.", | |
1383 | devc->n_bytes_to_read); | |
1384 | } | |
a38f0f5e GS |
1385 | if (!devc->download_finished && sr_sw_limits_check(&devc->sw_limits)) { |
1386 | sr_dbg("Acquisition limit reached."); | |
1387 | devc->download_finished = TRUE; | |
1388 | } | |
1389 | if (devc->download_finished) { | |
1390 | sr_dbg("Download finished, flushing session feed queue."); | |
1391 | feed_queue_logic_flush(devc->feed_queue); | |
dfac9592 | 1392 | } |
a38f0f5e | 1393 | sr_dbg("Total samples after chunk: %" PRIu64 ".", devc->total_samples); |
dfac9592 GS |
1394 | } |
1395 | ||
0fbb464b GS |
1396 | /* |
1397 | * Process a chunk of capture data in streaming mode. The memory layout | |
1398 | * is rather different from "normal mode" (see the send_chunk() routine | |
1399 | * above). In streaming mode data is not compressed, and memory cells | |
1400 | * neither contain raw sampled pin values at a given point in time. The | |
1401 | * memory content needs transformation. | |
1402 | * - The memory content can be seen as a sequence of memory cells. | |
1403 | * - Each cell contains samples that correspond to the same channel. | |
1404 | * The next cell contains samples for the next channel, etc. | |
1405 | * - Only enabled channels occupy memory cells. Disabled channels are | |
1406 | * not part of the capture data memory layout. | |
1407 | * - The LSB bit position in a cell is the sample which was taken first | |
1408 | * for this channel. Upper bit positions were taken later. | |
1409 | * | |
1410 | * Implementor's note: This routine is inspired by convert_sample_data() | |
1411 | * in the https://github.com/AlexUg/sigrok implementation. Which in turn | |
1412 | * appears to have been derived from the saleae-logic16 sigrok driver. | |
1413 | * The code is phrased conservatively to verify the layout as discussed | |
1414 | * above, performance was not a priority. Operation was verified with an | |
1415 | * LA2016 device. The memory layout of 32 channel models is yet to get | |
1416 | * determined. | |
1417 | */ | |
1418 | static void stream_data(struct sr_dev_inst *sdi, | |
1419 | const uint8_t *data_buffer, size_t data_length) | |
1420 | { | |
1421 | struct dev_context *devc; | |
1422 | struct stream_state_t *stream; | |
1423 | size_t bit_count; | |
1424 | const uint8_t *rp; | |
1425 | uint32_t sample_value; | |
1426 | uint8_t sample_buff[sizeof(sample_value)]; | |
1427 | size_t bit_idx; | |
1428 | uint32_t ch_mask; | |
1429 | ||
1430 | devc = sdi->priv; | |
1431 | stream = &devc->stream; | |
1432 | ||
1433 | /* Ignore incoming USB data after complete sample data download. */ | |
1434 | if (devc->download_finished) | |
1435 | return; | |
1436 | sr_dbg("Stream mode, got another chunk: %p, length %zu.", | |
1437 | data_buffer, data_length); | |
1438 | ||
1439 | /* TODO Add soft trigger support when in stream mode? */ | |
1440 | ||
1441 | /* | |
1442 | * TODO Are memory cells always as wide as the channel count? | |
1443 | * Are they always 16bits wide? Verify for 32 channel devices. | |
1444 | */ | |
1445 | bit_count = devc->model->channel_count; | |
1446 | if (bit_count == 32) { | |
1447 | data_length /= sizeof(uint32_t); | |
1448 | } else if (bit_count == 16) { | |
1449 | data_length /= sizeof(uint16_t); | |
1450 | } else { | |
1451 | /* | |
1452 | * Unhandled case. Acquisition should not start. | |
1453 | * The statement silences the compiler. | |
1454 | */ | |
1455 | return; | |
1456 | } | |
1457 | rp = data_buffer; | |
1458 | sample_value = 0; | |
1459 | while (data_length--) { | |
1460 | /* Get another entity. */ | |
1461 | if (bit_count == 32) | |
1462 | sample_value = read_u32le_inc(&rp); | |
1463 | else if (bit_count == 16) | |
1464 | sample_value = read_u16le_inc(&rp); | |
1465 | ||
1466 | /* Map the entity's bits to a channel's samples. */ | |
1467 | ch_mask = stream->channel_masks[stream->channel_index]; | |
1468 | for (bit_idx = 0; bit_idx < bit_count; bit_idx++) { | |
1469 | if (sample_value & (1UL << bit_idx)) | |
1470 | stream->sample_data[bit_idx] |= ch_mask; | |
1471 | } | |
1472 | ||
1473 | /* | |
1474 | * Advance to the next channel. Submit a block of | |
1475 | * samples when all channels' data was seen. | |
1476 | */ | |
1477 | stream->channel_index++; | |
1478 | if (stream->channel_index != stream->enabled_count) | |
1479 | continue; | |
1480 | for (bit_idx = 0; bit_idx < bit_count; bit_idx++) { | |
1481 | sample_value = stream->sample_data[bit_idx]; | |
1482 | write_u32le(sample_buff, sample_value); | |
1483 | feed_queue_logic_submit(devc->feed_queue, sample_buff, 1); | |
1484 | } | |
1485 | sr_sw_limits_update_samples_read(&devc->sw_limits, bit_count); | |
1486 | devc->total_samples += bit_count; | |
1487 | memset(stream->sample_data, 0, sizeof(stream->sample_data)); | |
1488 | stream->channel_index = 0; | |
1489 | } | |
1490 | ||
1491 | /* | |
1492 | * Need we count empty or failed USB transfers? This version | |
1493 | * doesn't, assumes that timeouts are perfectly legal because | |
1494 | * transfers are started early, and slow samplerates or trigger | |
1495 | * support in hardware are plausible causes for empty transfers. | |
1496 | * | |
1497 | * TODO Maybe a good condition would be (rather large) a timeout | |
1498 | * after a previous capture data chunk was seen? So that stalled | |
1499 | * streaming gets detected which _is_ an exceptional condition. | |
1500 | * We have observed these when "runmode" is set early but bulk | |
1501 | * transfers start late with a pause after setting the runmode. | |
1502 | */ | |
1503 | if (sr_sw_limits_check(&devc->sw_limits)) { | |
1504 | sr_dbg("Acquisition end reached (sw limits)."); | |
1505 | devc->download_finished = TRUE; | |
1506 | } | |
1507 | if (devc->download_finished) { | |
1508 | sr_dbg("Stream receive done, flushing session feed queue."); | |
1509 | feed_queue_logic_flush(devc->feed_queue); | |
1510 | } | |
1511 | sr_dbg("Total samples after chunk: %" PRIu64 ".", devc->total_samples); | |
1512 | } | |
1513 | ||
dfac9592 GS |
1514 | static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer) |
1515 | { | |
1516 | struct sr_dev_inst *sdi; | |
1517 | struct dev_context *devc; | |
462a2f0a | 1518 | gboolean was_cancelled, device_gone; |
dfac9592 GS |
1519 | int ret; |
1520 | ||
1521 | sdi = transfer->user_data; | |
1522 | devc = sdi->priv; | |
dfac9592 | 1523 | |
1291ea43 | 1524 | was_cancelled = transfer->status == LIBUSB_TRANSFER_CANCELLED; |
462a2f0a | 1525 | device_gone = transfer->status == LIBUSB_TRANSFER_NO_DEVICE; |
dfac9592 | 1526 | sr_dbg("receive_transfer(): status %s received %d bytes.", |
1ed93110 | 1527 | libusb_error_name(transfer->status), transfer->actual_length); |
462a2f0a GS |
1528 | if (device_gone) { |
1529 | sr_warn("Lost communication to USB device."); | |
1530 | devc->download_finished = TRUE; | |
1531 | return; | |
1532 | } | |
1533 | ||
a38f0f5e GS |
1534 | /* |
1535 | * Implementation detail: A USB transfer timeout is not fatal | |
1536 | * here. We just process whatever was received, empty input is | |
1537 | * perfectly acceptable. Reaching (or exceeding) the sw limits | |
1538 | * or exhausting the device's captured data will complete the | |
1539 | * sample data download. | |
1540 | */ | |
0fbb464b GS |
1541 | if (devc->continuous) |
1542 | stream_data(sdi, transfer->buffer, transfer->actual_length); | |
1543 | else | |
1544 | send_chunk(sdi, transfer->buffer, transfer->actual_length); | |
c7d14e31 | 1545 | |
1291ea43 GS |
1546 | /* |
1547 | * Re-submit completed transfers (regardless of timeout or | |
1548 | * data reception), unless the transfer was cancelled when | |
1549 | * the acquisition was terminated or has completed. | |
1550 | */ | |
1551 | if (!was_cancelled && !devc->download_finished) { | |
1552 | ret = la2016_usbxfer_resubmit(sdi, transfer); | |
1553 | if (ret == SR_OK) | |
dfac9592 | 1554 | return; |
c7d14e31 | 1555 | devc->download_finished = TRUE; |
dfac9592 | 1556 | } |
dfac9592 GS |
1557 | } |
1558 | ||
1559 | SR_PRIV int la2016_receive_data(int fd, int revents, void *cb_data) | |
1560 | { | |
1561 | const struct sr_dev_inst *sdi; | |
1562 | struct dev_context *devc; | |
1563 | struct drv_context *drvc; | |
1564 | struct timeval tv; | |
a38f0f5e | 1565 | int ret; |
dfac9592 GS |
1566 | |
1567 | (void)fd; | |
1568 | (void)revents; | |
1569 | ||
1570 | sdi = cb_data; | |
1571 | devc = sdi->priv; | |
1572 | drvc = sdi->driver->context; | |
1573 | ||
0fbb464b GS |
1574 | /* Arrange for the start of stream mode when requested. */ |
1575 | if (devc->continuous && !devc->frame_begin_sent) { | |
1576 | sr_dbg("First receive callback in stream mode."); | |
1577 | devc->download_finished = FALSE; | |
1578 | devc->trigger_marked = FALSE; | |
1579 | devc->total_samples = 0; | |
1580 | ||
1581 | std_session_send_df_frame_begin(sdi); | |
1582 | devc->frame_begin_sent = TRUE; | |
1583 | ||
1584 | ret = set_run_mode(sdi, RUNMODE_RUN); | |
1585 | if (ret != SR_OK) { | |
1586 | sr_err("Cannot set 'runmode' to 'run'."); | |
1587 | return FALSE; | |
1588 | } | |
1589 | ||
1590 | ret = ctrl_out(sdi, CMD_BULK_START, 0x00, 0, NULL, 0); | |
1591 | if (ret != SR_OK) { | |
1592 | sr_err("Cannot start USB bulk transfers."); | |
1593 | return FALSE; | |
1594 | } | |
1595 | sr_dbg("Stream data reception initiated."); | |
1596 | } | |
1597 | ||
a38f0f5e GS |
1598 | /* |
1599 | * Wait for the acquisition to complete in hardware. | |
1600 | * Periodically check a potentially configured msecs timeout. | |
1601 | */ | |
0fbb464b | 1602 | if (!devc->continuous && !devc->completion_seen) { |
cf057ac4 | 1603 | if (!la2016_is_idle(sdi)) { |
a38f0f5e GS |
1604 | if (sr_sw_limits_check(&devc->sw_limits)) { |
1605 | devc->sw_limits.limit_msec = 0; | |
1606 | sr_dbg("Limit reached. Stopping acquisition."); | |
1607 | la2016_stop_acquisition(sdi); | |
1608 | } | |
96dc954e | 1609 | /* Not yet ready for sample data download. */ |
dfac9592 GS |
1610 | return TRUE; |
1611 | } | |
a38f0f5e GS |
1612 | sr_dbg("Acquisition completion seen (hardware)."); |
1613 | devc->sw_limits.limit_msec = 0; | |
cf057ac4 GS |
1614 | devc->completion_seen = TRUE; |
1615 | devc->download_finished = FALSE; | |
1616 | devc->trigger_marked = FALSE; | |
dfac9592 | 1617 | devc->total_samples = 0; |
a38f0f5e | 1618 | |
33020165 GS |
1619 | la2016_dump_fpga_registers(sdi, "acquisition complete", 0, 0); |
1620 | ||
a38f0f5e GS |
1621 | /* Initiate the download of acquired sample data. */ |
1622 | std_session_send_df_frame_begin(sdi); | |
96a405ab | 1623 | devc->frame_begin_sent = TRUE; |
1291ea43 | 1624 | ret = la2016_start_download(sdi); |
a38f0f5e | 1625 | if (ret != SR_OK) { |
91f73872 | 1626 | sr_err("Cannot start acquisition data download."); |
dfac9592 GS |
1627 | return FALSE; |
1628 | } | |
91f73872 | 1629 | sr_dbg("Acquisition data download started."); |
dfac9592 GS |
1630 | |
1631 | return TRUE; | |
1632 | } | |
1633 | ||
a38f0f5e | 1634 | /* Handle USB reception. Drives sample data download. */ |
1291ea43 | 1635 | memset(&tv, 0, sizeof(tv)); |
dfac9592 GS |
1636 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); |
1637 | ||
0fbb464b GS |
1638 | /* |
1639 | * Periodically flush acquisition data in streaming mode. | |
1640 | * Without this nudge, previously received and accumulated data | |
1641 | * keeps sitting in queues and is not seen by applications. | |
1642 | */ | |
1643 | if (devc->continuous && devc->stream.flush_period_ms) { | |
1644 | uint64_t now, elapsed; | |
1645 | now = g_get_monotonic_time(); | |
1646 | if (!devc->stream.last_flushed) | |
1647 | devc->stream.last_flushed = now; | |
1648 | elapsed = now - devc->stream.last_flushed; | |
1649 | elapsed /= 1000; | |
1650 | if (elapsed >= devc->stream.flush_period_ms) { | |
1651 | sr_dbg("Stream mode, flushing."); | |
1652 | feed_queue_logic_flush(devc->feed_queue); | |
1653 | devc->stream.last_flushed = now; | |
1654 | } | |
1655 | } | |
1656 | ||
a38f0f5e | 1657 | /* Postprocess completion of sample data download. */ |
cf057ac4 | 1658 | if (devc->download_finished) { |
91f73872 | 1659 | sr_dbg("Download finished, post processing."); |
dfac9592 GS |
1660 | |
1661 | la2016_stop_acquisition(sdi); | |
a38f0f5e | 1662 | usb_source_remove(sdi->session, drvc->sr_ctx); |
1291ea43 GS |
1663 | |
1664 | la2016_usbxfer_cancel_all(sdi); | |
1665 | memset(&tv, 0, sizeof(tv)); | |
1666 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); | |
dfac9592 | 1667 | |
a38f0f5e GS |
1668 | feed_queue_logic_flush(devc->feed_queue); |
1669 | feed_queue_logic_free(devc->feed_queue); | |
1670 | devc->feed_queue = NULL; | |
96a405ab GS |
1671 | if (devc->frame_begin_sent) { |
1672 | std_session_send_df_frame_end(sdi); | |
1673 | devc->frame_begin_sent = FALSE; | |
1674 | } | |
a38f0f5e GS |
1675 | std_session_send_df_end(sdi); |
1676 | ||
91f73872 | 1677 | sr_dbg("Download finished, done post processing."); |
dfac9592 GS |
1678 | } |
1679 | ||
1680 | return TRUE; | |
1681 | } | |
1682 | ||
d466f61c GS |
1683 | SR_PRIV int la2016_identify_device(const struct sr_dev_inst *sdi, |
1684 | gboolean show_message) | |
f2cd2deb | 1685 | { |
8b172e78 | 1686 | struct dev_context *devc; |
3ab60908 | 1687 | uint8_t buf[8]; /* Larger size of manuf date and device type magic. */ |
64172b16 | 1688 | size_t rdoff, rdlen; |
43d2e52f GS |
1689 | const uint8_t *rdptr; |
1690 | uint8_t date_yy, date_mm; | |
1691 | uint8_t dinv_yy, dinv_mm; | |
9de389b1 | 1692 | uint8_t magic; |
d466f61c GS |
1693 | size_t model_idx; |
1694 | const struct kingst_model *model; | |
9de389b1 | 1695 | int ret; |
f2cd2deb | 1696 | |
8b172e78 KG |
1697 | devc = sdi->priv; |
1698 | ||
96dc954e | 1699 | /* |
43d2e52f GS |
1700 | * Four EEPROM bytes at offset 0x20 are the manufacturing date, |
1701 | * year and month in BCD format, followed by inverted values for | |
1702 | * consistency checks. For example bytes 20 04 df fb translate | |
1703 | * to 2020-04. This information can help identify the vintage of | |
1704 | * devices when unknown magic numbers are seen. | |
9de389b1 | 1705 | */ |
64172b16 GS |
1706 | rdoff = 0x20; |
1707 | rdlen = 4 * sizeof(uint8_t); | |
1708 | ret = ctrl_in(sdi, CMD_EEPROM, rdoff, 0, buf, rdlen); | |
d466f61c | 1709 | if (ret != SR_OK && !show_message) { |
64172b16 | 1710 | /* Non-fatal weak attempt during probe. Not worth logging. */ |
d466f61c GS |
1711 | sr_dbg("Cannot access EEPROM."); |
1712 | return SR_ERR_IO; | |
1713 | } else if (ret != SR_OK) { | |
64172b16 | 1714 | /* Failed attempt in regular use. Non-fatal. Worth logging. */ |
43d2e52f | 1715 | sr_err("Cannot read manufacture date in EEPROM."); |
1ed93110 | 1716 | } else { |
64172b16 GS |
1717 | if (sr_log_loglevel_get() >= SR_LOG_SPEW) { |
1718 | GString *txt; | |
1719 | txt = sr_hexdump_new(buf, rdlen); | |
1720 | sr_spew("Manufacture date bytes %s.", txt->str); | |
1721 | sr_hexdump_free(txt); | |
1722 | } | |
43d2e52f GS |
1723 | rdptr = &buf[0]; |
1724 | date_yy = read_u8_inc(&rdptr); | |
1725 | date_mm = read_u8_inc(&rdptr); | |
1726 | dinv_yy = read_u8_inc(&rdptr); | |
1727 | dinv_mm = read_u8_inc(&rdptr); | |
1728 | sr_info("Manufacture date: 20%02hx-%02hx.", date_yy, date_mm); | |
1729 | if ((date_mm ^ dinv_mm) != 0xff || (date_yy ^ dinv_yy) != 0xff) | |
1730 | sr_warn("Manufacture date fails checksum test."); | |
f2cd2deb | 1731 | } |
f2cd2deb | 1732 | |
9de389b1 | 1733 | /* |
96dc954e GS |
1734 | * Several Kingst logic analyzer devices share the same USB VID |
1735 | * and PID. The product ID determines which MCU firmware to load. | |
1736 | * The MCU firmware provides access to EEPROM content which then | |
1737 | * allows to identify the device model. Which in turn determines | |
1738 | * which FPGA bitstream to load. Eight bytes at offset 0x08 are | |
1739 | * to get inspected. | |
9de389b1 | 1740 | * |
96dc954e GS |
1741 | * EEPROM content for model identification is kept redundantly |
1742 | * in memory. The values are stored in verbatim and in inverted | |
1743 | * form, multiple copies are kept at different offsets. Example | |
1744 | * data: | |
9de389b1 | 1745 | * |
96dc954e GS |
1746 | * magic 0x08 |
1747 | * | ~magic 0xf7 | |
1748 | * | | | |
1749 | * 08f7000008f710ef | |
1750 | * | | | |
1751 | * | ~magic backup | |
1752 | * magic backup | |
9de389b1 | 1753 | * |
96dc954e GS |
1754 | * Exclusively inspecting the magic byte appears to be sufficient, |
1755 | * other fields seem to be 'don't care'. | |
9de389b1 | 1756 | * |
96dc954e GS |
1757 | * magic 2 == LA2016 using "kingst-la2016-fpga.bitstream" |
1758 | * magic 3 == LA1016 using "kingst-la1016-fpga.bitstream" | |
1759 | * magic 8 == LA2016a using "kingst-la2016a1-fpga.bitstream" | |
1760 | * (latest v1.3.0 PCB, perhaps others) | |
1761 | * magic 9 == LA1016a using "kingst-la1016a1-fpga.bitstream" | |
1762 | * (latest v1.3.0 PCB, perhaps others) | |
9de389b1 | 1763 | * |
96dc954e GS |
1764 | * When EEPROM content does not match the hardware configuration |
1765 | * (the board layout), the software may load but yield incorrect | |
1766 | * results (like swapped channels). The FPGA bitstream itself | |
1767 | * will authenticate with IC U10 and fail when its capabilities | |
1768 | * do not match the hardware model. An LA1016 won't become a | |
1769 | * LA2016 by faking its EEPROM content. | |
9de389b1 | 1770 | */ |
d466f61c | 1771 | devc->identify_magic = 0; |
64172b16 GS |
1772 | rdoff = 0x08; |
1773 | rdlen = 8 * sizeof(uint8_t); | |
1774 | ret = ctrl_in(sdi, CMD_EEPROM, rdoff, 0, &buf, rdlen); | |
1775 | if (ret != SR_OK) { | |
91f73872 | 1776 | sr_err("Cannot read EEPROM device identifier bytes."); |
f2cd2deb FS |
1777 | return ret; |
1778 | } | |
64172b16 GS |
1779 | if (sr_log_loglevel_get() >= SR_LOG_SPEW) { |
1780 | GString *txt; | |
1781 | txt = sr_hexdump_new(buf, rdlen); | |
1782 | sr_spew("EEPROM magic bytes %s.", txt->str); | |
1783 | sr_hexdump_free(txt); | |
1784 | } | |
43d2e52f | 1785 | if ((buf[0] ^ buf[1]) == 0xff) { |
96dc954e | 1786 | /* Primary copy of magic passes complement check. */ |
9de389b1 | 1787 | magic = buf[0]; |
64172b16 | 1788 | sr_dbg("Using primary magic, value %d.", (int)magic); |
43d2e52f | 1789 | } else if ((buf[4] ^ buf[5]) == 0xff) { |
96dc954e | 1790 | /* Backup copy of magic passes complement check. */ |
9de389b1 | 1791 | magic = buf[4]; |
64172b16 | 1792 | sr_dbg("Using backup magic, value %d.", (int)magic); |
43d2e52f GS |
1793 | } else { |
1794 | sr_err("Cannot find consistent device type identification."); | |
1795 | magic = 0; | |
f2cd2deb | 1796 | } |
d466f61c | 1797 | devc->identify_magic = magic; |
9de389b1 | 1798 | |
d466f61c GS |
1799 | devc->model = NULL; |
1800 | for (model_idx = 0; model_idx < ARRAY_SIZE(models); model_idx++) { | |
1801 | model = &models[model_idx]; | |
1802 | if (model->magic != magic) | |
1803 | continue; | |
1804 | devc->model = model; | |
64172b16 GS |
1805 | sr_info("Model '%s', %zu channels, max %" PRIu64 "MHz.", |
1806 | model->name, model->channel_count, | |
1807 | model->samplerate / SR_MHZ(1)); | |
d466f61c GS |
1808 | devc->fpga_bitstream = g_strdup_printf(FPGA_FWFILE_FMT, |
1809 | model->fpga_stem); | |
d466f61c | 1810 | sr_info("FPGA bitstream file '%s'.", devc->fpga_bitstream); |
d6f89d4b GS |
1811 | break; |
1812 | } | |
d466f61c | 1813 | if (!devc->model) { |
91f73872 | 1814 | sr_err("Cannot identify as one of the supported models."); |
286b3e13 | 1815 | return SR_ERR_DATA; |
3f48ab02 | 1816 | } |
f2cd2deb | 1817 | |
d466f61c GS |
1818 | return SR_OK; |
1819 | } | |
1820 | ||
6d53e949 | 1821 | SR_PRIV int la2016_init_hardware(const struct sr_dev_inst *sdi) |
d466f61c GS |
1822 | { |
1823 | struct dev_context *devc; | |
1824 | const char *bitstream_fn; | |
1825 | int ret; | |
1826 | uint16_t state; | |
1827 | ||
1828 | devc = sdi->priv; | |
1829 | bitstream_fn = devc ? devc->fpga_bitstream : ""; | |
1830 | ||
1831 | ret = check_fpga_bitstream(sdi); | |
1832 | if (ret != SR_OK) { | |
d6f89d4b GS |
1833 | ret = upload_fpga_bitstream(sdi, bitstream_fn); |
1834 | if (ret != SR_OK) { | |
1835 | sr_err("Cannot upload FPGA bitstream."); | |
1836 | return ret; | |
1837 | } | |
1838 | } | |
1839 | ret = enable_fpga_bitstream(sdi); | |
9de389b1 | 1840 | if (ret != SR_OK) { |
d6f89d4b | 1841 | sr_err("Cannot enable FPGA bitstream after upload."); |
9de389b1 KG |
1842 | return ret; |
1843 | } | |
1844 | ||
f2cd2deb | 1845 | state = run_state(sdi); |
44947217 GS |
1846 | if ((state & 0xfff0) != 0x85e0) { |
1847 | sr_warn("Unexpected run state, want 0x85eX, got 0x%04x.", state); | |
9de389b1 | 1848 | } |
f2cd2deb | 1849 | |
6d53e949 GS |
1850 | ret = ctrl_out(sdi, CMD_BULK_RESET, 0x00, 0, NULL, 0); |
1851 | if (ret != SR_OK) { | |
91f73872 | 1852 | sr_err("Cannot reset USB bulk transfer."); |
f2cd2deb FS |
1853 | return ret; |
1854 | } | |
9de389b1 | 1855 | |
91f73872 | 1856 | sr_dbg("Device should be initialized."); |
f2cd2deb | 1857 | |
6d53e949 GS |
1858 | return SR_OK; |
1859 | } | |
1860 | ||
6d53e949 | 1861 | SR_PRIV int la2016_deinit_hardware(const struct sr_dev_inst *sdi) |
f2cd2deb FS |
1862 | { |
1863 | int ret; | |
1864 | ||
6d53e949 GS |
1865 | ret = ctrl_out(sdi, CMD_FPGA_ENABLE, 0x00, 0, NULL, 0); |
1866 | if (ret != SR_OK) { | |
91f73872 | 1867 | sr_err("Cannot deinitialize device's FPGA."); |
f2cd2deb FS |
1868 | return ret; |
1869 | } | |
1870 | ||
1871 | return SR_OK; | |
1872 | } | |
08a49848 | 1873 | |
1291ea43 GS |
1874 | SR_PRIV void la2016_release_resources(const struct sr_dev_inst *sdi) |
1875 | { | |
1876 | (void)la2016_usbxfer_release(sdi); | |
1877 | } | |
1878 | ||
08a49848 GS |
1879 | SR_PRIV int la2016_write_pwm_config(const struct sr_dev_inst *sdi, size_t idx) |
1880 | { | |
1881 | return set_pwm_config(sdi, idx); | |
1882 | } |