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>
34 struct cmd_start_acquisition {
36 uint8_t sample_delay_h;
37 uint8_t sample_delay_l;
42 #define USB_TIMEOUT 100
44 static int command_get_fw_version(libusb_device_handle *devhdl,
45 struct version_info *vi)
49 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
50 LIBUSB_ENDPOINT_IN, DS_CMD_GET_FW_VERSION, 0x0000, 0x0000,
51 (unsigned char *)vi, sizeof(struct version_info), USB_TIMEOUT);
54 sr_err("Unable to get version info: %s.",
55 libusb_error_name(ret));
62 static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid)
64 struct sr_usb_dev_inst *usb = sdi->conn;
65 libusb_device_handle *devhdl = usb->devhdl;
68 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
69 LIBUSB_ENDPOINT_IN, DS_CMD_GET_REVID_VERSION, 0x0000, 0x0000,
70 revid, 1, USB_TIMEOUT);
73 sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
80 SR_PRIV int dslogic_set_voltage_threshold(const struct sr_dev_inst *sdi, double threshold)
83 struct dev_context *const devc = sdi->priv;
84 const struct sr_usb_dev_inst *const usb = sdi->conn;
85 const uint8_t value = (threshold / 5.0) * 255;
86 const uint16_t cmd = value | (DS_ADDR_VTH << 8);
88 /* Send the control command. */
89 ret = libusb_control_transfer(usb->devhdl,
90 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT,
91 DS_CMD_WR_REG, 0x0000, 0x0000,
92 (unsigned char *)&cmd, sizeof(cmd), 3000);
94 sr_err("Unable to set voltage-threshold register: %s.",
95 libusb_error_name(ret));
99 devc->cur_threshold = threshold;
104 SR_PRIV int dslogic_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
106 libusb_device **devlist;
107 struct sr_usb_dev_inst *usb;
108 struct libusb_device_descriptor des;
109 struct dev_context *devc;
110 struct drv_context *drvc;
111 struct version_info vi;
112 int ret, i, device_count;
114 char connection_id[64];
120 if (sdi->status == SR_ST_ACTIVE)
121 /* Device is already in use. */
124 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
125 if (device_count < 0) {
126 sr_err("Failed to get device list: %s.",
127 libusb_error_name(device_count));
131 for (i = 0; i < device_count; i++) {
132 libusb_get_device_descriptor(devlist[i], &des);
134 if (des.idVendor != devc->profile->vid
135 || des.idProduct != devc->profile->pid)
138 if ((sdi->status == SR_ST_INITIALIZING) ||
139 (sdi->status == SR_ST_INACTIVE)) {
141 * Check device by its physical USB bus/port address.
143 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
144 if (strcmp(sdi->connection_id, connection_id))
145 /* This is not the one. */
149 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
150 if (usb->address == 0xff)
152 * First time we touch this device after FW
153 * upload, so we don't know the address yet.
155 usb->address = libusb_get_device_address(devlist[i]);
157 sr_err("Failed to open device: %s.",
158 libusb_error_name(ret));
162 if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) {
163 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
164 if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) {
165 sr_err("Failed to detach kernel driver: %s.",
166 libusb_error_name(ret));
172 ret = command_get_fw_version(usb->devhdl, &vi);
174 sr_err("Failed to get firmware version.");
178 ret = command_get_revid_version(sdi, &revid);
180 sr_err("Failed to get REVID.");
185 * Changes in major version mean incompatible/API changes, so
186 * bail out if we encounter an incompatible version.
187 * Different minor versions are OK, they should be compatible.
189 if (vi.major != DSLOGIC_REQUIRED_VERSION_MAJOR) {
190 sr_err("Expected firmware version %d.x, "
191 "got %d.%d.", DSLOGIC_REQUIRED_VERSION_MAJOR,
196 sdi->status = SR_ST_ACTIVE;
197 sr_info("Opened device on %d.%d (logical) / %s (physical), "
198 "interface %d, firmware %d.%d.",
199 usb->bus, usb->address, connection_id,
200 USB_INTERFACE, vi.major, vi.minor);
202 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
203 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
207 libusb_free_device_list(devlist, 1);
209 if (sdi->status != SR_ST_ACTIVE)
215 SR_PRIV struct dev_context *dslogic_dev_new(void)
217 struct dev_context *devc;
219 devc = g_malloc0(sizeof(struct dev_context));
220 devc->profile = NULL;
221 devc->fw_updated = 0;
222 devc->cur_samplerate = 0;
223 devc->limit_samples = 0;
224 devc->capture_ratio = 0;
225 devc->continuous_mode = FALSE;
226 devc->clock_edge = DS_EDGE_RISING;
231 SR_PRIV void dslogic_abort_acquisition(struct dev_context *devc)
235 devc->acq_aborted = TRUE;
237 for (i = devc->num_transfers - 1; i >= 0; i--) {
238 if (devc->transfers[i])
239 libusb_cancel_transfer(devc->transfers[i]);
243 static void finish_acquisition(struct sr_dev_inst *sdi)
245 struct dev_context *devc;
249 std_session_send_df_end(sdi);
251 usb_source_remove(sdi->session, devc->ctx);
253 devc->num_transfers = 0;
254 g_free(devc->transfers);
257 static void free_transfer(struct libusb_transfer *transfer)
259 struct sr_dev_inst *sdi;
260 struct dev_context *devc;
263 sdi = transfer->user_data;
266 g_free(transfer->buffer);
267 transfer->buffer = NULL;
268 libusb_free_transfer(transfer);
270 for (i = 0; i < devc->num_transfers; i++) {
271 if (devc->transfers[i] == transfer) {
272 devc->transfers[i] = NULL;
277 devc->submitted_transfers--;
278 if (devc->submitted_transfers == 0)
279 finish_acquisition(sdi);
282 static void resubmit_transfer(struct libusb_transfer *transfer)
286 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
289 sr_err("%s: %s", __func__, libusb_error_name(ret));
290 free_transfer(transfer);
294 SR_PRIV void dslogic_send_data(struct sr_dev_inst *sdi,
295 uint8_t *data, size_t length, size_t sample_width)
297 const struct sr_datafeed_logic logic = {
299 .unitsize = sample_width,
303 const struct sr_datafeed_packet packet = {
308 sr_session_send(sdi, &packet);
311 SR_PRIV void LIBUSB_CALL dslogic_receive_transfer(struct libusb_transfer *transfer)
313 struct sr_dev_inst *sdi;
314 struct dev_context *devc;
315 gboolean packet_has_error = FALSE;
316 struct sr_datafeed_packet packet;
317 unsigned int num_samples;
318 int trigger_offset, cur_sample_count;
319 const int unitsize = 2;
321 sdi = transfer->user_data;
325 * If acquisition has already ended, just free any queued up
326 * transfer that come in.
328 if (devc->acq_aborted) {
329 free_transfer(transfer);
333 sr_dbg("receive_transfer(): status %s received %d bytes.",
334 libusb_error_name(transfer->status), transfer->actual_length);
336 /* Save incoming transfer before reusing the transfer struct. */
337 cur_sample_count = transfer->actual_length / unitsize;
339 switch (transfer->status) {
340 case LIBUSB_TRANSFER_NO_DEVICE:
341 dslogic_abort_acquisition(devc);
342 free_transfer(transfer);
344 case LIBUSB_TRANSFER_COMPLETED:
345 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
348 packet_has_error = TRUE;
352 if (transfer->actual_length == 0 || packet_has_error) {
353 devc->empty_transfer_count++;
354 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
356 * The FX2 gave up. End the acquisition, the frontend
357 * will work out that the samplecount is short.
359 dslogic_abort_acquisition(devc);
360 free_transfer(transfer);
362 resubmit_transfer(transfer);
366 devc->empty_transfer_count = 0;
368 if (devc->trigger_fired) {
369 if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) {
370 /* Send the incoming transfer to the session bus. */
371 if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples)
372 num_samples = devc->limit_samples - devc->sent_samples;
374 num_samples = cur_sample_count;
376 if (devc->trigger_pos > devc->sent_samples
377 && devc->trigger_pos <= devc->sent_samples + num_samples) {
378 /* DSLogic trigger in this block. Send trigger position. */
379 trigger_offset = devc->trigger_pos - devc->sent_samples;
380 /* Pre-trigger samples. */
381 dslogic_send_data(sdi, (uint8_t *)transfer->buffer,
382 trigger_offset * unitsize, unitsize);
383 devc->sent_samples += trigger_offset;
384 /* Trigger position. */
385 devc->trigger_pos = 0;
386 packet.type = SR_DF_TRIGGER;
387 packet.payload = NULL;
388 sr_session_send(sdi, &packet);
389 /* Post trigger samples. */
390 num_samples -= trigger_offset;
391 dslogic_send_data(sdi, (uint8_t *)transfer->buffer
392 + trigger_offset * unitsize, num_samples * unitsize, unitsize);
393 devc->sent_samples += num_samples;
395 dslogic_send_data(sdi, (uint8_t *)transfer->buffer,
396 num_samples * unitsize, unitsize);
397 devc->sent_samples += num_samples;
402 if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
403 dslogic_abort_acquisition(devc);
404 free_transfer(transfer);
406 resubmit_transfer(transfer);
409 static unsigned int to_bytes_per_ms(unsigned int samplerate)
411 return samplerate / 1000;
414 SR_PRIV size_t dslogic_get_buffer_size(struct dev_context *devc)
419 * The buffer should be large enough to hold 10ms of data and
422 s = 10 * to_bytes_per_ms(devc->cur_samplerate);
423 return (s + 511) & ~511;
426 SR_PRIV unsigned int dslogic_get_timeout(struct dev_context *devc)
429 unsigned int timeout;
431 total_size = dslogic_get_buffer_size(devc) *
432 dslogic_get_number_of_transfers(devc);
433 timeout = total_size / to_bytes_per_ms(devc->cur_samplerate);
434 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */