]>
Commit | Line | Data |
---|---|---|
f2cd2deb FS |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2020 Florian Schmidt <schmidt_florian@gmx.de> | |
5 | * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se> | |
6 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
7 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
8 | * | |
9 | * This program is free software: you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation, either version 3 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | */ | |
22 | ||
23 | #include <config.h> | |
a7740b06 | 24 | |
f2cd2deb | 25 | #include <libsigrok/libsigrok.h> |
a7740b06 GS |
26 | #include <string.h> |
27 | ||
f2cd2deb FS |
28 | #include "libsigrok-internal.h" |
29 | #include "protocol.h" | |
30 | ||
f2cd2deb | 31 | #define UC_FIRMWARE "kingst-la-%04x.fw" |
9de389b1 KG |
32 | #define FPGA_FW_LA2016 "kingst-la2016-fpga.bitstream" |
33 | #define FPGA_FW_LA2016A "kingst-la2016a1-fpga.bitstream" | |
8b172e78 KG |
34 | #define FPGA_FW_LA1016 "kingst-la1016-fpga.bitstream" |
35 | #define FPGA_FW_LA1016A "kingst-la1016a1-fpga.bitstream" | |
f2cd2deb | 36 | |
852c7d14 | 37 | /* Maximum device capabilities. May differ between models. */ |
8b172e78 KG |
38 | #define MAX_SAMPLE_RATE_LA2016 SR_MHZ(200) |
39 | #define MAX_SAMPLE_RATE_LA1016 SR_MHZ(100) | |
e9430410 GS |
40 | #define MAX_SAMPLE_DEPTH 10e9 |
41 | #define MAX_PWM_FREQ SR_MHZ(20) | |
42 | #define PWM_CLOCK SR_MHZ(200) /* 200MHz for both LA2016 and LA1016 */ | |
f2cd2deb | 43 | |
852c7d14 GS |
44 | /* |
45 | * Default device configuration. Must be applicable to any of the | |
46 | * supported devices (no model specific default values yet). Specific | |
47 | * firmware implementation details unfortunately won't let us detect | |
48 | * and keep using previously configured values. | |
49 | */ | |
50 | #define LA2016_DFLT_SAMPLERATE SR_MHZ(100) | |
51 | #define LA2016_DFLT_SAMPLEDEPTH (5 * 1000 * 1000) | |
52 | #define LA2016_DFLT_CAPT_RATIO 5 /* Capture ratio, in percent. */ | |
53 | ||
54 | /* TODO | |
55 | * What is the origin and motivation of that 128Mi literal? What is its | |
56 | * unit? How does it relate to a device's hardware capabilities? How to | |
57 | * map the 1GiB of RAM of an LA2016 (at 16 channels) to the 128Mi value? | |
58 | * It cannot be sample count. Is it memory size in bytes perhaps? | |
59 | */ | |
60 | #define LA2016_PRE_MEM_LIMIT_BASE (128 * 1024 * 1024) | |
61 | ||
96dc954e | 62 | /* USB vendor class control requests, executed by the Cypress FX2 MCU. */ |
84fe94bd | 63 | #define CMD_FPGA_ENABLE 0x10 |
96dc954e GS |
64 | #define CMD_FPGA_SPI 0x20 /* R/W access to FPGA registers via SPI. */ |
65 | #define CMD_BULK_START 0x30 /* Start sample data download via USB EP6 IN. */ | |
66 | #define CMD_BULK_RESET 0x38 /* Flush FIFO of FX2 USB EP6 IN. */ | |
67 | #define CMD_FPGA_INIT 0x50 /* Used before and after FPGA bitstream upload. */ | |
68 | #define CMD_KAUTH 0x60 /* Communicate to auth IC (U10). Not used. */ | |
69 | #define CMD_EEPROM 0xa2 /* R/W access to EEPROM content. */ | |
00849545 | 70 | |
42f6dd55 | 71 | /* |
96dc954e GS |
72 | * FPGA register addresses (base addresses when registers span multiple |
73 | * bytes, in that case data is kept in little endian format). Passed to | |
74 | * CMD_FPGA_SPI requests. The FX2 MCU transparently handles the detail | |
75 | * of SPI transfers encoding the read (1) or write (0) direction in the | |
76 | * MSB of the address field. There are some 60 byte-wide FPGA registers. | |
d6f89d4b GS |
77 | * |
78 | * Unfortunately the FPGA registers change their meaning between the | |
79 | * read and write directions of access, or exclusively provide one of | |
80 | * these directions and not the other. This is an arbitrary vendor's | |
81 | * choice, there is nothing which the sigrok driver could do about it. | |
82 | * Values written to registers typically cannot get read back, neither | |
83 | * verified after writing a configuration, nor queried upon startup for | |
84 | * automatic detection of the current configuration. Neither appear to | |
85 | * be there echo registers for presence and communication checks, nor | |
86 | * version identifying registers, as far as we know. | |
42f6dd55 | 87 | */ |
96dc954e GS |
88 | #define REG_RUN 0x00 /* Read capture status, write start capture. */ |
89 | #define REG_PWM_EN 0x02 /* User PWM channels on/off. */ | |
90 | #define REG_CAPT_MODE 0x03 /* Write 0x00 capture to SDRAM, 0x01 streaming. */ | |
91 | #define REG_BULK 0x08 /* Write start addr, byte count to download samples. */ | |
92 | #define REG_SAMPLING 0x10 /* Write capture config, read capture SDRAM location. */ | |
93 | #define REG_TRIGGER 0x20 /* write level and edge trigger config. */ | |
94 | #define REG_THRESHOLD 0x68 /* Write PWM config to setup input threshold DAC. */ | |
95 | #define REG_PWM1 0x70 /* Write config for user PWM1. */ | |
96 | #define REG_PWM2 0x78 /* Write config for user PWM2. */ | |
f2cd2deb | 97 | |
852c7d14 GS |
98 | /* Bit patterns to write to REG_RUN, setup run mode. */ |
99 | #define RUNMODE_HALT 0x00 | |
100 | #define RUNMODE_RUN 0x03 | |
101 | ||
f2cd2deb | 102 | static int ctrl_in(const struct sr_dev_inst *sdi, |
1ed93110 GS |
103 | uint8_t bRequest, uint16_t wValue, uint16_t wIndex, |
104 | void *data, uint16_t wLength) | |
f2cd2deb FS |
105 | { |
106 | struct sr_usb_dev_inst *usb; | |
107 | int ret; | |
108 | ||
109 | usb = sdi->conn; | |
110 | ||
852c7d14 GS |
111 | if ((ret = libusb_control_transfer(usb->devhdl, |
112 | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN, | |
f2cd2deb FS |
113 | bRequest, wValue, wIndex, (unsigned char *)data, wLength, |
114 | DEFAULT_TIMEOUT_MS)) != wLength) { | |
91f73872 GS |
115 | sr_dbg("USB ctrl in: %d bytes, req %d val %#x idx %d: %s.", |
116 | wLength, bRequest, wValue, wIndex, | |
117 | libusb_error_name(ret)); | |
118 | sr_err("Cannot read %d bytes from USB: %s.", | |
119 | wLength, libusb_error_name(ret)); | |
f2cd2deb FS |
120 | return SR_ERR; |
121 | } | |
122 | ||
123 | return SR_OK; | |
124 | } | |
125 | ||
126 | static int ctrl_out(const struct sr_dev_inst *sdi, | |
1ed93110 GS |
127 | uint8_t bRequest, uint16_t wValue, uint16_t wIndex, |
128 | void *data, uint16_t wLength) | |
f2cd2deb FS |
129 | { |
130 | struct sr_usb_dev_inst *usb; | |
131 | int ret; | |
132 | ||
133 | usb = sdi->conn; | |
134 | ||
852c7d14 GS |
135 | if ((ret = libusb_control_transfer(usb->devhdl, |
136 | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT, | |
f2cd2deb FS |
137 | bRequest, wValue, wIndex, (unsigned char*)data, wLength, |
138 | DEFAULT_TIMEOUT_MS)) != wLength) { | |
91f73872 GS |
139 | sr_dbg("USB ctrl out: %d bytes, req %d val %#x idx %d: %s.", |
140 | wLength, bRequest, wValue, wIndex, | |
141 | libusb_error_name(ret)); | |
142 | sr_err("Cannot write %d bytes to USB: %s.", | |
143 | wLength, libusb_error_name(ret)); | |
f2cd2deb FS |
144 | return SR_ERR; |
145 | } | |
146 | ||
147 | return SR_OK; | |
148 | } | |
149 | ||
d6f89d4b GS |
150 | /* |
151 | * Check the necessity for FPGA bitstream upload, because another upload | |
152 | * would take some 600ms which is undesirable after program startup. Try | |
153 | * to access some FPGA registers and check the values' plausibility. The | |
154 | * check should fail on the safe side, request another upload when in | |
155 | * doubt. A positive response (the request to continue operation with the | |
156 | * currently active bitstream) should be conservative. Accessing multiple | |
157 | * registers is considered cheap compared to the cost of bitstream upload. | |
158 | * | |
159 | * It helps though that both the vendor software and the sigrok driver | |
160 | * use the same bundle of MCU firmware and FPGA bitstream for any of the | |
161 | * supported models. We don't expect to successfully communicate to the | |
162 | * device yet disagree on its protocol. Ideally we would access version | |
163 | * identifying registers for improved robustness, but are not aware of | |
164 | * any. A bitstream reload can always be forced by a power cycle. | |
165 | */ | |
166 | static int check_fpga_bitstream(const struct sr_dev_inst *sdi) | |
167 | { | |
168 | uint8_t init_rsp; | |
169 | int ret; | |
170 | uint16_t run_state; | |
171 | uint8_t pwm_en; | |
172 | size_t read_len; | |
173 | uint8_t buff[sizeof(run_state)]; | |
174 | const uint8_t *rdptr; | |
175 | ||
176 | sr_dbg("Checking operation of the FPGA bitstream."); | |
177 | ||
852c7d14 | 178 | init_rsp = ~0; |
d6f89d4b GS |
179 | ret = ctrl_in(sdi, CMD_FPGA_INIT, 0x00, 0, &init_rsp, sizeof(init_rsp)); |
180 | if (ret != SR_OK || init_rsp != 0) { | |
181 | sr_dbg("FPGA init query failed, or unexpected response."); | |
182 | return SR_ERR_IO; | |
183 | } | |
184 | ||
185 | read_len = sizeof(run_state); | |
186 | ret = ctrl_in(sdi, CMD_FPGA_SPI, REG_RUN, 0, buff, read_len); | |
187 | if (ret != SR_OK) { | |
188 | sr_dbg("FPGA register access failed (run state)."); | |
189 | return SR_ERR_IO; | |
190 | } | |
191 | rdptr = buff; | |
192 | run_state = read_u16le_inc(&rdptr); | |
193 | sr_spew("FPGA register: run state 0x%04x.", run_state); | |
194 | if (run_state && (run_state & 0x3) != 0x1) { | |
195 | sr_dbg("Unexpected FPGA register content (run state)."); | |
196 | return SR_ERR_DATA; | |
197 | } | |
198 | if (run_state && (run_state & ~0xf) != 0x85e0) { | |
199 | sr_dbg("Unexpected FPGA register content (run state)."); | |
200 | return SR_ERR_DATA; | |
201 | } | |
202 | ||
203 | read_len = sizeof(pwm_en); | |
204 | ret = ctrl_in(sdi, CMD_FPGA_SPI, REG_PWM_EN, 0, buff, read_len); | |
205 | if (ret != SR_OK) { | |
206 | sr_dbg("FPGA register access failed (PWM enable)."); | |
207 | return SR_ERR_IO; | |
208 | } | |
209 | rdptr = buff; | |
210 | pwm_en = read_u8_inc(&rdptr); | |
211 | sr_spew("FPGA register: PWM enable 0x%02x.", pwm_en); | |
212 | if ((pwm_en & 0x3) != 0x0) { | |
213 | sr_dbg("Unexpected FPGA register content (PWM enable)."); | |
214 | return SR_ERR_DATA; | |
215 | } | |
216 | ||
217 | sr_info("Could re-use current FPGA bitstream. No upload required."); | |
218 | return SR_OK; | |
219 | } | |
220 | ||
1ed93110 GS |
221 | static int upload_fpga_bitstream(const struct sr_dev_inst *sdi, |
222 | const char *bitstream_fname) | |
f2cd2deb FS |
223 | { |
224 | struct drv_context *drvc; | |
225 | struct sr_usb_dev_inst *usb; | |
226 | struct sr_resource bitstream; | |
b0d0131e | 227 | uint32_t bitstream_size; |
c3d40037 HK |
228 | uint8_t buffer[sizeof(uint32_t)]; |
229 | uint8_t *wrptr; | |
f2cd2deb | 230 | uint8_t block[4096]; |
3f48ab02 FS |
231 | int len, act_len; |
232 | unsigned int pos; | |
f2cd2deb | 233 | int ret; |
b0d0131e | 234 | unsigned int zero_pad_to; |
f2cd2deb FS |
235 | |
236 | drvc = sdi->driver->context; | |
237 | usb = sdi->conn; | |
238 | ||
9de389b1 | 239 | sr_info("Uploading FPGA bitstream '%s'.", bitstream_fname); |
f2cd2deb | 240 | |
9de389b1 | 241 | ret = sr_resource_open(drvc->sr_ctx, &bitstream, SR_RESOURCE_FIRMWARE, bitstream_fname); |
f2cd2deb | 242 | if (ret != SR_OK) { |
91f73872 | 243 | sr_err("Cannot find FPGA bitstream %s.", bitstream_fname); |
f2cd2deb FS |
244 | return ret; |
245 | } | |
246 | ||
b0d0131e | 247 | bitstream_size = (uint32_t)bitstream.size; |
c3d40037 | 248 | wrptr = buffer; |
b0d0131e | 249 | write_u32le_inc(&wrptr, bitstream_size); |
00849545 | 250 | if ((ret = ctrl_out(sdi, CMD_FPGA_INIT, 0x00, 0, buffer, wrptr - buffer)) != SR_OK) { |
91f73872 | 251 | sr_err("Cannot initiate FPGA bitstream upload."); |
f2cd2deb FS |
252 | sr_resource_close(drvc->sr_ctx, &bitstream); |
253 | return ret; | |
254 | } | |
b0d0131e GS |
255 | zero_pad_to = bitstream_size; |
256 | zero_pad_to += LA2016_EP2_PADDING - 1; | |
257 | zero_pad_to /= LA2016_EP2_PADDING; | |
258 | zero_pad_to *= LA2016_EP2_PADDING; | |
f2cd2deb FS |
259 | |
260 | pos = 0; | |
261 | while (1) { | |
3f48ab02 FS |
262 | if (pos < bitstream.size) { |
263 | len = (int)sr_resource_read(drvc->sr_ctx, &bitstream, &block, sizeof(block)); | |
264 | if (len < 0) { | |
91f73872 | 265 | sr_err("Cannot read FPGA bitstream."); |
3f48ab02 FS |
266 | sr_resource_close(drvc->sr_ctx, &bitstream); |
267 | return SR_ERR; | |
268 | } | |
269 | } else { | |
96dc954e | 270 | /* Zero-pad until 'zero_pad_to'. */ |
3f48ab02 FS |
271 | len = zero_pad_to - pos; |
272 | if ((unsigned)len > sizeof(block)) | |
273 | len = sizeof(block); | |
274 | memset(&block, 0, len); | |
f2cd2deb FS |
275 | } |
276 | if (len == 0) | |
277 | break; | |
278 | ||
852c7d14 | 279 | ret = libusb_bulk_transfer(usb->devhdl, USB_EP_FPGA_BITSTREAM, |
1ed93110 | 280 | &block[0], len, &act_len, DEFAULT_TIMEOUT_MS); |
f2cd2deb | 281 | if (ret != 0) { |
91f73872 GS |
282 | sr_dbg("Cannot write FPGA bitstream, block %#x len %d: %s.", |
283 | pos, (int)len, libusb_error_name(ret)); | |
f2cd2deb FS |
284 | ret = SR_ERR; |
285 | break; | |
286 | } | |
287 | if (act_len != len) { | |
91f73872 GS |
288 | sr_dbg("Short write for FPGA bitstream, block %#x len %d: got %d.", |
289 | pos, (int)len, act_len); | |
f2cd2deb FS |
290 | ret = SR_ERR; |
291 | break; | |
292 | } | |
293 | pos += len; | |
294 | } | |
295 | sr_resource_close(drvc->sr_ctx, &bitstream); | |
296 | if (ret != 0) | |
297 | return ret; | |
91f73872 GS |
298 | sr_info("FPGA bitstream upload (%" PRIu64 " bytes) done.", |
299 | bitstream.size); | |
f2cd2deb | 300 | |
d6f89d4b GS |
301 | return SR_OK; |
302 | } | |
303 | ||
304 | static int enable_fpga_bitstream(const struct sr_dev_inst *sdi) | |
305 | { | |
306 | int ret; | |
307 | uint8_t cmd_resp; | |
308 | ||
00849545 | 309 | if ((ret = ctrl_in(sdi, CMD_FPGA_INIT, 0x00, 0, &cmd_resp, sizeof(cmd_resp))) != SR_OK) { |
91f73872 | 310 | sr_err("Cannot read response after FPGA bitstream upload."); |
f2cd2deb FS |
311 | return ret; |
312 | } | |
3f48ab02 | 313 | if (cmd_resp != 0) { |
91f73872 GS |
314 | sr_err("Unexpected FPGA bitstream upload response, got 0x%02x, want 0.", |
315 | cmd_resp); | |
3f48ab02 FS |
316 | return SR_ERR; |
317 | } | |
852c7d14 | 318 | g_usleep(30 * 1000); |
f2cd2deb | 319 | |
00849545 | 320 | if ((ret = ctrl_out(sdi, CMD_FPGA_ENABLE, 0x01, 0, NULL, 0)) != SR_OK) { |
91f73872 | 321 | sr_err("Cannot enable FPGA after bitstream upload."); |
f2cd2deb FS |
322 | return ret; |
323 | } | |
852c7d14 | 324 | g_usleep(40 * 1000); |
d6f89d4b | 325 | |
f2cd2deb FS |
326 | return SR_OK; |
327 | } | |
328 | ||
329 | static int set_threshold_voltage(const struct sr_dev_inst *sdi, float voltage) | |
330 | { | |
331 | struct dev_context *devc; | |
f2cd2deb FS |
332 | int ret; |
333 | ||
334 | devc = sdi->priv; | |
f2cd2deb | 335 | |
1ed93110 | 336 | uint16_t duty_R79, duty_R56; |
f2ad79d1 KG |
337 | uint8_t buf[2 * sizeof(uint16_t)]; |
338 | uint8_t *wrptr; | |
339 | ||
96dc954e | 340 | /* Clamp threshold setting to valid range for LA2016. */ |
f2ad79d1 KG |
341 | if (voltage > 4.0) { |
342 | voltage = 4.0; | |
1ed93110 | 343 | } else if (voltage < -4.0) { |
f2ad79d1 KG |
344 | voltage = -4.0; |
345 | } | |
346 | ||
347 | /* | |
96dc954e GS |
348 | * Two PWM output channels feed one DAC which generates a bias |
349 | * voltage, which offsets the input probe's voltage level, and | |
350 | * in combination with the FPGA pins' fixed threshold result in | |
351 | * a programmable input threshold from the user's perspective. | |
352 | * The PWM outputs can be seen on R79 and R56 respectively, the | |
353 | * frequency is 100kHz and the duty cycle varies. The R79 PWM | |
354 | * uses three discrete settings. The R56 PWM varies with desired | |
355 | * thresholds and depends on the R79 PWM configuration. See the | |
356 | * schematics comments which discuss the formulae. | |
f2ad79d1 KG |
357 | */ |
358 | if (voltage >= 2.9) { | |
96dc954e | 359 | duty_R79 = 0; /* PWM off (0V). */ |
f2ad79d1 | 360 | duty_R56 = (uint16_t)(302 * voltage - 363); |
c34f4a89 | 361 | } else if (voltage > -0.4) { |
96dc954e | 362 | duty_R79 = 0x00f2; /* 25% duty cycle. */ |
f2ad79d1 | 363 | duty_R56 = (uint16_t)(302 * voltage + 121); |
c34f4a89 GS |
364 | } else { |
365 | duty_R79 = 0x02d7; /* 72% duty cycle. */ | |
366 | duty_R56 = (uint16_t)(302 * voltage + 1090); | |
f2ad79d1 KG |
367 | } |
368 | ||
96dc954e | 369 | /* Clamp duty register values to sensible limits. */ |
f2ad79d1 KG |
370 | if (duty_R56 < 10) { |
371 | duty_R56 = 10; | |
1ed93110 | 372 | } else if (duty_R56 > 1100) { |
f2ad79d1 KG |
373 | duty_R56 = 1100; |
374 | } | |
375 | ||
91f73872 GS |
376 | sr_dbg("Set threshold voltage %.2fV.", voltage); |
377 | sr_dbg("Duty cycle values: R56 0x%04x, R79 0x%04x.", duty_R56, duty_R79); | |
f2ad79d1 KG |
378 | |
379 | wrptr = buf; | |
380 | write_u16le_inc(&wrptr, duty_R56); | |
381 | write_u16le_inc(&wrptr, duty_R79); | |
382 | ||
383 | ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_THRESHOLD, 0, buf, wrptr - buf); | |
f2cd2deb | 384 | if (ret != SR_OK) { |
91f73872 | 385 | sr_err("Cannot set threshold voltage %.2fV.", voltage); |
f2cd2deb FS |
386 | return ret; |
387 | } | |
388 | devc->threshold_voltage = voltage; | |
389 | ||
390 | return SR_OK; | |
391 | } | |
392 | ||
393 | static int enable_pwm(const struct sr_dev_inst *sdi, uint8_t p1, uint8_t p2) | |
394 | { | |
395 | struct dev_context *devc; | |
396 | uint8_t cfg; | |
397 | int ret; | |
398 | ||
399 | devc = sdi->priv; | |
400 | cfg = 0; | |
401 | ||
402 | if (p1) cfg |= 1 << 0; | |
403 | if (p2) cfg |= 1 << 1; | |
404 | ||
91f73872 | 405 | sr_dbg("Set PWM enable %d %d. Config 0x%02x.", p1, p2, cfg); |
42f6dd55 | 406 | ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_PWM_EN, 0, &cfg, sizeof(cfg)); |
f2cd2deb | 407 | if (ret != SR_OK) { |
91f73872 | 408 | sr_err("Cannot setup PWM enabled state."); |
f2cd2deb FS |
409 | return ret; |
410 | } | |
411 | devc->pwm_setting[0].enabled = (p1) ? 1 : 0; | |
412 | devc->pwm_setting[1].enabled = (p2) ? 1 : 0; | |
413 | ||
414 | return SR_OK; | |
415 | } | |
416 | ||
1ed93110 GS |
417 | static int set_pwm(const struct sr_dev_inst *sdi, uint8_t which, |
418 | float freq, float duty) | |
f2cd2deb | 419 | { |
42f6dd55 | 420 | int CTRL_PWM[] = { REG_PWM1, REG_PWM2 }; |
f2cd2deb FS |
421 | struct dev_context *devc; |
422 | pwm_setting_dev_t cfg; | |
423 | pwm_setting_t *setting; | |
424 | int ret; | |
c3d40037 HK |
425 | uint8_t buf[2 * sizeof(uint32_t)]; |
426 | uint8_t *wrptr; | |
f2cd2deb FS |
427 | |
428 | devc = sdi->priv; | |
429 | ||
852c7d14 | 430 | if (which < 1 || which > ARRAY_SIZE(CTRL_PWM)) { |
91f73872 | 431 | sr_err("Invalid PWM channel: %d.", which); |
f2cd2deb FS |
432 | return SR_ERR; |
433 | } | |
434 | if (freq > MAX_PWM_FREQ) { | |
91f73872 | 435 | sr_err("Too high a PWM frequency: %.1f.", freq); |
f2cd2deb FS |
436 | return SR_ERR; |
437 | } | |
438 | if (duty > 100 || duty < 0) { | |
91f73872 | 439 | sr_err("Invalid PWM duty cycle: %f.", duty); |
f2cd2deb FS |
440 | return SR_ERR; |
441 | } | |
442 | ||
443 | cfg.period = (uint32_t)(PWM_CLOCK / freq); | |
444 | cfg.duty = (uint32_t)(0.5f + (cfg.period * duty / 100.)); | |
91f73872 | 445 | sr_dbg("Set PWM%d period %d, duty %d.", which, cfg.period, cfg.duty); |
f2cd2deb | 446 | |
c3d40037 HK |
447 | wrptr = buf; |
448 | write_u32le_inc(&wrptr, cfg.period); | |
449 | write_u32le_inc(&wrptr, cfg.duty); | |
00849545 | 450 | ret = ctrl_out(sdi, CMD_FPGA_SPI, CTRL_PWM[which - 1], 0, buf, wrptr - buf); |
f2cd2deb | 451 | if (ret != SR_OK) { |
91f73872 GS |
452 | sr_err("Cannot setup PWM%d configuration %d %d.", |
453 | which, cfg.period, cfg.duty); | |
f2cd2deb FS |
454 | return ret; |
455 | } | |
456 | setting = &devc->pwm_setting[which - 1]; | |
457 | setting->freq = freq; | |
458 | setting->duty = duty; | |
f2cd2deb FS |
459 | |
460 | return SR_OK; | |
461 | } | |
462 | ||
463 | static int set_defaults(const struct sr_dev_inst *sdi) | |
464 | { | |
465 | struct dev_context *devc; | |
466 | int ret; | |
467 | ||
468 | devc = sdi->priv; | |
469 | ||
852c7d14 | 470 | devc->capture_ratio = LA2016_DFLT_CAPT_RATIO; |
f2cd2deb | 471 | devc->cur_channels = 0xffff; |
852c7d14 GS |
472 | devc->limit_samples = LA2016_DFLT_SAMPLEDEPTH; |
473 | devc->cur_samplerate = LA2016_DFLT_SAMPLERATE; | |
f2cd2deb FS |
474 | |
475 | ret = set_threshold_voltage(sdi, devc->threshold_voltage); | |
476 | if (ret) | |
477 | return ret; | |
478 | ||
479 | ret = enable_pwm(sdi, 0, 0); | |
480 | if (ret) | |
481 | return ret; | |
482 | ||
852c7d14 | 483 | ret = set_pwm(sdi, 1, SR_KHZ(1), 50); |
f2cd2deb FS |
484 | if (ret) |
485 | return ret; | |
486 | ||
852c7d14 | 487 | ret = set_pwm(sdi, 2, SR_KHZ(100), 50); |
f2cd2deb FS |
488 | if (ret) |
489 | return ret; | |
490 | ||
491 | ret = enable_pwm(sdi, 1, 1); | |
492 | if (ret) | |
493 | return ret; | |
494 | ||
495 | return SR_OK; | |
496 | } | |
497 | ||
498 | static int set_trigger_config(const struct sr_dev_inst *sdi) | |
499 | { | |
500 | struct dev_context *devc; | |
501 | struct sr_trigger *trigger; | |
502 | trigger_cfg_t cfg; | |
503 | GSList *stages; | |
504 | GSList *channel; | |
505 | struct sr_trigger_stage *stage1; | |
506 | struct sr_trigger_match *match; | |
507 | uint16_t ch_mask; | |
508 | int ret; | |
c3d40037 HK |
509 | uint8_t buf[4 * sizeof(uint32_t)]; |
510 | uint8_t *wrptr; | |
f2cd2deb FS |
511 | |
512 | devc = sdi->priv; | |
513 | trigger = sr_session_trigger_get(sdi->session); | |
514 | ||
515 | memset(&cfg, 0, sizeof(cfg)); | |
516 | ||
517 | cfg.channels = devc->cur_channels; | |
518 | ||
519 | if (trigger && trigger->stages) { | |
520 | stages = trigger->stages; | |
521 | stage1 = stages->data; | |
522 | if (stages->next) { | |
523 | sr_err("Only one trigger stage supported for now."); | |
524 | return SR_ERR; | |
525 | } | |
526 | channel = stage1->matches; | |
527 | while (channel) { | |
528 | match = channel->data; | |
529 | ch_mask = 1 << match->channel->index; | |
530 | ||
531 | switch (match->match) { | |
532 | case SR_TRIGGER_ZERO: | |
533 | cfg.level |= ch_mask; | |
534 | cfg.high_or_falling &= ~ch_mask; | |
535 | break; | |
536 | case SR_TRIGGER_ONE: | |
537 | cfg.level |= ch_mask; | |
538 | cfg.high_or_falling |= ch_mask; | |
539 | break; | |
540 | case SR_TRIGGER_RISING: | |
541 | if ((cfg.enabled & ~cfg.level)) { | |
91f73872 | 542 | sr_err("Device only supports one edge trigger."); |
f2cd2deb FS |
543 | return SR_ERR; |
544 | } | |
545 | cfg.level &= ~ch_mask; | |
546 | cfg.high_or_falling &= ~ch_mask; | |
547 | break; | |
548 | case SR_TRIGGER_FALLING: | |
549 | if ((cfg.enabled & ~cfg.level)) { | |
91f73872 | 550 | sr_err("Device only supports one edge trigger."); |
f2cd2deb FS |
551 | return SR_ERR; |
552 | } | |
553 | cfg.level &= ~ch_mask; | |
554 | cfg.high_or_falling |= ch_mask; | |
555 | break; | |
556 | default: | |
91f73872 | 557 | sr_err("Unknown trigger condition."); |
f2cd2deb FS |
558 | return SR_ERR; |
559 | } | |
560 | cfg.enabled |= ch_mask; | |
561 | channel = channel->next; | |
562 | } | |
563 | } | |
91f73872 GS |
564 | sr_dbg("Set trigger config: " |
565 | "channels 0x%04x, trigger-enabled 0x%04x, " | |
566 | "level-triggered 0x%04x, high/falling 0x%04x.", | |
567 | cfg.channels, cfg.enabled, cfg.level, cfg.high_or_falling); | |
f2cd2deb FS |
568 | |
569 | devc->had_triggers_configured = cfg.enabled != 0; | |
570 | ||
c3d40037 HK |
571 | wrptr = buf; |
572 | write_u32le_inc(&wrptr, cfg.channels); | |
573 | write_u32le_inc(&wrptr, cfg.enabled); | |
574 | write_u32le_inc(&wrptr, cfg.level); | |
575 | write_u32le_inc(&wrptr, cfg.high_or_falling); | |
852c7d14 GS |
576 | /* TODO |
577 | * Comment on this literal 16. Origin, meaning? Cannot be the | |
578 | * register offset, nor the transfer length. Is it a channels | |
579 | * count that is relevant for 16 and 32 channel models? Is it | |
580 | * an obsolete experiment? | |
581 | */ | |
42f6dd55 | 582 | ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_TRIGGER, 16, buf, wrptr - buf); |
f2cd2deb | 583 | if (ret != SR_OK) { |
91f73872 | 584 | sr_err("Cannot setup trigger configuration."); |
f2cd2deb FS |
585 | return ret; |
586 | } | |
587 | ||
588 | return SR_OK; | |
589 | } | |
590 | ||
591 | static int set_sample_config(const struct sr_dev_inst *sdi) | |
592 | { | |
593 | struct dev_context *devc; | |
f2cd2deb | 594 | double clock_divisor; |
adab4d91 GS |
595 | uint16_t divider_u16; |
596 | uint64_t pre_trigger_samples; | |
597 | uint64_t pre_trigger_memory; | |
598 | uint8_t buf[REG_TRIGGER - REG_SAMPLING]; /* Width of REG_SAMPLING. */ | |
c3d40037 | 599 | uint8_t *wrptr; |
adab4d91 | 600 | int ret; |
f2cd2deb FS |
601 | |
602 | devc = sdi->priv; | |
f2cd2deb | 603 | |
8b172e78 | 604 | if (devc->cur_samplerate > devc->max_samplerate) { |
91f73872 GS |
605 | sr_err("Too high a sample rate: %" PRIu64 ".", |
606 | devc->cur_samplerate); | |
f2cd2deb FS |
607 | return SR_ERR; |
608 | } | |
609 | ||
8b172e78 | 610 | clock_divisor = devc->max_samplerate / (double)devc->cur_samplerate; |
adab4d91 GS |
611 | if (clock_divisor > 65535) |
612 | return SR_ERR_ARG; | |
613 | divider_u16 = (uint16_t)(clock_divisor + 0.5); | |
614 | devc->cur_samplerate = devc->max_samplerate / divider_u16; | |
f2cd2deb FS |
615 | |
616 | if (devc->limit_samples > MAX_SAMPLE_DEPTH) { | |
91f73872 GS |
617 | sr_err("Too high a sample depth: %" PRIu64 ".", |
618 | devc->limit_samples); | |
f2cd2deb FS |
619 | return SR_ERR; |
620 | } | |
f2cd2deb | 621 | |
adab4d91 GS |
622 | /* |
623 | * The acquisition configuration communicates "pre-trigger" | |
624 | * specs in several formats. sigrok users provide a percentage | |
625 | * (0-100%), which translates to a pre-trigger samples count | |
626 | * (assuming that a total samples count limit was specified). | |
627 | * The device supports hardware compression, which depends on | |
628 | * slowly changing input data to be effective. Fast changing | |
629 | * input data may occupy more space in sample memory than its | |
630 | * uncompressed form would. This is why a third parameter can | |
631 | * limit the amount of sample memory to use for pre-trigger | |
632 | * data. Only the upper 24 bits of that memory size spec get | |
633 | * communicated to the device (written to its FPGA register). | |
634 | */ | |
635 | pre_trigger_samples = devc->limit_samples * devc->capture_ratio / 100; | |
636 | pre_trigger_memory = LA2016_PRE_MEM_LIMIT_BASE; | |
637 | pre_trigger_memory *= devc->capture_ratio; | |
638 | pre_trigger_memory /= 100; | |
639 | pre_trigger_memory &= 0xffffff00ul; /* Funny register layout. */ | |
f2cd2deb | 640 | |
adab4d91 GS |
641 | sr_dbg("Set sample config: %" PRIu64 "kHz, %" PRIu64 " samples.", |
642 | devc->cur_samplerate / 1000, devc->limit_samples); | |
643 | sr_dbg("Capture ratio %" PRIu64 "%%, count %" PRIu64 ", mem %" PRIu64 ".", | |
644 | devc->capture_ratio, pre_trigger_samples, pre_trigger_memory); | |
f2cd2deb | 645 | |
c3d40037 HK |
646 | wrptr = buf; |
647 | write_u32le_inc(&wrptr, devc->limit_samples); | |
0d8e1ffc | 648 | write_u8_inc(&wrptr, 0); |
adab4d91 GS |
649 | write_u32le_inc(&wrptr, pre_trigger_samples); |
650 | write_u32le_inc(&wrptr, pre_trigger_memory); | |
651 | write_u16le_inc(&wrptr, divider_u16); | |
0d8e1ffc | 652 | write_u8_inc(&wrptr, 0); |
42f6dd55 | 653 | ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_SAMPLING, 0, buf, wrptr - buf); |
f2cd2deb | 654 | if (ret != SR_OK) { |
91f73872 | 655 | sr_err("Cannot setup acquisition configuration."); |
f2cd2deb FS |
656 | return ret; |
657 | } | |
658 | ||
659 | return SR_OK; | |
660 | } | |
661 | ||
96dc954e GS |
662 | /* |
663 | * FPGA register REG_RUN holds the run state (u16le format). Bit fields | |
664 | * of interest: | |
665 | * bit 0: value 1 = idle | |
666 | * bit 1: value 1 = writing to SDRAM | |
667 | * bit 2: value 0 = waiting for trigger, 1 = trigger seen | |
668 | * bit 3: value 0 = pretrigger sampling, 1 = posttrigger sampling | |
669 | * The meaning of other bit fields is unknown. | |
7601dca7 | 670 | * |
96dc954e GS |
671 | * Typical values in order of appearance during execution: |
672 | * 0x85e2: pre-sampling, samples before the trigger position, | |
673 | * when capture ratio > 0% | |
674 | * 0x85ea: pre-sampling complete, now waiting for the trigger | |
675 | * (whilst sampling continuously) | |
676 | * 0x85ee: trigger seen, capturing post-trigger samples, running | |
677 | * 0x85ed: idle | |
f2cd2deb FS |
678 | */ |
679 | static uint16_t run_state(const struct sr_dev_inst *sdi) | |
680 | { | |
21d68fd9 GS |
681 | static uint16_t previous_state; |
682 | ||
f2cd2deb | 683 | int ret; |
21d68fd9 GS |
684 | uint16_t state; |
685 | uint8_t buff[sizeof(state)]; | |
686 | const uint8_t *rdptr; | |
687 | const char *label; | |
f2cd2deb | 688 | |
21d68fd9 | 689 | if ((ret = ctrl_in(sdi, CMD_FPGA_SPI, REG_RUN, 0, buff, sizeof(state))) != SR_OK) { |
91f73872 | 690 | sr_err("Cannot read run state."); |
f2cd2deb FS |
691 | return ret; |
692 | } | |
21d68fd9 GS |
693 | rdptr = buff; |
694 | state = read_u16le_inc(&rdptr); | |
7601dca7 | 695 | |
96dc954e GS |
696 | /* |
697 | * Avoid flooding the log, only dump values as they change. | |
698 | * The routine is called about every 50ms. | |
7601dca7 | 699 | */ |
84fe94bd | 700 | if (state != previous_state) { |
7601dca7 | 701 | previous_state = state; |
21d68fd9 GS |
702 | if ((state & 0x3) == 0x1) { |
703 | label = "idle"; | |
704 | } else if ((state & 0xf) == 0x2) { | |
705 | label = "pre-trigger sampling"; | |
706 | } else if ((state & 0xf) == 0xa) { | |
707 | label = "sampling, waiting for trigger"; | |
708 | } else if ((state & 0xf) == 0xe) { | |
709 | label = "post-trigger sampling"; | |
710 | } else { | |
711 | label = NULL; | |
712 | } | |
713 | if (label && *label) { | |
714 | sr_dbg("Run state: 0x%04x (%s).", state, label); | |
1ed93110 | 715 | } else { |
91f73872 | 716 | sr_dbg("Run state: 0x%04x.", state); |
7601dca7 KG |
717 | } |
718 | } | |
f2cd2deb FS |
719 | |
720 | return state; | |
721 | } | |
722 | ||
c34f4a89 GS |
723 | static int la2016_has_triggered(const struct sr_dev_inst *sdi) |
724 | { | |
725 | uint16_t state; | |
726 | ||
727 | state = run_state(sdi); | |
728 | if ((state & 0x3) == 0x1) | |
729 | return 1; | |
730 | ||
731 | return 0; | |
732 | } | |
733 | ||
734 | static int set_run_mode(const struct sr_dev_inst *sdi, uint8_t mode) | |
f2cd2deb FS |
735 | { |
736 | int ret; | |
737 | ||
c34f4a89 GS |
738 | if ((ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_RUN, 0, &mode, sizeof(mode))) != SR_OK) { |
739 | sr_err("Cannot configure run mode %d.", mode); | |
f2cd2deb FS |
740 | return ret; |
741 | } | |
742 | ||
743 | return SR_OK; | |
744 | } | |
745 | ||
746 | static int get_capture_info(const struct sr_dev_inst *sdi) | |
747 | { | |
748 | struct dev_context *devc; | |
749 | int ret; | |
c3d40037 HK |
750 | uint8_t buf[3 * sizeof(uint32_t)]; |
751 | const uint8_t *rdptr; | |
f2cd2deb FS |
752 | |
753 | devc = sdi->priv; | |
754 | ||
42f6dd55 | 755 | if ((ret = ctrl_in(sdi, CMD_FPGA_SPI, REG_SAMPLING, 0, buf, sizeof(buf))) != SR_OK) { |
91f73872 | 756 | sr_err("Cannot read capture info."); |
f2cd2deb FS |
757 | return ret; |
758 | } | |
c3d40037 HK |
759 | |
760 | rdptr = buf; | |
761 | devc->info.n_rep_packets = read_u32le_inc(&rdptr); | |
762 | devc->info.n_rep_packets_before_trigger = read_u32le_inc(&rdptr); | |
763 | devc->info.write_pos = read_u32le_inc(&rdptr); | |
f2cd2deb | 764 | |
91f73872 | 765 | sr_dbg("Capture info: n_rep_packets: 0x%08x/%d, before_trigger: 0x%08x/%d, write_pos: 0x%08x%d.", |
1ed93110 GS |
766 | devc->info.n_rep_packets, devc->info.n_rep_packets, |
767 | devc->info.n_rep_packets_before_trigger, | |
768 | devc->info.n_rep_packets_before_trigger, | |
769 | devc->info.write_pos, devc->info.write_pos); | |
f2cd2deb | 770 | |
852c7d14 GS |
771 | if (devc->info.n_rep_packets % NUM_PACKETS_IN_CHUNK) { |
772 | sr_warn("Unexpected packets count %lu, not a multiple of %d.", | |
773 | (unsigned long)devc->info.n_rep_packets, | |
774 | NUM_PACKETS_IN_CHUNK); | |
91f73872 | 775 | } |
f2cd2deb FS |
776 | |
777 | return SR_OK; | |
778 | } | |
779 | ||
1ed93110 GS |
780 | SR_PRIV int la2016_upload_firmware(struct sr_context *sr_ctx, |
781 | libusb_device *dev, uint16_t product_id) | |
f2cd2deb FS |
782 | { |
783 | char fw_file[1024]; | |
784 | snprintf(fw_file, sizeof(fw_file) - 1, UC_FIRMWARE, product_id); | |
40a0b2f4 | 785 | return ezusb_upload_firmware(sr_ctx, dev, USB_CONFIGURATION, fw_file); |
f2cd2deb FS |
786 | } |
787 | ||
788 | SR_PRIV int la2016_setup_acquisition(const struct sr_dev_inst *sdi) | |
789 | { | |
790 | struct dev_context *devc; | |
791 | int ret; | |
792 | uint8_t cmd; | |
793 | ||
794 | devc = sdi->priv; | |
795 | ||
796 | ret = set_threshold_voltage(sdi, devc->threshold_voltage); | |
797 | if (ret != SR_OK) | |
798 | return ret; | |
799 | ||
800 | cmd = 0; | |
42f6dd55 | 801 | if ((ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_CAPT_MODE, 0, &cmd, sizeof(cmd))) != SR_OK) { |
91f73872 | 802 | sr_err("Cannot send command to stop sampling."); |
f2cd2deb FS |
803 | return ret; |
804 | } | |
805 | ||
806 | ret = set_trigger_config(sdi); | |
807 | if (ret != SR_OK) | |
808 | return ret; | |
809 | ||
810 | ret = set_sample_config(sdi); | |
811 | if (ret != SR_OK) | |
812 | return ret; | |
813 | ||
814 | return SR_OK; | |
815 | } | |
816 | ||
817 | SR_PRIV int la2016_start_acquisition(const struct sr_dev_inst *sdi) | |
818 | { | |
3ebc1cb2 GS |
819 | int ret; |
820 | ||
852c7d14 | 821 | ret = set_run_mode(sdi, RUNMODE_RUN); |
3ebc1cb2 GS |
822 | if (ret != SR_OK) |
823 | return ret; | |
824 | ||
825 | return SR_OK; | |
f2cd2deb FS |
826 | } |
827 | ||
3ebc1cb2 | 828 | static int la2016_stop_acquisition(const struct sr_dev_inst *sdi) |
f2cd2deb | 829 | { |
3ebc1cb2 GS |
830 | int ret; |
831 | ||
852c7d14 | 832 | ret = set_run_mode(sdi, RUNMODE_HALT); |
3ebc1cb2 GS |
833 | if (ret != SR_OK) |
834 | return ret; | |
835 | ||
836 | return SR_OK; | |
f2cd2deb FS |
837 | } |
838 | ||
839 | SR_PRIV int la2016_abort_acquisition(const struct sr_dev_inst *sdi) | |
840 | { | |
3ebc1cb2 GS |
841 | int ret; |
842 | struct dev_context *devc; | |
843 | ||
844 | ret = la2016_stop_acquisition(sdi); | |
845 | if (ret != SR_OK) | |
846 | return ret; | |
847 | ||
848 | devc = sdi ? sdi->priv : NULL; | |
849 | if (devc && devc->transfer) | |
850 | libusb_cancel_transfer(devc->transfer); | |
851 | ||
852 | return SR_OK; | |
f2cd2deb FS |
853 | } |
854 | ||
1ed93110 GS |
855 | static int la2016_start_retrieval(const struct sr_dev_inst *sdi, |
856 | libusb_transfer_cb_fn cb) | |
f2cd2deb FS |
857 | { |
858 | struct dev_context *devc; | |
859 | struct sr_usb_dev_inst *usb; | |
860 | int ret; | |
c3d40037 HK |
861 | uint8_t wrbuf[2 * sizeof(uint32_t)]; |
862 | uint8_t *wrptr; | |
f2cd2deb FS |
863 | uint32_t to_read; |
864 | uint8_t *buffer; | |
865 | ||
866 | devc = sdi->priv; | |
867 | usb = sdi->conn; | |
868 | ||
869 | if ((ret = get_capture_info(sdi)) != SR_OK) | |
870 | return ret; | |
871 | ||
c3d40037 HK |
872 | devc->n_transfer_packets_to_read = devc->info.n_rep_packets / NUM_PACKETS_IN_CHUNK; |
873 | devc->n_bytes_to_read = devc->n_transfer_packets_to_read * TRANSFER_PACKET_LENGTH; | |
f2cd2deb FS |
874 | devc->read_pos = devc->info.write_pos - devc->n_bytes_to_read; |
875 | devc->n_reps_until_trigger = devc->info.n_rep_packets_before_trigger; | |
876 | ||
91f73872 | 877 | sr_dbg("Want to read %u xfer-packets starting from pos %" PRIu32 ".", |
1ed93110 | 878 | devc->n_transfer_packets_to_read, devc->read_pos); |
f2cd2deb | 879 | |
00849545 | 880 | if ((ret = ctrl_out(sdi, CMD_BULK_RESET, 0x00, 0, NULL, 0)) != SR_OK) { |
91f73872 | 881 | sr_err("Cannot reset USB bulk state."); |
f2cd2deb FS |
882 | return ret; |
883 | } | |
91f73872 GS |
884 | sr_dbg("Will read from 0x%08lx, 0x%08x bytes.", |
885 | (unsigned long)devc->read_pos, devc->n_bytes_to_read); | |
c3d40037 HK |
886 | wrptr = wrbuf; |
887 | write_u32le_inc(&wrptr, devc->read_pos); | |
888 | write_u32le_inc(&wrptr, devc->n_bytes_to_read); | |
42f6dd55 | 889 | if ((ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_BULK, 0, wrbuf, wrptr - wrbuf)) != SR_OK) { |
91f73872 | 890 | sr_err("Cannot send USB bulk config."); |
f2cd2deb FS |
891 | return ret; |
892 | } | |
00849545 | 893 | if ((ret = ctrl_out(sdi, CMD_BULK_START, 0x00, 0, NULL, 0)) != SR_OK) { |
91f73872 | 894 | sr_err("Cannot unblock USB bulk transfers."); |
f2cd2deb FS |
895 | return ret; |
896 | } | |
897 | ||
96dc954e GS |
898 | /* |
899 | * Pick a buffer size for all USB transfers. The buffer size | |
900 | * must be a multiple of the endpoint packet size. And cannot | |
901 | * exceed a maximum value. | |
902 | */ | |
f2cd2deb | 903 | to_read = devc->n_bytes_to_read; |
96dc954e GS |
904 | if (to_read >= LA2016_USB_BUFSZ) /* Multiple transfers. */ |
905 | to_read = LA2016_USB_BUFSZ; | |
906 | else /* One transfer. */ | |
e847645b | 907 | to_read = (to_read + (LA2016_EP6_PKTSZ-1)) & ~(LA2016_EP6_PKTSZ-1); |
f2cd2deb FS |
908 | buffer = g_try_malloc(to_read); |
909 | if (!buffer) { | |
91f73872 GS |
910 | sr_dbg("USB bulk transfer size %d bytes.", (int)to_read); |
911 | sr_err("Cannot allocate buffer for USB bulk transfer."); | |
f2cd2deb FS |
912 | return SR_ERR_MALLOC; |
913 | } | |
914 | ||
915 | devc->transfer = libusb_alloc_transfer(0); | |
852c7d14 GS |
916 | libusb_fill_bulk_transfer(devc->transfer, |
917 | usb->devhdl, USB_EP_CAPTURE_DATA | LIBUSB_ENDPOINT_IN, | |
918 | buffer, to_read, | |
f2cd2deb FS |
919 | cb, (void *)sdi, DEFAULT_TIMEOUT_MS); |
920 | ||
921 | if ((ret = libusb_submit_transfer(devc->transfer)) != 0) { | |
91f73872 | 922 | sr_err("Cannot submit USB transfer: %s.", libusb_error_name(ret)); |
f2cd2deb FS |
923 | libusb_free_transfer(devc->transfer); |
924 | devc->transfer = NULL; | |
925 | g_free(buffer); | |
926 | return SR_ERR; | |
927 | } | |
928 | ||
929 | return SR_OK; | |
930 | } | |
931 | ||
dfac9592 GS |
932 | static void send_chunk(struct sr_dev_inst *sdi, |
933 | const uint8_t *packets, unsigned int num_tfers) | |
934 | { | |
935 | struct dev_context *devc; | |
936 | struct sr_datafeed_logic logic; | |
937 | struct sr_datafeed_packet sr_packet; | |
938 | unsigned int max_samples, n_samples, total_samples, free_n_samples; | |
939 | unsigned int i, j, k; | |
940 | int do_signal_trigger; | |
fe953391 | 941 | uint8_t *wp; |
dfac9592 GS |
942 | const uint8_t *rp; |
943 | uint16_t state; | |
944 | uint8_t repetitions; | |
fe953391 | 945 | uint8_t sample_buff[sizeof(state)]; |
dfac9592 GS |
946 | |
947 | devc = sdi->priv; | |
948 | ||
fe953391 | 949 | logic.unitsize = sizeof(sample_buff); |
dfac9592 GS |
950 | logic.data = devc->convbuffer; |
951 | ||
952 | sr_packet.type = SR_DF_LOGIC; | |
953 | sr_packet.payload = &logic; | |
954 | ||
fe953391 | 955 | max_samples = devc->convbuffer_size / sizeof(sample_buff); |
dfac9592 | 956 | n_samples = 0; |
fe953391 | 957 | wp = devc->convbuffer; |
dfac9592 GS |
958 | total_samples = 0; |
959 | do_signal_trigger = 0; | |
960 | ||
961 | if (devc->had_triggers_configured && devc->reading_behind_trigger == 0 && devc->info.n_rep_packets_before_trigger == 0) { | |
962 | std_session_send_df_trigger(sdi); | |
963 | devc->reading_behind_trigger = 1; | |
964 | } | |
965 | ||
966 | rp = packets; | |
967 | for (i = 0; i < num_tfers; i++) { | |
968 | for (k = 0; k < NUM_PACKETS_IN_CHUNK; k++) { | |
969 | free_n_samples = max_samples - n_samples; | |
970 | if (free_n_samples < 256 || do_signal_trigger) { | |
971 | logic.length = n_samples * 2; | |
972 | sr_session_send(sdi, &sr_packet); | |
973 | n_samples = 0; | |
fe953391 | 974 | wp = devc->convbuffer; |
dfac9592 GS |
975 | if (do_signal_trigger) { |
976 | std_session_send_df_trigger(sdi); | |
977 | do_signal_trigger = 0; | |
978 | } | |
979 | } | |
980 | ||
981 | state = read_u16le_inc(&rp); | |
982 | repetitions = read_u8_inc(&rp); | |
fe953391 GS |
983 | write_u16le((void *)&sample_buff, state); |
984 | for (j = 0; j < repetitions; j++) { | |
985 | memcpy(wp, sample_buff, logic.unitsize); | |
986 | wp += logic.unitsize; | |
987 | } | |
dfac9592 GS |
988 | |
989 | n_samples += repetitions; | |
990 | total_samples += repetitions; | |
991 | devc->total_samples += repetitions; | |
992 | if (!devc->reading_behind_trigger) { | |
993 | devc->n_reps_until_trigger--; | |
994 | if (devc->n_reps_until_trigger == 0) { | |
995 | devc->reading_behind_trigger = 1; | |
996 | do_signal_trigger = 1; | |
91f73872 | 997 | sr_dbg("Trigger position after %" PRIu64 " samples, %.6fms.", |
1ed93110 GS |
998 | devc->total_samples, |
999 | (double)devc->total_samples / devc->cur_samplerate * 1e3); | |
dfac9592 GS |
1000 | } |
1001 | } | |
1002 | } | |
1003 | (void)read_u8_inc(&rp); /* Skip sequence number. */ | |
1004 | } | |
1005 | if (n_samples) { | |
fe953391 | 1006 | logic.length = n_samples * logic.unitsize; |
dfac9592 GS |
1007 | sr_session_send(sdi, &sr_packet); |
1008 | if (do_signal_trigger) { | |
1009 | std_session_send_df_trigger(sdi); | |
1010 | } | |
1011 | } | |
91f73872 | 1012 | sr_dbg("Send_chunk done after %u samples.", total_samples); |
dfac9592 GS |
1013 | } |
1014 | ||
1015 | static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer) | |
1016 | { | |
1017 | struct sr_dev_inst *sdi; | |
1018 | struct dev_context *devc; | |
1019 | struct sr_usb_dev_inst *usb; | |
1020 | int ret; | |
1021 | ||
1022 | sdi = transfer->user_data; | |
1023 | devc = sdi->priv; | |
1024 | usb = sdi->conn; | |
1025 | ||
1026 | sr_dbg("receive_transfer(): status %s received %d bytes.", | |
1ed93110 | 1027 | libusb_error_name(transfer->status), transfer->actual_length); |
dfac9592 GS |
1028 | |
1029 | if (transfer->status == LIBUSB_TRANSFER_TIMED_OUT) { | |
91f73872 | 1030 | sr_err("USB bulk transfer timeout."); |
dfac9592 GS |
1031 | devc->transfer_finished = 1; |
1032 | } | |
1033 | send_chunk(sdi, transfer->buffer, transfer->actual_length / TRANSFER_PACKET_LENGTH); | |
1034 | ||
1035 | devc->n_bytes_to_read -= transfer->actual_length; | |
1036 | if (devc->n_bytes_to_read) { | |
1037 | uint32_t to_read = devc->n_bytes_to_read; | |
96dc954e GS |
1038 | /* |
1039 | * Determine read size for the next USB transfer. Make | |
1040 | * the buffer size a multiple of the endpoint packet | |
1041 | * size. Don't exceed a maximum value. | |
1042 | */ | |
dfac9592 GS |
1043 | if (to_read >= LA2016_USB_BUFSZ) |
1044 | to_read = LA2016_USB_BUFSZ; | |
96dc954e | 1045 | else |
dfac9592 | 1046 | to_read = (to_read + (LA2016_EP6_PKTSZ-1)) & ~(LA2016_EP6_PKTSZ-1); |
852c7d14 GS |
1047 | libusb_fill_bulk_transfer(transfer, |
1048 | usb->devhdl, USB_EP_CAPTURE_DATA | LIBUSB_ENDPOINT_IN, | |
1049 | transfer->buffer, to_read, | |
dfac9592 GS |
1050 | receive_transfer, (void *)sdi, DEFAULT_TIMEOUT_MS); |
1051 | ||
1052 | if ((ret = libusb_submit_transfer(transfer)) == 0) | |
1053 | return; | |
91f73872 GS |
1054 | sr_err("Cannot submit another USB transfer: %s.", |
1055 | libusb_error_name(ret)); | |
dfac9592 GS |
1056 | } |
1057 | ||
1058 | g_free(transfer->buffer); | |
1059 | libusb_free_transfer(transfer); | |
1060 | devc->transfer_finished = 1; | |
1061 | } | |
1062 | ||
1063 | SR_PRIV int la2016_receive_data(int fd, int revents, void *cb_data) | |
1064 | { | |
1065 | const struct sr_dev_inst *sdi; | |
1066 | struct dev_context *devc; | |
1067 | struct drv_context *drvc; | |
1068 | struct timeval tv; | |
1069 | ||
1070 | (void)fd; | |
1071 | (void)revents; | |
1072 | ||
1073 | sdi = cb_data; | |
1074 | devc = sdi->priv; | |
1075 | drvc = sdi->driver->context; | |
1076 | ||
1077 | if (devc->have_trigger == 0) { | |
1078 | if (la2016_has_triggered(sdi) == 0) { | |
96dc954e | 1079 | /* Not yet ready for sample data download. */ |
dfac9592 GS |
1080 | return TRUE; |
1081 | } | |
1082 | devc->have_trigger = 1; | |
1083 | devc->transfer_finished = 0; | |
1084 | devc->reading_behind_trigger = 0; | |
1085 | devc->total_samples = 0; | |
96dc954e | 1086 | /* We can start downloading sample data. */ |
dfac9592 | 1087 | if (la2016_start_retrieval(sdi, receive_transfer) != SR_OK) { |
91f73872 | 1088 | sr_err("Cannot start acquisition data download."); |
dfac9592 GS |
1089 | return FALSE; |
1090 | } | |
91f73872 | 1091 | sr_dbg("Acquisition data download started."); |
dfac9592 GS |
1092 | std_session_send_df_frame_begin(sdi); |
1093 | ||
1094 | return TRUE; | |
1095 | } | |
1096 | ||
1097 | tv.tv_sec = tv.tv_usec = 0; | |
1098 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); | |
1099 | ||
1100 | if (devc->transfer_finished) { | |
91f73872 | 1101 | sr_dbg("Download finished, post processing."); |
dfac9592 GS |
1102 | std_session_send_df_frame_end(sdi); |
1103 | ||
1104 | usb_source_remove(sdi->session, drvc->sr_ctx); | |
1105 | std_session_send_df_end(sdi); | |
1106 | ||
1107 | la2016_stop_acquisition(sdi); | |
1108 | ||
1109 | g_free(devc->convbuffer); | |
1110 | devc->convbuffer = NULL; | |
1111 | ||
1112 | devc->transfer = NULL; | |
1113 | ||
91f73872 | 1114 | sr_dbg("Download finished, done post processing."); |
dfac9592 GS |
1115 | } |
1116 | ||
1117 | return TRUE; | |
1118 | } | |
1119 | ||
f2cd2deb FS |
1120 | SR_PRIV int la2016_init_device(const struct sr_dev_inst *sdi) |
1121 | { | |
8b172e78 | 1122 | struct dev_context *devc; |
f2cd2deb | 1123 | uint16_t state; |
9de389b1 | 1124 | uint8_t buf[8]; |
43d2e52f GS |
1125 | const uint8_t *rdptr; |
1126 | uint8_t date_yy, date_mm; | |
1127 | uint8_t dinv_yy, dinv_mm; | |
9de389b1 | 1128 | uint8_t magic; |
d6f89d4b | 1129 | const char *bitstream_fn; |
9de389b1 | 1130 | int ret; |
f2cd2deb | 1131 | |
8b172e78 KG |
1132 | devc = sdi->priv; |
1133 | ||
96dc954e | 1134 | /* |
43d2e52f GS |
1135 | * Four EEPROM bytes at offset 0x20 are the manufacturing date, |
1136 | * year and month in BCD format, followed by inverted values for | |
1137 | * consistency checks. For example bytes 20 04 df fb translate | |
1138 | * to 2020-04. This information can help identify the vintage of | |
1139 | * devices when unknown magic numbers are seen. | |
9de389b1 | 1140 | */ |
43d2e52f GS |
1141 | ret = ctrl_in(sdi, CMD_EEPROM, 0x20, 0, buf, 4 * sizeof(uint8_t)); |
1142 | if (ret != SR_OK) { | |
1143 | sr_err("Cannot read manufacture date in EEPROM."); | |
1ed93110 | 1144 | } else { |
43d2e52f GS |
1145 | rdptr = &buf[0]; |
1146 | date_yy = read_u8_inc(&rdptr); | |
1147 | date_mm = read_u8_inc(&rdptr); | |
1148 | dinv_yy = read_u8_inc(&rdptr); | |
1149 | dinv_mm = read_u8_inc(&rdptr); | |
1150 | sr_info("Manufacture date: 20%02hx-%02hx.", date_yy, date_mm); | |
1151 | if ((date_mm ^ dinv_mm) != 0xff || (date_yy ^ dinv_yy) != 0xff) | |
1152 | sr_warn("Manufacture date fails checksum test."); | |
f2cd2deb | 1153 | } |
f2cd2deb | 1154 | |
9de389b1 | 1155 | /* |
96dc954e GS |
1156 | * Several Kingst logic analyzer devices share the same USB VID |
1157 | * and PID. The product ID determines which MCU firmware to load. | |
1158 | * The MCU firmware provides access to EEPROM content which then | |
1159 | * allows to identify the device model. Which in turn determines | |
1160 | * which FPGA bitstream to load. Eight bytes at offset 0x08 are | |
1161 | * to get inspected. | |
9de389b1 | 1162 | * |
96dc954e GS |
1163 | * EEPROM content for model identification is kept redundantly |
1164 | * in memory. The values are stored in verbatim and in inverted | |
1165 | * form, multiple copies are kept at different offsets. Example | |
1166 | * data: | |
9de389b1 | 1167 | * |
96dc954e GS |
1168 | * magic 0x08 |
1169 | * | ~magic 0xf7 | |
1170 | * | | | |
1171 | * 08f7000008f710ef | |
1172 | * | | | |
1173 | * | ~magic backup | |
1174 | * magic backup | |
9de389b1 | 1175 | * |
96dc954e GS |
1176 | * Exclusively inspecting the magic byte appears to be sufficient, |
1177 | * other fields seem to be 'don't care'. | |
9de389b1 | 1178 | * |
96dc954e GS |
1179 | * magic 2 == LA2016 using "kingst-la2016-fpga.bitstream" |
1180 | * magic 3 == LA1016 using "kingst-la1016-fpga.bitstream" | |
1181 | * magic 8 == LA2016a using "kingst-la2016a1-fpga.bitstream" | |
1182 | * (latest v1.3.0 PCB, perhaps others) | |
1183 | * magic 9 == LA1016a using "kingst-la1016a1-fpga.bitstream" | |
1184 | * (latest v1.3.0 PCB, perhaps others) | |
9de389b1 | 1185 | * |
96dc954e GS |
1186 | * When EEPROM content does not match the hardware configuration |
1187 | * (the board layout), the software may load but yield incorrect | |
1188 | * results (like swapped channels). The FPGA bitstream itself | |
1189 | * will authenticate with IC U10 and fail when its capabilities | |
1190 | * do not match the hardware model. An LA1016 won't become a | |
1191 | * LA2016 by faking its EEPROM content. | |
9de389b1 | 1192 | */ |
9de389b1 | 1193 | if ((ret = ctrl_in(sdi, CMD_EEPROM, 0x08, 0, &buf, sizeof(buf))) != SR_OK) { |
91f73872 | 1194 | sr_err("Cannot read EEPROM device identifier bytes."); |
f2cd2deb FS |
1195 | return ret; |
1196 | } | |
43d2e52f | 1197 | if ((buf[0] ^ buf[1]) == 0xff) { |
96dc954e | 1198 | /* Primary copy of magic passes complement check. */ |
43d2e52f | 1199 | sr_dbg("Using primary copy of device type magic number."); |
9de389b1 | 1200 | magic = buf[0]; |
43d2e52f | 1201 | } else if ((buf[4] ^ buf[5]) == 0xff) { |
96dc954e | 1202 | /* Backup copy of magic passes complement check. */ |
91f73872 | 1203 | sr_dbg("Using backup copy of device type magic number."); |
9de389b1 | 1204 | magic = buf[4]; |
43d2e52f GS |
1205 | } else { |
1206 | sr_err("Cannot find consistent device type identification."); | |
1207 | magic = 0; | |
f2cd2deb | 1208 | } |
91f73872 | 1209 | sr_dbg("Device type: magic number is %hhu.", magic); |
9de389b1 | 1210 | |
96dc954e | 1211 | /* Select the FPGA bitstream depending on the model. */ |
9de389b1 KG |
1212 | switch (magic) { |
1213 | case 2: | |
d6f89d4b | 1214 | bitstream_fn = FPGA_FW_LA2016; |
8b172e78 KG |
1215 | devc->max_samplerate = MAX_SAMPLE_RATE_LA2016; |
1216 | break; | |
1217 | case 3: | |
d6f89d4b | 1218 | bitstream_fn = FPGA_FW_LA1016; |
8b172e78 | 1219 | devc->max_samplerate = MAX_SAMPLE_RATE_LA1016; |
9de389b1 KG |
1220 | break; |
1221 | case 8: | |
d6f89d4b | 1222 | bitstream_fn = FPGA_FW_LA2016A; |
8b172e78 KG |
1223 | devc->max_samplerate = MAX_SAMPLE_RATE_LA2016; |
1224 | break; | |
1225 | case 9: | |
d6f89d4b | 1226 | bitstream_fn = FPGA_FW_LA1016A; |
8b172e78 | 1227 | devc->max_samplerate = MAX_SAMPLE_RATE_LA1016; |
9de389b1 KG |
1228 | break; |
1229 | default: | |
d6f89d4b GS |
1230 | bitstream_fn = NULL; |
1231 | break; | |
1232 | } | |
1233 | if (!bitstream_fn || !*bitstream_fn) { | |
91f73872 | 1234 | sr_err("Cannot identify as one of the supported models."); |
3f48ab02 FS |
1235 | return SR_ERR; |
1236 | } | |
f2cd2deb | 1237 | |
d6f89d4b GS |
1238 | if (check_fpga_bitstream(sdi) != SR_OK) { |
1239 | ret = upload_fpga_bitstream(sdi, bitstream_fn); | |
1240 | if (ret != SR_OK) { | |
1241 | sr_err("Cannot upload FPGA bitstream."); | |
1242 | return ret; | |
1243 | } | |
1244 | } | |
1245 | ret = enable_fpga_bitstream(sdi); | |
9de389b1 | 1246 | if (ret != SR_OK) { |
d6f89d4b | 1247 | sr_err("Cannot enable FPGA bitstream after upload."); |
9de389b1 KG |
1248 | return ret; |
1249 | } | |
1250 | ||
f2cd2deb | 1251 | state = run_state(sdi); |
9de389b1 | 1252 | if (state != 0x85e9) { |
91f73872 | 1253 | sr_warn("Unexpected run state, want 0x85e9, got 0x%04x.", state); |
9de389b1 | 1254 | } |
f2cd2deb | 1255 | |
00849545 | 1256 | if ((ret = ctrl_out(sdi, CMD_BULK_RESET, 0x00, 0, NULL, 0)) != SR_OK) { |
91f73872 | 1257 | sr_err("Cannot reset USB bulk transfer."); |
f2cd2deb FS |
1258 | return ret; |
1259 | } | |
9de389b1 | 1260 | |
91f73872 | 1261 | sr_dbg("Device should be initialized."); |
f2cd2deb | 1262 | |
c34f4a89 GS |
1263 | ret = set_defaults(sdi); |
1264 | if (ret != SR_OK) | |
1265 | return ret; | |
1266 | ||
1267 | return SR_OK; | |
f2cd2deb FS |
1268 | } |
1269 | ||
1270 | SR_PRIV int la2016_deinit_device(const struct sr_dev_inst *sdi) | |
1271 | { | |
1272 | int ret; | |
1273 | ||
00849545 | 1274 | if ((ret = ctrl_out(sdi, CMD_FPGA_ENABLE, 0x00, 0, NULL, 0)) != SR_OK) { |
91f73872 | 1275 | sr_err("Cannot deinitialize device's FPGA."); |
f2cd2deb FS |
1276 | return ret; |
1277 | } | |
1278 | ||
1279 | return SR_OK; | |
1280 | } |