1 /***************************************************************************
2 ftdi_stream.c - description
4 copyright : (C) 2009 Micah Dowty 2010 Uwe Bonnes
5 email : opensource@intra2net.com
6 SPDX-License-Identifier: (LGPL-2.1-only AND MIT)
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU Lesser General Public License *
13 * version 2.1 as published by the Free Software Foundation; *
15 ***************************************************************************/
18 * fastftdi.c - A minimal FTDI FT232H interface for which supports bit-bang
19 * mode, but focuses on very high-performance support for
20 * synchronous FIFO mode. Requires libusb-1.0
22 * Copyright (C) 2009 Micah Dowty
24 * Permission is hereby granted, free of charge, to any person obtaining a copy
25 * of this software and associated documentation files (the "Software"), to deal
26 * in the Software without restriction, including without limitation the rights
27 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28 * copies of the Software, and to permit persons to whom the Software is
29 * furnished to do so, subject to the following conditions:
31 * The above copyright notice and this permission notice shall be included in
32 * all copies or substantial portions of the Software.
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
54 FTDIStreamCallback *callback;
59 FTDIProgressInfo progress;
64 * With Exit request, free memory and release the transfer
66 * state->result is only set when some error happens
68 static void LIBUSB_CALL
69 ftdi_readstream_cb(struct libusb_transfer *transfer)
71 FTDIStreamState *state = transfer->user_data;
72 int packet_size = state->packetsize;
75 if (transfer->status == LIBUSB_TRANSFER_COMPLETED)
78 uint8_t *ptr = transfer->buffer;
79 int length = transfer->actual_length;
80 int numPackets = (length + packet_size - 1) / packet_size;
83 for (i = 0; i < numPackets; i++)
86 int packetLen = length;
88 if (packetLen > packet_size)
89 packetLen = packet_size;
91 payloadLen = packetLen - 2;
92 state->progress.current.totalBytes += payloadLen;
94 res = state->callback(ptr + 2, payloadLen,
95 NULL, state->userdata);
102 free(transfer->buffer);
103 libusb_free_transfer(transfer);
107 transfer->status = -1;
108 state->result = libusb_submit_transfer(transfer);
113 fprintf(stderr, "unknown status %d\n",transfer->status);
114 state->result = LIBUSB_ERROR_IO;
119 Helper function to calculate (unix) time differences
125 TimevalDiff(const struct timeval *a, const struct timeval *b)
127 return (a->tv_sec - b->tv_sec) + 1e-6 * (a->tv_usec - b->tv_usec);
131 Streaming reading of data from the device
133 Use asynchronous transfers in libusb-1.0 for high-performance
134 streaming of data from a device interface back to the PC. This
135 function continuously transfers data until either an error occurs
136 or the callback returns a nonzero value. This function returns
137 a libusb error code or the callback's return value.
139 For every contiguous block of received data, the callback will
142 \param ftdi pointer to ftdi_context
143 \param callback to user supplied function for one block of data
145 \param packetsPerTransfer number of packets per transfer
146 \param numTransfers Number of transfers per callback
151 ftdi_readstream(struct ftdi_context *ftdi,
152 FTDIStreamCallback *callback, void *userdata,
153 int packetsPerTransfer, int numTransfers)
155 struct libusb_transfer **transfers;
156 FTDIStreamState state = { callback, userdata, ftdi->max_packet_size, 1 };
157 int bufferSize = packetsPerTransfer * ftdi->max_packet_size;
161 /* Only FT2232H and FT232H know about the synchronous FIFO Mode*/
162 if ((ftdi->type != TYPE_2232H) && (ftdi->type != TYPE_232H))
164 fprintf(stderr,"Device doesn't support synchronous FIFO mode\n");
168 /* We don't know in what state we are, switch to reset*/
169 if (ftdi_set_bitmode(ftdi, 0xff, BITMODE_RESET) < 0)
171 fprintf(stderr,"Can't reset mode\n");
175 /* Purge anything remaining in the buffers*/
176 if (ftdi_tcioflush(ftdi) < 0)
178 fprintf(stderr,"Can't flush FIFOs & buffers\n");
183 * Set up all transfers
186 transfers = calloc(numTransfers, sizeof *transfers);
189 err = LIBUSB_ERROR_NO_MEM;
193 for (xferIndex = 0; xferIndex < numTransfers; xferIndex++)
195 struct libusb_transfer *transfer;
197 transfer = libusb_alloc_transfer(0);
198 transfers[xferIndex] = transfer;
201 err = LIBUSB_ERROR_NO_MEM;
205 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->out_ep,
206 malloc(bufferSize), bufferSize,
210 if (!transfer->buffer)
212 err = LIBUSB_ERROR_NO_MEM;
216 transfer->status = -1;
217 err = libusb_submit_transfer(transfer);
222 /* Start the transfers only when everything has been set up.
223 * Otherwise the transfers start stuttering and the PC not
224 * fetching data for several to several ten milliseconds
227 if (ftdi_set_bitmode(ftdi, 0xff, BITMODE_SYNCFF) < 0)
229 fprintf(stderr,"Can't set synchronous fifo mode: %s\n",
230 ftdi_get_error_string(ftdi));
235 * Run the transfers, and periodically assess progress.
238 gettimeofday(&state.progress.first.time, NULL);
242 FTDIProgressInfo *progress = &state.progress;
243 const double progressInterval = 1.0;
244 struct timeval timeout = { 0, ftdi->usb_read_timeout * 1000};
247 int err = libusb_handle_events_timeout(ftdi->usb_ctx, &timeout);
248 if (err == LIBUSB_ERROR_INTERRUPTED)
249 /* restart interrupted events */
250 err = libusb_handle_events_timeout(ftdi->usb_ctx, &timeout);
255 if (state.activity == 0)
260 // If enough time has elapsed, update the progress
261 gettimeofday(&now, NULL);
262 if (TimevalDiff(&now, &progress->current.time) >= progressInterval)
264 progress->current.time = now;
265 progress->totalTime = TimevalDiff(&progress->current.time,
266 &progress->first.time);
268 if (progress->prev.totalBytes)
270 // We have enough information to calculate rates
274 currentTime = TimevalDiff(&progress->current.time,
275 &progress->prev.time);
277 progress->totalRate =
278 progress->current.totalBytes /progress->totalTime;
279 progress->currentRate =
280 (progress->current.totalBytes -
281 progress->prev.totalBytes) / currentTime;
284 state.callback(NULL, 0, progress, state.userdata);
285 progress->prev = progress->current;
288 } while (!state.result);
291 * Cancel any outstanding transfers, and free memory.
295 fprintf(stderr, "cleanup\n");