topher217
Posts: 78
Joined: Wed May 30, 2018 7:21 am

USB Simple Serial Example?

Wed Sep 20, 2023 9:18 pm

Hello,

I'm just trying to get a slow basic serial communication working between the rp2040 and an Android phone. If I printf to UART via USB (e.g. the pico-example hello_world example), I can read this on the Android phone via [this usb-serial library](https://github.com/mik3y/usb-serial-for-android) for Android, but I'm not sure how to receive serial data on the rp2040. Is there anything like serialRead or what I'm used to seeing on an Arduino or the like? I'm not very familiar with the USB stack, nor do I really want to become an expert just to pass a few bits back and forth, so hoping someone can point me at something a little more simple than the [dev_lowlevel](https://github.com/raspberrypi/pico-exa ... v_lowlevel) pico-example.

I saw something laid out [here](https://blog.smittytone.net/2021/10/31/ ... o-via-usb/) for doing this, but its definitely not a complete working code, and I'd rather rely on interrupts or something less cpu intensive, and robust than an infinite loop like they provide. I see the dev_lowlevel pico example does just that, but I'm struggling to get it working. If I build and run it in gdb, I see no errors occurring on the rp2040 side, but on the Android side, using just the default example project in the [usbSerialExamples directory of ](https://github.com/mik3y/usb-serial-for-android) , it states there is "no driver" on the rp2040. If I ran any other pico-example, like the hello_world USB one, the driver appears as expected, as CdcAcm. Is there something missing in the dev_lowlevel pico-example that is failing to identify a CdcAcm driver or removes/conflicts this ability?

I'll cross post this on the [usb-serial library for Android](https://github.com/mik3y/usb-serial-for-android) to see if they can identify anything on their end that would help identify what that library is looking for specifically.

cheers!

topher217
Posts: 78
Joined: Wed May 30, 2018 7:21 am

Re: USB Simple Serial Example?

Wed Sep 20, 2023 9:57 pm

I managed to get the driver part working, as that was on the Android library side not recognizing the custom vid and pid set in dev_lowlevel.h. But now that it finds a driver, it still fails with "connection failed: No control endpoint" You can find the relevant bits of code [here](https://github.com/mik3y/usb-serial-for ... .java#L147) which check for this control endpoint. Looks like the endpoint is supposed to satisfy:

Code: Select all

if ((ep.getDirection() == UsbConstants.USB_DIR_IN) && (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT))
and I'm only actually seeing 2 for mControlInterface.getEndpointCount(). Are there supposed to be 3 endpoints or just 2?

hippy
Posts: 15203
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: USB Simple Serial Example?

Wed Sep 20, 2023 10:02 pm

topher217 wrote:
Wed Sep 20, 2023 9:18 pm
I'm not sure how to receive serial data on the rp2040. Is there anything like serialRead
There's 'char c = getchar();' which will wait for a single character to be received which is simplest to start with -

Code: Select all

int main() {
    stdio_init_all();
    while (!stdio_usb_connected()) {
        sleep_ms(100);
    }
    printf("Started\n");
    while (true) {
        char c = getchar();
        printf("You typed 0x%02x (%d)\n", c, c);
    }
}
There's also 'readline' but I don't have an example for that.

I think you are digging deeper into the USB stack than you need to.

sonnybalut
Posts: 35
Joined: Fri Apr 17, 2015 2:57 am

Re: USB Simple Serial Example?

Thu Sep 21, 2023 1:54 am

I have a couple of youtube demo, link below, note: I'm using flutter otg to create androip app connected to pico, source in the description.

https://www.youtube.com/watch?v=MfhbKYTxzuQ

https://www.youtube.com/watch?v=5T75oIce2Kg

Return to “General”