]>
Commit | Line | Data |
---|---|---|
2f937611 UH |
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 | ||
6ec6c43b | 21 | #include <config.h> |
a7d7f93c BV |
22 | #include <glib.h> |
23 | #include <glib/gstdio.h> | |
2f937611 | 24 | #include "protocol.h" |
b9d53092 | 25 | #include "dslogic.h" |
2f937611 | 26 | |
2f937611 UH |
27 | #pragma pack(push, 1) |
28 | ||
29 | struct version_info { | |
30 | uint8_t major; | |
31 | uint8_t minor; | |
32 | }; | |
33 | ||
34 | struct cmd_start_acquisition { | |
35 | uint8_t flags; | |
36 | uint8_t sample_delay_h; | |
37 | uint8_t sample_delay_l; | |
38 | }; | |
39 | ||
40 | #pragma pack(pop) | |
41 | ||
4df5739a UH |
42 | #define USB_TIMEOUT 100 |
43 | ||
2f937611 UH |
44 | static int command_get_fw_version(libusb_device_handle *devhdl, |
45 | struct version_info *vi) | |
46 | { | |
47 | int ret; | |
48 | ||
49 | ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR | | |
50 | LIBUSB_ENDPOINT_IN, CMD_GET_FW_VERSION, 0x0000, 0x0000, | |
4df5739a | 51 | (unsigned char *)vi, sizeof(struct version_info), USB_TIMEOUT); |
2f937611 UH |
52 | |
53 | if (ret < 0) { | |
54 | sr_err("Unable to get version info: %s.", | |
55 | libusb_error_name(ret)); | |
56 | return SR_ERR; | |
57 | } | |
58 | ||
59 | return SR_OK; | |
60 | } | |
61 | ||
a54edb1d | 62 | static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid) |
2f937611 | 63 | { |
a7d7f93c | 64 | struct dev_context *devc = sdi->priv; |
a54edb1d ML |
65 | struct sr_usb_dev_inst *usb = sdi->conn; |
66 | libusb_device_handle *devhdl = usb->devhdl; | |
a7d7f93c | 67 | int cmd, ret; |
2f937611 | 68 | |
b9d53092 | 69 | cmd = devc->dslogic ? DS_CMD_GET_REVID_VERSION : CMD_GET_REVID_VERSION; |
2f937611 | 70 | ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR | |
4df5739a | 71 | LIBUSB_ENDPOINT_IN, cmd, 0x0000, 0x0000, revid, 1, USB_TIMEOUT); |
2f937611 UH |
72 | |
73 | if (ret < 0) { | |
74 | sr_err("Unable to get REVID: %s.", libusb_error_name(ret)); | |
75 | return SR_ERR; | |
76 | } | |
77 | ||
78 | return SR_OK; | |
79 | } | |
80 | ||
a54edb1d | 81 | SR_PRIV int fx2lafw_command_start_acquisition(const struct sr_dev_inst *sdi) |
2f937611 | 82 | { |
81a34976 BV |
83 | struct dev_context *devc; |
84 | struct sr_usb_dev_inst *usb; | |
85 | uint64_t samplerate; | |
86 | struct cmd_start_acquisition cmd; | |
87 | int delay, ret; | |
88 | ||
89 | devc = sdi->priv; | |
90 | usb = sdi->conn; | |
91 | samplerate = devc->cur_samplerate; | |
2f937611 UH |
92 | |
93 | /* Compute the sample rate. */ | |
81a34976 | 94 | if (devc->sample_wide && samplerate > MAX_16BIT_SAMPLE_RATE) { |
2f937611 UH |
95 | sr_err("Unable to sample at %" PRIu64 "Hz " |
96 | "when collecting 16-bit samples.", samplerate); | |
97 | return SR_ERR; | |
98 | } | |
99 | ||
81a34976 BV |
100 | delay = 0; |
101 | cmd.flags = cmd.sample_delay_h = cmd.sample_delay_l = 0; | |
2f937611 UH |
102 | if ((SR_MHZ(48) % samplerate) == 0) { |
103 | cmd.flags = CMD_START_FLAGS_CLK_48MHZ; | |
104 | delay = SR_MHZ(48) / samplerate - 1; | |
105 | if (delay > MAX_SAMPLE_DELAY) | |
106 | delay = 0; | |
107 | } | |
108 | ||
109 | if (delay == 0 && (SR_MHZ(30) % samplerate) == 0) { | |
110 | cmd.flags = CMD_START_FLAGS_CLK_30MHZ; | |
111 | delay = SR_MHZ(30) / samplerate - 1; | |
112 | } | |
113 | ||
b9d53092 | 114 | sr_dbg("GPIF delay = %d, clocksource = %sMHz.", delay, |
2f937611 UH |
115 | (cmd.flags & CMD_START_FLAGS_CLK_48MHZ) ? "48" : "30"); |
116 | ||
117 | if (delay <= 0 || delay > MAX_SAMPLE_DELAY) { | |
118 | sr_err("Unable to sample at %" PRIu64 "Hz.", samplerate); | |
119 | return SR_ERR; | |
120 | } | |
121 | ||
122 | cmd.sample_delay_h = (delay >> 8) & 0xff; | |
123 | cmd.sample_delay_l = delay & 0xff; | |
124 | ||
125 | /* Select the sampling width. */ | |
81a34976 | 126 | cmd.flags |= devc->sample_wide ? CMD_START_FLAGS_SAMPLE_16BIT : |
2f937611 | 127 | CMD_START_FLAGS_SAMPLE_8BIT; |
7fb90f94 | 128 | /* Enable CTL2 clock. */ |
f9592d65 | 129 | cmd.flags |= (g_slist_length(devc->enabled_analog_channels) > 0) ? CMD_START_FLAGS_CLK_CTL2 : 0; |
2f937611 UH |
130 | |
131 | /* Send the control message. */ | |
81a34976 | 132 | ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR | |
2f937611 | 133 | LIBUSB_ENDPOINT_OUT, CMD_START, 0x0000, 0x0000, |
4df5739a | 134 | (unsigned char *)&cmd, sizeof(cmd), USB_TIMEOUT); |
2f937611 UH |
135 | if (ret < 0) { |
136 | sr_err("Unable to send start command: %s.", | |
137 | libusb_error_name(ret)); | |
138 | return SR_ERR; | |
139 | } | |
140 | ||
141 | return SR_OK; | |
142 | } | |
143 | ||
144 | /** | |
145 | * Check the USB configuration to determine if this is an fx2lafw device. | |
146 | * | |
a7d7f93c | 147 | * @return TRUE if the device's configuration profile matches fx2lafw |
6fcf3f0a | 148 | * configuration, FALSE otherwise. |
2f937611 | 149 | */ |
a7d7f93c BV |
150 | SR_PRIV gboolean match_manuf_prod(libusb_device *dev, const char *manufacturer, |
151 | const char *product) | |
2f937611 UH |
152 | { |
153 | struct libusb_device_descriptor des; | |
154 | struct libusb_device_handle *hdl; | |
155 | gboolean ret; | |
156 | unsigned char strdesc[64]; | |
157 | ||
158 | hdl = NULL; | |
159 | ret = FALSE; | |
160 | while (!ret) { | |
161 | /* Assume the FW has not been loaded, unless proven wrong. */ | |
2a8f2d41 | 162 | libusb_get_device_descriptor(dev, &des); |
2f937611 UH |
163 | |
164 | if (libusb_open(dev, &hdl) != 0) | |
165 | break; | |
166 | ||
167 | if (libusb_get_string_descriptor_ascii(hdl, | |
a7d7f93c | 168 | des.iManufacturer, strdesc, sizeof(strdesc)) < 0) |
2f937611 | 169 | break; |
bc497772 | 170 | if (strcmp((const char *)strdesc, manufacturer)) |
2f937611 UH |
171 | break; |
172 | ||
173 | if (libusb_get_string_descriptor_ascii(hdl, | |
174 | des.iProduct, strdesc, sizeof(strdesc)) < 0) | |
175 | break; | |
bc497772 | 176 | if (strcmp((const char *)strdesc, product)) |
2f937611 UH |
177 | break; |
178 | ||
2f937611 UH |
179 | ret = TRUE; |
180 | } | |
181 | if (hdl) | |
182 | libusb_close(hdl); | |
183 | ||
184 | return ret; | |
185 | } | |
186 | ||
187 | SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di) | |
188 | { | |
189 | libusb_device **devlist; | |
190 | struct sr_usb_dev_inst *usb; | |
191 | struct libusb_device_descriptor des; | |
192 | struct dev_context *devc; | |
193 | struct drv_context *drvc; | |
194 | struct version_info vi; | |
5e2c86eb | 195 | int ret, i, device_count; |
2f937611 | 196 | uint8_t revid; |
5e2c86eb | 197 | char connection_id[64]; |
2f937611 | 198 | |
41812aca | 199 | drvc = di->context; |
2f937611 UH |
200 | devc = sdi->priv; |
201 | usb = sdi->conn; | |
202 | ||
203 | if (sdi->status == SR_ST_ACTIVE) | |
204 | /* Device is already in use. */ | |
205 | return SR_ERR; | |
206 | ||
2f937611 UH |
207 | device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
208 | if (device_count < 0) { | |
209 | sr_err("Failed to get device list: %s.", | |
210 | libusb_error_name(device_count)); | |
211 | return SR_ERR; | |
212 | } | |
213 | ||
214 | for (i = 0; i < device_count; i++) { | |
2a8f2d41 | 215 | libusb_get_device_descriptor(devlist[i], &des); |
2f937611 UH |
216 | |
217 | if (des.idVendor != devc->profile->vid | |
218 | || des.idProduct != devc->profile->pid) | |
219 | continue; | |
220 | ||
5e2c86eb SA |
221 | if ((sdi->status == SR_ST_INITIALIZING) || |
222 | (sdi->status == SR_ST_INACTIVE)) { | |
2f937611 | 223 | /* |
5e2c86eb | 224 | * Check device by its physical USB bus/port address. |
2f937611 | 225 | */ |
5e2c86eb SA |
226 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); |
227 | if (strcmp(sdi->connection_id, connection_id)) | |
2f937611 UH |
228 | /* This is not the one. */ |
229 | continue; | |
230 | } | |
231 | ||
232 | if (!(ret = libusb_open(devlist[i], &usb->devhdl))) { | |
233 | if (usb->address == 0xff) | |
234 | /* | |
235 | * First time we touch this device after FW | |
236 | * upload, so we don't know the address yet. | |
237 | */ | |
238 | usb->address = libusb_get_device_address(devlist[i]); | |
239 | } else { | |
240 | sr_err("Failed to open device: %s.", | |
241 | libusb_error_name(ret)); | |
242 | break; | |
243 | } | |
244 | ||
dc2903bb | 245 | if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) { |
c11a1e61 UH |
246 | if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) { |
247 | if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) { | |
dc2903bb UH |
248 | sr_err("Failed to detach kernel driver: %s.", |
249 | libusb_error_name(ret)); | |
250 | return SR_ERR; | |
251 | } | |
252 | } | |
253 | } | |
254 | ||
2f937611 UH |
255 | ret = command_get_fw_version(usb->devhdl, &vi); |
256 | if (ret != SR_OK) { | |
257 | sr_err("Failed to get firmware version."); | |
258 | break; | |
259 | } | |
260 | ||
a54edb1d | 261 | ret = command_get_revid_version(sdi, &revid); |
2f937611 UH |
262 | if (ret != SR_OK) { |
263 | sr_err("Failed to get REVID."); | |
264 | break; | |
265 | } | |
266 | ||
267 | /* | |
268 | * Changes in major version mean incompatible/API changes, so | |
269 | * bail out if we encounter an incompatible version. | |
270 | * Different minor versions are OK, they should be compatible. | |
271 | */ | |
272 | if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) { | |
273 | sr_err("Expected firmware version %d.x, " | |
274 | "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR, | |
275 | vi.major, vi.minor); | |
276 | break; | |
277 | } | |
278 | ||
279 | sdi->status = SR_ST_ACTIVE; | |
5e2c86eb | 280 | sr_info("Opened device on %d.%d (logical) / %s (physical), " |
2f937611 | 281 | "interface %d, firmware %d.%d.", |
5e2c86eb | 282 | usb->bus, usb->address, connection_id, |
2f937611 UH |
283 | USB_INTERFACE, vi.major, vi.minor); |
284 | ||
285 | sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.", | |
286 | revid, (revid != 1) ? " (FX2)" : "A (FX2LP)"); | |
287 | ||
288 | break; | |
289 | } | |
290 | libusb_free_device_list(devlist, 1); | |
291 | ||
292 | if (sdi->status != SR_ST_ACTIVE) | |
293 | return SR_ERR; | |
294 | ||
295 | return SR_OK; | |
296 | } | |
297 | ||
2f937611 UH |
298 | SR_PRIV struct dev_context *fx2lafw_dev_new(void) |
299 | { | |
300 | struct dev_context *devc; | |
301 | ||
f57d8ffe | 302 | devc = g_malloc0(sizeof(struct dev_context)); |
2f937611 UH |
303 | devc->profile = NULL; |
304 | devc->fw_updated = 0; | |
305 | devc->cur_samplerate = 0; | |
306 | devc->limit_samples = 0; | |
7bfcb25c | 307 | devc->capture_ratio = 0; |
87b545fb | 308 | devc->sample_wide = FALSE; |
41dc2547 | 309 | devc->dslogic_continuous_mode = FALSE; |
d9a58763 | 310 | devc->dslogic_clock_edge = DS_EDGE_RISING; |
335122f0 | 311 | devc->stl = NULL; |
2f937611 UH |
312 | |
313 | return devc; | |
314 | } | |
315 | ||
316 | SR_PRIV void fx2lafw_abort_acquisition(struct dev_context *devc) | |
317 | { | |
318 | int i; | |
319 | ||
b0ccd64d | 320 | devc->acq_aborted = TRUE; |
2f937611 UH |
321 | |
322 | for (i = devc->num_transfers - 1; i >= 0; i--) { | |
323 | if (devc->transfers[i]) | |
324 | libusb_cancel_transfer(devc->transfers[i]); | |
325 | } | |
326 | } | |
327 | ||
102f1239 | 328 | static void finish_acquisition(struct sr_dev_inst *sdi) |
2f937611 | 329 | { |
102f1239 BV |
330 | struct dev_context *devc; |
331 | ||
332 | devc = sdi->priv; | |
2f937611 | 333 | |
bee2b016 | 334 | std_session_send_df_end(sdi); |
2f937611 | 335 | |
102f1239 | 336 | usb_source_remove(sdi->session, devc->ctx); |
2f937611 UH |
337 | |
338 | devc->num_transfers = 0; | |
339 | g_free(devc->transfers); | |
335122f0 | 340 | |
f9592d65 UH |
341 | /* Free the deinterlace buffers if we had them. */ |
342 | if (g_slist_length(devc->enabled_analog_channels) > 0) { | |
343 | g_free(devc->logic_buffer); | |
344 | g_free(devc->analog_buffer); | |
345 | } | |
7e5ccff2 | 346 | |
335122f0 BV |
347 | if (devc->stl) { |
348 | soft_trigger_logic_free(devc->stl); | |
349 | devc->stl = NULL; | |
350 | } | |
2f937611 UH |
351 | } |
352 | ||
353 | static void free_transfer(struct libusb_transfer *transfer) | |
354 | { | |
9615eeb5 | 355 | struct sr_dev_inst *sdi; |
2f937611 UH |
356 | struct dev_context *devc; |
357 | unsigned int i; | |
358 | ||
9615eeb5 BV |
359 | sdi = transfer->user_data; |
360 | devc = sdi->priv; | |
2f937611 UH |
361 | |
362 | g_free(transfer->buffer); | |
363 | transfer->buffer = NULL; | |
364 | libusb_free_transfer(transfer); | |
365 | ||
366 | for (i = 0; i < devc->num_transfers; i++) { | |
367 | if (devc->transfers[i] == transfer) { | |
368 | devc->transfers[i] = NULL; | |
369 | break; | |
370 | } | |
371 | } | |
372 | ||
373 | devc->submitted_transfers--; | |
374 | if (devc->submitted_transfers == 0) | |
102f1239 | 375 | finish_acquisition(sdi); |
2f937611 UH |
376 | } |
377 | ||
378 | static void resubmit_transfer(struct libusb_transfer *transfer) | |
379 | { | |
380 | int ret; | |
381 | ||
382 | if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS) | |
383 | return; | |
384 | ||
9615eeb5 | 385 | sr_err("%s: %s", __func__, libusb_error_name(ret)); |
2f937611 | 386 | free_transfer(transfer); |
2f937611 | 387 | |
9615eeb5 BV |
388 | } |
389 | ||
695dc859 | 390 | SR_PRIV void mso_send_data_proc(struct sr_dev_inst *sdi, |
7e5ccff2 JH |
391 | uint8_t *data, size_t length, size_t sample_width) |
392 | { | |
393 | size_t i; | |
695dc859 | 394 | struct dev_context *devc; |
a6ad49b3 UH |
395 | struct sr_datafeed_analog analog; |
396 | struct sr_analog_encoding encoding; | |
397 | struct sr_analog_meaning meaning; | |
398 | struct sr_analog_spec spec; | |
695dc859 | 399 | |
e91d4ce2 UH |
400 | (void)sample_width; |
401 | ||
695dc859 UH |
402 | devc = sdi->priv; |
403 | ||
7e5ccff2 JH |
404 | length /= 2; |
405 | ||
7e5ccff2 | 406 | /* Send the logic */ |
695dc859 | 407 | for (i = 0; i < length; i++) { |
d9251a2c | 408 | devc->logic_buffer[i] = data[i * 2]; |
7fb90f94 | 409 | /* Rescale to -10V - +10V from 0-255. */ |
2ab0679a | 410 | devc->analog_buffer[i] = (data[i * 2 + 1] - 128.0f) / 12.8f; |
7e5ccff2 JH |
411 | }; |
412 | ||
413 | const struct sr_datafeed_logic logic = { | |
414 | .length = length, | |
415 | .unitsize = 1, | |
416 | .data = devc->logic_buffer | |
417 | }; | |
418 | ||
419 | const struct sr_datafeed_packet logic_packet = { | |
420 | .type = SR_DF_LOGIC, | |
421 | .payload = &logic | |
422 | }; | |
695dc859 UH |
423 | |
424 | sr_session_send(sdi, &logic_packet); | |
7e5ccff2 | 425 | |
0cce2383 | 426 | sr_analog_init(&analog, &encoding, &meaning, &spec, 2); |
a6ad49b3 UH |
427 | analog.meaning->channels = devc->enabled_analog_channels; |
428 | analog.meaning->mq = SR_MQ_VOLTAGE; | |
429 | analog.meaning->unit = SR_UNIT_VOLT; | |
430 | analog.meaning->mqflags = 0 /* SR_MQFLAG_DC */; | |
431 | analog.num_samples = length; | |
432 | analog.data = devc->analog_buffer; | |
7e5ccff2 JH |
433 | |
434 | const struct sr_datafeed_packet analog_packet = { | |
a6ad49b3 | 435 | .type = SR_DF_ANALOG, |
7e5ccff2 JH |
436 | .payload = &analog |
437 | }; | |
7e5ccff2 | 438 | |
695dc859 | 439 | sr_session_send(sdi, &analog_packet); |
7e5ccff2 JH |
440 | } |
441 | ||
695dc859 | 442 | SR_PRIV void la_send_data_proc(struct sr_dev_inst *sdi, |
7b5d1c64 JH |
443 | uint8_t *data, size_t length, size_t sample_width) |
444 | { | |
445 | const struct sr_datafeed_logic logic = { | |
446 | .length = length, | |
447 | .unitsize = sample_width, | |
448 | .data = data | |
449 | }; | |
450 | ||
451 | const struct sr_datafeed_packet packet = { | |
452 | .type = SR_DF_LOGIC, | |
453 | .payload = &logic | |
454 | }; | |
455 | ||
695dc859 | 456 | sr_session_send(sdi, &packet); |
7b5d1c64 JH |
457 | } |
458 | ||
55462b8b | 459 | SR_PRIV void LIBUSB_CALL fx2lafw_receive_transfer(struct libusb_transfer *transfer) |
2f937611 | 460 | { |
9615eeb5 BV |
461 | struct sr_dev_inst *sdi; |
462 | struct dev_context *devc; | |
2f937611 | 463 | gboolean packet_has_error = FALSE; |
4237fbca | 464 | struct sr_datafeed_packet packet; |
9615eeb5 BV |
465 | unsigned int num_samples; |
466 | int trigger_offset, cur_sample_count, unitsize; | |
7bfcb25c | 467 | int pre_trigger_samples; |
2f937611 | 468 | |
9615eeb5 BV |
469 | sdi = transfer->user_data; |
470 | devc = sdi->priv; | |
2f937611 UH |
471 | |
472 | /* | |
473 | * If acquisition has already ended, just free any queued up | |
474 | * transfer that come in. | |
475 | */ | |
b0ccd64d | 476 | if (devc->acq_aborted) { |
2f937611 UH |
477 | free_transfer(transfer); |
478 | return; | |
479 | } | |
480 | ||
0f262763 UH |
481 | sr_dbg("receive_transfer(): status %s received %d bytes.", |
482 | libusb_error_name(transfer->status), transfer->actual_length); | |
2f937611 UH |
483 | |
484 | /* Save incoming transfer before reusing the transfer struct. */ | |
9615eeb5 BV |
485 | unitsize = devc->sample_wide ? 2 : 1; |
486 | cur_sample_count = transfer->actual_length / unitsize; | |
2f937611 UH |
487 | |
488 | switch (transfer->status) { | |
489 | case LIBUSB_TRANSFER_NO_DEVICE: | |
490 | fx2lafw_abort_acquisition(devc); | |
491 | free_transfer(transfer); | |
492 | return; | |
493 | case LIBUSB_TRANSFER_COMPLETED: | |
494 | case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */ | |
495 | break; | |
496 | default: | |
497 | packet_has_error = TRUE; | |
498 | break; | |
499 | } | |
500 | ||
501 | if (transfer->actual_length == 0 || packet_has_error) { | |
502 | devc->empty_transfer_count++; | |
503 | if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) { | |
504 | /* | |
505 | * The FX2 gave up. End the acquisition, the frontend | |
506 | * will work out that the samplecount is short. | |
507 | */ | |
508 | fx2lafw_abort_acquisition(devc); | |
509 | free_transfer(transfer); | |
510 | } else { | |
511 | resubmit_transfer(transfer); | |
512 | } | |
513 | return; | |
514 | } else { | |
515 | devc->empty_transfer_count = 0; | |
516 | } | |
9615eeb5 | 517 | if (devc->trigger_fired) { |
2f663c82 | 518 | if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) { |
9615eeb5 | 519 | /* Send the incoming transfer to the session bus. */ |
2f663c82 | 520 | if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples) |
9615eeb5 BV |
521 | num_samples = devc->limit_samples - devc->sent_samples; |
522 | else | |
523 | num_samples = cur_sample_count; | |
7b5d1c64 | 524 | |
9803346f UH |
525 | if (devc->dslogic && devc->trigger_pos > devc->sent_samples |
526 | && devc->trigger_pos <= devc->sent_samples + num_samples) { | |
527 | /* DSLogic trigger in this block. Send trigger position. */ | |
4237fbca | 528 | trigger_offset = devc->trigger_pos - devc->sent_samples; |
9803346f | 529 | /* Pre-trigger samples. */ |
4237fbca DA |
530 | devc->send_data_proc(sdi, (uint8_t *)transfer->buffer, |
531 | trigger_offset * unitsize, unitsize); | |
532 | devc->sent_samples += trigger_offset; | |
9803346f | 533 | /* Trigger position. */ |
4237fbca DA |
534 | devc->trigger_pos = 0; |
535 | packet.type = SR_DF_TRIGGER; | |
536 | packet.payload = NULL; | |
537 | sr_session_send(sdi, &packet); | |
9803346f | 538 | /* Post trigger samples. */ |
4237fbca DA |
539 | num_samples -= trigger_offset; |
540 | devc->send_data_proc(sdi, (uint8_t *)transfer->buffer | |
9803346f | 541 | + trigger_offset * unitsize, num_samples * unitsize, unitsize); |
4237fbca | 542 | devc->sent_samples += num_samples; |
9803346f | 543 | } else { |
4237fbca DA |
544 | devc->send_data_proc(sdi, (uint8_t *)transfer->buffer, |
545 | num_samples * unitsize, unitsize); | |
546 | devc->sent_samples += num_samples; | |
547 | } | |
9615eeb5 BV |
548 | } |
549 | } else { | |
335122f0 | 550 | trigger_offset = soft_trigger_logic_check(devc->stl, |
7bfcb25c | 551 | transfer->buffer, transfer->actual_length, &pre_trigger_samples); |
9615eeb5 | 552 | if (trigger_offset > -1) { |
7bfcb25c | 553 | devc->sent_samples += pre_trigger_samples; |
9615eeb5 BV |
554 | num_samples = cur_sample_count - trigger_offset; |
555 | if (devc->limit_samples && | |
556 | num_samples > devc->limit_samples - devc->sent_samples) | |
557 | num_samples = devc->limit_samples - devc->sent_samples; | |
7b5d1c64 | 558 | |
695dc859 UH |
559 | devc->send_data_proc(sdi, (uint8_t *)transfer->buffer |
560 | + trigger_offset * unitsize, | |
561 | num_samples * unitsize, unitsize); | |
9615eeb5 BV |
562 | devc->sent_samples += num_samples; |
563 | ||
564 | devc->trigger_fired = TRUE; | |
2f937611 | 565 | } |
06b5d7f7 BV |
566 | } |
567 | ||
568 | if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) { | |
569 | fx2lafw_abort_acquisition(devc); | |
570 | free_transfer(transfer); | |
9615eeb5 BV |
571 | } else |
572 | resubmit_transfer(transfer); | |
2f937611 UH |
573 | } |
574 | ||
575 | static unsigned int to_bytes_per_ms(unsigned int samplerate) | |
576 | { | |
577 | return samplerate / 1000; | |
578 | } | |
579 | ||
580 | SR_PRIV size_t fx2lafw_get_buffer_size(struct dev_context *devc) | |
581 | { | |
582 | size_t s; | |
583 | ||
584 | /* | |
585 | * The buffer should be large enough to hold 10ms of data and | |
586 | * a multiple of 512. | |
587 | */ | |
588 | s = 10 * to_bytes_per_ms(devc->cur_samplerate); | |
589 | return (s + 511) & ~511; | |
590 | } | |
591 | ||
592 | SR_PRIV unsigned int fx2lafw_get_number_of_transfers(struct dev_context *devc) | |
593 | { | |
594 | unsigned int n; | |
595 | ||
596 | /* Total buffer size should be able to hold about 500ms of data. */ | |
597 | n = (500 * to_bytes_per_ms(devc->cur_samplerate) / | |
598 | fx2lafw_get_buffer_size(devc)); | |
599 | ||
600 | if (n > NUM_SIMUL_TRANSFERS) | |
601 | return NUM_SIMUL_TRANSFERS; | |
602 | ||
603 | return n; | |
604 | } | |
605 | ||
606 | SR_PRIV unsigned int fx2lafw_get_timeout(struct dev_context *devc) | |
607 | { | |
608 | size_t total_size; | |
609 | unsigned int timeout; | |
610 | ||
611 | total_size = fx2lafw_get_buffer_size(devc) * | |
612 | fx2lafw_get_number_of_transfers(devc); | |
613 | timeout = total_size / to_bytes_per_ms(devc->cur_samplerate); | |
614 | return timeout + timeout / 4; /* Leave a headroom of 25% percent. */ | |
615 | } |