2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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/>.
24 #include <glib/gstdio.h>
27 #define DS_CMD_GET_FW_VERSION 0xb0
28 #define DS_CMD_GET_REVID_VERSION 0xb1
29 #define DS_CMD_START 0xb2
30 #define DS_CMD_CONFIG 0xb3
31 #define DS_CMD_SETTING 0xb4
32 #define DS_CMD_CONTROL 0xb5
33 #define DS_CMD_STATUS 0xb6
34 #define DS_CMD_STATUS_INFO 0xb7
35 #define DS_CMD_WR_REG 0xb8
36 #define DS_CMD_WR_NVM 0xb9
37 #define DS_CMD_RD_NVM 0xba
38 #define DS_CMD_RD_NVM_PRE 0xbb
39 #define DS_CMD_GET_HW_INFO 0xbc
41 #define DS_START_FLAGS_STOP (1 << 7)
42 #define DS_START_FLAGS_CLK_48MHZ (1 << 6)
43 #define DS_START_FLAGS_SAMPLE_WIDE (1 << 5)
44 #define DS_START_FLAGS_MODE_LA (1 << 4)
46 #define DS_ADDR_COMB 0x68
47 #define DS_ADDR_EEWP 0x70
48 #define DS_ADDR_VTH 0x78
50 #define DS_MAX_LOGIC_DEPTH SR_MHZ(16)
51 #define DS_MAX_LOGIC_SAMPLERATE SR_MHZ(100)
52 #define DS_MAX_TRIG_PERCENT 90
54 #define DS_MODE_TRIG_EN (1 << 0)
55 #define DS_MODE_CLK_TYPE (1 << 1)
56 #define DS_MODE_CLK_EDGE (1 << 2)
57 #define DS_MODE_RLE_MODE (1 << 3)
58 #define DS_MODE_DSO_MODE (1 << 4)
59 #define DS_MODE_HALF_MODE (1 << 5)
60 #define DS_MODE_QUAR_MODE (1 << 6)
61 #define DS_MODE_ANALOG_MODE (1 << 7)
62 #define DS_MODE_FILTER (1 << 8)
63 #define DS_MODE_INSTANT (1 << 9)
64 #define DS_MODE_STRIG_MODE (1 << 11)
65 #define DS_MODE_STREAM_MODE (1 << 12)
66 #define DS_MODE_LPB_TEST (1 << 13)
67 #define DS_MODE_EXT_TEST (1 << 14)
68 #define DS_MODE_INT_TEST (1 << 15)
70 #define DSLOGIC_ATOMIC_SAMPLES (sizeof(uint64_t) * 8)
71 #define DSLOGIC_ATOMIC_BYTES sizeof(uint64_t)
74 * The FPGA is configured with TLV tuples. Length is specified as the
75 * number of 16-bit words.
77 #define _DS_CFG(variable, wordcnt) ((variable << 8) | wordcnt)
78 #define DS_CFG_START 0xf5a5f5a5
79 #define DS_CFG_MODE _DS_CFG(0, 1)
80 #define DS_CFG_DIVIDER _DS_CFG(1, 2)
81 #define DS_CFG_COUNT _DS_CFG(3, 2)
82 #define DS_CFG_TRIG_POS _DS_CFG(5, 2)
83 #define DS_CFG_TRIG_GLB _DS_CFG(7, 1)
84 #define DS_CFG_CH_EN _DS_CFG(8, 1)
85 #define DS_CFG_TRIG _DS_CFG(64, 160)
86 #define DS_CFG_END 0xfa5afa5a
95 struct cmd_start_acquisition {
97 uint8_t sample_delay_h;
98 uint8_t sample_delay_l;
104 uint16_t mode_header;
106 uint16_t divider_header;
108 uint16_t count_header;
110 uint16_t trig_pos_header;
112 uint16_t trig_glb_header;
114 uint16_t ch_en_header;
117 uint16_t trig_header;
118 uint16_t trig_mask0[NUM_TRIGGER_STAGES];
119 uint16_t trig_mask1[NUM_TRIGGER_STAGES];
120 uint16_t trig_value0[NUM_TRIGGER_STAGES];
121 uint16_t trig_value1[NUM_TRIGGER_STAGES];
122 uint16_t trig_edge0[NUM_TRIGGER_STAGES];
123 uint16_t trig_edge1[NUM_TRIGGER_STAGES];
124 uint16_t trig_logic0[NUM_TRIGGER_STAGES];
125 uint16_t trig_logic1[NUM_TRIGGER_STAGES];
126 uint32_t trig_count[NUM_TRIGGER_STAGES];
134 * This should be larger than the FPGA bitstream image so that it'll get
135 * uploaded in one big operation. There seem to be issues when uploading
138 #define FW_BUFSIZE (1024 * 1024)
140 #define FPGA_UPLOAD_DELAY (10 * 1000)
142 #define USB_TIMEOUT (3 * 1000)
144 static int command_get_fw_version(libusb_device_handle *devhdl,
145 struct version_info *vi)
149 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
150 LIBUSB_ENDPOINT_IN, DS_CMD_GET_FW_VERSION, 0x0000, 0x0000,
151 (unsigned char *)vi, sizeof(struct version_info), USB_TIMEOUT);
154 sr_err("Unable to get version info: %s.",
155 libusb_error_name(ret));
162 static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid)
164 struct sr_usb_dev_inst *usb = sdi->conn;
165 libusb_device_handle *devhdl = usb->devhdl;
168 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
169 LIBUSB_ENDPOINT_IN, DS_CMD_GET_REVID_VERSION, 0x0000, 0x0000,
170 revid, 1, USB_TIMEOUT);
173 sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
180 static int command_start_acquisition(const struct sr_dev_inst *sdi)
182 struct sr_usb_dev_inst *usb;
183 struct dslogic_mode mode;
186 mode.flags = DS_START_FLAGS_MODE_LA | DS_START_FLAGS_SAMPLE_WIDE;
187 mode.sample_delay_h = mode.sample_delay_l = 0;
190 ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
191 LIBUSB_ENDPOINT_OUT, DS_CMD_START, 0x0000, 0x0000,
192 (unsigned char *)&mode, sizeof(mode), USB_TIMEOUT);
194 sr_err("Failed to send start command: %s.", libusb_error_name(ret));
201 static int command_stop_acquisition(const struct sr_dev_inst *sdi)
203 struct sr_usb_dev_inst *usb;
204 struct dslogic_mode mode;
207 mode.flags = DS_START_FLAGS_STOP;
208 mode.sample_delay_h = mode.sample_delay_l = 0;
211 ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
212 LIBUSB_ENDPOINT_OUT, DS_CMD_START, 0x0000, 0x0000,
213 (unsigned char *)&mode, sizeof(struct dslogic_mode), USB_TIMEOUT);
215 sr_err("Failed to send stop command: %s.", libusb_error_name(ret));
222 SR_PRIV int dslogic_fpga_firmware_upload(const struct sr_dev_inst *sdi)
224 const char *name = NULL;
226 struct sr_resource bitstream;
227 struct drv_context *drvc;
228 struct dev_context *devc;
229 struct sr_usb_dev_inst *usb;
234 const uint8_t cmd[3] = {0, 0, 0};
236 drvc = sdi->driver->context;
240 if (!strcmp(devc->profile->model, "DSLogic")) {
241 if (devc->cur_threshold < 1.40)
242 name = DSLOGIC_FPGA_FIRMWARE_3V3;
244 name = DSLOGIC_FPGA_FIRMWARE_5V;
245 } else if (!strcmp(devc->profile->model, "DSLogic Pro")){
246 name = DSLOGIC_PRO_FPGA_FIRMWARE;
247 } else if (!strcmp(devc->profile->model, "DSLogic Plus")){
248 name = DSLOGIC_PLUS_FPGA_FIRMWARE;
249 } else if (!strcmp(devc->profile->model, "DSLogic Basic")){
250 name = DSLOGIC_BASIC_FPGA_FIRMWARE;
251 } else if (!strcmp(devc->profile->model, "DSCope")) {
252 name = DSCOPE_FPGA_FIRMWARE;
254 sr_err("Failed to select FPGA firmware.");
258 sr_dbg("Uploading FPGA firmware '%s'.", name);
260 result = sr_resource_open(drvc->sr_ctx, &bitstream,
261 SR_RESOURCE_FIRMWARE, name);
265 /* Tell the device firmware is coming. */
266 if ((ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
267 LIBUSB_ENDPOINT_OUT, DS_CMD_CONFIG, 0x0000, 0x0000,
268 (unsigned char *)&cmd, sizeof(cmd), USB_TIMEOUT)) < 0) {
269 sr_err("Failed to upload FPGA firmware: %s.", libusb_error_name(ret));
270 sr_resource_close(drvc->sr_ctx, &bitstream);
274 /* Give the FX2 time to get ready for FPGA firmware upload. */
275 g_usleep(FPGA_UPLOAD_DELAY);
277 buf = g_malloc(FW_BUFSIZE);
281 chunksize = sr_resource_read(drvc->sr_ctx, &bitstream,
288 if ((ret = libusb_bulk_transfer(usb->devhdl, 2 | LIBUSB_ENDPOINT_OUT,
289 buf, chunksize, &transferred, USB_TIMEOUT)) < 0) {
290 sr_err("Unable to configure FPGA firmware: %s.",
291 libusb_error_name(ret));
296 sr_spew("Uploaded %" PRIu64 "/%" PRIu64 " bytes.",
297 sum, bitstream.size);
299 if (transferred != chunksize) {
300 sr_err("Short transfer while uploading FPGA firmware.");
306 sr_resource_close(drvc->sr_ctx, &bitstream);
309 sr_dbg("FPGA firmware upload done.");
314 static unsigned int enabled_channel_count(const struct sr_dev_inst *sdi)
316 unsigned int count = 0;
317 for (const GSList *l = sdi->channels; l; l = l->next) {
318 const struct sr_channel *const probe = (struct sr_channel *)l->data;
325 static uint16_t enabled_channel_mask(const struct sr_dev_inst *sdi)
327 unsigned int mask = 0;
328 for (const GSList *l = sdi->channels; l; l = l->next) {
329 const struct sr_channel *const probe = (struct sr_channel *)l->data;
331 mask |= 1 << probe->index;
337 * Get the session trigger and configure the FPGA structure
340 static void set_trigger(const struct sr_dev_inst *sdi, struct fpga_config *cfg)
342 struct sr_trigger *trigger;
343 struct sr_trigger_stage *stage;
344 struct sr_trigger_match *match;
345 struct dev_context *devc;
347 const unsigned int num_enabled_channels = enabled_channel_count(sdi);
348 int num_trigger_stages = 0;
350 int channelbit, i = 0;
351 uint32_t trigger_point;
355 cfg->ch_en = enabled_channel_mask(sdi);
357 cfg->trig_glb = num_enabled_channels << 4;
359 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
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;
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);
381 if (!(trigger = sr_session_trigger_get(sdi->session))) {
382 sr_dbg("No session trigger found");
386 for (l = trigger->stages; l; l = l->next) {
388 num_trigger_stages++;
389 for (m = stage->matches; m; m = m->next) {
391 if (!match->channel->enabled)
392 /* Ignore disabled channels with a trigger. */
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;
423 cfg->trig_glb |= num_trigger_stages;
426 static int fpga_configure(const struct sr_dev_inst *sdi)
428 struct dev_context *devc;
429 struct sr_usb_dev_inst *usb;
431 struct fpga_config cfg;
434 int transferred, len, ret;
436 sr_dbg("Configuring FPGA.");
441 WL32(&cfg.sync, DS_CFG_START);
442 WL16(&cfg.mode_header, DS_CFG_MODE);
443 WL16(&cfg.divider_header, DS_CFG_DIVIDER);
444 WL16(&cfg.count_header, DS_CFG_COUNT);
445 WL16(&cfg.trig_pos_header, DS_CFG_TRIG_POS);
446 WL16(&cfg.trig_glb_header, DS_CFG_TRIG_GLB);
447 WL16(&cfg.ch_en_header, DS_CFG_CH_EN);
448 WL16(&cfg.trig_header, DS_CFG_TRIG);
449 WL32(&cfg.end_sync, DS_CFG_END);
451 /* Pass in the length of a fixed-size struct. Really. */
452 len = sizeof(struct fpga_config) / 2;
454 c[1] = (len >> 8) & 0xff;
455 c[2] = (len >> 16) & 0xff;
457 ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
458 LIBUSB_ENDPOINT_OUT, DS_CMD_SETTING, 0x0000, 0x0000,
459 c, sizeof(c), USB_TIMEOUT);
461 sr_err("Failed to send FPGA configure command: %s.",
462 libusb_error_name(ret));
468 if (devc->mode == DS_OP_INTERNAL_TEST)
469 v16 = DS_MODE_INT_TEST;
470 else if (devc->mode == DS_OP_EXTERNAL_TEST)
471 v16 = DS_MODE_EXT_TEST;
472 else if (devc->mode == DS_OP_LOOPBACK_TEST)
473 v16 = DS_MODE_LPB_TEST;
475 if (devc->cur_samplerate == DS_MAX_LOGIC_SAMPLERATE * 2)
476 v16 |= DS_MODE_HALF_MODE;
477 else if (devc->cur_samplerate == DS_MAX_LOGIC_SAMPLERATE * 4)
478 v16 |= DS_MODE_QUAR_MODE;
480 if (devc->continuous_mode)
481 v16 |= DS_MODE_STREAM_MODE;
482 if (devc->external_clock) {
483 v16 |= DS_MODE_CLK_TYPE;
484 if (devc->clock_edge == DS_EDGE_FALLING)
485 v16 |= DS_MODE_CLK_EDGE;
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.
493 v16 |= DS_MODE_RLE_MODE;
496 WL16(&cfg.mode, v16);
497 v32 = ceil(DS_MAX_LOGIC_SAMPLERATE * 1.0 / devc->cur_samplerate);
498 WL32(&cfg.divider, v32);
500 /* Number of 16-sample units. */
501 WL32(&cfg.count, devc->limit_samples / 16);
503 set_trigger(sdi, &cfg);
505 len = sizeof(struct fpga_config);
506 ret = libusb_bulk_transfer(usb->devhdl, 2 | LIBUSB_ENDPOINT_OUT,
507 (unsigned char *)&cfg, len, &transferred, USB_TIMEOUT);
508 if (ret < 0 || transferred != len) {
509 sr_err("Failed to send FPGA configuration: %s.", libusb_error_name(ret));
516 SR_PRIV int dslogic_set_voltage_threshold(const struct sr_dev_inst *sdi, double threshold)
519 struct dev_context *const devc = sdi->priv;
520 const struct sr_usb_dev_inst *const usb = sdi->conn;
521 const uint8_t value = (threshold / 5.0) * 255;
522 const uint16_t cmd = value | (DS_ADDR_VTH << 8);
524 /* Send the control command. */
525 ret = libusb_control_transfer(usb->devhdl,
526 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT,
527 DS_CMD_WR_REG, 0x0000, 0x0000,
528 (unsigned char *)&cmd, sizeof(cmd), 3000);
530 sr_err("Unable to set voltage-threshold register: %s.",
531 libusb_error_name(ret));
535 devc->cur_threshold = threshold;
540 SR_PRIV int dslogic_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
542 libusb_device **devlist;
543 struct sr_usb_dev_inst *usb;
544 struct libusb_device_descriptor des;
545 struct dev_context *devc;
546 struct drv_context *drvc;
547 struct version_info vi;
548 int ret = SR_ERR, i, device_count;
550 char connection_id[64];
556 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
557 if (device_count < 0) {
558 sr_err("Failed to get device list: %s.",
559 libusb_error_name(device_count));
563 for (i = 0; i < device_count; i++) {
564 libusb_get_device_descriptor(devlist[i], &des);
566 if (des.idVendor != devc->profile->vid
567 || des.idProduct != devc->profile->pid)
570 if ((sdi->status == SR_ST_INITIALIZING) ||
571 (sdi->status == SR_ST_INACTIVE)) {
572 /* Check device by its physical USB bus/port address. */
573 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
574 if (strcmp(sdi->connection_id, connection_id))
575 /* This is not the one. */
579 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
580 if (usb->address == 0xff)
582 * First time we touch this device after FW
583 * upload, so we don't know the address yet.
585 usb->address = libusb_get_device_address(devlist[i]);
587 sr_err("Failed to open device: %s.",
588 libusb_error_name(ret));
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));
604 ret = command_get_fw_version(usb->devhdl, &vi);
606 sr_err("Failed to get firmware version.");
610 ret = command_get_revid_version(sdi, &revid);
612 sr_err("Failed to get REVID.");
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.
621 if (vi.major != DSLOGIC_REQUIRED_VERSION_MAJOR) {
622 sr_err("Expected firmware version %d.x, "
623 "got %d.%d.", DSLOGIC_REQUIRED_VERSION_MAJOR,
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);
634 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
635 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
642 libusb_free_device_list(devlist, 1);
647 SR_PRIV struct dev_context *dslogic_dev_new(void)
649 struct dev_context *devc;
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;
663 static void abort_acquisition(struct dev_context *devc)
667 devc->acq_aborted = TRUE;
669 for (i = devc->num_transfers - 1; i >= 0; i--) {
670 if (devc->transfers[i])
671 libusb_cancel_transfer(devc->transfers[i]);
675 static void finish_acquisition(struct sr_dev_inst *sdi)
677 struct dev_context *devc;
681 std_session_send_df_end(sdi);
683 usb_source_remove(sdi->session, devc->ctx);
685 devc->num_transfers = 0;
686 g_free(devc->transfers);
687 g_free(devc->deinterleave_buffer);
690 static void free_transfer(struct libusb_transfer *transfer)
692 struct sr_dev_inst *sdi;
693 struct dev_context *devc;
696 sdi = transfer->user_data;
699 g_free(transfer->buffer);
700 transfer->buffer = NULL;
701 libusb_free_transfer(transfer);
703 for (i = 0; i < devc->num_transfers; i++) {
704 if (devc->transfers[i] == transfer) {
705 devc->transfers[i] = NULL;
710 devc->submitted_transfers--;
711 if (devc->submitted_transfers == 0)
712 finish_acquisition(sdi);
715 static void resubmit_transfer(struct libusb_transfer *transfer)
719 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
722 sr_err("%s: %s", __func__, libusb_error_name(ret));
723 free_transfer(transfer);
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)
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;
738 for (size_t channel = 0; channel != channel_count;
740 if ((channel_mask & (1 << channel)) &&
741 (*word_ptr++ & (1ULL << bit)))
742 sample |= 1 << channel;
749 static void send_data(struct sr_dev_inst *sdi,
750 uint16_t *data, size_t sample_count)
752 const struct sr_datafeed_logic logic = {
753 .length = sample_count * sizeof(uint16_t),
754 .unitsize = sizeof(uint16_t),
758 const struct sr_datafeed_packet packet = {
763 sr_session_send(sdi, &packet);
766 static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
768 struct sr_dev_inst *const sdi = transfer->user_data;
769 struct dev_context *const devc = sdi->priv;
770 const size_t channel_count = enabled_channel_count(sdi);
771 const uint16_t channel_mask = enabled_channel_mask(sdi);
772 const unsigned int cur_sample_count = DSLOGIC_ATOMIC_SAMPLES *
773 transfer->actual_length /
774 (DSLOGIC_ATOMIC_BYTES * channel_count);
776 gboolean packet_has_error = FALSE;
777 struct sr_datafeed_packet packet;
778 unsigned int num_samples;
782 * If acquisition has already ended, just free any queued up
783 * transfer that come in.
785 if (devc->acq_aborted) {
786 free_transfer(transfer);
790 sr_dbg("receive_transfer(): status %s received %d bytes.",
791 libusb_error_name(transfer->status), transfer->actual_length);
793 /* Save incoming transfer before reusing the transfer struct. */
795 switch (transfer->status) {
796 case LIBUSB_TRANSFER_NO_DEVICE:
797 abort_acquisition(devc);
798 free_transfer(transfer);
800 case LIBUSB_TRANSFER_COMPLETED:
801 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
804 packet_has_error = TRUE;
808 if (transfer->actual_length == 0 || packet_has_error) {
809 devc->empty_transfer_count++;
810 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
812 * The FX2 gave up. End the acquisition, the frontend
813 * will work out that the samplecount is short.
815 abort_acquisition(devc);
816 free_transfer(transfer);
818 resubmit_transfer(transfer);
822 devc->empty_transfer_count = 0;
825 if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) {
826 if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples)
827 num_samples = devc->limit_samples - devc->sent_samples;
829 num_samples = cur_sample_count;
832 * The DSLogic emits sample data as sequences of 64-bit sample words
833 * in a round-robin i.e. 64-bits from channel 0, 64-bits from channel 1
834 * etc. for each of the enabled channels, then looping back to the
837 * Because sigrok's internal representation is bit-interleaved channels
838 * we must recast the data.
840 * Hopefully in future it will be possible to pass the data on as-is.
842 if (transfer->actual_length % (DSLOGIC_ATOMIC_BYTES * channel_count) != 0)
843 sr_err("Invalid transfer length!");
844 deinterleave_buffer(transfer->buffer, transfer->actual_length,
845 devc->deinterleave_buffer, channel_count, channel_mask);
847 /* Send the incoming transfer to the session bus. */
848 if (devc->trigger_pos > devc->sent_samples
849 && devc->trigger_pos <= devc->sent_samples + num_samples) {
850 /* DSLogic trigger in this block. Send trigger position. */
851 trigger_offset = devc->trigger_pos - devc->sent_samples;
852 /* Pre-trigger samples. */
853 send_data(sdi, devc->deinterleave_buffer, trigger_offset);
854 devc->sent_samples += trigger_offset;
855 /* Trigger position. */
856 devc->trigger_pos = 0;
857 packet.type = SR_DF_TRIGGER;
858 packet.payload = NULL;
859 sr_session_send(sdi, &packet);
860 /* Post trigger samples. */
861 num_samples -= trigger_offset;
862 send_data(sdi, devc->deinterleave_buffer
863 + trigger_offset, num_samples);
864 devc->sent_samples += num_samples;
866 send_data(sdi, devc->deinterleave_buffer, num_samples);
867 devc->sent_samples += num_samples;
871 if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
872 abort_acquisition(devc);
873 free_transfer(transfer);
875 resubmit_transfer(transfer);
878 static int receive_data(int fd, int revents, void *cb_data)
881 struct drv_context *drvc;
886 drvc = (struct drv_context *)cb_data;
888 tv.tv_sec = tv.tv_usec = 0;
889 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
894 static size_t to_bytes_per_ms(const struct sr_dev_inst *sdi)
896 const struct dev_context *const devc = sdi->priv;
897 const size_t ch_count = enabled_channel_count(sdi);
899 if (devc->continuous_mode)
900 return (devc->cur_samplerate * ch_count) / (1000 * 8);
903 /* If we're in buffered mode, the transfer rate is not so important,
904 * but we expect to get at least 10% of the high-speed USB bandwidth.
906 return 35000000 / (1000 * 10);
909 static size_t get_buffer_size(const struct sr_dev_inst *sdi)
912 * The buffer should be large enough to hold 10ms of data and
913 * a multiple of the size of a data atom.
915 const size_t block_size = enabled_channel_count(sdi) * 512;
916 const size_t s = 10 * to_bytes_per_ms(sdi);
917 return ((s + block_size - 1) / block_size) * block_size;
920 static unsigned int get_number_of_transfers(const struct sr_dev_inst *sdi)
922 /* Total buffer size should be able to hold about 100ms of data. */
923 const unsigned int s = get_buffer_size(sdi);
924 const unsigned int n = (100 * to_bytes_per_ms(sdi) + s - 1) / s;
925 return (n > NUM_SIMUL_TRANSFERS) ? NUM_SIMUL_TRANSFERS : n;
928 static unsigned int get_timeout(const struct sr_dev_inst *sdi)
930 const size_t total_size = get_buffer_size(sdi) *
931 get_number_of_transfers(sdi);
932 const unsigned int timeout = total_size / to_bytes_per_ms(sdi);
933 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
936 static int start_transfers(const struct sr_dev_inst *sdi)
938 const size_t channel_count = enabled_channel_count(sdi);
939 const size_t size = get_buffer_size(sdi);
940 const unsigned int num_transfers = get_number_of_transfers(sdi);
941 const unsigned int timeout = get_timeout(sdi);
943 struct dev_context *devc;
944 struct sr_usb_dev_inst *usb;
945 struct libusb_transfer *transfer;
953 devc->sent_samples = 0;
954 devc->acq_aborted = FALSE;
955 devc->empty_transfer_count = 0;
956 devc->submitted_transfers = 0;
958 g_free(devc->transfers);
959 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
960 if (!devc->transfers) {
961 sr_err("USB transfers malloc failed.");
962 return SR_ERR_MALLOC;
965 devc->deinterleave_buffer = g_try_malloc(DSLOGIC_ATOMIC_SAMPLES *
966 (size / (channel_count * DSLOGIC_ATOMIC_BYTES)) * sizeof(uint16_t));
967 if (!devc->deinterleave_buffer) {
968 sr_err("Deinterleave buffer malloc failed.");
969 g_free(devc->deinterleave_buffer);
970 return SR_ERR_MALLOC;
973 devc->num_transfers = num_transfers;
974 for (i = 0; i < num_transfers; i++) {
975 if (!(buf = g_try_malloc(size))) {
976 sr_err("USB transfer buffer malloc failed.");
977 return SR_ERR_MALLOC;
979 transfer = libusb_alloc_transfer(0);
980 libusb_fill_bulk_transfer(transfer, usb->devhdl,
981 6 | LIBUSB_ENDPOINT_IN, buf, size,
982 receive_transfer, (void *)sdi, timeout);
983 sr_info("submitting transfer: %d", i);
984 if ((ret = libusb_submit_transfer(transfer)) != 0) {
985 sr_err("Failed to submit transfer: %s.",
986 libusb_error_name(ret));
987 libusb_free_transfer(transfer);
989 abort_acquisition(devc);
992 devc->transfers[i] = transfer;
993 devc->submitted_transfers++;
996 std_session_send_df_header(sdi);
1001 static void LIBUSB_CALL trigger_receive(struct libusb_transfer *transfer)
1003 const struct sr_dev_inst *sdi;
1004 struct dslogic_trigger_pos *tpos;
1005 struct dev_context *devc;
1007 sdi = transfer->user_data;
1009 if (transfer->status == LIBUSB_TRANSFER_CANCELLED) {
1010 sr_dbg("Trigger transfer canceled.");
1011 /* Terminate session. */
1012 std_session_send_df_end(sdi);
1013 usb_source_remove(sdi->session, devc->ctx);
1014 devc->num_transfers = 0;
1015 g_free(devc->transfers);
1016 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED
1017 && transfer->actual_length == sizeof(struct dslogic_trigger_pos)) {
1018 tpos = (struct dslogic_trigger_pos *)transfer->buffer;
1019 sr_info("tpos real_pos %d ram_saddr %d cnt %d", tpos->real_pos,
1020 tpos->ram_saddr, tpos->remain_cnt);
1021 devc->trigger_pos = tpos->real_pos;
1023 start_transfers(sdi);
1025 libusb_free_transfer(transfer);
1028 SR_PRIV int dslogic_acquisition_start(const struct sr_dev_inst *sdi)
1030 const unsigned int timeout = get_timeout(sdi);
1032 struct sr_dev_driver *di;
1033 struct drv_context *drvc;
1034 struct dev_context *devc;
1035 struct sr_usb_dev_inst *usb;
1036 struct dslogic_trigger_pos *tpos;
1037 struct libusb_transfer *transfer;
1045 devc->ctx = drvc->sr_ctx;
1046 devc->sent_samples = 0;
1047 devc->empty_transfer_count = 0;
1048 devc->acq_aborted = FALSE;
1050 usb_source_add(sdi->session, devc->ctx, timeout, receive_data, drvc);
1052 if ((ret = command_stop_acquisition(sdi)) != SR_OK)
1055 if ((ret = fpga_configure(sdi)) != SR_OK)
1058 if ((ret = command_start_acquisition(sdi)) != SR_OK)
1061 sr_dbg("Getting trigger.");
1062 tpos = g_malloc(sizeof(struct dslogic_trigger_pos));
1063 transfer = libusb_alloc_transfer(0);
1064 libusb_fill_bulk_transfer(transfer, usb->devhdl, 6 | LIBUSB_ENDPOINT_IN,
1065 (unsigned char *)tpos, sizeof(struct dslogic_trigger_pos),
1066 trigger_receive, (void *)sdi, 0);
1067 if ((ret = libusb_submit_transfer(transfer)) < 0) {
1068 sr_err("Failed to request trigger: %s.", libusb_error_name(ret));
1069 libusb_free_transfer(transfer);
1074 devc->transfers = g_try_malloc0(sizeof(*devc->transfers));
1075 if (!devc->transfers) {
1076 sr_err("USB trigger_pos transfer malloc failed.");
1077 return SR_ERR_MALLOC;
1079 devc->num_transfers = 1;
1080 devc->submitted_transfers++;
1081 devc->transfers[0] = transfer;
1086 SR_PRIV int dslogic_acquisition_stop(struct sr_dev_inst *sdi)
1088 command_stop_acquisition(sdi);
1089 abort_acquisition(sdi->priv);