Panda381 / PicoLibSDK

Alternative extended C/C++ SDK library for Raspberry Pico and RP2040.
26 stars 7 forks source link

Add support for USB Host / CDC Devices based on FTDI chipsets (FT232) #7

Open shreeve opened 7 months ago

shreeve commented 7 months ago

This PR is not intended to be merged, but rather to review to see if support can be enabled for USB Host / CDC Devices based on FTDI chipsets (FT232).

I am communicating with the Raspberry Pi Pico W through its UART.

The Pico is connected to a USB Device that internally uses an FT232 chip. This is essentially a USB CDC class device, but it presents itself as a device with a class of 0x00 and subclass of 0x00, which aren't valid.

As a result, in this PR, instead of calling UsbHostCdcOpen, I have a new method called UsbHostCdcOpenFTDI, which just creates one interface and a pair of endpoints.

In addition, instead of calling UsbHostCdcCfg, I have a new method called UsbHostCdcCfgFTDI which does three things:

1) Sends a reset packet to the FT232 (clears RX/TX, clears DTR/CTS, sets 8N1, leaves baud alone) 2) Sends a packet to enable DTR/CTS 3) Sends a packet to set the baud rate to 9600

In addition, I added a little echo support to show if you type characters in the UART as well as small logging messages to track the progress.

This code is an initial effort to communicate with these FTDI based devices (millions exist in the world).

I think everything here should work, but it's still not 100% there. I think it's REALLY close though...

Any ideas to get it running? @Panda381 - is this the general idea? Is it close?

Thanks!

shreeve commented 7 months ago

Everything with this seems to be working as expected, except for the last bit, which is actually getting data back from the device. 😨

Here's some (sloppy) code I was using to try to read from this FTDI device, which should have properly been enumerated and setup, etc.

int main()
{
    Print("Launched\n");
    UsbHostInit();

    // wait for connection
    while (!UsbCdcIsMounted())
    {
        Print("-");
        WaitMs(100);
    }
    Print("UsbCdcIsMounted\n");

    // print device descriptor
    sUsbHostDev *cfg;
    cfg = UsbHostGetDev(1);
        Print("\r\nDevice Descriptor:\r\n");
    Print("  idVendor             0x%04X\r\n", cfg->vendor);
    Print("  idProduct            0x%04X\r\n", cfg->product);
    Print("  Vendor               0x%02X\r\n", cfg->i_manufact);
    Print("  Product              0x%02X\r\n", cfg->i_product);
    Print("  Serial               0x%02X\r\n", cfg->i_serial);
    Print("  bMaxPacketSize       0x%02X\r\n", cfg->pktmax);

    // main loop
    u8 i = 0;
    char ch;

    while (True)
    {
        if ((++i % 100) == 0) {
            while (!UsbCdcWriteReady()) {
                WaitMs(100);
                Print("!");
            }
            if (!UsbCdcWriteChar(0x06)) {
                Print("N"); // NAK
            } else {
                Print("K"); // ACK
            }
        }
        WaitMs(100);
        if (UsbCdcIsMounted()) {
            while (ch = UsbCdcReadChar()) {
                Print("%02X\n", ch);
            }
            Print(".");
        } else {
            Print("-");
        }
    }
}

The output looks like this:

Launched
UsbHostCdcInit
--UsbHostEnum(1)
-UsbHostEnum(2)
----UsbHostEnum(3)
UsbHostEnum(4)
UsbHostEnum(5)
UsbHostEnum(6)
UsbHostEnum(7)
UsbHostEnum(8)
UsbHostEnum(9)
UsbHostCdcOpenFTDI
UsbHostEnum(10)
UsbHostCfgComp(1,255)
UsbHostCdcCfgFTDI
UsbHostCdcCfgFTDIBaud(1,0)
UsbHostCdcCfgFTDILine(1,0)
UsbHostCfgComp(1,1)
UsbCdcIsMounted

Device Descriptor:
  idVendor             0x0403
  idProduct            0xCD18
  Vendor               0x01
  Product              0x02
  Serial               0x03
  bMaxPacketSize       0x08
..........................................................................................
........K.................................................................................
...................K........................................................K.............
.........................................................................................K
..........................................................................................
.......

Any thoughts on this?

Panda381 commented 7 months ago

Sorry I can't help, I don't know much about USB interface anymore (see email). :-(