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(struct sr_dev_inst *sdi, uint8_t *revid)
72 struct sr_usb_dev_inst *usb = sdi->conn;
73 libusb_device_handle *devhdl = usb->devhdl;
76 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
77 LIBUSB_ENDPOINT_IN, CMD_GET_REVID_VERSION, 0x0000, 0x0000,
81 sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
88 SR_PRIV int fx2lafw_command_start_acquisition(const struct sr_dev_inst *sdi)
90 struct dev_context *devc = sdi->priv;
91 struct sr_usb_dev_inst *usb = sdi->conn;
92 libusb_device_handle *devhdl = usb->devhdl;
93 uint64_t samplerate = devc->cur_samplerate;
94 gboolean samplewide = devc->sample_wide;
95 struct cmd_start_acquisition cmd = { 0 };
98 /* Compute the sample rate. */
99 if (samplewide && samplerate > MAX_16BIT_SAMPLE_RATE) {
100 sr_err("Unable to sample at %" PRIu64 "Hz "
101 "when collecting 16-bit samples.", samplerate);
105 if ((SR_MHZ(48) % samplerate) == 0) {
106 cmd.flags = CMD_START_FLAGS_CLK_48MHZ;
107 delay = SR_MHZ(48) / samplerate - 1;
108 if (delay > MAX_SAMPLE_DELAY)
112 if (delay == 0 && (SR_MHZ(30) % samplerate) == 0) {
113 cmd.flags = CMD_START_FLAGS_CLK_30MHZ;
114 delay = SR_MHZ(30) / samplerate - 1;
117 sr_info("GPIF delay = %d, clocksource = %sMHz.", delay,
118 (cmd.flags & CMD_START_FLAGS_CLK_48MHZ) ? "48" : "30");
120 if (delay <= 0 || delay > MAX_SAMPLE_DELAY) {
121 sr_err("Unable to sample at %" PRIu64 "Hz.", samplerate);
125 cmd.sample_delay_h = (delay >> 8) & 0xff;
126 cmd.sample_delay_l = delay & 0xff;
128 /* Select the sampling width. */
129 cmd.flags |= samplewide ? CMD_START_FLAGS_SAMPLE_16BIT :
130 CMD_START_FLAGS_SAMPLE_8BIT;
132 /* Send the control message. */
133 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
134 LIBUSB_ENDPOINT_OUT, CMD_START, 0x0000, 0x0000,
135 (unsigned char *)&cmd, sizeof(cmd), 100);
137 sr_err("Unable to send start command: %s.",
138 libusb_error_name(ret));
146 * Check the USB configuration to determine if this is an fx2lafw device.
148 * @return TRUE if the device's configuration profile match fx2lafw
149 * configuration, FALSE otherwise.
151 SR_PRIV gboolean fx2lafw_check_conf_profile(libusb_device *dev)
153 struct libusb_device_descriptor des;
154 struct libusb_device_handle *hdl;
156 unsigned char strdesc[64];
161 /* Assume the FW has not been loaded, unless proven wrong. */
162 if (libusb_get_device_descriptor(dev, &des) != 0)
165 if (libusb_open(dev, &hdl) != 0)
168 if (libusb_get_string_descriptor_ascii(hdl,
169 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
171 if (strncmp((const char *)strdesc, "sigrok", 6))
174 if (libusb_get_string_descriptor_ascii(hdl,
175 des.iProduct, strdesc, sizeof(strdesc)) < 0)
177 if (strncmp((const char *)strdesc, "fx2lafw", 7))
180 /* If we made it here, it must be an fx2lafw. */
189 SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
191 libusb_device **devlist;
192 struct sr_usb_dev_inst *usb;
193 struct libusb_device_descriptor des;
194 struct dev_context *devc;
195 struct drv_context *drvc;
196 struct version_info vi;
197 int ret, skip, i, device_count;
204 if (sdi->status == SR_ST_ACTIVE)
205 /* Device is already in use. */
209 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
210 if (device_count < 0) {
211 sr_err("Failed to get device list: %s.",
212 libusb_error_name(device_count));
216 for (i = 0; i < device_count; i++) {
217 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
218 sr_err("Failed to get device descriptor: %s.",
219 libusb_error_name(ret));
223 if (des.idVendor != devc->profile->vid
224 || des.idProduct != devc->profile->pid)
227 if (sdi->status == SR_ST_INITIALIZING) {
228 if (skip != sdi->index) {
229 /* Skip devices of this type that aren't the one we want. */
233 } else if (sdi->status == SR_ST_INACTIVE) {
235 * This device is fully enumerated, so we need to find
236 * this device by vendor, product, bus and address.
238 if (libusb_get_bus_number(devlist[i]) != usb->bus
239 || libusb_get_device_address(devlist[i]) != usb->address)
240 /* This is not the one. */
244 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
245 if (usb->address == 0xff)
247 * First time we touch this device after FW
248 * upload, so we don't know the address yet.
250 usb->address = libusb_get_device_address(devlist[i]);
252 sr_err("Failed to open device: %s.",
253 libusb_error_name(ret));
257 ret = command_get_fw_version(usb->devhdl, &vi);
259 sr_err("Failed to get firmware version.");
263 ret = command_get_revid_version(sdi, &revid);
265 sr_err("Failed to get REVID.");
270 * Changes in major version mean incompatible/API changes, so
271 * bail out if we encounter an incompatible version.
272 * Different minor versions are OK, they should be compatible.
274 if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) {
275 sr_err("Expected firmware version %d.x, "
276 "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR,
281 sdi->status = SR_ST_ACTIVE;
282 sr_info("Opened device %d on %d.%d, "
283 "interface %d, firmware %d.%d.",
284 sdi->index, usb->bus, usb->address,
285 USB_INTERFACE, vi.major, vi.minor);
287 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
288 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
292 libusb_free_device_list(devlist, 1);
294 if (sdi->status != SR_ST_ACTIVE)
300 SR_PRIV int fx2lafw_configure_channels(const struct sr_dev_inst *sdi)
302 struct dev_context *devc;
303 struct sr_channel *ch;
305 int channel_bit, stage, i;
309 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
310 devc->trigger_mask[i] = 0;
311 devc->trigger_value[i] = 0;
315 for (l = sdi->channels; l; l = l->next) {
316 ch = (struct sr_channel *)l->data;
317 if (ch->enabled == FALSE)
321 devc->sample_wide = TRUE;
323 channel_bit = 1 << (ch->index);
328 for (tc = ch->trigger; *tc; tc++) {
329 devc->trigger_mask[stage] |= channel_bit;
331 devc->trigger_value[stage] |= channel_bit;
333 if (stage > NUM_TRIGGER_STAGES)
340 * We didn't configure any triggers, make sure acquisition
341 * doesn't wait for any.
343 devc->trigger_fired = TRUE;
345 devc->trigger_fired = FALSE;
346 devc->trigger_stage = 0;
352 SR_PRIV struct dev_context *fx2lafw_dev_new(void)
354 struct dev_context *devc;
356 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
357 sr_err("Device context malloc failed.");
361 devc->profile = NULL;
362 devc->fw_updated = 0;
363 devc->cur_samplerate = 0;
364 devc->limit_samples = 0;
365 devc->sample_wide = FALSE;
370 SR_PRIV void fx2lafw_abort_acquisition(struct dev_context *devc)
374 devc->acq_aborted = TRUE;
376 for (i = devc->num_transfers - 1; i >= 0; i--) {
377 if (devc->transfers[i])
378 libusb_cancel_transfer(devc->transfers[i]);
382 static void finish_acquisition(struct dev_context *devc)
384 struct sr_datafeed_packet packet;
386 /* Terminate session. */
387 packet.type = SR_DF_END;
388 sr_session_send(devc->cb_data, &packet);
390 /* Remove fds from polling. */
391 usb_source_remove(devc->ctx);
393 devc->num_transfers = 0;
394 g_free(devc->transfers);
397 static void free_transfer(struct libusb_transfer *transfer)
399 struct dev_context *devc;
402 devc = transfer->user_data;
404 g_free(transfer->buffer);
405 transfer->buffer = NULL;
406 libusb_free_transfer(transfer);
408 for (i = 0; i < devc->num_transfers; i++) {
409 if (devc->transfers[i] == transfer) {
410 devc->transfers[i] = NULL;
415 devc->submitted_transfers--;
416 if (devc->submitted_transfers == 0)
417 finish_acquisition(devc);
420 static void resubmit_transfer(struct libusb_transfer *transfer)
424 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
427 free_transfer(transfer);
428 /* TODO: Stop session? */
430 sr_err("%s: %s", __func__, libusb_error_name(ret));
433 SR_PRIV void fx2lafw_receive_transfer(struct libusb_transfer *transfer)
435 gboolean packet_has_error = FALSE;
436 struct sr_datafeed_packet packet;
437 struct sr_datafeed_logic logic;
438 struct dev_context *devc;
439 unsigned int trigger_offset, num_samples;
440 int cur_sample_count, sample_width, i;
444 devc = transfer->user_data;
447 * If acquisition has already ended, just free any queued up
448 * transfer that come in.
450 if (devc->acq_aborted) {
451 free_transfer(transfer);
455 sr_info("receive_transfer(): status %d received %d bytes.",
456 transfer->status, transfer->actual_length);
458 /* Save incoming transfer before reusing the transfer struct. */
459 cur_buf = transfer->buffer;
460 sample_width = devc->sample_wide ? 2 : 1;
461 cur_sample_count = transfer->actual_length / sample_width;
463 switch (transfer->status) {
464 case LIBUSB_TRANSFER_NO_DEVICE:
465 fx2lafw_abort_acquisition(devc);
466 free_transfer(transfer);
468 case LIBUSB_TRANSFER_COMPLETED:
469 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
472 packet_has_error = TRUE;
476 if (transfer->actual_length == 0 || packet_has_error) {
477 devc->empty_transfer_count++;
478 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
480 * The FX2 gave up. End the acquisition, the frontend
481 * will work out that the samplecount is short.
483 fx2lafw_abort_acquisition(devc);
484 free_transfer(transfer);
486 resubmit_transfer(transfer);
490 devc->empty_transfer_count = 0;
494 if (!devc->trigger_fired) {
495 for (i = 0; i < cur_sample_count; i++) {
496 cur_sample = devc->sample_wide ?
497 *((uint16_t *)cur_buf + i) :
498 *((uint8_t *)cur_buf + i);
500 if ((cur_sample & devc->trigger_mask[devc->trigger_stage]) ==
501 devc->trigger_value[devc->trigger_stage]) {
502 /* Match on this trigger stage. */
503 devc->trigger_buffer[devc->trigger_stage] = cur_sample;
504 devc->trigger_stage++;
506 if (devc->trigger_stage == NUM_TRIGGER_STAGES ||
507 devc->trigger_mask[devc->trigger_stage] == 0) {
508 /* Match on all trigger stages, we're done. */
512 * TODO: Send pre-trigger buffer to session bus.
513 * Tell the frontend we hit the trigger here.
515 packet.type = SR_DF_TRIGGER;
516 packet.payload = NULL;
517 sr_session_send(devc->cb_data, &packet);
520 * Send the samples that triggered it,
521 * since we're skipping past them.
523 packet.type = SR_DF_LOGIC;
524 packet.payload = &logic;
525 num_samples = cur_sample_count - trigger_offset;
526 if (devc->limit_samples &&
527 num_samples > devc->limit_samples - devc->sent_samples)
528 num_samples = devc->limit_samples - devc->sent_samples;
529 logic.length = num_samples * sample_width;
530 logic.unitsize = sample_width;
531 logic.data = cur_buf + trigger_offset * sample_width;
532 sr_session_send(devc->cb_data, &packet);
533 devc->sent_samples += num_samples;
535 devc->trigger_fired = TRUE;
538 } else if (devc->trigger_stage > 0) {
540 * We had a match before, but not in the next sample. However, we may
541 * have a match on this stage in the next bit -- trigger on 0001 will
542 * fail on seeing 00001, so we need to go back to stage 0 -- but at
543 * the next sample from the one that matched originally, which the
544 * counter increment at the end of the loop takes care of.
546 i -= devc->trigger_stage;
548 i = -1; /* Oops, went back past this buffer. */
549 /* Reset trigger stage. */
550 devc->trigger_stage = 0;
553 } else if (devc->sent_samples < devc->limit_samples) {
554 /* Send the incoming transfer to the session bus. */
555 packet.type = SR_DF_LOGIC;
556 packet.payload = &logic;
557 if (devc->sent_samples + cur_sample_count > devc->limit_samples)
558 num_samples = devc->limit_samples - devc->sent_samples;
560 num_samples = cur_sample_count;
561 logic.length = num_samples * sample_width;
562 logic.unitsize = sample_width;
563 logic.data = cur_buf;
564 sr_session_send(devc->cb_data, &packet);
565 devc->sent_samples += cur_sample_count;
568 if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
569 fx2lafw_abort_acquisition(devc);
570 free_transfer(transfer);
574 resubmit_transfer(transfer);
577 static unsigned int to_bytes_per_ms(unsigned int samplerate)
579 return samplerate / 1000;
582 SR_PRIV size_t fx2lafw_get_buffer_size(struct dev_context *devc)
587 * The buffer should be large enough to hold 10ms of data and
590 s = 10 * to_bytes_per_ms(devc->cur_samplerate);
591 return (s + 511) & ~511;
594 SR_PRIV unsigned int fx2lafw_get_number_of_transfers(struct dev_context *devc)
598 /* Total buffer size should be able to hold about 500ms of data. */
599 n = (500 * to_bytes_per_ms(devc->cur_samplerate) /
600 fx2lafw_get_buffer_size(devc));
602 if (n > NUM_SIMUL_TRANSFERS)
603 return NUM_SIMUL_TRANSFERS;
608 SR_PRIV unsigned int fx2lafw_get_timeout(struct dev_context *devc)
611 unsigned int timeout;
613 total_size = fx2lafw_get_buffer_size(devc) *
614 fx2lafw_get_number_of_transfers(devc);
615 timeout = total_size / to_bytes_per_ms(devc->cur_samplerate);
616 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */