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 #include <glib/gstdio.h>
33 struct cmd_start_acquisition {
35 uint8_t sample_delay_h;
36 uint8_t sample_delay_l;
41 #define USB_TIMEOUT 100
43 static int command_get_fw_version(libusb_device_handle *devhdl,
44 struct version_info *vi)
48 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
49 LIBUSB_ENDPOINT_IN, CMD_GET_FW_VERSION, 0x0000, 0x0000,
50 (unsigned char *)vi, sizeof(struct version_info), USB_TIMEOUT);
53 sr_err("Unable to get version info: %s.",
54 libusb_error_name(ret));
61 static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid)
63 struct sr_usb_dev_inst *usb = sdi->conn;
64 libusb_device_handle *devhdl = usb->devhdl;
67 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
68 LIBUSB_ENDPOINT_IN, CMD_GET_REVID_VERSION, 0x0000, 0x0000,
69 revid, 1, USB_TIMEOUT);
72 sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
79 static int command_start_acquisition(const struct sr_dev_inst *sdi)
81 struct dev_context *devc;
82 struct sr_usb_dev_inst *usb;
84 struct cmd_start_acquisition cmd;
89 samplerate = devc->cur_samplerate;
91 /* Compute the sample rate. */
92 if (devc->sample_wide && samplerate > MAX_16BIT_SAMPLE_RATE) {
93 sr_err("Unable to sample at %" PRIu64 "Hz "
94 "when collecting 16-bit samples.", samplerate);
99 cmd.flags = cmd.sample_delay_h = cmd.sample_delay_l = 0;
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_dbg("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 |= devc->sample_wide ? CMD_START_FLAGS_SAMPLE_16BIT :
125 CMD_START_FLAGS_SAMPLE_8BIT;
126 /* Enable CTL2 clock. */
127 cmd.flags |= (g_slist_length(devc->enabled_analog_channels) > 0) ? CMD_START_FLAGS_CLK_CTL2 : 0;
129 /* Send the control message. */
130 ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
131 LIBUSB_ENDPOINT_OUT, CMD_START, 0x0000, 0x0000,
132 (unsigned char *)&cmd, sizeof(cmd), USB_TIMEOUT);
134 sr_err("Unable to send start command: %s.",
135 libusb_error_name(ret));
142 SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
144 libusb_device **devlist;
145 struct sr_usb_dev_inst *usb;
146 struct libusb_device_descriptor des;
147 struct dev_context *devc;
148 struct drv_context *drvc;
149 struct version_info vi;
150 int ret, i, device_count;
152 char connection_id[64];
158 if (sdi->status == SR_ST_ACTIVE)
159 /* Device is already in use. */
162 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
163 if (device_count < 0) {
164 sr_err("Failed to get device list: %s.",
165 libusb_error_name(device_count));
169 for (i = 0; i < device_count; i++) {
170 libusb_get_device_descriptor(devlist[i], &des);
172 if (des.idVendor != devc->profile->vid
173 || des.idProduct != devc->profile->pid)
176 if ((sdi->status == SR_ST_INITIALIZING) ||
177 (sdi->status == SR_ST_INACTIVE)) {
179 * Check device by its physical USB bus/port address.
181 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
182 if (strcmp(sdi->connection_id, connection_id))
183 /* This is not the one. */
187 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
188 if (usb->address == 0xff)
190 * First time we touch this device after FW
191 * upload, so we don't know the address yet.
193 usb->address = libusb_get_device_address(devlist[i]);
195 sr_err("Failed to open device: %s.",
196 libusb_error_name(ret));
200 if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) {
201 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
202 if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) {
203 sr_err("Failed to detach kernel driver: %s.",
204 libusb_error_name(ret));
210 ret = command_get_fw_version(usb->devhdl, &vi);
212 sr_err("Failed to get firmware version.");
216 ret = command_get_revid_version(sdi, &revid);
218 sr_err("Failed to get REVID.");
223 * Changes in major version mean incompatible/API changes, so
224 * bail out if we encounter an incompatible version.
225 * Different minor versions are OK, they should be compatible.
227 if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) {
228 sr_err("Expected firmware version %d.x, "
229 "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR,
234 sdi->status = SR_ST_ACTIVE;
235 sr_info("Opened device on %d.%d (logical) / %s (physical), "
236 "interface %d, firmware %d.%d.",
237 usb->bus, usb->address, connection_id,
238 USB_INTERFACE, vi.major, vi.minor);
240 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
241 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
245 libusb_free_device_list(devlist, 1);
247 if (sdi->status != SR_ST_ACTIVE)
253 SR_PRIV struct dev_context *fx2lafw_dev_new(void)
255 struct dev_context *devc;
257 devc = g_malloc0(sizeof(struct dev_context));
258 devc->profile = NULL;
259 devc->fw_updated = 0;
260 devc->cur_samplerate = 0;
261 devc->limit_samples = 0;
262 devc->capture_ratio = 0;
263 devc->sample_wide = FALSE;
269 SR_PRIV void fx2lafw_abort_acquisition(struct dev_context *devc)
273 devc->acq_aborted = TRUE;
275 for (i = devc->num_transfers - 1; i >= 0; i--) {
276 if (devc->transfers[i])
277 libusb_cancel_transfer(devc->transfers[i]);
281 static void finish_acquisition(struct sr_dev_inst *sdi)
283 struct dev_context *devc;
287 std_session_send_df_end(sdi);
289 usb_source_remove(sdi->session, devc->ctx);
291 devc->num_transfers = 0;
292 g_free(devc->transfers);
294 /* Free the deinterlace buffers if we had them. */
295 if (g_slist_length(devc->enabled_analog_channels) > 0) {
296 g_free(devc->logic_buffer);
297 g_free(devc->analog_buffer);
301 soft_trigger_logic_free(devc->stl);
306 static void free_transfer(struct libusb_transfer *transfer)
308 struct sr_dev_inst *sdi;
309 struct dev_context *devc;
312 sdi = transfer->user_data;
315 g_free(transfer->buffer);
316 transfer->buffer = NULL;
317 libusb_free_transfer(transfer);
319 for (i = 0; i < devc->num_transfers; i++) {
320 if (devc->transfers[i] == transfer) {
321 devc->transfers[i] = NULL;
326 devc->submitted_transfers--;
327 if (devc->submitted_transfers == 0)
328 finish_acquisition(sdi);
331 static void resubmit_transfer(struct libusb_transfer *transfer)
335 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
338 sr_err("%s: %s", __func__, libusb_error_name(ret));
339 free_transfer(transfer);
343 static void mso_send_data_proc(struct sr_dev_inst *sdi,
344 uint8_t *data, size_t length, size_t sample_width)
347 struct dev_context *devc;
348 struct sr_datafeed_analog analog;
349 struct sr_analog_encoding encoding;
350 struct sr_analog_meaning meaning;
351 struct sr_analog_spec spec;
360 for (i = 0; i < length; i++) {
361 devc->logic_buffer[i] = data[i * 2];
362 /* Rescale to -10V - +10V from 0-255. */
363 devc->analog_buffer[i] = (data[i * 2 + 1] - 128.0f) / 12.8f;
366 const struct sr_datafeed_logic logic = {
369 .data = devc->logic_buffer
372 const struct sr_datafeed_packet logic_packet = {
377 sr_session_send(sdi, &logic_packet);
379 sr_analog_init(&analog, &encoding, &meaning, &spec, 2);
380 analog.meaning->channels = devc->enabled_analog_channels;
381 analog.meaning->mq = SR_MQ_VOLTAGE;
382 analog.meaning->unit = SR_UNIT_VOLT;
383 analog.meaning->mqflags = 0 /* SR_MQFLAG_DC */;
384 analog.num_samples = length;
385 analog.data = devc->analog_buffer;
387 const struct sr_datafeed_packet analog_packet = {
388 .type = SR_DF_ANALOG,
392 sr_session_send(sdi, &analog_packet);
395 static void la_send_data_proc(struct sr_dev_inst *sdi,
396 uint8_t *data, size_t length, size_t sample_width)
398 const struct sr_datafeed_logic logic = {
400 .unitsize = sample_width,
404 const struct sr_datafeed_packet packet = {
409 sr_session_send(sdi, &packet);
412 static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
414 struct sr_dev_inst *sdi;
415 struct dev_context *devc;
416 gboolean packet_has_error = FALSE;
417 unsigned int num_samples;
418 int trigger_offset, cur_sample_count, unitsize;
419 int pre_trigger_samples;
421 sdi = transfer->user_data;
425 * If acquisition has already ended, just free any queued up
426 * transfer that come in.
428 if (devc->acq_aborted) {
429 free_transfer(transfer);
433 sr_dbg("receive_transfer(): status %s received %d bytes.",
434 libusb_error_name(transfer->status), transfer->actual_length);
436 /* Save incoming transfer before reusing the transfer struct. */
437 unitsize = devc->sample_wide ? 2 : 1;
438 cur_sample_count = transfer->actual_length / unitsize;
440 switch (transfer->status) {
441 case LIBUSB_TRANSFER_NO_DEVICE:
442 fx2lafw_abort_acquisition(devc);
443 free_transfer(transfer);
445 case LIBUSB_TRANSFER_COMPLETED:
446 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
449 packet_has_error = TRUE;
453 if (transfer->actual_length == 0 || packet_has_error) {
454 devc->empty_transfer_count++;
455 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
457 * The FX2 gave up. End the acquisition, the frontend
458 * will work out that the samplecount is short.
460 fx2lafw_abort_acquisition(devc);
461 free_transfer(transfer);
463 resubmit_transfer(transfer);
467 devc->empty_transfer_count = 0;
469 if (devc->trigger_fired) {
470 if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) {
471 /* Send the incoming transfer to the session bus. */
472 if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples)
473 num_samples = devc->limit_samples - devc->sent_samples;
475 num_samples = cur_sample_count;
477 devc->send_data_proc(sdi, (uint8_t *)transfer->buffer,
478 num_samples * unitsize, unitsize);
479 devc->sent_samples += num_samples;
482 trigger_offset = soft_trigger_logic_check(devc->stl,
483 transfer->buffer, transfer->actual_length, &pre_trigger_samples);
484 if (trigger_offset > -1) {
485 devc->sent_samples += pre_trigger_samples;
486 num_samples = cur_sample_count - trigger_offset;
487 if (devc->limit_samples &&
488 num_samples > devc->limit_samples - devc->sent_samples)
489 num_samples = devc->limit_samples - devc->sent_samples;
491 devc->send_data_proc(sdi, (uint8_t *)transfer->buffer
492 + trigger_offset * unitsize,
493 num_samples * unitsize, unitsize);
494 devc->sent_samples += num_samples;
496 devc->trigger_fired = TRUE;
500 if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
501 fx2lafw_abort_acquisition(devc);
502 free_transfer(transfer);
504 resubmit_transfer(transfer);
507 static int configure_channels(const struct sr_dev_inst *sdi)
509 struct dev_context *devc;
512 struct sr_channel *ch;
513 uint32_t channel_mask = 0, num_analog = 0;
517 g_slist_free(devc->enabled_analog_channels);
518 devc->enabled_analog_channels = NULL;
520 for (l = sdi->channels, p = 0; l; l = l->next, p++) {
522 if ((p <= NUM_CHANNELS) && (ch->type == SR_CHANNEL_ANALOG)
525 devc->enabled_analog_channels =
526 g_slist_append(devc->enabled_analog_channels, ch);
528 channel_mask |= ch->enabled << p;
533 * Use wide sampling if either any of the LA channels 8..15 is enabled,
534 * and/or at least one analog channel is enabled.
536 devc->sample_wide = channel_mask > 0xff || num_analog > 0;
541 static unsigned int to_bytes_per_ms(unsigned int samplerate)
543 return samplerate / 1000;
546 static size_t get_buffer_size(struct dev_context *devc)
551 * The buffer should be large enough to hold 10ms of data and
554 s = 10 * to_bytes_per_ms(devc->cur_samplerate);
555 return (s + 511) & ~511;
558 static unsigned int get_number_of_transfers(struct dev_context *devc)
562 /* Total buffer size should be able to hold about 500ms of data. */
563 n = (500 * to_bytes_per_ms(devc->cur_samplerate) /
564 get_buffer_size(devc));
566 if (n > NUM_SIMUL_TRANSFERS)
567 return NUM_SIMUL_TRANSFERS;
572 static unsigned int get_timeout(struct dev_context *devc)
575 unsigned int timeout;
577 total_size = get_buffer_size(devc) *
578 get_number_of_transfers(devc);
579 timeout = total_size / to_bytes_per_ms(devc->cur_samplerate);
580 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
583 static int receive_data(int fd, int revents, void *cb_data)
586 struct drv_context *drvc;
591 drvc = (struct drv_context *)cb_data;
593 tv.tv_sec = tv.tv_usec = 0;
594 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
599 static int start_transfers(const struct sr_dev_inst *sdi)
601 struct dev_context *devc;
602 struct sr_usb_dev_inst *usb;
603 struct sr_trigger *trigger;
604 struct libusb_transfer *transfer;
605 unsigned int i, num_transfers;
613 devc->sent_samples = 0;
614 devc->acq_aborted = FALSE;
615 devc->empty_transfer_count = 0;
617 if ((trigger = sr_session_trigger_get(sdi->session))) {
618 int pre_trigger_samples = 0;
619 if (devc->limit_samples > 0)
620 pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100;
621 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
623 return SR_ERR_MALLOC;
624 devc->trigger_fired = FALSE;
626 devc->trigger_fired = TRUE;
628 num_transfers = get_number_of_transfers(devc);
630 size = get_buffer_size(devc);
631 devc->submitted_transfers = 0;
633 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
634 if (!devc->transfers) {
635 sr_err("USB transfers malloc failed.");
636 return SR_ERR_MALLOC;
639 timeout = get_timeout(devc);
640 devc->num_transfers = num_transfers;
641 for (i = 0; i < num_transfers; i++) {
642 if (!(buf = g_try_malloc(size))) {
643 sr_err("USB transfer buffer malloc failed.");
644 return SR_ERR_MALLOC;
646 transfer = libusb_alloc_transfer(0);
647 libusb_fill_bulk_transfer(transfer, usb->devhdl,
648 2 | LIBUSB_ENDPOINT_IN, buf, size,
649 receive_transfer, (void *)sdi, timeout);
650 sr_info("submitting transfer: %d", i);
651 if ((ret = libusb_submit_transfer(transfer)) != 0) {
652 sr_err("Failed to submit transfer: %s.",
653 libusb_error_name(ret));
654 libusb_free_transfer(transfer);
656 fx2lafw_abort_acquisition(devc);
659 devc->transfers[i] = transfer;
660 devc->submitted_transfers++;
664 * If this device has analog channels and at least one of them is
665 * enabled, use mso_send_data_proc() to properly handle the analog
666 * data. Otherwise use la_send_data_proc().
668 if (g_slist_length(devc->enabled_analog_channels) > 0)
669 devc->send_data_proc = mso_send_data_proc;
671 devc->send_data_proc = la_send_data_proc;
673 std_session_send_df_header(sdi);
678 SR_PRIV int fx2lafw_start_acquisition(const struct sr_dev_inst *sdi)
680 struct sr_dev_driver *di;
681 struct drv_context *drvc;
682 struct dev_context *devc;
690 devc->ctx = drvc->sr_ctx;
691 devc->sent_samples = 0;
692 devc->empty_transfer_count = 0;
693 devc->acq_aborted = FALSE;
695 if (configure_channels(sdi) != SR_OK) {
696 sr_err("Failed to configure channels.");
700 timeout = get_timeout(devc);
701 usb_source_add(sdi->session, devc->ctx, timeout, receive_data, drvc);
703 size = get_buffer_size(devc);
704 /* Prepare for analog sampling. */
705 if (g_slist_length(devc->enabled_analog_channels) > 0) {
706 /* We need a buffer half the size of a transfer. */
707 devc->logic_buffer = g_try_malloc(size / 2);
708 devc->analog_buffer = g_try_malloc(
709 sizeof(float) * size / 2);
711 start_transfers(sdi);
712 if ((ret = command_start_acquisition(sdi)) != SR_OK) {
713 fx2lafw_abort_acquisition(devc);