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;
91 struct sr_usb_dev_inst *usb;
93 struct cmd_start_acquisition cmd;
98 samplerate = devc->cur_samplerate;
100 /* Compute the sample rate. */
101 if (devc->sample_wide && samplerate > MAX_16BIT_SAMPLE_RATE) {
102 sr_err("Unable to sample at %" PRIu64 "Hz "
103 "when collecting 16-bit samples.", samplerate);
108 cmd.flags = cmd.sample_delay_h = cmd.sample_delay_l = 0;
109 if ((SR_MHZ(48) % samplerate) == 0) {
110 cmd.flags = CMD_START_FLAGS_CLK_48MHZ;
111 delay = SR_MHZ(48) / samplerate - 1;
112 if (delay > MAX_SAMPLE_DELAY)
116 if (delay == 0 && (SR_MHZ(30) % samplerate) == 0) {
117 cmd.flags = CMD_START_FLAGS_CLK_30MHZ;
118 delay = SR_MHZ(30) / samplerate - 1;
121 sr_info("GPIF delay = %d, clocksource = %sMHz.", delay,
122 (cmd.flags & CMD_START_FLAGS_CLK_48MHZ) ? "48" : "30");
124 if (delay <= 0 || delay > MAX_SAMPLE_DELAY) {
125 sr_err("Unable to sample at %" PRIu64 "Hz.", samplerate);
129 cmd.sample_delay_h = (delay >> 8) & 0xff;
130 cmd.sample_delay_l = delay & 0xff;
132 /* Select the sampling width. */
133 cmd.flags |= devc->sample_wide ? CMD_START_FLAGS_SAMPLE_16BIT :
134 CMD_START_FLAGS_SAMPLE_8BIT;
136 /* Send the control message. */
137 ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
138 LIBUSB_ENDPOINT_OUT, CMD_START, 0x0000, 0x0000,
139 (unsigned char *)&cmd, sizeof(cmd), 100);
141 sr_err("Unable to send start command: %s.",
142 libusb_error_name(ret));
150 * Check the USB configuration to determine if this is an fx2lafw device.
152 * @return TRUE if the device's configuration profile match fx2lafw
153 * configuration, FALSE otherwise.
155 SR_PRIV gboolean fx2lafw_check_conf_profile(libusb_device *dev)
157 struct libusb_device_descriptor des;
158 struct libusb_device_handle *hdl;
160 unsigned char strdesc[64];
165 /* Assume the FW has not been loaded, unless proven wrong. */
166 if (libusb_get_device_descriptor(dev, &des) != 0)
169 if (libusb_open(dev, &hdl) != 0)
172 if (libusb_get_string_descriptor_ascii(hdl,
173 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
175 if (strncmp((const char *)strdesc, "sigrok", 6))
178 if (libusb_get_string_descriptor_ascii(hdl,
179 des.iProduct, strdesc, sizeof(strdesc)) < 0)
181 if (strncmp((const char *)strdesc, "fx2lafw", 7))
184 /* If we made it here, it must be an fx2lafw. */
193 SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
195 libusb_device **devlist;
196 struct sr_usb_dev_inst *usb;
197 struct libusb_device_descriptor des;
198 struct dev_context *devc;
199 struct drv_context *drvc;
200 struct version_info vi;
201 int ret, i, device_count;
203 char connection_id[64];
209 if (sdi->status == SR_ST_ACTIVE)
210 /* Device is already in use. */
213 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
214 if (device_count < 0) {
215 sr_err("Failed to get device list: %s.",
216 libusb_error_name(device_count));
220 for (i = 0; i < device_count; i++) {
221 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
222 sr_err("Failed to get device descriptor: %s.",
223 libusb_error_name(ret));
227 if (des.idVendor != devc->profile->vid
228 || des.idProduct != devc->profile->pid)
231 if ((sdi->status == SR_ST_INITIALIZING) ||
232 (sdi->status == SR_ST_INACTIVE)) {
234 * Check device by its physical USB bus/port address.
236 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
237 if (strcmp(sdi->connection_id, connection_id))
238 /* This is not the one. */
242 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
243 if (usb->address == 0xff)
245 * First time we touch this device after FW
246 * upload, so we don't know the address yet.
248 usb->address = libusb_get_device_address(devlist[i]);
250 sr_err("Failed to open device: %s.",
251 libusb_error_name(ret));
255 ret = command_get_fw_version(usb->devhdl, &vi);
257 sr_err("Failed to get firmware version.");
261 ret = command_get_revid_version(sdi, &revid);
263 sr_err("Failed to get REVID.");
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.
272 if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) {
273 sr_err("Expected firmware version %d.x, "
274 "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR,
279 sdi->status = SR_ST_ACTIVE;
280 sr_info("Opened device on %d.%d (logical) / %s (physical), "
281 "interface %d, firmware %d.%d.",
282 usb->bus, usb->address, connection_id,
283 USB_INTERFACE, vi.major, vi.minor);
285 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
286 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
290 libusb_free_device_list(devlist, 1);
292 if (sdi->status != SR_ST_ACTIVE)
298 SR_PRIV struct dev_context *fx2lafw_dev_new(void)
300 struct dev_context *devc;
302 devc = g_malloc0(sizeof(struct dev_context));
303 devc->profile = NULL;
304 devc->fw_updated = 0;
305 devc->cur_samplerate = 0;
306 devc->limit_samples = 0;
307 devc->sample_wide = FALSE;
313 SR_PRIV void fx2lafw_abort_acquisition(struct dev_context *devc)
317 devc->acq_aborted = TRUE;
319 for (i = devc->num_transfers - 1; i >= 0; i--) {
320 if (devc->transfers[i])
321 libusb_cancel_transfer(devc->transfers[i]);
325 static void finish_acquisition(struct sr_dev_inst *sdi)
327 struct sr_datafeed_packet packet;
328 struct dev_context *devc;
332 /* Terminate session. */
333 packet.type = SR_DF_END;
334 sr_session_send(sdi, &packet);
336 /* Remove fds from polling. */
337 usb_source_remove(sdi->session, devc->ctx);
339 devc->num_transfers = 0;
340 g_free(devc->transfers);
343 soft_trigger_logic_free(devc->stl);
348 static void free_transfer(struct libusb_transfer *transfer)
350 struct sr_dev_inst *sdi;
351 struct dev_context *devc;
354 sdi = transfer->user_data;
357 g_free(transfer->buffer);
358 transfer->buffer = NULL;
359 libusb_free_transfer(transfer);
361 for (i = 0; i < devc->num_transfers; i++) {
362 if (devc->transfers[i] == transfer) {
363 devc->transfers[i] = NULL;
368 devc->submitted_transfers--;
369 if (devc->submitted_transfers == 0)
370 finish_acquisition(sdi);
373 static void resubmit_transfer(struct libusb_transfer *transfer)
377 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
380 sr_err("%s: %s", __func__, libusb_error_name(ret));
381 free_transfer(transfer);
385 SR_PRIV void fx2lafw_receive_transfer(struct libusb_transfer *transfer)
387 struct sr_dev_inst *sdi;
388 struct dev_context *devc;
389 gboolean packet_has_error = FALSE;
390 struct sr_datafeed_packet packet;
391 struct sr_datafeed_logic logic;
392 unsigned int num_samples;
393 int trigger_offset, cur_sample_count, unitsize;
395 sdi = transfer->user_data;
399 * If acquisition has already ended, just free any queued up
400 * transfer that come in.
402 if (devc->acq_aborted) {
403 free_transfer(transfer);
407 sr_info("receive_transfer(): status %d received %d bytes.",
408 transfer->status, transfer->actual_length);
410 /* Save incoming transfer before reusing the transfer struct. */
411 unitsize = devc->sample_wide ? 2 : 1;
412 cur_sample_count = transfer->actual_length / unitsize;
414 switch (transfer->status) {
415 case LIBUSB_TRANSFER_NO_DEVICE:
416 fx2lafw_abort_acquisition(devc);
417 free_transfer(transfer);
419 case LIBUSB_TRANSFER_COMPLETED:
420 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
423 packet_has_error = TRUE;
427 if (transfer->actual_length == 0 || packet_has_error) {
428 devc->empty_transfer_count++;
429 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
431 * The FX2 gave up. End the acquisition, the frontend
432 * will work out that the samplecount is short.
434 fx2lafw_abort_acquisition(devc);
435 free_transfer(transfer);
437 resubmit_transfer(transfer);
441 devc->empty_transfer_count = 0;
444 if (devc->trigger_fired) {
445 if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) {
446 /* Send the incoming transfer to the session bus. */
447 packet.type = SR_DF_LOGIC;
448 packet.payload = &logic;
449 if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples)
450 num_samples = devc->limit_samples - devc->sent_samples;
452 num_samples = cur_sample_count;
453 logic.length = num_samples * unitsize;
454 logic.unitsize = unitsize;
455 logic.data = transfer->buffer;
456 sr_session_send(devc->cb_data, &packet);
457 devc->sent_samples += num_samples;
460 trigger_offset = soft_trigger_logic_check(devc->stl,
461 transfer->buffer, transfer->actual_length);
462 if (trigger_offset > -1) {
463 packet.type = SR_DF_LOGIC;
464 packet.payload = &logic;
465 num_samples = cur_sample_count - trigger_offset;
466 if (devc->limit_samples &&
467 num_samples > devc->limit_samples - devc->sent_samples)
468 num_samples = devc->limit_samples - devc->sent_samples;
469 logic.length = num_samples * unitsize;
470 logic.unitsize = unitsize;
471 logic.data = transfer->buffer + trigger_offset * unitsize;
472 sr_session_send(devc->cb_data, &packet);
473 devc->sent_samples += num_samples;
475 devc->trigger_fired = TRUE;
479 if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
480 fx2lafw_abort_acquisition(devc);
481 free_transfer(transfer);
483 resubmit_transfer(transfer);
486 static unsigned int to_bytes_per_ms(unsigned int samplerate)
488 return samplerate / 1000;
491 SR_PRIV size_t fx2lafw_get_buffer_size(struct dev_context *devc)
496 * The buffer should be large enough to hold 10ms of data and
499 s = 10 * to_bytes_per_ms(devc->cur_samplerate);
500 return (s + 511) & ~511;
503 SR_PRIV unsigned int fx2lafw_get_number_of_transfers(struct dev_context *devc)
507 /* Total buffer size should be able to hold about 500ms of data. */
508 n = (500 * to_bytes_per_ms(devc->cur_samplerate) /
509 fx2lafw_get_buffer_size(devc));
511 if (n > NUM_SIMUL_TRANSFERS)
512 return NUM_SIMUL_TRANSFERS;
517 SR_PRIV unsigned int fx2lafw_get_timeout(struct dev_context *devc)
520 unsigned int timeout;
522 total_size = fx2lafw_get_buffer_size(devc) *
523 fx2lafw_get_number_of_transfers(devc);
524 timeout = total_size / to_bytes_per_ms(devc->cur_samplerate);
525 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */