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/>.
23 /* Protocol commands */
24 #define CMD_GET_FW_VERSION 0xb0
25 #define CMD_START 0xb1
26 #define CMD_GET_REVID_VERSION 0xb2
28 #define CMD_START_FLAGS_WIDE_POS 5
29 #define CMD_START_FLAGS_CLK_SRC_POS 6
31 #define CMD_START_FLAGS_SAMPLE_8BIT (0 << CMD_START_FLAGS_WIDE_POS)
32 #define CMD_START_FLAGS_SAMPLE_16BIT (1 << CMD_START_FLAGS_WIDE_POS)
34 #define CMD_START_FLAGS_CLK_30MHZ (0 << CMD_START_FLAGS_CLK_SRC_POS)
35 #define CMD_START_FLAGS_CLK_48MHZ (1 << CMD_START_FLAGS_CLK_SRC_POS)
44 struct cmd_start_acquisition {
46 uint8_t sample_delay_h;
47 uint8_t sample_delay_l;
52 static int command_get_fw_version(libusb_device_handle *devhdl,
53 struct version_info *vi)
57 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
58 LIBUSB_ENDPOINT_IN, CMD_GET_FW_VERSION, 0x0000, 0x0000,
59 (unsigned char *)vi, sizeof(struct version_info), 100);
62 sr_err("Unable to get version info: %s.",
63 libusb_error_name(ret));
70 static int command_get_revid_version(libusb_device_handle *devhdl,
75 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
76 LIBUSB_ENDPOINT_IN, CMD_GET_REVID_VERSION, 0x0000, 0x0000,
80 sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
87 SR_PRIV int fx2lafw_command_start_acquisition(libusb_device_handle *devhdl,
88 uint64_t samplerate, gboolean samplewide)
90 struct cmd_start_acquisition cmd;
93 /* Compute the sample rate. */
94 if (samplewide && samplerate > MAX_16BIT_SAMPLE_RATE) {
95 sr_err("Unable to sample at %" PRIu64 "Hz "
96 "when collecting 16-bit samples.", samplerate);
100 if ((SR_MHZ(48) % samplerate) == 0) {
101 cmd.flags = CMD_START_FLAGS_CLK_48MHZ;
102 delay = SR_MHZ(48) / samplerate - 1;
103 if (delay > MAX_SAMPLE_DELAY)
107 if (delay == 0 && (SR_MHZ(30) % samplerate) == 0) {
108 cmd.flags = CMD_START_FLAGS_CLK_30MHZ;
109 delay = SR_MHZ(30) / samplerate - 1;
112 sr_info("GPIF delay = %d, clocksource = %sMHz.", delay,
113 (cmd.flags & CMD_START_FLAGS_CLK_48MHZ) ? "48" : "30");
115 if (delay <= 0 || delay > MAX_SAMPLE_DELAY) {
116 sr_err("Unable to sample at %" PRIu64 "Hz.", samplerate);
120 cmd.sample_delay_h = (delay >> 8) & 0xff;
121 cmd.sample_delay_l = delay & 0xff;
123 /* Select the sampling width. */
124 cmd.flags |= samplewide ? CMD_START_FLAGS_SAMPLE_16BIT :
125 CMD_START_FLAGS_SAMPLE_8BIT;
127 /* Send the control message. */
128 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
129 LIBUSB_ENDPOINT_OUT, CMD_START, 0x0000, 0x0000,
130 (unsigned char *)&cmd, sizeof(cmd), 100);
132 sr_err("Unable to send start command: %s.",
133 libusb_error_name(ret));
141 * Check the USB configuration to determine if this is an fx2lafw device.
143 * @return TRUE if the device's configuration profile match fx2lafw
144 * configuration, FALSE otherwise.
146 SR_PRIV gboolean fx2lafw_check_conf_profile(libusb_device *dev)
148 struct libusb_device_descriptor des;
149 struct libusb_device_handle *hdl;
151 unsigned char strdesc[64];
156 /* Assume the FW has not been loaded, unless proven wrong. */
157 if (libusb_get_device_descriptor(dev, &des) != 0)
160 if (libusb_open(dev, &hdl) != 0)
163 if (libusb_get_string_descriptor_ascii(hdl,
164 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
166 if (strncmp((const char *)strdesc, "sigrok", 6))
169 if (libusb_get_string_descriptor_ascii(hdl,
170 des.iProduct, strdesc, sizeof(strdesc)) < 0)
172 if (strncmp((const char *)strdesc, "fx2lafw", 7))
175 /* If we made it here, it must be an fx2lafw. */
184 SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
186 libusb_device **devlist;
187 struct sr_usb_dev_inst *usb;
188 struct libusb_device_descriptor des;
189 struct dev_context *devc;
190 struct drv_context *drvc;
191 struct version_info vi;
192 int ret, skip, i, device_count;
199 if (sdi->status == SR_ST_ACTIVE)
200 /* Device is already in use. */
204 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
205 if (device_count < 0) {
206 sr_err("Failed to get device list: %s.",
207 libusb_error_name(device_count));
211 for (i = 0; i < device_count; i++) {
212 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
213 sr_err("Failed to get device descriptor: %s.",
214 libusb_error_name(ret));
218 if (des.idVendor != devc->profile->vid
219 || des.idProduct != devc->profile->pid)
222 if (sdi->status == SR_ST_INITIALIZING) {
223 if (skip != sdi->index) {
224 /* Skip devices of this type that aren't the one we want. */
228 } else if (sdi->status == SR_ST_INACTIVE) {
230 * This device is fully enumerated, so we need to find
231 * this device by vendor, product, bus and address.
233 if (libusb_get_bus_number(devlist[i]) != usb->bus
234 || libusb_get_device_address(devlist[i]) != usb->address)
235 /* This is not the one. */
239 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
240 if (usb->address == 0xff)
242 * First time we touch this device after FW
243 * upload, so we don't know the address yet.
245 usb->address = libusb_get_device_address(devlist[i]);
247 sr_err("Failed to open device: %s.",
248 libusb_error_name(ret));
252 ret = command_get_fw_version(usb->devhdl, &vi);
254 sr_err("Failed to get firmware version.");
258 ret = command_get_revid_version(usb->devhdl, &revid);
260 sr_err("Failed to get REVID.");
265 * Changes in major version mean incompatible/API changes, so
266 * bail out if we encounter an incompatible version.
267 * Different minor versions are OK, they should be compatible.
269 if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) {
270 sr_err("Expected firmware version %d.x, "
271 "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR,
276 sdi->status = SR_ST_ACTIVE;
277 sr_info("Opened device %d on %d.%d, "
278 "interface %d, firmware %d.%d.",
279 sdi->index, usb->bus, usb->address,
280 USB_INTERFACE, vi.major, vi.minor);
282 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
283 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
287 libusb_free_device_list(devlist, 1);
289 if (sdi->status != SR_ST_ACTIVE)
295 SR_PRIV int fx2lafw_configure_probes(const struct sr_dev_inst *sdi)
297 struct dev_context *devc;
298 struct sr_probe *probe;
300 int probe_bit, stage, i;
304 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
305 devc->trigger_mask[i] = 0;
306 devc->trigger_value[i] = 0;
310 for (l = sdi->probes; l; l = l->next) {
311 probe = (struct sr_probe *)l->data;
312 if (probe->enabled == FALSE)
315 if (probe->index > 7)
316 devc->sample_wide = TRUE;
318 probe_bit = 1 << (probe->index);
319 if (!(probe->trigger))
323 for (tc = probe->trigger; *tc; tc++) {
324 devc->trigger_mask[stage] |= probe_bit;
326 devc->trigger_value[stage] |= probe_bit;
328 if (stage > NUM_TRIGGER_STAGES)
335 * We didn't configure any triggers, make sure acquisition
336 * doesn't wait for any.
338 devc->trigger_stage = TRIGGER_FIRED;
340 devc->trigger_stage = 0;
345 SR_PRIV struct dev_context *fx2lafw_dev_new(void)
347 struct dev_context *devc;
349 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
350 sr_err("Device context malloc failed.");
354 devc->profile = NULL;
355 devc->fw_updated = 0;
356 devc->cur_samplerate = 0;
357 devc->limit_samples = 0;
358 devc->sample_wide = 0;
363 SR_PRIV void fx2lafw_abort_acquisition(struct dev_context *devc)
367 devc->num_samples = -1;
369 for (i = devc->num_transfers - 1; i >= 0; i--) {
370 if (devc->transfers[i])
371 libusb_cancel_transfer(devc->transfers[i]);
375 static void finish_acquisition(struct dev_context *devc)
377 struct sr_datafeed_packet packet;
380 /* Terminate session. */
381 packet.type = SR_DF_END;
382 sr_session_send(devc->cb_data, &packet);
384 /* Remove fds from polling. */
385 for (i = 0; devc->usbfd[i] != -1; i++)
386 sr_source_remove(devc->usbfd[i]);
389 devc->num_transfers = 0;
390 g_free(devc->transfers);
393 static void free_transfer(struct libusb_transfer *transfer)
395 struct dev_context *devc;
398 devc = transfer->user_data;
400 g_free(transfer->buffer);
401 transfer->buffer = NULL;
402 libusb_free_transfer(transfer);
404 for (i = 0; i < devc->num_transfers; i++) {
405 if (devc->transfers[i] == transfer) {
406 devc->transfers[i] = NULL;
411 devc->submitted_transfers--;
412 if (devc->submitted_transfers == 0)
413 finish_acquisition(devc);
416 static void resubmit_transfer(struct libusb_transfer *transfer)
420 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
423 free_transfer(transfer);
424 /* TODO: Stop session? */
426 sr_err("%s: %s", __func__, libusb_error_name(ret));
429 SR_PRIV void fx2lafw_receive_transfer(struct libusb_transfer *transfer)
431 gboolean packet_has_error = FALSE;
432 struct sr_datafeed_packet packet;
433 struct sr_datafeed_logic logic;
434 struct dev_context *devc;
435 int trigger_offset, i, sample_width, cur_sample_count;
436 int trigger_offset_bytes;
439 devc = transfer->user_data;
442 * If acquisition has already ended, just free any queued up
443 * transfer that come in.
445 if (devc->num_samples == -1) {
446 free_transfer(transfer);
450 sr_info("receive_transfer(): status %d received %d bytes.",
451 transfer->status, transfer->actual_length);
453 /* Save incoming transfer before reusing the transfer struct. */
454 cur_buf = transfer->buffer;
455 sample_width = devc->sample_wide ? 2 : 1;
456 cur_sample_count = transfer->actual_length / sample_width;
458 switch (transfer->status) {
459 case LIBUSB_TRANSFER_NO_DEVICE:
460 fx2lafw_abort_acquisition(devc);
461 free_transfer(transfer);
463 case LIBUSB_TRANSFER_COMPLETED:
464 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
467 packet_has_error = TRUE;
471 if (transfer->actual_length == 0 || packet_has_error) {
472 devc->empty_transfer_count++;
473 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
475 * The FX2 gave up. End the acquisition, the frontend
476 * will work out that the samplecount is short.
478 fx2lafw_abort_acquisition(devc);
479 free_transfer(transfer);
481 resubmit_transfer(transfer);
485 devc->empty_transfer_count = 0;
489 if (devc->trigger_stage >= 0) {
490 for (i = 0; i < cur_sample_count; i++) {
492 const uint16_t cur_sample = devc->sample_wide ?
493 *((const uint16_t*)cur_buf + i) :
494 *((const uint8_t*)cur_buf + i);
496 if ((cur_sample & devc->trigger_mask[devc->trigger_stage]) ==
497 devc->trigger_value[devc->trigger_stage]) {
498 /* Match on this trigger stage. */
499 devc->trigger_buffer[devc->trigger_stage] = cur_sample;
500 devc->trigger_stage++;
502 if (devc->trigger_stage == NUM_TRIGGER_STAGES ||
503 devc->trigger_mask[devc->trigger_stage] == 0) {
504 /* Match on all trigger stages, we're done. */
505 trigger_offset = i + 1;
508 * TODO: Send pre-trigger buffer to session bus.
509 * Tell the frontend we hit the trigger here.
511 packet.type = SR_DF_TRIGGER;
512 packet.payload = NULL;
513 sr_session_send(devc->cb_data, &packet);
516 * Send the samples that triggered it,
517 * since we're skipping past them.
519 packet.type = SR_DF_LOGIC;
520 packet.payload = &logic;
521 logic.unitsize = sizeof(*devc->trigger_buffer);
522 logic.length = devc->trigger_stage * logic.unitsize;
523 logic.data = devc->trigger_buffer;
524 sr_session_send(devc->cb_data, &packet);
526 devc->trigger_stage = TRIGGER_FIRED;
529 } else if (devc->trigger_stage > 0) {
531 * We had a match before, but not in the next sample. However, we may
532 * have a match on this stage in the next bit -- trigger on 0001 will
533 * fail on seeing 00001, so we need to go back to stage 0 -- but at
534 * the next sample from the one that matched originally, which the
535 * counter increment at the end of the loop takes care of.
537 i -= devc->trigger_stage;
539 i = -1; /* Oops, went back past this buffer. */
540 /* Reset trigger stage. */
541 devc->trigger_stage = 0;
546 if (devc->trigger_stage == TRIGGER_FIRED) {
547 /* Send the incoming transfer to the session bus. */
548 trigger_offset_bytes = trigger_offset * sample_width;
549 packet.type = SR_DF_LOGIC;
550 packet.payload = &logic;
551 logic.length = transfer->actual_length - trigger_offset_bytes;
552 logic.unitsize = sample_width;
553 logic.data = cur_buf + trigger_offset_bytes;
554 sr_session_send(devc->cb_data, &packet);
556 devc->num_samples += cur_sample_count;
557 if (devc->limit_samples &&
558 (unsigned int)devc->num_samples > devc->limit_samples) {
559 fx2lafw_abort_acquisition(devc);
560 free_transfer(transfer);
565 * TODO: Buffer pre-trigger data in capture
566 * ratio-sized buffer.
570 resubmit_transfer(transfer);
573 static unsigned int to_bytes_per_ms(unsigned int samplerate)
575 return samplerate / 1000;
578 SR_PRIV size_t fx2lafw_get_buffer_size(struct dev_context *devc)
583 * The buffer should be large enough to hold 10ms of data and
586 s = 10 * to_bytes_per_ms(devc->cur_samplerate);
587 return (s + 511) & ~511;
590 SR_PRIV unsigned int fx2lafw_get_number_of_transfers(struct dev_context *devc)
594 /* Total buffer size should be able to hold about 500ms of data. */
595 n = (500 * to_bytes_per_ms(devc->cur_samplerate) /
596 fx2lafw_get_buffer_size(devc));
598 if (n > NUM_SIMUL_TRANSFERS)
599 return NUM_SIMUL_TRANSFERS;
604 SR_PRIV unsigned int fx2lafw_get_timeout(struct dev_context *devc)
607 unsigned int timeout;
609 total_size = fx2lafw_get_buffer_size(devc) *
610 fx2lafw_get_number_of_transfers(devc);
611 timeout = total_size / to_bytes_per_ms(devc->cur_samplerate);
612 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */