]>
Commit | Line | Data |
---|---|---|
adcb9951 JH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
5 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
4bd770f5 | 22 | #include <math.h> |
f05600f4 | 23 | #include <stdbool.h> |
adcb9951 JH |
24 | #include <glib.h> |
25 | #include <glib/gstdio.h> | |
26 | #include "protocol.h" | |
4bd770f5 JH |
27 | |
28 | #define DS_CMD_GET_FW_VERSION 0xb0 | |
29 | #define DS_CMD_GET_REVID_VERSION 0xb1 | |
30 | #define DS_CMD_START 0xb2 | |
31 | #define DS_CMD_CONFIG 0xb3 | |
32 | #define DS_CMD_SETTING 0xb4 | |
33 | #define DS_CMD_CONTROL 0xb5 | |
34 | #define DS_CMD_STATUS 0xb6 | |
35 | #define DS_CMD_STATUS_INFO 0xb7 | |
36 | #define DS_CMD_WR_REG 0xb8 | |
37 | #define DS_CMD_WR_NVM 0xb9 | |
38 | #define DS_CMD_RD_NVM 0xba | |
39 | #define DS_CMD_RD_NVM_PRE 0xbb | |
40 | #define DS_CMD_GET_HW_INFO 0xbc | |
41 | ||
42 | #define DS_START_FLAGS_STOP (1 << 7) | |
43 | #define DS_START_FLAGS_CLK_48MHZ (1 << 6) | |
44 | #define DS_START_FLAGS_SAMPLE_WIDE (1 << 5) | |
45 | #define DS_START_FLAGS_MODE_LA (1 << 4) | |
46 | ||
47 | #define DS_ADDR_COMB 0x68 | |
48 | #define DS_ADDR_EEWP 0x70 | |
49 | #define DS_ADDR_VTH 0x78 | |
50 | ||
51 | #define DS_MAX_LOGIC_DEPTH SR_MHZ(16) | |
52 | #define DS_MAX_LOGIC_SAMPLERATE SR_MHZ(100) | |
53 | #define DS_MAX_TRIG_PERCENT 90 | |
54 | ||
55 | #define DS_MODE_TRIG_EN (1 << 0) | |
56 | #define DS_MODE_CLK_TYPE (1 << 1) | |
57 | #define DS_MODE_CLK_EDGE (1 << 2) | |
58 | #define DS_MODE_RLE_MODE (1 << 3) | |
59 | #define DS_MODE_DSO_MODE (1 << 4) | |
60 | #define DS_MODE_HALF_MODE (1 << 5) | |
61 | #define DS_MODE_QUAR_MODE (1 << 6) | |
62 | #define DS_MODE_ANALOG_MODE (1 << 7) | |
63 | #define DS_MODE_FILTER (1 << 8) | |
64 | #define DS_MODE_INSTANT (1 << 9) | |
65 | #define DS_MODE_STRIG_MODE (1 << 11) | |
66 | #define DS_MODE_STREAM_MODE (1 << 12) | |
67 | #define DS_MODE_LPB_TEST (1 << 13) | |
68 | #define DS_MODE_EXT_TEST (1 << 14) | |
69 | #define DS_MODE_INT_TEST (1 << 15) | |
70 | ||
03a0002e JH |
71 | #define DSLOGIC_ATOMIC_SAMPLES (sizeof(uint64_t) * 8) |
72 | #define DSLOGIC_ATOMIC_BYTES sizeof(uint64_t) | |
4bd770f5 JH |
73 | |
74 | /* | |
75 | * The FPGA is configured with TLV tuples. Length is specified as the | |
76 | * number of 16-bit words. | |
77 | */ | |
78 | #define _DS_CFG(variable, wordcnt) ((variable << 8) | wordcnt) | |
44b46d70 UH |
79 | #define DS_CFG_START 0xf5a5f5a5 |
80 | #define DS_CFG_MODE _DS_CFG(0, 1) | |
81 | #define DS_CFG_DIVIDER _DS_CFG(1, 2) | |
82 | #define DS_CFG_COUNT _DS_CFG(3, 2) | |
83 | #define DS_CFG_TRIG_POS _DS_CFG(5, 2) | |
84 | #define DS_CFG_TRIG_GLB _DS_CFG(7, 1) | |
85 | #define DS_CFG_CH_EN _DS_CFG(8, 1) | |
86 | #define DS_CFG_TRIG _DS_CFG(64, 160) | |
87 | #define DS_CFG_END 0xfa5afa5a | |
adcb9951 JH |
88 | |
89 | #pragma pack(push, 1) | |
90 | ||
91 | struct version_info { | |
92 | uint8_t major; | |
93 | uint8_t minor; | |
94 | }; | |
95 | ||
96 | struct cmd_start_acquisition { | |
97 | uint8_t flags; | |
98 | uint8_t sample_delay_h; | |
99 | uint8_t sample_delay_l; | |
100 | }; | |
101 | ||
4b25cbff | 102 | struct fpga_config { |
4bd770f5 JH |
103 | uint32_t sync; |
104 | ||
105 | uint16_t mode_header; | |
106 | uint16_t mode; | |
107 | uint16_t divider_header; | |
108 | uint32_t divider; | |
109 | uint16_t count_header; | |
110 | uint32_t count; | |
111 | uint16_t trig_pos_header; | |
112 | uint32_t trig_pos; | |
113 | uint16_t trig_glb_header; | |
114 | uint16_t trig_glb; | |
115 | uint16_t ch_en_header; | |
116 | uint16_t ch_en; | |
117 | ||
118 | uint16_t trig_header; | |
119 | uint16_t trig_mask0[NUM_TRIGGER_STAGES]; | |
120 | uint16_t trig_mask1[NUM_TRIGGER_STAGES]; | |
121 | uint16_t trig_value0[NUM_TRIGGER_STAGES]; | |
122 | uint16_t trig_value1[NUM_TRIGGER_STAGES]; | |
123 | uint16_t trig_edge0[NUM_TRIGGER_STAGES]; | |
124 | uint16_t trig_edge1[NUM_TRIGGER_STAGES]; | |
125 | uint16_t trig_logic0[NUM_TRIGGER_STAGES]; | |
126 | uint16_t trig_logic1[NUM_TRIGGER_STAGES]; | |
127 | uint32_t trig_count[NUM_TRIGGER_STAGES]; | |
128 | ||
129 | uint32_t end_sync; | |
130 | }; | |
131 | ||
adcb9951 JH |
132 | #pragma pack(pop) |
133 | ||
4bd770f5 JH |
134 | /* |
135 | * This should be larger than the FPGA bitstream image so that it'll get | |
136 | * uploaded in one big operation. There seem to be issues when uploading | |
137 | * it in chunks. | |
138 | */ | |
139 | #define FW_BUFSIZE (1024 * 1024) | |
140 | ||
141 | #define FPGA_UPLOAD_DELAY (10 * 1000) | |
142 | ||
143 | #define USB_TIMEOUT (3 * 1000) | |
adcb9951 JH |
144 | |
145 | static int command_get_fw_version(libusb_device_handle *devhdl, | |
146 | struct version_info *vi) | |
147 | { | |
148 | int ret; | |
149 | ||
150 | ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR | | |
151 | LIBUSB_ENDPOINT_IN, DS_CMD_GET_FW_VERSION, 0x0000, 0x0000, | |
152 | (unsigned char *)vi, sizeof(struct version_info), USB_TIMEOUT); | |
153 | ||
154 | if (ret < 0) { | |
155 | sr_err("Unable to get version info: %s.", | |
156 | libusb_error_name(ret)); | |
157 | return SR_ERR; | |
158 | } | |
159 | ||
160 | return SR_OK; | |
161 | } | |
162 | ||
163 | static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid) | |
164 | { | |
165 | struct sr_usb_dev_inst *usb = sdi->conn; | |
166 | libusb_device_handle *devhdl = usb->devhdl; | |
167 | int ret; | |
168 | ||
169 | ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR | | |
170 | LIBUSB_ENDPOINT_IN, DS_CMD_GET_REVID_VERSION, 0x0000, 0x0000, | |
171 | revid, 1, USB_TIMEOUT); | |
172 | ||
173 | if (ret < 0) { | |
174 | sr_err("Unable to get REVID: %s.", libusb_error_name(ret)); | |
175 | return SR_ERR; | |
176 | } | |
177 | ||
178 | return SR_OK; | |
179 | } | |
180 | ||
4bd770f5 JH |
181 | static int command_start_acquisition(const struct sr_dev_inst *sdi) |
182 | { | |
183 | struct sr_usb_dev_inst *usb; | |
184 | struct dslogic_mode mode; | |
185 | int ret; | |
186 | ||
187 | mode.flags = DS_START_FLAGS_MODE_LA | DS_START_FLAGS_SAMPLE_WIDE; | |
188 | mode.sample_delay_h = mode.sample_delay_l = 0; | |
189 | ||
190 | usb = sdi->conn; | |
191 | ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR | | |
192 | LIBUSB_ENDPOINT_OUT, DS_CMD_START, 0x0000, 0x0000, | |
193 | (unsigned char *)&mode, sizeof(mode), USB_TIMEOUT); | |
194 | if (ret < 0) { | |
195 | sr_err("Failed to send start command: %s.", libusb_error_name(ret)); | |
196 | return SR_ERR; | |
197 | } | |
198 | ||
199 | return SR_OK; | |
200 | } | |
201 | ||
202 | static int command_stop_acquisition(const struct sr_dev_inst *sdi) | |
203 | { | |
204 | struct sr_usb_dev_inst *usb; | |
205 | struct dslogic_mode mode; | |
206 | int ret; | |
207 | ||
208 | mode.flags = DS_START_FLAGS_STOP; | |
209 | mode.sample_delay_h = mode.sample_delay_l = 0; | |
210 | ||
211 | usb = sdi->conn; | |
212 | ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR | | |
213 | LIBUSB_ENDPOINT_OUT, DS_CMD_START, 0x0000, 0x0000, | |
214 | (unsigned char *)&mode, sizeof(struct dslogic_mode), USB_TIMEOUT); | |
215 | if (ret < 0) { | |
216 | sr_err("Failed to send stop command: %s.", libusb_error_name(ret)); | |
217 | return SR_ERR; | |
218 | } | |
219 | ||
220 | return SR_OK; | |
221 | } | |
222 | ||
223 | SR_PRIV int dslogic_fpga_firmware_upload(const struct sr_dev_inst *sdi) | |
224 | { | |
225 | const char *name = NULL; | |
226 | uint64_t sum; | |
227 | struct sr_resource bitstream; | |
228 | struct drv_context *drvc; | |
229 | struct dev_context *devc; | |
230 | struct sr_usb_dev_inst *usb; | |
231 | unsigned char *buf; | |
232 | ssize_t chunksize; | |
233 | int transferred; | |
234 | int result, ret; | |
235 | const uint8_t cmd[3] = {0, 0, 0}; | |
236 | ||
237 | drvc = sdi->driver->context; | |
238 | devc = sdi->priv; | |
239 | usb = sdi->conn; | |
240 | ||
241 | if (!strcmp(devc->profile->model, "DSLogic")) { | |
242 | if (devc->cur_threshold < 1.40) | |
243 | name = DSLOGIC_FPGA_FIRMWARE_3V3; | |
244 | else | |
245 | name = DSLOGIC_FPGA_FIRMWARE_5V; | |
246 | } else if (!strcmp(devc->profile->model, "DSLogic Pro")){ | |
247 | name = DSLOGIC_PRO_FPGA_FIRMWARE; | |
248 | } else if (!strcmp(devc->profile->model, "DSLogic Plus")){ | |
249 | name = DSLOGIC_PLUS_FPGA_FIRMWARE; | |
250 | } else if (!strcmp(devc->profile->model, "DSLogic Basic")){ | |
251 | name = DSLOGIC_BASIC_FPGA_FIRMWARE; | |
252 | } else if (!strcmp(devc->profile->model, "DSCope")) { | |
253 | name = DSCOPE_FPGA_FIRMWARE; | |
254 | } else { | |
255 | sr_err("Failed to select FPGA firmware."); | |
256 | return SR_ERR; | |
257 | } | |
258 | ||
259 | sr_dbg("Uploading FPGA firmware '%s'.", name); | |
260 | ||
261 | result = sr_resource_open(drvc->sr_ctx, &bitstream, | |
262 | SR_RESOURCE_FIRMWARE, name); | |
263 | if (result != SR_OK) | |
264 | return result; | |
265 | ||
266 | /* Tell the device firmware is coming. */ | |
267 | if ((ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR | | |
268 | LIBUSB_ENDPOINT_OUT, DS_CMD_CONFIG, 0x0000, 0x0000, | |
269 | (unsigned char *)&cmd, sizeof(cmd), USB_TIMEOUT)) < 0) { | |
270 | sr_err("Failed to upload FPGA firmware: %s.", libusb_error_name(ret)); | |
271 | sr_resource_close(drvc->sr_ctx, &bitstream); | |
272 | return SR_ERR; | |
273 | } | |
274 | ||
275 | /* Give the FX2 time to get ready for FPGA firmware upload. */ | |
276 | g_usleep(FPGA_UPLOAD_DELAY); | |
277 | ||
278 | buf = g_malloc(FW_BUFSIZE); | |
279 | sum = 0; | |
280 | result = SR_OK; | |
281 | while (1) { | |
282 | chunksize = sr_resource_read(drvc->sr_ctx, &bitstream, | |
283 | buf, FW_BUFSIZE); | |
284 | if (chunksize < 0) | |
285 | result = SR_ERR; | |
286 | if (chunksize <= 0) | |
287 | break; | |
288 | ||
289 | if ((ret = libusb_bulk_transfer(usb->devhdl, 2 | LIBUSB_ENDPOINT_OUT, | |
290 | buf, chunksize, &transferred, USB_TIMEOUT)) < 0) { | |
291 | sr_err("Unable to configure FPGA firmware: %s.", | |
292 | libusb_error_name(ret)); | |
293 | result = SR_ERR; | |
294 | break; | |
295 | } | |
296 | sum += transferred; | |
297 | sr_spew("Uploaded %" PRIu64 "/%" PRIu64 " bytes.", | |
298 | sum, bitstream.size); | |
299 | ||
300 | if (transferred != chunksize) { | |
301 | sr_err("Short transfer while uploading FPGA firmware."); | |
302 | result = SR_ERR; | |
303 | break; | |
304 | } | |
305 | } | |
306 | g_free(buf); | |
307 | sr_resource_close(drvc->sr_ctx, &bitstream); | |
308 | ||
309 | if (result == SR_OK) | |
310 | sr_dbg("FPGA firmware upload done."); | |
311 | ||
312 | return result; | |
313 | } | |
314 | ||
6dfa2c39 JH |
315 | static unsigned int enabled_channel_count(const struct sr_dev_inst *sdi) |
316 | { | |
317 | unsigned int count = 0; | |
318 | for (const GSList *l = sdi->channels; l; l = l->next) { | |
319 | const struct sr_channel *const probe = (struct sr_channel *)l->data; | |
320 | if (probe->enabled) | |
321 | count++; | |
322 | } | |
323 | return count; | |
324 | } | |
325 | ||
326 | static uint16_t enabled_channel_mask(const struct sr_dev_inst *sdi) | |
327 | { | |
328 | unsigned int mask = 0; | |
329 | for (const GSList *l = sdi->channels; l; l = l->next) { | |
330 | const struct sr_channel *const probe = (struct sr_channel *)l->data; | |
331 | if (probe->enabled) | |
332 | mask |= 1 << probe->index; | |
333 | } | |
334 | return mask; | |
335 | } | |
336 | ||
4bd770f5 JH |
337 | /* |
338 | * Get the session trigger and configure the FPGA structure | |
339 | * accordingly. | |
f05600f4 | 340 | * @return @c true if any triggers are enabled, @c false otherwise. |
4bd770f5 | 341 | */ |
f05600f4 | 342 | static bool set_trigger(const struct sr_dev_inst *sdi, struct fpga_config *cfg) |
4bd770f5 JH |
343 | { |
344 | struct sr_trigger *trigger; | |
345 | struct sr_trigger_stage *stage; | |
346 | struct sr_trigger_match *match; | |
347 | struct dev_context *devc; | |
348 | const GSList *l, *m; | |
6dfa2c39 JH |
349 | const unsigned int num_enabled_channels = enabled_channel_count(sdi); |
350 | int num_trigger_stages = 0; | |
351 | ||
4bd770f5 JH |
352 | int channelbit, i = 0; |
353 | uint32_t trigger_point; | |
354 | ||
355 | devc = sdi->priv; | |
356 | ||
6dfa2c39 | 357 | cfg->ch_en = enabled_channel_mask(sdi); |
4bd770f5 | 358 | |
b23ecd6c | 359 | for (i = 0; i < NUM_TRIGGER_STAGES; i++) { |
4bd770f5 JH |
360 | cfg->trig_mask0[i] = 0xffff; |
361 | cfg->trig_mask1[i] = 0xffff; | |
362 | cfg->trig_value0[i] = 0; | |
363 | cfg->trig_value1[i] = 0; | |
364 | cfg->trig_edge0[i] = 0; | |
365 | cfg->trig_edge1[i] = 0; | |
366 | cfg->trig_logic0[i] = 2; | |
367 | cfg->trig_logic1[i] = 2; | |
368 | cfg->trig_count[i] = 0; | |
369 | } | |
370 | ||
371 | trigger_point = (devc->capture_ratio * devc->limit_samples) / 100; | |
372 | if (trigger_point < DSLOGIC_ATOMIC_SAMPLES) | |
373 | trigger_point = DSLOGIC_ATOMIC_SAMPLES; | |
374 | const uint32_t mem_depth = devc->profile->mem_depth; | |
375 | const uint32_t max_trigger_point = devc->continuous_mode ? ((mem_depth * 10) / 100) : | |
376 | ((mem_depth * DS_MAX_TRIG_PERCENT) / 100); | |
377 | if (trigger_point > max_trigger_point) | |
378 | trigger_point = max_trigger_point; | |
379 | cfg->trig_pos = trigger_point & ~(DSLOGIC_ATOMIC_SAMPLES - 1); | |
380 | ||
381 | if (!(trigger = sr_session_trigger_get(sdi->session))) { | |
382 | sr_dbg("No session trigger found"); | |
f05600f4 | 383 | return false; |
4bd770f5 JH |
384 | } |
385 | ||
386 | for (l = trigger->stages; l; l = l->next) { | |
387 | stage = l->data; | |
388 | num_trigger_stages++; | |
389 | for (m = stage->matches; m; m = m->next) { | |
390 | match = m->data; | |
391 | if (!match->channel->enabled) | |
392 | /* Ignore disabled channels with a trigger. */ | |
393 | continue; | |
394 | channelbit = 1 << (match->channel->index); | |
395 | /* Simple trigger support (event). */ | |
396 | if (match->match == SR_TRIGGER_ONE) { | |
397 | cfg->trig_mask0[0] &= ~channelbit; | |
398 | cfg->trig_mask1[0] &= ~channelbit; | |
399 | cfg->trig_value0[0] |= channelbit; | |
400 | cfg->trig_value1[0] |= channelbit; | |
401 | } else if (match->match == SR_TRIGGER_ZERO) { | |
402 | cfg->trig_mask0[0] &= ~channelbit; | |
403 | cfg->trig_mask1[0] &= ~channelbit; | |
404 | } else if (match->match == SR_TRIGGER_FALLING) { | |
405 | cfg->trig_mask0[0] &= ~channelbit; | |
406 | cfg->trig_mask1[0] &= ~channelbit; | |
407 | cfg->trig_edge0[0] |= channelbit; | |
408 | cfg->trig_edge1[0] |= channelbit; | |
409 | } else if (match->match == SR_TRIGGER_RISING) { | |
410 | cfg->trig_mask0[0] &= ~channelbit; | |
411 | cfg->trig_mask1[0] &= ~channelbit; | |
412 | cfg->trig_value0[0] |= channelbit; | |
413 | cfg->trig_value1[0] |= channelbit; | |
414 | cfg->trig_edge0[0] |= channelbit; | |
415 | cfg->trig_edge1[0] |= channelbit; | |
416 | } else if (match->match == SR_TRIGGER_EDGE) { | |
417 | cfg->trig_edge0[0] |= channelbit; | |
418 | cfg->trig_edge1[0] |= channelbit; | |
419 | } | |
420 | } | |
421 | } | |
422 | ||
b7a3d79e | 423 | cfg->trig_glb = (num_enabled_channels << 4) | (num_trigger_stages - 1); |
f05600f4 JH |
424 | |
425 | return num_trigger_stages != 0; | |
4bd770f5 JH |
426 | } |
427 | ||
428 | static int fpga_configure(const struct sr_dev_inst *sdi) | |
429 | { | |
16d4c982 JH |
430 | const struct dev_context *const devc = sdi->priv; |
431 | const struct sr_usb_dev_inst *const usb = sdi->conn; | |
4bd770f5 | 432 | uint8_t c[3]; |
4b25cbff | 433 | struct fpga_config cfg; |
9f580230 JH |
434 | uint16_t mode = 0; |
435 | uint32_t divider; | |
4bd770f5 JH |
436 | int transferred, len, ret; |
437 | ||
438 | sr_dbg("Configuring FPGA."); | |
439 | ||
4bd770f5 JH |
440 | WL32(&cfg.sync, DS_CFG_START); |
441 | WL16(&cfg.mode_header, DS_CFG_MODE); | |
442 | WL16(&cfg.divider_header, DS_CFG_DIVIDER); | |
443 | WL16(&cfg.count_header, DS_CFG_COUNT); | |
444 | WL16(&cfg.trig_pos_header, DS_CFG_TRIG_POS); | |
445 | WL16(&cfg.trig_glb_header, DS_CFG_TRIG_GLB); | |
446 | WL16(&cfg.ch_en_header, DS_CFG_CH_EN); | |
447 | WL16(&cfg.trig_header, DS_CFG_TRIG); | |
448 | WL32(&cfg.end_sync, DS_CFG_END); | |
449 | ||
450 | /* Pass in the length of a fixed-size struct. Really. */ | |
4b25cbff | 451 | len = sizeof(struct fpga_config) / 2; |
4bd770f5 JH |
452 | c[0] = len & 0xff; |
453 | c[1] = (len >> 8) & 0xff; | |
454 | c[2] = (len >> 16) & 0xff; | |
455 | ||
456 | ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR | | |
457 | LIBUSB_ENDPOINT_OUT, DS_CMD_SETTING, 0x0000, 0x0000, | |
458 | c, sizeof(c), USB_TIMEOUT); | |
459 | if (ret < 0) { | |
460 | sr_err("Failed to send FPGA configure command: %s.", | |
461 | libusb_error_name(ret)); | |
462 | return SR_ERR; | |
463 | } | |
464 | ||
f05600f4 JH |
465 | if (set_trigger(sdi, &cfg)) |
466 | mode |= DS_MODE_TRIG_EN; | |
467 | ||
4bd770f5 | 468 | if (devc->mode == DS_OP_INTERNAL_TEST) |
f05600f4 | 469 | mode |= DS_MODE_INT_TEST; |
4bd770f5 | 470 | else if (devc->mode == DS_OP_EXTERNAL_TEST) |
f05600f4 | 471 | mode |= DS_MODE_EXT_TEST; |
4bd770f5 | 472 | else if (devc->mode == DS_OP_LOOPBACK_TEST) |
f05600f4 | 473 | mode |= DS_MODE_LPB_TEST; |
4bd770f5 JH |
474 | |
475 | if (devc->cur_samplerate == DS_MAX_LOGIC_SAMPLERATE * 2) | |
9f580230 | 476 | mode |= DS_MODE_HALF_MODE; |
4bd770f5 | 477 | else if (devc->cur_samplerate == DS_MAX_LOGIC_SAMPLERATE * 4) |
9f580230 | 478 | mode |= DS_MODE_QUAR_MODE; |
4bd770f5 JH |
479 | |
480 | if (devc->continuous_mode) | |
9f580230 | 481 | mode |= DS_MODE_STREAM_MODE; |
4bd770f5 | 482 | if (devc->external_clock) { |
9f580230 | 483 | mode |= DS_MODE_CLK_TYPE; |
4bd770f5 | 484 | if (devc->clock_edge == DS_EDGE_FALLING) |
9f580230 | 485 | mode |= DS_MODE_CLK_EDGE; |
4bd770f5 JH |
486 | } |
487 | if (devc->limit_samples > DS_MAX_LOGIC_DEPTH * | |
488 | ceil(devc->cur_samplerate * 1.0 / DS_MAX_LOGIC_SAMPLERATE) | |
489 | && !devc->continuous_mode) { | |
490 | /* Enable RLE for long captures. | |
491 | * Without this, captured data present errors. | |
492 | */ | |
9f580230 | 493 | mode |= DS_MODE_RLE_MODE; |
4bd770f5 JH |
494 | } |
495 | ||
9f580230 JH |
496 | WL16(&cfg.mode, mode); |
497 | divider = ceil(DS_MAX_LOGIC_SAMPLERATE * 1.0 / devc->cur_samplerate); | |
498 | WL32(&cfg.divider, divider); | |
4bd770f5 JH |
499 | |
500 | /* Number of 16-sample units. */ | |
501 | WL32(&cfg.count, devc->limit_samples / 16); | |
502 | ||
4b25cbff | 503 | len = sizeof(struct fpga_config); |
4bd770f5 JH |
504 | ret = libusb_bulk_transfer(usb->devhdl, 2 | LIBUSB_ENDPOINT_OUT, |
505 | (unsigned char *)&cfg, len, &transferred, USB_TIMEOUT); | |
506 | if (ret < 0 || transferred != len) { | |
507 | sr_err("Failed to send FPGA configuration: %s.", libusb_error_name(ret)); | |
508 | return SR_ERR; | |
509 | } | |
510 | ||
511 | return SR_OK; | |
512 | } | |
513 | ||
1ee70746 JH |
514 | SR_PRIV int dslogic_set_voltage_threshold(const struct sr_dev_inst *sdi, double threshold) |
515 | { | |
516 | int ret; | |
517 | struct dev_context *const devc = sdi->priv; | |
518 | const struct sr_usb_dev_inst *const usb = sdi->conn; | |
519 | const uint8_t value = (threshold / 5.0) * 255; | |
520 | const uint16_t cmd = value | (DS_ADDR_VTH << 8); | |
521 | ||
522 | /* Send the control command. */ | |
523 | ret = libusb_control_transfer(usb->devhdl, | |
524 | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT, | |
525 | DS_CMD_WR_REG, 0x0000, 0x0000, | |
526 | (unsigned char *)&cmd, sizeof(cmd), 3000); | |
527 | if (ret < 0) { | |
528 | sr_err("Unable to set voltage-threshold register: %s.", | |
529 | libusb_error_name(ret)); | |
530 | return SR_ERR; | |
531 | } | |
532 | ||
533 | devc->cur_threshold = threshold; | |
534 | ||
535 | return SR_OK; | |
536 | } | |
537 | ||
adcb9951 JH |
538 | SR_PRIV int dslogic_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di) |
539 | { | |
540 | libusb_device **devlist; | |
541 | struct sr_usb_dev_inst *usb; | |
542 | struct libusb_device_descriptor des; | |
543 | struct dev_context *devc; | |
544 | struct drv_context *drvc; | |
545 | struct version_info vi; | |
7e463623 | 546 | int ret = SR_ERR, i, device_count; |
adcb9951 JH |
547 | uint8_t revid; |
548 | char connection_id[64]; | |
549 | ||
550 | drvc = di->context; | |
551 | devc = sdi->priv; | |
552 | usb = sdi->conn; | |
553 | ||
adcb9951 JH |
554 | device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
555 | if (device_count < 0) { | |
556 | sr_err("Failed to get device list: %s.", | |
557 | libusb_error_name(device_count)); | |
558 | return SR_ERR; | |
559 | } | |
560 | ||
561 | for (i = 0; i < device_count; i++) { | |
562 | libusb_get_device_descriptor(devlist[i], &des); | |
563 | ||
564 | if (des.idVendor != devc->profile->vid | |
565 | || des.idProduct != devc->profile->pid) | |
566 | continue; | |
567 | ||
568 | if ((sdi->status == SR_ST_INITIALIZING) || | |
569 | (sdi->status == SR_ST_INACTIVE)) { | |
7e463623 | 570 | /* Check device by its physical USB bus/port address. */ |
6c1a76d1 RT |
571 | if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0) |
572 | continue; | |
573 | ||
adcb9951 JH |
574 | if (strcmp(sdi->connection_id, connection_id)) |
575 | /* This is not the one. */ | |
576 | continue; | |
577 | } | |
578 | ||
579 | if (!(ret = libusb_open(devlist[i], &usb->devhdl))) { | |
580 | if (usb->address == 0xff) | |
581 | /* | |
582 | * First time we touch this device after FW | |
583 | * upload, so we don't know the address yet. | |
584 | */ | |
585 | usb->address = libusb_get_device_address(devlist[i]); | |
586 | } else { | |
587 | sr_err("Failed to open device: %s.", | |
588 | libusb_error_name(ret)); | |
7e463623 | 589 | ret = SR_ERR; |
adcb9951 JH |
590 | break; |
591 | } | |
592 | ||
593 | if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) { | |
594 | if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) { | |
595 | if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) { | |
596 | sr_err("Failed to detach kernel driver: %s.", | |
597 | libusb_error_name(ret)); | |
7e463623 UH |
598 | ret = SR_ERR; |
599 | break; | |
adcb9951 JH |
600 | } |
601 | } | |
602 | } | |
603 | ||
604 | ret = command_get_fw_version(usb->devhdl, &vi); | |
605 | if (ret != SR_OK) { | |
606 | sr_err("Failed to get firmware version."); | |
607 | break; | |
608 | } | |
609 | ||
610 | ret = command_get_revid_version(sdi, &revid); | |
611 | if (ret != SR_OK) { | |
612 | sr_err("Failed to get REVID."); | |
613 | break; | |
614 | } | |
615 | ||
616 | /* | |
617 | * Changes in major version mean incompatible/API changes, so | |
618 | * bail out if we encounter an incompatible version. | |
619 | * Different minor versions are OK, they should be compatible. | |
620 | */ | |
621 | if (vi.major != DSLOGIC_REQUIRED_VERSION_MAJOR) { | |
622 | sr_err("Expected firmware version %d.x, " | |
623 | "got %d.%d.", DSLOGIC_REQUIRED_VERSION_MAJOR, | |
624 | vi.major, vi.minor); | |
7e463623 | 625 | ret = SR_ERR; |
adcb9951 JH |
626 | break; |
627 | } | |
628 | ||
adcb9951 JH |
629 | sr_info("Opened device on %d.%d (logical) / %s (physical), " |
630 | "interface %d, firmware %d.%d.", | |
631 | usb->bus, usb->address, connection_id, | |
632 | USB_INTERFACE, vi.major, vi.minor); | |
633 | ||
634 | sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.", | |
635 | revid, (revid != 1) ? " (FX2)" : "A (FX2LP)"); | |
636 | ||
7e463623 UH |
637 | ret = SR_OK; |
638 | ||
adcb9951 JH |
639 | break; |
640 | } | |
adcb9951 | 641 | |
7e463623 | 642 | libusb_free_device_list(devlist, 1); |
adcb9951 | 643 | |
7e463623 | 644 | return ret; |
adcb9951 JH |
645 | } |
646 | ||
647 | SR_PRIV struct dev_context *dslogic_dev_new(void) | |
648 | { | |
649 | struct dev_context *devc; | |
650 | ||
651 | devc = g_malloc0(sizeof(struct dev_context)); | |
652 | devc->profile = NULL; | |
653 | devc->fw_updated = 0; | |
654 | devc->cur_samplerate = 0; | |
655 | devc->limit_samples = 0; | |
656 | devc->capture_ratio = 0; | |
657 | devc->continuous_mode = FALSE; | |
658 | devc->clock_edge = DS_EDGE_RISING; | |
659 | ||
660 | return devc; | |
661 | } | |
662 | ||
4bd770f5 | 663 | static void abort_acquisition(struct dev_context *devc) |
adcb9951 JH |
664 | { |
665 | int i; | |
666 | ||
667 | devc->acq_aborted = TRUE; | |
668 | ||
669 | for (i = devc->num_transfers - 1; i >= 0; i--) { | |
670 | if (devc->transfers[i]) | |
671 | libusb_cancel_transfer(devc->transfers[i]); | |
672 | } | |
673 | } | |
674 | ||
675 | static void finish_acquisition(struct sr_dev_inst *sdi) | |
676 | { | |
677 | struct dev_context *devc; | |
678 | ||
679 | devc = sdi->priv; | |
680 | ||
681 | std_session_send_df_end(sdi); | |
682 | ||
683 | usb_source_remove(sdi->session, devc->ctx); | |
684 | ||
685 | devc->num_transfers = 0; | |
686 | g_free(devc->transfers); | |
f74485b6 | 687 | g_free(devc->deinterleave_buffer); |
adcb9951 JH |
688 | } |
689 | ||
690 | static void free_transfer(struct libusb_transfer *transfer) | |
691 | { | |
692 | struct sr_dev_inst *sdi; | |
693 | struct dev_context *devc; | |
694 | unsigned int i; | |
695 | ||
696 | sdi = transfer->user_data; | |
697 | devc = sdi->priv; | |
698 | ||
699 | g_free(transfer->buffer); | |
700 | transfer->buffer = NULL; | |
701 | libusb_free_transfer(transfer); | |
702 | ||
703 | for (i = 0; i < devc->num_transfers; i++) { | |
704 | if (devc->transfers[i] == transfer) { | |
705 | devc->transfers[i] = NULL; | |
706 | break; | |
707 | } | |
708 | } | |
709 | ||
710 | devc->submitted_transfers--; | |
711 | if (devc->submitted_transfers == 0) | |
712 | finish_acquisition(sdi); | |
713 | } | |
714 | ||
715 | static void resubmit_transfer(struct libusb_transfer *transfer) | |
716 | { | |
717 | int ret; | |
718 | ||
719 | if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS) | |
720 | return; | |
721 | ||
722 | sr_err("%s: %s", __func__, libusb_error_name(ret)); | |
723 | free_transfer(transfer); | |
724 | ||
725 | } | |
726 | ||
f74485b6 JH |
727 | static void deinterleave_buffer(const uint8_t *src, size_t length, |
728 | uint16_t *dst_ptr, size_t channel_count, uint16_t channel_mask) | |
729 | { | |
730 | uint16_t sample; | |
731 | ||
732 | for (const uint64_t *src_ptr = (uint64_t*)src; | |
733 | src_ptr < (uint64_t*)(src + length); | |
734 | src_ptr += channel_count) { | |
735 | for (int bit = 0; bit != 64; bit++) { | |
736 | const uint64_t *word_ptr = src_ptr; | |
737 | sample = 0; | |
55584d38 | 738 | for (unsigned int channel = 0; channel != 16; |
f74485b6 | 739 | channel++) { |
55584d38 JH |
740 | const uint16_t m = channel_mask >> channel; |
741 | if (!m) | |
742 | break; | |
d9b716fc | 743 | if ((m & 1) && ((*word_ptr++ >> bit) & UINT64_C(1))) |
f74485b6 JH |
744 | sample |= 1 << channel; |
745 | } | |
746 | *dst_ptr++ = sample; | |
747 | } | |
748 | } | |
749 | } | |
750 | ||
4bd770f5 | 751 | static void send_data(struct sr_dev_inst *sdi, |
f74485b6 | 752 | uint16_t *data, size_t sample_count) |
adcb9951 JH |
753 | { |
754 | const struct sr_datafeed_logic logic = { | |
f74485b6 JH |
755 | .length = sample_count * sizeof(uint16_t), |
756 | .unitsize = sizeof(uint16_t), | |
adcb9951 JH |
757 | .data = data |
758 | }; | |
759 | ||
760 | const struct sr_datafeed_packet packet = { | |
761 | .type = SR_DF_LOGIC, | |
762 | .payload = &logic | |
763 | }; | |
764 | ||
765 | sr_session_send(sdi, &packet); | |
766 | } | |
767 | ||
4bd770f5 | 768 | static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer) |
adcb9951 | 769 | { |
f74485b6 JH |
770 | struct sr_dev_inst *const sdi = transfer->user_data; |
771 | struct dev_context *const devc = sdi->priv; | |
772 | const size_t channel_count = enabled_channel_count(sdi); | |
773 | const uint16_t channel_mask = enabled_channel_mask(sdi); | |
774 | const unsigned int cur_sample_count = DSLOGIC_ATOMIC_SAMPLES * | |
775 | transfer->actual_length / | |
776 | (DSLOGIC_ATOMIC_BYTES * channel_count); | |
777 | ||
adcb9951 | 778 | gboolean packet_has_error = FALSE; |
adcb9951 | 779 | unsigned int num_samples; |
f74485b6 | 780 | int trigger_offset; |
adcb9951 JH |
781 | |
782 | /* | |
783 | * If acquisition has already ended, just free any queued up | |
784 | * transfer that come in. | |
785 | */ | |
786 | if (devc->acq_aborted) { | |
787 | free_transfer(transfer); | |
788 | return; | |
789 | } | |
790 | ||
791 | sr_dbg("receive_transfer(): status %s received %d bytes.", | |
792 | libusb_error_name(transfer->status), transfer->actual_length); | |
793 | ||
794 | /* Save incoming transfer before reusing the transfer struct. */ | |
adcb9951 JH |
795 | |
796 | switch (transfer->status) { | |
797 | case LIBUSB_TRANSFER_NO_DEVICE: | |
4bd770f5 | 798 | abort_acquisition(devc); |
adcb9951 JH |
799 | free_transfer(transfer); |
800 | return; | |
801 | case LIBUSB_TRANSFER_COMPLETED: | |
802 | case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */ | |
803 | break; | |
804 | default: | |
805 | packet_has_error = TRUE; | |
806 | break; | |
807 | } | |
808 | ||
809 | if (transfer->actual_length == 0 || packet_has_error) { | |
810 | devc->empty_transfer_count++; | |
811 | if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) { | |
812 | /* | |
813 | * The FX2 gave up. End the acquisition, the frontend | |
814 | * will work out that the samplecount is short. | |
815 | */ | |
4bd770f5 | 816 | abort_acquisition(devc); |
adcb9951 JH |
817 | free_transfer(transfer); |
818 | } else { | |
819 | resubmit_transfer(transfer); | |
820 | } | |
821 | return; | |
822 | } else { | |
823 | devc->empty_transfer_count = 0; | |
824 | } | |
5e7e327a JH |
825 | |
826 | if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) { | |
5e7e327a JH |
827 | if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples) |
828 | num_samples = devc->limit_samples - devc->sent_samples; | |
829 | else | |
830 | num_samples = cur_sample_count; | |
831 | ||
f74485b6 JH |
832 | /** |
833 | * The DSLogic emits sample data as sequences of 64-bit sample words | |
834 | * in a round-robin i.e. 64-bits from channel 0, 64-bits from channel 1 | |
835 | * etc. for each of the enabled channels, then looping back to the | |
836 | * channel. | |
837 | * | |
838 | * Because sigrok's internal representation is bit-interleaved channels | |
839 | * we must recast the data. | |
840 | * | |
841 | * Hopefully in future it will be possible to pass the data on as-is. | |
842 | */ | |
ecadb118 UH |
843 | if (transfer->actual_length % (DSLOGIC_ATOMIC_BYTES * channel_count) != 0) |
844 | sr_err("Invalid transfer length!"); | |
f74485b6 JH |
845 | deinterleave_buffer(transfer->buffer, transfer->actual_length, |
846 | devc->deinterleave_buffer, channel_count, channel_mask); | |
847 | ||
848 | /* Send the incoming transfer to the session bus. */ | |
5e7e327a JH |
849 | if (devc->trigger_pos > devc->sent_samples |
850 | && devc->trigger_pos <= devc->sent_samples + num_samples) { | |
851 | /* DSLogic trigger in this block. Send trigger position. */ | |
852 | trigger_offset = devc->trigger_pos - devc->sent_samples; | |
853 | /* Pre-trigger samples. */ | |
f74485b6 | 854 | send_data(sdi, devc->deinterleave_buffer, trigger_offset); |
5e7e327a JH |
855 | devc->sent_samples += trigger_offset; |
856 | /* Trigger position. */ | |
857 | devc->trigger_pos = 0; | |
0fa71943 | 858 | std_session_send_df_trigger(sdi); |
5e7e327a JH |
859 | /* Post trigger samples. */ |
860 | num_samples -= trigger_offset; | |
f74485b6 JH |
861 | send_data(sdi, devc->deinterleave_buffer |
862 | + trigger_offset, num_samples); | |
5e7e327a JH |
863 | devc->sent_samples += num_samples; |
864 | } else { | |
f74485b6 | 865 | send_data(sdi, devc->deinterleave_buffer, num_samples); |
5e7e327a | 866 | devc->sent_samples += num_samples; |
adcb9951 JH |
867 | } |
868 | } | |
869 | ||
870 | if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) { | |
4bd770f5 | 871 | abort_acquisition(devc); |
adcb9951 JH |
872 | free_transfer(transfer); |
873 | } else | |
874 | resubmit_transfer(transfer); | |
875 | } | |
876 | ||
4bd770f5 | 877 | static int receive_data(int fd, int revents, void *cb_data) |
adcb9951 | 878 | { |
4bd770f5 JH |
879 | struct timeval tv; |
880 | struct drv_context *drvc; | |
881 | ||
882 | (void)fd; | |
883 | (void)revents; | |
884 | ||
885 | drvc = (struct drv_context *)cb_data; | |
886 | ||
887 | tv.tv_sec = tv.tv_usec = 0; | |
888 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); | |
889 | ||
890 | return TRUE; | |
adcb9951 JH |
891 | } |
892 | ||
03a0002e | 893 | static size_t to_bytes_per_ms(const struct sr_dev_inst *sdi) |
adcb9951 | 894 | { |
03a0002e JH |
895 | const struct dev_context *const devc = sdi->priv; |
896 | const size_t ch_count = enabled_channel_count(sdi); | |
897 | ||
898 | if (devc->continuous_mode) | |
899 | return (devc->cur_samplerate * ch_count) / (1000 * 8); | |
900 | ||
901 | ||
902 | /* If we're in buffered mode, the transfer rate is not so important, | |
903 | * but we expect to get at least 10% of the high-speed USB bandwidth. | |
904 | */ | |
905 | return 35000000 / (1000 * 10); | |
4bd770f5 | 906 | } |
adcb9951 | 907 | |
03a0002e | 908 | static size_t get_buffer_size(const struct sr_dev_inst *sdi) |
4bd770f5 | 909 | { |
adcb9951 JH |
910 | /* |
911 | * The buffer should be large enough to hold 10ms of data and | |
03a0002e | 912 | * a multiple of the size of a data atom. |
adcb9951 | 913 | */ |
03a0002e JH |
914 | const size_t block_size = enabled_channel_count(sdi) * 512; |
915 | const size_t s = 10 * to_bytes_per_ms(sdi); | |
051e4beb GS |
916 | if (!block_size) |
917 | return s; | |
03a0002e | 918 | return ((s + block_size - 1) / block_size) * block_size; |
adcb9951 JH |
919 | } |
920 | ||
03a0002e | 921 | static unsigned int get_number_of_transfers(const struct sr_dev_inst *sdi) |
adcb9951 | 922 | { |
4bd770f5 | 923 | /* Total buffer size should be able to hold about 100ms of data. */ |
03a0002e JH |
924 | const unsigned int s = get_buffer_size(sdi); |
925 | const unsigned int n = (100 * to_bytes_per_ms(sdi) + s - 1) / s; | |
926 | return (n > NUM_SIMUL_TRANSFERS) ? NUM_SIMUL_TRANSFERS : n; | |
4bd770f5 | 927 | } |
adcb9951 | 928 | |
03a0002e | 929 | static unsigned int get_timeout(const struct sr_dev_inst *sdi) |
4bd770f5 | 930 | { |
03a0002e JH |
931 | const size_t total_size = get_buffer_size(sdi) * |
932 | get_number_of_transfers(sdi); | |
933 | const unsigned int timeout = total_size / to_bytes_per_ms(sdi); | |
adcb9951 JH |
934 | return timeout + timeout / 4; /* Leave a headroom of 25% percent. */ |
935 | } | |
4bd770f5 JH |
936 | |
937 | static int start_transfers(const struct sr_dev_inst *sdi) | |
938 | { | |
f74485b6 | 939 | const size_t channel_count = enabled_channel_count(sdi); |
03a0002e JH |
940 | const size_t size = get_buffer_size(sdi); |
941 | const unsigned int num_transfers = get_number_of_transfers(sdi); | |
942 | const unsigned int timeout = get_timeout(sdi); | |
943 | ||
4bd770f5 JH |
944 | struct dev_context *devc; |
945 | struct sr_usb_dev_inst *usb; | |
946 | struct libusb_transfer *transfer; | |
03a0002e JH |
947 | unsigned int i; |
948 | int ret; | |
4bd770f5 | 949 | unsigned char *buf; |
4bd770f5 JH |
950 | |
951 | devc = sdi->priv; | |
952 | usb = sdi->conn; | |
953 | ||
954 | devc->sent_samples = 0; | |
955 | devc->acq_aborted = FALSE; | |
956 | devc->empty_transfer_count = 0; | |
4bd770f5 JH |
957 | devc->submitted_transfers = 0; |
958 | ||
5e23d42f | 959 | g_free(devc->transfers); |
4bd770f5 JH |
960 | devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers); |
961 | if (!devc->transfers) { | |
962 | sr_err("USB transfers malloc failed."); | |
963 | return SR_ERR_MALLOC; | |
964 | } | |
965 | ||
f74485b6 JH |
966 | devc->deinterleave_buffer = g_try_malloc(DSLOGIC_ATOMIC_SAMPLES * |
967 | (size / (channel_count * DSLOGIC_ATOMIC_BYTES)) * sizeof(uint16_t)); | |
968 | if (!devc->deinterleave_buffer) { | |
969 | sr_err("Deinterleave buffer malloc failed."); | |
970 | g_free(devc->deinterleave_buffer); | |
971 | return SR_ERR_MALLOC; | |
972 | } | |
973 | ||
4bd770f5 JH |
974 | devc->num_transfers = num_transfers; |
975 | for (i = 0; i < num_transfers; i++) { | |
976 | if (!(buf = g_try_malloc(size))) { | |
977 | sr_err("USB transfer buffer malloc failed."); | |
978 | return SR_ERR_MALLOC; | |
979 | } | |
980 | transfer = libusb_alloc_transfer(0); | |
981 | libusb_fill_bulk_transfer(transfer, usb->devhdl, | |
982 | 6 | LIBUSB_ENDPOINT_IN, buf, size, | |
983 | receive_transfer, (void *)sdi, timeout); | |
984 | sr_info("submitting transfer: %d", i); | |
985 | if ((ret = libusb_submit_transfer(transfer)) != 0) { | |
986 | sr_err("Failed to submit transfer: %s.", | |
987 | libusb_error_name(ret)); | |
988 | libusb_free_transfer(transfer); | |
989 | g_free(buf); | |
990 | abort_acquisition(devc); | |
991 | return SR_ERR; | |
992 | } | |
993 | devc->transfers[i] = transfer; | |
994 | devc->submitted_transfers++; | |
995 | } | |
996 | ||
997 | std_session_send_df_header(sdi); | |
998 | ||
999 | return SR_OK; | |
1000 | } | |
1001 | ||
1002 | static void LIBUSB_CALL trigger_receive(struct libusb_transfer *transfer) | |
1003 | { | |
1004 | const struct sr_dev_inst *sdi; | |
1005 | struct dslogic_trigger_pos *tpos; | |
1006 | struct dev_context *devc; | |
1007 | ||
1008 | sdi = transfer->user_data; | |
1009 | devc = sdi->priv; | |
1010 | if (transfer->status == LIBUSB_TRANSFER_CANCELLED) { | |
1011 | sr_dbg("Trigger transfer canceled."); | |
1012 | /* Terminate session. */ | |
1013 | std_session_send_df_end(sdi); | |
1014 | usb_source_remove(sdi->session, devc->ctx); | |
1015 | devc->num_transfers = 0; | |
1016 | g_free(devc->transfers); | |
1017 | } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED | |
1018 | && transfer->actual_length == sizeof(struct dslogic_trigger_pos)) { | |
1019 | tpos = (struct dslogic_trigger_pos *)transfer->buffer; | |
10481ef0 JA |
1020 | sr_info("tpos real_pos %d ram_saddr %d cnt_h %d cnt_l %d", tpos->real_pos, |
1021 | tpos->ram_saddr, tpos->remain_cnt_h, tpos->remain_cnt_l); | |
4bd770f5 JH |
1022 | devc->trigger_pos = tpos->real_pos; |
1023 | g_free(tpos); | |
1024 | start_transfers(sdi); | |
1025 | } | |
1026 | libusb_free_transfer(transfer); | |
1027 | } | |
1028 | ||
658caaf0 | 1029 | SR_PRIV int dslogic_acquisition_start(const struct sr_dev_inst *sdi) |
4bd770f5 | 1030 | { |
03a0002e JH |
1031 | const unsigned int timeout = get_timeout(sdi); |
1032 | ||
658caaf0 JH |
1033 | struct sr_dev_driver *di; |
1034 | struct drv_context *drvc; | |
1035 | struct dev_context *devc; | |
4bd770f5 | 1036 | struct sr_usb_dev_inst *usb; |
4bd770f5 | 1037 | struct dslogic_trigger_pos *tpos; |
658caaf0 | 1038 | struct libusb_transfer *transfer; |
4bd770f5 JH |
1039 | int ret; |
1040 | ||
658caaf0 JH |
1041 | di = sdi->driver; |
1042 | drvc = di->context; | |
4bd770f5 | 1043 | devc = sdi->priv; |
658caaf0 JH |
1044 | usb = sdi->conn; |
1045 | ||
1046 | devc->ctx = drvc->sr_ctx; | |
1047 | devc->sent_samples = 0; | |
1048 | devc->empty_transfer_count = 0; | |
1049 | devc->acq_aborted = FALSE; | |
1050 | ||
658caaf0 | 1051 | usb_source_add(sdi->session, devc->ctx, timeout, receive_data, drvc); |
4bd770f5 JH |
1052 | |
1053 | if ((ret = command_stop_acquisition(sdi)) != SR_OK) | |
1054 | return ret; | |
1055 | ||
1056 | if ((ret = fpga_configure(sdi)) != SR_OK) | |
1057 | return ret; | |
1058 | ||
1059 | if ((ret = command_start_acquisition(sdi)) != SR_OK) | |
1060 | return ret; | |
1061 | ||
1062 | sr_dbg("Getting trigger."); | |
1063 | tpos = g_malloc(sizeof(struct dslogic_trigger_pos)); | |
1064 | transfer = libusb_alloc_transfer(0); | |
1065 | libusb_fill_bulk_transfer(transfer, usb->devhdl, 6 | LIBUSB_ENDPOINT_IN, | |
1066 | (unsigned char *)tpos, sizeof(struct dslogic_trigger_pos), | |
1067 | trigger_receive, (void *)sdi, 0); | |
1068 | if ((ret = libusb_submit_transfer(transfer)) < 0) { | |
1069 | sr_err("Failed to request trigger: %s.", libusb_error_name(ret)); | |
1070 | libusb_free_transfer(transfer); | |
1071 | g_free(tpos); | |
1072 | return SR_ERR; | |
1073 | } | |
1074 | ||
1075 | devc->transfers = g_try_malloc0(sizeof(*devc->transfers)); | |
1076 | if (!devc->transfers) { | |
1077 | sr_err("USB trigger_pos transfer malloc failed."); | |
1078 | return SR_ERR_MALLOC; | |
1079 | } | |
1080 | devc->num_transfers = 1; | |
1081 | devc->submitted_transfers++; | |
1082 | devc->transfers[0] = transfer; | |
1083 | ||
1084 | return ret; | |
1085 | } | |
1086 | ||
4bd770f5 JH |
1087 | SR_PRIV int dslogic_acquisition_stop(struct sr_dev_inst *sdi) |
1088 | { | |
1089 | command_stop_acquisition(sdi); | |
1090 | abort_acquisition(sdi->priv); | |
1091 | return SR_OK; | |
1092 | } |