Reputation: 89
Using libftdi 1.5 with an FTDI FT232 USB serial converter, I've run into a situation in which bit-bang mode causes the device to fill some internal receive buffer with unwanted data that cannot be purged using the normal buffer flush routines.
My use case requires that one of the pins on the FT232 be used in bit-bang mode (BITMODE_BITBANG
) for a couple seconds, and then promptly return to serial/FIFO mode to read data that the remote side will begin sending. The problem: after returning to normal serial mode, calls to ftdi_read_data()
will return immediately, reading bytes with the value 0xFC or 0xFF (which I'm guessing represent the bit-bang pin state.) Only after reading 256 bytes like this will the ftdi_read_data()
begin to return bytes that actually arrived on the serial lines. The number of 0xFC/0xFF bytes in the buffer seems to correspond to the amount of time spent in bit-bang mode, but it fills quickly -- the entire 256-byte buffer will be filled in a matter of a few milliseconds.
Calling ftdi_tciflush()
after leaving bit-bang mode does not appear to have any effect; ftdi_read_data()
will still return the unwanted data first.
Is this an expected behavior of the FTDI part, or an idiosyncrasy of libftdi? Other than timing the arrival of bytes in the receive buffer, how is the user meant to distinguish the bit-bang-related bytes from the serial port bytes?
Upvotes: 1
Views: 579
Reputation: 8168
ftdi_tciflush is the wrong function to clear the RX buffer. Did you try FT_Purge ?
ftStatus = FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX); // Purge both Rx and Tx buffers
Upvotes: 0