mik3y / usb-serial-for-android

Android USB host serial driver library for CDC, FTDI, Arduino and other devices.
MIT License
4.81k stars 1.58k forks source link

Question: FTDI Purge HW Buffers #518

Closed widavies closed 1 year ago

widavies commented 1 year ago

I have a quick question here that I haven't been able to find the answer to any where else.

To me, I find it odd that RESET_PURGE_RX is used for purging write buffers and that RESET_PURGE_TX is used for read buffers. Should it be the inverse or are the TX/RX lines flipped in Android?

@Override
public void purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException {
    if (purgeWriteBuffers) {
        int result = mConnection.controlTransfer(REQTYPE_HOST_TO_DEVICE, RESET_REQUEST,
               // Purging write buffers but RESET_PURGE_RX?
                RESET_PURGE_RX, mPortNumber+1, null, 0, USB_WRITE_TIMEOUT_MILLIS);
        if (result != 0) {
            throw new IOException("Purge write buffer failed: result=" + result);
        }
    }

    if (purgeReadBuffers) {
        int result = mConnection.controlTransfer(REQTYPE_HOST_TO_DEVICE, RESET_REQUEST,
               // Purging read buffers but RESET_PURGE_TX?
                RESET_PURGE_TX, mPortNumber+1, null, 0, USB_WRITE_TIMEOUT_MILLIS);
        if (result != 0) {
            throw new IOException("Purge read buffer failed: result=" + result);
        }
    }
}

https://github.com/mik3y/usb-serial-for-android/blob/a9c835bcb07e6e49e346cc1bcd890fabac2161ba/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/FtdiSerialDriver.java#L367C1-L384C10

kai-morich commented 1 year ago

The constants are from FTDI device perspective. The functions are from Android perspective. Therefor it appears swapped.

widavies commented 1 year ago

@kai-morich Great, thanks!